From 7c4423889c83ee3604a4fe7c040717be2df9387b Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 14 Jun 2009 23:37:18 +0300 Subject: [PATCH] Fixed ticket #405 (#ifdef A and #if defined A should be handled as same configuration) http://sourceforge.net/apps/trac/cppcheck/ticket/405 --- src/preprocessor.cpp | 7 +++++-- test/testpreprocessor.cpp | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 5b8ddfc06..b1c7e724f 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -513,7 +513,7 @@ void Preprocessor::preprocess(std::istream &istr, std::string &processedFile, st std::string Preprocessor::getdef(std::string line, bool def) { // If def is true, the line must start with "#ifdef" - if (def && line.find("#ifdef ") != 0 && line.find("#if ") != 0 && line.find("#elif ") != 0) + if (def && line.find("#ifdef ") != 0 && line.find("#if ") != 0 && line.find("#elif ") != 0 && line.find("#if defined ") != 0) { return ""; } @@ -525,7 +525,10 @@ std::string Preprocessor::getdef(std::string line, bool def) } // Remove the "#ifdef" or "#ifndef" - line.erase(0, line.find(" ")); + if (line.find("#if defined ") == 0) + line.erase(0, 11); + else + line.erase(0, line.find(" ")); // Remove all spaces. while (line.find(" ") != std::string::npos) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 184478722..29611a7fc 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -132,6 +132,7 @@ private: TEST_CASE(macro_parameters); TEST_CASE(newline_in_macro); TEST_CASE(includes); + TEST_CASE(ifdef_ifdefined); } @@ -1122,6 +1123,27 @@ private: ASSERT_EQUALS("c.h", src); } } + + void ifdef_ifdefined() + { + const char filedata[] = "#ifdef ABC\n" + "A\n" + "#endif\t\n" + "#if defined ABC\n" + "A\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\n\n", actual[""]); + ASSERT_EQUALS("\nA\n\n\nA\n\n", actual["ABC"]); + ASSERT_EQUALS(2, static_cast(actual.size())); + } }; REGISTER_TEST(TestPreprocessor)