git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: M Hickford <mirth.hickford@gmail.com>,
	 Junio C Hamano <gitster@pobox.com>, Johannes Sixt <j6t@kdbg.org>
Subject: [PATCH v2 08/10] meson: wire up static analysis via Coccinelle
Date: Wed, 19 Feb 2025 14:13:48 +0100	[thread overview]
Message-ID: <20250219-b4-pks-meson-contrib-v2-8-1ba5d7fde0b9@pks.im> (raw)
In-Reply-To: <20250219-b4-pks-meson-contrib-v2-0-1ba5d7fde0b9@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


  parent reply	other threads:[~2025-02-19 13:14 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-18  7:45 [PATCH 00/12] meson: wire up bits and pieces from "contrib/" Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 01/12] GIT-BUILD-OPTIONS: propagate project's source directory Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 02/12] contrib/credential: fix "netrc" tests with out-of-tree builds Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 03/12] contrib/credential: fix compilation of wincred helper with MSVC Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 04/12] contrib/credential: fix compiling "libsecret" helper Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 05/12] contrib/credential: fix compilation of "osxkeychain" helper Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 06/12] meson: wire up credential helpers Patrick Steinhardt
2025-02-18 10:11   ` M Hickford
2025-02-18 11:13     ` Patrick Steinhardt
2025-03-29  7:15       ` M Hickford
2025-03-31  6:55         ` Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 07/12] meson: wire up git-contacts(1) Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 08/12] meson: wire up static analysis via Coccinelle Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 09/12] gitk: extract script to build Gitk Patrick Steinhardt
2025-02-18 22:25   ` Junio C Hamano
2025-02-19  5:53     ` Patrick Steinhardt
2025-02-19 11:42       ` Johannes Sixt
2025-02-19 11:51         ` Patrick Steinhardt
2025-02-19 17:32           ` Johannes Sixt
2025-02-18  7:45 ` [PATCH 10/12] meson: wire up Gitk Patrick Steinhardt
2025-02-18  7:45 ` [PATCH 11/12] ci: fix propagating UTF-8 test locale in musl-based Meson job Patrick Steinhardt
2025-02-18  7:46 ` [PATCH 12/12] ci: exercise credential helpers Patrick Steinhardt
2025-02-18 10:10 ` [PATCH 00/12] meson: wire up bits and pieces from "contrib/" M Hickford
2025-02-19 13:13 ` [PATCH v2 00/10] " Patrick Steinhardt
2025-02-19 13:13   ` [PATCH v2 01/10] GIT-BUILD-OPTIONS: propagate project's source directory Patrick Steinhardt
2025-02-19 13:13   ` [PATCH v2 02/10] contrib/credential: fix "netrc" tests with out-of-tree builds Patrick Steinhardt
2025-02-19 13:13   ` [PATCH v2 03/10] contrib/credential: fix compilation of wincred helper with MSVC Patrick Steinhardt
2025-02-19 13:13   ` [PATCH v2 04/10] contrib/credential: fix compiling "libsecret" helper Patrick Steinhardt
2025-02-19 13:13   ` [PATCH v2 05/10] contrib/credential: fix compilation of "osxkeychain" helper Patrick Steinhardt
2025-02-19 13:13   ` [PATCH v2 06/10] meson: wire up credential helpers Patrick Steinhardt
2025-02-19 13:13   ` [PATCH v2 07/10] meson: wire up git-contacts(1) Patrick Steinhardt
2025-02-19 13:13   ` Patrick Steinhardt [this message]
2025-02-19 13:13   ` [PATCH v2 09/10] ci: fix propagating UTF-8 test locale in musl-based Meson job Patrick Steinhardt
2025-02-19 13:13   ` [PATCH v2 10/10] ci: exercise credential helpers Patrick Steinhardt
2025-02-20  1:25   ` [-SPAM-] [PATCH v2 00/10] meson: wire up bits and pieces from "contrib/" Ramsay Jones
2025-02-20  6:25     ` Patrick Steinhardt
2025-02-21 19:50       ` Ramsay Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250219-b4-pks-meson-contrib-v2-8-1ba5d7fde0b9@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j6t@kdbg.org \
    --cc=mirth.hickford@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).