Preprocessor question
Hello,
I have a question about macro expansion: is it valid for an #include file to end during the arguments of a macro? That is, suppose I have two source files:
/* test.h */
TEST(f,
and
/* test.c */
#define TEST(name,val) int name() { return val; }
#include "test.h"
1)
Does test.c expand to
int f() { return 1; }
or is that undefined behavior? clang --std=c99 -E test.c gives the expansion, but gcc --std=c99 -E test.c gives the error:
In file included from test.c:2:0:
test.h:1:0: error: unterminated argument list invoking macro "TEST"
TEST(f,
My reading of the standard is that gcc is wrong; the only thing anywhere close to supporting it is 6.10.3 paragraph 11, which says that the presence of preprocesing tokens in the arguments of a macro is undefined behavior. I don't think it applies to this example, does it?
I'm asking because I'm writing a C preprocessor, and I just now thought of this. With the way I structured things it's really hard to make that example work, so I really want gcc to be right :) Should I just rewrite the way I'm handling #includes, or is gcc right?
8 comments · 84% upvoted · other discussions
I found this at page 29 in your linked document (emphasis by me):
And in phase 3:
So it looks like
gccbehavior is correct. Since it says "preprocessing token", i'd guess that normal C code wouldn't be affected.moefh [OP] 1 points · Aug 2017
Thanks! I should have paid more attention to the translation phases.
[deleted] 1 points · Aug 2017
[deleted]
Neui 1 points · Aug 2017
Page 182 (emphasis by me):
OP used
#include "test.h", and the spec talks about source file, sotest.his treated as a source file. Above the text in the spec for the<xyz>version talks about headers.Also, I've looked what a header exactly is, and I found this at the bottom of page 199:
So a header might be a source file, but it might also not (eg. implemented in the compiler itself I could imagine)