Skip to content

Commit

Permalink
made TemplateSimplifier::mErrorLogger and Tokenizer::mErrorLogger
Browse files Browse the repository at this point in the history
… references
  • Loading branch information
firewave committed Apr 11, 2024
1 parent 628b894 commit ac40e86
Show file tree
Hide file tree
Showing 26 changed files with 80 additions and 86 deletions.
6 changes: 3 additions & 3 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ unsigned int CppCheck::checkClang(const std::string &path)
}

try {
Tokenizer tokenizer(mSettings, this);
Tokenizer tokenizer(mSettings, *this);
tokenizer.list.appendFileIfNew(path);
std::istringstream ast(output2);
clangimport::parseClangAstDump(tokenizer, ast);
Expand Down Expand Up @@ -657,7 +657,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
if (mSettings.library.markupFile(filename)) {
if (mUnusedFunctionsCheck && mSettings.useSingleJob() && mSettings.buildDir.empty()) {
// this is not a real source file - we just want to tokenize it. treat it as C anyways as the language needs to be determined.
Tokenizer tokenizer(mSettings, this);
Tokenizer tokenizer(mSettings, *this);
tokenizer.list.setLang(Standards::Language::C);
if (fileStream) {
tokenizer.list.createTokens(*fileStream, filename);
Expand Down Expand Up @@ -886,7 +886,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
continue;
}

Tokenizer tokenizer(mSettings, this);
Tokenizer tokenizer(mSettings, *this);
if (mSettings.showtime != SHOWTIME_MODES::SHOWTIME_NONE)
tokenizer.setTimerResults(&s_timerResults);
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies?
Expand Down
36 changes: 18 additions & 18 deletions lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,14 +1170,14 @@ void TemplateSimplifier::useDefaultArgumentValues(TokenAndName &declaration)
while (it != eq.cend()) {
// check for end
if (!it->end) {
if (mSettings.debugwarnings && mErrorLogger && mSettings.severity.isEnabled(Severity::debug)) {
if (mSettings.debugwarnings && mSettings.severity.isEnabled(Severity::debug)) {
const std::list<const Token*> locationList(1, it->eq);
const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::debug,
"noparamend",
"TemplateSimplifier couldn't find end of template parameter.",
Certainty::normal);
mErrorLogger->reportErr(errmsg);
mErrorLogger.reportErr(errmsg);
}
break;
}
Expand Down Expand Up @@ -3078,7 +3078,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
numberOfTemplateInstantiations = mTemplateInstantiations.size();
++recursiveCount;
if (recursiveCount > mSettings.maxTemplateRecursion) {
if (mErrorLogger && mSettings.severity.isEnabled(Severity::information)) {
if (mSettings.severity.isEnabled(Severity::information)) {
std::list<std::string> typeStringsUsedInTemplateInstantiation;
const std::string typeForNewName = templateDeclaration.name() + "<" + getNewName(instantiation.token(), typeStringsUsedInTemplateInstantiation) + ">";

Expand All @@ -3091,7 +3091,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
+ std::to_string(mSettings.maxTemplateRecursion)
+ ") reached for template '"+typeForNewName+"'. You might want to limit Cppcheck recursion.",
Certainty::normal);
mErrorLogger->reportErr(errmsg);
mErrorLogger.reportErr(errmsg);
}

// bail out..
Expand Down Expand Up @@ -3159,8 +3159,8 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
continue;

Token * const tok2 = instantiation.token();
if (mErrorLogger && !mTokenList.getFiles().empty())
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());
if (!mTokenList.getFiles().empty())
mErrorLogger.reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());

if (maxtime > 0 && std::time(nullptr) > maxtime) {
if (mSettings.debugwarnings) {
Expand All @@ -3171,7 +3171,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
"Template instantiation maximum time exceeded",
"templateMaxTime",
Certainty::normal);
mErrorLogger->reportErr(errmsg);
mErrorLogger.reportErr(errmsg);
}
return false;
}
Expand Down Expand Up @@ -3201,10 +3201,10 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(

if ((typeForNewName.empty() && !templateDeclaration.isVariadic()) ||
(!typeParametersInDeclaration.empty() && !instantiateMatch(tok2, typeParametersInDeclaration.size(), templateDeclaration.isVariadic(), nullptr))) {
if (printDebug && mErrorLogger) {
if (printDebug) {
std::list<const Token *> callstack(1, tok2);
mErrorLogger->reportErr(ErrorMessage(callstack, &mTokenList, Severity::debug, "templateInstantiation",
"Failed to instantiate template \"" + instantiation.name() + "\". The checking continues anyway.", Certainty::normal));
mErrorLogger.reportErr(ErrorMessage(callstack, &mTokenList, Severity::debug, "templateInstantiation",
"Failed to instantiate template \"" + instantiation.name() + "\". The checking continues anyway.", Certainty::normal));
}
if (typeForNewName.empty())
continue;
Expand All @@ -3229,8 +3229,8 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
// TODO: remove the specialized check and handle all uninstantiated templates someday.
if (!instantiated && specialized) {
auto * tok2 = const_cast<Token *>(templateDeclaration.nameToken());
if (mErrorLogger && !mTokenList.getFiles().empty())
mErrorLogger->reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());
if (!mTokenList.getFiles().empty())
mErrorLogger.reportProgress(mTokenList.getFiles()[0], "TemplateSimplifier::simplifyTemplateInstantiations()", tok2->progressValue());

if (maxtime > 0 && std::time(nullptr) > maxtime) {
if (mSettings.debugwarnings) {
Expand All @@ -3241,7 +3241,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
"Template instantiation maximum time exceeded",
"templateMaxTime",
Certainty::normal);
mErrorLogger->reportErr(errmsg);
mErrorLogger.reportErr(errmsg);
}
return false;
}
Expand Down Expand Up @@ -3278,10 +3278,10 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
std::string typeForNewName = getNewName(tok2, typeStringsUsedInTemplateInstantiation);

if (typeForNewName.empty()) {
if (printDebug && mErrorLogger) {
if (printDebug) {
std::list<const Token *> callstack(1, tok2);
mErrorLogger->reportErr(ErrorMessage(callstack, &mTokenList, Severity::debug, "templateInstantiation",
"Failed to instantiate template \"" + templateDeclaration.name() + "\". The checking continues anyway.", Certainty::normal));
mErrorLogger.reportErr(ErrorMessage(callstack, &mTokenList, Severity::debug, "templateInstantiation",
"Failed to instantiate template \"" + templateDeclaration.name() + "\". The checking continues anyway.", Certainty::normal));
}
return false;
}
Expand Down Expand Up @@ -3972,14 +3972,14 @@ void TemplateSimplifier::simplifyTemplates(const std::time_t maxtime)
}

if (passCount == passCountMax) {
if (mSettings.debugwarnings && mErrorLogger) {
if (mSettings.debugwarnings) {
const std::list<const Token*> locationList(1, mTokenList.front());
const ErrorMessage errmsg(locationList, &mTokenizer.list,
Severity::debug,
"debug",
"TemplateSimplifier: pass count limit hit before simplifications were finished.",
Certainty::normal);
mErrorLogger->reportErr(errmsg);
mErrorLogger.reportErr(errmsg);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/templatesimplifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ class CPPCHECKLIB TemplateSimplifier {
Tokenizer &mTokenizer;
TokenList &mTokenList;
const Settings &mSettings;
ErrorLogger *mErrorLogger;
ErrorLogger &mErrorLogger;
bool mChanged{};

std::list<TokenAndName> mTemplateDeclarations;
Expand Down
44 changes: 19 additions & 25 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static bool isClassStructUnionEnumStart(const Token * tok)

//---------------------------------------------------------------------------

Tokenizer::Tokenizer(const Settings &settings, ErrorLogger *errorLogger) :
Tokenizer::Tokenizer(const Settings &settings, ErrorLogger &errorLogger) :
list(&settings),
mSettings(settings),
mErrorLogger(errorLogger),
Expand Down Expand Up @@ -1141,22 +1141,22 @@ void Tokenizer::simplifyTypedefCpp()
const std::time_t maxTime = mSettings.typedefMaxTime > 0 ? std::time(nullptr) + mSettings.typedefMaxTime: 0;

for (Token *tok = list.front(); tok; tok = tok->next()) {
if (mErrorLogger && !list.getFiles().empty())
mErrorLogger->reportProgress(list.getFiles()[0], "Tokenize (typedef)", tok->progressValue());
if (!list.getFiles().empty())
mErrorLogger.reportProgress(list.getFiles()[0], "Tokenize (typedef)", tok->progressValue());

if (Settings::terminated())
return;

if (maxTime > 0 && std::time(nullptr) > maxTime) {
if (mErrorLogger && mSettings.debugwarnings) {
if (mSettings.debugwarnings) {
ErrorMessage::FileLocation loc(list.getFiles()[0], 0, 0);
ErrorMessage errmsg({std::move(loc)},
emptyString,
Severity::debug,
"Typedef simplification instantiation maximum time exceeded",
"typedefMaxTime",
Certainty::normal);
mErrorLogger->reportErr(errmsg);
mErrorLogger.reportErr(errmsg);
}
return;
}
Expand Down Expand Up @@ -2876,8 +2876,8 @@ bool Tokenizer::simplifyUsing()
std::list<Using> usingList;

for (Token *tok = list.front(); tok; tok = tok->next()) {
if (mErrorLogger && !list.getFiles().empty())
mErrorLogger->reportProgress(list.getFiles()[0], "Tokenize (using)", tok->progressValue());
if (!list.getFiles().empty())
mErrorLogger.reportProgress(list.getFiles()[0], "Tokenize (using)", tok->progressValue());

if (Settings::terminated())
return substitute;
Expand Down Expand Up @@ -3342,7 +3342,7 @@ bool Tokenizer::simplifyUsing()

void Tokenizer::simplifyUsingError(const Token* usingStart, const Token* usingEnd)
{
if (mSettings.debugwarnings && mErrorLogger) {
if (mSettings.debugwarnings) {
std::string str;
for (const Token *tok = usingStart; tok && tok != usingEnd; tok = tok->next()) {
if (!str.empty())
Expand All @@ -3351,8 +3351,8 @@ void Tokenizer::simplifyUsingError(const Token* usingStart, const Token* usingEn
}
str += " ;";
std::list<const Token *> callstack(1, usingStart);
mErrorLogger->reportErr(ErrorMessage(callstack, &list, Severity::debug, "simplifyUsing",
"Failed to parse \'" + str + "\'. The checking continues anyway.", Certainty::normal));
mErrorLogger.reportErr(ErrorMessage(callstack, &list, Severity::debug, "simplifyUsing",
"Failed to parse \'" + str + "\'. The checking continues anyway.", Certainty::normal));
}
}

Expand Down Expand Up @@ -3405,12 +3405,11 @@ bool Tokenizer::simplifyTokens1(const std::string &configuration)
const bool doValueFlow = !disableValueflowEnv || (std::strcmp(disableValueflowEnv, "1") != 0);

if (doValueFlow) {
assert(mErrorLogger);
if (mTimerResults) {
Timer t("Tokenizer::simplifyTokens1::ValueFlow", mSettings.showtime, mTimerResults);
ValueFlow::setValues(list, *mSymbolDatabase, *mErrorLogger, mSettings, mTimerResults);
ValueFlow::setValues(list, *mSymbolDatabase, mErrorLogger, mSettings, mTimerResults);
} else {
ValueFlow::setValues(list, *mSymbolDatabase, *mErrorLogger, mSettings, mTimerResults);
ValueFlow::setValues(list, *mSymbolDatabase, mErrorLogger, mSettings, mTimerResults);
}

arraySizeAfterValueFlow();
Expand Down Expand Up @@ -9987,10 +9986,8 @@ void Tokenizer::simplifyBorland()

void Tokenizer::createSymbolDatabase()
{
if (!mSymbolDatabase) {
assert(mErrorLogger != nullptr);
mSymbolDatabase = new SymbolDatabase(*this, mSettings, *mErrorLogger);
}
if (!mSymbolDatabase)
mSymbolDatabase = new SymbolDatabase(*this, mSettings, mErrorLogger);
mSymbolDatabase->validate();
}

Expand Down Expand Up @@ -10430,10 +10427,7 @@ void Tokenizer::reportError(const Token* tok, const Severity severity, const std
void Tokenizer::reportError(const std::list<const Token*>& callstack, Severity severity, const std::string& id, const std::string& msg, bool inconclusive) const
{
const ErrorMessage errmsg(callstack, &list, severity, id, msg, inconclusive ? Certainty::inconclusive : Certainty::normal);
if (mErrorLogger)
mErrorLogger->reportErr(errmsg);
else
Check::writeToErrorList(errmsg);
mErrorLogger.reportErr(errmsg);
}

void Tokenizer::setPodTypes()
Expand Down Expand Up @@ -10683,10 +10677,10 @@ bool Tokenizer::hasIfdef(const Token *start, const Token *end) const
const auto& directives = mDirectives;
return std::any_of(directives.cbegin(), directives.cend(), [&](const Directive& d) {
return startsWith(d.str, "#if") &&
d.linenr >= start->linenr() &&
d.linenr <= end->linenr() &&
start->fileIndex() < list.getFiles().size() &&
d.file == list.getFiles()[start->fileIndex()];
d.linenr >= start->linenr() &&
d.linenr <= end->linenr() &&
start->fileIndex() < list.getFiles().size() &&
d.file == list.getFiles()[start->fileIndex()];
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CPPCHECKLIB Tokenizer {
friend class TestTokenizer;

public:
explicit Tokenizer(const Settings & settings, ErrorLogger *errorLogger);
explicit Tokenizer(const Settings & settings, ErrorLogger &errorLogger);
~Tokenizer();

void setTimerResults(TimerResults *tr) {
Expand Down Expand Up @@ -645,7 +645,7 @@ class CPPCHECKLIB Tokenizer {
const Settings & mSettings;

/** errorlogger */
ErrorLogger* const mErrorLogger;
ErrorLogger& mErrorLogger;

/** Symbol database that all checks etc can use */
SymbolDatabase* mSymbolDatabase{};
Expand Down
4 changes: 2 additions & 2 deletions test/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ namespace simplecpp {
class SimpleTokenizer : public Tokenizer {
public:
SimpleTokenizer(ErrorLogger& errorlogger, const char code[], bool cpp = true)
: Tokenizer{s_settings, &errorlogger}
: Tokenizer{s_settings, errorlogger}
{
if (!tokenize(code, cpp))
throw std::runtime_error("creating tokens failed");
}

SimpleTokenizer(const Settings& settings, ErrorLogger& errorlogger)
: Tokenizer{settings, &errorlogger}
: Tokenizer{settings, errorlogger}
{}

/*
Expand Down
2 changes: 1 addition & 1 deletion test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TestBufferOverrun : public TestFixture {
.c(Standards::CLatest).cpp(Standards::CPPLatest).certainty(Certainty::inconclusive).build();

std::vector<std::string> files(1, filename);
Tokenizer tokenizer(settings, this);
Tokenizer tokenizer(settings, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

// Tokenizer..
Expand Down
4 changes: 2 additions & 2 deletions test/testclangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class TestClangImport : public TestFixture {

std::string parse(const char clang[]) {
const Settings settings = settingsBuilder().clang().build();
Tokenizer tokenizer(settings, this);
Tokenizer tokenizer(settings, *this);
std::istringstream istr(clang);
clangimport::parseClangAstDump(tokenizer, istr);
if (!tokenizer.tokens()) {
Expand Down Expand Up @@ -1054,7 +1054,7 @@ class TestClangImport : public TestFixture {

#define GET_SYMBOL_DB(AST) \
const Settings settings = settingsBuilder().clang().platform(Platform::Type::Unix64).build(); \
Tokenizer tokenizer(settings, this); \
Tokenizer tokenizer(settings, *this); \
{ \
std::istringstream istr(AST); \
clangimport::parseClangAstDump(tokenizer, istr); \
Expand Down
6 changes: 3 additions & 3 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8504,7 +8504,7 @@ class TestClass : public TestFixture {
const Settings settings = settingsBuilder().severity(Severity::style).build();

std::vector<std::string> files(1, "test.cpp");
Tokenizer tokenizer(settings, this);
Tokenizer tokenizer(settings, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
Expand Down Expand Up @@ -8847,7 +8847,7 @@ class TestClass : public TestFixture {
// getFileInfo
std::list<Check::FileInfo*> fileInfo;
for (const std::string& c: code) {
Tokenizer tokenizer(settingsDefault, this);
Tokenizer tokenizer(settingsDefault, *this);
std::istringstream istr(c);
const std::string filename = std::to_string(fileInfo.size()) + ".cpp";
ASSERT(tokenizer.list.createTokens(istr, filename));
Expand Down Expand Up @@ -8911,7 +8911,7 @@ class TestClass : public TestFixture {
const Settings settings = settingsBuilder().severity(Severity::performance).library("std.cfg").build();

std::vector<std::string> files(1, "test.cpp");
Tokenizer tokenizer(settings, this);
Tokenizer tokenizer(settings, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
Expand Down
2 changes: 1 addition & 1 deletion test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class TestCondition : public TestFixture {
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_(const char* file, int line, const char code[], const Settings &settings, const char* filename = "test.cpp") {
std::vector<std::string> files(1, filename);
Tokenizer tokenizer(settings, this);
Tokenizer tokenizer(settings, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

// Tokenizer..
Expand Down
2 changes: 1 addition & 1 deletion test/testincompletestatement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TestIncompleteStatement : public TestFixture {
const Settings settings1 = settingsBuilder(settings).certainty(Certainty::inconclusive, inconclusive).build();

std::vector<std::string> files(1, "test.cpp");
Tokenizer tokenizer(settings1, this);
Tokenizer tokenizer(settings1, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

// Tokenize..
Expand Down
2 changes: 1 addition & 1 deletion test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3071,7 +3071,7 @@ class TestLeakAutoVarRecursiveCountLimit : public TestFixture {
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
void checkP_(const char* file, int line, const char code[], bool cpp = false) {
std::vector<std::string> files(1, cpp?"test.cpp":"test.c");
Tokenizer tokenizer(settings, this);
Tokenizer tokenizer(settings, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

// Tokenizer..
Expand Down
Loading

0 comments on commit ac40e86

Please sign in to comment.