From 363605906fbec0a1e5605d0646b589d33359c9e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 21 Aug 2016 07:45:15 +0200 Subject: [PATCH] Fixed #7695 (Running with -U_WIN32 still checks related configurations) --- lib/preprocessor.cpp | 22 +++++++++++++++++++++- test/testpreprocessor.cpp | 27 +++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index e42500b64..1db2ec32d 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -231,6 +231,23 @@ static std::string cfg(const std::vector &configs) return ret; } +static bool isUndefined(const std::string &cfg, const std::set &undefined) +{ + for (std::string::size_type pos1 = 0U; pos1 < cfg.size();) { + const std::string::size_type pos2 = cfg.find(";",pos1); + const std::string def = (pos2 == std::string::npos) ? cfg.substr(pos1) : cfg.substr(pos1, pos2 - pos1); + + std::string::size_type eq = def.find("="); + if (eq == std::string::npos && undefined.find(def) != undefined.end()) + return true; + if (eq != std::string::npos && undefined.find(def.substr(0,eq)) != undefined.end() && def.substr(eq) != "=0") + return true; + + pos1 = (pos2 == std::string::npos) ? pos2 : pos2 + 1U; + } + return false; +} + static void getConfigs(const simplecpp::TokenList &tokens, std::set &defined, const std::set &undefined, std::set &ret) { std::vector configs_if; @@ -253,8 +270,11 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set } else if (cmdtok->str == "if") { config = readcondition(cmdtok, defined); } - if (undefined.find(config) != undefined.end()) + + // skip undefined configurations.. + if (isUndefined(config, undefined)) config.clear(); + configs_if.push_back((cmdtok->str == "ifndef") ? std::string() : config); configs_ifndef.push_back((cmdtok->str == "ifndef") ? config : std::string()); ret.insert(cfg(configs_if)); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 4953fee57..a05e32e65 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -220,6 +220,9 @@ private: TEST_CASE(undef1); TEST_CASE(undef2); TEST_CASE(undef3); + TEST_CASE(undef4); + TEST_CASE(undef5); + TEST_CASE(undef6); TEST_CASE(validateCfg); @@ -2087,8 +2090,6 @@ private: } void undef3() { - Settings settings; - const char filedata[] = "#ifndef X\n" "Fred & Wilma\n" "#else\n" @@ -2098,6 +2099,28 @@ private: ASSERT_EQUALS("\nX\n", getConfigsStr(filedata)); } + void undef4() { + const char filedata[] = "#if defined(X) || defined(Y) || defined(Z)\n" + "#else\n" + "#endif\n"; + ASSERT_EQUALS("\n", getConfigsStr(filedata, "X")); + ASSERT_EQUALS("\nX;Y;Z\n", getConfigsStr(filedata)); + } + + void undef5() { + const char filedata[] = "#if X==1\n" + "#endif\n"; + ASSERT_EQUALS("\n", getConfigsStr(filedata, "X")); + ASSERT_EQUALS("\nX=1\n", getConfigsStr(filedata)); + } + + void undef6() { + const char filedata[] = "#if X==0\n" + "#endif\n"; + ASSERT_EQUALS("\nX=0\n", getConfigsStr(filedata, "X")); + ASSERT_EQUALS("\nX=0\n", getConfigsStr(filedata)); + } + void validateCfg() { Settings settings; Preprocessor preprocessor(settings, this);