summaryrefslogtreecommitdiffstats
path: root/clang/lib/Basic/Diagnostic.cpp
diff options
context:
space:
mode:
authorAlp Toker <alp@nuanti.com>2013-12-24 09:48:30 +0000
committerAlp Toker <alp@nuanti.com>2013-12-24 09:48:30 +0000
commitec543279db99283f711c92c37f37b05a243dcf4d (patch)
tree03698109ecc933f59566d8c95a3f2f68b96046b9 /clang/lib/Basic/Diagnostic.cpp
parent090a7cd76df5805b13a34d1b6b4ee8dae084b528 (diff)
downloadbcm5719-llvm-ec543279db99283f711c92c37f37b05a243dcf4d.tar.gz
bcm5719-llvm-ec543279db99283f711c92c37f37b05a243dcf4d.zip
Support and use token kinds as diagnostic arguments
Introduce proper facilities to render token spellings using the diagnostic formatter. Replaces most of the hard-coded diagnostic messages related to expected tokens, which all shared the same semantics but had to be multiply defined due to variations in token order or quote marks. The associated parser changes are largely mechanical but they expose commonality in whole chunks of the parser that can now be factored away. This commit uses C++11 typed enums along with a speculative legacy fallback until the transition is complete. Requires corresponding changes in LLVM r197895. llvm-svn: 197972
Diffstat (limited to 'clang/lib/Basic/Diagnostic.cpp')
-rw-r--r--clang/lib/Basic/Diagnostic.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp
index 45d4b539e8c..4a6f070af70 100644
--- a/clang/lib/Basic/Diagnostic.cpp
+++ b/clang/lib/Basic/Diagnostic.cpp
@@ -639,6 +639,16 @@ 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) {
+ switch (Kind) {
+ case tok::identifier:
+ return "identifier";
+ default:
+ return 0;
+ }
+}
/// FormatDiagnostic - Format this diagnostic into a string, substituting the
/// formal arguments into the %0 slots. The result is appended onto the Str
@@ -812,6 +822,25 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
}
break;
}
+ // ---- TOKEN SPELLINGS ----
+ case DiagnosticsEngine::ak_tokenkind: {
+ tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
+ assert(ModifierLen == 0 && "No modifiers for token kinds yet");
+
+ llvm::raw_svector_ostream Out(OutStr);
+ if (const char *S = getTokenNameForDiagnostic(Kind))
+ // Unquoted translatable token name.
+ Out << S;
+ else if (const char *S = tok::getTokenSimpleSpelling(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 << '>';
+ else
+ Out << "(null)";
+ break;
+ }
// ---- NAMES and TYPES ----
case DiagnosticsEngine::ak_identifierinfo: {
const IdentifierInfo *II = getArgIdentifier(ArgNo);
OpenPOWER on IntegriCloud