Skip to content

Commit

Permalink
Expand __VA_ARGS__ when used as per the nonstandard gcc/clang extensi…
Browse files Browse the repository at this point in the history
…on for empty varargs, to yield gcc-like behavior also in cases where varargs isn't empty (#66)
  • Loading branch information
datadiode committed Aug 29, 2024
1 parent 91f42e6 commit 9d084ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,11 @@ namespace simplecpp {
if (sameline(tok, tok->next) && tok->next && tok->next->op == '#' && tok->next->next && tok->next->next->op == '#') {
if (!sameline(tok, tok->next->next->next))
throw invalidHashHash::unexpectedNewline(tok->location, name());
if (tok->op == ',' && tok->next->next->next->str() == "__VA_ARGS__") {
output->push_back(newMacroToken(tok->str(), loc, isReplaced(expandedmacros), tok));
tok = expandToken(output, loc, tok->next->next->next, macros, expandedmacros, parametertokens2);
continue;
}
TokenList new_output(files);
if (!expandArg(&new_output, tok, parametertokens2))
output->push_back(newMacroToken(tok->str(), loc, isReplaced(expandedmacros), tok));
Expand Down
9 changes: 9 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,14 @@ static void hashhash4() // nonstandard gcc/clang extension for empty varargs
ASSERT_EQUALS("\n\na ( 1 ) ;", preprocess(code));
}

static void hashhash4a()
{
const char code[] = "#define GETMYID(a) ((a))+1\n"
"#define FIGHT_FOO(c, ...) foo(c, ##__VA_ARGS__)\n"
"FIGHT_FOO(1, GETMYID(a));";
ASSERT_EQUALS("\n\nfoo ( 1 , ( ( a ) ) + 1 ) ;", preprocess(code));
}

static void hashhash5()
{
ASSERT_EQUALS("x1", preprocess("x##__LINE__"));
Expand Down Expand Up @@ -2909,6 +2917,7 @@ int main(int argc, char **argv)
TEST_CASE(hashhash2);
TEST_CASE(hashhash3);
TEST_CASE(hashhash4);
TEST_CASE(hashhash4a); // #66
TEST_CASE(hashhash5);
TEST_CASE(hashhash6);
TEST_CASE(hashhash7); // # ## # (C standard; 6.10.3.3.p4)
Expand Down

0 comments on commit 9d084ad

Please sign in to comment.