Hi Jason!

The "meh" of result-decl-plugin-test-2.C should likely be omitted,
grokdeclarator would need some changes to add richloc hints and we would not
be able to make a reliable guess what to remove precisely.
C.f. /* Check all other uses of type modifiers.  */
Furthermore it is unrelated to DECL_RESULT so not of direct interest
here. The other tests in test-2.C, f() and huh() should work though.

I don't know if it's acceptable to change ipa-pure-const to make the
missing noreturn warning more precise and emit a fixit-hint. At least it
would be a real test for the DECL_RESULT and would spare us the plugin.

HTH,

gcc/cp/ChangeLog:

        * decl.cc (start_preparsed_function): Set the result decl source
        location to the location of the typespec.
        (start_function): Likewise.

gcc/ChangeLog:

        * ipa-pure-const.cc (suggest_attribute): Add fixit-hint for the
        noreturn attribute.

gcc/testsuite/ChangeLog:

        * c-c++-common/pr68833-1.c: Adjust noreturn warning line number.
        * gcc.dg/noreturn-1.c: Likewise.
        * g++.dg/plugin/plugin.exp: Add new plugin test.
        * g++.dg/other/resultdecl-1.C: New test.
        * g++.dg/plugin/result-decl-plugin-test-1.C: New test.
        * g++.dg/plugin/result-decl-plugin-test-2.C: New test.
        * g++.dg/plugin/result_decl_plugin.C: New test.
---
 gcc/cp/decl.cc                                | 26 +++++++-
 gcc/ipa-pure-const.cc                         | 14 ++++-
 gcc/testsuite/c-c++-common/pr68833-1.c        |  2 +-
 gcc/testsuite/g++.dg/other/resultdecl-1.C     | 32 ++++++++++
 gcc/testsuite/g++.dg/plugin/plugin.exp        |  3 +
 .../g++.dg/plugin/result-decl-plugin-test-1.C | 31 ++++++++++
 .../g++.dg/plugin/result-decl-plugin-test-2.C | 59 +++++++++++++++++++
 .../g++.dg/plugin/result_decl_plugin.C        | 53 +++++++++++++++++
 gcc/testsuite/gcc.dg/noreturn-1.c             |  2 +-
 9 files changed, 218 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/other/resultdecl-1.C
 create mode 100644 gcc/testsuite/g++.dg/plugin/result-decl-plugin-test-1.C
 create mode 100644 gcc/testsuite/g++.dg/plugin/result-decl-plugin-test-2.C
 create mode 100644 gcc/testsuite/g++.dg/plugin/result_decl_plugin.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index d28889ed865..0c053b6d19f 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -17235,6 +17235,17 @@ start_preparsed_function (tree decl1, tree attrs, int 
flags)
       cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
     }
 
+  /* Set the result decl source location to the location of the typespec.  */
+  if (DECL_RESULT (decl1)
+      && DECL_TEMPLATE_INSTANTIATION (decl1)
+      && DECL_TEMPLATE_INFO (decl1)
+      && DECL_TI_TEMPLATE (decl1)
+      && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl1))
+      && DECL_RESULT (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl1))))
+      DECL_SOURCE_LOCATION (DECL_RESULT (decl1))
+       = DECL_SOURCE_LOCATION (
+           DECL_RESULT (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl1))));
+
   /* Record the decl so that the function name is defined.
      If we already have a decl for this name, and it is a FUNCTION_DECL,
      use the old decl.  */
@@ -17532,7 +17543,20 @@ start_function (cp_decl_specifier_seq *declspecs,
     gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
                             integer_type_node));
 
-  return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
+  bool ret = start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
+
+  /* decl1 might be ggc_freed here.  */
+  decl1 = current_function_decl;
+
+  tree result;
+  /* Set the result decl source location to the location of the typespec.  */
+  if (ret
+      && TREE_CODE (decl1) == FUNCTION_DECL
+      && declspecs->locations[ds_type_spec] != UNKNOWN_LOCATION
+      && (result = DECL_RESULT (decl1)) != NULL_TREE
+      && DECL_SOURCE_LOCATION (result) == input_location)
+    DECL_SOURCE_LOCATION (result) = declspecs->locations[ds_type_spec];
+  return ret;
 }
 
 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
diff --git a/gcc/ipa-pure-const.cc b/gcc/ipa-pure-const.cc
index 572a6da274f..1c80034f38d 100644
--- a/gcc/ipa-pure-const.cc
+++ b/gcc/ipa-pure-const.cc
@@ -63,6 +63,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "ipa-fnsummary.h"
 #include "symtab-thunks.h"
 #include "dbgcnt.h"
+#include "gcc-rich-location.h"
 
 /* Lattice values for const and pure functions.  Everything starts out
    being const, then may drop to pure and then neither depending on
@@ -212,7 +213,18 @@ suggest_attribute (int option, tree decl, bool 
known_finite,
   if (warned_about->contains (decl))
     return warned_about;
   warned_about->add (decl);
-  warning_at (DECL_SOURCE_LOCATION (decl),
+
+  gcc_rich_location richloc (option == OPT_Wsuggest_attribute_noreturn
+                            ? DECL_SOURCE_LOCATION (DECL_RESULT (decl))
+                            : DECL_SOURCE_LOCATION (decl));
+  if (option == OPT_Wsuggest_attribute_noreturn)
+    {
+      richloc.add_fixit_replace (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (
+                                void_type_node))));
+      richloc.add_fixit_insert_before (DECL_SOURCE_LOCATION (decl),
+                                      " __attribute__((__noreturn__)) ");
+    }
+  warning_at (&richloc,
              option,
              known_finite
              ? G_("function might be candidate for attribute %qs")
diff --git a/gcc/testsuite/c-c++-common/pr68833-1.c 
b/gcc/testsuite/c-c++-common/pr68833-1.c
index a6aefad5c98..b27b783d61a 100644
--- a/gcc/testsuite/c-c++-common/pr68833-1.c
+++ b/gcc/testsuite/c-c++-common/pr68833-1.c
@@ -15,7 +15,7 @@ f1 (const char *fmt)
 
 extern void f2 (void);
 void
-f2 (void) /* { dg-error "candidate for attribute 'noreturn'" "detect noreturn 
candidate" } */
+f2 (void) /* { dg-error "candidate for attribute 'noreturn'" "detect noreturn 
candidate" { target *-*-* } .-1 } */
 {
   __builtin_exit (0);
 }
diff --git a/gcc/testsuite/g++.dg/other/resultdecl-1.C 
b/gcc/testsuite/g++.dg/other/resultdecl-1.C
new file mode 100644
index 00000000000..e3951c339a9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/resultdecl-1.C
@@ -0,0 +1,32 @@
+/* Verify correct result decl location.  */
+// { dg-options "-fdiagnostics-generate-patch -O1 -fipa-pure-const 
-Wsuggest-attribute=noreturn" }
+//
+// foo4: rem int, add: void attribute((noreturn))
+
+
+
+int
+foo4(void)
+{ while (1); }
+
+
+#if 0
+// { dg-warning "function might be candidate for attribute .noreturn." "" { 
target *-*-* } .-6 } // the fnname is at line .-5 though
+// prune the diff path..
+/* { dg-regexp "\\-\\-\\- .*" } */
+/* { dg-regexp "\\+\\+\\+ .*" } */
+{ dg-begin-multiline-output "" }
+@@ -5,8 +5,8 @@
+ 
+ 
+ 
+-int
+-foo4(void)
++void
++ __attribute__((__noreturn__)) foo4(void)
+ { while (1); }
+ 
+ 
+{ dg-end-multiline-output "" }
+#endif
+
diff --git a/gcc/testsuite/g++.dg/plugin/plugin.exp 
b/gcc/testsuite/g++.dg/plugin/plugin.exp
index b5fb42fa77a..21d7ef0313e 100644
--- a/gcc/testsuite/g++.dg/plugin/plugin.exp
+++ b/gcc/testsuite/g++.dg/plugin/plugin.exp
@@ -80,6 +80,9 @@ set plugin_test_list [list \
          show-template-tree-color-labels.C \
          show-template-tree-color-no-elide-type.C } \
     { comment_plugin.c comments-1.C } \
+    { result_decl_plugin.C \
+         result-decl-plugin-test-1.C \
+         result-decl-plugin-test-2.C } \
 ]
 
 foreach plugin_test $plugin_test_list {
diff --git a/gcc/testsuite/g++.dg/plugin/result-decl-plugin-test-1.C 
b/gcc/testsuite/g++.dg/plugin/result-decl-plugin-test-1.C
new file mode 100644
index 00000000000..1bbdc4c8c44
--- /dev/null
+++ b/gcc/testsuite/g++.dg/plugin/result-decl-plugin-test-1.C
@@ -0,0 +1,31 @@
+/* Verify that class member functions result decl have the correct location.  
*/
+// { dg-options "-fdiagnostics-generate-patch" }
+namespace std { template < typename, typename > struct pair; }
+template < typename > struct __mini_vector
+{
+  int _M_finish;
+  const
+  unsigned long
+  __attribute__((deprecated))
+  _M_space_left()
+  { return _M_finish != 0; }
+};
+ template class __mini_vector< std::pair< long, long > >;
+ template class __mini_vector< int >;
+#if 0
+// { dg-warning "Function ._M_space_left. result location" "" { target *-*-* } 
.-5 }
+// prune the diff path..
+/* { dg-regexp "\\-\\-\\- .*" } */
+/* { dg-regexp "\\+\\+\\+ .*" } */
+{ dg-begin-multiline-output "" }
+@@ -8,7 +8,7 @@ template < typename > struct __mini_vect
+ {
+   int _M_finish;
+   const
+-  unsigned long
++  bool
+   __attribute__((deprecated))
+   _M_space_left()
+   { return _M_finish != 0; }
+{ dg-end-multiline-output "" }
+#endif
diff --git a/gcc/testsuite/g++.dg/plugin/result-decl-plugin-test-2.C 
b/gcc/testsuite/g++.dg/plugin/result-decl-plugin-test-2.C
new file mode 100644
index 00000000000..c67388195a1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/plugin/result-decl-plugin-test-2.C
@@ -0,0 +1,59 @@
+/* Verify that template functions result decl have the correct location.  */
+// { dg-options "-fdiagnostics-generate-patch" }
+
+
+
+template <class T>
+int
+f()
+{
+  return 42;
+}
+int main()
+{
+   f<int>();
+}
+unsigned long long huh(void)
+{
+  return 1ULL;
+}
+bool unsigned meh(void) {return 0;}
+
+
+
+
+#if 0
+// { dg-warning "Function .f. result location" "" { target *-*-* } .-19 }
+// { dg-warning "Function .huh. result location" "" { target *-*-* } .-11 }
+// { dg-warning "Function .meh. result location" "" { target *-*-* } .-8 }
+// prune the diff path..
+/* { dg-regexp "\\-\\-\\- .*" } */
+/* { dg-regexp "\\+\\+\\+ .*" } */
+// { dg-prune ".unsigned. specified with .bool." }
+{ dg-begin-multiline-output "" }
+@@ -4,7 +4,7 @@
+ 
+ 
+ template <class T>
+-int
++bool
+ f()
+ {
+   return 42;
+@@ -13,11 +13,11 @@
+ {
+    f<int>();
+ }
+-unsigned long long huh(void)
++bool huh(void)
+ {
+   return 1ULL;
+ }
+-bool unsigned meh(void) {return 0;}
++bool meh(void) {return 0;}
+ 
+ 
+ 
+{ dg-end-multiline-output "" }
+#endif
+// Note: f() should NOT +bbool but just +bool
diff --git a/gcc/testsuite/g++.dg/plugin/result_decl_plugin.C 
b/gcc/testsuite/g++.dg/plugin/result_decl_plugin.C
new file mode 100644
index 00000000000..9ab408e8a65
--- /dev/null
+++ b/gcc/testsuite/g++.dg/plugin/result_decl_plugin.C
@@ -0,0 +1,53 @@
+/* A plugin example that points at the location of function result decl */
+/* This file is part of GCC */
+/* { dg-options "-O" } */
+#include "gcc-plugin.h"
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tree.h"
+#include "intl.h"
+#include "diagnostic.h"
+#include "gcc-rich-location.h"
+#include "langhooks.h"
+#include "plugin-version.h"
+
+/* Callback function to invoke after GCC finishes a function definition. */
+
+static void plugin_finish_decl (void *event_data, void *data)
+{
+  tree decl = (tree) event_data;
+  if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
+    return;
+  if (MAIN_NAME_P (DECL_NAME (decl)))
+    return;
+
+  tree logical_1_t = boolean_type_node;
+  tree logical_1_n = DECL_NAME (TYPE_NAME (logical_1_t));
+  location_t result_loc = DECL_SOURCE_LOCATION (DECL_RESULT (decl));
+  gcc_rich_location richloc (result_loc);
+  richloc.add_fixit_replace (result_loc, IDENTIFIER_POINTER (logical_1_n));
+
+  warning_at (&richloc, 0, G_("Function %qs result location"),
+           IDENTIFIER_POINTER (DECL_NAME (decl)));
+}
+
+int
+plugin_init (struct plugin_name_args *plugin_info,
+             struct plugin_gcc_version *version)
+{
+  if (!plugin_default_version_check (version, &gcc_version))
+    {
+      error(G_("incompatible gcc/plugin versions"));
+      return 1;
+    }
+
+  const char *plugin_name = plugin_info->base_name;
+
+  register_callback (plugin_name, PLUGIN_FINISH_PARSE_FUNCTION,
+                     plugin_finish_decl, NULL);
+  return 0;
+}
+
+/* Announce that we are GPL.  */
+int plugin_is_GPL_compatible;
diff --git a/gcc/testsuite/gcc.dg/noreturn-1.c 
b/gcc/testsuite/gcc.dg/noreturn-1.c
index cdbfb8dd667..a713ee924fc 100644
--- a/gcc/testsuite/gcc.dg/noreturn-1.c
+++ b/gcc/testsuite/gcc.dg/noreturn-1.c
@@ -25,7 +25,7 @@ foo3(void)
 
 extern void foo4(void);
 void
-foo4(void) /* { dg-warning "candidate for attribute 'noreturn'" "detect 
noreturn candidate" } */
+foo4(void) /* { dg-warning "candidate for attribute 'noreturn'" "detect 
noreturn candidate" { target *-*-* } .-1 } */
 {
   exit(0);
 }
-- 
2.38.1

Reply via email to