ned14 / pcpp

A C99 preprocessor written in pure Python
Other
222 stars 41 forks source link

Do not evaluate unknown function-like macros to 0 #52

Closed schra closed 4 years ago

schra commented 4 years ago

In the following we will assume this file that contains a call to a undefined function-like macro:

$ cat /tmp/test.c
#if UNDEFINED_FUNCTION(1,2)
#endif

Before this patch:

$ pcpp /tmp/test.c
<string>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
/tmp/test.c:1 warning: couldn't evaluate expression due to Traceback (most recent call last):
  File "/storage/binds/home/andre/Git/pcpp/pcpp/preprocessor.py", line 1162, in evalexpr
    result = int(eval(expr, evalfuncts, evalvars))
  File "<string>", line 1, in <module>
TypeError: 'int' object is not callable

Converted expression was 0(1,2) with evalvars = {}

That is, currently all macros - including function-like macros, are evaluated to 0 if they are not defined. This is not correct.

With this patch:

$ pcpp /tmp/test.c
test.c:1 error: function-like macro 'UNDEFINED_FUNCTION' is not defined

GCC handles this case like this:

$ gcc /tmp/test.c
/tmp/test.c:1:23: error: missing binary operator before token "("
    1 | #if UNDEFINED_FUNCTION(1,2)
      |                       ^

Clang handles this like this:

$ clang /tmp/test.c
/tmp/test.c:1:5: error: function-like macro 'UNDEFINED_FUNCTION' is not defined
#if UNDEFINED_FUNCTION(1,2)
    ^
1 error generated.
schra commented 4 years ago

With the force-push I also now skip whitespaces between the macro name and the parenthesis. With this change, this PR also fixes #49

ned14 commented 4 years ago

Fixed using alternative means via the new expression evaluator. But thanks for the PR.