]> granicus.if.org Git - clang/commitdiff
Made the "expected string literal" diagnostic more expressive
authorAndy Gibbs <andyg1001@hotmail.co.uk>
Sat, 17 Nov 2012 19:16:52 +0000 (19:16 +0000)
committerAndy Gibbs <andyg1001@hotmail.co.uk>
Sat, 17 Nov 2012 19:16:52 +0000 (19:16 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@168267 91177308-0d34-0410-b5e6-96231b3b80d8

14 files changed:
include/clang/Basic/DiagnosticCommonKinds.td
include/clang/Lex/Preprocessor.h
lib/Lex/PPMacroExpansion.cpp
lib/Lex/Pragma.cpp
lib/Lex/Preprocessor.cpp
lib/Parse/ParseDecl.cpp
lib/Parse/ParseDeclCXX.cpp
lib/Parse/Parser.cpp
test/Lexer/pragma-message.c
test/Parser/attr-availability.c
test/Preprocessor/pragma_diagnostic.c
test/Preprocessor/pragma_microsoft.c
test/Preprocessor/warning_tests.c
test/Sema/static-assert.c

index c2df5da11abcc50373cef676bb7b870602039e69..534531bb4a179989cbbb95e93f34ed5438db313d 100644 (file)
@@ -41,7 +41,9 @@ def err_expected_colon : Error<"expected ':'">;
 def err_expected_colon_after_setter_name : Error<
   "method name referenced in property setter attribute "
   "must end with ':'">;
-def err_expected_string_literal : Error<"expected string literal">;
+def err_expected_string_literal : Error<"expected string literal "
+  "%select{in %1|for diagnostic message in static_assert|"
+          "for optional message in 'availability' attribute}0">;
 def err_invalid_string_udl : Error<
   "string literal with user-defined suffix cannot be used here">;
 def err_invalid_character_udl : Error<
index 486dcd8e684536661f3ce98a7db5d18cd1222a4c..7e5c66cbd8daa57d6350000ac7037d9c05c75795 100644 (file)
@@ -700,17 +700,19 @@ public:
   /// string literals and may even come from macro expansion.
   /// \returns true on success, false if a error diagnostic has been generated.
   bool LexStringLiteral(Token &Result, std::string &String,
-                        bool AllowMacroExpansion) {
+                        const char *DiagnosticTag, bool AllowMacroExpansion) {
     if (AllowMacroExpansion)
       Lex(Result);
     else
       LexUnexpandedToken(Result);
-    return FinishLexStringLiteral(Result, String, AllowMacroExpansion);
+    return FinishLexStringLiteral(Result, String, DiagnosticTag,
+                                  AllowMacroExpansion);
   }
 
   /// \brief Complete the lexing of a string literal where the first token has
   /// already been lexed (see LexStringLiteral).
   bool FinishLexStringLiteral(Token &Result, std::string &String,
+                              const char *DiagnosticTag,
                               bool AllowMacroExpansion);
 
   /// LexNonComment - Lex a token.  If it's a comment, keep lexing until we get
index 9f962b027fcbc788583b268111f3dc2c55296392..6baa593e788e7c9078775f35ee7b3832e272a002 100644 (file)
@@ -1290,7 +1290,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
       LexUnexpandedToken(Tok);
       std::string WarningName;
       SourceLocation StrStartLoc = Tok.getLocation();
-      if (!FinishLexStringLiteral(Tok, WarningName, /*MacroExpansion=*/false)) {
+      if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'",
+                                  /*MacroExpansion=*/false)) {
         // Eat tokens until ')'.
         while (Tok.isNot(tok::r_paren)
                  && Tok.isNot(tok::eod)
index 0c1c9dbee51bf5944cab09617ae5754c0f544b56..783588e1f7f79b41dd030687d94f8b5b7b716e8f 100644 (file)
@@ -503,6 +503,7 @@ void Preprocessor::HandlePragmaComment(Token &Tok) {
   Lex(Tok);
   std::string ArgumentString;
   if (Tok.is(tok::comma) && !LexStringLiteral(Tok, ArgumentString,
+                                              "pragma comment",
                                               /*MacroExpansion=*/true))
     return;
 
@@ -559,7 +560,8 @@ void Preprocessor::HandlePragmaMessage(Token &Tok) {
   }
 
   std::string MessageString;
-  if (!FinishLexStringLiteral(Tok, MessageString, /*MacroExpansion=*/true))
+  if (!FinishLexStringLiteral(Tok, MessageString, "pragma message",
+                              /*MacroExpansion=*/true))
     return;
 
   if (ExpectClosingParen) {
@@ -1039,7 +1041,8 @@ public:
     SourceLocation StringLoc = Tok.getLocation();
 
     std::string WarningName;
-    if (!PP.FinishLexStringLiteral(Tok, WarningName, /*MacroExpansion=*/false))
+    if (!PP.FinishLexStringLiteral(Tok, WarningName, "pragma diagnostic",
+                                   /*MacroExpansion=*/false))
       return;
 
     if (Tok.isNot(tok::eod)) {
index 52d6bb6a83bb2ad0ed541d5260db4a88d755022b..94885841057bd680a650e85889c5a7da6814df89 100644 (file)
@@ -691,10 +691,12 @@ void Preprocessor::LexAfterModuleImport(Token &Result) {
 }
 
 bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String,
+                                          const char *DiagnosticTag,
                                           bool AllowMacroExpansion) {
   // We need at least one string literal.
   if (Result.isNot(tok::string_literal)) {
-    Diag(Result, diag::err_expected_string_literal);
+    Diag(Result, diag::err_expected_string_literal)
+      << /*Source='in...'*/0 << DiagnosticTag;
     return false;
   }
 
@@ -720,7 +722,8 @@ bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String,
     return false;
 
   if (Literal.Pascal) {
-    Diag(StrToks[0].getLocation(), diag::err_expected_string_literal);
+    Diag(StrToks[0].getLocation(), diag::err_expected_string_literal)
+      << /*Source='in...'*/0 << DiagnosticTag;
     return false;
   }
 
index d898447ab5c0f5a0edec08c0280892ee5add2c68..7c5b1531407ca8fec5175839c9f8757f6c83047e 100644 (file)
@@ -735,7 +735,8 @@ void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
     ConsumeToken();
     if (Keyword == Ident_message) {
       if (!isTokenStringLiteral()) {
-        Diag(Tok, diag::err_expected_string_literal);
+        Diag(Tok, diag::err_expected_string_literal)
+          << /*Source='availability attribute'*/2;
         SkipUntil(tok::r_paren);
         return;
       }
index 0d785af3efdd64564b3372e3f214daccf331bd3b..b8ebd9b6a7e538273a3abeca74504f6de9b32956 100644 (file)
@@ -637,7 +637,8 @@ Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd){
     return 0;
 
   if (!isTokenStringLiteral()) {
-    Diag(Tok, diag::err_expected_string_literal);
+    Diag(Tok, diag::err_expected_string_literal)
+      << /*Source='static_assert'*/1;
     SkipMalformedDecl();
     return 0;
   }
index f4cdd619cef4e584bb2d5f5ef318ac35ad352d61..dfffe8150f2f9c1aa6ca7ec0b5496f24e5228efc 100644 (file)
@@ -1263,7 +1263,8 @@ Parser::ExprResult Parser::ParseAsmStringLiteral() {
       return ExprError();
     }
     default:
-      Diag(Tok, diag::err_expected_string_literal);
+      Diag(Tok, diag::err_expected_string_literal)
+        << /*Source='in...'*/0 << "'asm'";
       return ExprError();
   }
 
index 807edda66a0c837e99db953e042263f83dcb50cf..b67886fa3302fc1155696e566cfe717ab33b4c79 100644 (file)
@@ -12,3 +12,5 @@
 #define STRING(x) STRING2(x)
 #pragma message(":O I'm a message! " STRING(__LINE__)) // expected-warning {{:O I'm a message! 13}}
 #pragma message ":O gcc accepts this! " STRING(__LINE__) // expected-warning {{:O gcc accepts this! 14}}
+
+#pragma message(invalid) // expected-error {{expected string literal in pragma message}}
index b9ff31c811c5630655abe9abc7a2bb0a717ef67f..0ed8391775310cda25579f6a4a7ca14789dae2ce 100644 (file)
@@ -20,7 +20,7 @@ void f6() __attribute__((availability(macosx,unavailable,introduced=10.5))); //
 
 // rdar://10095131
 enum E{
-    gorf __attribute__((availability(macosx,introduced=8.5, message = 10.0))), // expected-error {{expected string literal}}
+    gorf __attribute__((availability(macosx,introduced=8.5, message = 10.0))), // expected-error {{expected string literal for optional message in 'availability' attribute}}
     garf __attribute__((availability(macosx,introduced=8.5, message))), // expected-error {{expected '=' after 'message'}}
 
     foo __attribute__((availability(macosx,introduced=8.5,deprecated=9.0, message="Use CTFontCopyPostScriptName()", deprecated=10.0))) // expected-error {{expected ')'}} \
index 5c91079619e621dc84a5fe51cfba538134ee5dbf..e8a67abb79eb15f0ac2f8608330ec56fd77462cd 100644 (file)
@@ -23,7 +23,7 @@
 #define foo error
 #pragma GCC diagnostic foo "-Wundef"  // expected-warning {{pragma diagnostic expected 'error', 'warning', 'ignored', 'fatal', 'push', or 'pop'}}
 
-#pragma GCC diagnostic error 42  // expected-error {{expected string literal}}
+#pragma GCC diagnostic error 42  // expected-error {{expected string literal in pragma diagnostic}}
 
 #pragma GCC diagnostic error "-Wundef" 42  // expected-warning {{unexpected token in pragma diagnostic}}
 #pragma GCC diagnostic error "invalid-name"  // expected-warning {{pragma diagnostic expected option name (e.g. "-Wundef")}}
index 782f986f7e63166782cc2ee3e66d8689ddf974d6..156d05243b17211da0ea2ea106a9fc58785da210 100644 (file)
@@ -11,7 +11,7 @@
 #pragma comment( user, "Compiled on " __DATE__ " at " __TIME__ ) 
 
 #pragma comment(foo)    // expected-error {{unknown kind of pragma comment}}
-#pragma comment(compiler,)     // expected-error {{expected string literal}}
+#pragma comment(compiler,)     // expected-error {{expected string literal in pragma comment}}
 #define foo compiler
 #pragma comment(foo)   // macro expand kind.
 #pragma comment(foo) x // expected-error {{pragma comment requires}}
index 77b84562694eb8db3d567bc8a6533168a9a9f546..c0c22ef2d71fd00e091cf43ba5639a22c16088a3 100644 (file)
@@ -11,7 +11,7 @@
 #warning Should have -Wparentheses
 #endif
 
-// expected-error@+2 {{expected string literal}}
+// expected-error@+2 {{expected string literal in '__has_warning'}}
 // expected-error@+1 {{expected value in expression}}
 #if __has_warning(-Wfoo)
 #endif
index 13d70708582b98eaa8422f8c4add28eac6b22676..9309987431b97f8fba05e7a06b68761eb1d6aa2d 100644 (file)
@@ -9,3 +9,5 @@ void foo(void) {
   _Static_assert(1, "1 is nonzero");
   _Static_assert(0, "0 is nonzero"); // expected-error {{static_assert failed "0 is nonzero"}}
 }
+
+_Static_assert(1, invalid); // expected-error {{expected string literal for diagnostic message in static_assert}}