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

From: Julia Lawall

Date: Sun Aug 02 2026 - 15:30:24 EST


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 <rostedt@xxxxxxxxxxx>
Signed-off-by: Julia Lawall <Julia.Lawall@xxxxxxxx>

---
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 *)