[cocci] bug: Matching a specific declarer actually matches any declarer

Tobias Deiminger <[email protected]>
Newsgroups fr.inria.cocci
Message-ID <[email protected]>
Hi,

to reproduce the issue, try mutex2.cocci from coccinellery [1] with the 
code below.

Expectation: Only spin_lock calls where the variable was declared with 
DEFINE_MUTEX() are patched.

Actual: The declarer name seems to be ignored. All spin_lock calls are 
patched, regardless of the declarer name

// mutex2.c
DEFINE_MUTEX(my_mutex);
OTHER_DECLARER(other_mutex);

void main() {
     // this should be changed, declarer was DEFINE_MUTEX
     spin_lock(&my_mutex);

     // this should not be changed, declarer was OTHER_DECLARER
     spin_lock(&other_mutex)
}

// mutex2.cocci
@def@
declarer DEFINE_MUTEX;
identifier m;
@@

DEFINE_MUTEX(m);

@@
identifier def.m;
@@

(
- spin_lock(&m)
+ mutex_lock(&m)
|
- spin_unlock(&m)
+ mutex_unlock(&m)
)

The unexpected result:

$ spatch --sp-file mutex2.cocci mutex2.c
init_defs_builtins: /usr/lib/coccinelle/standard.h
HANDLING: mutex2.c
SPECIAL NAMES: adding OTHER_DECLARER as a declarer
diff =
--- mutex2.c
+++ /tmp/cocci-output-71243-6eaa4e-my.c
@@ -3,8 +3,8 @@ OTHER_DECLARER(other_mutex);

  void main() {
      // this should be changed, declarer was DEFINE_MUTEX
-    spin_lock(&my_mutex);
+    mutex_lock(&my_mutex);

      // this should not be changed, declarer was OTHER_DECLARER
-    spin_lock(&other_mutex)
+    mutex_lock(&other_mutex)
  }


Could you confirm, and maybe suggest a workaround? I used the 
coccinellery example as reproducer because it's public, but actually 
stumbled upon the issue in a simpler case: I searched for all 
occurrences of DEVICE_ATTR_RO(attr) in the Linux kernel, but actually 
found every place where *any* macro was used to declare something.

Cheers
Tobias

PS: I'm using spatch 1.3 from Debian trixie.

[1] 
https://github.com/coccinelle/coccinellery/blob/master/cris/mutex2.cocci
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.