git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 08/12] meson: wire up static analysis via Coccinelle

From
Patrick Steinhardt <ps@pks.im>
Date
Feb 18, 2025, 07:45 UTC
Message-ID
<20250218-b4-pks-meson-contrib-v1-8-c3edd292beb8@pks.im>
In-Reply-To
<20250218-b4-pks-meson-contrib-v1-0-c3edd292beb8@pks.im>

Wire up static analysis via Coccinelle via a new test target "coccicheck". This target can be executed via `meson compile coccicheck` and generates the semantic patch for us.

Note that we don't hardcode the list of source and header files that shall be analyzed, and instead use git-ls-files(1) to find them for us. This is because we also want to analyze files that may not get built on the current platform, so finding all sources at configure time is easier than introducing a new variable that tracks all sources, including those which aren't being built.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 contrib/coccinelle/meson.build | 89 ++++++++++++++++++++++++++++++++++++++++++
 contrib/meson.build            |  1 +
 meson_options.txt              |  2 +
 3 files changed, 92 insertions(+)
diff --git a/contrib/coccinelle/meson.build b/contrib/coccinelle/meson.build
new file mode 100644
index 00000000000..5d76a7fee6f
--- /dev/null
+++ b/contrib/coccinelle/meson.build
@@ -0,0 +1,89 @@
+spatch = find_program('spatch', required: get_option('coccinelle'))
+if not spatch.found()
+  subdir_done()
+endif
+
+third_party_sources = [
+  ':!contrib',
+  ':!compat/inet_ntop.c',
+  ':!compat/inet_pton.c',
+  ':!compat/nedmalloc',
+  ':!compat/obstack.*',
+  ':!compat/poll',
+  ':!compat/regex',
+  ':!sha1collisiondetection',
+  ':!sha1dc',
+  ':!t/unit-tests/clar',
+  ':!t/unit-tests/clar',
+  ':!t/t[0-9][0-9][0-9][0-9]*',
+]
+
+rules = [
+  'array.cocci',
+  'commit.cocci',
+  'config_fn_ctx.pending.cocci',
+  'equals-null.cocci',
+  'flex_alloc.cocci',
+  'free.cocci',
+  'git_config_number.cocci',
+  'hashmap.cocci',
+  'index-compatibility.cocci',
+  'object_id.cocci',
+  'preincr.cocci',
+  'qsort.cocci',
+  'refs.cocci',
+  'strbuf.cocci',
+  'swap.cocci',
+  'the_repository.cocci',
+  'xcalloc.cocci',
+  'xopen.cocci',
+  'xstrdup_or_null.cocci',
+  'xstrncmpz.cocci',
+]
+
+concatenated_rules = custom_target(
+  command: [
+    'cat', '@INPUT@',
+  ],
+  input: rules,
+  output: 'rules.cocci',
+  capture: true,
+)
+
+sources = [ ]
+foreach source : run_command(git, '-C', meson.project_source_root(), 'ls-files', '--deduplicate', '*.c', third_party_sources, check: true).stdout().split()
+  sources += source
+endforeach
+
+headers = [ ]
+foreach header : run_command(git, '-C', meson.project_source_root(), 'ls-files', '--deduplicate', '*.h', third_party_sources, check: true).stdout().split()
+  headers += meson.project_source_root() / header
+endforeach
+
+patches = [ ]
+foreach source : sources
+  patches += custom_target(
+    command: [
+      spatch,
+      '--all-includes',
+      '--sp-file', concatenated_rules,
+      '--patch', meson.project_source_root(),
+      '@INPUT@',
+    ],
+    input: meson.project_source_root() / source,
+    output: source.underscorify() + '.patch',
+    capture: true,
+    depend_files: headers,
+  )
+endforeach
+
+concatenated_patch = custom_target(
+  command: [
+    'cat', '@INPUT@',
+  ],
+  input: patches,
+  output: 'cocci.patch',
+  capture: true,
+)
+
+alias_target('coccicheck', concatenated_patch)
diff --git a/contrib/meson.build b/contrib/meson.build
index 569c23ee768..a88c5dfe09e 100644
--- a/contrib/meson.build
+++ b/contrib/meson.build
@@ -2,4 +2,5 @@ foreach feature : get_option('contrib')
   subdir(feature)
 endforeach
 
+subdir('coccinelle')
 subdir('credential')
diff --git a/meson_options.txt b/meson_options.txt
index c51ba88d853..afa908d6c53 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -101,6 +101,8 @@ option('docs_backend', type: 'combo', choices: ['asciidoc', 'asciidoctor', 'auto
   description: 'Which backend to use to generate documentation.')
 
 # Testing.
+option('coccinelle', type: 'feature', value: 'auto',
+  description: 'Provide a coccicheck target that generates a Coccinelle patch.')
 option('tests', type: 'boolean', value: true,
   description: 'Enable building tests. This requires Perl, but is separate from the "perl" option such that you can build tests without Perl features enabled.')
 option('test_output_directory', type: 'string',
-- 
2.48.1.666.gff9fcf71b7.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 13 of 37 in “meson: wire up bits and pieces from "contrib/"”
  1. 00/12 meson: wire up bits and pieces from "contrib/"Patrick Steinhardt, Feb 18, 2025
  2. 01/12 GIT-BUILD-OPTIONS: propagate project's source directoryPatrick Steinhardt, Feb 18, 2025
  3. 02/12 contrib/credential: fix "netrc" tests with out-of-tree buildsPatrick Steinhardt, Feb 18, 2025
  4. 03/12 contrib/credential: fix compilation of wincred helper with MSVCPatrick Steinhardt, Feb 18, 2025
  5. 05/12 contrib/credential: fix compilation of "osxkeychain" helperPatrick Steinhardt, Feb 18, 2025
  6. 04/12 contrib/credential: fix compiling "libsecret" helperPatrick Steinhardt, Feb 18, 2025
  7. 06/12 meson: wire up credential helpersPatrick Steinhardt, Feb 18, 2025
  8. M HickfordFeb 18, 2025
  9. Patrick SteinhardtFeb 18, 2025
  10. M HickfordMar 29, 2025
  11. Patrick SteinhardtMar 31, 2025
  12. 07/12 meson: wire up git-contacts(1)Patrick Steinhardt, Feb 18, 2025
  13. 08/12 meson: wire up static analysis via CoccinellePatrick Steinhardt, Feb 18, 2025
  14. 09/12 gitk: extract script to build GitkPatrick Steinhardt, Feb 18, 2025
  15. Junio C HamanoFeb 18, 2025
  16. Patrick SteinhardtFeb 19, 2025
  17. Johannes SixtFeb 19, 2025
  18. Patrick SteinhardtFeb 19, 2025
  19. Johannes SixtFeb 19, 2025
  20. 10/12 meson: wire up GitkPatrick Steinhardt, Feb 18, 2025
  21. 11/12 ci: fix propagating UTF-8 test locale in musl-based Meson jobPatrick Steinhardt, Feb 18, 2025
  22. 12/12 ci: exercise credential helpersPatrick Steinhardt, Feb 18, 2025
  23. M HickfordFeb 18, 2025
  24. 00/10 meson: wire up bits and pieces from "contrib/"Patrick Steinhardt, Feb 19, 2025
  25. 01/10 GIT-BUILD-OPTIONS: propagate project's source directoryPatrick Steinhardt, Feb 19, 2025
  26. 02/10 contrib/credential: fix "netrc" tests with out-of-tree buildsPatrick Steinhardt, Feb 19, 2025
  27. 03/10 contrib/credential: fix compilation of wincred helper with MSVCPatrick Steinhardt, Feb 19, 2025
  28. 05/10 contrib/credential: fix compilation of "osxkeychain" helperPatrick Steinhardt, Feb 19, 2025
  29. 04/10 contrib/credential: fix compiling "libsecret" helperPatrick Steinhardt, Feb 19, 2025
  30. 06/10 meson: wire up credential helpersPatrick Steinhardt, Feb 19, 2025
  31. 07/10 meson: wire up git-contacts(1)Patrick Steinhardt, Feb 19, 2025
  32. 08/10 meson: wire up static analysis via CoccinellePatrick Steinhardt, Feb 19, 2025
  33. 09/10 ci: fix propagating UTF-8 test locale in musl-based Meson jobPatrick Steinhardt, Feb 19, 2025
  34. 10/10 ci: exercise credential helpersPatrick Steinhardt, Feb 19, 2025
  35. Ramsay JonesFeb 20, 2025
  36. Patrick SteinhardtFeb 20, 2025
  37. Ramsay JonesFeb 21, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.