All of lore.kernel.org
 help / color / mirror / Atom feed
From: volanschi@metaware.fr (Nic Volanschi (R&D))
To: cocci@systeme.lip6.fr
Subject: [Cocci] spatch issue with C struct initialization
Date: Mon, 07 Apr 2014 15:27:49 +0200	[thread overview]
Message-ID: <1396877269.3201.20.camel@zamolxis.metaware.tm.fr> (raw)
In-Reply-To: <alpine.DEB.2.10.1404071452020.2187@hadrien>

On Mon, 2014-04-07 at 14:54 +0200, Julia Lawall wrote:
> On Mon, 7 Apr 2014, Nic Volanschi (R&D) wrote:
> > I agree that the "initializer" nonterminal is not the right place to
> > extend the grammar, because it would not allow legal statements such as:
> >
> >   structure = ((struct foo) {x + y, 'a', 0});
> >
> > the example coming from:
> > http://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html
> >
> > Rather, it seems to me that the right place to extend the grammar is the
> > "cast_expression" nonterminal:
> >
> > > cast_expression
> > > 	: unary_expression
> > > 	| '(' type_name ')' cast_expression
> >           | '(' type_name ')' '{' initializer_list '}'
> > > 	;
> >
> > This would not only allow the above legal expression, but should also
> > avoid to introduce a conflict in the grammar.
> 
> I can try this.  So the point is that if a type is present, then { ... }
> is a legal expression in any context?  

This is how I understand the above web page about compound literals.
Indeed, I tried the following code in gcc and it works fine:

int main() { 
  struct s { int i; int j; } s1;
  printf("{%d, %d}\n", (struct s){ 44, 55}.i, (struct s){ 66, 77}.j);
  printf("%d\n", (int[]){12,13,14}[1] + 10);
} 

So it seems that a compound literal can be used in any place where an
lvalue of the corresponding type is legal.

> On the other hand, Coccinelle is
> not a syntax checker.  It is fine for it to accept syntax that is invalid,
> as long as the effect in terms of metavariables etc is expectable to the
> user.

Sure. But in the given case, I'm not sure you create illegal uses,
anyways.

Nic.

  reply	other threads:[~2014-04-07 13:27 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-07 10:37 [Cocci] spatch issue with C struct initialization Nic Volanschi (R&D)
2014-04-07 12:54 ` Julia Lawall
2014-04-07 13:27   ` Nic Volanschi (R&D) [this message]
2014-04-07 13:48     ` Julia Lawall
  -- strict thread matches above, loose matches on Subject: below --
2014-04-03  8:30 Marcel Apfelbaum
2014-04-03 10:16 ` Julia Lawall
2014-04-03 13:31   ` Marcel Apfelbaum
2014-04-03 14:19     ` Julia Lawall
2014-04-03 14:29       ` Marcel Apfelbaum
2014-04-03 14:33         ` Julia Lawall
2014-04-03 14:38           ` Marcel Apfelbaum
2014-04-04 11:21         ` Julia Lawall
2014-04-05 15:01           ` Michael Stefaniuc
2014-04-05 15:17             ` Julia Lawall
2014-04-06  7:45               ` Marcel Apfelbaum
2014-04-06 10:57                 ` Julia Lawall
2014-04-20 20:29                 ` Julia Lawall
2014-04-22  8:21                   ` Marcel Apfelbaum
2014-04-03 14:22     ` Julia Lawall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1396877269.3201.20.camel@zamolxis.metaware.tm.fr \
    --to=volanschi@metaware.fr \
    --cc=cocci@systeme.lip6.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.