diff --git a/simplecpp.cpp b/simplecpp.cpp index 48b8c6f1..72987c59 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -1605,6 +1605,11 @@ namespace simplecpp { return invalidHashHash(loc, macroName, "Combining '\\"+ tokenA->str()+ "' and '"+ strAB.substr(tokenA->str().size()) + "' yields universal character '\\" + strAB + "'. This is undefined behavior according to C standard chapter 5.1.1.2, paragraph 4."); } }; + + bool expandsTok(const Token *tok) const + { + return expandedToks.find(tok) != expandedToks.end(); + } private: /** Create new token where Token::macro is set for replaced tokens */ Token *newMacroToken(const TokenString &str, const Location &loc, bool replaced, const Token *expandedFromToken=nullptr) const { @@ -1612,7 +1617,7 @@ namespace simplecpp { if (replaced) tok->macro = nameTokDef->str(); if (expandedFromToken) - tok->setExpandedFrom(expandedFromToken, this); + const_cast(this)->addExpandedTok(tok); return tok; } @@ -2031,7 +2036,7 @@ namespace simplecpp { return true; for (const Token *partok = parametertokens[argnr]->next; partok != parametertokens[argnr + 1U];) { const MacroMap::const_iterator it = macros.find(partok->str()); - if (it != macros.end() && !partok->isExpandedFrom(&it->second) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end())) + if (it != macros.end() && !it->second.expandsTok(partok) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end())) partok = it->second.expand(output, loc, partok, macros, expandedmacros); else { output->push_back(newMacroToken(partok->str(), loc, isReplaced(expandedmacros), partok)); @@ -2184,6 +2189,11 @@ namespace simplecpp { return (it != expandedmacros.end()); } + void addExpandedTok(const Token *tok) + { + expandedToks.insert(tok); + } + /** name token in definition */ const Token *nameTokDef; @@ -2210,6 +2220,8 @@ namespace simplecpp { /** was the value of this macro actually defined in the code? */ bool valueDefinedInCode_; + + std::set expandedToks; }; } diff --git a/simplecpp.h b/simplecpp.h index b6124953..1184ecb3 100755 --- a/simplecpp.h +++ b/simplecpp.h @@ -159,14 +159,6 @@ namespace simplecpp { return tok; } - void setExpandedFrom(const Token *tok, const Macro* m) { - mExpandedFrom = tok->mExpandedFrom; - mExpandedFrom.insert(m); - } - bool isExpandedFrom(const Macro* m) const { - return mExpandedFrom.find(m) != mExpandedFrom.end(); - } - void printAll() const; void printOut() const; private: diff --git a/test.cpp b/test.cpp index 5c7142c0..de6f0dfb 100644 --- a/test.cpp +++ b/test.cpp @@ -768,6 +768,14 @@ static void define_define_18() ASSERT_EQUALS("\n\n\n( ( p -> var ) ) ;", preprocess(code)); } +static void define_define_19() +{ + const char code[] = "#define A0(a, b) ((a) + (b))\n" + "#define A1(a, b) ((a) > (b)) ? A0((a) - (b), (b)) : A0((b) - (a), (a))\n" + " A1(a, b);"; + ASSERT_EQUALS("\n\n( ( a ) > ( b ) ) ? ( ( ( a ) - ( b ) ) + ( ( b ) ) ) : ( ( ( b ) - ( a ) ) + ( ( a ) ) ) ;", preprocess(code)); +} + static void define_va_args_1() { const char code[] = "#define A(fmt...) dostuff(fmt)\n" @@ -2662,6 +2670,7 @@ int main(int argc, char **argv) TEST_CASE(define_define_16); TEST_CASE(define_define_17); TEST_CASE(define_define_18); + TEST_CASE(define_define_19); TEST_CASE(define_va_args_1); TEST_CASE(define_va_args_2); TEST_CASE(define_va_args_3);