diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 313b6d5d1ac..33599b53542 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -506,8 +506,7 @@ unsigned int CppCheck::checkClang(const std::string &path) } try { - Preprocessor preprocessor(mSettings, this); - Tokenizer tokenizer(mSettings, this, &preprocessor); + Tokenizer tokenizer(mSettings, this); tokenizer.list.appendFileIfNew(path); std::istringstream ast(output2); clangimport::parseClangAstDump(tokenizer, ast); @@ -887,7 +886,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string continue; } - Tokenizer tokenizer(mSettings, this, &preprocessor); + Tokenizer tokenizer(mSettings, this); if (mSettings.showtime != SHOWTIME_MODES::SHOWTIME_NONE) tokenizer.setTimerResults(&s_timerResults); tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies? diff --git a/lib/preprocessor.h b/lib/preprocessor.h index 45e9b873f4f..b8aad5e0916 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -68,7 +68,7 @@ struct CPPCHECKLIB Directive { * The preprocessor has special functionality for extracting the various ifdef * configurations that exist in a source file. */ -class CPPCHECKLIB Preprocessor { +class CPPCHECKLIB WARN_UNUSED Preprocessor { // TODO: get rid of this friend class PreprocessorHelper; friend class TestPreprocessor; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 46487727719..53a08a3a6f1 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -152,12 +152,11 @@ static bool isClassStructUnionEnumStart(const Token * tok) //--------------------------------------------------------------------------- -Tokenizer::Tokenizer(const Settings &settings, ErrorLogger *errorLogger, const Preprocessor *preprocessor) : +Tokenizer::Tokenizer(const Settings &settings, ErrorLogger *errorLogger) : list(&settings), mSettings(settings), mErrorLogger(errorLogger), - mTemplateSimplifier(new TemplateSimplifier(*this)), - mPreprocessor(preprocessor) + mTemplateSimplifier(new TemplateSimplifier(*this)) {} Tokenizer::~Tokenizer() @@ -10676,8 +10675,6 @@ void Tokenizer::setDirectives(std::list directives) bool Tokenizer::hasIfdef(const Token *start, const Token *end) const { - assert(mPreprocessor); - const auto& directives = mDirectives; return std::any_of(directives.cbegin(), directives.cend(), [&](const Directive& d) { return startsWith(d.str, "#if") && @@ -10690,8 +10687,6 @@ bool Tokenizer::hasIfdef(const Token *start, const Token *end) const bool Tokenizer::isPacked(const Token * bodyStart) const { - assert(mPreprocessor); - const auto& directives = mDirectives; // TODO: should this return true if the #pragma exists in any line before the start token? return std::any_of(directives.cbegin(), directives.cend(), [&](const Directive& d) { diff --git a/lib/tokenize.h b/lib/tokenize.h index 7a86b6c8d02..79051884961 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -36,7 +36,6 @@ class TimerResults; class Token; class TemplateSimplifier; class ErrorLogger; -class Preprocessor; struct Directive; enum class Severity; @@ -54,7 +53,7 @@ class CPPCHECKLIB Tokenizer { friend class TestTokenizer; public: - explicit Tokenizer(const Settings & settings, ErrorLogger *errorLogger, const Preprocessor *preprocessor = nullptr); + explicit Tokenizer(const Settings & settings, ErrorLogger *errorLogger); ~Tokenizer(); void setTimerResults(TimerResults *tr) { @@ -681,8 +680,6 @@ class CPPCHECKLIB Tokenizer { * TimerResults */ TimerResults* mTimerResults{}; - - const Preprocessor * const mPreprocessor; }; /// @} diff --git a/test/helpers.cpp b/test/helpers.cpp index e63d00c014e..e0b5fdbdf6d 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -45,7 +45,6 @@ class SuppressionList; const Settings SimpleTokenizer::s_settings; -const Preprocessor SimpleTokenizer::s_preprocessor{s_settings, nullptr}; // TODO: provide ErrorLogger // TODO: better path-only usage ScopedFile::ScopedFile(std::string name, const std::string &content, std::string path) diff --git a/test/helpers.h b/test/helpers.h index eb37021c1bb..130748f4fc0 100644 --- a/test/helpers.h +++ b/test/helpers.h @@ -42,18 +42,14 @@ namespace simplecpp { class SimpleTokenizer : public Tokenizer { public: SimpleTokenizer(ErrorLogger& errorlogger, const char code[], bool cpp = true) - : Tokenizer{s_settings, &errorlogger, &s_preprocessor} + : Tokenizer{s_settings, &errorlogger} { if (!tokenize(code, cpp)) throw std::runtime_error("creating tokens failed"); } SimpleTokenizer(const Settings& settings, ErrorLogger& errorlogger) - : Tokenizer{settings, &errorlogger, &s_preprocessor} - {} - - SimpleTokenizer(const Settings& settings, ErrorLogger& errorlogger, const Preprocessor* preprocessor) - : Tokenizer{settings, &errorlogger, preprocessor} + : Tokenizer{settings, &errorlogger} {} /* @@ -87,7 +83,6 @@ class SimpleTokenizer : public Tokenizer { private: // TODO. find a better solution static const Settings s_settings; - static const Preprocessor s_preprocessor; }; class SimpleTokenList diff --git a/test/testclass.cpp b/test/testclass.cpp index 989dfa516a0..a6478139074 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -21,7 +21,6 @@ #include "errortypes.h" #include "fixture.h" #include "helpers.h" -#include "preprocessor.h" #include "settings.h" #include "tokenize.h" @@ -248,10 +247,8 @@ class TestClass : public TestFixture { void checkCopyCtorAndEqOperator_(const char code[], const char* file, int line) { const Settings settings = settingsBuilder().severity(Severity::warning).build(); - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -350,10 +347,8 @@ class TestClass : public TestFixture { #define checkExplicitConstructors(code) checkExplicitConstructors_(code, __FILE__, __LINE__) void checkExplicitConstructors_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings0); - // Tokenize.. - SimpleTokenizer tokenizer(settings0, *this, &preprocessor); + SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -500,10 +495,8 @@ class TestClass : public TestFixture { #define checkDuplInheritedMembers(code) checkDuplInheritedMembers_(code, __FILE__, __LINE__) void checkDuplInheritedMembers_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings1); - // Tokenize.. - SimpleTokenizer tokenizer(settings1, *this, &preprocessor); + SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -718,10 +711,8 @@ class TestClass : public TestFixture { #define checkCopyConstructor(code) checkCopyConstructor_(code, __FILE__, __LINE__) void checkCopyConstructor_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings3); - // Tokenize.. - SimpleTokenizer tokenizer(settings3, *this, &preprocessor); + SimpleTokenizer tokenizer(settings3, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -1163,10 +1154,8 @@ class TestClass : public TestFixture { // Check that operator Equal returns reference to this #define checkOpertorEqRetRefThis(code) checkOpertorEqRetRefThis_(code, __FILE__, __LINE__) void checkOpertorEqRetRefThis_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings0); - // Tokenize.. - SimpleTokenizer tokenizer(settings0, *this, &preprocessor); + SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -1635,10 +1624,8 @@ class TestClass : public TestFixture { // Check that operator Equal checks for assignment to self #define checkOpertorEqToSelf(code) checkOpertorEqToSelf_(code, __FILE__, __LINE__) void checkOpertorEqToSelf_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings1); - // Tokenize.. - SimpleTokenizer tokenizer(settings1, *this, &preprocessor); + SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -2594,10 +2581,8 @@ class TestClass : public TestFixture { void checkVirtualDestructor_(const char* file, int line, const char code[], bool inconclusive = false) { const Settings s = settingsBuilder(settings0).certainty(Certainty::inconclusive, inconclusive).severity(Severity::warning).build(); - Preprocessor preprocessor(s); - // Tokenize.. - SimpleTokenizer tokenizer(s, *this, &preprocessor); + SimpleTokenizer tokenizer(s, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -2932,10 +2917,8 @@ class TestClass : public TestFixture { } void checkNoMemset_(const char* file, int line, const char code[], const Settings &settings) { - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -3579,10 +3562,8 @@ class TestClass : public TestFixture { #define checkThisSubtraction(code) checkThisSubtraction_(code, __FILE__, __LINE__) void checkThisSubtraction_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings1); - // Tokenize.. - SimpleTokenizer tokenizer(settings1, *this, &preprocessor); + SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -3612,10 +3593,8 @@ class TestClass : public TestFixture { void checkConst_(const char* file, int line, const char code[], const Settings *s = nullptr, bool inconclusive = true) { const Settings settings = settingsBuilder(s ? *s : settings0).certainty(Certainty::inconclusive, inconclusive).build(); - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckClass checkClass(&tokenizer, &settings, this); @@ -7532,11 +7511,8 @@ class TestClass : public TestFixture { #define checkInitializerListOrder(code) checkInitializerListOrder_(code, __FILE__, __LINE__) void checkInitializerListOrder_(const char code[], const char* file, int line) { - // Check.. - Preprocessor preprocessor(settings2); - // Tokenize.. - SimpleTokenizer tokenizer(settings2, *this, &preprocessor); + SimpleTokenizer tokenizer(settings2, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckClass checkClass(&tokenizer, &settings2, this); @@ -7663,10 +7639,8 @@ class TestClass : public TestFixture { // Check.. const Settings settings = settingsBuilder().severity(Severity::performance).build(); - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckClass checkClass(&tokenizer, &settings, this); @@ -7873,10 +7847,8 @@ class TestClass : public TestFixture { #define checkSelfInitialization(code) checkSelfInitialization_(code, __FILE__, __LINE__) void checkSelfInitialization_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings0); - // Tokenize.. - SimpleTokenizer tokenizer(settings0, *this, &preprocessor); + SimpleTokenizer tokenizer(settings0, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckClass checkClass(&tokenizer, &settings0, this); @@ -7985,10 +7957,8 @@ class TestClass : public TestFixture { // Check.. const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::style).certainty(Certainty::inconclusive, inconclusive).build(); - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckClass checkClass(&tokenizer, &settings, this); @@ -8330,10 +8300,8 @@ class TestClass : public TestFixture { void checkOverride_(const char code[], const char* file, int line) { const Settings settings = settingsBuilder().severity(Severity::style).build(); - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -8704,10 +8672,8 @@ class TestClass : public TestFixture { /*const*/ Settings settings = settingsBuilder().severity(Severity::warning).build(); settings.safeChecks.classes = true; - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -8723,10 +8689,8 @@ class TestClass : public TestFixture { #define checkThisUseAfterFree(code) checkThisUseAfterFree_(code, __FILE__, __LINE__) void checkThisUseAfterFree_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings1); - // Tokenize.. - SimpleTokenizer tokenizer(settings1, *this, &preprocessor); + SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. @@ -8924,10 +8888,8 @@ class TestClass : public TestFixture { #define getFileInfo(code) getFileInfo_(code, __FILE__, __LINE__) void getFileInfo_(const char code[], const char* file, int line) { - Preprocessor preprocessor(settings1); - // Tokenize.. - SimpleTokenizer tokenizer(settings1, *this, &preprocessor); + SimpleTokenizer tokenizer(settings1, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); // Check.. diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 7ae5338448f..d1b42b18cc6 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -129,7 +129,7 @@ class TestCondition : public TestFixture { void check_(const char* file, int line, const char code[], const Settings &settings, const char* filename = "test.cpp") { Preprocessor preprocessor(settings); std::vector files(1, filename); - Tokenizer tokenizer(settings, this, &preprocessor); + Tokenizer tokenizer(settings, this); PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer); // Tokenizer.. diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index 8972da28463..887f08bd14e 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -20,7 +20,6 @@ #include "errortypes.h" #include "fixture.h" #include "helpers.h" -#include "preprocessor.h" #include "settings.h" #include "token.h" @@ -283,10 +282,8 @@ class TestGarbage : public TestFixture { } std::string checkCodeInternal_(const char code[], bool cpp, const char* file, int line) { - Preprocessor preprocessor(settings); - // tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); // call all "runChecks" in all registered Check classes diff --git a/test/testother.cpp b/test/testother.cpp index eaddae272c6..6171845505e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -306,10 +306,8 @@ class TestOther : public TestFixture { settings->certainty.setEnabled(Certainty::inconclusive, inconclusive); settings->verbose = verbose; - Preprocessor preprocessor(*settings); - // Tokenize.. - SimpleTokenizer tokenizer(*settings, *this, &preprocessor); + SimpleTokenizer tokenizer(*settings, *this); ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line); // Check.. @@ -335,7 +333,7 @@ class TestOther : public TestFixture { Preprocessor preprocessor(*settings); std::vector files(1, filename); - Tokenizer tokenizer(*settings, this, &preprocessor); + Tokenizer tokenizer(*settings, this); PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer); // Tokenizer.. @@ -1703,10 +1701,8 @@ class TestOther : public TestFixture { // #5560 - set c++03 const Settings settings = settingsBuilder().severity(Severity::style).cpp(Standards::CPP03).build(); - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizerCpp(settings, *this, &preprocessor); + SimpleTokenizer tokenizerCpp(settings, *this); ASSERT_LOC(tokenizerCpp.tokenize(code), file, line); CheckOther checkOtherCpp(&tokenizerCpp, &settings, this); @@ -1924,10 +1920,8 @@ class TestOther : public TestFixture { /*const*/ Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability, portability).certainty(Certainty::inconclusive, inconclusive).build(); settings.platform.defaultSign = 's'; - Preprocessor preprocessor(settings); - // Tokenize.. - SimpleTokenizer tokenizer(settings, *this, &preprocessor); + SimpleTokenizer tokenizer(settings, *this); ASSERT_LOC(tokenizer.tokenize(code), file, line); CheckOther checkOtherCpp(&tokenizer, &settings, this); diff --git a/test/testtype.cpp b/test/testtype.cpp index 17eebb60c9b..d96ad4661fc 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -67,7 +67,7 @@ class TestType : public TestFixture { Preprocessor preprocessor(settings1); std::vector files(1, filename); - Tokenizer tokenizer(settings1, this, &preprocessor); + Tokenizer tokenizer(settings1, this); PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer, dui); // Tokenizer.. diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index ba87fe9e160..8ae6de2019b 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -260,12 +260,10 @@ class TestUnusedVar : public TestFixture { #define functionVariableUsage(...) functionVariableUsage_(__FILE__, __LINE__, __VA_ARGS__) #define checkStructMemberUsage(...) checkStructMemberUsage_(__FILE__, __LINE__, __VA_ARGS__) void checkStructMemberUsage_(const char* file, int line, const char code[], const std::list* directives = nullptr, const Settings *s = nullptr) { - Preprocessor preprocessor(settings); - const Settings *settings1 = s ? s : &settings; // Tokenize.. - SimpleTokenizer tokenizer(*settings1, *this, &preprocessor); + SimpleTokenizer tokenizer(*settings1, *this); if (directives) tokenizer.setDirectives(*directives); ASSERT_LOC(tokenizer.tokenize(code), file, line); @@ -279,7 +277,7 @@ class TestUnusedVar : public TestFixture { void checkStructMemberUsageP_(const char* file, int line, const char code[]) { std::vector files(1, "test.cpp"); Preprocessor preprocessor(settings); - Tokenizer tokenizer(settings, this, &preprocessor); + Tokenizer tokenizer(settings, this); PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer); // Tokenizer.. @@ -294,7 +292,7 @@ class TestUnusedVar : public TestFixture { void checkFunctionVariableUsageP_(const char* file, int line, const char code[], const char* filename = "test.cpp") { Preprocessor preprocessor(settings); std::vector files(1, filename); - Tokenizer tokenizer(settings, this, &preprocessor); + Tokenizer tokenizer(settings, this); PreprocessorHelper::preprocess(preprocessor, code, files, tokenizer); // Tokenizer..