October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Coccinelle: How Semantic Patches Change C Code Across a Project

Coccinelle uses structural semantic patches to report or transform C code across a project. Here’s how SmPL works and how to use it with Linux kernel coccicheck.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Coccinelle is a tool for finding and transforming patterns in C source code. Its Semantic Patch Language (SmPL) lets developers describe a code change by structure rather than by exact file names and line numbers, so one rule can address many relevant locations. In the Linux kernel, developers can run it through make coccicheck to report matches or propose changes; every result still needs review because matches can be false positives.

What Coccinelle does

Coccinelle addresses two related tasks: applying widespread code changes and identifying code patterns worth investigating. It was developed in response to “collateral evolutions”—updates required in many callers when a library or API changes. It is also used to look for potential bugs in systems code.

As an Amazon Associate I earn from qualifying purchases.

An ordinary patch typically records changes against particular lines in particular files. A semantic patch instead describes the relevant code pattern and the change to make. That distinction helps when a change appears in many places but the exact surrounding lines differ.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How semantic patches work

SmPL, the Semantic Patch Language, expresses a rule in a patch-like notation while matching code structure. The developer describes the pattern to find and, when appropriate, the transformation to apply. Coccinelle then searches for matching locations and can report them or produce transformed code.

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.”

The Coccinelle project’s examples include API changes such as renaming a function, adding an argument whose value depends on context, and reorganizing a data structure. Other examples cover conversions to helpers such as ARRAY_SIZE, rounding helpers, and checks for suspicious expressions. These examples illustrate the range of patterns the tool can express; they are not proof that every matching expression is incorrect.

How to run Coccinelle on the Linux kernel

The kernel’s coccicheck make target runs semantic patches from scripts/coccinelle by default. The kernel documentation describes four output modes. The exact variables and requirements are documented in the Linux kernel Coccinelle guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Mode What it does Best use
report Lists matching locations and messages. Reviewing candidate matches without applying a transformation.
context Shows matches with surrounding code context. Understanding a match in its local code.
patch Proposes fixes when the semantic patch supports a transformation. Generating candidate changes for inspection and review.
org Produces Org-format output. Using results in an Org-oriented workflow.

Not every semantic patch supports every mode. To run one specific semantic patch rather than the default set, the kernel documentation provides the COCCI make variable. The make target also supports narrowing a run to selected files through documented make variables; consult the guide for the current variable names and syntax.

  1. Install a compatible Coccinelle version. The kernel guide says its semantic patches use features and options available in Coccinelle 1.0.0-rc11 and later. It points to distribution packages and the project’s current release for installation options.
  2. Choose a scope. Use the default target to run the kernel’s semantic patches, or select a particular rule with COCCI and narrow the files if that is appropriate.
  3. Select an output mode. Use report or context to inspect candidates; use patch where the rule supports proposing changes. Check the documentation for mode support before assuming a rule can transform code.
  4. Run make coccicheck and inspect the output. Treat reports as leads and generated patches as proposals, not verified fixes. Check each relevant match and review any resulting patch before using it.

Can Coccinelle find bugs?

It can help identify suspicious code patterns, but a match is not automatically a defect. Static analysis may produce false positives, and a transformation can be unsafe if it ignores how an expression behaves. For example, replacing a condition with a BUG_ON form must not discard expressions with side effects. Review the matched code’s meaning and the rule’s assumptions before accepting a result.

Coccinelle is therefore most useful as a way to automate systematic searches and propose consistent edits, with a developer responsible for validating applicability and correctness.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What the historical adoption figures show

Lawall and Muller’s 2018 paper measured Coccinelle’s impact at that time. Its figures describe a historical study, not current project totals or current kernel size.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Measure reported Historical figure Qualification
Linux kernel commits attributed to Coccinelle Over 6,000 Reported by Lawall and Muller in their 2018 USENIX Annual Technical Conference paper; 900 were from Linux kernel maintainers.
Semantic patches in the Linux kernel source tree 59 Count reported in the 2018 paper.
Linux kernel code size 16.5 million lines Kernel version 4.15, January 2018, as reported in the paper.
Commits per release Around 13,000 Period-specific context characterized as “recently” in the paper’s 2018 discussion, not a present-day rate.

The paper’s design discussion and measurements are available in “Coccinelle: 10 Years of Automated Evolution in the Linux Kernel”.

Best Value

Where to learn more

The Coccinelle project site provides an overview, examples, and links to learning resources. Its GitHub mirror documents the source and the spatch executable. The project also lists papers, tutorials, workshop exercises, and videos in its papers and resources section. For kernel-specific integration and current usage details, use the kernel’s Coccinelle documentation.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.