From: Jason Merrill <jason@redhat.com>
To: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>,
gcc-patches@gcc.gnu.org
Cc: Bernhard Reutner-Fischer <aldot@gcc.gnu.org>
Subject: Re: [PATCH 2/5] c++: Set the locus of the function result decl
Date: Tue, 22 Nov 2022 15:25:35 -0500 [thread overview]
Message-ID: <3d2485a6-ca86-aece-6c28-f7cc8547e8d3@redhat.com> (raw)
In-Reply-To: <20221120170604.2534647-1-aldot@gcc.gnu.org>
[-- Attachment #1: Type: text/plain, Size: 921 bytes --]
On 11/20/22 12:06, Bernhard Reutner-Fischer wrote:
> 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.
The main problem I see with that change is that the syntax of the fixit
might be wrong for non-C-family front-ends.
Here's a version of the patch that fixes template/method handling, and
adjusts -Waggregate-return as well:
[-- Attachment #2: 0001-c-Set-the-locus-of-the-function-result-decl.patch --]
[-- Type: text/x-patch, Size: 7595 bytes --]
From 5075d2ac12f655f8f83f6f3be27e2c1141e1ce99 Mon Sep 17 00:00:00 2001
From: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
Date: Sun, 20 Nov 2022 18:06:04 +0100
Subject: [PATCH] c++: Set the locus of the function result decl
To: gcc-patches@gcc.gnu.org
gcc/cp/ChangeLog:
* decl.cc (grokdeclarator): Build RESULT_DECL.
(start_preparsed_function): Copy location from template.
gcc/ChangeLog:
* function.cc (init_function_start): Use DECL_RESULT location
for -Waggregate-return warning.
* 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/diagnostic/return-type-loc1.C: New test.
* g++.dg/other/resultdecl-1.C: New test.
Co-authored-by: Jason Merrill <jason@redhat.com>
---
gcc/cp/decl.cc | 26 +++++++++++++--
gcc/function.cc | 3 +-
gcc/ipa-pure-const.cc | 14 +++++++-
gcc/testsuite/c-c++-common/pr68833-1.c | 2 +-
.../g++.dg/diagnostic/return-type-loc1.C | 20 ++++++++++++
gcc/testsuite/g++.dg/other/resultdecl-1.C | 32 +++++++++++++++++++
gcc/testsuite/gcc.dg/noreturn-1.c | 2 +-
7 files changed, 93 insertions(+), 6 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/diagnostic/return-type-loc1.C
create mode 100644 gcc/testsuite/g++.dg/other/resultdecl-1.C
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 544efdc9914..2c5cd930e0a 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -14774,6 +14774,18 @@ grokdeclarator (const cp_declarator *declarator,
else if (constinit_p)
DECL_DECLARED_CONSTINIT_P (decl) = true;
}
+ else if (TREE_CODE (decl) == FUNCTION_DECL)
+ {
+ location_t loc = smallest_type_location (declspecs);
+ if (loc != UNKNOWN_LOCATION)
+ {
+ tree restype = TREE_TYPE (TREE_TYPE (decl));
+ tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
+ DECL_ARTIFICIAL (resdecl) = 1;
+ DECL_IGNORED_P (resdecl) = 1;
+ DECL_RESULT (decl) = resdecl;
+ }
+ }
/* Record constancy and volatility on the DECL itself . There's
no need to do this when processing a template; we'll do this
@@ -17328,9 +17340,19 @@ start_preparsed_function (tree decl1, tree attrs, int flags)
if (DECL_RESULT (decl1) == NULL_TREE)
{
- tree resdecl;
+ /* In a template instantiation, copy the return type location. When
+ parsing, the location will be set in grokdeclarator. */
+ location_t loc = input_location;
+ if (DECL_TEMPLATE_INSTANTIATION (decl1)
+ && !DECL_CXX_CONSTRUCTOR_P (decl1)
+ && !DECL_CXX_DESTRUCTOR_P (decl1))
+ {
+ tree tmpl = template_for_substitution (decl1);
+ tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl));
+ loc = DECL_SOURCE_LOCATION (res);
+ }
- resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
+ tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
DECL_ARTIFICIAL (resdecl) = 1;
DECL_IGNORED_P (resdecl) = 1;
DECL_RESULT (decl1) = resdecl;
diff --git a/gcc/function.cc b/gcc/function.cc
index 9c8773bbc59..dc333c27e92 100644
--- a/gcc/function.cc
+++ b/gcc/function.cc
@@ -4997,7 +4997,8 @@ init_function_start (tree subr)
/* Warn if this value is an aggregate type,
regardless of which calling convention we are using for it. */
if (AGGREGATE_TYPE_P (TREE_TYPE (DECL_RESULT (subr))))
- warning (OPT_Waggregate_return, "function returns an aggregate");
+ warning_at (DECL_SOURCE_LOCATION (DECL_RESULT (subr)),
+ OPT_Waggregate_return, "function returns an aggregate");
}
/* Expand code to verify the stack_protect_guard. This is invoked at
diff --git a/gcc/ipa-pure-const.cc b/gcc/ipa-pure-const.cc
index 572a6da274f..8f6e8f63d91 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 (TYPE_IDENTIFIER (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/diagnostic/return-type-loc1.C b/gcc/testsuite/g++.dg/diagnostic/return-type-loc1.C
new file mode 100644
index 00000000000..f96179ed4d5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/diagnostic/return-type-loc1.C
@@ -0,0 +1,20 @@
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -Waggregate-return }
+
+struct B { int i,j; };
+
+template <class T>
+struct A
+{
+ template <class U>
+ U // { dg-warning "aggregate" }
+ f() { return {}; }
+};
+
+int main()
+{
+ A<int>().f<B>(); // { dg-warning "aggregate" }
+}
+
+B // { dg-warning "aggregate" }
+g() { return {}; }
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/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.31.1
next prev parent reply other threads:[~2022-11-22 20:25 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-12 23:45 [PATCH 0/5] function result decl location; type demotion Bernhard Reutner-Fischer
2022-11-12 23:45 ` [PATCH 1/5] c: Set the locus of the function result decl Bernhard Reutner-Fischer
2022-11-14 21:25 ` Joseph Myers
2022-11-12 23:45 ` [PATCH 2/5] c++: " Bernhard Reutner-Fischer
2022-11-15 23:52 ` Jason Merrill
2022-11-17 8:56 ` Bernhard Reutner-Fischer
2022-11-17 14:53 ` Jason Merrill
2022-11-17 19:02 ` Bernhard Reutner-Fischer
2022-11-17 23:52 ` Jason Merrill
2022-11-18 10:49 ` Bernhard Reutner-Fischer
2022-11-18 16:06 ` Jason Merrill
2022-11-18 18:26 ` Bernhard Reutner-Fischer
2022-11-19 9:56 ` Bernhard Reutner-Fischer
2022-11-20 17:06 ` Bernhard Reutner-Fischer
2022-11-22 20:25 ` Jason Merrill [this message]
2022-11-23 15:28 ` Jason Merrill
2022-12-02 19:30 ` Jason Merrill
2022-12-02 19:55 ` Bernhard Reutner-Fischer
2022-11-12 23:45 ` [PATCH 3/5] Fortran: Narrow return types [PR78798] Bernhard Reutner-Fischer
2022-11-13 10:13 ` Janne Blomqvist
2022-11-13 10:39 ` Bernhard Reutner-Fischer
2022-11-13 20:29 ` Harald Anlauf
2022-11-13 20:29 ` Harald Anlauf
2022-11-13 21:50 ` Bernhard Reutner-Fischer
2023-05-10 16:47 ` [PATCH v2] " Bernhard Reutner-Fischer
2023-05-14 12:27 ` Mikael Morin
2023-05-14 13:04 ` Thomas Koenig
2023-05-14 15:24 ` Bernhard Reutner-Fischer
2023-05-14 18:06 ` Mikael Morin
2023-05-18 19:52 ` Bernhard Reutner-Fischer
2022-11-12 23:45 ` [PATCH 4/5] value-range: Add as_string diagnostics helper Bernhard Reutner-Fischer
2022-11-12 23:55 ` Andrew Pinski
2022-11-17 3:30 ` Jeff Law
2022-11-12 23:45 ` [PATCH 5/5] gimple: Add pass to note possible type demotions; IPA pro/demotion; DO NOT MERGE Bernhard Reutner-Fischer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3d2485a6-ca86-aece-6c28-f7cc8547e8d3@redhat.com \
--to=jason@redhat.com \
--cc=aldot@gcc.gnu.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=rep.dot.nop@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).