[cocci] [PATCH] Coccinelle: alloc_cast: coccinelle: api: check for macro context

Julia Lawall <[email protected]> Sun, 2 Aug 2026 21:29:57 +0200
Newsgroups fr.inria.cocci,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
A cast on a call to an allocation function that is the body of a macro can
be useful, as it ensures tha the macro is used for allocating objects of
the right type.  Add two new rules to ignore the macro case.

Suggested-by: Steven Rostedt <[email protected]>
Signed-off-by: Julia Lawall <[email protected]>

---
 scripts/coccinelle/api/alloc/alloc_cast.cocci |   23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/scripts/coccinelle/api/alloc/alloc_cast.cocci b/scripts/coccinelle/api/alloc/alloc_cast.cocci
index f6f0ccdb6409..958c914f8ba6 100644
--- a/scripts/coccinelle/api/alloc/alloc_cast.cocci
+++ b/scripts/coccinelle/api/alloc/alloc_cast.cocci
@@ -20,6 +20,24 @@ virtual patch
 virtual org
 virtual report
 
+@m1@
+identifier i;
+expression e;
+type T;
+position p1;
+@@
+
+#define i (T@p1 *)e
+
+@m2@
+identifier i;
+expression e;
+type T;
+position p2;
+@@
+
+#define i(...) (T@p2 *)e
+
 @initialize:python@
 @@
 import re
@@ -28,9 +46,10 @@ m = re.compile(pattern)
 
 @r1 depends on context || patch@
 type T;
+position p != {m1.p1,m2.p2};
 @@
 
-  (T *)
+  (T@p *)
   \(kmalloc\|kzalloc\|kcalloc\|kmem_cache_alloc\|kmem_cache_zalloc\|
    kmem_cache_alloc_node\|kmalloc_node\|kzalloc_node\|vmalloc\|vzalloc\|
    dma_alloc_coherent\|devm_kmalloc\|devm_kzalloc\|
@@ -90,7 +109,7 @@ type r1.T;
 
 @r2 depends on org || report@
 type T;
-position p;
+position p != {m1.p1,m2.p2};
 @@
 
  (T@p *)