r243692 - Improved error recovery for _Pragma

Hubert Tong hubert.reinterpretcast at gmail.com
Thu Jul 30 14:30:00 PDT 2015


Author: hubert.reinterpretcast
Date: Thu Jul 30 16:30:00 2015
New Revision: 243692

URL: http://llvm.org/viewvc/llvm-project?rev=243692&view=rev
Log:
Improved error recovery for _Pragma

Summary:
Currently, if the argument to _Pragma is not a parenthesised string
literal, the bad token will be consumed, as well as the ')', if present.
If additional bad tokens are passed to the _Pragma, this results in
extra error messages which may distract from the true problem.

The proposed patch causes all tokens to be consumed until the closing
')' or a new line, whichever is reached first.

Reviewers: hfinkel, rsmith

Subscribers: hubert.reinterpretcast, fraggamuffin, rnk, cfe-commits

Differential Revision: http://reviews.llvm.org/D8308

Patch by Rachel Craik!

Modified:
    cfe/trunk/lib/Lex/Pragma.cpp
    cfe/trunk/test/Preprocessor/_Pragma.c

Modified: cfe/trunk/lib/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Pragma.cpp?rev=243692&r1=243691&r2=243692&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Pragma.cpp (original)
+++ cfe/trunk/lib/Lex/Pragma.cpp Thu Jul 30 16:30:00 2015
@@ -191,9 +191,13 @@ void Preprocessor::Handle_Pragma(Token &
   Lex(Tok);
   if (!tok::isStringLiteral(Tok.getKind())) {
     Diag(PragmaLoc, diag::err__Pragma_malformed);
-    // Skip this token, and the ')', if present.
+    // Skip bad tokens, and the ')', if present.
     if (Tok.isNot(tok::r_paren) && Tok.isNot(tok::eof))
       Lex(Tok);
+    while (Tok.isNot(tok::r_paren) &&
+           !Tok.isAtStartOfLine() &&
+           Tok.isNot(tok::eof))
+      Lex(Tok);
     if (Tok.is(tok::r_paren))
       Lex(Tok);
     return _PragmaLexing.failed();

Modified: cfe/trunk/test/Preprocessor/_Pragma.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/_Pragma.c?rev=243692&r1=243691&r2=243692&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/_Pragma.c (original)
+++ cfe/trunk/test/Preprocessor/_Pragma.c Thu Jul 30 16:30:00 2015
@@ -12,4 +12,8 @@ _Pragma("message(\"foo \\\\\\\\ bar\")")
 #error #define invalid
 #endif
 
+_Pragma(unroll 1 // expected-error{{_Pragma takes a parenthesized string literal}}
+
+_Pragma(clang diagnostic push) // expected-error{{_Pragma takes a parenthesized string literal}}
+
 _Pragma( // expected-error{{_Pragma takes a parenthesized string literal}}





More information about the cfe-commits mailing list