Skip to contentSkip to conversation

Can Coccinelle parse files where macros are used as binary operations?

cocci Alice Lee 3 participants 5 messages Mar 13 – Mar 17

lore
Share & tools
lore

Alice Lee

author
#0 lore raw

* Hello,

I am trying to use Coccinelle to search C codebases (I split up my questions to make the threads more clear) and I am struggling to understand the scope of the --macro-file argument.
*
* Can macros used with #if in the source code be defined in a file passed with --macro-file, and then the right sections of source code are ignored? I was unable to get a small example to work, so I assume that I can define the macro but then there is no preprocessing to drop code. If I'm not overlooking something, how would you recommend I parse a file including these lines and then uses LS/RS as binary operators<https://git.musl-libc.org/cgit/musl/tree/src/string/memcpy.c#n50>:
*

*
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LS >>
#define RS <<
#else
#define LS <<
#define RS >>
#endif

I'm especially confused by this example because Coccinelle also cannot parse a version in which I remove the code above and put one of the pairs of defines in the macro definition file.

Thank you,
Alice Lee

#1 lore raw
Quoted 21 lines · Alice Lee

Hello,

I am trying to use Coccinelle to search C codebases (I split up my questions to make the threads more clear) and I am struggling to understand the scope of the --macro-file argument.

Can macros used with #if in the source code be defined in a file passed with --macro-file, and then the right sections of source code are ignored? I was unable to get a small example to work, so I assume that I can define the macro but then there is no preprocessing to drop code. If I'm not overlooking something, how would you recommend I parse a file including these lines and then uses LS/RS as binary operators:
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LS >>
#define RS <<
#else
#define LS <<
#define RS >>
#endif

Quoted 3 lines

I'm especially confused by this example because Coccinelle also cannot parse a version in which I remove the code above and put one of the pairs of defines in the macro definition file.

I think you want the following command line options:

--defined <symbol> treat cpp symbol as defined in #ifdef
--undefined <symbol> treat cpp symbol as undefined in #ifdef

--macro-file is for providing dummy definitions for macros used in the code that you are trying to parse.

julia

Quoted 5 lines

Thank you,
Alice Lee

author
#2 lore raw

Hi Julia,

Is there an alternative to --defined that allows me to actually define a macro as in the "#define LS <<" example? If I modify the code to use a simpler switch than the equality check from the original as follows:

#ifdef SYMB
#define LS >>
#define RS <<
#else
#define LS <<
#define RS >>
#endif

I still get a parse error when I run "spatch --cocci-file rule_test.cocci --defined SYMB musl-1.2.5/src/string/memcpy.c --debug"'; is this actually best done by a preprocessor and out of scope for Coccinelle?

Thanks,
Alice

________________________________
From: Julia Lawall <[email protected]>
Sent: Friday, March 13, 2026 3:12:34 PM
To: Alice Lee
Cc: [email protected]
Subject: Re: [cocci] Can Coccinelle parse files where macros are used as binary operations?

[EXTERNAL EMAIL - USE CAUTION when clicking links or attachments]

Quoted 21 lines · Alice Lee

Hello,

I am trying to use Coccinelle to search C codebases (I split up my questions to make the threads more clear) and I am struggling to understand the scope of the --macro-file argument.

Can macros used with #if in the source code be defined in a file passed with --macro-file, and then the right sections of source code are ignored? I was unable to get a small example to work, so I assume that I can define the macro but then there is no preprocessing to drop code. If I'm not overlooking something, how would you recommend I parse a file including these lines and then uses LS/RS as binary operators:
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LS >>
#define RS <<
#else
#define LS <<
#define RS >>
#endif

Quoted 3 lines

I'm especially confused by this example because Coccinelle also cannot parse a version in which I remove the code above and put one of the pairs of defines in the macro definition file.

I think you want the following command line options:

--defined <symbol> treat cpp symbol as defined in #ifdef
--undefined <symbol> treat cpp symbol as undefined in #ifdef

--macro-file is for providing dummy definitions for macros used in the code that you are trying to parse.

julia

Quoted 5 lines

Thank you,
Alice Lee

#3 lore raw
Quoted 16 lines · Alice Lee

Hi Julia,

Is there an alternative to --defined that allows me to actually define a macro as in the "#define LS <<" example? If I modify the code to use a simpler switch than the equality check from the original as follows:

#ifdef SYMB
#define LS >>
#define RS <<
#else
#define LS <<
#define RS >>
#endif

Quoted 2 lines

I still get a parse error when I run "spatch --cocci-file rule_test.cocci --defined SYMB musl-1.2.5/src/string/memcpy.c --debug"'; is this actually best done by a preprocessor and out of scope for Coccinelle?

Coccinelle seeing the macro definition doesn't force it to use the macro definition. Try putting the #defines you want in a file and then putting --macro-file-builtins file.

Alternatively, are these definition actually in the file where you expect the expansion to happen? Normally, if there is a parse error, which a use of eg LS should trigger, it will start trying to expand macros. But I wonder if it only tries for the ones that look like functions? If these definitions are in a header file, you may want an argument like --all-includes or --recursive-includes. Or you can put --include file.h on the command line explicitly. Or if you don't want to type it all the time, you can put at the beginning of your .cocci file:

#spatch --include file.h

Or any other command line arguments you want.

julia

Quoted 59 lines

Thanks,
Alice

_______________________________________________________________________________________________________________________________________________________________________________________________________________
From: Julia Lawall <[email protected]>
Sent: Friday, March 13, 2026 3:12:34 PM
To: Alice Lee
Cc: [email protected]
Subject: Re: [cocci] Can Coccinelle parse files where macros are used as binary operations? [EXTERNAL EMAIL - USE CAUTION when clicking links or attachments]

Quoted 21 lines · Alice Lee

Hello,

I am trying to use Coccinelle to search C codebases (I split up my questions to make the threads more clear) and I am struggling to understand the scope of the --macro-file argument.

Can macros used with #if in the source code be defined in a file passed with --macro-file, and then the right sections of source code are ignored? I was unable to get a small example to work, so I assume that I can define the macro but then there is no preprocessing to drop code. If I'm not overlooking something, how would you recommend I parse a file including these lines and then uses LS/RS as binary operators:
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LS >>
#define RS <<
#else
#define LS <<
#define RS >>
#endif

Quoted 3 lines

I'm especially confused by this example because Coccinelle also cannot parse a version in which I remove the code above and put one of the pairs of defines in the macro definition file.

I think you want the following command line options:

  --defined                     <symbol> treat cpp symbol as defined in #ifdef --undefined                   <symbol> treat cpp symbol as undefined in #ifdef

--macro-file is for providing dummy definitions for macros used in the code that you are trying to parse.

julia

Quoted 5 lines

Thank you,
Alice Lee

#4 lore raw
In reply to Alice Lee
Quoted 1 line

I am trying to use Coccinelle to search C codebases (…)

Nice …

Quoted 1 line

and I am struggling to understand the scope of the --macro-file argument.

https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/233b5ceb1d5a701fb270ebdf0a9bd6be0a6ce53b/docs/manual/spatch_options.tex#L409-412

Quoted 1 line

Can macros used with #if in the source code be defined in a file passed with --macro-file, and then the right sections of source code are ignored?

I suggest to reconsider corresponding expectations in more detail.

1. Some developers would like to expect that the Coccinelle software

Quoted 1 line

# Can macros used with #if in the source code be defined in a file passed with --macro-file, and then the right sections of source code are ignored?

1. Some developers would like to expect that the Coccinelle software can handle source files also without extra preprocessing.

But it was discovered that implementation details are still waiting on corresponding clarifications.

Exclude macro calls from searches for function implementations 2024-10-28
https://github.com/coccinelle/coccinelle/issues/381

2. Software components can be improved accordingly.

Improve support for the handling of #if conditions 2022-10-29
https://github.com/coccinelle/coccinelle/issues/293

Regards,
Markus