* [Cocci] Patching const char * constants
@ 2018-01-10 2:22 Rob Hoelz
2018-01-10 8:40 ` Julia Lawall
[not found] ` <58ff3bae-29c1-19cf-99f1-5b96e7855101@users.sourceforge.net>
0 siblings, 2 replies; 5+ messages in thread
From: Rob Hoelz @ 2018-01-10 2:22 UTC (permalink / raw)
To: cocci
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?
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!
Thanks,
Rob
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Cocci] Patching const char * constants
2018-01-10 2:22 [Cocci] Patching const char * constants Rob Hoelz
@ 2018-01-10 8:40 ` Julia Lawall
2018-01-10 19:34 ` Rob Hoelz
[not found] ` <58ff3bae-29c1-19cf-99f1-5b96e7855101@users.sourceforge.net>
1 sibling, 1 reply; 5+ messages in thread
From: Julia Lawall @ 2018-01-10 8:40 UTC (permalink / raw)
To: cocci
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Cocci] Patching const char * constants
2018-01-10 8:40 ` Julia Lawall
@ 2018-01-10 19:34 ` Rob Hoelz
[not found] ` <5ab8be10-1f1a-707b-b629-e25e7fb731fa@users.sourceforge.net>
0 siblings, 1 reply; 5+ messages in thread
From: Rob Hoelz @ 2018-01-10 19:34 UTC (permalink / raw)
To: cocci
On Wed, 10 Jan 2018 09:40:19 +0100 (CET)
Julia Lawall <julia.lawall@lip6.fr> wrote:
> 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.
/me facepalms
Of course! What a silly mistake on my part =/ Thanks for clarifying for me!
>
> >
> > 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
>
Thanks for the quick answer, Julia! I'll try to undertake something a little
simpler to start.
-Rob
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Cocci] Patching string literal juxtaposition
[not found] ` <5ab8be10-1f1a-707b-b629-e25e7fb731fa@users.sourceforge.net>
@ 2018-01-15 16:57 ` Rob Hoelz
0 siblings, 0 replies; 5+ messages in thread
From: Rob Hoelz @ 2018-01-15 16:57 UTC (permalink / raw)
To: cocci
On Thu, 11 Jan 2018 11:11:10 +0100
SF Markus Elfring <elfring@users.sourceforge.net> wrote:
> > I'll try to undertake something a little simpler to start.
>
> Thanks for your understanding of the current software situation.
>
> I hope that you will find another promising source code
> transformation task that will work also for you finally. But there
> can occasionally occur mismatches for your expectations on an
> approach which might look simpler than what can be really performed
> with the Coccinelle software at the moment.
>
> Extensions could be developed over time. Are you looking for
> additional tools and corresponding support?
>
> Regards,
> Markus
>
I have a number of things I can try with Coccinelle - I think that the string literal
matching one was just one of the more ambitious ones! As far as additional tooling
and support go, I'm just playing around with Coccinelle for the moment.
Thanks,
Rob
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Cocci] Patching string literal juxtaposition
[not found] ` <58ff3bae-29c1-19cf-99f1-5b96e7855101@users.sourceforge.net>
@ 2018-01-15 16:59 ` Rob Hoelz
0 siblings, 0 replies; 5+ messages in thread
From: Rob Hoelz @ 2018-01-15 16:59 UTC (permalink / raw)
To: cocci
On Thu, 11 Jan 2018 14:30:16 +0100
SF Markus Elfring <elfring@users.sourceforge.net> wrote:
> > @@
> > 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);
>
> It would be nice if this small source code adjustment could be
> specified also for the semantic patch language in such a convenient
> and succinct way. But the Coccinelle software provides a specific
> behaviour for string literals so far. I imagine then that you could
> achieve the desired transformation with an other approach.
>
> * How do you think about to choose any of the supported programming
> languages for the construction of corresponding SmPL script rules?
> http://coccinelle.lip6.fr/docs/main_grammar003.html
>
> * A ?constant? could be stored in a script variable by this
> metavariable type. http://coccinelle.lip6.fr/docs/main_grammar002.html
>
> The received input string can be adjusted in ways you like by
> additional function calls from the available programming interfaces.
> Would you like to propagate the constructed value back to another
> script rule by calling the SmPL method ?make_expr??
>
> Regards,
> Markus
>
I'm *very* much new to Coccinelle, so I think I'll have to trying the scripting
interface as per Julia's suggestion. I think I need to experiment with Coccinelle
a bit (or a lot!) more to get the hang of it.
-Rob
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-01-15 16:59 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-01-10 2:22 [Cocci] Patching const char * constants Rob Hoelz
2018-01-10 8:40 ` Julia Lawall
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
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.