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 by glimpseindex.
  • -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_res file.
  • --version β€” show the version of spatch.
  • --date β€” show the date on which spatch was 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_res file
  • --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 by glimpseindex
  • --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 --dir by 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, .hpp and .hxx. Note that this option disables the processing of .c files 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_raw pre-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 on file.[c,cocci]
  • --ctestall β€” launch spatch on all files in tests/ having a .res
  • --cpptestall β€” launch spatch on all files in cpptests/ (C++) having a .res
  • --testall β€” launch spatch on all files in tests/ and cpptests/ having a .res
  • --test-okfailed β€” generates .{ok,failed,spatch_ok} files using .res files
  • --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 --dir option 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>

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