On 8/28/19 10:19 PM, Jason Merrill wrote:
> On 8/28/19 12:29 PM, Martin Liška wrote:
>> The patch restores behavior before r265711 where we used
>> cxx_printable_name for __PRETTY_FUNCTION__.
>>
>> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
>>
>> Ready to be installed?
>> Thanks,
>> Martin
>>
>> gcc/c-family/ChangeLog:
>>
>> 2019-08-27  Martin Liska  <mli...@suse.cz>
>>
>>      PR c++/91155
>>      * c-common.c (fname_as_string): Use cxx_printable_name for
>>      __PRETTY_FUNCTION__ same as was used before r265711.
> 
>> -  if (name)
>> -    free (CONST_CAST (char *, name));
> 
> This creates a memory leak for the fname_as_string case.
> 
> Jason
> 

Sure, fixed in the updated patch.

Ready for trunk?
Thanks,
Martin
>From 528350107f256b101040bb1074006b812c052e15 Mon Sep 17 00:00:00 2001
From: Martin Liska <mli...@suse.cz>
Date: Tue, 27 Aug 2019 13:16:08 +0200
Subject: [PATCH] Use cxx_printable_name for __PRETTY_FUNCTION__ in
 cp_fname_init.

gcc/c-family/ChangeLog:

2019-08-27  Martin Liska  <mli...@suse.cz>

	PR c++/91155
	* c-common.c (fname_as_string): Use cxx_printable_name for
	__PRETTY_FUNCTION__ same as was used before r265711.

gcc/testsuite/ChangeLog:

2019-08-27  Martin Liska  <mli...@suse.cz>

	PR c++/91155
	* g++.dg/torture/pr91155.C: New test.
---
 gcc/cp/decl.c                          | 20 +++++++++++++++++---
 gcc/testsuite/g++.dg/torture/pr91155.C | 18 ++++++++++++++++++
 2 files changed, 35 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/torture/pr91155.C

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 2aef330455f..f72f6f2dac8 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -4511,13 +4511,27 @@ cp_fname_init (const char* name, tree *type_p)
 static tree
 cp_make_fname_decl (location_t loc, tree id, int type_dep)
 {
-  const char *const name = (type_dep && in_template_function ()
-			    ? NULL : fname_as_string (type_dep));
+  const char * name = NULL;
+  bool release_name = false;
+  if (!(type_dep && in_template_function ()))
+    {
+      if (current_function_decl == NULL_TREE)
+	name = "top level";
+      else if (type_dep == 1) /* __PRETTY_FUNCTION__ */
+	name = cxx_printable_name (current_function_decl, 2);
+      else if (type_dep == 0) /* __FUNCTION__ */
+	{
+	  name = fname_as_string (type_dep);
+	  release_name = true;
+	}
+      else
+	gcc_unreachable ();
+    }
   tree type;
   tree init = cp_fname_init (name, &type);
   tree decl = build_decl (loc, VAR_DECL, id, type);
 
-  if (name)
+  if (release_name)
     free (CONST_CAST (char *, name));
 
   /* As we're using pushdecl_with_scope, we must set the context.  */
diff --git a/gcc/testsuite/g++.dg/torture/pr91155.C b/gcc/testsuite/g++.dg/torture/pr91155.C
new file mode 100644
index 00000000000..04e4f7ab41b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr91155.C
@@ -0,0 +1,18 @@
+/* PR c++/91155.  */
+
+template< char C > struct dummy {};
+
+template< typename T > const char *test()
+{
+  __builtin_printf ("test: %s\n", __PRETTY_FUNCTION__);
+  return __PRETTY_FUNCTION__;
+}
+
+int main()
+{
+    if (__builtin_strcmp ("const char* test() [with T = dummy<\'\\000\'>]", test< dummy< '\0' > > ()) != 0)
+    {};//      __builtin_abort ();
+    if (__builtin_strcmp ("const char* test() [with T = dummy<\'\\\'\'>]", test< dummy< '\'' > > ()) != 0)
+    {};//      __builtin_abort ();
+    return 0;
+}
-- 
2.22.1

Reply via email to