ned14 / pcpp

A C99 preprocessor written in pure Python
Other
215 stars 39 forks source link

Preprocessor to AND, OR options #66

Closed soumyaranjansabat closed 2 years ago

soumyaranjansabat commented 2 years ago

If a condition as such :

define CONDITION_1 ON

if ((CONDITION_1 == ON) || (CONDITION_2 == OFF))

#endif In the example, if CONDITION_1 macro is defined and is ON and CONDITION_2 macro is not defined, in such scenarios the preprocessor doesn't get resolved ie. it still prints as : #if ((CONDITION_1 == ON) || (CONDITION_2 == OFF)) #endif I expect, condition_2 shouldn't have been checked as per normal C. Is my expectation wrong ? Similarly in usage of AND
ned14 commented 2 years ago

Can you supply the command line args you fed to pcpp to yield this result please?

soumyaranjansabat commented 2 years ago

Was running it on default without any arguments . Just -o.

[Off-topic but I think might resolve the issue] Is there a way to not expand the macros / macro like functions in the generated file ?

ned14 commented 2 years ago

test.h:

#define CONDITION_1 ON

#if ((CONDITION_1 == ON) || (CONDITION_2 == OFF))
Foo
#endif

I run:

pcpp -o test1.i test.h

test1.i:

#line 4 "test.h"
Foo

I run:

gcc -E -o test2.i test.h

test2.i:

# 1 "test.h"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "test.h"

Foo

Looks to me pcpp is doing the right thing here?

soumyaranjansabat commented 2 years ago
  1. Could you please tell, use of which command line option would have disabled such output?
  2. Is there a way to disable the expansion of macros and only process the #if(s) like mentioned above?

ex :

#define MYVALUE 0x05
#define CONDITION_1 ON
if(MYVALUE > 0x01)
{
<code>
#if ((CONDITION_1 == ON) || (CONDITION_2 == OFF))
Foo
#endif
}

at present in the resultant output is,

if(0x05> 0x01)
{
<code>

Foo

}

Can the expansion of the macro be disabled like,

if(MYVALUE > 0x01)
{
<code>

Foo
}

Similarly for Macro like functions.

ned14 commented 2 years ago

To get exactly what you want, you'll need to customise the preprocessor hooks. See https://ned14.github.io/pcpp/preprocessor.html#pcpp.preprocessor.PreprocessorHooks.

If you want some macros to be expanded but not others, the Readme on the front page tells you how.