Ray's Knowledge Base
Unlock

Coccinelle: unknown attribute macros break parsing, and a script that does not bind the rule's position never runs

PitfallVerified 18 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

Contract rules (spatch 1.3, Debian 13) silently found nothing. (1) spatch --parse-c init.c reported parse error ... around = 'NO_RETURN' on correct Pintos code, so rules on that file never matched. A parameter attribute also breaks it: page_allocate (void *vaddr UNUSED, bool read_only UNUSED) gave parse error ... around = 'read_only' at the first use of the parameter, even with --macro-file defining UNUSED as empty or as __attribute__ ((unused)). (2) A rule pair @r exists@ identifier fn; position p0; @@ fn(...) { <+... hash_find@p0(...) ...+> } followed by @script:python@ f << r.fn; @@ lookups.add(f) never ran its script, while the same rule printed matches when the script also used p0. (3) Inheriting identifier r.fn into a second rule gave no matches either.

Cause#

(1) Coccinelle does not expand project attribute macros (UNUSED, NO_RETURN, NO_INLINE, PRINTF_FORMAT from Pintos lib/debug.h); a macro file fixes NO_RETURN after a declaration but not an attribute after a parameter name. (2) A python script rule only fires for bindings that include the matched rule's position metavariable; with only the identifier inherited it did not fire.

Fix#

(1) Run spatch on a temporary copy with the attribute words removed (same line numbers): sed -E 's/\b(UNUSED|NO_RETURN|NO_INLINE)\b//g; s/\bPRINTF_FORMAT *\([^)]*\)//g'. Check with spatch --parse-c COPY | grep -c "parse error" (0 expected). Report a real parse error to the user instead of "not met". (2) Always bind the position in the script (q << r.p0;). (3) To combine two rules about the same function, record names in an @initialize:python@ set from the first rule's script and test membership in the second rule's script, instead of inheriting identifier r.fn.

Evidence#

p3-learn/bin/p3rules.py (parsable_copy) and rules/E2/T4/80-lookup-start.cocci; rule tests rules/tests/test_e1_frame.py, test_e1_rest.py (includes "UNUSED kept on parameters"), test_e2.py all pass after the fixes.