diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index 337be9ed9..c21d8e761 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -1222,10 +1222,10 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filename // If endfile is encountered, we have moved to a next file in our stack, // so remove last path in our list. - while ((endfilePos = code.find("#endfile", endfilePos)) != std::string::npos && endfilePos < pos) + while ((endfilePos = code.find("\n#endfile", endfilePos)) != std::string::npos && endfilePos < pos) { paths.pop_back(); - endfilePos += 8; // size of #endfile + endfilePos += 9; // size of #endfile } endfilePos = pos; diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 59cc31e8e..05e8542eb 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -156,6 +156,7 @@ private: // define and then ifdef TEST_CASE(define_ifdef); + TEST_CASE(endfile); } @@ -1602,6 +1603,23 @@ private: TODO_ASSERT_EQUALS(1, actual.size()); } } + + void endfile() + { + const char filedata[] = "char a[] = \"#endfile\";\n" + "char b[] = \"#endfile\";\n" + "#include \"notfound.h\"\n"; + + // Preprocess => actual result.. + std::istringstream istr(filedata); + std::map actual; + Preprocessor preprocessor; + preprocessor.preprocess(istr, actual, "file.c"); + + // Compare results.. + ASSERT_EQUALS("char a[] = \"#endfile\";\nchar b[] = \"#endfile\";\n\n", actual[""]); + ASSERT_EQUALS(1, actual.size()); + } }; REGISTER_TEST(TestPreprocessor)