diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index f7f56185d..0fc64cc33 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -178,6 +178,10 @@ std::string Preprocessor::read(std::istream &istr, const std::string &filename, // Remove all comments.. result = removeComments(result, filename, settings); + // Remove '#if 0' blocks + if (result.find("#if 0\n") != std::string::npos) + result = removeIf0(result); + // ------------------------------------------------------------------------------------------ // // Clean up all preprocessor statements @@ -547,6 +551,37 @@ std::string Preprocessor::removeComments(const std::string &str, const std::stri return code.str(); } +std::string Preprocessor::removeIf0(const std::string &code) +{ + std::ostringstream ret; + std::istringstream istr(code); + std::string line; + while (std::getline(istr,line)) + { + if (line != "#if 0") + ret << line << "\n"; + else + { + // replace '#if 0' with empty line + ret << "\n"; + + // goto the end of the '#if 0' block + unsigned int level = 1; + while (level > 0 && std::getline(istr,line)) + { + if (line.compare(0,3,"#if") == 0) + ++level; + else if (line == "#endif") + --level; + + // replace code within '#if 0' block with empty lines + ret << "\n"; + } + } + } + return ret.str(); +} + std::string Preprocessor::removeParantheses(const std::string &str) { diff --git a/lib/preprocessor.h b/lib/preprocessor.h index 82f5da2bd..e871f8562 100644 --- a/lib/preprocessor.h +++ b/lib/preprocessor.h @@ -144,6 +144,13 @@ protected: */ std::string removeComments(const std::string &str, const std::string &filename, Settings *settings); + /** + * Cleanup 'if 0' from the code + * @param str Code processed by read(). + * @return code without 'if 0' + */ + static std::string removeIf0(const std::string &code); + /** * Remove redundant parentheses from preprocessor commands. This should only be called from read(). * @param str Code processed by read(). diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index d01a79829..37df95c61 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -92,6 +92,10 @@ private: TEST_CASE(error3); + // Don't handle include in a #if 0 block + TEST_CASE(if0_include_1); + TEST_CASE(if0_include_2); + // Handling include guards (don't create extra configuration for it) TEST_CASE(includeguard1); TEST_CASE(includeguard2); @@ -637,6 +641,32 @@ private: ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str()); } + void if0_include_1() + { + Settings settings; + Preprocessor preprocessor(&settings, this); + + std::istringstream code("#if 0\n" + "#include \"a.h\"\n" + "#endif\n" + "AB\n"); + ASSERT_EQUALS("\n\n\nAB\n", preprocessor.read(code,"",NULL)); + } + + void if0_include_2() + { + Settings settings; + Preprocessor preprocessor(&settings, this); + + std::istringstream code("#if 0\n" + "#include \"a.h\"\n" + "#ifdef WIN32\n" + "#else\n" + "#endif\n" + "#endif\n" + "AB\n"); + ASSERT_EQUALS("\n\n\n\n\n\nAB\n", preprocessor.read(code,"",NULL)); + } void includeguard1() {