TemplateSimplifier: made `mTokenizer` and `mSettings` references (#4856)

This commit is contained in:
Oliver Stöneberg 2023-03-07 12:12:31 +01:00 committed by GitHub
parent d17e804235
commit a3cacf1ba1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 29 deletions

View File

@ -272,9 +272,9 @@ bool TemplateSimplifier::TokenAndName::isAliasToken(const Token *tok) const
return false; return false;
} }
TemplateSimplifier::TemplateSimplifier(Tokenizer *tokenizer) TemplateSimplifier::TemplateSimplifier(Tokenizer &tokenizer)
: mTokenizer(tokenizer), mTokenList(tokenizer->list), mSettings(tokenizer->mSettings), : mTokenizer(tokenizer), mTokenList(mTokenizer.list), mSettings(*mTokenizer.mSettings),
mErrorLogger(tokenizer->mErrorLogger), mChanged(false) mErrorLogger(mTokenizer.mErrorLogger), mChanged(false)
{} {}
TemplateSimplifier::~TemplateSimplifier() TemplateSimplifier::~TemplateSimplifier()
@ -1137,14 +1137,14 @@ void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration)
while (it != eq.cend()) { while (it != eq.cend()) {
// check for end // check for end
if (!it->end) { if (!it->end) {
if (mSettings->debugwarnings) { if (mSettings.debugwarnings) {
const std::list<const Token*> locationList(1, it->eq); const std::list<const Token*> locationList(1, it->eq);
const ErrorMessage errmsg(locationList, &mTokenizer->list, const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::debug, Severity::debug,
"noparamend", "noparamend",
"TemplateSimplifier couldn't find end of template parameter.", "TemplateSimplifier couldn't find end of template parameter.",
Certainty::normal); Certainty::normal);
if (mErrorLogger && mSettings->severity.isEnabled(Severity::debug)) if (mErrorLogger && mSettings.severity.isEnabled(Severity::debug))
mErrorLogger->reportErr(errmsg); mErrorLogger->reportErr(errmsg);
} }
break; break;
@ -1479,7 +1479,7 @@ int TemplateSimplifier::getTemplateNamePosition(const Token *tok)
assert(tok && tok->str() == ">"); assert(tok && tok->str() == ">");
auto it = mTemplateNamePos.find(tok); auto it = mTemplateNamePos.find(tok);
if (!mSettings->debugtemplate && it != mTemplateNamePos.end()) { if (!mSettings.debugtemplate && it != mTemplateNamePos.end()) {
return it->second; return it->second;
} }
// get the position of the template name // get the position of the template name
@ -2435,15 +2435,15 @@ void TemplateSimplifier::simplifyTemplateArgs(Token *start, Token *end)
} }
else if (Token::Match(tok->next(), "( %type% * )")) { else if (Token::Match(tok->next(), "( %type% * )")) {
tok->str(MathLib::toString(mTokenizer->sizeOfType(tok->tokAt(3)))); tok->str(MathLib::toString(mTokenizer.sizeOfType(tok->tokAt(3))));
tok->deleteNext(4); tok->deleteNext(4);
again = true; again = true;
} else if (Token::simpleMatch(tok->next(), "( * )")) { } else if (Token::simpleMatch(tok->next(), "( * )")) {
tok->str(MathLib::toString(mTokenizer->sizeOfType(tok->tokAt(2)))); tok->str(MathLib::toString(mTokenizer.sizeOfType(tok->tokAt(2))));
tok->deleteNext(3); tok->deleteNext(3);
again = true; again = true;
} else if (Token::Match(tok->next(), "( %type% )")) { } else if (Token::Match(tok->next(), "( %type% )")) {
const unsigned int size = mTokenizer->sizeOfType(tok->tokAt(2)); const unsigned int size = mTokenizer.sizeOfType(tok->tokAt(2));
if (size > 0) { if (size > 0) {
tok->str(MathLib::toString(size)); tok->str(MathLib::toString(size));
tok->deleteNext(3); tok->deleteNext(3);
@ -3006,7 +3006,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
// Contains tokens such as "T" // Contains tokens such as "T"
std::vector<const Token *> typeParametersInDeclaration; std::vector<const Token *> typeParametersInDeclaration;
getTemplateParametersInDeclaration(templateDeclaration.token()->tokAt(2), typeParametersInDeclaration); getTemplateParametersInDeclaration(templateDeclaration.token()->tokAt(2), typeParametersInDeclaration);
const bool printDebug = mSettings->debugwarnings; const bool printDebug = mSettings.debugwarnings;
const bool specialized = templateDeclaration.isSpecialization(); const bool specialized = templateDeclaration.isSpecialization();
const bool isfunc = templateDeclaration.isFunction(); const bool isfunc = templateDeclaration.isFunction();
const bool isVar = templateDeclaration.isVariable(); const bool isVar = templateDeclaration.isVariable();
@ -3024,20 +3024,20 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
if (numberOfTemplateInstantiations != mTemplateInstantiations.size()) { if (numberOfTemplateInstantiations != mTemplateInstantiations.size()) {
numberOfTemplateInstantiations = mTemplateInstantiations.size(); numberOfTemplateInstantiations = mTemplateInstantiations.size();
++recursiveCount; ++recursiveCount;
if (recursiveCount > mSettings->maxTemplateRecursion) { if (recursiveCount > mSettings.maxTemplateRecursion) {
std::list<std::string> typeStringsUsedInTemplateInstantiation; std::list<std::string> typeStringsUsedInTemplateInstantiation;
const std::string typeForNewName = templateDeclaration.name() + "<" + getNewName(instantiation.token(), typeStringsUsedInTemplateInstantiation) + ">"; const std::string typeForNewName = templateDeclaration.name() + "<" + getNewName(instantiation.token(), typeStringsUsedInTemplateInstantiation) + ">";
const std::list<const Token *> callstack(1, instantiation.token()); const std::list<const Token *> callstack(1, instantiation.token());
const ErrorMessage errmsg(callstack, const ErrorMessage errmsg(callstack,
&mTokenizer->list, &mTokenizer.list,
Severity::information, Severity::information,
"templateRecursion", "templateRecursion",
"TemplateSimplifier: max template recursion (" "TemplateSimplifier: max template recursion ("
+ MathLib::toString(mSettings->maxTemplateRecursion) + MathLib::toString(mSettings.maxTemplateRecursion)
+ ") reached for template '"+typeForNewName+"'. You might want to limit Cppcheck recursion.", + ") reached for template '"+typeForNewName+"'. You might want to limit Cppcheck recursion.",
Certainty::normal); Certainty::normal);
if (mErrorLogger && mSettings->severity.isEnabled(Severity::information)) if (mErrorLogger && mSettings.severity.isEnabled(Severity::information))
mErrorLogger->reportErr(errmsg); mErrorLogger->reportErr(errmsg);
// bail out.. // bail out..
@ -3109,7 +3109,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue()); mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());
if (maxtime > 0 && std::time(nullptr) > maxtime) { if (maxtime > 0 && std::time(nullptr) > maxtime) {
if (mSettings->debugwarnings) { if (mSettings.debugwarnings) {
ErrorMessage::FileLocation loc; ErrorMessage::FileLocation loc;
loc.setfile(mTokenList.getFiles()[0]); loc.setfile(mTokenList.getFiles()[0]);
ErrorMessage errmsg({std::move(loc)}, ErrorMessage errmsg({std::move(loc)},
@ -3180,7 +3180,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue()); mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());
if (maxtime > 0 && std::time(nullptr) > maxtime) { if (maxtime > 0 && std::time(nullptr) > maxtime) {
if (mSettings->debugwarnings) { if (mSettings.debugwarnings) {
ErrorMessage::FileLocation loc; ErrorMessage::FileLocation loc;
loc.setfile(mTokenList.getFiles()[0]); loc.setfile(mTokenList.getFiles()[0]);
ErrorMessage errmsg({std::move(loc)}, ErrorMessage errmsg({std::move(loc)},
@ -3699,7 +3699,7 @@ void TemplateSimplifier::simplifyTemplates(
} }
} }
if (mSettings->standards.cpp >= Standards::CPP20) { if (mSettings.standards.cpp >= Standards::CPP20) {
// Remove concepts/requires // Remove concepts/requires
// TODO concepts are not removed yet // TODO concepts are not removed yet
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) { for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
@ -3721,7 +3721,7 @@ void TemplateSimplifier::simplifyTemplates(
} }
} }
mTokenizer->calculateScopes(); mTokenizer.calculateScopes();
unsigned int passCount = 0; unsigned int passCount = 0;
const unsigned int passCountMax = 10; const unsigned int passCountMax = 10;
@ -3729,7 +3729,7 @@ void TemplateSimplifier::simplifyTemplates(
if (passCount) { if (passCount) {
// it may take more than one pass to simplify type aliases // it may take more than one pass to simplify type aliases
bool usingChanged = false; bool usingChanged = false;
while (mTokenizer->simplifyUsing()) while (mTokenizer.simplifyUsing())
usingChanged = true; usingChanged = true;
if (!usingChanged && !mChanged) if (!usingChanged && !mChanged)
@ -3756,7 +3756,7 @@ void TemplateSimplifier::simplifyTemplates(
if (mTemplateDeclarations.empty() && mTemplateForwardDeclarations.empty()) if (mTemplateDeclarations.empty() && mTemplateForwardDeclarations.empty())
return; return;
if (mSettings->debugtemplate && mSettings->debugnormal) { if (mSettings.debugtemplate && mSettings.debugnormal) {
std::string title("Template Simplifier pass " + std::to_string(passCount + 1)); std::string title("Template Simplifier pass " + std::to_string(passCount + 1));
mTokenList.front()->printOut(title.c_str(), mTokenList.getFiles()); mTokenList.front()->printOut(title.c_str(), mTokenList.getFiles());
} }
@ -3778,7 +3778,7 @@ void TemplateSimplifier::simplifyTemplates(
simplifyTemplateAliases(); simplifyTemplateAliases();
if (mSettings->debugtemplate) if (mSettings.debugtemplate)
printOut("### Template Simplifier pass " + std::to_string(passCount + 1) + " ###"); printOut("### Template Simplifier pass " + std::to_string(passCount + 1) + " ###");
std::set<std::string> expandedtemplates; std::set<std::string> expandedtemplates;
@ -3868,9 +3868,9 @@ void TemplateSimplifier::simplifyTemplates(
} }
if (passCount == passCountMax) { if (passCount == passCountMax) {
if (mSettings->debugwarnings) { if (mSettings.debugwarnings) {
const std::list<const Token*> locationList(1, mTokenList.front()); const std::list<const Token*> locationList(1, mTokenList.front());
const ErrorMessage errmsg(locationList, &mTokenizer->list, const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::debug, Severity::debug,
"debug", "debug",
"TemplateSimplifier: pass count limit hit before simplifications were finished.", "TemplateSimplifier: pass count limit hit before simplifications were finished.",
@ -3881,7 +3881,7 @@ void TemplateSimplifier::simplifyTemplates(
} }
// Tweak uninstantiated C++17 fold expressions (... && args) // Tweak uninstantiated C++17 fold expressions (... && args)
if (mSettings->standards.cpp >= Standards::CPP17) { if (mSettings.standards.cpp >= Standards::CPP17) {
bool simplify = false; bool simplify = false;
for (Token *tok = mTokenList.front(); tok; tok = tok->next()) { for (Token *tok = mTokenList.front(); tok; tok = tok->next()) {
if (tok->str() == "template") if (tok->str() == "template")

View File

@ -46,7 +46,7 @@ class CPPCHECKLIB TemplateSimplifier {
friend class TestSimplifyTemplate; friend class TestSimplifyTemplate;
public: public:
explicit TemplateSimplifier(Tokenizer *tokenizer); explicit TemplateSimplifier(Tokenizer &tokenizer);
~TemplateSimplifier(); ~TemplateSimplifier();
/** /**
@ -489,9 +489,9 @@ private:
const std::string &indent = " ") const; const std::string &indent = " ") const;
void printOut(const std::string &text = emptyString) const; void printOut(const std::string &text = emptyString) const;
Tokenizer *mTokenizer; Tokenizer &mTokenizer;
TokenList &mTokenList; TokenList &mTokenList;
const Settings *mSettings; const Settings &mSettings;
ErrorLogger *mErrorLogger; ErrorLogger *mErrorLogger;
bool mChanged; bool mChanged;

View File

@ -162,7 +162,7 @@ Tokenizer::Tokenizer(const Settings *settings, ErrorLogger *errorLogger, const P
mSettings(settings), mSettings(settings),
mErrorLogger(errorLogger), mErrorLogger(errorLogger),
mSymbolDatabase(nullptr), mSymbolDatabase(nullptr),
mTemplateSimplifier(new TemplateSimplifier(this)), mTemplateSimplifier(new TemplateSimplifier(*this)),
mVarId(0), mVarId(0),
mUnnamedCount(0), mUnnamedCount(0),
mCodeWithTemplates(false), //is there any templates? mCodeWithTemplates(false), //is there any templates?