public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/99672] New: std::source_location yield different column numbers between free function and template functions
@ 2021-03-19 15:25 hewillk at gmail dot com
  2021-03-19 16:36 ` [Bug c++/99672] " jakub at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: hewillk at gmail dot com @ 2021-03-19 15:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99672

            Bug ID: 99672
           Summary: std::source_location yield different column numbers
                    between free function and template functions
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hewillk at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/8T3MGv

====

#include <iostream>
#include <source_location>

auto g(auto...) {
std::cout << std::source_location::current().column() << "\n";
}

auto f() {
std::cout << std::source_location::current().column() << "\n";
}

int main() {
g();
f();
std::cout << std::source_location::current().column() << "\n";
}

====

GCC-trunk output 43, 44, and 44, but the first one should be 44.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/99672] std::source_location yield different column numbers between free function and template functions
  2021-03-19 15:25 [Bug c++/99672] New: std::source_location yield different column numbers between free function and template functions hewillk at gmail dot com
@ 2021-03-19 16:36 ` jakub at gcc dot gnu.org
  2021-03-19 16:55 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-03-19 16:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99672

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Adjusted testcase without headers.

namespace std {
  struct source_location {
    struct __impl {
      const char *_M_file_name;
      const char *_M_function_name;
      unsigned int _M_line, _M_column;
    };
    const __impl *__ptr;
    constexpr source_location () : __ptr (nullptr) {}
    static consteval source_location
    current (const void *__p = __builtin_source_location ()) {
      source_location __ret;
      __ret.__ptr = static_cast <const __impl *> (__p);
      return __ret;
    }
    constexpr const char *file_name () const {
      return __ptr ? __ptr->_M_file_name : "";
    }
    constexpr const char *function_name () const {
      return __ptr ? __ptr->_M_function_name : "";
    }
    constexpr unsigned line () const {
      return __ptr ? __ptr->_M_line : 0;
    }
    constexpr unsigned column () const {
      return __ptr ? __ptr->_M_column : 0;
    }
  };
}

int g(auto...) {
return std::source_location::current().column();
}

int f() {
return std::source_location::current().column();
}

int a = g();
int b = f();

I think the standard doesn't specify anything about what exactly the column
should be, so using different columns isn't standard violation.

In one case we have more detailed locus with ranges:
pr99672.C:32:37 start: pr99672.C:32:8 finish: pr99672.C:32:38
i.e. caret on column 37 (the opening paren), start at column 8, end at column
38 (the closing paren), in the other case just a simple location of the closing
paren.
The thing is that the location for the default argument is set from
input_location by break_out_target_exprs called from convert_default_arg.
When in template, this is called from tsubst* which sets input_location
temporarily from the CALL_EXPR locus and is the detailed one, which was created
by cp_parser_postfix_expression:
7728                if (close_paren_loc != UNKNOWN_LOCATION)
7729                  {
7730                    location_t combined_loc = make_location
(token->location,
7731                                                             start_loc,
7732                                                            
close_paren_loc);
7733                    postfix_expression.set_location (combined_loc);
7734                  }

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/99672] std::source_location yield different column numbers between free function and template functions
  2021-03-19 15:25 [Bug c++/99672] New: std::source_location yield different column numbers between free function and template functions hewillk at gmail dot com
  2021-03-19 16:36 ` [Bug c++/99672] " jakub at gcc dot gnu.org
@ 2021-03-19 16:55 ` jakub at gcc dot gnu.org
  2021-03-21  9:18 ` hewillk at gmail dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-03-19 16:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99672

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 50434
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50434&action=edit
gcc11-pr99672.patch

Untested fix.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/99672] std::source_location yield different column numbers between free function and template functions
  2021-03-19 15:25 [Bug c++/99672] New: std::source_location yield different column numbers between free function and template functions hewillk at gmail dot com
  2021-03-19 16:36 ` [Bug c++/99672] " jakub at gcc dot gnu.org
  2021-03-19 16:55 ` jakub at gcc dot gnu.org
@ 2021-03-21  9:18 ` hewillk at gmail dot com
  2021-03-25 20:36 ` cvs-commit at gcc dot gnu.org
  2021-03-25 20:38 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: hewillk at gmail dot com @ 2021-03-21  9:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99672

--- Comment #3 from 康桓瑋 <hewillk at gmail dot com> ---
I think the issue of line() function also needs to do a patch.

https://godbolt.org/z/q8EPnG
====

constexpr int g(auto...) {
return std::source_location::current( // < opening paren line number
).line();
}

constexpr int f() {
return std::source_location::current( 
).line();                             // < closing paren line number
}

====

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/99672] std::source_location yield different column numbers between free function and template functions
  2021-03-19 15:25 [Bug c++/99672] New: std::source_location yield different column numbers between free function and template functions hewillk at gmail dot com
                   ` (2 preceding siblings ...)
  2021-03-21  9:18 ` hewillk at gmail dot com
@ 2021-03-25 20:36 ` cvs-commit at gcc dot gnu.org
  2021-03-25 20:38 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-03-25 20:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99672

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:2132a36370e282d8c0ed0c97e5bfb952e23dbfa1

commit r11-7836-g2132a36370e282d8c0ed0c97e5bfb952e23dbfa1
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Mar 25 21:35:11 2021 +0100

    c++: Fix source_location inconsistency between calls from templates and
non-templates [PR99672]

    The srcloc19.C testcase shows inconsistency in
    std::source_location::current() locations between calls from
    templates and non-templates.  The location used by
__builtin_source_location
    comes in both cases from input_location which is set on it by bot_manip
    when handling the default argument, called during finish_call_expr.
    The problem is that in templates that input_location comes from the
    CALL_EXPR we built earlier and that has the combined locus with
    range between first character of the function name and closing paren
    with caret on the opening paren, so something printed as caret as:
    foobar ();
    ~~~~~~^~
    But outside of templates, finish_call_expr is called when input_location
    is just the closing paren token, i.e.
    foobar ();
            ^
    and only after that returns we create the combined location and set
    the CALL_EXPR location to that.  So, it means
std::source_location::current()
    reports in templates the column of opening (, while outside of templates
    closing ).

    The following patch makes it consistent by creating the combined location
    already before calling finish_call_expr and temporarily overriding
    input_location to that.

    2021-03-25  Jakub Jelinek  <jakub@redhat.com>

            PR c++/99672
            * parser.c (cp_parser_postfix_expression): For calls, create
            combined_loc and temporarily set input_location to it before
            calling finish_call_expr.

            * g++.dg/concepts/diagnostic2.C: Adjust expected caret line.
            * g++.dg/cpp1y/builtin_location.C (f4, n6): Move #line directives
            to match locus changes.
            * g++.dg/cpp2a/srcloc1.C: Adjust expected column numbers.
            * g++.dg/cpp2a/srcloc2.C: Likewise.
            * g++.dg/cpp2a/srcloc15.C: Likewise.
            * g++.dg/cpp2a/srcloc16.C: Likewise.
            * g++.dg/cpp2a/srcloc19.C: New test.
            * g++.dg/modules/adhoc-1_b.C: Adjust expected column numbers
            and caret line.
            * g++.dg/modules/macloc-1_c.C: Adjust expected column numbers.
            * g++.dg/modules/macloc-1_d.C: Likewise.
            * g++.dg/plugin/diagnostic-test-expressions-1.C: Adjust expected
            caret line.

            * testsuite/18_support/source_location/consteval.cc (main): Adjust
            expected column numbers.
            * testsuite/18_support/source_location/1.cc (main): Likewise.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/99672] std::source_location yield different column numbers between free function and template functions
  2021-03-19 15:25 [Bug c++/99672] New: std::source_location yield different column numbers between free function and template functions hewillk at gmail dot com
                   ` (3 preceding siblings ...)
  2021-03-25 20:36 ` cvs-commit at gcc dot gnu.org
@ 2021-03-25 20:38 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-03-25 20:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99672

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-03-25 20:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-19 15:25 [Bug c++/99672] New: std::source_location yield different column numbers between free function and template functions hewillk at gmail dot com
2021-03-19 16:36 ` [Bug c++/99672] " jakub at gcc dot gnu.org
2021-03-19 16:55 ` jakub at gcc dot gnu.org
2021-03-21  9:18 ` hewillk at gmail dot com
2021-03-25 20:36 ` cvs-commit at gcc dot gnu.org
2021-03-25 20:38 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).