From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x62c.google.com (mail-ej1-x62c.google.com [IPv6:2a00:1450:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id B790C3852219; Fri, 18 Nov 2022 10:49:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B790C3852219 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-x62c.google.com with SMTP id ft34so11971103ejc.12; Fri, 18 Nov 2022 02:49:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to; bh=SPdwLa0bqswAYedgUfOLE4KdihFsJA30+LL+2TQ1kEE=; b=XT05Fu4qgwUYXH9g3yGeERC5iI5rewTYIlfZCZqlzbNoEjC2u4lOo4Y6PtATCKf3IR 3vz7p8aPMg3sRIGTR/5YagOeqVFiz2RZYLi+kL6mpBuYSq+xl6vlIOeua2xwkPmeqoyO gJL29mjwNe4mOnOrKOPry0Su9c+uvdwB8aLyMGkCVgiFCSKBQoI/HabALHrVEVUEF6Ye pi6ESabyqHuU0KQ03/UApjSzFaH/oFXOFIOn6BENDBIG8sdN0fUgzsRClj3NCIF28fzG yrPknOXekgsODzJF8Vfq9ik9LSGFwdIhkeoPJiliMxn3BUSDw3lA2NfDX/et6pi6GYfo /EHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:references:in-reply-to:message-id:subject:cc:to:from :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SPdwLa0bqswAYedgUfOLE4KdihFsJA30+LL+2TQ1kEE=; b=ZefZwX9R0Ue9U3WiN5M1K5xI/QhwEBRintfaqPOrfo++ubIJgJS59NQ6eGg9CSpjXw KDCz28BMVgd4LV3yRgs7EX6CfIP1Zw4kFwUcRCZATqI0R/HhzlUn/h113gzKtVusZ9Xq b+mdMRgMG2kZ7v9W3/SSSJtTV2s7REWHJB67COfHJ5y24Hcp3vYS+tlbmduM1FGa/7QO LKSdForMYxAkO9VDkCmeKldWyH/2RIgLSpN18nULIlvOZ1BetYlP+IkBTZj3WkA8Xe4X xdYClRpGfLA3NqNF+g0unqgg7MVPAEQM95hDjaOYDW1SPT76npsNN6iQFoPvl9Jlzmlt CoGA== X-Gm-Message-State: ANoB5pmMvPBJuLLyG2drZsk90Tpeb/ZUcPeu15A8pDVcDi5ABnEF4Fyk rQy+p3MVKt3/vqr0z17Nnjo= X-Google-Smtp-Source: AA0mqf53kne86rS7XkUA2BLGapWWdD3Kj2N7XFopiSORPRRYYlq/lMzKuWh9Q0t6NqDh8wOkxtNbyg== X-Received: by 2002:a17:906:6093:b0:78d:b37c:83d9 with SMTP id t19-20020a170906609300b0078db37c83d9mr5416756ejj.637.1668768583333; Fri, 18 Nov 2022 02:49:43 -0800 (PST) Received: from nbbrfq (80-110-214-113.static.upcbusiness.at. [80.110.214.113]) by smtp.gmail.com with ESMTPSA id kx4-20020a170907774400b007838e332d78sm1506246ejc.128.2022.11.18.02.49.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Nov 2022 02:49:42 -0800 (PST) Date: Fri, 18 Nov 2022 11:49:38 +0100 From: Bernhard Reutner-Fischer To: Jason Merrill Cc: rep.dot.nop@gmail.com, gcc-patches@gcc.gnu.org, Bernhard Reutner-Fischer , Nathan Sidwell Subject: Re: [PATCH 2/5] c++: Set the locus of the function result decl Message-ID: <20221118114938.48a39cbd@nbbrfq> In-Reply-To: <8b923e25-db47-4d68-504f-6b3c664349c7@redhat.com> References: <20221112234543.95441-1-aldot@gcc.gnu.org> <20221112234543.95441-3-aldot@gcc.gnu.org> <20221117095647.42fefe06@nbbrfq> <4fcd1dd6-1691-6974-f181-3c57a4c305c9@redhat.com> <20221117200208.3048de7a@nbbrfq> <8b923e25-db47-4d68-504f-6b3c664349c7@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/DKjL00l9l5/ermqbGYwalSJ" 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,WEIRD_PORT 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: --MP_/DKjL00l9l5/ermqbGYwalSJ Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Thu, 17 Nov 2022 18:52:36 -0500 Jason Merrill wrote: > On 11/17/22 14:02, Bernhard Reutner-Fischer wrote: > > On Thu, 17 Nov 2022 09:53:32 -0500 > > Jason Merrill wrote: > >> Instead, you want to copy the location for instantiations, i.e. check > >> DECL_TEMPLATE_INSTANTIATION instead of !DECL_USE_TEMPLATE. =20 > >=20 > > No, that makes no difference. =20 >=20 > Hmm, when I stop there when processing the instantiation the template's=20 > DECL_RESULT has the right location information, e.g. for >=20 > template int f() { return 42; } >=20 > int main() > { > f(); > } >=20 > #1 0x0000000000f950e8 in instantiate_body (pattern=3D 0x7ffff7ff5080 f>, args=3D, d=3D 0x7fffe971e600 f>, nested_p=3Dfalse) at /home/jason/gt/gcc/cp/pt.cc:26470 > #0 start_preparsed_function (decl1=3D,=20 > attrs=3D, flags=3D1) at /home/jason/gt/gcc/cp/decl.cc:17252 > (gdb) p expand_location (input_location) > $13 =3D {file =3D 0x4962370 "wa.C", line =3D 1, column =3D 24, data =3D 0= x0, sysp=20 > =3D false} > (gdb) p expand_location (DECL_SOURCE_LOCATION (DECL_RESULT=20 > (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl1))))) > $14 =3D {file =3D 0x4962370 "wa.C", line =3D 1, column =3D 20, data =3D 0= x0, sysp=20 > =3D false} Yes, that works. Sorry if i was not clear: The thing in the cover letter in this series does not work, the mini_vector reduced testcase from the libstdc++-v3/include/ext/bitmap_allocator.h. class template member function return type location, would that be it? AFAIR the problem was that that these member functions get their result decl late. When they get them, there are no declspecs->locations[ds_type_spec] around anywhere to tuck that on the resdecl. While the result decl is clear, there is no obvious way where to store the ds_type_spec (somewhere in the template, as you told me). Back then I tried moving the resdecl building from start_preparsed_function to grokfndecl but that did not work out easily IIRC and i ultimately gave up to move stuff around rather blindly. I also tried to find a spot where i could store the ds_type_spec locus somewhere in grokmethod, but i think the problem was the same, i had just the type where i cannot store a locus and did not find a place where i could smuggle the locus along. So, to make that clear. Your template function (?) works: $ XXX=3D1 ./xg++ -B. -S -o /dev/null ../tmp4/return-narrow-2j.cc=20 ../tmp4/return-narrow-2j.cc: In function =E2=80=98int f()=E2=80=99: ../tmp4/return-narrow-2j.cc:1:20: warning: result decl locus sample 1 | template int f() { return 42; } | ^~~ | the return type ../tmp4/return-narrow-2j.cc: In function =E2=80=98int main()=E2=80=99: ../tmp4/return-narrow-2j.cc:3:1: warning: result decl locus sample 3 | int main() | ^~~ | the return type ../tmp4/return-narrow-2j.cc: In instantiation of =E2=80=98int f() [with T = =3D int]=E2=80=99: ../tmp4/return-narrow-2j.cc:5:10: required from here ../tmp4/return-narrow-2j.cc:1:20: warning: result decl locus sample 1 | template int f() { return 42; } | ^~~ | the return type The class member fn not so much (IMHO, see attached): $ XXX=3D1 ./xg++ -B. -S -o /dev/null ../tmp4/return-narrow-2.cc=20 ../tmp4/return-narrow-2.cc: In member function =E2=80=98const long unsigned= int __mini_vector< >::_M_space_left()=E2=80=99: ../tmp4/return-narrow-2.cc:9:3: warning: result decl locus sample 9 | { return _M_finish !=3D 0; } | ^ | the return type ../tmp4/return-narrow-2.cc: In instantiation of =E2=80=98const long unsigne= d int __mini_vector< >::_M_space_left() [with =3D std::pair]=E2=80=99: ../tmp4/return-narrow-2.cc:11:17: required from here ../tmp4/return-narrow-2.cc:9:3: warning: result decl locus sample 9 | { return _M_finish !=3D 0; } | ^ | the return type ../tmp4/return-narrow-2.cc: In instantiation of =E2=80=98const long unsigne= d int __mini_vector< >::_M_space_left() [with =3D int]=E2=80=99: ../tmp4/return-narrow-2.cc:12:17: required from here ../tmp4/return-narrow-2.cc:9:3: warning: result decl locus sample 9 | { return _M_finish !=3D 0; } | ^ | the return type >=20 > > But really I'm not interested in the template case, i only mentioned > > them because they don't work and in case somebody wanted to have correct > > locations. > > I remember just frustration when i looked at those a year ago. =20 >=20 > I'd like to get the template case right while we're looking at it. I=20 > guess I can add that myself if you're done trying. >=20 > > Is the hunk for normal functions OK for trunk? =20 >=20 > You also need a testcase for the desired behavior, with e.g. > { dg-error "23:" } I'd have to think about how to test that with trunk, yes. There are no existing warnings that want to point to the return type, are there? Maybe a g++.dg/plugin/result_decl_plugin.c then. set plugin_test_list [list hmz. That strikes me as not all that flexible. We could glob *_plugin.[cC][c]*, and have foo_plugin.lst contain it's files. Whatever. thanks, --MP_/DKjL00l9l5/ermqbGYwalSJ Content-Type: text/plain Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=c++-ctmfrv-locus3.patch diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index d28889ed865..e0f057fc37b 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -17235,6 +17236,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 +17544,19 @@ 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 (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 @@ -18063,6 +18087,14 @@ finish_function (bool inline_p) 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 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))) --MP_/DKjL00l9l5/ermqbGYwalSJ Content-Type: text/plain 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; 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 >; --MP_/DKjL00l9l5/ermqbGYwalSJ--