Coccinelle

Source code manipulation

Coccinelle is a tool for manipulating C source code. Because it really understands C, you can make complex changes, e.g. If x is an integer, replace if (!x) with if (x == 0)

This means that you could define some code style rules and automatically check that they’re being used.

Coccinelle uses a transformation language that it applies to C source. The output is a diff that can be applied.

/* Source file to be checked */
int main()
{
  char *x = NULL;

  if (x == NULL)
    something();
}
// Find pointers that are checked against NULL
@@
type T;
identifier I;
statement S1;
@@

T *I;

(
- if (I == NULL)
+ if (!I)
S1
)
# Generate the diff
spatch --sp-file null-check.cocci source.c
--- source.c
+++ source.c
@@ -4,6 +4,6 @@ int main()
 {
   char *x = NULL;
 
-  if (x == NULL)
+  if (!x)
     something();
 }

Here are more examples that have been used on NeoMutt:

Search by Algolia