r197331 - Lexer: Issue -Wbackslash-newline-escape for line comments

Alp Toker alp at nuanti.com
Sat Dec 14 15:32:31 PST 2013


Author: alp
Date: Sat Dec 14 17:32:31 2013
New Revision: 197331

URL: http://llvm.org/viewvc/llvm-project?rev=197331&view=rev
Log:
Lexer: Issue -Wbackslash-newline-escape for line comments

The warning for backslash and newline separated by whitespace was missed in
this code path.

backslash<whitespace><newline> 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.

Modified:
    cfe/trunk/lib/Lex/Lexer.cpp
    cfe/trunk/test/Lexer/bcpl-escaped-newline.c

Modified: cfe/trunk/lib/Lex/Lexer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Lexer.cpp?rev=197331&r1=197330&r2=197331&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Lexer.cpp (original)
+++ cfe/trunk/lib/Lex/Lexer.cpp Sat Dec 14 17:32:31 2013
@@ -2023,8 +2023,11 @@ bool Lexer::SkipLineComment(Token &Resul
     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 &Resul
         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

Modified: cfe/trunk/test/Lexer/bcpl-escaped-newline.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/bcpl-escaped-newline.c?rev=197331&r1=197330&r2=197331&view=diff
==============================================================================
--- cfe/trunk/test/Lexer/bcpl-escaped-newline.c (original)
+++ cfe/trunk/test/Lexer/bcpl-escaped-newline.c Sat Dec 14 17:32:31 2013
@@ -11,3 +11,4 @@
 // Trailing whitespace!
 //\ 
 #error quux
+// expected-warning at -2 {{backslash and newline separated by space}}





More information about the cfe-commits mailing list