mycoboco / beluga

a standard C compiler (with an integrated preprocessor)
http://code.woong.org/beluga
Other
65 stars 8 forks source link

incorrect handling of variadic macro calls with empty arguments #104

Closed mycoboco closed 6 years ago

mycoboco commented 6 years ago

beluga's preprocessor goes wrong when the first argument to the variadic part is empty, that is, the first token for __VA_ARGS__ is a comma.

For example,

#define foo(...) bar(__VA_ARGS__, end)
#define bar(...) __VA_ARGS__

foo()

gives

t.c:1:35: ERROR - too many arguments to macro `bar'
  #define foo(...) bar(__VA_ARGS__, end)
                                    ^~~
t.c:4:1: note - expanded from here
  foo()
  ^~~~~
t.c:2:9: note - see this definition
  #define bar(...) __VA_ARGS__
          ^~~
mycoboco commented 6 years ago

In recarg() from mcr.c, if the first token to the variadic part is a comma, the comma is not merged into a single item because the accumulated number of arguments, n in the code, is less than the number of parameters, which makes the comma separate arguments.

mycoboco commented 6 years ago

Giving a comma to the variadic part as the first token cannot:

But, handling for too many arguments to macro warning is still necessary.