Skip to content

Commit

Permalink
Fixed macro expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Apr 4, 2021
1 parent 8bfd548 commit f1086d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,10 @@ namespace simplecpp {
rawtokens2.push_back(new Token(rawtok->str(), rawtok1->location));
rawtok = rawtok->next;
}
if (expand(&output2, rawtok1->location, rawtokens2.cfront(), macros, expandedmacros))
bool first = true;
if (valueToken && valueToken->str() == rawtok1->str())
first = false;
if (expand(&output2, rawtok1->location, rawtokens2.cfront(), macros, expandedmacros, first))
rawtok = rawtok1->next;
} else {
rawtok = expand(&output2, rawtok->location, rawtok, macros, expandedmacros);
Expand Down Expand Up @@ -1517,8 +1520,10 @@ namespace simplecpp {
return sameline(lpar,tok) ? tok : NULL;
}

const Token * expand(TokenList * const output, const Location &loc, const Token * const nameTokInst, const std::map<TokenString,Macro> &macros, std::set<TokenString> expandedmacros) const {
expandedmacros.insert(nameTokInst->str());
const Token * expand(TokenList * const output, const Location &loc, const Token * const nameTokInst, const std::map<TokenString,Macro> &macros, std::set<TokenString> expandedmacros, bool first=false) const {

if (!first)
expandedmacros.insert(nameTokInst->str());

usageList.push_back(loc);

Expand Down
9 changes: 9 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,14 @@ static void define_define_16() // issue #72 with __VA_ARGS__
ASSERT_EQUALS("\n\n\n1 2", preprocess(code));
}

static void define_define_17()
{
const char code[] = "#define Bar(x) x\n"
"#define Foo Bar(1)\n"
"Bar( Foo ) ;";
ASSERT_EQUALS("\n\n1 ;", preprocess(code));
}

static void define_va_args_1()
{
const char code[] = "#define A(fmt...) dostuff(fmt)\n"
Expand Down Expand Up @@ -1999,6 +2007,7 @@ int main(int argc, char **argv)
TEST_CASE(define_define_14);
TEST_CASE(define_define_15);
TEST_CASE(define_define_16);
TEST_CASE(define_define_17);
TEST_CASE(define_va_args_1);
TEST_CASE(define_va_args_2);
TEST_CASE(define_va_args_3);
Expand Down

0 comments on commit f1086d2

Please sign in to comment.