diff --git a/simplecpp.cpp b/simplecpp.cpp index 7f65309d..4a4d4145 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -771,8 +771,21 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, ch = stream.readChar(); } stream.ungetChar(); - push_back(new Token(currentToken, location)); - location.adjust(currentToken); + std::string::size_type pos = 0; + unsigned int spliced = 0; + while ((pos = currentToken.find('\\', pos)) != std::string::npos) { + if (pos + 1 < currentToken.size() && currentToken[pos + 1] == '\n') { + currentToken.erase(pos, 2); + ++spliced; + } else { + ++pos; + } + } + if (!currentToken.empty()) { + push_back(new Token(currentToken, location)); + location.adjust(currentToken); + } + location.line += spliced; continue; } } diff --git a/test.cpp b/test.cpp index cd6a4db5..e45cbe39 100644 --- a/test.cpp +++ b/test.cpp @@ -1423,6 +1423,43 @@ static void error5() ASSERT_EQUALS("file0,1,#error,#error x\n", toString(outputList)); } +static void error6() +{ + // "#error\" + const char code[] = "\xFF\xFE\x23\x00\x65\x00\x72\x00\x72\x00\x6f\x00\x72\x00\x20\x00\x5c\x00\x0a\x00"; + std::vector files; + simplecpp::FileDataCache cache; + simplecpp::OutputList outputList; + simplecpp::TokenList tokens2(files); + const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code),files,"test.c"); + simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList); + ASSERT_EQUALS("file0,1,#error,#error \n", toString(outputList)); +} + +static void error7() +{ + const char code[] = "#error bla\\\nbla\n"; + std::vector files; + simplecpp::FileDataCache cache; + simplecpp::OutputList outputList; + simplecpp::TokenList tokens2(files); + const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code),files,"test.c"); + simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList); + ASSERT_EQUALS("file0,1,#error,#error blabla\n", toString(outputList)); +} + +static void error8() +{ + const char code[] = "#error bla\\\r\nbla\n"; + std::vector files; + simplecpp::FileDataCache cache; + simplecpp::OutputList outputList; + simplecpp::TokenList tokens2(files); + const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code),files,"test.c"); + simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList); + ASSERT_EQUALS("file0,1,#error,#error blabla\n", toString(outputList)); +} + static void garbage() { simplecpp::OutputList outputList; @@ -3933,6 +3970,9 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(error3); TEST_CASE(error4); TEST_CASE(error5); + TEST_CASE(error6); + TEST_CASE(error7); + TEST_CASE(error8); TEST_CASE(garbage); TEST_CASE(garbage_endif);