Skip to content

Commit 29abbc6

Browse files
authored
fix #337 - line splicing in comment not handled properly (#431)
1 parent 5bf471d commit 29abbc6

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

simplecpp.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -758,17 +758,34 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
758758

759759
// comment
760760
else if (ch == '/' && stream.peekChar() == '/') {
761-
while (stream.good() && ch != '\r' && ch != '\n') {
761+
while (stream.good() && ch != '\n') {
762762
currentToken += ch;
763763
ch = stream.readChar();
764+
if(ch == '\\') {
765+
TokenString tmp;
766+
char tmp_ch = ch;
767+
while((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) {
768+
tmp += tmp_ch;
769+
tmp_ch = stream.readChar();
770+
}
771+
if(!stream.good()) {
772+
break;
773+
}
774+
775+
if(tmp_ch != '\n') {
776+
currentToken += tmp;
777+
} else {
778+
TokenString check_portability = currentToken + tmp;
779+
const std::string::size_type pos = check_portability.find_last_not_of(" \t");
780+
if (pos < check_portability.size() - 1U && check_portability[pos] == '\\')
781+
portabilityBackslash(outputList, files, location);
782+
++multiline;
783+
tmp_ch = stream.readChar();
784+
}
785+
ch = tmp_ch;
786+
}
764787
}
765-
const std::string::size_type pos = currentToken.find_last_not_of(" \t");
766-
if (pos < currentToken.size() - 1U && currentToken[pos] == '\\')
767-
portabilityBackslash(outputList, files, location);
768-
if (currentToken[currentToken.size() - 1U] == '\\') {
769-
++multiline;
770-
currentToken.erase(currentToken.size() - 1U);
771-
} else {
788+
if (ch == '\n') {
772789
stream.ungetChar();
773790
}
774791
}

test.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,30 @@ static void comment_multiline()
434434
const char code[] = "#define ABC {// \\\n"
435435
"}\n"
436436
"void f() ABC\n";
437-
ASSERT_EQUALS("\n\nvoid f ( ) { }", preprocess(code));
437+
ASSERT_EQUALS("\n\nvoid f ( ) {", preprocess(code));
438+
439+
const char code1[] = "#define ABC {// \\\r\n"
440+
"}\n"
441+
"void f() ABC\n";
442+
ASSERT_EQUALS("\n\nvoid f ( ) {", preprocess(code1));
443+
444+
const char code2[] = "#define A 1// \\\r"
445+
"\r"
446+
"2\r"
447+
"A\r";
448+
ASSERT_EQUALS("\n\n2\n1", preprocess(code2));
449+
450+
const char code3[] = "void f() {// \\ \n}\n";
451+
ASSERT_EQUALS("void f ( ) {", preprocess(code3));
452+
453+
const char code4[] = "void f() {// \\\\\\\t\t\n}\n";
454+
ASSERT_EQUALS("void f ( ) {", preprocess(code4));
455+
456+
const char code5[] = "void f() {// \\\\\\a\n}\n";
457+
ASSERT_EQUALS("void f ( ) {\n}", preprocess(code5));
458+
459+
const char code6[] = "void f() {// \\\n\n\n}\n";
460+
ASSERT_EQUALS("void f ( ) {\n\n\n}", preprocess(code6));
438461
}
439462

440463

0 commit comments

Comments
 (0)