From 3eed07bb6d36f9b54ad785811a88d385bc98c6d6 Mon Sep 17 00:00:00 2001 From: William Jakobsson Date: Mon, 25 May 2026 14:22:41 +0200 Subject: [PATCH 1/4] fix --- simplecpp.cpp | 14 ++++++++++++++ test.cpp | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/simplecpp.cpp b/simplecpp.cpp index 7f65309d..9d3eeacc 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -771,8 +771,22 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, ch = stream.readChar(); } stream.ungetChar(); + 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 if (pos + 2 < currentToken.size() && currentToken[pos + 1] == '\r' && currentToken[pos + 2] == '\n') { + currentToken.erase(pos, 3); + ++spliced; + } else { + ++pos; + } + } push_back(new Token(currentToken, location)); location.adjust(currentToken); + location.line += spliced; continue; } } diff --git a/test.cpp b/test.cpp index cd6a4db5..f405beab 100644 --- a/test.cpp +++ b/test.cpp @@ -1423,6 +1423,19 @@ 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 garbage() { simplecpp::OutputList outputList; @@ -3933,6 +3946,7 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(error3); TEST_CASE(error4); TEST_CASE(error5); + TEST_CASE(error6); TEST_CASE(garbage); TEST_CASE(garbage_endif); From 5445579fbfc05556c637917312f19ec86df38fb6 Mon Sep 17 00:00:00 2001 From: William Jakobsson Date: Mon, 25 May 2026 14:33:44 +0200 Subject: [PATCH 2/4] tests --- test.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test.cpp b/test.cpp index f405beab..e45cbe39 100644 --- a/test.cpp +++ b/test.cpp @@ -1436,6 +1436,30 @@ static void error6() 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; @@ -3947,6 +3971,8 @@ static void runTests(int argc, char **argv, Input input) TEST_CASE(error4); TEST_CASE(error5); TEST_CASE(error6); + TEST_CASE(error7); + TEST_CASE(error8); TEST_CASE(garbage); TEST_CASE(garbage_endif); From 6aefdc4fa8507be24d73b5da241018a7e291b99e Mon Sep 17 00:00:00 2001 From: William Jakobsson Date: Mon, 25 May 2026 16:27:37 +0200 Subject: [PATCH 3/4] fix --- simplecpp.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 9d3eeacc..d887a66a 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -784,8 +784,10 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, ++pos; } } - push_back(new Token(currentToken, location)); - location.adjust(currentToken); + if (!currentToken.empty()) { + push_back(new Token(currentToken, location)); + location.adjust(currentToken); + } location.line += spliced; continue; } From 5ac75f618450b4e0b175d76bee5b05d1c669ec7c Mon Sep 17 00:00:00 2001 From: William Jakobsson Date: Tue, 26 May 2026 10:17:02 +0200 Subject: [PATCH 4/4] remove --- simplecpp.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index d887a66a..4a4d4145 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -777,9 +777,6 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, if (pos + 1 < currentToken.size() && currentToken[pos + 1] == '\n') { currentToken.erase(pos, 2); ++spliced; - } else if (pos + 2 < currentToken.size() && currentToken[pos + 1] == '\r' && currentToken[pos + 2] == '\n') { - currentToken.erase(pos, 3); - ++spliced; } else { ++pos; }