| name | coccinelle |
| description | Run semantic patches on C/C++ via spatch. Use for AST-level refactoring, finding API usage patterns, enforcing coding standards structurally, migrating deprecated APIs, and finding null-deref / resource-leak patterns across large codebases. Pairs with the Pi `coccinelle` extension's pre-defined patterns (null-checks, error-handling, memory-leaks, resource-leaks, dead-code, simplify-conditionals, const-correctness). |
Coccinelle — C Semantic Patching
Use Coccinelle (spatch) for structured, semantic code transformations and searches across C codebases. Coccinelle understands C syntax at the AST level, unlike regex-based tools.
When to Use
- Refactoring C code (dbsql, libdb, openldap, PostgreSQL extensions)
- Finding API usage patterns across a large codebase
- Enforcing coding standards structurally
- Migrating deprecated API calls to new ones
- Finding bugs (null dereference patterns, resource leaks)
Core Commands
spatch --sp-file rule.cocci --dir src/
spatch --sp-file rule.cocci --dir src/ --dry-run
spatch --sp-file find.cocci --dir src/ -o /dev/null
spatch --sp-file rule.cocci --include-headers --dir .
SmPL (Semantic Patch Language) Patterns
Find a function call pattern
@@
expression E;
@@
- malloc(E)
+ xmalloc(E)
Find null-check-after-use bugs
@@
expression p, E;
@@
*p = ...;
... when != p = E
*if (p == NULL) { ... }
Rename a function across codebase
@@
@@
- old_function_name(
+ new_function_name(
...)
Add error check after allocation
@@
expression p;
@@
p = malloc(...);
+ if (p == NULL) return -ENOMEM;
Best Practices
- Always use
--dry-run first to preview changes
- Use
... when != X to express "without intervening X"
- Use
* prefix on lines to mark them for reporting (search mode)
- Use
@@ metavariable blocks to bind expressions, types, identifiers
- Combine with
--dir to scope to specific subdirectories
- Use
--include-headers when patterns span .h and .c files