diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 55b44bea6..fba95c7e8 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -637,6 +637,8 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const // 0=>Source file, 1=>Included by source file, 2=>included by header that was included by source file, etc int filelevel = 0; + bool includeguard = false; + unsigned int linenr = 0; std::istringstream istr(filedata); std::string line; @@ -646,18 +648,20 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const if (line.compare(0, 6, "#file ") == 0) { + includeguard = true; ++filelevel; continue; } else if (line == "#endfile") { + includeguard = false; if (filelevel > 0) --filelevel; continue; } - else if (line.compare(0, 8, "#define ") == 0 && line.find("(", 8) == std::string::npos) + if (line.compare(0, 8, "#define ") == 0 && line.find("(", 8) == std::string::npos) { if (line.find(" ", 8) == std::string::npos) defines.insert(line.substr(8)); @@ -669,7 +673,10 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const } } - if (filelevel > 0) + if (!line.empty() && line.compare(0, 3, "#if") != 0) + includeguard = false; + + if (includeguard) continue; std::string def = getdef(line, true) + getdef(line, false); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 304bddbcc..41f91037b 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -82,7 +82,8 @@ private: TEST_CASE(error2); // Handling include guards (don't create extra configuration for it) - TEST_CASE(includeguard); + TEST_CASE(includeguard1); + TEST_CASE(includeguard2); TEST_CASE(newlines); @@ -436,7 +437,7 @@ private: } - void includeguard() + void includeguard1() { // Handling include guards.. const char filedata[] = "#file \"abc.h\"\n" @@ -457,6 +458,29 @@ private: ASSERT_EQUALS(2, static_cast(actual.size())); } + void includeguard2() + { + // Handling include guards.. + const char filedata[] = "#file \"abc.h\"\n" + "foo\n" + "#ifdef ABC\n" + "\n" + "#endif\n" + "#endfile\n"; + + // Preprocess => actual result.. + std::istringstream istr(filedata); + std::map actual; + Preprocessor preprocessor; + preprocessor.preprocess(istr, actual, "file.c"); + + // Expected configurations: "" and "ABC" + ASSERT_EQUALS(2, static_cast(actual.size())); + ASSERT_EQUALS(true, actual.find("") != actual.end()); + ASSERT_EQUALS(true, actual.find("ABC") != actual.end()); + } + + void ifdefwithfile() { // Handling include guards..