Hi! On Tue, Dec 01, 2020 at 01:03:52PM +0000, Jonathan Wakely via Gcc-patches wrote: > I mentioned in PR 80780 that a __builtin__PRETTY_FUNCTION would have > been nice, because __FUNCTION__ isn't very useful for C++, because of > overloading and namespace/class scopes. There are an unlimited number > of functions that have __FUNCTION__ == "s", e.g. "ns::s(int)" and > "ns::s()" and "another_scope::s::s<T...>(T...)" etc. > > Since __builtin_source_location() can do whatever it wants (without > needing to add __builtin__PRETTY_FUNCTION) it might be nice to use the > __PRETTY_FUNCTION__ string. JeanHeyd's tests would still need changes, > because the name would be "s::s(void*)" not "s::s" but that still > seems better for users.
When I've added template tests for the previous patch, I have noticed that the current __builtin_source_location behavior is not really __FUNCTION__, just close, because e.g. in function template __FUNCTION__ is still "bar" but __builtin_source_location gave "bar<0>". Anyway, this patch implements above request to follow __PRETTY_FUNCTION__ (on top of the earlier posted patch). Tested on x86_64-linux, ok for trunk if it passes full bootstrap/regtest on x86_64-linux? 2020-12-02 Jakub Jelinek <ja...@redhat.com> PR c++/80780 * cp-gimplify.c (fold_builtin_source_location): Use 2 instead of 0 as last argument to cxx_printable_name. * g++.dg/cpp2a/srcloc1.C (quux): Use __PRETTY_FUNCTION__ instead of function. * g++.dg/cpp2a/srcloc2.C (quux): Likewise. * g++.dg/cpp2a/srcloc15.C (S::S): Likewise. (bar): Likewise. Adjust expected column. * g++.dg/cpp2a/srcloc17.C (S::S): Likewise. (bar): Likewise. Adjust expected column. --- gcc/cp/cp-gimplify.c.jj 2020-12-02 12:26:21.596922205 +0100 +++ gcc/cp/cp-gimplify.c 2020-12-02 13:16:48.599284262 +0100 @@ -3001,7 +3001,7 @@ fold_builtin_source_location (location_t const char *name = ""; if (current_function_decl) - name = cxx_printable_name (current_function_decl, 0); + name = cxx_printable_name (current_function_decl, 2); val = build_string_literal (strlen (name) + 1, name); } --- gcc/testsuite/g++.dg/cpp2a/srcloc1.C.jj 2020-07-28 15:39:10.012756173 +0200 +++ gcc/testsuite/g++.dg/cpp2a/srcloc1.C 2020-12-02 13:17:39.010708329 +0100 @@ -88,7 +88,7 @@ quux () const char *file1 = source_location::current ().file_name (); const char *file2 = __FILE__; const char *function1 = source_location::current ().function_name (); - const char *function2 = __FUNCTION__; + const char *function2 = __PRETTY_FUNCTION__; int line1 = source_location::current ().line (); int line2 = __LINE__ - 1; int column --- gcc/testsuite/g++.dg/cpp2a/srcloc2.C.jj 2020-07-28 15:39:10.012756173 +0200 +++ gcc/testsuite/g++.dg/cpp2a/srcloc2.C 2020-12-02 13:17:51.851562576 +0100 @@ -92,7 +92,7 @@ quux () const char *file1 = source_location::current ().file_name (); const char *file2 = __FILE__; const char *function1 = source_location::current ().function_name (); - const char *function2 = __FUNCTION__; + const char *function2 = __PRETTY_FUNCTION__; int line1 = source_location::current ().line (); int line2 = __LINE__ - 1; int column --- gcc/testsuite/g++.dg/cpp2a/srcloc15.C.jj 2020-12-02 12:48:20.592828477 +0100 +++ gcc/testsuite/g++.dg/cpp2a/srcloc15.C 2020-12-02 13:20:46.768577089 +0100 @@ -44,12 +44,12 @@ struct S { source_location loc = source_location::current (); constexpr S (int l, source_location loc = source_location::current ()) - : func(__FUNCTION__), line(l), loc(loc) + : func(__PRETTY_FUNCTION__), line(l), loc(loc) {} constexpr S (double) - : func(__FUNCTION__), line(__LINE__) - // ^ column 38 + : func(__PRETTY_FUNCTION__), line(__LINE__) + // ^ column 45 {} }; @@ -73,7 +73,7 @@ bar () // ^ column 49 const source_location *d[3] = { &a, &b, &c }; const char *file1 = __FILE__; - const char *function1 = __FUNCTION__; + const char *function1 = __PRETTY_FUNCTION__; for (int j = 0; j < 3; j++) { int i= 0; @@ -104,7 +104,7 @@ bar () return false; if (e.loc.column () != 9) return false; - if (f.loc.column () != 38) + if (f.loc.column () != 45) return false; return true; } --- gcc/testsuite/g++.dg/cpp2a/srcloc17.C.jj 2020-12-02 12:58:26.113904280 +0100 +++ gcc/testsuite/g++.dg/cpp2a/srcloc17.C 2020-12-02 13:21:11.943291326 +0100 @@ -46,12 +46,12 @@ struct S { source_location loc = source_location::current (); constexpr S (int l, source_location loc = source_location::current ()) - : func(__FUNCTION__), line(l), loc(loc) + : func(__PRETTY_FUNCTION__), line(l), loc(loc) {} constexpr S (double) - : func(__FUNCTION__), line(__LINE__) - // ^ column 38 + : func(__PRETTY_FUNCTION__), line(__LINE__) + // ^ column 45 {} }; @@ -76,7 +76,7 @@ bar () // ^ column 48 const source_location *d[3] = { &a, &b, &c }; const char *file1 = __FILE__; - const char *function1 = b.function_name (); + const char *function1 = __PRETTY_FUNCTION__; for (int j = 0; j < 3; j++) { int i= 0; @@ -107,7 +107,7 @@ bar () return false; if (e.loc.column () != 8) return false; - if (f.loc.column () != 38) + if (f.loc.column () != 45) return false; return true; } Jakub