diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 81be83222..e0e405ece 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1543,6 +1543,7 @@ void Preprocessor::simplifyCondition(const std::map &c modified = false; modified |= tokenizer.simplifySizeof(); modified |= tokenizer.simplifyCalculations(); + modified |= tokenizer.simplifyConstTernaryOp(); modified |= tokenizer.simplifyRedundantParentheses(); for (Token *tok = const_cast(tokenizer.tokens()); tok; tok = tok->next()) { if (Token::Match(tok, "! %num%")) { @@ -1728,13 +1729,18 @@ std::string Preprocessor::getcode(const std::string &filedata, const std::string } } + else if (line.compare(0,4,"#if ") == 0) { + matching_ifdef.push_back(match_cfg_def(cfgmap, line.substr(4))); + matched_ifdef.push_back(matching_ifdef.back()); + } + else if (! def.empty()) { - matching_ifdef.push_back(match_cfg_def(cfgmap, def)); + matching_ifdef.push_back(cfgmap.find(def) != cfgmap.end()); matched_ifdef.push_back(matching_ifdef.back()); } else if (! ndef.empty()) { - matching_ifdef.push_back(! match_cfg_def(cfgmap, ndef)); + matching_ifdef.push_back(cfgmap.find(ndef) == cfgmap.end()); matched_ifdef.push_back(matching_ifdef.back()); } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index f5732b3f6..c8899c148 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -227,6 +227,7 @@ private: TEST_CASE(define_if3); TEST_CASE(define_if4); // #4079 - #define X +123 TEST_CASE(define_if5); // #4516 - #define B (A & 0x00f0) + TEST_CASE(define_if6); // #4863 - #define B (A?-1:1) TEST_CASE(define_ifdef); TEST_CASE(define_ifndef1); TEST_CASE(define_ifndef2); @@ -2771,6 +2772,27 @@ private: } } + void define_if6() { // #4516 - #define B (A?1:-1) + const char filedata[] = "#ifdef A\n" + "#define B (A?1:-1)\n" + "#endif\n" + "\n" + "#if B < 0\n" + "123\n" + "#endif\n" + "\n" + "#if B >= 0\n" + "456\n" + "#endif\n"; + Preprocessor preprocessor(NULL, this); + const std::string actualA0 = preprocessor.getcode(filedata, "A=0", "test.c"); + ASSERT_EQUALS(true, actualA0.find("123") != std::string::npos); + ASSERT_EQUALS(false, actualA0.find("456") != std::string::npos); + const std::string actualA1 = preprocessor.getcode(filedata, "A=1", "test.c"); + ASSERT_EQUALS(false, actualA1.find("123") != std::string::npos); + ASSERT_EQUALS(true, actualA1.find("456") != std::string::npos); + } + void define_ifdef() { { const char filedata[] = "#define ABC\n" @@ -2830,7 +2852,7 @@ private: { const char filedata[] = "#define A 1\n" - "#ifdef A>0\n" + "#if A>0\n" "A\n" "#endif\n"; @@ -3123,7 +3145,7 @@ private: } void predefine1() { - const std::string src("#ifdef X || Y\n" + const std::string src("#if defined X || Y\n" "Fred & Wilma\n" "#endif\n"); @@ -3134,7 +3156,7 @@ private: } void predefine2() { - const std::string src("#ifdef X && Y\n" + const std::string src("#if defined(X) && Y\n" "Fred & Wilma\n" "#endif\n"); {