Skip to content

Commit

Permalink
Expand varargs when referenced as per the nonstandard gcc/clang exten…
Browse files Browse the repository at this point in the history
…sion for empty varargs, to yield gcc-like behavior also in cases where varargs isn't empty (#66, #130)
  • Loading branch information
datadiode committed Aug 29, 2024
1 parent 91f42e6 commit f773611
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,14 @@ 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 (variadic && tok->op == ',' && tok->next->next->next->str() == args.back()) {
Token *const comma = newMacroToken(tok->str(), loc, isReplaced(expandedmacros), tok);
output->push_back(comma);
tok = expandToken(output, loc, tok->next->next->next, macros, expandedmacros, parametertokens2);
if (output->back() == comma)
output->deleteToken(comma);
continue;
}
TokenList new_output(files);
if (!expandArg(&new_output, tok, parametertokens2))
output->push_back(newMacroToken(tok->str(), loc, isReplaced(expandedmacros), tok));
Expand Down
11 changes: 11 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,16 @@ 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"
"#define FIGHT_BAR(c, args...) bar(c, ##args)\n"
"FIGHT_FOO(1, GETMYID(a));\n"
"FIGHT_BAR(1, GETMYID(b));";
ASSERT_EQUALS("\n\n\nfoo ( 1 , ( ( a ) ) + 1 ) ;\nbar ( 1 , ( ( b ) ) + 1 ) ;", preprocess(code));
}

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

0 comments on commit f773611

Please sign in to comment.