* [Cocci] Matching variable declarations
@ 2019-09-05 11:17 Valentin Schneider
2019-09-05 12:03 ` Julia Lawall
0 siblings, 1 reply; 3+ messages in thread
From: Valentin Schneider @ 2019-09-05 11:17 UTC (permalink / raw)
To: cocci
Hi,
I'm trying to change the type of variables that match a certain rule.
Abstracting away this specific rule and only looking at coming up with a
rule to match variable declarations, I have a few newbie questions.
Say I want to replace all long declarations into ints, this simple rule
seems to be working fine:
---
@@
identifier var;
type T = long;
expression E;
@@
(
- T var;
+ int var;
|
- T var = E;
+ int var = E;
)
---
I tried to write it as
---
- T
+ int
var
? = E
;
---
but that doesn't seem to be a valid syntax.
Now, I'd like to match declarations of several variables in a single
statement as well.
Say I want to get something like
- long a, b = 2, c, d = 42, e;
+ long a, b = 2, c, e;
+ int d = 42;
(match on longs initialized to 42)
My current attempt is
---
@@
identifier var;
type T = long;
expression L, R;
@@
T L,
- var = 42
R;
+ int var;
---
But that isn't valid either. How should I go about writing this sort of
rule?
If you're curious, there's more context about what I'm trying to achieve
at:
https://lore.kernel.org/lkml/20190902210558.GA23013@avx2/
Thanks,
Valentin
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Cocci] Matching variable declarations
2019-09-05 11:17 [Cocci] Matching variable declarations Valentin Schneider
@ 2019-09-05 12:03 ` Julia Lawall
2019-09-05 14:08 ` Valentin Schneider
0 siblings, 1 reply; 3+ messages in thread
From: Julia Lawall @ 2019-09-05 12:03 UTC (permalink / raw)
To: Valentin Schneider; +Cc: cocci
On Thu, 5 Sep 2019, Valentin Schneider wrote:
> Hi,
>
> I'm trying to change the type of variables that match a certain rule.
> Abstracting away this specific rule and only looking at coming up with a
> rule to match variable declarations, I have a few newbie questions.
>
> Say I want to replace all long declarations into ints, this simple rule
> seems to be working fine:
>
> ---
> @@
> identifier var;
> type T = long;
> expression E;
> @@
>
> (
> - T var;
> + int var;
> |
> - T var = E;
> + int var = E;
> )
You could just replace the T by long in the above.
You could also forget about the declaration part and just put
- long
+ int
if you just want to change the types everywhere.
> ---
>
> I tried to write it as
> ---
> - T
> + int
> var
> ? = E
> ;
> ---
> but that doesn't seem to be a valid syntax.
No. ? is restricted to complete statements.
>
>
> Now, I'd like to match declarations of several variables in a single
> statement as well.
>
> Say I want to get something like
>
> - long a, b = 2, c, d = 42, e;
> + long a, b = 2, c, e;
> + int d = 42;
>
> (match on longs initialized to 42)
>
> My current attempt is
> ---
> @@
> identifier var;
> type T = long;
> expression L, R;
> @@
>
> T L,
> - var = 42
> R;
> + int var;
> ---
> But that isn't valid either. How should I go about writing this sort of
> rule?
Declaartions with multiple variables are tricky. By the following may
work:
- long
+ int
x;
Since you are leaving the variables alone in this case, I think this will
match thing that declare multiple variables as well.
>
> If you're curious, there's more context about what I'm trying to achieve
> at:
>
> https://lore.kernel.org/lkml/20190902210558.GA23013@avx2/
I guess your next question will be about converting %ld to %d, etc.
It may be helpful to look at coccinelle/demos/format.cocci
julia
>
> Thanks,
> Valentin
> _______________________________________________
> Cocci mailing list
> Cocci@systeme.lip6.fr
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Cocci] Matching variable declarations
2019-09-05 12:03 ` Julia Lawall
@ 2019-09-05 14:08 ` Valentin Schneider
0 siblings, 0 replies; 3+ messages in thread
From: Valentin Schneider @ 2019-09-05 14:08 UTC (permalink / raw)
To: Julia Lawall; +Cc: cocci
On 05/09/2019 13:03, Julia Lawall wrote:
>> I tried to write it as
>> ---
>> - T
>> + int
>> var
>> ? = E
>> ;
>> ---
>> but that doesn't seem to be a valid syntax.
>
> No. ? is restricted to complete statements.
>
Makes sense now that I'm looking at other snippets, thanks.
>>
>>
>> Now, I'd like to match declarations of several variables in a single
>> statement as well.
>>
>> Say I want to get something like
>>
>> - long a, b = 2, c, d = 42, e;
>> + long a, b = 2, c, e;
>> + int d = 42;
>>
>> (match on longs initialized to 42)
>>
>> My current attempt is
>> ---
>> @@
>> identifier var;
>> type T = long;
>> expression L, R;
>> @@
>>
>> T L,
>> - var = 42
>> R;
>> + int var;
>> ---
>> But that isn't valid either. How should I go about writing this sort of
>> rule?
>
> Declaartions with multiple variables are tricky. By the following may
> work:
>
> - long
> + int
> x;
>
> Since you are leaving the variables alone in this case, I think this will
> match thing that declare multiple variables as well.
>
I can't get it to match on something like
long a, b = 42;
Perhaps a roundabout way of getting there - would it be possible to
specify a rule where an identifier has to be a variable? That way I could
at least print their use and cross-check the output diff (I don't expect
multiple variables declaration to be common for this, but would like to
have some way of raising a flag when it does occur).
>>
>> If you're curious, there's more context about what I'm trying to achieve
>> at:
>>
>> https://lore.kernel.org/lkml/20190902210558.GA23013@avx2/
>
> I guess your next question will be about converting %ld to %d, etc.
>
> It may be helpful to look at coccinelle/demos/format.cocci
>
Thanks for the pointers & swift reply! Much appreciated.
_______________________________________________
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-09-05 14:16 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-05 11:17 [Cocci] Matching variable declarations Valentin Schneider
2019-09-05 12:03 ` Julia Lawall
2019-09-05 14:08 ` Valentin Schneider
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).