From 9715663f7db7ac57121c9a60dd0078787e274f66 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Fri, 4 Dec 2020 08:08:39 +0100 Subject: [PATCH] c++: Change __builtin_source_location to use __PRETTY_FUNCTION__ instead of __FUNCTION__ [PR80780] 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...)" 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). 2020-12-04 Jakub Jelinek 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. * testsuite/18_support/source_location/1.cc (main): Adjust for __builtin_source_location using __PRETTY_FUNCTION__-like names instead __FUNCTION__-like. * testsuite/18_support/source_location/consteval.cc (main): Likewise. --- gcc/cp/cp-gimplify.c | 2 +- gcc/testsuite/g++.dg/cpp2a/srcloc1.C | 2 +- gcc/testsuite/g++.dg/cpp2a/srcloc15.C | 10 +++++----- gcc/testsuite/g++.dg/cpp2a/srcloc17.C | 10 +++++----- gcc/testsuite/g++.dg/cpp2a/srcloc2.C | 2 +- .../testsuite/18_support/source_location/1.cc | 16 +++++++--------- .../18_support/source_location/consteval.cc | 16 +++++++--------- 7 files changed, 27 insertions(+), 31 deletions(-) diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index bafcaf5369d..8bbcf017369 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -3005,7 +3005,7 @@ fold_builtin_source_location (location_t loc) 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); } diff --git a/gcc/testsuite/g++.dg/cpp2a/srcloc1.C b/gcc/testsuite/g++.dg/cpp2a/srcloc1.C index 6e19ff77756..029a0370f3c 100644 --- a/gcc/testsuite/g++.dg/cpp2a/srcloc1.C +++ b/gcc/testsuite/g++.dg/cpp2a/srcloc1.C @@ -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 diff --git a/gcc/testsuite/g++.dg/cpp2a/srcloc15.C b/gcc/testsuite/g++.dg/cpp2a/srcloc15.C index 30e58451211..d02617e11ee 100644 --- a/gcc/testsuite/g++.dg/cpp2a/srcloc15.C +++ b/gcc/testsuite/g++.dg/cpp2a/srcloc15.C @@ -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; } diff --git a/gcc/testsuite/g++.dg/cpp2a/srcloc17.C b/gcc/testsuite/g++.dg/cpp2a/srcloc17.C index 16704d0d33e..a02ea487eef 100644 --- a/gcc/testsuite/g++.dg/cpp2a/srcloc17.C +++ b/gcc/testsuite/g++.dg/cpp2a/srcloc17.C @@ -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; } diff --git a/gcc/testsuite/g++.dg/cpp2a/srcloc2.C b/gcc/testsuite/g++.dg/cpp2a/srcloc2.C index 5ef09bbb1b1..4e0b960bf83 100644 --- a/gcc/testsuite/g++.dg/cpp2a/srcloc2.C +++ b/gcc/testsuite/g++.dg/cpp2a/srcloc2.C @@ -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 diff --git a/libstdc++-v3/testsuite/18_support/source_location/1.cc b/libstdc++-v3/testsuite/18_support/source_location/1.cc index c945aaa4d97..c9cb7434dc5 100644 --- a/libstdc++-v3/testsuite/18_support/source_location/1.cc +++ b/libstdc++-v3/testsuite/18_support/source_location/1.cc @@ -89,7 +89,7 @@ int main () VERIFY(main_sl.line() == main_sl_line); // closing paren of call VERIFY(main_sl.column() == 64); - VERIFY(main_sl_fn_name.ends_with("main"sv)); + VERIFY(main_sl_fn_name.ends_with("main()"sv)); VERIFY(main_sl_fi_name.ends_with("1.cc"sv)); std::string_view f_arg_sl_fn_name(f_arg_sl.function_name()); @@ -97,28 +97,28 @@ int main () VERIFY(f_arg_sl.line() == f_arg_sl_line); // closing paren of call VERIFY(f_arg_sl.column() == 64); - VERIFY(f_arg_sl_fn_name.ends_with("main"sv)); + VERIFY(f_arg_sl_fn_name.ends_with("main()"sv)); VERIFY(f_arg_sl_fi_name.ends_with("1.cc"sv)); std::string_view g_sl_fn_name(g_sl.function_name()); std::string_view g_sl_fi_name(g_sl.file_name()); VERIFY(g_sl.line() == g_sl_line); VERIFY(g_sl.column() == 58); // closing paren of call - VERIFY(g_sl_fn_name.ends_with("g"sv)); + VERIFY(g_sl_fn_name.ends_with("g()"sv)); VERIFY(g_sl_fi_name.ends_with("1.cc"sv)); std::string_view h_sl_fn_name(h_sl.function_name()); std::string_view h_sl_fi_name(h_sl.file_name()); VERIFY(h_sl.line() == 23); VERIFY(h_sl.column() == 58); // closing paren of call - VERIFY(h_sl_fn_name.ends_with("h"sv)); + VERIFY(h_sl_fn_name.ends_with("h()"sv)); VERIFY(h_sl_fi_name.ends_with("srcloc.h"sv)); std::string_view member_main_sl_fn_name(member_main_sl.member.function_name()); std::string_view member_main_sl_fi_name(member_main_sl.member.file_name()); VERIFY(member_main_sl.member.line() == main_sl_line); VERIFY(member_main_sl.member.column() == 64); - VERIFY(member_main_sl_fn_name.ends_with("main"sv)); + VERIFY(member_main_sl_fn_name.ends_with("main()"sv)); VERIFY(member_main_sl_fi_name.ends_with("1.cc"sv)); std::string_view member_defaulted_sl_fi_name( @@ -128,9 +128,7 @@ int main () VERIFY(member_defaulted_sl.member.line() == 46); // closing paren of constructor declaration VERIFY(member_defaulted_sl.member.column() == 25); -#if 0 VERIFY(member_defaulted_sl_fn_name.starts_with("s::s(int)"sv)); -#endif VERIFY(member_defaulted_sl_fi_name.ends_with("1.cc"sv)); std::string_view member_sl_fi_name( @@ -140,7 +138,7 @@ int main () VERIFY(member_sl.member.line() == member_sl_line); // closing brace/paren of constructor VERIFY(member_sl.member.column() == 19); - VERIFY(member_sl_fn_name.starts_with("main"sv)); + VERIFY(member_sl_fn_name.starts_with("int main()"sv)); VERIFY(member_sl_fi_name.ends_with("1.cc"sv)); std::string_view f_sl_fi_name(f_sl.file_name()); @@ -148,7 +146,7 @@ int main () VERIFY(f_sl.line() == f_sl_line); // closing paren of call VERIFY(f_sl.column() == 33); - VERIFY(f_sl_fn_name.ends_with("main"sv)); + VERIFY(f_sl_fn_name.ends_with("main()"sv)); VERIFY(f_sl_fi_name.ends_with("1.cc"sv)); return 0; diff --git a/libstdc++-v3/testsuite/18_support/source_location/consteval.cc b/libstdc++-v3/testsuite/18_support/source_location/consteval.cc index 9b137f8641d..51c8fb265c1 100644 --- a/libstdc++-v3/testsuite/18_support/source_location/consteval.cc +++ b/libstdc++-v3/testsuite/18_support/source_location/consteval.cc @@ -83,7 +83,7 @@ int main () static_assert(main_sl.line() == main_sl_line); // closing paren of call static_assert(main_sl.column() == 74); - static_assert(main_sl_fn_name.ends_with("main"sv)); + static_assert(main_sl_fn_name.ends_with("main()"sv)); static_assert(main_sl_fi_name.ends_with("consteval.cc"sv)); constexpr std::string_view f_arg_sl_fn_name(f_arg_sl.function_name()); @@ -91,28 +91,28 @@ int main () static_assert(f_arg_sl.line() == f_arg_sl_line); // closing paren of call static_assert(f_arg_sl.column() == 74); - static_assert(f_arg_sl_fn_name.ends_with("main"sv)); + static_assert(f_arg_sl_fn_name.ends_with("main()"sv)); static_assert(f_arg_sl_fi_name.ends_with("consteval.cc"sv)); constexpr std::string_view g_sl_fn_name(g_sl.function_name()); constexpr std::string_view g_sl_fi_name(g_sl.file_name()); static_assert(g_sl.line() == g_sl_line); static_assert(g_sl.column() == 58); // closing paren of call - static_assert(g_sl_fn_name.ends_with("g"sv)); + static_assert(g_sl_fn_name.ends_with("g()"sv)); static_assert(g_sl_fi_name.ends_with("consteval.cc"sv)); constexpr std::string_view h_sl_fn_name(h_sl.function_name()); constexpr std::string_view h_sl_fi_name(h_sl.file_name()); static_assert(h_sl.line() == 23); static_assert(h_sl.column() == 58); // closing paren of call - static_assert(h_sl_fn_name.ends_with("h"sv)); + static_assert(h_sl_fn_name.ends_with("h()"sv)); static_assert(h_sl_fi_name.ends_with("srcloc.h"sv)); constexpr std::string_view member_main_sl_fn_name(member_main_sl.member.function_name()); constexpr std::string_view member_main_sl_fi_name(member_main_sl.member.file_name()); static_assert(member_main_sl.member.line() == main_sl_line); static_assert(member_main_sl.member.column() == 74); - static_assert(member_main_sl_fn_name.ends_with("main"sv)); + static_assert(member_main_sl_fn_name.ends_with("main()"sv)); static_assert(member_main_sl_fi_name.ends_with("consteval.cc"sv)); constexpr std::string_view member_defaulted_sl_fi_name( @@ -122,9 +122,7 @@ int main () static_assert(member_defaulted_sl.member.line() == 36); // closing paren of constructor declaration static_assert(member_defaulted_sl.member.column() == 25); -#if 0 static_assert(member_defaulted_sl_fn_name.ends_with("s::s(int)"sv)); -#endif static_assert(member_defaulted_sl_fi_name.ends_with("consteval.cc"sv)); constexpr std::string_view member_sl_fi_name( @@ -134,7 +132,7 @@ int main () static_assert(member_sl.member.line() == member_sl_line); // closing brace/paren of constructor static_assert(member_sl.member.column() == 29); - static_assert(member_sl_fn_name.starts_with("main"sv)); + static_assert(member_sl_fn_name.starts_with("int main()"sv)); static_assert(member_sl_fi_name.ends_with("consteval.cc"sv)); constexpr std::string_view f_sl_fi_name(f_sl.file_name()); @@ -142,7 +140,7 @@ int main () static_assert(f_sl.line() == f_sl_line); // closing paren of call static_assert(f_sl.column() == 43); - static_assert(f_sl_fn_name.ends_with("main"sv)); + static_assert(f_sl_fn_name.ends_with("main()"sv)); static_assert(f_sl_fi_name.ends_with("consteval.cc"sv)); return 0; -- 2.30.2