Skip to content

Commit 1f70ca1

Browse files
committed
1.10.2
1 parent ae297dc commit 1f70ca1

1 file changed

Lines changed: 55 additions & 29 deletions

File tree

‎externals/simplecpp/simplecpp.cpp‎

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ class simplecpp::TokenList::Stream {
424424
return 0;
425425
}
426426

427-
unsigned short bom{0};
427+
// cppcheck-suppress uninitMemberVarPrivate - FP this class is a abstract base class and its members are initialized in init()
428+
unsigned short bom;
428429

429430
/** characters that are read but not used yet. The next character is at the back. */
430431
std::string buffer;
@@ -435,12 +436,14 @@ class simplecpp::TokenList::Stream {
435436
/** get() has returned EOF */
436437
bool inputEnd{false};
437438
protected:
438-
bool isUtf16{false};
439+
// cppcheck-suppress uninitMemberVarNoCtor - FP this class is a abstract base class and its members are initialized in init()
440+
bool isUtf16;
439441
};
440442

441443
namespace {
442444
class StdIStream final : public simplecpp::TokenList::Stream {
443445
public:
446+
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
444447
explicit StdIStream(std::istream &istr)
445448
: istr(istr) {
446449
assert(istr.good());
@@ -464,6 +467,7 @@ namespace {
464467

465468
class StdCharBufStream final : public simplecpp::TokenList::Stream {
466469
public:
470+
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
467471
StdCharBufStream(const unsigned char* str, std::size_t size)
468472
: str(str)
469473
, size(size)
@@ -497,6 +501,7 @@ namespace {
497501
/**
498502
* @throws simplecpp::Output thrown if file is not found
499503
*/
504+
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
500505
explicit FileStream(const std::string &filename, std::vector<std::string> &files)
501506
: file(fopen(filename.c_str(), "rb"))
502507
{
@@ -2393,6 +2398,12 @@ namespace simplecpp {
23932398
std::cout << " expand " << name() << " " << locstring(defineLocation()) << std::endl;
23942399
#endif
23952400

2401+
// No arguments => not macro expansion
2402+
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2403+
output.push_back(new Token(nameTokInst->str(), loc));
2404+
return nameTokInst->next;
2405+
}
2406+
23962407
usageList.emplace_back(loc);
23972408

23982409
if (expandBuiltin(output, loc, nameTokInst))
@@ -2401,13 +2412,7 @@ namespace simplecpp {
24012412
const bool calledInDefine = (loc.fileIndex != nameTokInst->location.fileIndex ||
24022413
loc.line < nameTokInst->location.line);
24032414

2404-
// No arguments => not macro expansion
2405-
if (functionLike() && nameTokInst->next && nameTokInst->next->op != '(') {
2406-
output.push_back(new Token(nameTokInst->str(), loc));
2407-
return nameTokInst->next;
2408-
}
2409-
2410-
if (functionLike() && nameTokInst->next && nameTokInst->next->op == '(' && isIntegerConstantMacro(name()))
2415+
if (functionLike() && nameTokInst->next && isIntegerConstantMacro(name()))
24112416
usageList.back().arg = writtenArgument(nameTokInst, calledInDefine);
24122417

24132418
std::vector<const Token*> parametertokens1(getMacroParameters(nameTokInst, calledInDefine));
@@ -4186,6 +4191,27 @@ static std::string getTimeDefine(const struct tm *timep)
41864191
return std::string("\"").append(buf).append("\"");
41874192
}
41884193

4194+
/** Report the usages of a macro. This is done when its definition goes away, so that #undef and redefinition do not lose them. */
4195+
static void addMacroUsage(const simplecpp::Macro &macro, std::map<std::string, std::list<simplecpp::Location>> &maybeUsedMacros, std::list<simplecpp::MacroUsage> *macroUsage)
4196+
{
4197+
if (!macroUsage)
4198+
return;
4199+
std::list<simplecpp::Macro::Usage> usage = macro.usage();
4200+
const std::map<std::string, std::list<simplecpp::Location>>::iterator maybeUsed = maybeUsedMacros.find(macro.name());
4201+
if (maybeUsed != maybeUsedMacros.end()) {
4202+
usage.insert(usage.end(), maybeUsed->second.begin(), maybeUsed->second.end());
4203+
maybeUsedMacros.erase(maybeUsed);
4204+
}
4205+
for (std::list<simplecpp::Macro::Usage>::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4206+
simplecpp::MacroUsage mu(macro.valueDefinedInCode());
4207+
mu.macroName = macro.name();
4208+
mu.macroLocation = macro.defineLocation();
4209+
mu.useLocation = usageIt->location;
4210+
mu.arg = std::move(usageIt->arg);
4211+
macroUsage->emplace_back(std::move(mu));
4212+
}
4213+
}
4214+
41894215
void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, simplecpp::FileDataCache &cache, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond)
41904216
{
41914217
#ifdef SIMPLECPP_WINDOWS
@@ -4329,6 +4355,8 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
43294355
}
43304356

43314357
std::map<std::string, std::list<Location>> maybeUsedMacros;
4358+
// usages of macros that are undefined or redefined, handed over only when preprocessing succeeds
4359+
std::list<MacroUsage> pendingUsage;
43324360

43334361
for (const Token *rawtok = nullptr; rawtok || !includetokenstack.empty();) {
43344362
if (rawtok == nullptr) {
@@ -4393,10 +4421,12 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
43934421
const Macro &macro = Macro(rawtok->previous, files);
43944422
if (dui.undefined.find(macro.name()) == dui.undefined.end()) {
43954423
const MacroMap::iterator it = macros.find(macro.name());
4396-
if (it == macros.end())
4424+
if (it == macros.end()) {
43974425
macros.insert(std::pair<TokenString, Macro>(macro.name(), macro));
4398-
else
4426+
} else {
4427+
addMacroUsage(it->second, maybeUsedMacros, macroUsage ? &pendingUsage : nullptr);
43994428
it->second = macro;
4429+
}
44004430
}
44014431
} catch (const std::runtime_error &err) {
44024432
if (outputList) {
@@ -4615,8 +4645,10 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
46154645
continue;
46164646
}
46174647

4618-
// usage of defined macros is recorded when they are expanded
4619-
if (macros.find(tok->str()) == macros.end())
4648+
// usage of defined macros is recorded when they are expanded,
4649+
// but a function-like macro without ( is not expanded
4650+
const MacroMap::const_iterator macroIt = macros.find(tok->str());
4651+
if (macroIt == macros.end() || (macroIt->second.functionLike() && tok->next && tok->next->op != '('))
46204652
maybeUsedMacros[tok->str()].emplace_back(tok->location);
46214653

46224654
const Token *tmp = tok;
@@ -4685,8 +4717,13 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
46854717
const Token *tok = rawtok->next;
46864718
while (sameline(rawtok,tok) && tok->comment)
46874719
tok = tok->next;
4688-
if (sameline(rawtok, tok))
4689-
macros.erase(tok->str());
4720+
if (sameline(rawtok, tok)) {
4721+
const MacroMap::iterator it = macros.find(tok->str());
4722+
if (it != macros.end()) {
4723+
addMacroUsage(it->second, maybeUsedMacros, macroUsage ? &pendingUsage : nullptr);
4724+
macros.erase(it);
4725+
}
4726+
}
46904727
}
46914728
} else if (ifstates.top() == True && rawtok->str() == PRAGMA && rawtok->next && rawtok->next->str() == ONCE && sameline(rawtok,rawtok->next)) {
46924729
pragmaOnce.insert(rawtokens.file(rawtok->location));
@@ -4739,20 +4776,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
47394776
}
47404777

47414778
if (macroUsage) {
4742-
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt) {
4743-
const Macro &macro = macroIt->second;
4744-
std::list<Macro::Usage> usage = macro.usage();
4745-
const std::list<Location>& temp = maybeUsedMacros[macro.name()];
4746-
usage.insert(usage.end(), temp.begin(), temp.end());
4747-
for (std::list<Macro::Usage>::iterator usageIt = usage.begin(); usageIt != usage.end(); ++usageIt) {
4748-
MacroUsage mu(macro.valueDefinedInCode());
4749-
mu.macroName = macro.name();
4750-
mu.macroLocation = macro.defineLocation();
4751-
mu.useLocation = usageIt->location;
4752-
mu.arg = std::move(usageIt->arg);
4753-
macroUsage->emplace_back(std::move(mu));
4754-
}
4755-
}
4779+
macroUsage->splice(macroUsage->end(), pendingUsage);
4780+
for (simplecpp::MacroMap::const_iterator macroIt = macros.begin(); macroIt != macros.end(); ++macroIt)
4781+
addMacroUsage(macroIt->second, maybeUsedMacros, macroUsage);
47564782
}
47574783
}
47584784

0 commit comments

Comments
 (0)