From 417f14fccf12a340845f7d3b4ae2ad02fca1c236 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 28 Mar 2010 15:15:25 +0300 Subject: [PATCH] Fix #1534 (False positive with #elif (defined NAME)) http://sourceforge.net/apps/trac/cppcheck/ticket/1534 --- lib/preprocessor.cpp | 7 ++--- test/testpreprocessor.cpp | 54 ++++++++++++++++++++++++++++----------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 0e61382bb..262ccec63 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -353,7 +353,8 @@ std::string Preprocessor::removeParantheses(const std::string &str) } // "#if(A) => #if A", but avoid "#if (defined A) || defined (B)" - if (line.compare(0, 4, "#if(") == 0 && line[line.length() - 1] == ')') + if ((line.compare(0, 4, "#if(") == 0 || line.compare(0, 6, "#elif(") == 0) && + line[line.length() - 1] == ')') { int ind = 0; for (std::string::size_type i = 0; i < line.length(); ++i) @@ -367,7 +368,7 @@ std::string Preprocessor::removeParantheses(const std::string &str) { if (i == line.length() - 1) { - line[3] = ' '; + line[line.find('(')] = ' '; line.erase(line.length() - 1); } break; @@ -378,7 +379,7 @@ std::string Preprocessor::removeParantheses(const std::string &str) if (line.compare(0, 4, "#if(") == 0) line.insert(3, " "); - else if (line.compare(0, 4, "#elif(") == 0) + else if (line.compare(0, 6, "#elif(") == 0) line.insert(5, " "); } ret << line << "\n"; diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 5da07e4b1..08d2db93f 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -616,23 +616,47 @@ private: void elif() { - const char filedata[] = "#if DEF1\n" - "ABC\n" - "#elif DEF2\n" - "DEF\n" - "#endif\n"; + { + const char filedata[] = "#if DEF1\n" + "ABC\n" + "#elif DEF2\n" + "DEF\n" + "#endif\n"; - // Preprocess => actual result.. - std::istringstream istr(filedata); - std::map actual; - Preprocessor preprocessor; - preprocessor.preprocess(istr, actual, "file.c"); + // Preprocess => actual result.. + std::istringstream istr(filedata); + std::map actual; + Preprocessor preprocessor; + preprocessor.preprocess(istr, actual, "file.c"); - // Compare results.. - ASSERT_EQUALS("\n\n\n\n\n", actual[""]); - ASSERT_EQUALS("\nABC\n\n\n\n", actual["DEF1"]); - ASSERT_EQUALS("\n\n\nDEF\n\n", actual["DEF2"]); - ASSERT_EQUALS(3, static_cast(actual.size())); + // Compare results.. + ASSERT_EQUALS("\n\n\n\n\n", actual[""]); + ASSERT_EQUALS("\nABC\n\n\n\n", actual["DEF1"]); + ASSERT_EQUALS("\n\n\nDEF\n\n", actual["DEF2"]); + ASSERT_EQUALS(3, static_cast(actual.size())); + } + + { + const char filedata[] = "#if(defined DEF1)\n" + "ABC\n" + "#elif(defined DEF2)\n" + "DEF\n" + "#else\n" + "GHI\n" + "#endif\n"; + + // Preprocess => actual result.. + std::istringstream istr(filedata); + std::map actual; + Preprocessor preprocessor; + preprocessor.preprocess(istr, actual, "file.c"); + + // Compare results.. + ASSERT_EQUALS("\n\n\n\n\nGHI\n\n", actual[""]); + ASSERT_EQUALS("\nABC\n\n\n\n\n\n", actual["DEF1"]); + ASSERT_EQUALS("\n\n\nDEF\n\n\n\n", actual["DEF2"]); + ASSERT_EQUALS(3, static_cast(actual.size())); + } }