Skip to content

Commit d48a44f

Browse files
committed
Take naveen-seth's suggestion.
1 parent be7e37d commit d48a44f

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ static bool isQuoteCppDigitSeparator(const char *const Start,
419419
}
420420

421421
void Scanner::skipLine(const char *&First, const char *const End) {
422-
const char *const OldFirst = First;
423422
for (;;) {
424423
assert(First <= End);
425424
if (First == End)
@@ -430,6 +429,9 @@ void Scanner::skipLine(const char *&First, const char *const End) {
430429
return;
431430
}
432431
const char *Start = First;
432+
// Use `LastNonWhitespace`to track if a line-continuation has ever been seen
433+
// before a new-line character:
434+
char LastNonWhitespace = ' ';
433435
while (First != End && !isVerticalWhitespace(*First)) {
434436
// Iterate over strings correctly to avoid comments and newlines.
435437
if (*First == '"' ||
@@ -453,6 +455,8 @@ void Scanner::skipLine(const char *&First, const char *const End) {
453455
// Iterate over comments correctly.
454456
if (*First != '/' || End - First < 2) {
455457
LastTokenPtr = First;
458+
if (!isWhitespace(*First))
459+
LastNonWhitespace = *First;
456460
++First;
457461
continue;
458462
}
@@ -465,6 +469,8 @@ void Scanner::skipLine(const char *&First, const char *const End) {
465469

466470
if (First[1] != '*') {
467471
LastTokenPtr = First;
472+
if (!isWhitespace(*First))
473+
LastNonWhitespace = *First;
468474
++First;
469475
continue;
470476
}
@@ -476,26 +482,9 @@ void Scanner::skipLine(const char *&First, const char *const End) {
476482
return;
477483

478484
// Skip over the newline.
479-
auto Len = skipNewline(First, End);
480-
// Since P2223R2 allows the line-continuation slash \ to be followed by
481-
// additional whitespace, we need to check that here if `First`
482-
// follows a `\\` and whitespaces.
483-
484-
// `LookBack` points to the character before the newline:
485-
const char *LookBack = First - Len;
486-
bool LineContinuationFound = false;
487-
488-
// Move `LookBack` backwards to find line-continuation and whitespaces:
489-
while (LookBack >= OldFirst) { // bound `LookBack` by `OldFirst`:
490-
if (isWhitespace(*LookBack)) {
491-
--LookBack; // whitespace before '\\' is ok
492-
continue;
493-
}
494-
if (*LookBack == '\\')
495-
LineContinuationFound = true;
496-
break; // not a whitespace, end loop
497-
}
498-
if (!LineContinuationFound)
485+
skipNewline(First, End);
486+
487+
if (LastNonWhitespace != '\\')
499488
break;
500489
}
501490
}

clang/unittests/Lex/DependencyDirectivesScannerTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,10 @@ TEST(MinimizeSourceToDependencyDirectivesTest,
890890
"#define __TEST \n"
891891
"#if defined(__TEST_DUMMY) \n"
892892
"#if defined(__TEST_DUMMY2) \n"
893-
"#pragma GCC warning \\ \n"
893+
"#pragma GCC warning \\ \n"
894894
"\"hello!\"\n"
895895
"#else\n"
896-
"#pragma GCC error \\ \n"
896+
"#pragma GCC error \\ \n"
897897
"\"world!\" \n"
898898
"#endif // defined(__TEST_DUMMY2) \n"
899899
"#endif // defined(__TEST_DUMMY) \n"

0 commit comments

Comments
 (0)