Skip to content

Commit 9d084ad

Browse files
committed
Expand __VA_ARGS__ when used as per the nonstandard gcc/clang extension for empty varargs, to yield gcc-like behavior also in cases where varargs isn't empty (#66)
1 parent 91f42e6 commit 9d084ad

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

simplecpp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,11 @@ namespace simplecpp {
18951895
if (sameline(tok, tok->next) && tok->next && tok->next->op == '#' && tok->next->next && tok->next->next->op == '#') {
18961896
if (!sameline(tok, tok->next->next->next))
18971897
throw invalidHashHash::unexpectedNewline(tok->location, name());
1898+
if (tok->op == ',' && tok->next->next->next->str() == "__VA_ARGS__") {
1899+
output->push_back(newMacroToken(tok->str(), loc, isReplaced(expandedmacros), tok));
1900+
tok = expandToken(output, loc, tok->next->next->next, macros, expandedmacros, parametertokens2);
1901+
continue;
1902+
}
18981903
TokenList new_output(files);
18991904
if (!expandArg(&new_output, tok, parametertokens2))
19001905
output->push_back(newMacroToken(tok->str(), loc, isReplaced(expandedmacros), tok));

test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,14 @@ static void hashhash4() // nonstandard gcc/clang extension for empty varargs
10741074
ASSERT_EQUALS("\n\na ( 1 ) ;", preprocess(code));
10751075
}
10761076

1077+
static void hashhash4a()
1078+
{
1079+
const char code[] = "#define GETMYID(a) ((a))+1\n"
1080+
"#define FIGHT_FOO(c, ...) foo(c, ##__VA_ARGS__)\n"
1081+
"FIGHT_FOO(1, GETMYID(a));";
1082+
ASSERT_EQUALS("\n\nfoo ( 1 , ( ( a ) ) + 1 ) ;", preprocess(code));
1083+
}
1084+
10771085
static void hashhash5()
10781086
{
10791087
ASSERT_EQUALS("x1", preprocess("x##__LINE__"));
@@ -2909,6 +2917,7 @@ int main(int argc, char **argv)
29092917
TEST_CASE(hashhash2);
29102918
TEST_CASE(hashhash3);
29112919
TEST_CASE(hashhash4);
2920+
TEST_CASE(hashhash4a); // #66
29122921
TEST_CASE(hashhash5);
29132922
TEST_CASE(hashhash6);
29142923
TEST_CASE(hashhash7); // # ## # (C standard; 6.10.3.3.p4)

0 commit comments

Comments
 (0)