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 21, 2023
1 parent 03a3613 commit 03deabe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 17 additions & 1 deletion simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,18 @@ 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.");
}
};

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

void addExpander(const Token *tok) const
{
expandedToks.insert(tok);
}

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 {
Expand Down Expand Up @@ -2023,7 +2035,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 @@ -2205,6 +2217,10 @@ namespace simplecpp {
};
}

void simplecpp::Token::setExpandedFrom(const Token *tok, const Macro* m) {
m->addExpander(this);
}

namespace simplecpp {

std::string convertCygwinToWindowsPath(const std::string &cygwinPath)
Expand Down
8 changes: 1 addition & 7 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,7 @@ 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 setExpandedFrom(const Token *tok, const Macro* m);

void printAll() const;
void printOut() const;
Expand Down

0 comments on commit 03deabe

Please sign in to comment.