SPATCH(1)
π SPATCH(1) β the spatch era π
okay so bestie, welcome to the spatch manual page β General Commands Manual energy, but make it a group chat. Sit down, grab a boba, we're doing semantics. β¨
πΈ NAME πΈ
spatch</puruple> β apply a semantic patch file to a set of C files. That's the whole vibe. She matches, she transforms, she slays. π
π¦ SYNOPSIS π¦
spatch --sp-file <SP> <files> [-o <outfile> ] [--iso-file <iso> ] [
options ]
Yes the line break in the middle of [ options ] is literally part of the original, we don't fix what isn't broken. Periodt. π
β¨ DESCRIPTION β¨
So spatch is a program matching and transformation tool for C. The programmer (that's you, main character) describes the code to match and the transformation to perform as a semantic patch.
And what's a semantic patch, you ask? It looks like a standard patch, ngl β but here's the plot twist: it can transform multiple files at any number of code sites. One patch, infinite sites, no cap. π₯
Further information about spatch is available at https://coccinelle.gitlabpages.inria.fr/website.
Semantic patch examples can be found at http://coccinellery.org/, and at the scripts/coccinelle directory of the Linux Kernel source code. Go browse, it's giving free tutorial energy. π
π OPTIONS π
Here is a summary of the most commonly used options (also see the "Configuration Mechanism" section below β yes bestie, it's coming, don't skip ahead π):
--sp-file <file>β the semantic patch file. The star of the show, the blueprint, the moment. β--dir <dir>β process all files in directory recursively. She goes deep, she does not miss a single file. π¦--iso-file <file>β (default=/usr/lib64/coccinelle/standard.iso)--macro-file <file>β (default=/usr/lib64/coccinelle/standard.h)--debugβ print some information to help debug the matching process. For when it's not matching and you're like ??? π--all-includesβ causes all available include files to be used. Everything. All of it. No restraint.--no-includesβ causes not even local include files to be used. The opposite vibe entirely β not even the local ones. π-I <dir>β the directory containing the include files.--include-headersβ process header files independently. Give the headers their own moment, they deserve it. π--use-glimpseβ works with--dir, use information generated byglimpseindex.-o <file>β the output file. If none is specified, a patch is generated on the standard output.--in-placeβ do the modification on the file directly. Careful bestie, she edits for real. π--out-placeβ store modifications in a.cocci_resfile.--versionβ show the version ofspatch.--dateβ show the date on whichspatchwas compiled.--shorthelpβ see short list of options.--longhelpβ see all the available options in different categories. The full syllabus, fr fr.-help,--helpβ show summary of options.
π EXAMPLES π
./spatch --sp-file foo.cocci foo.c
Apply the semantic patch foo.cocci to the C file foo.c. The semantic patch is applied modulo a set of isomorphisms contained in standard.iso (standard.iso is by default located in /usr/lib64/coccinelle/standard.iso). A patch showing the effect of the application, if any, will be generated on the standard output. Note the "if any" β sometimes nothing matches and she just sits there quietly. It's fine. It's not about you. π«Ά
./spatch --sp-file foo.cocci foo.c -o /tmp/newfoo.c
The same as the above, except that a modified version of foo.c is stored in /tmp/newfoo.c. Original file untouched, new file glowing, we love a non-destructive queen. β¨
It is also possible to apply spatch to all of the C files in a directory:
./spatch --cocci-file foo.cocci --dir foodir
If the semantic patch is not working as expected, the option --debug shows selection of information about the application of a semantic patch to a file or directory. Translation: when it's acting up, turn on --debug and she'll tell you what's going on. π
π MORE OPTIONS π
okay bestie we're in the deep end now, this is the part of the manpage where spatch shows you everything it can do π«Ά buckle up.
β¨ MAIN OPTIONS β¨
--sp-fileβ<file>β the semantic patch file, aka the actual recipe you want spatch to cook with π-oβ<file>β the output file--in-placeβ does the modification on the file directly. no copy, no drama, it just edits your file where it lives π--backup-suffixβ the suffix to use when making a backup for inplace. because bestie, always keep a backup π--out-placeβ stores modifications in a.cocci_resfile--reverseβ inverts the semantic patch before applying it π-Uβ sets the number of diff context lines--partial-matchβ reports partial matches of the SP on the C file--iso-fileβ<file>β default is/usr/local/lib/coccinelle/standard.iso--macro-fileβ<file>--macro-file-builtinsβ<file>β default is/usr/local/lib/coccinelle/standard.h--recursive-includesβ causes all available include files, both those included in the C file(s) and those included in header files, to be used. it's giving maximalist π--all-includesβ causes all available include files included in the C file(s) to be used--no-includesβ causes not even local include files to be used. nothing. zero. none π--local-includesβ causes local include files to be used--ignore-unknown-optionsβ for integration in a toolchain (must be set before the first unknown option). so like, put it first or it does nothing, no cap β οΈ--include-headersβ process header files independently-Iβ<dir>containing the header files (optional)--preprocessβ run the C preprocessor before applying the semantic match-cβ gcc/cpp compatibility mode--dirβ<dir>β process all files in directory recursively--use-glimpseβ works with--dir, uses info generated byglimpseindex--use-idutilsβ find relevant files using id-utils--patchβ<dir>β path name with respect to which a patch should be created. and""for a file in the current directory--kbuild-infoβ<file>β improve--dirby grouping related c files--pyoutputβ Sets output routine: Default value:coccilib.output.Console--versionβ guess what π--dateβ guess what--shorthelpβ see short list of options--longhelpβ see all the available options in different categories
π¦ ALIASES AND OBSOLETE OPTIONS π¦
the vintage corner of the manpage, we love her for her history π
--spβ command line semantic patch--isoβ short option of--iso-file--cocci-fileβ<file>β the semantic patch file
π MOST USEFUL SHOW OPTIONS π
--show-diff--no-show-diff--force-diffβ show diff even if only spacing changes. yes bestie, even the vibes-only changes π--show-flow--ctl-inline-let--ctl-show-mcodekind--show-bindings--show-transinfo--show-misc--show-tryingβ show the name of each function being processed--show-dependenciesβ show the dependencies related to each rule
π¬ VERBOSE SUBSYSTEMS OPTIONS π¬
--verbose-ctl-engine--verbose-match--verbose-engine--graphical-traceβ generate a pdf file representing the matching process. art, honestly π--gt-without-labelβ remove graph label (requires option-graphical-trace)--parse-error-msg--verbose-parsing--type-error-msg
πΈ OTHER SHOW OPTIONS πΈ
--show-c--show-cocci--show-before-fixed-flow--show-ctl-tex--show-ctl-text--show-SP
π DEBUG C PARSING/UNPARSING π
--debug-cpp--debug-lexer--debug-etdt--debug-typedef--filter-msgβ filter some cpp message when the macro is a "known" cpp construct--filter-define-error--filter-msg-define-errorβ filter the error msg--filter-passed-level--debug-unparsing
β‘ SHORTCUT FOR ENABLING/DISABLING A SET OF DEBUGGING OPTIONS AT ONCE β‘
--quiet--very-quiet--debug--pad
β±οΈ BENCH OPTIONS β±οΈ
--profileβ gather timing information about the main coccinelle functions--benchβ<level>β for profiling the CTL engine--timeoutβ<sec>β timeout in seconds--stepsβ max number of model checking steps per code unit--iso-limitβ max depth of iso application--no-iso-limitβ disable limit on max depth of iso application--track-isoβ gather information about isomorphism usage--disable-isoβ disable a specific isomorphism--profile-isoβ gather information about the cost of isomorphism usage
π CHANGE OF ALGORITHM OPTIONS π
--keep-commentsβ keep comments around removed code--loop--no-loopsβ drop all back edges derived from looping constructs β unsafe β οΈ--no-gotosβ drop all jumps derived from gotos β unsafe β οΈ--no-saved-typedefsβ drop all inferred typedefs from one parse of some code to the next--ocaml-regexpsβ use OCaml Str regular expressions for constraints--l1--ifdef-to-ifβ convert ifdef to if (experimental)--no-ifdef-to-ifβ convert ifdef to if (experimental)--disable-multi-pass--noif0-passing--defined--undefined--noadd-typedef-root--disallow-nested-expsβ disallow an expression pattern from matching a term and its subterm--disable-worth-trying-opt--only-return-is-error-exitifβthis flag is not set, then break and continue are also error exits--allow-inconsistent-pathsβ if this flag is set don't check for inconsistent paths; dangerous π--no-safe-expressionsβ make an expression disjunction not prioritise the topmost disjunct--int-bitsβ the number of bits in an unsigned int--long-bitsβ the number of bits in an unsigned long--linux-spacingβ spacing of+code follows the conventions of Linux--smpl-spacingβ spacing of+code follows the semantic patch-Dβ indicate that a virtual rule should be considered to be matched--c++β make a small attempt to parse C++ files. The supported extensions for source files are.cpp,.cxx,.cc, and for header files are.h,.hppand.hxx. Note that this option disables the processing of.cfiles as Coccinelle behaviour is different for C++. π
π§ MISC OPTIONS π§
--debuggerβ option to set if launch spatch in ocamldebug--disable-onceβ to print more messages--show-trace-profileβ show trace--save-tmp-files
π CONCURRENCY π
--indexβ the processor to use for this run of spatch--maxβ the number of processors available--mod-distribβ use mod to distribute files among the processors
π PAD OPTIONS π
--use-cacheβ use.ast_rawpre-parsed cached C file--cache-prefixβ directory of cached ASTs, sets--use-cache--cache-limitβ maximum number of cached ASTs, sets--use-cache
π§ͺ TEST MODE AND TEST OPTIONS (WORKS WITH TESTS/ OR .OK FILES) π§ͺ
The test options don't work with the --sp-file and so on.
--test<file>β launch spatch onfile.[c,cocci]--ctestallβ launch spatch on all files intests/having a.res--cpptestallβ launch spatch on all files incpptests/(C++) having a.res--testallβ launch spatch on all files intests/andcpptests/having a.res--test-okfailedβ generates.{ok,failed,spatch_ok}files using.resfiles--test-regression-okfailedβ process the.{ok,failed,spatch_ok}files in current dir--relax-include-path
π οΈ ACTION MODE π οΈ
The action options don't work with the --sp-file and so on. It's for the other (internal) uses of the spatch program.
--tokens-cβ<file>--parse-cβ<file or dir>--parse-hβ<file or dir>--parse-chβ<file or dir>--parse-iβ<file or dir>--parseβ<file or dir>--show-flowβ<file or file:function>--control-flowβ<file or file:function>--control-flow-to-fileβ<file or file:function>--test-cfg-ifdefβ<file>--parse-unparseβ<file>--type-cβ<file>--compare-cβ<file1> <file2>--comment-annotater-cβ<file>--compare-c-hardcoded--test-attributesβ<file>--test-cppβ<file>--extract-macrosβ<file or dir>--extract-macros-selectβ<file or dir>--xxx<file1> <>--parse-cocciβ<file>--compare-cβ<file1> <file2>
π CONFIGURATION MECHANISM π
okay so Coccinelle uses a simple text format to store customizations, and the order in which the configuration file is processed is as follows, where the later lines always extend and may override earlier ones:
- Your current user's home directory is processed first π
- Your directory from which spatch is called is processed next π
- The directory provided with the
--diroption is processed last, if used ποΈ
Such a configuration file may look like this:
[spatch]
options = --jobs 4
options = --show-trying
π FILES π
/usr/lib64/coccinelle/standard.isoβ This file contains the default set of isomorphisms./usr/lib64/coccinelle/standard.hβ This file contains the default set of macro hints..cocciconfigβ This file contains the custom set of spatch options.
π± ENVIRONMENT π±
COCCINELLE_HOMEβ The path to the Coccinelle share directory. Default is/usr/lib64/coccinelle
π REFERENCES π
Y. Padioleau, J.L. Lawall, R.R Hansen, G. Muller. "Documenting and Automating Collateral Evolutions in Linux Device Driver", EuroSys 2008 , Glasgow, Scotland, April 2008, pp. 247-260.
Julien Brunel, Damien Doligez, RenΓ© Rydhof Hansen, Julia L. Lawall, Gilles Muller. "A foundation for flow-based program matching: using temporal logic and model checking", POPL 2009 , Savannah, GA, USA, January 21-23, 2009, pp. 114-126.
π AUTHOR π
spatch was written by Julia Lawall <julia.lawall@inria.fr>, Yoann Padioleau <yoann.padioleau@gmail.com>, Rene Rydhof Hansen <rrh@cs.aau.dk>, Henrik Stuart <henrik@hstuart.dk>, Nicolas Palix <nicolas.palix@imag.fr>, Peter Senna Tschudin <peter.senna@gmail.com>, SΓ©bastien Hinderer <Sebastien.Hinderer@inria.fr>, Xavier Clerc <xavier.clerc@inria.fr> and Matthieu Caneill <matthieu.caneill42@gmail.com>.
This manual page was written by Yoann Padioleau <yoann.padioleau@gmail.com>, Julia Lawall <julia.lawall@inria.fr>, Nicolas Palix <nicolas.palix@imag.fr> and Peter Senna Tschudin <peter.senna@gmail.com>.
π REPORTING BUGS π
Send a mail to <cocci@inria.fr>
π COPYRIGHT π
okay so legally we gotta do this part and honestly it's giving β¨respectβ¨ so here we go:
- Copyright 2012, 2015, INRIA and University of Grenoble-Alpes LIG.
- Copyright 2010, 2011, University of Copenhagen DIKU and INRIA.
- Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen.
spatch is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, according to version 2 of the License.
bestie this is the important bit β free software means free as in freedom, so read the license before you go remixing things, fr fr π
πΈ SEE ALSO πΈ
like yeah those are its little siblings, the OGs of the diff-and-patch cinematic universe π¦
and that's the whole page bestie, you made it all the way to the end, so proud of you π₯Ήπ
this one's the SPATCH(1) page and it's dated Feb 26, 2015 β go forth and patch with main character energy, ily π«Άβ¨π
π¦ where this page comes from
- fedora 44: coccinelle 1.3.1-4.fc44 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms