Skip to content

Commit

Permalink
inverted the relationship between expanded macros and tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Aug 24, 2023
1 parent ce5f06b commit a2e3610
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
16 changes: 14 additions & 2 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1605,14 +1605,19 @@ 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 {
Token *tok = new Token(str,loc);
if (replaced)
tok->macro = nameTokDef->str();
if (expandedFromToken)
tok->setExpandedFrom(expandedFromToken, this);
const_cast<Macro*>(this)->addExpandedTok(tok);
return tok;
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;

Expand All @@ -2210,6 +2220,8 @@ namespace simplecpp {

/** was the value of this macro actually defined in the code? */
bool valueDefinedInCode_;

std::set<const Token*> expandedToks;
};
}

Expand Down
8 changes: 0 additions & 8 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a2e3610

Please sign in to comment.