diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index d1dbe6f13..7a01126eb 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -467,7 +467,9 @@ std::string Preprocessor::removeSpaceNearNL(const std::string &str) std::string Preprocessor::replaceIfDefined(const std::string &str) { std::string ret(str); - std::string::size_type pos = 0; + std::string::size_type pos; + + pos = 0; while ((pos = ret.find("#if defined(", pos)) != std::string::npos) { std::string::size_type pos2 = ret.find(")", pos + 9); @@ -482,6 +484,21 @@ std::string Preprocessor::replaceIfDefined(const std::string &str) ++pos; } + pos = 0; + while ((pos = ret.find("#if !defined(", pos)) != std::string::npos) + { + std::string::size_type pos2 = ret.find(")", pos + 9); + if (pos2 > ret.length() - 1) + break; + if (ret[pos2+1] == '\n') + { + ret.erase(pos2, 1); + ret.erase(pos + 3, 10); + ret.insert(pos + 3, "ndef "); + } + ++pos; + } + return ret; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index f6d9149a6..e9fd1ce2c 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -93,6 +93,7 @@ private: TEST_CASE(multiline4); TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA" + TEST_CASE(if_not_defined); // "#if !defined(AAA)" => "#ifndef AAA" // Macros.. TEST_CASE(macro_simple1); @@ -591,13 +592,14 @@ private: { const char filedata[] = "#if defined(AAA)\n" "#endif\n"; + ASSERT_EQUALS("#ifdef AAA\n#endif\n", OurPreprocessor::replaceIfDefined(filedata)); + } - - // Expected result.. - std::string expected("#ifdef AAA\n#endif\n"); - - // Compare result.. - ASSERT_EQUALS(expected, OurPreprocessor::replaceIfDefined(filedata)); + void if_not_defined() + { + const char filedata[] = "#if !defined(AAA)\n" + "#endif\n"; + ASSERT_EQUALS("#ifndef AAA\n#endif\n", OurPreprocessor::replaceIfDefined(filedata)); }