From 0deb694d943f914657437229f077b64aacf4fec7 Mon Sep 17 00:00:00 2001 From: Hubert Tong Date: Thu, 30 Jul 2015 21:30:00 +0000 Subject: [PATCH] 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! llvm-svn: 243692 --- clang/lib/Lex/Pragma.cpp | 6 +++++- clang/test/Preprocessor/_Pragma.c | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp index 5eb6655..db8e305 100644 --- a/clang/lib/Lex/Pragma.cpp +++ b/clang/lib/Lex/Pragma.cpp @@ -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(); diff --git a/clang/test/Preprocessor/_Pragma.c b/clang/test/Preprocessor/_Pragma.c index 120e754..9923187 100644 --- a/clang/test/Preprocessor/_Pragma.c +++ b/clang/test/Preprocessor/_Pragma.c @@ -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}} -- 2.7.4