]> granicus.if.org Git - clang/commitdiff
Support diagnostic formatting of keyword tokens
authorAlp Toker <alp@nuanti.com>
Mon, 6 Jan 2014 12:54:18 +0000 (12:54 +0000)
committerAlp Toker <alp@nuanti.com>
Mon, 6 Jan 2014 12:54:18 +0000 (12:54 +0000)
Implemented with a new getKeywordSpelling() accessor. Unlike getTokenName() the
result of this function is stable and may be used in diagnostic output.

Uses of this feature are split out into the subsequent commit.

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

include/clang/Basic/TokenKinds.h
lib/Basic/Diagnostic.cpp
lib/Basic/TokenKinds.cpp

index e77e732f7036f48427f0c72bc8a2eea1cd8d8fb4..f7a5f9eef92875d5bc49fcabd12ef3c275f89ea1 100644 (file)
@@ -65,6 +65,10 @@ const char *getTokenName(enum TokenKind Kind) LLVM_READNONE;
 /// Preprocessor::getSpelling().
 const char *getPunctuatorSpelling(enum TokenKind Kind) LLVM_READNONE;
 
+/// \brief Determines the spelling of simple keyword and contextual keyword
+/// tokens like 'int' and 'dynamic_cast'. Returns NULL for other token kinds.
+const char *getKeywordSpelling(enum TokenKind Kind) LLVM_READNONE;
+
 /// \brief Return true if this is a raw identifier or an identifier kind.
 inline bool isAnyIdentifier(TokenKind K) {
   return (K == tok::identifier) || (K == tok::raw_identifier);
index 9d7643b3622a6ea6f6a30598b3744a5f797e6e4c..a5242c94521bc62e5bd9a1e3639219bf8bad5bce 100644 (file)
@@ -639,12 +639,15 @@ static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
   }
 }
 
-/// \brief Returns the friendly name for a token kind that will / appear
-// without quotes in diagnostic messages.
-static const char *getTokenNameForDiagnostic(tok::TokenKind Kind) {
+/// \brief Returns the friendly description for a token kind that will appear
+/// without quotes in diagnostic messages. These strings may be translatable in
+/// future.
+static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
   switch (Kind) {
   case tok::identifier:
     return "identifier";
+  case tok::annot_template_id:
+    return "template name";
   default:
     return 0;
   }
@@ -828,12 +831,15 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
       assert(ModifierLen == 0 && "No modifiers for token kinds yet");
 
       llvm::raw_svector_ostream Out(OutStr);
-      if (const char *S = getTokenNameForDiagnostic(Kind))
+      if (const char *S = tok::getPunctuatorSpelling(Kind))
+        // Quoted token spelling for punctuators.
+        Out << '\'' << S << '\'';
+      else if (const char *S = tok::getKeywordSpelling(Kind))
+        // Unquoted token spelling for keywords.
+        Out << S;
+      else if (const char *S = getTokenDescForDiagnostic(Kind))
         // Unquoted translatable token name.
         Out << S;
-      else if (const char *S = tok::getPunctuatorSpelling(Kind))
-        // Quoted token spelling, currently only covers punctuators.
-        Out << '\'' << S << '\'';
       else if (const char *S = tok::getTokenName(Kind))
         // Debug name, shouldn't appear in user-facing diagnostics.
         Out << '<' << S << '>';
index 2a9c9bfa2f97a98d727a781c3cc8d2f5549f248d..50fe0a688771a48cc4bb9973bae9d15a1dad3e29 100644 (file)
@@ -35,6 +35,14 @@ const char *tok::getPunctuatorSpelling(enum TokenKind Kind) {
 #include "clang/Basic/TokenKinds.def"
   default: break;
   }
+  return 0;
+}
 
+const char *tok::getKeywordSpelling(enum TokenKind Kind) {
+  switch (Kind) {
+#define KEYWORD(X,Y) case kw_ ## X: return #X;
+#include "clang/Basic/TokenKinds.def"
+    default: break;
+  }
   return 0;
 }