From 4b9323f6a6b5e96c4fd0d077209b9a89527749f4 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 21 Oct 2025 08:50:01 +0200 Subject: [PATCH] store reference of associated tokenlist in `Preprocessor` --- lib/cppcheck.cpp | 34 ++++++++-------- lib/cppcheck.h | 3 +- lib/preprocessor.cpp | 85 ++++++++++++++++++++------------------- lib/preprocessor.h | 29 +++++++------ test/helpers.cpp | 8 ++-- test/testcppcheck.cpp | 10 ++--- test/testpreprocessor.cpp | 54 +++++++++++++------------ test/testtokenize.cpp | 6 +-- test/testtokenlist.cpp | 4 +- 9 files changed, 118 insertions(+), 115 deletions(-) diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 2048d6f8098..ddffda0fac3 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -847,7 +847,7 @@ unsigned int CppCheck::check(const FileSettings &fs) return returnValue; } -std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const simplecpp::TokenList& tokens, const std::string& filePath) const +std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const std::string& filePath) const { std::ostringstream toolinfo; toolinfo << (mSettings.cppcheckCfgProductName.empty() ? CPPCHECK_VERSION_STRING : mSettings.cppcheckCfgProductName); @@ -865,7 +865,7 @@ std::size_t CppCheck::calculateHash(const Preprocessor& preprocessor, const simp toolinfo << mSettings.premiumArgs; // TODO: do we need to add more options? mSuppressions.nomsg.dump(toolinfo, filePath); - return preprocessor.calculateHash(tokens, toolinfo.str()); + return preprocessor.calculateHash(toolinfo.str()); } unsigned int CppCheck::checkBuffer(const FileWithDetails &file, const std::string &cfgname, int fileIndex, const uint8_t* data, std::size_t size) @@ -938,8 +938,8 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str std::vector files; simplecpp::TokenList tokens = createTokenList(files, nullptr); if (analyzerInformation) { - const Preprocessor preprocessor(mSettings, mErrorLogger, file.lang()); - hash = calculateHash(preprocessor, tokens); + const Preprocessor preprocessor(tokens, mSettings, mErrorLogger, file.lang()); + hash = calculateHash(preprocessor); } tokenlist.createTokens(std::move(tokens)); // 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. @@ -984,9 +984,9 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str return mLogger->exitcode(); } - Preprocessor preprocessor(mSettings, mErrorLogger, file.lang()); + Preprocessor preprocessor(tokens1, mSettings, mErrorLogger, file.lang()); - if (!preprocessor.loadFiles(tokens1, files)) + if (!preprocessor.loadFiles(files)) return mLogger->exitcode(); if (!mSettings.plistOutput.empty()) { @@ -1006,14 +1006,14 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str } // Parse comments and then remove them - mLogger->setRemarkComments(preprocessor.getRemarkComments(tokens1)); - preprocessor.inlineSuppressions(tokens1, mSuppressions.nomsg); + mLogger->setRemarkComments(preprocessor.getRemarkComments()); + preprocessor.inlineSuppressions(mSuppressions.nomsg); if (mSettings.dump || !mSettings.addons.empty()) { std::ostringstream oss; mSuppressions.nomsg.dump(oss); dumpProlog += oss.str(); } - preprocessor.removeComments(tokens1); + preprocessor.removeComments(); if (!mSettings.buildDir.empty()) { analyzerInformation.reset(new AnalyzerInformation); @@ -1022,7 +1022,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str if (analyzerInformation) { // Calculate hash so it can be compared with old hash / future hashes - const std::size_t hash = calculateHash(preprocessor, tokens1, file.spath()); + const std::size_t hash = calculateHash(preprocessor, file.spath()); std::list errors; if (!analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, fileIndex, hash, errors)) { while (!errors.empty()) { @@ -1035,16 +1035,16 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str } // Get directives - std::list directives = preprocessor.createDirectives(tokens1); - preprocessor.simplifyPragmaAsm(tokens1); + std::list directives = preprocessor.createDirectives(); + preprocessor.simplifyPragmaAsm(); - Preprocessor::setPlatformInfo(tokens1, mSettings); + preprocessor.setPlatformInfo(); // Get configurations.. std::set configurations; if ((mSettings.checkAllConfigurations && mSettings.userDefines.empty()) || mSettings.force) { Timer::run("Preprocessor::getConfigs", mSettings.showtime, &s_timerResults, [&]() { - configurations = preprocessor.getConfigs(tokens1); + configurations = preprocessor.getConfigs(); }); } else { configurations.insert(mSettings.userDefines); @@ -1052,7 +1052,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str if (mSettings.checkConfiguration) { for (const std::string &config : configurations) - (void)preprocessor.getcode(tokens1, config, files, false); + (void)preprocessor.getcode(config, files, false); if (analyzerInformation) mLogger->setAnalyzerInfo(nullptr); @@ -1125,7 +1125,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str if (mSettings.preprocessOnly) { std::string codeWithoutCfg; Timer::run("Preprocessor::getcode", mSettings.showtime, &s_timerResults, [&]() { - codeWithoutCfg = preprocessor.getcode(tokens1, currentConfig, files, true); + codeWithoutCfg = preprocessor.getcode(currentConfig, files, true); }); if (startsWith(codeWithoutCfg,"#file")) @@ -1148,7 +1148,7 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str // Create tokens, skip rest of iteration if failed Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() { - simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, currentConfig, files, true); + simplecpp::TokenList tokensP = preprocessor.preprocess(currentConfig, files, true); tokenlist.createTokens(std::move(tokensP)); }); hasValidConfig = true; diff --git a/lib/cppcheck.h b/lib/cppcheck.h index 9108ac28430..dc075fc8b2e 100644 --- a/lib/cppcheck.h +++ b/lib/cppcheck.h @@ -172,10 +172,9 @@ class CPPCHECKLIB CppCheck { * @brief Calculate hash used to detect when a file needs to be reanalyzed. * * @param preprocessor Preprocessor used to calculate the hash. - * @param tokens Token list from preprocessed file. * @return hash */ - std::size_t calculateHash(const Preprocessor &preprocessor, const simplecpp::TokenList &tokens, const std::string& filePath = {}) const; + std::size_t calculateHash(const Preprocessor &preprocessor, const std::string& filePath = {}) const; /** * @brief Check a file diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index a701d29a962..99a162a4d8b 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -65,8 +65,9 @@ Directive::DirectiveToken::DirectiveToken(const simplecpp::Token & _tok) : char Preprocessor::macroChar = char(1); -Preprocessor::Preprocessor(const Settings& settings, ErrorLogger &errorLogger, Standards::Language lang) - : mSettings(settings) +Preprocessor::Preprocessor(simplecpp::TokenList& tokens, const Settings& settings, ErrorLogger &errorLogger, Standards::Language lang) + : mTokens(tokens) + , mSettings(settings) , mErrorLogger(errorLogger) , mLang(lang) { @@ -301,12 +302,12 @@ static void addInlineSuppressions(const simplecpp::TokenList &tokens, const Sett bad.emplace_back(suppr.fileName, suppr.lineNumber, "Suppress Begin: No matching end"); } -void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens, SuppressionList &suppressions) +void Preprocessor::inlineSuppressions(SuppressionList &suppressions) { if (!mSettings.inlineSuppressions) return; std::list err; - ::addInlineSuppressions(tokens, mSettings, suppressions, err); + ::addInlineSuppressions(mTokens, mSettings, suppressions, err); for (const auto &filedata : mFileCache) { ::addInlineSuppressions(filedata->tokens, mSettings, suppressions, err); } @@ -315,24 +316,24 @@ void Preprocessor::inlineSuppressions(const simplecpp::TokenList &tokens, Suppre } } -std::vector Preprocessor::getRemarkComments(const simplecpp::TokenList &tokens) const +std::vector Preprocessor::getRemarkComments() const { std::vector ret; - addRemarkComments(tokens, ret); + addRemarkComments(mTokens, ret); for (const auto &filedata : mFileCache) { addRemarkComments(filedata->tokens, ret); } return ret; } -std::list Preprocessor::createDirectives(const simplecpp::TokenList &tokens) const +std::list Preprocessor::createDirectives() const { // directive list.. std::list directives; std::vector list; list.reserve(1U + mFileCache.size()); - list.push_back(&tokens); + list.push_back(&mTokens); std::transform(mFileCache.cbegin(), mFileCache.cend(), std::back_inserter(list), [](const std::unique_ptr &filedata) { return &filedata->tokens; @@ -656,15 +657,15 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set } -std::set Preprocessor::getConfigs(const simplecpp::TokenList &tokens) const +std::set Preprocessor::getConfigs() const { std::set ret = { "" }; - if (!tokens.cfront()) + if (!mTokens.cfront()) return ret; std::set defined = { "__cplusplus" }; - ::getConfigs(tokens, defined, mSettings.userDefines, mSettings.userUndefs, ret); + ::getConfigs(mTokens, defined, mSettings.userDefines, mSettings.userUndefs, ret); for (const auto &filedata : mFileCache) { if (!mSettings.configurationExcluded(filedata->filename)) @@ -774,45 +775,45 @@ void Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool th } } -bool Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector &files) +bool Preprocessor::loadFiles(std::vector &files) { const simplecpp::DUI dui = createDUI(mSettings, "", mLang); simplecpp::OutputList outputList; - mFileCache = simplecpp::load(rawtokens, files, dui, &outputList); + mFileCache = simplecpp::load(mTokens, files, dui, &outputList); handleErrors(outputList, false); return !hasErrors(outputList); } -void Preprocessor::removeComments(simplecpp::TokenList &tokens) const +void Preprocessor::removeComments() { - tokens.removeComments(); + mTokens.removeComments(); for (const auto &filedata : mFileCache) { filedata->tokens.removeComments(); } } -void Preprocessor::setPlatformInfo(simplecpp::TokenList &tokens, const Settings& settings) +void Preprocessor::setPlatformInfo() { - tokens.sizeOfType["bool"] = settings.platform.sizeof_bool; - tokens.sizeOfType["short"] = settings.platform.sizeof_short; - tokens.sizeOfType["int"] = settings.platform.sizeof_int; - tokens.sizeOfType["long"] = settings.platform.sizeof_long; - tokens.sizeOfType["long long"] = settings.platform.sizeof_long_long; - tokens.sizeOfType["float"] = settings.platform.sizeof_float; - tokens.sizeOfType["double"] = settings.platform.sizeof_double; - tokens.sizeOfType["long double"] = settings.platform.sizeof_long_double; - tokens.sizeOfType["bool *"] = settings.platform.sizeof_pointer; - tokens.sizeOfType["short *"] = settings.platform.sizeof_pointer; - tokens.sizeOfType["int *"] = settings.platform.sizeof_pointer; - tokens.sizeOfType["long *"] = settings.platform.sizeof_pointer; - tokens.sizeOfType["long long *"] = settings.platform.sizeof_pointer; - tokens.sizeOfType["float *"] = settings.platform.sizeof_pointer; - tokens.sizeOfType["double *"] = settings.platform.sizeof_pointer; - tokens.sizeOfType["long double *"] = settings.platform.sizeof_pointer; + mTokens.sizeOfType["bool"] = mSettings.platform.sizeof_bool; + mTokens.sizeOfType["short"] = mSettings.platform.sizeof_short; + mTokens.sizeOfType["int"] = mSettings.platform.sizeof_int; + mTokens.sizeOfType["long"] = mSettings.platform.sizeof_long; + mTokens.sizeOfType["long long"] = mSettings.platform.sizeof_long_long; + mTokens.sizeOfType["float"] = mSettings.platform.sizeof_float; + mTokens.sizeOfType["double"] = mSettings.platform.sizeof_double; + mTokens.sizeOfType["long double"] = mSettings.platform.sizeof_long_double; + mTokens.sizeOfType["bool *"] = mSettings.platform.sizeof_pointer; + mTokens.sizeOfType["short *"] = mSettings.platform.sizeof_pointer; + mTokens.sizeOfType["int *"] = mSettings.platform.sizeof_pointer; + mTokens.sizeOfType["long *"] = mSettings.platform.sizeof_pointer; + mTokens.sizeOfType["long long *"] = mSettings.platform.sizeof_pointer; + mTokens.sizeOfType["float *"] = mSettings.platform.sizeof_pointer; + mTokens.sizeOfType["double *"] = mSettings.platform.sizeof_pointer; + mTokens.sizeOfType["long double *"] = mSettings.platform.sizeof_pointer; } -simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector &files, bool throwError) +simplecpp::TokenList Preprocessor::preprocess(const std::string &cfg, std::vector &files, bool throwError) { const simplecpp::DUI dui = createDUI(mSettings, cfg, mLang); @@ -820,7 +821,7 @@ simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens std::list macroUsage; std::list ifCond; simplecpp::TokenList tokens2(files); - simplecpp::preprocess(tokens2, tokens1, files, mFileCache, dui, &outputList, ¯oUsage, &ifCond); + simplecpp::preprocess(tokens2, mTokens, files, mFileCache, dui, &outputList, ¯oUsage, &ifCond); mMacroUsage = std::move(macroUsage); mIfCond = std::move(ifCond); @@ -831,9 +832,9 @@ simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens return tokens2; } -std::string Preprocessor::getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector &files, const bool writeLocations) +std::string Preprocessor::getcode(const std::string &cfg, std::vector &files, const bool writeLocations) { - simplecpp::TokenList tokens2 = preprocess(tokens1, cfg, files, false); + simplecpp::TokenList tokens2 = preprocess(cfg, files, false); unsigned int prevfile = 0; unsigned int line = 1; std::ostringstream ret; @@ -929,7 +930,9 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line void Preprocessor::getErrorMessages(ErrorLogger &errorLogger, const Settings &settings) { - Preprocessor preprocessor(settings, errorLogger, Standards::Language::CPP); + std::vector files; + simplecpp::TokenList tokens(files); + Preprocessor preprocessor(tokens, settings, errorLogger, Standards::Language::CPP); preprocessor.missingInclude("", 1, "", UserHeader); preprocessor.missingInclude("", 1, "", SystemHeader); preprocessor.error("", 1, "#error message"); // #error .. @@ -971,10 +974,10 @@ void Preprocessor::dump(std::ostream &out) const } } -std::size_t Preprocessor::calculateHash(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const +std::size_t Preprocessor::calculateHash(const std::string &toolinfo) const { std::string hashData = toolinfo; - for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) { + for (const simplecpp::Token *tok = mTokens.cfront(); tok; tok = tok->next) { if (!tok->comment) { hashData += tok->str(); hashData += static_cast(tok->location.line); @@ -993,9 +996,9 @@ std::size_t Preprocessor::calculateHash(const simplecpp::TokenList &tokens1, con return (std::hash{})(hashData); } -void Preprocessor::simplifyPragmaAsm(simplecpp::TokenList &tokenList) const +void Preprocessor::simplifyPragmaAsm() { - Preprocessor::simplifyPragmaAsmPrivate(tokenList); + Preprocessor::simplifyPragmaAsmPrivate(mTokens); for (const auto &filedata : mFileCache) { Preprocessor::simplifyPragmaAsmPrivate(filedata->tokens); } diff --git a/lib/preprocessor.h b/lib/preprocessor.h index ddb43266454..f31265ff782 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -100,45 +100,42 @@ class CPPCHECKLIB RemarkComment { */ class CPPCHECKLIB WARN_UNUSED Preprocessor { // TODO: get rid of this - friend class PreprocessorHelper; friend class TestPreprocessor; - friend class TestUnusedVar; public: /** character that is inserted in expanded macros */ static char macroChar; - explicit Preprocessor(const Settings& settings, ErrorLogger &errorLogger, Standards::Language lang); + explicit Preprocessor(simplecpp::TokenList& tokens, const Settings& settings, ErrorLogger &errorLogger, Standards::Language lang); virtual ~Preprocessor() = default; - void inlineSuppressions(const simplecpp::TokenList &tokens, SuppressionList &suppressions); + void inlineSuppressions(SuppressionList &suppressions); - std::list createDirectives(const simplecpp::TokenList &tokens) const; + std::list createDirectives() const; - std::set getConfigs(const simplecpp::TokenList &tokens) const; + std::set getConfigs() const; - std::vector getRemarkComments(const simplecpp::TokenList &tokens) const; + std::vector getRemarkComments() const; - bool loadFiles(const simplecpp::TokenList &rawtokens, std::vector &files); + bool loadFiles(std::vector &files); - void removeComments(simplecpp::TokenList &tokens) const; + void removeComments(); - static void setPlatformInfo(simplecpp::TokenList &tokens, const Settings& settings); + void setPlatformInfo(); - simplecpp::TokenList preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector &files, bool throwError = false); + simplecpp::TokenList preprocess(const std::string &cfg, std::vector &files, bool throwError = false); - std::string getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector &files, bool writeLocations); + std::string getcode(const std::string &cfg, std::vector &files, bool writeLocations); /** * Calculate HASH. Using toolinfo, tokens1, filedata. * - * @param tokens1 Sourcefile tokens * @param toolinfo Arbitrary extra toolinfo * @return HASH */ - std::size_t calculateHash(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const; + std::size_t calculateHash(const std::string &toolinfo) const; - void simplifyPragmaAsm(simplecpp::TokenList &tokenList) const; + void simplifyPragmaAsm(); static void getErrorMessages(ErrorLogger &errorLogger, const Settings &settings); @@ -171,6 +168,8 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor { void addRemarkComments(const simplecpp::TokenList &tokens, std::vector &remarkComments) const; + simplecpp::TokenList& mTokens; + const Settings& mSettings; ErrorLogger &mErrorLogger; diff --git a/test/helpers.cpp b/test/helpers.cpp index 3530854f5b5..e3087966cda 100644 --- a/test/helpers.cpp +++ b/test/helpers.cpp @@ -113,15 +113,15 @@ ScopedFile::~ScopedFile() { void SimpleTokenizer2::preprocess(const char* code, std::size_t size, std::vector &files, const std::string& file0, Tokenizer& tokenizer, ErrorLogger& errorlogger) { - const simplecpp::TokenList tokens1(code, size, files, file0); + simplecpp::TokenList tokens1(code, size, files, file0); - Preprocessor preprocessor(tokenizer.getSettings(), errorlogger, Path::identify(tokens1.getFiles()[0], false)); - simplecpp::TokenList tokens2 = preprocessor.preprocess(tokens1, "", files, true); + Preprocessor preprocessor(tokens1, tokenizer.getSettings(), errorlogger, Path::identify(tokens1.getFiles()[0], false)); + simplecpp::TokenList tokens2 = preprocessor.preprocess("", files, true); // Tokenizer.. tokenizer.list.createTokens(std::move(tokens2)); - std::list directives = preprocessor.createDirectives(tokens1); + std::list directives = preprocessor.createDirectives(); tokenizer.setDirectives(std::move(directives)); } diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index ca5998bdd6b..b82f165fd45 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -531,10 +531,10 @@ class TestCppcheck : public TestFixture { std::vector files; const char code[] = "void f();\nint x;\n"; - const simplecpp::TokenList tokens(code, files, "m1.c"); + simplecpp::TokenList tokens(code, files, "m1.c"); - Preprocessor preprocessor(settings, errorLogger, Standards::Language::C); - ASSERT(preprocessor.loadFiles(tokens, files)); + Preprocessor preprocessor(tokens, settings, errorLogger, Standards::Language::C); + ASSERT(preprocessor.loadFiles(files)); AddonInfo premiumaddon; premiumaddon.name = "premiumaddon.json"; @@ -546,10 +546,10 @@ class TestCppcheck : public TestFixture { settings.premiumArgs = "misra-c-2012"; CppCheck check(settings, supprs, errorLogger, false, {}); - const size_t hash1 = check.calculateHash(preprocessor, tokens); + const size_t hash1 = check.calculateHash(preprocessor); settings.premiumArgs = ""; - const size_t hash2 = check.calculateHash(preprocessor, tokens); + const size_t hash2 = check.calculateHash(preprocessor); // cppcheck-suppress knownConditionTrueFalse ASSERT(hash1 != hash2); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 0582b8ef70e..c4ac6f405ee 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -53,9 +53,9 @@ class TestPreprocessor : public TestFixture { std::string expandMacros(const char (&code)[size], ErrorLogger &errorLogger) const { simplecpp::OutputList outputList; std::vector files; - const simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", &outputList); - Preprocessor p(settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false)); - simplecpp::TokenList tokens2 = p.preprocess(tokens1, "", files, true); + simplecpp::TokenList tokens1 = simplecpp::TokenList(code, files, "file.cpp", &outputList); + Preprocessor p(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false)); + simplecpp::TokenList tokens2 = p.preprocess("", files, true); p.reportOutput(outputList, true); return tokens2.stringify(); } @@ -85,10 +85,10 @@ class TestPreprocessor : public TestFixture { std::vector getRemarkComments(const char (&code)[size], ErrorLogger& errorLogger) const { std::vector files; - const simplecpp::TokenList tokens1(code, files, "test.cpp"); + simplecpp::TokenList tokens1(code, files, "test.cpp"); - const Preprocessor preprocessor(settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false)); - return preprocessor.getRemarkComments(tokens1); + const Preprocessor preprocessor(tokens1, settingsDefault, errorLogger, Path::identify(tokens1.getFiles()[0], false)); + return preprocessor.getRemarkComments(); } static std::string getcodeforcfg(const Settings& settings, ErrorLogger& errorlogger, const char* code, std::size_t size, const std::string &cfg, const std::string &filename, SuppressionList *inlineSuppression = nullptr) @@ -119,11 +119,11 @@ class TestPreprocessor : public TestFixture { simplecpp::TokenList tokens(code, size, files, Path::simplifyPath(filename), &outputList); // TODO: we should be using the actual Preprocessor implementation - Preprocessor preprocessor(settings, errorlogger, Path::identify(tokens.getFiles()[0], false)); + Preprocessor preprocessor(tokens, settings, errorlogger, Path::identify(tokens.getFiles()[0], false)); if (inlineSuppression) - preprocessor.inlineSuppressions(tokens, *inlineSuppression); - preprocessor.removeComments(tokens); - preprocessor.simplifyPragmaAsm(tokens); + preprocessor.inlineSuppressions(*inlineSuppression); + preprocessor.removeComments(); + preprocessor.simplifyPragmaAsm(); preprocessor.reportOutput(outputList, true); @@ -132,11 +132,11 @@ class TestPreprocessor : public TestFixture { std::map cfgcode; if (cfgs.empty()) - cfgs = preprocessor.getConfigs(tokens); + cfgs = preprocessor.getConfigs(); for (const std::string & config : cfgs) { try { const bool writeLocations = (strstr(code, "#file") != nullptr) || (strstr(code, "#include") != nullptr); - cfgcode[config] = preprocessor.getcode(tokens, config, files, writeLocations); + cfgcode[config] = preprocessor.getcode(config, files, writeLocations); } catch (const simplecpp::Output &) { cfgcode[config] = ""; } @@ -366,9 +366,9 @@ class TestPreprocessor : public TestFixture { std::vector files; // TODO: this adds an empty filename simplecpp::TokenList tokens(code,files); - tokens.removeComments(); - Preprocessor preprocessor(settings, *this, Standards::Language::C); // TODO: do we need to consider #file? - const std::set configs = preprocessor.getConfigs(tokens); + Preprocessor preprocessor(tokens, settings, *this, Standards::Language::C); // TODO: do we need to consider #file? + preprocessor.removeComments(); + const std::set configs = preprocessor.getConfigs(); std::string ret; for (const std::string & config : configs) ret += config + '\n'; @@ -380,9 +380,9 @@ class TestPreprocessor : public TestFixture { std::vector files; // TODO: this adds an empty filename simplecpp::TokenList tokens(code,files); - tokens.removeComments(); - Preprocessor preprocessor(settingsDefault, *this, Standards::Language::C); // TODO: do we need to consider #file? - return preprocessor.calculateHash(tokens, ""); + Preprocessor preprocessor(tokens, settingsDefault, *this, Standards::Language::C); // TODO: do we need to consider #file? + preprocessor.removeComments(); + return preprocessor.calculateHash(""); } void Bug2190219() { @@ -533,23 +533,25 @@ class TestPreprocessor : public TestFixture { "#else\n" "2\n" "#endif\n"; - std::vector files; - simplecpp::TokenList tokens(filedata, files, "test.c"); // preprocess code with unix32 platform.. { + std::vector files; + simplecpp::TokenList tokens(filedata, files, "test.c"); const Settings settings = settingsBuilder().platform(Platform::Type::Unix32).build(); - Preprocessor::setPlatformInfo(tokens, settings); - Preprocessor preprocessor(settings, *this, Path::identify(tokens.getFiles()[0], false)); - ASSERT_EQUALS("\n1", preprocessor.getcode(tokens, "", files, false)); + Preprocessor preprocessor(tokens, settings, *this, Path::identify(tokens.getFiles()[0], false)); + preprocessor.setPlatformInfo(); + ASSERT_EQUALS("\n1", preprocessor.getcode("", files, false)); } // preprocess code with unix64 platform.. { + std::vector files; + simplecpp::TokenList tokens(filedata, files, "test.c"); const Settings settings = settingsBuilder().platform(Platform::Type::Unix64).build(); - Preprocessor::setPlatformInfo(tokens, settings); - Preprocessor preprocessor(settings, *this, Path::identify(tokens.getFiles()[0], false)); - ASSERT_EQUALS("\n\n\n2", preprocessor.getcode(tokens, "", files, false)); + Preprocessor preprocessor(tokens, settings, *this, Path::identify(tokens.getFiles()[0], false)); + preprocessor.setPlatformInfo(); + ASSERT_EQUALS("\n\n\n2", preprocessor.getcode("", files, false)); } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d698ca5ce1a..989634826c4 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -579,9 +579,9 @@ class TestTokenizer : public TestFixture { void directiveDump(const char (&code)[size], const char filename[], const Settings& settings, std::ostream& ostr) { simplecpp::OutputList outputList; std::vector files; - const simplecpp::TokenList tokens1(code, files, filename, &outputList); - Preprocessor preprocessor(settings, *this, Path::identify(tokens1.getFiles()[0], false)); - std::list directives = preprocessor.createDirectives(tokens1); + simplecpp::TokenList tokens1(code, files, filename, &outputList); + Preprocessor preprocessor(tokens1, settings, *this, Path::identify(tokens1.getFiles()[0], false)); + std::list directives = preprocessor.createDirectives(); TokenList tokenlist{settings, Path::identify(filename, false)}; Tokenizer tokenizer(std::move(tokenlist), *this); diff --git a/test/testtokenlist.cpp b/test/testtokenlist.cpp index 7a38c91c791..2e7baa9e3a5 100644 --- a/test/testtokenlist.cpp +++ b/test/testtokenlist.cpp @@ -157,8 +157,8 @@ class TestTokenList : public TestFixture { const char code[] = "#include "; std::vector files; simplecpp::TokenList tokens1(code, files, "poll.h", nullptr); - Preprocessor preprocessor(settingsDefault, *this, Path::identify(tokens1.getFiles()[0], false)); - simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, "", files, true); + Preprocessor preprocessor(tokens1, settingsDefault, *this, Path::identify(tokens1.getFiles()[0], false)); + simplecpp::TokenList tokensP = preprocessor.preprocess("", files, true); TokenList tokenlist(settingsDefault, Standards::Language::C); // headers are treated as C files tokenlist.createTokens(std::move(tokensP)); // do not assert }