diff mbox

[PATCH/RFC] C++ FE: expression ranges (v2)

Message ID 1448297620.19594.168.camel@surprise
State New
Headers show

Commit Message

David Malcolm Nov. 23, 2015, 4:53 p.m. UTC
On Mon, 2015-11-23 at 10:59 +0100, Richard Biener wrote:
> On Sat, Nov 21, 2015 at 9:21 AM, Jakub Jelinek <jakub@redhat.com> wrote:
> > On Sat, Nov 21, 2015 at 02:16:49AM -0500, Jason Merrill wrote:
> >> On 11/19/2015 03:46 PM, Jason Merrill wrote:
> >> >On 11/15/2015 12:01 AM, David Malcolm wrote:
> >> >>As with the C frontend, there's an issue with tree nodes that
> >> >>don't have locations: VAR_DECL, INTEGER_CST, etc:
> >> >>
> >> >>   int test (int foo)
> >> >>   {
> >> >>     return foo * 100;
> >> >>            ^^^   ^^^
> >> >>   }
> >> >>
> >> >>where we'd like to access the source spelling ranges of the expressions
> >> >>during parsing, so that we can use them when reporting parser errors.
> >> >
> >> >Hmm, I had been thinking to address this in the C++ front end by
> >> >wrapping uses in another tree: NOP_EXPR for rvalues, VIEW_CONVERT_EXPR
> >> >for lvalues.
> >>
> >> On the other hand, my direction seems likely to cause more issues,
> >> especially with code that doesn't yet know how to handle VIEW_CONVERT_EXPR,
> >> and could create ambiguity with explicit conversions.  So I guess your
> >> approach seems reasonable.
> >
> > But your approach would allow better diagnostics even in places where you
> > don't have the structures with tree, location_t pairs around.  With that
> > it will be limited solely to the parser and nothing else, so even template
> > instantiation if it is something that can be only detected when
> > instantiating would be too late.
> >
> > I think using a new tree (rather than using NOP_EXPR/VIEW_CONVERT_EXPR)
> > that would be just some expression with location and teaching the FE and
> > folder about it might be even better.
> 
> Agreed.  Note that we already have NON_LVALUE_EXPR and fold-const.c uses
> that to stick locations on things that cannot have them.
> 
> OTOH I would like to get rid of NON_LVALUE_EXPR in the middle-end (and thus
> fold-const.c).

Thanks.

Does the following look like the kind of thing you had in mind?  (just
the tree.def part for now).   Presumably usable for both lvalues and
rvalues, where the thing it wraps is what's important.  It merely exists
to add an EXPR_LOCATION, for a usage of the wrapped thing.

Comments

Jakub Jelinek Nov. 23, 2015, 4:57 p.m. UTC | #1
On Mon, Nov 23, 2015 at 11:53:40AM -0500, David Malcolm wrote:
> Does the following look like the kind of thing you had in mind?  (just
> the tree.def part for now).   Presumably usable for both lvalues and
> rvalues, where the thing it wraps is what's important.  It merely exists
> to add an EXPR_LOCATION, for a usage of the wrapped thing.

Yes, but please see with Jason, Richard and perhaps others if they are ok
with that too before spending too much time in that direction.
All occurrences of it would have to be folded away during the gimplification
at latest, this shouldn't be something we use in the middle-end.

> diff --git a/gcc/tree.def b/gcc/tree.def
> index 44e5a5e..30fd766 100644
> --- a/gcc/tree.def
> +++ b/gcc/tree.def
> @@ -1407,6 +1407,13 @@ DEFTREECODE (CILK_SPAWN_STMT, "cilk_spawn_stmt", tcc_statement, 1)
>  /* Cilk Sync statement: Does not have any operands.  */
>  DEFTREECODE (CILK_SYNC_STMT, "cilk_sync_stmt", tcc_statement, 0)
>  
> +/* Wrapper to add a source code location to an expression, either one
> +   that doesn't have one (such as an INTEGER_CST), or to a usage of a
> +   variable (e.g. PARAM_DECL or VAR_DECL), where we want to record
> +   the site in the source where the variable was *used* rather than
> +   where it was declared.  */
> +DEFTREECODE (LOCATION_EXPR, "location_expr", tcc_unary, 1)
> +
>  /*
>  Local variables:
>  mode:c

	Jakub
Marek Polacek Nov. 23, 2015, 5:07 p.m. UTC | #2
On Mon, Nov 23, 2015 at 05:57:54PM +0100, Jakub Jelinek wrote:
> On Mon, Nov 23, 2015 at 11:53:40AM -0500, David Malcolm wrote:
> > Does the following look like the kind of thing you had in mind?  (just
> > the tree.def part for now).   Presumably usable for both lvalues and
> > rvalues, where the thing it wraps is what's important.  It merely exists
> > to add an EXPR_LOCATION, for a usage of the wrapped thing.
> 
> Yes, but please see with Jason, Richard and perhaps others if they are ok
> with that too before spending too much time in that direction.
> All occurrences of it would have to be folded away during the gimplification
> at latest, this shouldn't be something we use in the middle-end.

I'd expect LOCATION_EXPR be defined in c-family/c-common.def, not tree.def.
And I'd think it shouldn't survive genericizing, thus never leak into the ME.

	Marek
Jason Merrill Nov. 23, 2015, 7:25 p.m. UTC | #3
On 11/23/2015 12:07 PM, Marek Polacek wrote:
> On Mon, Nov 23, 2015 at 05:57:54PM +0100, Jakub Jelinek wrote:
>> On Mon, Nov 23, 2015 at 11:53:40AM -0500, David Malcolm wrote:
>>> Does the following look like the kind of thing you had in mind?  (just
>>> the tree.def part for now).   Presumably usable for both lvalues and
>>> rvalues, where the thing it wraps is what's important.  It merely exists
>>> to add an EXPR_LOCATION, for a usage of the wrapped thing.
>>
>> Yes, but please see with Jason, Richard and perhaps others if they are ok
>> with that too before spending too much time in that direction.
>> All occurrences of it would have to be folded away during the gimplification
>> at latest, this shouldn't be something we use in the middle-end.
>
> I'd expect LOCATION_EXPR be defined in c-family/c-common.def, not tree.def.
> And I'd think it shouldn't survive genericizing, thus never leak into the ME.

Makes sense.

Jason
Richard Biener Nov. 24, 2015, 9:40 a.m. UTC | #4
On Mon, Nov 23, 2015 at 8:25 PM, Jason Merrill <jason@redhat.com> wrote:
> On 11/23/2015 12:07 PM, Marek Polacek wrote:
>>
>> On Mon, Nov 23, 2015 at 05:57:54PM +0100, Jakub Jelinek wrote:
>>>
>>> On Mon, Nov 23, 2015 at 11:53:40AM -0500, David Malcolm wrote:
>>>>
>>>> Does the following look like the kind of thing you had in mind?  (just
>>>> the tree.def part for now).   Presumably usable for both lvalues and
>>>> rvalues, where the thing it wraps is what's important.  It merely exists
>>>> to add an EXPR_LOCATION, for a usage of the wrapped thing.
>>>
>>>
>>> Yes, but please see with Jason, Richard and perhaps others if they are ok
>>> with that too before spending too much time in that direction.
>>> All occurrences of it would have to be folded away during the
>>> gimplification
>>> at latest, this shouldn't be something we use in the middle-end.
>>
>>
>> I'd expect LOCATION_EXPR be defined in c-family/c-common.def, not
>> tree.def.
>> And I'd think it shouldn't survive genericizing, thus never leak into the
>> ME.
>
>
> Makes sense.

OTOH then the FEs need to strip trees of it if they ever want to use
sth from fold-const.c for example.  NON_LVALUE_EXPR is not
in c-family/c-common.def either... (and conveniently stripped with
STRIP_NOPS and friends).

Ok, I _would_ like to move NON_LVALUE_EXPR to the C frontend
family as well...  so I guess that would be a nice starting project
and if you can make that work you can add LOCATION_EXPR to
the C family only.

Richard.

> Jason
>
>
diff mbox

Patch

diff --git a/gcc/tree.def b/gcc/tree.def
index 44e5a5e..30fd766 100644
--- a/gcc/tree.def
+++ b/gcc/tree.def
@@ -1407,6 +1407,13 @@  DEFTREECODE (CILK_SPAWN_STMT, "cilk_spawn_stmt", tcc_statement, 1)
 /* Cilk Sync statement: Does not have any operands.  */
 DEFTREECODE (CILK_SYNC_STMT, "cilk_sync_stmt", tcc_statement, 0)
 
+/* Wrapper to add a source code location to an expression, either one
+   that doesn't have one (such as an INTEGER_CST), or to a usage of a
+   variable (e.g. PARAM_DECL or VAR_DECL), where we want to record
+   the site in the source where the variable was *used* rather than
+   where it was declared.  */
+DEFTREECODE (LOCATION_EXPR, "location_expr", tcc_unary, 1)
+
 /*
 Local variables:
 mode:c