c++: Change __builtin_source_location to use __PRETTY_FUNCTION__ instead of __FUNCTIO...
authorJakub Jelinek <jakub@redhat.com>
Fri, 4 Dec 2020 07:08:39 +0000 (08:08 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 4 Dec 2020 07:08:39 +0000 (08:08 +0100)
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).

2020-12-04  Jakub Jelinek  <jakub@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.

* 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
gcc/testsuite/g++.dg/cpp2a/srcloc1.C
gcc/testsuite/g++.dg/cpp2a/srcloc15.C
gcc/testsuite/g++.dg/cpp2a/srcloc17.C
gcc/testsuite/g++.dg/cpp2a/srcloc2.C
libstdc++-v3/testsuite/18_support/source_location/1.cc
libstdc++-v3/testsuite/18_support/source_location/consteval.cc

index bafcaf5369d4f41e1ad577228dbd8d43894af095..8bbcf01736982f062c2b7457e2b776d10b85a594 100644 (file)
@@ -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);
            }
index 6e19ff77756becf2466cdd10258b321267f6bdd8..029a0370f3c5f7eea4274d47262de0f2bf4aeb7a 100644 (file)
@@ -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
index 30e5845121117b53474e6bc400ec460f4f74895e..d02617e11eea06b5ba580d51d859e1cd05f6bcaf 100644 (file)
@@ -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;
 }
index 16704d0d33e85c286a7356368fd3e38ef0067d1d..a02ea487eef8310451bb83aa63d1b8b97568a1dc 100644 (file)
@@ -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;
 }
index 5ef09bbb1b1ba7fb3292c74b52deeb9c01cb7cfa..4e0b960bf83b511e85d7d5271ba33c4dd93fb544 100644 (file)
@@ -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
index c945aaa4d97cffda6056cb7ded3664f4a914f284..c9cb7434dc595bb6a87dcea8d6715832eaf73e39 100644 (file)
@@ -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;
index 9b137f8641d3c47d7d3ca81463ca2062419484fc..51c8fb265c15f65a8f92c3d33e96464953f68768 100644 (file)
@@ -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;