Skip to content

Commit df3b4c0

Browse files
dmcppcheckclaude
andcommitted
Add Macro::expandBuiltin() for __FILE__, __LINE__ and __COUNTER__
No behaviour change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 92b451a commit df3b4c0

1 file changed

Lines changed: 24 additions & 14 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,29 @@ namespace simplecpp {
22862286
return sameline(lpar,tok) ? tok : nullptr;
22872287
}
22882288

2289+
/**
2290+
* Expand the built-in macros __FILE__, __LINE__ and __COUNTER__.
2291+
* The usage must already be added to usageList, __COUNTER__ is the number of earlier usages.
2292+
* @param output destination tokenlist
2293+
* @param loc location of the usage
2294+
* @param nameTokInst the macro name
2295+
* @return true if the macro is built-in and its value is added to output
2296+
*/
2297+
bool expandBuiltin(TokenList &output, const Location &loc, const Token *nameTokInst) const {
2298+
std::string value;
2299+
if (nameTokInst->str() == "__FILE__")
2300+
value = '\"' + output.file(loc) + '\"';
2301+
else if (nameTokInst->str() == "__LINE__")
2302+
value = toString(loc.line);
2303+
else if (nameTokInst->str() == "__COUNTER__")
2304+
value = toString(usageList.size() - 1U);
2305+
else
2306+
return false;
2307+
output.push_back(new Token(value, loc));
2308+
output.back()->macro = nameTokInst->str();
2309+
return true;
2310+
}
2311+
22892312
const Token * expand(TokenList & output, const Location &loc, const Token * const nameTokInst, const MacroMap &macros, std::set<TokenString> expandedmacros, OutputList *outputList) const {
22902313
expandedmacros.insert(nameTokInst->str());
22912314

@@ -2295,21 +2318,8 @@ namespace simplecpp {
22952318

22962319
usageList.emplace_back(loc);
22972320

2298-
if (nameTokInst->str() == "__FILE__") {
2299-
output.push_back(new Token('\"'+output.file(loc)+'\"', loc));
2300-
output.back()->macro = "__FILE__";
2301-
return nameTokInst->next;
2302-
}
2303-
if (nameTokInst->str() == "__LINE__") {
2304-
output.push_back(new Token(toString(loc.line), loc));
2305-
output.back()->macro = "__LINE__";
2321+
if (expandBuiltin(output, loc, nameTokInst))
23062322
return nameTokInst->next;
2307-
}
2308-
if (nameTokInst->str() == "__COUNTER__") {
2309-
output.push_back(new Token(toString(usageList.size()-1U), loc));
2310-
output.back()->macro = "__COUNTER__";
2311-
return nameTokInst->next;
2312-
}
23132323

23142324
const bool calledInDefine = (loc.fileIndex != nameTokInst->location.fileIndex ||
23152325
loc.line < nameTokInst->location.line);

0 commit comments

Comments
 (0)