]> granicus.if.org Git - clang/commitdiff
Improved error recovery for _Pragma
authorHubert Tong <hubert.reinterpretcast@gmail.com>
Thu, 30 Jul 2015 21:30:00 +0000 (21:30 +0000)
committerHubert Tong <hubert.reinterpretcast@gmail.com>
Thu, 30 Jul 2015 21:30:00 +0000 (21:30 +0000)
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!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@243692 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Lex/Pragma.cpp
test/Preprocessor/_Pragma.c

index 5eb665549e86ffa1ce7836f48251ce6b1a2ccd7c..db8e3055b85cd966e02eed58749e385f281115a5 100644 (file)
@@ -191,9 +191,13 @@ void Preprocessor::Handle_Pragma(Token &Tok) {
   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();
index 120e754cb986831425fc22087a835b23d134fd49..99231879ece0604e2be789bec344a050a692292a 100644 (file)
@@ -12,4 +12,8 @@ _Pragma("message(\"foo \\\\\\\\ bar\")") // expected-warning {{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}}