Skip to content

Commit fdf4313

Browse files
dmcppcheckclaude
andcommitted
Rename mExpandedFrom to mHideSet and hold macro names instead of Macro*
The names are what Prosser's algorithm uses, and they can not dangle after #undef. This also removes the dependency on class Macro from Token and simplecpp.h. No change in behaviour. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 79c4663 commit fdf4313

2 files changed

Lines changed: 29 additions & 20 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,7 @@ namespace simplecpp {
20242024
if (output2.cfront() != output2.cback() && macro2tok->str() == this->name())
20252025
break;
20262026
const MacroMap::const_iterator macro = macros.find(macro2tok->str());
2027-
if (macro == macros.end() || !macro->second.functionLike() || macro2tok->isExpandedFrom(&macro->second))
2027+
if (macro == macros.end() || !macro->second.functionLike() || macro2tok->isInHideSet(macro->first))
20282028
break;
20292029
TokenList rawtokens2(inputFiles);
20302030
const Location loc(macro2tok->location);
@@ -2123,13 +2123,22 @@ namespace simplecpp {
21232123
}
21242124
};
21252125
private:
2126-
/** Create new token where Token::macro is set for replaced tokens */
2126+
/**
2127+
* Create a new token in the expansion of this macro.
2128+
* @param str the token text
2129+
* @param loc the location
2130+
* @param replaced Token::macro is set to the name of this macro
2131+
* @param expandedFromToken if not null, the new token copies its hide set and
2132+
* its whitespace, and this macro is added to the hide set
2133+
*/
21272134
Token *newMacroToken(const TokenString &str, const Location &loc, bool replaced, const Token *expandedFromToken=nullptr) const {
21282135
auto *tok = new Token(str,loc);
21292136
if (replaced)
21302137
tok->macro = nameTokDef->str();
2131-
if (expandedFromToken)
2132-
tok->setExpandedFrom(expandedFromToken, this);
2138+
if (expandedFromToken) {
2139+
tok->setHideSet(expandedFromToken, name());
2140+
tok->whitespaceahead = expandedFromToken->whitespaceahead;
2141+
}
21332142
return tok;
21342143
}
21352144

@@ -2519,7 +2528,7 @@ namespace simplecpp {
25192528
const MacroMap::const_iterator it = macros.find(temp.cback()->str());
25202529
if (it == macros.end() || expandedmacros.find(temp.cback()->str()) != expandedmacros.end())
25212530
return nullptr;
2522-
if (!it->second.functionLike() || temp.cback()->isExpandedFrom(&it->second))
2531+
if (!it->second.functionLike() || temp.cback()->isInHideSet(it->first))
25232532
return nullptr;
25242533
return &it->second;
25252534
}
@@ -2615,7 +2624,7 @@ namespace simplecpp {
26152624

26162625
output.push_back(newMacroToken(tok->str(), loc, true, tok));
26172626
if (it != macros.end())
2618-
output.back()->markExpandedFrom(&it->second);
2627+
output.back()->addToHideSet(it->first);
26192628
return tok->next;
26202629
}
26212630

@@ -2727,7 +2736,7 @@ namespace simplecpp {
27272736
const Token * const argEnd = parametertokens[argnr + 1U];
27282737
for (const Token *partok = parametertokens[argnr]->next; partok != argEnd;) {
27292738
const MacroMap::const_iterator it = macros.find(partok->str());
2730-
if (it != macros.end() && !partok->isExpandedFrom(&it->second) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end())) {
2739+
if (it != macros.end() && !partok->isInHideSet(it->first) && (partok->str() == name() || expandedmacros.find(partok->str()) == expandedmacros.end())) {
27312740
std::set<TokenString> expandedmacros2(expandedmacros); // temporary amnesia to allow reexpansion of currently expanding macros during argument evaluation
27322741
expandedmacros2.erase(name());
27332742
TokenList temp(files);

‎simplecpp.h‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ namespace simplecpp {
117117
};
118118
#endif // defined(__cpp_lib_string_view) && !defined(__cpp_lib_span)
119119

120-
class Macro;
121-
122120
/**
123121
* Location in source code
124122
*/
@@ -165,7 +163,7 @@ namespace simplecpp {
165163
}
166164

167165
Token(const Token &tok) :
168-
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), string(tok.string), mExpandedFrom(tok.mExpandedFrom) {}
166+
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), string(tok.string), mHideSet(tok.mHideSet) {}
169167

170168
Token &operator=(const Token &tok) = delete;
171169

@@ -210,17 +208,19 @@ namespace simplecpp {
210208
return tok;
211209
}
212210

213-
void setExpandedFrom(const Token *tok, const Macro* m) {
214-
mExpandedFrom = tok->mExpandedFrom;
215-
mExpandedFrom.insert(m);
216-
if (tok->whitespaceahead)
217-
whitespaceahead = true;
211+
/**
212+
* The hide set holds the macros that this token must not be expanded into.
213+
* Set it to the hide set of tok, with the macro macroName added.
214+
*/
215+
void setHideSet(const Token *tok, const TokenString &macroName) {
216+
mHideSet = tok->mHideSet;
217+
mHideSet.insert(macroName);
218218
}
219-
bool isExpandedFrom(const Macro* m) const {
220-
return mExpandedFrom.find(m) != mExpandedFrom.end();
219+
bool isInHideSet(const TokenString &macroName) const {
220+
return mHideSet.find(macroName) != mHideSet.end();
221221
}
222-
void markExpandedFrom(const Macro* m) {
223-
mExpandedFrom.insert(m);
222+
void addToHideSet(const TokenString &macroName) {
223+
mHideSet.insert(macroName);
224224
}
225225

226226
void printAll() const;
@@ -236,7 +236,7 @@ namespace simplecpp {
236236

237237
TokenString string;
238238

239-
std::set<const Macro*> mExpandedFrom;
239+
std::set<TokenString> mHideSet;
240240
};
241241

242242
/** Output from preprocessor */

0 commit comments

Comments
 (0)