Coccinelle archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: cocci@inria.fr
Subject: [cocci] making a coccinelle rule apply only for a specific file
Date: Thu, 11 Apr 2024 18:33:47 +0100	[thread overview]
Message-ID: <CAFEAcA8vr4tPAWy=cHKCfAafjZXfccb=ppquuKwZV5xhnwPQBg@mail.gmail.com> (raw)

Hi; I'm trying to make a coccinelle rule that only applies when
a particular file is being processed. Looking at the documentation,
this seems like the 'depends on file in "..."' syntax ought to work
for this, but I'm getting weird results where the behaviour depends
on how I invoke spatch. Here's a test example:

$ cat foo/foo.c
#include <stdio.h>

int main(void) {
    printf("hello world\n");
    return 0;
}
$ cat test.cocci
@ test_rule depends on file in "foo/foo.c" @
@@
- printf("hello world\n");
+ printf("goodbye world\n");
$ spatch --dir foo --sp-file test.cocci
init_defs_builtins: /usr/lib/coccinelle/standard.h
HANDLING: foo/foo.c
$ spatch foo/foo.c --sp-file test.cocci
init_defs_builtins: /usr/lib/coccinelle/standard.h
HANDLING: foo/foo.c
diff =
--- foo/foo.c
+++ /tmp/cocci-output-756201-03be74-foo.c
@@ -1,6 +1,6 @@
 #include <stdio.h>

 int main(void) {
-    printf("hello world\n");
+    printf("goodbye world\n");
     return 0;
 }

As you can see, if I tell spatch which files to operate on by
passing '--dir foo', it says it's HANDLING foo/foo.c but doesn't
actually apply the rule. If I tell spatch to operate on foo/foo.c
specifically then it *does* apply the rule.

How do I write a "depends on file in" rule that works
regardless of how I specify the files for spatch to work on?

Side note: is there a way to get multiple "--dir" arguments
to do the right thing? I'd like to tell spatch "process
files in directories foo and bar", but the obvious
"--dir foo --dir bar" doesn't work: it tells me it is ignoring
all but the last specified directory. At the moment I work
around this with "for dir in foo bar; do spatch ... --dir $dir"
but that's a bit awkward, and it would be nice to be able to
pass multiple directories on one command line.

thanks
-- PMM

                 reply	other threads:[~2024-04-11 17:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='CAFEAcA8vr4tPAWy=cHKCfAafjZXfccb=ppquuKwZV5xhnwPQBg@mail.gmail.com' \
    --to=peter.maydell@linaro.org \
    --cc=cocci@inria.fr \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).