Currently, if a warning references a cloned function, the name of the cloned
function will be emitted in the "In function 'xyz'" part of the diagnostic,
which users aren't supposed to see. This patch follows the DECL_ORIGIN link
to get the name of the original function, so the internal compiler details
aren't exposed.

gcc/ChangeLog:
        PR diagnostics/102061
        * langhooks.cc (lhd_print_error_function): Follow DECL_ORIGIN
        links.
        * gcc.dg/pr102061.c: New testcase.

Signed-off-by: Peter Damianov <peter0...@disroot.org>
---
v4: Address formatting nits, add comment.
v4: Rework testcase. It is now shorter and covers more, including both
"inlined from" and "in function" parts of diagnostics.

I would still appreciate a review regarding cp_print_error_function, perhaps
with more info about whether it needs adjustment or whatever circumstances it
is used in.

 gcc/langhooks.cc                |  6 ++++++
 gcc/testsuite/gcc.dg/pr102061.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr102061.c

diff --git a/gcc/langhooks.cc b/gcc/langhooks.cc
index 61f2b676256..270f7aee1c1 100644
--- a/gcc/langhooks.cc
+++ b/gcc/langhooks.cc
@@ -395,6 +395,11 @@ lhd_print_error_function (diagnostic_context *context, 
const char *file,
          else
            fndecl = current_function_decl;
 
+         // Follow DECL_ORIGIN link, in case this is a cloned function.
+         // Otherwise, we will emit names like "foo.constprop" or "bar.isra"
+         // in the diagnostic.
+         fndecl = DECL_ORIGIN (fndecl);
+
          if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
            pp_printf
              (context->printer, _("In member function %qs"),
@@ -439,6 +444,7 @@ lhd_print_error_function (diagnostic_context *context, 
const char *file,
                }
              if (fndecl)
                {
+                 fndecl = DECL_ORIGIN (fndecl);
                  expanded_location s = expand_location (*locus);
                  pp_comma (context->printer);
                  pp_newline (context->printer);
diff --git a/gcc/testsuite/gcc.dg/pr102061.c b/gcc/testsuite/gcc.dg/pr102061.c
new file mode 100644
index 00000000000..aff1062ca5a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr102061.c
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-Wall -O2" } */
+/* { dg-message "In function 'foo'" "" { target *-*-* } 0 } */
+/* { dg-message "inlined from 'bar'" "" { target *-*-* } 0 } */
+/* { dg-message "isra" "" { xfail *-*-* } 0 } */
+/* { dg-excess-errors "" } */
+
+// The warnings generated here should not contain names of clones, like
+// 'foo.isra' and 'bar.isra'
+
+// Emit warning with "In function 'foo'"
+__attribute__((noinline))
+static int foo(char* p) {
+    __builtin_strncpy(p, p, 1);
+    return 0;
+}
+
+// Emit warning with "inlined from 'bar'"
+// For some reason, this function needs to be infinite recursive
+// for the warning to show up in an isra clone.
+static int bar(char* p) {
+    __builtin_strncpy(p, p, 1);
+    bar(p);
+    return 0;
+}
+
+void baz() {
+    char c[0];
+    foo(c);
+    bar(c);
+}
-- 
2.39.2

Reply via email to