From: julia.lawall@lip6.fr (Julia Lawall)
To: cocci@systeme.lip6.fr
Subject: [Cocci] Patching const char * constants
Date: Wed, 10 Jan 2018 09:40:19 +0100 (CET) [thread overview]
Message-ID: <alpine.DEB.2.20.1801100933580.3252@hadrien> (raw)
In-Reply-To: <20180109202248.7a1e4e1c@eridanus>
On Tue, 9 Jan 2018, Rob Hoelz wrote:
> Hi Coccinelle community,
>
> I just discovered Coccinelle a few days ago, and already my mind is reeling with the possibilities.
> I'm doing some work porting the Claws Mail mail client to GTK3, and I think Coccinelle would be a
> great tool to use to accomplish this task.
>
> One of the thing I'm trying to use Coccinelle to do is to detect and patch string concatenation via
> string literal juxtaposition; for example:
>
> > ...
> > GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
>
> I would like to write a spatch that does something like the following:
>
> > @@
> > constant char *stock_constant =~ "^GTK_STOCK";
> > @@
> >
> > -"+" stock_constant
> > +stock_constant
>
> For starters, Coccinelle doesn't seem to recognize string juxtaposition (spatch gives me an error when
> I try this); secondly, I can't seem to get "constant char *p" to match *any* string literals. For example,
> take this simple test program:
>
> > int
> > main(void)
> > {
> > const char *p = "bar";
> > return 0;
> > }
>
> If I just use "constant p", it'll match the 0 literal in the return statement. I can use "constant int p" to
> make only that 0 match, but I'd like to match the "bar" string literal, and "constant char *p" doesn't seem to
> work. Is there some way I can get Coccinelle to match only string literals to a metavariable?
It's constant char[] c. A string is an array.
>
> Another quick question: is there a way to tell Coccinelle to dump its AST so I can see which type an expression
> has been assigned? Having such a tool might help me get more familiar with the tool!
No there is nothing like this. There is a BNF in the documentation
(http://coccinelle.lip6.fr/docs/index.html).
People don't find it very user-friendly, but for example here:
http://coccinelle.lip6.fr/docs/main_grammar010.html
you can see that there are possibilities like:
exp metaidAssignOp exp
exp metaidBinOp exp
metaidExp
metaidConst
Unfortunately, none of those names actually correspond to the way in which
one declares those metavariables, which are assignment operator x, binary
operator x, expression, and constant, respectively.
It may be helpful to look at the examples in http://coccinellery.org/
For the concatenated strings, unfortunately the patern matching language
doesn't support that. The only thing you can do is match the full string,
and then do some magic with python to get out the parts you want. I'm not
sure that this is the best first project.
julia
next prev parent reply other threads:[~2018-01-10 8:40 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-10 2:22 [Cocci] Patching const char * constants Rob Hoelz
2018-01-10 8:40 ` Julia Lawall [this message]
2018-01-10 19:34 ` Rob Hoelz
[not found] ` <5ab8be10-1f1a-707b-b629-e25e7fb731fa@users.sourceforge.net>
2018-01-15 16:57 ` [Cocci] Patching string literal juxtaposition Rob Hoelz
[not found] ` <58ff3bae-29c1-19cf-99f1-5b96e7855101@users.sourceforge.net>
2018-01-15 16:59 ` Rob Hoelz
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=alpine.DEB.2.20.1801100933580.3252@hadrien \
--to=julia.lawall@lip6.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.