Skip to content

Commit 4bb6eba

Browse files
authored
Fix danmar#397 (Debracket macro not expanded) (danmar#398)
1 parent d4323f7 commit 4bb6eba

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

simplecpp.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,13 +2046,19 @@ namespace simplecpp {
20462046
calledMacro.expand(&temp, loc, tok, macros, expandedmacros);
20472047
return recursiveExpandToken(output, temp, loc, tok, macros, expandedmacros2, parametertokens);
20482048
}
2049-
if (!sameline(tok, tok->next) || tok->next->op != '(') {
2049+
if (!sameline(tok, tok->next)) {
20502050
output->push_back(newMacroToken(tok->str(), loc, true, tok));
20512051
return tok->next;
20522052
}
20532053
TokenList tokens(files);
20542054
tokens.push_back(new Token(*tok));
2055-
const Token * const tok2 = appendTokens(&tokens, loc, tok->next, macros, expandedmacros, parametertokens);
2055+
const Token * tok2 = nullptr;
2056+
if (tok->next->op == '(')
2057+
tok2 = appendTokens(&tokens, loc, tok->next, macros, expandedmacros, parametertokens);
2058+
else if (expandArg(&tokens, tok->next, loc, macros, expandedmacros, parametertokens)) {
2059+
if (tokens.cfront()->next && tokens.cfront()->next->op == '(')
2060+
tok2 = tok->next;
2061+
}
20562062
if (!tok2) {
20572063
output->push_back(newMacroToken(tok->str(), loc, true, tok));
20582064
return tok->next;

test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,20 @@ static void define_define_20() // #384 arg contains comma
821821
ASSERT_EQUALS("\n\n\n\n\n\na = 1", preprocess(code));
822822
}
823823

824+
static void define_define_21() // #397 DEBRACKET macro
825+
{
826+
const char code1[] = "#define A(val) B val\n"
827+
"#define B(val) val\n"
828+
"A((2))\n";
829+
ASSERT_EQUALS("\n\n2", preprocess(code1));
830+
831+
const char code2[] = "#define x (2)\n"
832+
"#define A B x\n"
833+
"#define B(val) val\n"
834+
"A\n";
835+
ASSERT_EQUALS("\n\n\nB ( 2 )", preprocess(code2));
836+
}
837+
824838
static void define_va_args_1()
825839
{
826840
const char code[] = "#define A(fmt...) dostuff(fmt)\n"
@@ -2999,6 +3013,7 @@ int main(int argc, char **argv)
29993013
TEST_CASE(define_define_18);
30003014
TEST_CASE(define_define_19);
30013015
TEST_CASE(define_define_20); // 384 arg contains comma
3016+
TEST_CASE(define_define_21);
30023017
TEST_CASE(define_va_args_1);
30033018
TEST_CASE(define_va_args_2);
30043019
TEST_CASE(define_va_args_3);

0 commit comments

Comments
 (0)