Skip to content

Commit 96fc564

Browse files
author
Your Name
committed
Avoid a recursive expansion
1 parent 0fa51dc commit 96fc564

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

simplecpp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1635,7 +1635,7 @@ namespace simplecpp {
16351635
if (output2.cfront() != output2.cback() && macro2tok->str() == this->name())
16361636
break;
16371637
const MacroMap::const_iterator macro = macros.find(macro2tok->str());
1638-
if (macro == macros.end() || !macro->second.functionLike())
1638+
if (macro == macros.end() || !macro->second.functionLike() || macro2tok->isExpandedFrom(&macro->second))
16391639
break;
16401640
TokenList rawtokens2(inputFiles);
16411641
const Location loc(macro2tok->location);

test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,21 @@ static void define25()
934934
"f ( )", preprocess(code));
935935
}
936936

937+
static void define26()
938+
{
939+
// a macro name that came from expanding that same macro must not be
940+
// re-expanded with arguments taken from the raw token stream
941+
const char code[] = "#define f() f\n"
942+
"f()()\n";
943+
ASSERT_EQUALS("\n"
944+
"f ( )", preprocess(code));
945+
946+
const char code2[] = "#define f() f\n"
947+
"f()()()\n";
948+
ASSERT_EQUALS("\n"
949+
"f ( ) ( )", preprocess(code2));
950+
}
951+
937952

938953
static void define_invalid_1()
939954
{
@@ -4123,6 +4138,7 @@ static void runTests(int argc, char **argv, Input input)
41234138
TEST_CASE(define23); // #40
41244139
TEST_CASE(define24);
41254140
TEST_CASE(define25);
4141+
TEST_CASE(define26);
41264142
TEST_CASE(define_invalid_1);
41274143
TEST_CASE(define_invalid_2);
41284144
TEST_CASE(define_invalid_3);

0 commit comments

Comments
 (0)