Coccinelle is a program matching and transformation engine which provides the language SmPL (Semantic Patch Language) for specifying desired matches and transformations in C code
Questions tagged [coccinelle]
18 questions
5
votes
2 answers
Coccinelle output
I am a beginner in coccinelle and try to run my first example.
Currently I'am following the steps of this article
I created the c file
I created the coccinelle script
I run it using
$ spatch -sp_file test.cocci test.c
In the terminal I got…

fedi
- 368
- 3
- 7
- 18
5
votes
1 answer
Adding missing NULL checks after malloc with coccinelle
I want to write a semantic patch for coccinelle, so that it will add if (ptr == NULL) ... checks after calls to malloc where they are missing.
Let's say I have the following input source code:
#include
#include
#include…

hlovdal
- 26,565
- 10
- 94
- 165
4
votes
2 answers
Detect passing pointer to uninitialized variable
Some functions have a pointer argument that points to where a result from
calling the function should be stored, but the function also require that
when calling the function this pointer points to some value used as input value
(e.g. an in/out…

hlovdal
- 26,565
- 10
- 94
- 165
4
votes
1 answer
Can I use coccinelle to change argument types in a function signature
I am new to coccinelle and trying to find how can I change argument types in function signatures.
I have a function that takes a pointer of type T1 as an argument
int fn(T1* p)
I would like to change the function to take an argument of type T2 as…

pk-sk
- 41
- 1
3
votes
0 answers
Limit Coccinelle matches to expression of given type
A transform like the one below, works for func_1(&something->field) when something is a foo_t but does not catch cases where v is itself a field such as func_1(&something->v->field)
@@
typedef foo_t;
foo_t *v;
@@
- func_1(&v->field)
+ func_2(v)
On…

Akuri
- 33
- 3
3
votes
2 answers
Coccinelle: Change ordered struct declaration to unordered
I'm trying to find some resources/tutorials to help me create a coccinelle script to find structure declarations and change them from ordered to unordered.
In our code base we use several structs hundreds of times. Somebody added a member in the…

DarthNoodles
- 370
- 3
- 11
2
votes
0 answers
Remove a field from a struct instantiation with spatch
I keep getting errors when I try to spatch out a field of a struct inside an instantiation:
@@
expression e; identifier i;
@@
static struct genl_family i = {
...
- .id = e
...
}
outputs:
init_defs_builtins: /usr/lib/coccinelle/standard.h
75…

Tyler Curtis Jowers
- 19
- 3
2
votes
1 answer
Coccinelle rule to match foo() call inside an if
So, the problem I stumble upon is that code inside if can be pretty complex, it can be stuff like if (NOT(ret = foo())) and also if (foo() == NULL), and other variations are possible.
To me the obvious answer is the rule line if (...foo()...), but…

Hi-Angel
- 4,933
- 8
- 63
- 86
2
votes
1 answer
Coccinelle help to replace a function with variable args
I have something like this
void test_fn(int a, ...)
{
...
}
int main(int argc, char *argv[])
{
int b,a1,a2,a3;
....
b = test_fn(a1,a2,a3);
return 0;
}
I want to replace test_fn with a different function func_1. Both test_fn and func_1 have…

nogeek001
- 713
- 1
- 7
- 14
2
votes
1 answer
Matching *ptr = value with coccinelle
I want to match
*ptr=value;
in a C code and then replace it by
CHECK_PTR(ptr)
*ptr=value;
CHECK_PTR() is a macro that check if ptr is not null
I write a Coccinelle script to do the work
@rule1@
type T;
T* ptr;
expression…

fedi
- 368
- 3
- 7
- 18
1
vote
4 answers
Regex for refactoring to arrays
I have a very big C programming project that uses thousands of struct variables with this naming convention:
specificstruct->x = specificstruct->y + specificstruct->z
I want to do some heavy refactoring, namely converting most of these struct…
Per Nilsson
1
vote
1 answer
How can I find string ending with `\n` in coccinelle
I have a macro in C, say LOG(), which prints given string. The macro adds "\n" at the end. Because of this, if we have LOG("hello\n"), the new line is redundant.
How can I find such macro with newline at the end with coccinelle and remove it?
BTW,…

Yasushi Shoji
- 4,028
- 1
- 26
- 47
1
vote
0 answers
Change struct names from upper-case to lower-case using coccinelle
I am trying to use Coccinelle to update a large number of files with C code to the Linux coding styles, but I can't manage to change the struct names. I am using the following script:
@rule1 disable all@
type t;
@@
typedef struct {
...
} t;
@…

curious_penguin
- 151
- 6
1
vote
1 answer
Using coccinelle out of the kernel tree
Is it possible to use coccinelle out of a kernel tree using the kernel rules in scripts/coccinelle/ something like:
hedin@home:~/projects/linux/eudyptula$ spatch --sp-file /home/hedin…

Roman Storozhenko
- 369
- 1
- 3
- 16
1
vote
2 answers
Pointer issues in Coccinelle
I want to change my code using a Coccinelle script:
// Before modification
#include
#include
int main()
{
int i;
int *p;
*p=i;
return 0;
}
The expected result is:
// After modification
#include…

fedi
- 368
- 3
- 7
- 18