From 381361629e15c6538c7291a30c87841d7310c624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Thu, 2 Mar 2023 21:50:57 +0100 Subject: [PATCH] Preprocessor: removed unreachable `ConfigurationNotChecked` finding (#4790) --- lib/preprocessor.cpp | 43 --------------------------------------- lib/preprocessor.h | 9 -------- test/testpreprocessor.cpp | 35 +------------------------------ 3 files changed, 1 insertion(+), 86 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 212cfeeaa..3fa5663b0 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -725,10 +725,6 @@ simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens tokens2.removeComments(); - // ensure that guessed define macros without value are not used in the code - if (!validateCfg(cfg, macroUsage)) - return simplecpp::TokenList(files); - return tokens2; } @@ -878,44 +874,6 @@ void Preprocessor::missingInclude(const std::string &filename, unsigned int line } } -bool Preprocessor::validateCfg(const std::string &cfg, const std::list ¯oUsageList) -{ - bool ret = true; - std::list defines; - splitcfg(cfg, defines, emptyString); - for (const std::string &define : defines) { - if (define.find('=') != std::string::npos) - continue; - const std::string macroName(define.substr(0, define.find('('))); - for (const simplecpp::MacroUsage &mu : macroUsageList) { - if (mu.macroValueKnown) - continue; - if (mu.macroName != macroName) - continue; - const bool directiveLocation = std::any_of(mDirectives.cbegin(), mDirectives.cend(), - [=](const Directive &dir) { - return mu.useLocation.file() == dir.file && mu.useLocation.line == dir.linenr; - }); - - if (!directiveLocation) { - if (mSettings.severity.isEnabled(Severity::information)) - validateCfgError(mu.useLocation.file(), mu.useLocation.line, cfg, macroName); - ret = false; - } - } - } - - return ret; -} - -void Preprocessor::validateCfgError(const std::string &file, const unsigned int line, const std::string &cfg, const std::string ¯o) -{ - const std::string id = "ConfigurationNotChecked"; - ErrorMessage::FileLocation loc(file, line, 0); - const ErrorMessage errmsg({std::move(loc)}, mFile0, Severity::information, "Skipping configuration '" + cfg + "' since the value of '" + macro + "' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.", id, Certainty::normal); - mErrorLogger->reportInfo(errmsg); -} - void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) { Settings settings2(*settings); @@ -923,7 +881,6 @@ void Preprocessor::getErrorMessages(ErrorLogger *errorLogger, const Settings *se settings2.checkConfiguration = true; preprocessor.missingInclude(emptyString, 1, emptyString, UserHeader); preprocessor.missingInclude(emptyString, 1, emptyString, SystemHeader); - preprocessor.validateCfgError(emptyString, 1, "X", "X"); preprocessor.error(emptyString, 1, "#error message"); // #error .. } diff --git a/lib/preprocessor.h b/lib/preprocessor.h index cc641368b..0c97333da 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -156,15 +156,6 @@ public: */ std::string getcode(const std::string &filedata, const std::string &cfg, const std::string &filename); - /** - * make sure empty configuration macros are not used in code. the given code must be a single configuration - * @param cfg configuration - * @param macroUsageList macro usage list - * @return true => configuration is valid - */ - bool validateCfg(const std::string &cfg, const std::list ¯oUsageList); - void validateCfgError(const std::string &file, const unsigned int line, const std::string &cfg, const std::string ¯o); - /** * Calculate HASH. Using toolinfo, tokens1, filedata. * diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 84798ae29..27b7deaf2 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -244,9 +244,6 @@ private: TEST_CASE(getConfigsU6); TEST_CASE(getConfigsU7); - TEST_CASE(validateCfg1); - TEST_CASE(validateCfg2); - TEST_CASE(if_sizeof); TEST_CASE(invalid_ifs); // #5909 @@ -274,6 +271,7 @@ private: TEST_CASE(testMissingIncludeCheckConfig); } + // TODO: we should be calling the actual Preprocessor::preprocess() implementation void preprocess(const char* code, std::map& actual, const char filename[] = "file.c") { errout.str(""); std::istringstream istr(code); @@ -2281,37 +2279,6 @@ private: ASSERT_EQUALS("\nY\n", getConfigsStr(code, "-DX")); } - - void validateCfg1() { - Preprocessor preprocessor(settings0, this); - - std::vector files(1, "test.c"); - simplecpp::MacroUsage macroUsage(files, false); - macroUsage.useLocation.fileIndex = 0; - macroUsage.useLocation.line = 1; - macroUsage.macroName = "X"; - std::list macroUsageList(1, macroUsage); - - ASSERT_EQUALS(true, preprocessor.validateCfg("", macroUsageList)); - ASSERT_EQUALS(false, preprocessor.validateCfg("X",macroUsageList)); - ASSERT_EQUALS(false, preprocessor.validateCfg("A=42;X", macroUsageList)); - ASSERT_EQUALS(true, preprocessor.validateCfg("X=1", macroUsageList)); - ASSERT_EQUALS(true, preprocessor.validateCfg("Y", macroUsageList)); - - macroUsageList.front().macroValueKnown = true; // #8404 - ASSERT_EQUALS(true, preprocessor.validateCfg("X", macroUsageList)); - } - - void validateCfg2() { - const char filedata[] = "#ifdef ABC\n" - "#endif\n" - "int i = ABC;"; - - std::map actual; - preprocess(filedata, actual, "file.cpp"); - ASSERT_EQUALS("[file.cpp:3]: (information) Skipping configuration 'ABC' since the value of 'ABC' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.\n", errout.str()); - } - void if_sizeof() { // #4071 static const char* code = "#if sizeof(unsigned short) == 2\n" "Fred & Wilma\n"