From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) by sourceware.org (Postfix) with ESMTPS id 21FF53858C52; Sun, 20 Nov 2022 17:06:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 21FF53858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x62e.google.com with SMTP id kt23so23803337ejc.7; Sun, 20 Nov 2022 09:06:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cepT26NfycVWhfX0UvSc6LoOopfmBfqYQzZHPSRNqxg=; b=N5UMfa6nXEn1DNl0dneCFtSEVAyyYmVodgMKoP0iATh/jQbFWmmg8PjChwNUGxNLL5 bImJkXxNoEa0ILZ10Dw+1muWLLgDh5Jq3y1jZunk01FWfnnktryT+dnZXFcMioYp5Vyo M9u6M9CwShy5+bM5q+VNGQ7p5BvCtX1S8THvJYvgF0eLizPrS1gFxBtd/A5RAbvRe6gK ptKDD8BPckJvvP0RfvLg+Aw0bozj/Y4ZExE22LgR/2DiHrRfEFD0ePJtrJqPt8CzdYya f1HcJFWYcgbwfWOFPef3EVGwodLJWR4Yn///aTPSZv1HVqBO2fP7jq9qSrVLc5KNkQdr ka3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cepT26NfycVWhfX0UvSc6LoOopfmBfqYQzZHPSRNqxg=; b=pYTIlXzSU7Qc26y6pvsZWWsvSgPKyMJ8zRlhB7oMHmRQLJJKxf0Yy/2D1w6GiktaRc fjx6o00hOBAyNxQZhZaAmwE9k5nhpoOZuJ0jLiVHAjXM+rNjQNQ8tJOLQRS19AjbBtEk 3nSGBGQ8/DqE+Eq8tDgBHW85n6e5mk4QAojGv5cSnf6REHTWxGzonWIE+j0EpxTe5Or7 KpbF4uqPZkMhc/Q0zQNc6eV8rYpqt4cKtKRvMxGzb+yogBMriNqgxjFLyYMZFbvyW4dn p2z8+OQEsWIGxJKX2aeNvcU1OcemGKYRKeHFUBRPPW2Rem/R8JNtbgbqTeojvCwiDBG/ Apbg== X-Gm-Message-State: ANoB5pnHcveqUEmzDY+wrlj6h9rEst1FZYlVjzO1OY29wESlMFMeRDt3 fUQR5uXUWjtpN0hcSbuCzJ4= X-Google-Smtp-Source: AA0mqf4ZKLLbmHkYXlZApR2hQbpWNmFpbVMs2/2Er8HmpnWvHIXq/bR+bOqRh/fDhv8fso2S+XMiWA== X-Received: by 2002:a17:906:cedc:b0:7ad:e8dd:837c with SMTP id si28-20020a170906cedc00b007ade8dd837cmr13535652ejb.264.1668963981565; Sun, 20 Nov 2022 09:06:21 -0800 (PST) Received: from nbbrfq ([2001:871:227:81c4:d7ff:3a0e:48da:5fe3]) by smtp.gmail.com with ESMTPSA id r9-20020a17090609c900b0077b2b0563f4sm4142164eje.173.2022.11.20.09.06.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Nov 2022 09:06:20 -0800 (PST) From: Bernhard Reutner-Fischer X-Google-Original-From: Bernhard Reutner-Fischer Received: from b by nbbrfq with local (Exim 4.96) (envelope-from ) id 1ownlr-00AdNd-2W; Sun, 20 Nov 2022 18:06:19 +0100 To: gcc-patches@gcc.gnu.org Cc: Bernhard Reutner-Fischer , Bernhard Reutner-Fischer , jason@redhat.com Subject: Re: [PATCH 2/5] c++: Set the locus of the function result decl Date: Sun, 20 Nov 2022 18:06:04 +0100 Message-Id: <20221120170604.2534647-1-aldot@gcc.gnu.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221119095657.101929-1-aldot@gcc.gnu.org> References: <20221119095657.101929-1-aldot@gcc.gnu.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 +int +f() +{ + return 42; +} +int main() +{ + f(); +} +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 +-int ++bool + f() + { + return 42; +@@ -13,11 +13,11 @@ + { + f(); + } +-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