From e72ec4282dadb4d5577a5429e66167da874e1975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 22 Apr 2017 12:44:57 +0200 Subject: [PATCH] Fixed #7842 (Preprocessor::getConfigs: #error in #ifndef not handled well) --- lib/preprocessor.cpp | 14 ++++++++++++-- test/testpreprocessor.cpp | 37 +++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index d2c1f7330..fa7297fb0 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -184,6 +184,11 @@ static std::string readcondition(const simplecpp::Token *iftok, const std::setstr; } + if (len == 2 && cond->op == '!' && next1->name) { + if (defined.find(next1->str) == defined.end()) + return next1->str + "=0"; + } + if (len == 3 && cond->op == '(' && next1->name && next2->op == ')') { if (defined.find(next1->str) == defined.end() && undefined.find(next1->str) == undefined.end()) return next1->str; @@ -375,7 +380,10 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set ret.erase(""); std::vector configs(configs_if); configs.push_back(configs_ifndef.back()); - ret.insert(cfg(configs, userDefines)); + ret.erase(cfg(configs, userDefines)); + if (!elseError.empty()) + elseError += ';'; + elseError += cfg(configs_ifndef, userDefines); } if (!configs_if.empty() && !configs_if.back().empty()) { const std::string &last = configs_if.back(); @@ -383,9 +391,11 @@ static void getConfigs(const simplecpp::TokenList &tokens, std::set std::vector configs(configs_if); ret.erase(cfg(configs, userDefines)); configs[configs.size() - 1U] = last.substr(0,last.size()-2U); - ret.insert(cfg(configs, userDefines)); if (configs.size() == 1U) ret.erase(""); + if (!elseError.empty()) + elseError += ';'; + elseError += cfg(configs, userDefines); } } } else if (cmdtok->str == "define" && sameline(tok, cmdtok->next) && cmdtok->next->name) { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 8a834e769..2d17254fb 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -379,15 +379,32 @@ private: } void error6() { - const char filedata[] = "#ifdef A\n" - "#else\n" - "#error 1\n" - "#endif\n" - "#ifdef B\n" - "#else\n" - "#error 2\n" - "#endif\n"; - ASSERT_EQUALS("\nA\nA;B\nB\n", getConfigsStr(filedata)); + const char filedata1[] = "#ifdef A\n" + "#else\n" + "#error 1\n" + "#endif\n" + "#ifdef B\n" + "#else\n" + "#error 2\n" + "#endif\n"; + ASSERT_EQUALS("\nA\nA;B\nB\n", getConfigsStr(filedata1)); + + const char filedata2[] = "#ifndef A\n" + "#error 1\n" + "#endif\n" + "#ifndef B\n" + "#error 2\n" + "#endif\n"; + ASSERT_EQUALS("A;B\n", getConfigsStr(filedata2)); + + const char filedata3[] = "#if !A\n" + "#error 1\n" + "#endif\n" + "#if !B\n" + "#error 2\n" + "#endif\n"; + ASSERT_EQUALS("A;B\n", getConfigsStr(filedata3)); + } void setPlatformInfo() { @@ -2105,7 +2122,7 @@ private: "#error \"!Y\"\n" "#endif\n" "#endif\n"; - ASSERT_EQUALS("\nX\nX;Y\n", getConfigsStr(filedata2)); + ASSERT_EQUALS("\nX\nY\n", getConfigsStr(filedata2)); } void getConfigsD1() {