Skip to contentSkip to conversation

Problems: Adding Include directives, Adding ({}) construct

Cocci sebald.ziegler.cocci at ikolus.de 2 participants 5 messages Feb 27, 2017 – Mar 7, 2017

lore
Share & tools
lore
#0 lore raw

Hi,

I am trying to use coccinelle to add an include directive before the other includes and one after the includes:

@PreInclude@
@@
+ #include "pre.h"
#include ...

@PostInclude@
@@
#include ...
+ #include "post.h"

this works well if the source file (all includes are always at the top of the file) contains more than one include directive. However, if there is only one include directive -> only the PreInclude rule is executed.

So for this program:
#include <stdio.h>

int main() {
    return 0;
}

This is the result:
#include "pre.h"
#include <stdio.h>

int main() {
    return 0;
}

So it seems that only one rule can match some code line/code pattern. Am I mistaken? What could be an approach that works for both the single include directive and multiple include directives case?

Another Question:
With the following rule I want to add a macro definition just above the main function:
@macro_square@
@@
+ #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })
int main(int argc, char ** argv) {
...
}

If I execute spatch on the program above this error is reported (coccinelle
version: 1.0.6):
plus: parse error:
File "/tmp/testing/test.c-brackets.cocci", line 3, column 21, charpos = 39 around = '{',
whole content = + #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })

Is this kind of code not supported or did I make a mistake somewhere?

Thank you for any information and hints!

Regards,
Sebald

#1 lore raw
Quoted 11 lines · sebald.ziegler.cocci at ikolus.de

Hi,

I am trying to use coccinelle to add an include directive before the other includes and one after the includes:

@PreInclude@
@@
+ #include "pre.h"
#include ...

Quoted 8 lines

@PostInclude@
@@
#include ...
+ #include "post.h"

this works well if the source file (all includes are always at the top of the file) contains more than one include directive. However, if there is only one include directive -> only the PreInclude rule is executed.

Sorry not to have responded sooner.

This seems quite strange. Each rule sees the result of the previous one, so by the time it reaches the post include rule, there would be two includes in the program. I will check on it.

Quoted 12 lines

So for this program:
#include <stdio.h>

int main() {
    return 0;
}

This is the result:
#include "pre.h"
#include <stdio.h>

Quoted 6 lines
int main() {
    return 0;
}

So it seems that only one rule can match some code line/code pattern. Am I mistaken?

This is not correct. Coccinelle works on the first rule, then provides the resulting code to the second rule, and so on.

Quoted 14 lines

What could be an approach that works for both the single include directive and multiple include directives case?

Another Question:
With the following rule I want to add a macro definition just above the main function:
@macro_square@
@@
+ #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })
int main(int argc, char ** argv) {
...
}

Quoted 8 lines

If I execute spatch on the program above this error is reported (coccinelle
version: 1.0.6):
plus: parse error:
File "/tmp/testing/test.c-brackets.cocci", line 3, column 21, charpos = 39 around = '{',
whole content = + #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })

Is this kind of code not supported or did I make a mistake somewhere?

It is quite possible that ({ }) is not supported in the pattern matching language. There is a hackish solution:

@script:python r@
str;
@@

coccinelle.str = "({printf("SQUARE(x)\n"); x*x; })"

@macro_square@
idntifier r.str;
@@
+ #define SQUARE(x) str
int main(int argc, char ** argv) {
...
}

Coccinelle won't actually check that what you have put into the variable str is an identifier.

julia

#2 lore raw
Quoted 95 lines · CET Julia Lawall
Quoted 11 lines · sebald.ziegler.cocci at ikolus.de

Hi,

I am trying to use coccinelle to add an include directive before the other includes and one after the includes:

@PreInclude@
@@
+ #include "pre.h"
#include ...

Quoted 8 lines

@PostInclude@
@@
#include ...
+ #include "post.h"

this works well if the source file (all includes are always at the top of the file) contains more than one include directive. However, if there is only one include directive -> only the PreInclude rule is executed.

Sorry not to have responded sooner.

This seems quite strange. Each rule sees the result of the previous one, so by the time it reaches the post include rule, there would be two includes in the program. I will check on it.

Quoted 13 lines

So for this program:
#include <stdio.h>

int main() {

    return 0;

}

This is the result:
#include "pre.h"
#include <stdio.h>

Quoted 8 lines

int main() {

    return 0;

}

So it seems that only one rule can match some code line/code pattern. Am I mistaken?

This is not correct. Coccinelle works on the first rule, then provides the resulting code to the second rule, and so on.

Quoted 18 lines

What could be an approach that works for both the single include directive and multiple include directives case?

Another Question:
With the following rule I want to add a macro definition just above the main function:
@macro_square@
@@
+ #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })
int main(int argc, char ** argv) {
...
}

If I execute spatch on the program above this error is reported (coccinelle
version: 1.0.6):

Quoted 7 lines

plus: parse error:
File "/tmp/testing/test.c-brackets.cocci", line 3, column 21, charpos = 39
around = '{',
whole content = + #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })

Is this kind of code not supported or did I make a mistake somewhere?

It is quite possible that ({ }) is not supported in the pattern matching language. There is a hackish solution:

@script:python r@
str;
@@

coccinelle.str = "({printf("SQUARE(x)\n"); x*x; })"

@macro_square@
idntifier r.str;
@@
+ #define SQUARE(x) str
int main(int argc, char ** argv) {
...
}

Quoted 4 lines

Coccinelle won't actually check that what you have put into the variable str is an identifier.

julia

Thank you very much for your answer!
I used the workaround you described and can now generate those macros without errors. However, there is a weird thing happening: Before every second " a line break is added:

When I use this coccinelle script:
@script:python macros@
SQUARE_str;
@@
coccinelle.SQUARE_str = '({printf("SQUARE\n"); printf("SQUARE\n"); x*x; })'

@macro_wrapper_int_SQUARE_int@
identifier macros.SQUARE_str;
@@
+#define SQUARE(x) SQUARE_str
int main(int argc, char ** argv) {
...
}

Then this is generated as diff:
[...]
+#define SQUARE(x) ({printf("SQUARE
+"); printf("SQUARE
+"); x*x; })
[...]

Is there a simple solution for this?

Thanks,
Sebald

#3 lore raw
Quoted 120 lines · sebald.ziegler.cocci at ikolus.de
Quoted 95 lines · CET Julia Lawall
Quoted 11 lines · sebald.ziegler.cocci at ikolus.de

Hi,

I am trying to use coccinelle to add an include directive before the other includes and one after the includes:

@PreInclude@
@@
+ #include "pre.h"
#include ...

Quoted 8 lines

@PostInclude@
@@
#include ...
+ #include "post.h"

this works well if the source file (all includes are always at the top of the file) contains more than one include directive. However, if there is only one include directive -> only the PreInclude rule is executed.

Sorry not to have responded sooner.

This seems quite strange. Each rule sees the result of the previous one, so by the time it reaches the post include rule, there would be two includes in the program. I will check on it.

Quoted 13 lines

So for this program:
#include <stdio.h>

int main() {

    return 0;

}

This is the result:
#include "pre.h"
#include <stdio.h>

Quoted 8 lines

int main() {

    return 0;

}

So it seems that only one rule can match some code line/code pattern. Am I mistaken?

This is not correct. Coccinelle works on the first rule, then provides the resulting code to the second rule, and so on.

Quoted 18 lines

What could be an approach that works for both the single include directive and multiple include directives case?

Another Question:
With the following rule I want to add a macro definition just above the main function:
@macro_square@
@@
+ #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })
int main(int argc, char ** argv) {
...
}

If I execute spatch on the program above this error is reported (coccinelle
version: 1.0.6):

Quoted 7 lines

plus: parse error:
File "/tmp/testing/test.c-brackets.cocci", line 3, column 21, charpos = 39
around = '{',
whole content = + #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })

Is this kind of code not supported or did I make a mistake somewhere?

It is quite possible that ({ }) is not supported in the pattern matching language. There is a hackish solution:

@script:python r@
str;
@@

coccinelle.str = "({printf("SQUARE(x)\n"); x*x; })"

@macro_square@
idntifier r.str;
@@
+ #define SQUARE(x) str
int main(int argc, char ** argv) {
...
}

Quoted 4 lines

Coccinelle won't actually check that what you have put into the variable str is an identifier.

julia

Thank you very much for your answer!
I used the workaround you described and can now generate those macros without errors. However, there is a weird thing happening: Before every second " a line break is added:

When I use this coccinelle script:
@script:python macros@
SQUARE_str;
@@
coccinelle.SQUARE_str = '({printf("SQUARE\n"); printf("SQUARE\n"); x*x; })'

@macro_wrapper_int_SQUARE_int@
identifier macros.SQUARE_str;
@@
+#define SQUARE(x) SQUARE_str
int main(int argc, char ** argv) {
...
}

Quoted 8 lines

Then this is generated as diff:
[...]
+#define SQUARE(x) ({printf("SQUARE
+"); printf("SQUARE
+"); x*x; })
[...]

Is there a simple solution for this?

Try \\n instead of \n.

julia

#4 lore raw
In reply to sebald.ziegler.cocci at ikolus.de
Quoted 31 lines · sebald.ziegler.cocci at ikolus.de

Hi,

I am trying to use coccinelle to add an include directive before the other includes and one after the includes:

@PreInclude@
@@
+ #include "pre.h"
#include ...

@PostInclude@
@@
#include ...
+ #include "post.h"

this works well if the source file (all includes are always at the top of the file) contains more than one include directive. However, if there is only one include directive -> only the PreInclude rule is executed.

So for this program:
#include <stdio.h>

int main() {
    return 0;
}

This is the result:
#include "pre.h"
#include <stdio.h>

Quoted 7 lines
int main() {
    return 0;
}

So it seems that only one rule can match some code line/code pattern. Am I mistaken? What could be an approach that works for both the single include directive and multiple include directives case?

It seems to be a special case for includes. Includes know whether they are the first or last include. But when you add something before or after an include, it loses this information. Afterwards, a rule that wants to add before the first include or after the last one does not apply.

julia

Quoted 30 lines

Another Question:
With the following rule I want to add a macro definition just above the main function:
@macro_square@
@@
+ #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })
int main(int argc, char ** argv) {
...
}

If I execute spatch on the program above this error is reported (coccinelle
version: 1.0.6):
plus: parse error:
File "/tmp/testing/test.c-brackets.cocci", line 3, column 21, charpos = 39 around = '{',
whole content = + #define SQUARE(x) ({printf("SQUARE(x)\n"); x*x; })

Is this kind of code not supported or did I make a mistake somewhere?

Thank you for any information and hints!

Regards,
Sebald
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci