On 11/17/22 14:02, Bernhard Reutner-Fischer wrote:
On Thu, 17 Nov 2022 09:53:32 -0500
Jason Merrill <ja...@redhat.com> wrote:

On 11/17/22 03:56, Bernhard Reutner-Fischer wrote:
On Tue, 15 Nov 2022 18:52:41 -0500
Jason Merrill <ja...@redhat.com> wrote:
On 11/12/22 13:45, Bernhard Reutner-Fischer wrote:
gcc/cp/ChangeLog:

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

---
Bootstrapped and regtested on x86_86-unknown-linux with no regressions.
Ok for trunk?

Cc: Nathan Sidwell <nat...@acm.org>
Cc: Jason Merrill <ja...@redhat.com>
---
    gcc/cp/decl.cc | 15 ++++++++++++++-
    1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 6e98ea35a39..ed40815e645 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -17449,6 +17449,8 @@ start_function (cp_decl_specifier_seq *declspecs,
                tree attrs)
    {
      tree decl1;
+  tree result;
+  bool ret;

We now prefer to declare new variables as late as possible, usually when
they are initialized.

Moved. Ok like attached? Bootstrapped and regtested fine.
      decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
      invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
@@ -17461,7 +17463,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];

One way to handle the template case would be for the code in
start_preparsed_function that sets DECL_RESULT to check whether decl1 is
a template instantiation, and in that case copy the location from the
template's DECL_RESULT, i.e.

DECL_RESULT (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl1)))

Well, that would probably work if something would set the location of
that template result decl properly, which nothing does out of the box.

Hmm, it should get set by your patch, since templates go through
start_function like normal functions.

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index ed7226b82f0..65d78c82a2d 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -17230,6 +17231,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_USE_TEMPLATE (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))))

This condition is true only for the template definition, for which you
haven't gotten to your start_function change yet.

Instead, you want to copy the location for instantiations, i.e. check
DECL_TEMPLATE_INSTANTIATION instead of !DECL_USE_TEMPLATE.

No, that makes no difference.

Hmm, when I stop there when processing the instantiation the template's DECL_RESULT has the right location information, e.g. for

template <class T> int f() { return 42; }

int main()
{
  f<int>();
}

#1 0x0000000000f950e8 in instantiate_body (pattern=<template_decl 0x7ffff7ff5080 f>, args=<tree_vec 0x7fffe9712ae0>, d=<function_decl 0x7fffe971e600 f>, nested_p=false) at /home/jason/gt/gcc/cp/pt.cc:26470 #0 start_preparsed_function (decl1=<function_decl 0x7fffe971e600 f>, attrs=<tree 0x0>, flags=1) at /home/jason/gt/gcc/cp/decl.cc:17252
(gdb) p expand_location (input_location)
$13 = {file = 0x4962370 "wa.C", line = 1, column = 24, data = 0x0, sysp = false} (gdb) p expand_location (DECL_SOURCE_LOCATION (DECL_RESULT (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl1))))) $14 = {file = 0x4962370 "wa.C", line = 1, column = 20, data = 0x0, sysp = false}

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.

I'd like to get the template case right while we're looking at it. I guess I can add that myself if you're done trying.

Is the hunk for normal functions OK for trunk?

You also need a testcase for the desired behavior, with e.g.
{ dg-error "23:" }

thanks,


+      DECL_SOURCE_LOCATION (DECL_RESULT (decl1))
+       = DECL_SOURCE_LOCATION (

Open paren goes on the new line.

+           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.  */

(gdb) call inform(DECL_SOURCE_LOCATION (DECL_RESULT (decl1)), "decl1 result locus 
before")
../tmp4/return-narrow-2.cc:7:3: note: decl1 result locus before
      7 |   { return _M_finish != 0; }
        |   ^
(gdb) n
(gdb) call inform(DECL_SOURCE_LOCATION (DECL_RESULT (decl1)), "decl1 result locus 
from TI")
../tmp4/return-narrow-2.cc:7:3: note: decl1 result locus from TI
(gdb) p DECL_SOURCE_LOCATION (DECL_RESULT (decl1))
$1 = 267168

I'm leaving the template case alone for now, maybe i'm motivated later
on to again look at grokfndecl and/or grokmethod to fill in the proper
location. For starters i only need normal functions.
But many thanks for the hint on where the template stuff is, i thought
i would not need it at all but had hoped that there is a spot where
both declspec are at hand and something is "derived" from the templates.
+  return ret;
    }
    
    /* Returns true iff an EH_SPEC_BLOCK should be created in the body of



Reply via email to