Skip to contentSkip to conversation

Add code after local variable declarations ?

Cocci Jerome Glisse 3 participants 9 messages Apr 16, 2019 – Apr 17, 2019

lore
Share & tools
lore
#0 lore raw

I would like to add code after local variable declaration but there is nothing particular to match there for me. Roughly i want to do:

    void FooFun(..., struct fooicareabout *identifier, ...)
    {
        // bunch of local variable

        // I want to add code here after all local variables
    }

The thing i match on is one of the function argument. So is that do- able ? If so any pointer on how to do this would be much appreciated.

Thank you
Jérôme
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#1 lore raw
Quoted 9 lines · Jerome Glisse

I would like to add code after local variable declaration but there is nothing particular to match there for me. Roughly i want to do:

    void FooFun(..., struct fooicareabout *identifier, ...)
    {
        // bunch of local variable

        // I want to add code here after all local variables
    }

@@
statement S,S1;
@@

foo(...) {
... when != S
+ added code
S1
...
}

A declaration doesn't match S.

Quoted 3 lines

The thing i match on is one of the function argument. So is that do- able ? If so any pointer on how to do this would be much appreciated.

I'm not sure what you are trying to do here. You can certainly make a pattern for your parameters like you have shown in the example. Do you mean that you want to go find the argument value at the call site?

julia
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#2 lore raw

Re: [Cocci] Add code after local variable declarations?

Quoted 4 lines

@@
statement S,S1;
@@

Quoted 8 lines

foo(...) {
... when != S
+ added code
S1
...
}

A declaration doesn't match S.

Can this information trigger additional considerations for the handling of variable definitions?

Would you like to add any source code only after a variable declaration (or definition) block?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#3 lore raw

Re: [Cocci] Add code after local variable declarations?

Quoted 11 lines · Markus Elfring
Quoted 11 lines

@@
statement S,S1;
@@

foo(...) {
... when != S
+ added code
S1
...
}

Quoted 6 lines
Quoted 1 line

A declaration doesn't match S.

Can this information trigger additional considerations for the handling of variable definitions?

Would you like to add any source code only after a variable declaration

Coccinelle doesn't allow this. It has no way of knowing whether other declarations come afterwards, and in the Linux kernel it is not allowed to mix statements and declarations.

julia

Quoted 5 lines

(or definition) block?

Regards,
Markus

_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#4 lore raw

Re: [Cocci] Add code after local variable declarations?

Quoted 4 lines
Quoted 1 line

Would you like to add any source code only after a variable declaration

Coccinelle doesn't allow this. It has no way of knowing whether other declarations come afterwards,

I suggest to reconsider this quick response.
Are there approximations supported for such a source code search approach?

Quoted 1 line

and in the Linux kernel it is not allowed to mix statements and declarations.

There are adjustments possible for the desired scopes, aren't there?

Quoted 1 line
Quoted 1 line

(or definition) block?

Which details will become more interesting for corresponding transformation patterns?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#5 lore raw

Re: [Cocci] Add code after local variable declarations?

In reply to Julia Lawall
Quoted 4 lines
Quoted 1 line

Would you like to add any source code only after a variable declaration

Coccinelle doesn't allow this. It has no way of knowing whether other declarations come afterwards,

I suggest to reconsider this quick response.
Are there approximations supported for such a source code search approach?

Quoted 1 line

and in the Linux kernel it is not allowed to mix statements and declarations.

There are adjustments possible for the desired scopes, aren't there?

Quoted 1 line
Quoted 1 line

(or definition) block?

Which details will become more interesting for corresponding transformation patterns?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#6 lore raw
In reply to Julia Lawall
Quoted 18 lines · Julia Lawall
Quoted 9 lines · Jerome Glisse

I would like to add code after local variable declaration but there is nothing particular to match there for me. Roughly i want to do:

    void FooFun(..., struct fooicareabout *identifier, ...)
    {
        // bunch of local variable

        // I want to add code here after all local variables
    }

@@
statement S,S1;
@@

Quoted 8 lines

foo(...) {
... when != S
+ added code
S1
...
}

A declaration doesn't match S.

Oh cool did not think of that thanks. What happens if some of the declaration also have initializer that are statement ie for instance:

foobar(int a, int b, int c, struct fooicareabout *id)

{
    int toto = funbar(a, b);
    int k, l, m;

    ...
}
Quoted 8 lines
Quoted 4 lines

The thing i match on is one of the function argument. So is that do- able ? If so any pointer on how to do this would be much appreciated.

I'm not sure what you are trying to do here. You can certainly make a pattern for your parameters like you have shown in the example. Do you mean that you want to go find the argument value at the call site?

No i want to add some code to _all_ function that have a specific structure as one of their argument. Thus i match on the function parameter list for the struct i am interested in and then after all the declaration i add a code snippet.

Cheers,
Jérôme
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#7 lore raw
Quoted 26 lines · Jerome Glisse
Quoted 25 lines · Julia Lawall
Quoted 9 lines · Jerome Glisse

I would like to add code after local variable declaration but there is nothing particular to match there for me. Roughly i want to do:

    void FooFun(..., struct fooicareabout *identifier, ...)
    {
        // bunch of local variable

        // I want to add code here after all local variables
    }

@@
statement S,S1;
@@

foo(...) {
... when != S
+ added code
S1
...
}

Quoted 4 lines
Quoted 1 line

A declaration doesn't match S.

Oh cool did not think of that thanks. What happens if some of the declaration also have initializer that are statement ie for instance:

No difference.

Quoted 14 lines

foobar(int a, int b, int c, struct fooicareabout *id)

{
    int toto = funbar(a, b);
    int k, l, m;

    ...
}
Quoted 5 lines
Quoted 4 lines

The thing i match on is one of the function argument. So is that do- able ? If so any pointer on how to do this would be much appreciated.

Quoted 8 lines
Quoted 3 lines

I'm not sure what you are trying to do here. You can certainly make a pattern for your parameters like you have shown in the example. Do you mean that you want to go find the argument value at the call site?

No i want to add some code to _all_ function that have a specific structure as one of their argument. Thus i match on the function parameter list for the struct i am interested in and then after all the declaration i add a code snippet.

OK, you should be fine with the pattern you wrote.

julia
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

#8 lore raw

Re: [Cocci] Add code after local variable declarations?

In reply to Jerome Glisse
Quoted 3 lines

No i want to add some code to _all_ function that have a specific structure as one of their argument. Thus i match on the function parameter list for the struct i am interested in

This source code search approach is generally fine.

Quoted 1 line

and then after all the declaration i add a code snippet.

Our programmer eyes are trained for pattern recognition according to such a software transformation.

* How many efforts would you like to invest into the development for the automation of this refactoring (together with the semantic patch language)?

* Would you like to determine the source code positions between variable declaration (or definition) blocks and other statements safely?

* Will any additional constraints become relevant for this change attempt?

Regards,
Markus
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci