From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id C3CBE3858D1E for ; Wed, 20 Apr 2022 17:20:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C3CBE3858D1E Received: by mail-ej1-x633.google.com with SMTP id y20so4919195eju.7 for ; Wed, 20 Apr 2022 10:20:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version; bh=FCUBv9Ny8a/GibBIv04tqztYcblW3c6bVGbBfd6B/lI=; b=FbSTMBFweDINzmv1+LdufTIxhbecRo7XmTtUucv7POXieTJZlM5NhcJ75fTs13HW+c oSmkfTCLC25OYL7duVZQY32lyjbY7sqyNAvZjirBwz9NmTSY5uUWt1zGtH4O5s+zas2S XxVxTAfJ2Qu/FCR61BV8F/Ihpk6SDMcCU67JxAi9xDbnPeSA6Mc5DnBRRYU31KjB+rVj ZfMQEHwZIRyMrdAnNjyb3hj1eryV1eciFH+FqN8KaYohjyjLLsKSwXu9Ij8UDCNjok4M 2erNTMnGzsDDpCnbWydEG9CCVpC6rIxdYoQbJhg8uhld5qq8sZ4ZLwOtxppCn2gJKizV JG3w== X-Gm-Message-State: AOAM530XK6JvND7XcCsXmZjZAkqE2FMWqEGhiEPtPJinaozwfmlp7AXC GieDGqvM6TUj7uonpe2r8Vo= X-Google-Smtp-Source: ABdhPJxThlSfjV3YkDreRqNbm12rF56igDUKUEZBx8i7FoU0yu1ctxTD84ceJdyTTueGQ/Zo/BK6AA== X-Received: by 2002:a17:907:1686:b0:6f0:c0f:2773 with SMTP id hc6-20020a170907168600b006f00c0f2773mr2759394ejc.445.1650475227828; Wed, 20 Apr 2022 10:20:27 -0700 (PDT) Received: from nbbrfq (80-110-214-113.static.upcbusiness.at. [80.110.214.113]) by smtp.gmail.com with ESMTPSA id gq5-20020a170906e24500b006e87644f2f7sm6859614ejb.38.2022.04.20.10.20.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 10:20:27 -0700 (PDT) Date: Wed, 20 Apr 2022 19:20:24 +0200 From: Bernhard Reutner-Fischer To: gcc-help@gcc.gnu.org Cc: Bernhard Reutner-Fischer , jason@redhat.com Subject: C++: missing result_decl loc of (member) functions Message-ID: <20220420192024.4353ac8c@nbbrfq> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/woVI+dNnQKnfu7we+B3APdg" X-Spam-Status: No, score=-10.5 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Apr 2022 17:20:32 -0000 --MP_/woVI+dNnQKnfu7we+B3APdg Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi! I'm having a hard time to set the correct location of the result decl of member functions in C++. Any hint on where to best fix the missing locus? TIA for any hint (or a fix :). Long version: Neither the C nor the C++ frontend seem to have a working/proper location for the RESULT_DECL of functions. This makes it impossible to have diagnostics point at the return type of a function and hence it is impossible to have fixit-replace hints for the return types. [There is a gfortran bug that asks to use narrower return types for functions in the FE and i meant to get me a C++ warning that would point out all affected spots in the fortran FE..] Adding the correct location to the result decl of a function in the C frontend was easy, see attached. Same for the C++ FE for normal functions. These are covered in the attached sample-testcase return-narrow.cc =C2=B9) and work well. I'm attaching a single patch for the C and C++ FE, knowing that they are to be sent separately upon real submission and probably only once stage1 reopens. But i cannot seem to find a proper spot to handle member functions (if that's the correct term for them). These are preparsed functions AFAICS and i'd need a place where i can get at the declspecs[ds_type_spec] and the function is parsed "enough" that the return type is determined, i think. I'm attaching the sample-testcase return-narrow-2.cc which, with a pristine compiler, gives no location for the result decl at all: ---8<--- $ XXX=3D1 ../gcc/xg++ -B../gcc -c -o return-narrow.o return-narrow-2.cc -fd= iagnostics-color=3Dalways=20 return-narrow-2.cc: In member function =E2=80=98long unsigned int __mini_ve= ctor< >::_M_space_left()=E2=80=99: return-narrow-2.cc:7:3: warning: result decl locus sample 7 | { return _M_finish !=3D 0; } | ^ | the return type return-narrow-2.cc: In instantiation of =E2=80=98long unsigned int __mini_v= ector< >::_M_space_left() [with =3D std::pair]=E2=80=99: return-narrow-2.cc:9:17: required from here return-narrow-2.cc:6:3: warning: result decl locus sample 6 | _M_space_left() | ^~~~~~~~~~~~~ | the return type return-narrow-2.cc: In instantiation of =E2=80=98long unsigned int __mini_v= ector< >::_M_space_left() [with =3D int]=E2=80=99: return-narrow-2.cc:10:17: required from here return-narrow-2.cc:6:3: warning: result decl locus sample 6 | _M_space_left() | ^~~~~~~~~~~~~ | the return type ---8<--- I'd like to have the result_decl of _M_space_left point to the=20 type unsigned long on line 5, which i think would be the correct type of that memberfn: 1 namespace std { template < typename, typename > struct pair; } 2 template < typename > struct __mini_vector 3 { 4 int _M_finish; 5 unsigned long 6 _M_space_left() 7 { return _M_finish !=3D 0; } 8 }; 9 template class __mini_vector< std::pair< long, long > >; 10 template class __mini_vector< int >; help? :) TIA! =C2=B9) C functions work out fine once fixed to have the correct loc. C++ functions worked fine once, but i now see that apparently "int my_main" on line 42 regressed again in the meantime: $ XXX=3D1 ../gcc/xg++ -B../gcc -c -o return-narrow.o return-narrow.cc -fdia= gnostics-color=3Dalways=20 return-narrow.cc: In function =E2=80=98int xyz(int, int, int)=E2=80=99: return-narrow.cc:1:1: warning: result decl locus sample 1 | int xyz (int param1, int param2, int param3) | ^~~ | the return type return-narrow.cc: In function =E2=80=98int abc(int, int, int)=E2=80=99: return-narrow.cc:11:1: warning: result decl locus sample 11 | int abc (int param1, int param2, int param3) | ^~~ | the return type return-narrow.cc: In function =E2=80=98const void* const* pointer_thingie(v= oid*)=E2=80=99: return-narrow.cc:21:7: warning: result decl locus sample 21 | const void *const * pointer_thingie (void *i) | ^~~~ | the return type return-narrow.cc: In function =E2=80=98int comparer(int, int, int)=E2=80=99: return-narrow.cc:25:1: warning: result decl locus sample 25 | int comparer (int param1, int param2, int param3) | ^~~ | the return type return-narrow.cc: In function =E2=80=98int main()=E2=80=99: return-narrow.cc:35:1: warning: result decl locus sample 35 | int main (void) // dg-bogus "Function .main. could return .bool." | ^~~ | the return type return-narrow.cc: In function =E2=80=98int my_main(int, char**)=E2=80=99: return-narrow.cc:42:25: warning: result decl locus sample 42 | int my_main (int, char**) { return 0; } | ^ | the return type return-narrow.cc: In function =E2=80=98int hidden_main(int, char**)=E2=80= =99: return-narrow.cc:43:1: warning: result decl locus sample 43 | int hidden_main (int, char**) { return 1; } | ^~~ | the return type return-narrow.cc: In function =E2=80=98int i64c(int)=E2=80=99: return-narrow.cc:46:1: warning: result decl locus sample 46 | int i64c(int i) | ^~~ | the return type --MP_/woVI+dNnQKnfu7we+B3APdg Content-Type: text/x-patch Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=result_decl_loc.00.patch gcc/c/ChangeLog: * c-decl.cc (start_function): Set the result decl location. gcc/cp/ChangeLog: * decl.cc (start_function): Set the result decl location. (finish_function): XXX locus sample gcc/ChangeLog: * tree-pretty-print.cc (dump_location): Use pp_colon and pp_right_bracket where applicable. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index c701f07befe..a441d187c1b 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -9487,6 +9487,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator, tree decl1, old_decl; tree restype, resdecl; location_t loc; + location_t result_loc; current_function_returns_value = 0; /* Assume, until we see it does. */ current_function_returns_null = 0; @@ -9712,8 +9713,11 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator, push_scope (); declare_parm_level (); + /* Set the result decl source location to the location of the typespec. */ + result_loc = (declspecs->locations[cdw_typespec] == UNKNOWN_LOCATION + ? loc : declspecs->locations[cdw_typespec]); restype = TREE_TYPE (TREE_TYPE (current_function_decl)); - resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype); + resdecl = build_decl (result_loc, RESULT_DECL, NULL_TREE, restype); DECL_ARTIFICIAL (resdecl) = 1; DECL_IGNORED_P (resdecl) = 1; DECL_RESULT (current_function_decl) = resdecl; diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index c136dbbba1a..38874bf1558 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -17361,6 +17361,8 @@ start_function (cp_decl_specifier_seq *declspecs, tree attrs) { tree decl1; + tree result; + bool ret; decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs); invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1); @@ -17373,7 +17375,18 @@ 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); + ret = start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT); + + /* decl1 might be ggc_freed here. */ + //decl1 = current_function_decl; + + /* Set the result decl source location to the location of the typespec. */ + if (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 @@ -17942,6 +17955,13 @@ finish_function (bool inline_p) "non-void")) suppress_warning (fndecl, OPT_Wreturn_type); } + if (getenv("XXX") != NULL) + { + location_t result_loc = DECL_SOURCE_LOCATION (DECL_RESULT (fndecl)); + gcc_rich_location richloc (result_loc); + richloc.add_fixit_replace (result_loc, "the return type"); + warning_at (&richloc, 0, "result dec%c locus sample", 'l'); + } /* Lambda closure members are implicitly constexpr if possible. */ if (cxx_dialect >= cxx17 diff --git a/gcc/tree-pretty-print.cc b/gcc/tree-pretty-print.cc index 99af977979d..591883c4063 100644 --- a/gcc/tree-pretty-print.cc +++ b/gcc/tree-pretty-print.cc @@ -1412,12 +1412,12 @@ dump_location (pretty_printer *pp, location_t loc) if (xloc.file) { pp_string (pp, xloc.file); - pp_string (pp, ":"); + pp_colon (pp); } pp_decimal_int (pp, xloc.line); pp_colon (pp); pp_decimal_int (pp, xloc.column); - pp_string (pp, "] "); + pp_right_bracket (pp); } --MP_/woVI+dNnQKnfu7we+B3APdg Content-Type: text/x-c++src Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=return-narrow.cc int xyz (int param1, int param2, int param3) { if (param1 == 42) return 1; if (param2 == 17) return 1; if (param3 == 99) return 1; return 0; } int abc (int param1, int param2, int param3) { if (param1 == 42) return 0; if (param2 == 17) return 0; if (param3 == 99) return 0; return 0; } const void *const * pointer_thingie (void *i) { return (const void *const *)((long)i & 1); } int comparer (int param1, int param2, int param3) { if (param1 >= 42) return 1; if (param2 == 17) return 1; if (param3 <= 99) return -1; return 0; } int main (void) // dg-bogus "Function .main. could return .bool." { return 0; } _Pragma("GCC visibility push(hidden)") int my_main (int argc, char**argv) __attribute__(( visibility("default") )); int my_main (int, char**) { return 0; } int hidden_main (int, char**) { return 1; } _Pragma("GCC visibility pop") int i64c(int i) { i &= 0x3f; if (i == 0) return '.'; if (i == 1) return '/'; if (i < 12) return ('0' - 2 + i); if (i < 38) return ('A' - 12 + i); return ('a' - 38 + i); } --MP_/woVI+dNnQKnfu7we+B3APdg Content-Type: text/x-c++src Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=return-narrow-2.cc namespace std { template < typename, typename > struct pair; } template < typename > struct __mini_vector { int _M_finish; unsigned long _M_space_left() { return _M_finish != 0; } }; template class __mini_vector< std::pair< long, long > >; template class __mini_vector< int >; --MP_/woVI+dNnQKnfu7we+B3APdg--