diff options
-rw-r--r-- | clang/lib/Format/FormatTokenLexer.cpp | 3 | ||||
-rw-r--r-- | clang/lib/Lex/Lexer.cpp | 28 | ||||
-rw-r--r-- | clang/test/Lexer/newline-nul.c | bin | 0 -> 332 bytes | |||
-rw-r--r-- | clang/unittests/Format/FormatTestComments.cpp | 2 |
4 files changed, 14 insertions, 19 deletions
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 4ee43d6937e..1acc0c30651 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -467,6 +467,9 @@ FormatToken *FormatTokenLexer::getNextToken() { if (pos >= 0 && Text[pos] == '\r') --pos; // See whether there is an odd number of '\' before this. + // FIXME: This is wrong. A '\' followed by a newline is always removed, + // regardless of whether there is another '\' before it. + // FIXME: Newlines can also be escaped by a '?' '?' '/' trigraph. unsigned count = 0; for (; pos >= 0; --pos, ++count) if (Text[pos] != '\\') diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 4c051939471..5d0fe42defd 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -1282,12 +1282,6 @@ Slash: Size += EscapedNewLineSize; Ptr += EscapedNewLineSize; - // If the char that we finally got was a \n, then we must have had - // something like \<newline><newline>. We don't want to consume the - // second newline. - if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') - return ' '; - // Use slow version to accumulate a correct size field. return getCharAndSizeSlow(Ptr, Size, Tok); } @@ -1338,12 +1332,6 @@ Slash: Size += EscapedNewLineSize; Ptr += EscapedNewLineSize; - // If the char that we finally got was a \n, then we must have had - // something like \<newline><newline>. We don't want to consume the - // second newline. - if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') - return ' '; - // Use slow version to accumulate a correct size field. return getCharAndSizeSlowNoWarn(Ptr, Size, LangOpts); } @@ -2070,8 +2058,11 @@ bool Lexer::SkipLineComment(Token &Result, const char *CurPtr, // Scan over the body of the comment. The common case, when scanning, is that // the comment contains normal ascii characters with nothing interesting in // them. As such, optimize for this case with the inner loop. + // + // This loop terminates with CurPtr pointing at the newline (or end of buffer) + // character that ends the line comment. char C; - do { + while (true) { C = *CurPtr; // Skip over characters in the fast loop. while (C != 0 && // Potentially EOF. @@ -2097,6 +2088,8 @@ bool Lexer::SkipLineComment(Token &Result, const char *CurPtr, break; // This is a newline, we're done. // If there was space between the backslash and newline, warn about it. + // FIXME: This warning is bogus if trigraphs are disabled and the line + // ended with '?' '?' '\\' '\n'. if (HasSpace && !isLexingRawMode()) Diag(EscapePtr, diag::backslash_newline_space); } @@ -2140,9 +2133,9 @@ bool Lexer::SkipLineComment(Token &Result, const char *CurPtr, } } - if (CurPtr == BufferEnd+1) { - --CurPtr; - break; + if (C == '\r' || C == '\n' || CurPtr == BufferEnd + 1) { + --CurPtr; + break; } if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { @@ -2150,8 +2143,7 @@ bool Lexer::SkipLineComment(Token &Result, const char *CurPtr, cutOffLexing(); return false; } - - } while (C != '\n' && C != '\r'); + } // Found but did not consume the newline. Notify comment handlers about the // comment unless we're in a #if 0 block. diff --git a/clang/test/Lexer/newline-nul.c b/clang/test/Lexer/newline-nul.c Binary files differnew file mode 100644 index 00000000000..dd1b4199d42 --- /dev/null +++ b/clang/test/Lexer/newline-nul.c diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index df24abe2612..e6310158e91 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -2018,7 +2018,7 @@ TEST_F(FormatTestComments, AlignTrailingComments) { format("#define MACRO(V)\\\n" "V(Rt2) /* one more char */ \\\n" "V(Rs) /* than here */ \\\n" - "/* comment 3 */ \\\n", + "/* comment 3 */\n", getLLVMStyleWithColumns(40))); EXPECT_EQ("int i = f(abc, // line 1\n" " d, // line 2\n" |