iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Coccinelle is a tool for matching and transforming C code with rules written in SmPL, its Semantic Patch Language. Instead of tying a change to particular lines in particular files, a semantic patch describes a code pattern, letting developers locate or update relevant instances across a codebase. It was designed to automate widespread changes such as adapting code to a changed library API, and it can also help identify suspicious code.
What Coccinelle is for
Coccinelle connects patch-like notation with the structure of C code. Developers write a semantic patch to describe a pattern and, optionally, a transformation. The tool then searches for matching code and can report candidate locations or propose edits.
The project describes API-evolution tasks such as renaming a function, adding an argument whose value depends on context, or reorganizing a data structure. Its examples also cover conversions to helpers such as ARRAY_SIZE and checks for suspicious expressions. Some examples are specific to Linux; the underlying approach can apply to similar maintenance problems elsewhere.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Julia Lawall and Gilles Muller described the design in their 2018 USENIX Annual Technical Conference paper: “The novel contribution of Coccinelle was that it allows software developers to write code manipulation rules in terms of the code structure itself, via a generalization of the patch syntax.” Read the paper.
#1 Best Overall
How a semantic patch differs from an ordinary patch
| Approach | What it describes | How it applies | Typical result |
|---|---|---|---|
| Ordinary patch | Changes associated with specific file contents and locations | Applies where the expected context matches | A concrete edit at those locations |
| Coccinelle semantic patch | A structural pattern and, when desired, a transformation | Finds relevant matching sites across the selected codebase | A report of candidates or proposed transformations, depending on the mode |
This makes semantic patches useful when the same conceptual change appears in many forms or locations. The rule expresses the intended code-level relationship rather than requiring developers to enumerate every occurrence by line number.
How to run Coccinelle on the Linux kernel
The Linux kernel integrates Coccinelle through the make coccicheck target. By default, it applies semantic patches from scripts/coccinelle across the kernel. You can select one semantic patch with the COCCI make variable or narrow the run to files using the make variables documented by the kernel.
- Install Coccinelle. Use a distribution package or the project’s current release. Kernel semantic patches rely on features and options provided by Coccinelle version 1.0.0-rc11 or later; consult the kernel Coccinelle documentation for current requirements.
- Choose a mode. The kernel documents
patch,report,context, andorgmodes.patchproposes fixes where supported;reportlists matches and messages. Not every semantic patch supports every mode. - Run the target. From the kernel source tree, use
make coccicheck, adding the documented make variables to select a mode, semantic patch, or file scope as needed. - Inspect the output. Treat reports as candidates and review proposed changes in their surrounding code before acting on them.
For exact variable names and invocation details, use the rolling Linux kernel documentation, since available options can vary with the kernel and Coccinelle version.
Recommended Free Tools
Can Coccinelle find bugs?
It can help flag suspicious patterns and identify places where a bug-related rule matches, but a match is not proof of a defect. The kernel documentation warns that Coccinelle, like other static analyzers, can produce false positives; users should check reports and review patches.
Transformation rules also need to preserve program behavior. For example, a rewrite to BUG_ON must avoid discarding expressions with side effects. A rule can therefore include safety conditions, and developers still need to judge whether the match and proposed edit are appropriate in context.
What the Linux adoption figures show—and do not show
Lawall and Muller’s 2018 paper gives a historical snapshot of Coccinelle’s use in Linux, not a current project total. In the context of Linux kernel version 4.15, which contained 16.5 million lines of code in January 2018, they reported 59 semantic patches in the kernel source tree and more than 6,000 kernel commits associated with Coccinelle; 900 of those commits came from Linux kernel maintainers. The paper also described roughly 13,000 commits per release as a rate of that period. These figures should not be read as present-day counts or rates. Source: USENIX Annual Technical Conference paper, 2018.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Where to learn more
The Coccinelle project site explains the tool, SmPL, and example semantic patches. The kernel documentation covers the kernel integration and usage modes. The project also lists tutorials, workshop exercises, papers, and videos on its resources page; the source mirror documents the spatch executable and installation information at GitHub.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

