Skip to content

Commit 2ce31ac

Browse files
wjakobssonWilliam Jakobsson
andauthored
Fixed #591: Newline in #error message (#654)
Co-authored-by: William Jakobsson <william.jakobsson@cppcheck.com>
1 parent 4ca0ee6 commit 2ce31ac

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

simplecpp.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,21 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
773773
ch = stream.readChar();
774774
}
775775
stream.ungetChar();
776-
push_back(new Token(currentToken, location));
777-
location.adjust(currentToken);
776+
std::string::size_type pos = 0;
777+
unsigned int spliced = 0;
778+
while ((pos = currentToken.find('\\', pos)) != std::string::npos) {
779+
if (pos + 1 < currentToken.size() && currentToken[pos + 1] == '\n') {
780+
currentToken.erase(pos, 2);
781+
++spliced;
782+
} else {
783+
++pos;
784+
}
785+
}
786+
if (!currentToken.empty()) {
787+
push_back(new Token(currentToken, location));
788+
location.adjust(currentToken);
789+
}
790+
location.line += spliced;
778791
continue;
779792
}
780793
}

test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,6 +1423,43 @@ static void error5()
14231423
ASSERT_EQUALS("file0,1,#error,#error x\n", toString(outputList));
14241424
}
14251425

1426+
static void error6()
1427+
{
1428+
// "#error\<LF>"
1429+
const char code[] = "\xFF\xFE\x23\x00\x65\x00\x72\x00\x72\x00\x6f\x00\x72\x00\x20\x00\x5c\x00\x0a\x00";
1430+
std::vector<std::string> files;
1431+
simplecpp::FileDataCache cache;
1432+
simplecpp::OutputList outputList;
1433+
simplecpp::TokenList tokens2(files);
1434+
const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code),files,"test.c");
1435+
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
1436+
ASSERT_EQUALS("file0,1,#error,#error \n", toString(outputList));
1437+
}
1438+
1439+
static void error7()
1440+
{
1441+
const char code[] = "#error bla\\\nbla\n";
1442+
std::vector<std::string> files;
1443+
simplecpp::FileDataCache cache;
1444+
simplecpp::OutputList outputList;
1445+
simplecpp::TokenList tokens2(files);
1446+
const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code),files,"test.c");
1447+
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
1448+
ASSERT_EQUALS("file0,1,#error,#error blabla\n", toString(outputList));
1449+
}
1450+
1451+
static void error8()
1452+
{
1453+
const char code[] = "#error bla\\\r\nbla\n";
1454+
std::vector<std::string> files;
1455+
simplecpp::FileDataCache cache;
1456+
simplecpp::OutputList outputList;
1457+
simplecpp::TokenList tokens2(files);
1458+
const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code),files,"test.c");
1459+
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
1460+
ASSERT_EQUALS("file0,1,#error,#error blabla\n", toString(outputList));
1461+
}
1462+
14261463
static void garbage()
14271464
{
14281465
simplecpp::OutputList outputList;
@@ -3933,6 +3970,9 @@ static void runTests(int argc, char **argv, Input input)
39333970
TEST_CASE(error3);
39343971
TEST_CASE(error4);
39353972
TEST_CASE(error5);
3973+
TEST_CASE(error6);
3974+
TEST_CASE(error7);
3975+
TEST_CASE(error8);
39363976

39373977
TEST_CASE(garbage);
39383978
TEST_CASE(garbage_endif);

0 commit comments

Comments
 (0)