r198604 - Support diagnostic formatting of keyword tokens

Alp Toker alp at nuanti.com
Mon Jan 6 04:54:19 PST 2014


Author: alp
Date: Mon Jan  6 06:54:18 2014
New Revision: 198604

URL: http://llvm.org/viewvc/llvm-project?rev=198604&view=rev
Log:
Support diagnostic formatting of keyword tokens

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.

Modified:
    cfe/trunk/include/clang/Basic/TokenKinds.h
    cfe/trunk/lib/Basic/Diagnostic.cpp
    cfe/trunk/lib/Basic/TokenKinds.cpp

Modified: cfe/trunk/include/clang/Basic/TokenKinds.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/TokenKinds.h?rev=198604&r1=198603&r2=198604&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/TokenKinds.h (original)
+++ cfe/trunk/include/clang/Basic/TokenKinds.h Mon Jan  6 06:54:18 2014
@@ -65,6 +65,10 @@ const char *getTokenName(enum TokenKind
 /// 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);

Modified: cfe/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=198604&r1=198603&r2=198604&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Mon Jan  6 06:54:18 2014
@@ -639,12 +639,15 @@ static void HandlePluralModifier(const D
   }
 }
 
-/// \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, co
       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 << '>';

Modified: cfe/trunk/lib/Basic/TokenKinds.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/TokenKinds.cpp?rev=198604&r1=198603&r2=198604&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/TokenKinds.cpp (original)
+++ cfe/trunk/lib/Basic/TokenKinds.cpp Mon Jan  6 06:54:18 2014
@@ -35,6 +35,14 @@ const char *tok::getPunctuatorSpelling(e
 #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;
 }





More information about the cfe-commits mailing list