From 6de6da603e11e119488ac535e74df2b4bb394aeb Mon Sep 17 00:00:00 2001 From: Alp Toker Date: Sat, 14 Dec 2013 23:32:31 +0000 Subject: [PATCH] Lexer: Issue -Wbackslash-newline-escape for line comments The warning for backslash and newline separated by whitespace was missed in this code path. backslash is handled differently from compiler to compiler so it's important to warn consistently where there's ambiguity. Matches similar handling of block comments and non-comment lines. llvm-svn: 197331 --- clang/lib/Lex/Lexer.cpp | 9 ++++++++- clang/test/Lexer/bcpl-escaped-newline.c | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp index 009f18ad2349..6ad214258054 100644 --- a/clang/lib/Lex/Lexer.cpp +++ b/clang/lib/Lex/Lexer.cpp @@ -2023,8 +2023,11 @@ bool Lexer::SkipLineComment(Token &Result, const char *CurPtr, if (C != 0) { // We found a newline, see if it's escaped. const char *EscapePtr = CurPtr-1; - while (isHorizontalWhitespace(*EscapePtr)) // Skip whitespace. + bool HasSpace = false; + while (isHorizontalWhitespace(*EscapePtr)) { // Skip whitespace. --EscapePtr; + HasSpace = true; + } if (*EscapePtr == '\\') // Escaped newline. CurPtr = EscapePtr; @@ -2033,6 +2036,10 @@ bool Lexer::SkipLineComment(Token &Result, const char *CurPtr, CurPtr = EscapePtr-2; else break; // This is a newline, we're done. + + // If there was space between the backslash and newline, warn about it. + if (HasSpace && !isLexingRawMode()) + Diag(EscapePtr, diag::backslash_newline_space); } // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to diff --git a/clang/test/Lexer/bcpl-escaped-newline.c b/clang/test/Lexer/bcpl-escaped-newline.c index d87ee9b83aed..05d4773b87eb 100644 --- a/clang/test/Lexer/bcpl-escaped-newline.c +++ b/clang/test/Lexer/bcpl-escaped-newline.c @@ -11,3 +11,4 @@ // Trailing whitespace! //\ #error quux +// expected-warning@-2 {{backslash and newline separated by space}}