]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
++: Fix up __PRETTY_FUNCTION__ for -fexec-charset= [PR122228]
authorJakub Jelinek <jakub@redhat.com>
Mon, 13 Oct 2025 19:36:47 +0000 (21:36 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 13 Oct 2025 19:36:47 +0000 (21:36 +0200)
When working on reflection, I've noticed that while we correctly translate
__FUNCTION__ content into the execution charset, for C++ we don't translate
__PRETTY_FUNCTION__ content and leave it in the SOURCE_CHARSET encoding:

const char *
file ()
{
  return __FILE__;
}

const char *
func ()
{
  return __func__;
}

const char *
function ()
{
  return __FUNCTION__;
}

const char *
pretty_function ()
{
  return __PRETTY_FUNCTION__;
}
./cc1 -quiet -fexec-charset=IBM1047 /tmp/0.C -o - | grep string
        .string "a\243\224\227a\360K\303"
        .string "\206\244\225\203"
        .string "\206\244\225\203\243\211\226\225"
        .string "\227\231\205\243\243\250m\206\244\225\203\243\211\226\225"
./cc1plus -quiet -fexec-charset=IBM1047 /tmp/0.C -o - | grep string
        .string "a\243\224\227a\360K\303"
        .string "\206\244\225\203"
        .string "\206\244\225\203\243\211\226\225"
        .string "const char* pretty_function()"

The following patch fixes that.

2025-10-13  Jakub Jelinek  <jakub@redhat.com>

PR c++/122228
* decl.cc (cp_make_fname_decl): When not using fname_as_decl,
attempt to translate name into ordinary literal encoding.

* g++.dg/cpp1y/func_constexpr3.C: New test.

gcc/cp/decl.cc
gcc/testsuite/g++.dg/cpp1y/func_constexpr3.C [new file with mode: 0644]

index 05791076d87802b14cd0b1ef2c7b3e8cbf344c46..2089e4c21accf1e055c1b7b70555cacc5b97286e 100644 (file)
@@ -5806,6 +5806,23 @@ cp_make_fname_decl (location_t loc, tree id, int type_dep)
          name = cxx_printable_name (current_function_decl, 2);
        }
 
+      if (!release_name)
+       {
+         cpp_string cstr = { 0, 0 }, strname;
+         size_t len = strlen (name) + 3; /* Two for '"'s.  One for NULL.  */
+         char *namep = XNEWVEC (char, len);
+         snprintf (namep, len, "\"%s\"", name);
+         strname.text = (unsigned char *) namep;
+         strname.len = len - 1;
+         if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
+           {
+             name = (const char *) cstr.text;
+             release_name = true;
+           }
+
+         XDELETEVEC (namep);
+       }
+
       size_t length = strlen (name);
       domain = build_index_type (size_int (length));
       init = build_string (length + 1, name);
diff --git a/gcc/testsuite/g++.dg/cpp1y/func_constexpr3.C b/gcc/testsuite/g++.dg/cpp1y/func_constexpr3.C
new file mode 100644 (file)
index 0000000..f2963f1
--- /dev/null
@@ -0,0 +1,6 @@
+// PR c++/122228
+// { dg-do compile { target c++11 } }
+// { dg-require-iconv "IBM1047" }
+// { dg-options "-fexec-charset=IBM1047 -std=c++11" }
+
+#include "func_constexpr.C"