Skip to content

Commit

Permalink
Fix #397 (Debracket macro not expanded)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Dec 12, 2024
1 parent d4323f7 commit c2b2aa9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
11 changes: 9 additions & 2 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2046,13 +2046,20 @@ namespace simplecpp {
calledMacro.expand(&temp, loc, tok, macros, expandedmacros);
return recursiveExpandToken(output, temp, loc, tok, macros, expandedmacros2, parametertokens);
}
if (!sameline(tok, tok->next) || tok->next->op != '(') {
if (!sameline(tok, tok->next)) {
output->push_back(newMacroToken(tok->str(), loc, true, tok));
return tok->next;
}
TokenList tokens(files);
tokens.push_back(new Token(*tok));
const Token * const tok2 = appendTokens(&tokens, loc, tok->next, macros, expandedmacros, parametertokens);
const Token * tok2 = nullptr;
if (tok->next->op == '(')
tok2 = appendTokens(&tokens, loc, tok->next, macros, expandedmacros, parametertokens);
else if (tok->next->name && tok->next->next && tok->next->next->op != '(') {
expandToken(&tokens, loc, tok->next, macros, expandedmacros2, parametertokens);
if (tokens.cfront()->next && tokens.cfront()->next->op == '(')
tok2 = tok->next;
}
if (!tok2) {
output->push_back(newMacroToken(tok->str(), loc, true, tok));
return tok->next;
Expand Down
9 changes: 9 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,14 @@ static void define_define_20() // #384 arg contains comma
ASSERT_EQUALS("\n\n\n\n\n\na = 1", preprocess(code));
}

static void define_define_21() // #397 DEBRACKET macro
{
const char code[] = "#define A(val) B val\n"
"#define B(val) val\n"
"A((2))\n";
ASSERT_EQUALS("\n\n2", preprocess(code));
}

static void define_va_args_1()
{
const char code[] = "#define A(fmt...) dostuff(fmt)\n"
Expand Down Expand Up @@ -2999,6 +3007,7 @@ int main(int argc, char **argv)
TEST_CASE(define_define_18);
TEST_CASE(define_define_19);
TEST_CASE(define_define_20); // 384 arg contains comma
TEST_CASE(define_define_21);
TEST_CASE(define_va_args_1);
TEST_CASE(define_va_args_2);
TEST_CASE(define_va_args_3);
Expand Down

0 comments on commit c2b2aa9

Please sign in to comment.