15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the Diagnostic-related interfaces.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
143f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose#include "clang/Basic/CharInfo.h"
15ec55c941f2846db48bce4ed6dd2ce339e1a48962Ted Kremenek#include "clang/Basic/Diagnostic.h"
1602c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor#include "clang/Basic/DiagnosticOptions.h"
1743b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner#include "clang/Basic/IdentifierTable.h"
18ec55c941f2846db48bce4ed6dd2ce339e1a48962Ted Kremenek#include "clang/Basic/PartialDiagnostic.h"
198fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer#include "llvm/ADT/SmallString.h"
20615a092a511cd2dfe1a5364ebf5f80e55e33034dJordan Rose#include "llvm/ADT/StringExtras.h"
2103201fbbdeb3eb7f465610b09c281ee6aa84e3caTed Kremenek#include "llvm/Support/CrashRecoveryContext.h"
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h"
2303201fbbdeb3eb7f465610b09c281ee6aa84e3caTed Kremenek
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikiestatic void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
27ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                            StringRef Modifier, StringRef Argument,
28ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                            ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
29ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                            SmallVectorImpl<char> &Output,
30ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                            void *Cookie,
31ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                            ArrayRef<intptr_t> QualTypeVals) {
32ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  StringRef Str = "<can't format argument>";
33ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  Output.append(Str.begin(), Str.end());
3422caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner}
3522caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner
3622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner
37d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine::DiagnosticsEngine(
38c93dc7889644293e318e19d82830ea2acc45b678Dylan Noblesmith                       const IntrusiveRefCntPtr<DiagnosticIDs> &diags,
3902c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor                       DiagnosticOptions *DiagOpts,
4078ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie                       DiagnosticConsumer *client, bool ShouldOwnClient)
4102c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor  : Diags(diags), DiagOpts(DiagOpts), Client(client),
426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    OwnsDiagClient(ShouldOwnClient), SourceMgr(nullptr) {
433fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  ArgToStringFn = DummyArgToStringFn;
446bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ArgToStringCookie = nullptr;
451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  AllExtensionsSilenced = 0;
47cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  IgnoreAllWarnings = false;
48cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  WarningsAsErrors = false;
491e473ccb0e0f6fd1954bef330f7193c1a3fb3ba1Ted Kremenek  EnableAllWarnings = false;
50cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ErrorsAsFatal = false;
51cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  SuppressSystemWarnings = false;
52cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  SuppressAllDiagnostics = false;
53246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  ElideType = true;
54246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  PrintTemplateTree = false;
55246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  ShowColors = false;
56cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ShowOverloads = Ovl_All;
57ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  ExtBehavior = diag::Severity::Ignored;
58cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
59cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ErrorLimit = 0;
60cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  TemplateBacktraceLimit = 0;
6108d6e032a2a0a8656d12b3b7b93942987bb12eb7Richard Smith  ConstexprBacktraceLimit = 0;
62cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
63abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  Reset();
64182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner}
65182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner
66d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine::~DiagnosticsEngine() {
6733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (OwnsDiagClient)
6833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    delete Client;
69182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner}
70182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner
7178ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikievoid DiagnosticsEngine::setClient(DiagnosticConsumer *client,
72d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie                                  bool ShouldOwnClient) {
734f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor  if (OwnsDiagClient && Client)
744f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor    delete Client;
754f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor
764f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor  Client = client;
774f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor  OwnsDiagClient = ShouldOwnClient;
784f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor}
7904ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
80d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikievoid DiagnosticsEngine::pushMappings(SourceLocation Loc) {
810827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStateOnPushStack.push_back(GetCurDiagState());
8204ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner}
8304ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
84d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikiebool DiagnosticsEngine::popMappings(SourceLocation Loc) {
850827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (DiagStateOnPushStack.empty())
8604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner    return false;
8704ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
880827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (DiagStateOnPushStack.back() != GetCurDiagState()) {
890827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // State changed at some point between push/pop.
900827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
910827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStateOnPushStack.pop_back();
9304ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  return true;
9404ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner}
9504ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
96d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikievoid DiagnosticsEngine::Reset() {
97abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ErrorOccurred = false;
9812f37e411462b8388eb1309357bd62257debacacDeLesley Hutchins  UncompilableErrorOccurred = false;
99abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  FatalErrorOccurred = false;
10085bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor  UnrecoverableErrorOccurred = false;
101abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
102abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumWarnings = 0;
103abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumErrors = 0;
104abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumErrorsSuppressed = 0;
105c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  TrapNumErrorsOccurred = 0;
106c0a575f9b791a25c94b1c3c832dd73ec564646bbArgyrios Kyrtzidis  TrapNumUnrecoverableErrorsOccurred = 0;
10785bea9777d444ccbcc086d98f075fe666c2e865dDouglas Gregor
108abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  CurDiagID = ~0U;
1095b9268f26ac53a74d2c504279fe577d988d5615dRichard Smith  LastDiagLevel = DiagnosticIDs::Ignored;
110abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  DelayedDiagID = 0;
111dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis
112dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  // Clear state related to #pragma diagnostic.
113dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  DiagStates.clear();
114dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  DiagStatePoints.clear();
115dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  DiagStateOnPushStack.clear();
116dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis
117dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  // Create a DiagState and DiagStatePoint representing diagnostic changes
118dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  // through command-line.
119dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  DiagStates.push_back(DiagState());
12000aae5243d965aa7bcee81a39ba0900c7869be21Richard Smith  DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
121abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor}
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
123d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikievoid DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
124dfaee4922b990fbf33808b91b961dc27df585030Chad Rosier                                             StringRef Arg2) {
12593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  if (DelayedDiagID)
12693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor    return;
12793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
12893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagID = DiagID;
1299e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  DelayedDiagArg1 = Arg1.str();
1309e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  DelayedDiagArg2 = Arg2.str();
13193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
13293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
133d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikievoid DiagnosticsEngine::ReportDelayed() {
13493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
13593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagID = 0;
13693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagArg1.clear();
13793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagArg2.clear();
13893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
13993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
140d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine::DiagStatePointsTy::iterator
141d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
1420827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(!DiagStatePoints.empty());
1430827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(DiagStatePoints.front().Loc.isInvalid() &&
1440827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Should have created a DiagStatePoint for command-line");
1450827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1469e63dc582b3bfb9ab2e80631b5311b070a73dff8Richard Smith  if (!SourceMgr)
1479e63dc582b3bfb9ab2e80631b5311b070a73dff8Richard Smith    return DiagStatePoints.end() - 1;
1489e63dc582b3bfb9ab2e80631b5311b070a73dff8Richard Smith
1490827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc Loc(L, *SourceMgr);
1500827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Loc.isInvalid())
1510827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return DiagStatePoints.end() - 1;
1520827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1530827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
1540827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
1550827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (LastStateChangePos.isValid() &&
1560827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
1570827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
1586bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                           DiagStatePoint(nullptr, Loc));
1590827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  --Pos;
1600827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  return Pos;
1610827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis}
1620827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
163ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesvoid DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
164ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                    SourceLocation L) {
1650827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(Diag < diag::DIAG_UPPER_LIMIT &&
1660827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Can only map builtin diagnostics");
1670827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert((Diags->isBuiltinWarningOrExtension(Diag) ||
168ef8225444452a1486bd721f3285301fe84643b00Stephen Hines          (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
1690827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Cannot map errors into warnings!");
1700827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(!DiagStatePoints.empty());
171c95ad0061f093de2f73b8aab58ff3f0444d0123fRichard Smith  assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
1720827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
173c95ad0061f093de2f73b8aab58ff3f0444d0123fRichard Smith  FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
1740827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
1757a0a31ce0cd38147bfe853f71a3f7261444ddf4cChad Rosier  // Don't allow a mapping to a warning override an error/fatal mapping.
176ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  if (Map == diag::Severity::Warning) {
177ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
178ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    if (Info.getSeverity() == diag::Severity::Error ||
179ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        Info.getSeverity() == diag::Severity::Fatal)
180ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      Map = Info.getSeverity();
1817a0a31ce0cd38147bfe853f71a3f7261444ddf4cChad Rosier  }
182ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  DiagnosticMapping Mapping = makeUserMapping(Map, L);
18353201a86f775c064b62ec25742106c7b43ba8749Daniel Dunbar
1840827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Common case; setting all the diagnostics of a group in one place.
1850827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Loc.isInvalid() || Loc == LastStateChangePos) {
186ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    GetCurDiagState()->setMapping(Diag, Mapping);
1870827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
1880827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
1890827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1900827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Another common case; modifying diagnostic state in a source location
1910827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // after the previous one.
1920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
1930827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
194fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // A diagnostic pragma occurred, create a new DiagState initialized with
1950827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // the current one and a new DiagStatePoint to record at which location
1960827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // the new state became active.
1970827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagStates.push_back(*GetCurDiagState());
1980827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    PushDiagStatePoint(&DiagStates.back(), Loc);
199ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    GetCurDiagState()->setMapping(Diag, Mapping);
2000827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
2010827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2020827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2030827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // We allow setting the diagnostic state in random source order for
2040827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // completeness but it should not be actually happening in normal practice.
2050827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2060827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
2070827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(Pos != DiagStatePoints.end());
2080827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2090827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Update all diagnostic states that are active after the given location.
2100827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  for (DiagStatePointsTy::iterator
2110827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
212ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    GetCurDiagState()->setMapping(Diag, Mapping);
2130827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2140827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2150827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // If the location corresponds to an existing point, just update its state.
2160827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Pos->Loc == Loc) {
217ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    GetCurDiagState()->setMapping(Diag, Mapping);
2180827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
2190827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2200827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2210827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Create a new state/point and fit it into the vector of DiagStatePoints
2220827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // so that the vector is always ordered according to location.
223651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc));
2240827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStates.push_back(*Pos->State);
2250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagState *NewState = &DiagStates.back();
226ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  GetCurDiagState()->setMapping(Diag, Mapping);
2270827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
2280827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                               FullSourceLoc(Loc, *SourceMgr)));
2290827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis}
2300827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
231ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesbool DiagnosticsEngine::setSeverityForGroup(StringRef Group, diag::Severity Map,
232ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                            SourceLocation Loc) {
2333f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar  // Get the diagnostics in this group.
234cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<diag::kind, 8> GroupDiags;
2353f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar  if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
2363f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar    return true;
2373f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar
2383f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar  // Set the mapping.
2393f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar  for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i)
240ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    setSeverity(GroupDiags[i], Map, Loc);
2413f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar
2423f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar  return false;
2433f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar}
2443f8394669673451061f57ced81f0a2cae087f119Daniel Dunbar
2454aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbarbool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
2464aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar                                                         bool Enabled) {
247a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // If we are enabling this feature, just set the diagnostic mappings to map to
248a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // errors.
249a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  if (Enabled)
250ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    return setSeverityForGroup(Group, diag::Severity::Error);
251a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
252a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
253a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // potentially downgrade anything already mapped to be a warning.
254a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
255a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // Get the diagnostics in this group.
256cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<diag::kind, 8> GroupDiags;
257a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
258a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar    return true;
259a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
260a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // Perform the mapping change.
261a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
262ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);
263a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
264ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    if (Info.getSeverity() == diag::Severity::Error ||
265ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        Info.getSeverity() == diag::Severity::Fatal)
266ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      Info.setSeverity(diag::Severity::Warning);
267be1aa410274b28fc143c47c814f07c989f4534d6Daniel Dunbar
268a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar    Info.setNoWarningAsError(true);
269a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  }
270a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
271a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  return false;
2724aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar}
2734aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar
2744aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbarbool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
2754aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar                                                       bool Enabled) {
276a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // If we are enabling this feature, just set the diagnostic mappings to map to
277a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // fatal errors.
278a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  if (Enabled)
279ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    return setSeverityForGroup(Group, diag::Severity::Fatal);
280a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
281a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
282a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // potentially downgrade anything already mapped to be an error.
283a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
284a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // Get the diagnostics in this group.
285cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<diag::kind, 8> GroupDiags;
286a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
287a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar    return true;
288a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
289a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  // Perform the mapping change.
290a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
291ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);
292a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
293ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    if (Info.getSeverity() == diag::Severity::Fatal)
294ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      Info.setSeverity(diag::Severity::Error);
295be1aa410274b28fc143c47c814f07c989f4534d6Daniel Dunbar
296a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar    Info.setNoErrorAsFatal(true);
297a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  }
298a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar
299a5e41333b06fdf036d58495490eb14d6a0a3fcf7Daniel Dunbar  return false;
3004aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar}
3014aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar
302ef8225444452a1486bd721f3285301fe84643b00Stephen Hinesvoid DiagnosticsEngine::setSeverityForAll(diag::Severity Map,
303ef8225444452a1486bd721f3285301fe84643b00Stephen Hines                                          SourceLocation Loc) {
30411583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis  // Get all the diagnostics.
305cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<diag::kind, 64> AllDiags;
30611583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis  Diags->getAllDiagnostics(AllDiags);
30711583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis
30811583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis  // Set the mapping.
30911583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis  for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
31011583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis    if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
311ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      setSeverity(AllDiags[i], Map, Loc);
31211583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis}
31311583c757bac6ce5c342f2eb572055dd2619a657Argyrios Kyrtzidis
314d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikievoid DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
315e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
316e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
317e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  CurDiagLoc = storedDiag.getLocation();
318e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  CurDiagID = storedDiag.getID();
319e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  NumDiagArgs = 0;
320e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
3216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DiagRanges.clear();
3226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DiagRanges.reserve(storedDiag.range_size());
323e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  for (StoredDiagnostic::range_iterator
324e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis         RI = storedDiag.range_begin(),
325e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis         RE = storedDiag.range_end(); RI != RE; ++RI)
3266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    DiagRanges.push_back(*RI);
327e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
3286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DiagFixItHints.clear();
3296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  DiagFixItHints.reserve(storedDiag.fixit_size());
330e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  for (StoredDiagnostic::fixit_iterator
331e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis         FI = storedDiag.fixit_begin(),
332e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis         FE = storedDiag.fixit_end(); FI != FE; ++FI)
3336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    DiagFixItHints.push_back(*FI);
334e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
33578ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikie  assert(Client && "DiagnosticConsumer not set!");
336e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  Level DiagLevel = storedDiag.getLevel();
33740847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie  Diagnostic Info(this, storedDiag.getMessage());
338e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  Client->HandleDiagnostic(DiagLevel, Info);
339e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  if (Client->IncludeInDiagnosticCounts()) {
340d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    if (DiagLevel == DiagnosticsEngine::Warning)
341e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis      ++NumWarnings;
342e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  }
343e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
344e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  CurDiagID = ~0U;
345e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis}
346e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
347c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rosebool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
348c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  assert(getClient() && "DiagnosticClient not set!");
349c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose
350c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  bool Emitted;
351c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  if (Force) {
352c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    Diagnostic Info(this);
353c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose
354c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    // Figure out the diagnostic level of this message.
355c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    DiagnosticIDs::Level DiagLevel
356c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose      = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
357c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose
358c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    Emitted = (DiagLevel != DiagnosticIDs::Ignored);
359c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    if (Emitted) {
360c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose      // Emit the diagnostic regardless of suppression level.
361c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose      Diags->EmitDiag(*this, DiagLevel);
362c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    }
363c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  } else {
364c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    // Process the diagnostic, sending the accumulated information to the
365c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    // DiagnosticConsumer.
366c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose    Emitted = ProcessDiag();
367c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  }
36893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
36993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // Clear out the current diagnostic object.
3703054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar  unsigned DiagID = CurDiagID;
3713054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar  Clear();
37293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
37393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // If there was a delayed diagnostic, emit it now.
374c6d64a26c28bbeee50e06c94c4f4c08e610327b7Jordan Rose  if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
3753054f097f813d19090bdb23645dcd48df71d1a89Daniel Dunbar    ReportDelayed();
37693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
37793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  return Emitted;
37893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
37993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
3807bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
38178ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid BlaikieDiagnosticConsumer::~DiagnosticConsumer() {}
3827bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
38378ad0b98848c17a0a11847fa1d456e2dfec8aa2fDavid Blaikievoid DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
38440847cfb58acc3cac7d68727df9455ac45f2e118David Blaikie                                        const Diagnostic &Info) {
385f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  if (!IncludeInDiagnosticCounts())
386f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    return;
387f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
388d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  if (DiagLevel == DiagnosticsEngine::Warning)
389f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    ++NumWarnings;
390d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  else if (DiagLevel >= DiagnosticsEngine::Error)
391f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    ++NumErrors;
392f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis}
393f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
394af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// ModifierIs - Return true if the specified modifier matches specified string.
395af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnertemplate <std::size_t StrLen>
396af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnerstatic bool ModifierIs(const char *Modifier, unsigned ModifierLen,
397af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                       const char (&Str)[StrLen]) {
398af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
399af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
400af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
401909c182f6a0e6169475eef6a71add14555b6162cJohn McCall/// ScanForward - Scans forward, looking for the given character, skipping
402909c182f6a0e6169475eef6a71add14555b6162cJohn McCall/// nested clauses and escaped characters.
403909c182f6a0e6169475eef6a71add14555b6162cJohn McCallstatic const char *ScanFormat(const char *I, const char *E, char Target) {
404909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  unsigned Depth = 0;
405909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
406909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  for ( ; I != E; ++I) {
407909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (Depth == 0 && *I == Target) return I;
408909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (Depth != 0 && *I == '}') Depth--;
409909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
410909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (*I == '%') {
411909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      I++;
412909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      if (I == E) break;
413909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
414909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      // Escaped characters get implicitly skipped here.
415909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
416909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      // Format specifier.
4173f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose      if (!isDigit(*I) && !isPunctuation(*I)) {
4183f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose        for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
419909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        if (I == E) break;
420909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        if (*I == '{')
421909c182f6a0e6169475eef6a71add14555b6162cJohn McCall          Depth++;
422909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      }
423909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    }
424909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  }
425909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  return E;
426909c182f6a0e6169475eef6a71add14555b6162cJohn McCall}
427909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
428af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// HandleSelectModifier - Handle the integer 'select' modifier.  This is used
429af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// like this:  %select{foo|bar|baz}2.  This means that the integer argument
430af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// "%2" has a value from 0-2.  If the value is 0, the diagnostic prints 'foo'.
431af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// If the value is 1, it prints 'bar'.  If it has the value 2, it prints 'baz'.
432af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// This is very useful for certain classes of variant diagnostics.
43340847cfb58acc3cac7d68727df9455ac45f2e118David Blaikiestatic void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
434af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                                 const char *Argument, unsigned ArgumentLen,
4355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 SmallVectorImpl<char> &OutStr) {
436af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  const char *ArgumentEnd = Argument+ArgumentLen;
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
438af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  // Skip over 'ValNo' |'s.
439af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  while (ValNo) {
440909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
441af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
442af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner           " larger than the number of options in the diagnostic string!");
443af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    Argument = NextVal+1;  // Skip this string.
444af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    --ValNo;
445af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  }
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
447af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  // Get the end of the value.  This is either the } or the |.
448909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
4499f28614bf1a8387000d8df57a713fcf69e198145John McCall
4509f28614bf1a8387000d8df57a713fcf69e198145John McCall  // Recursively format the result of the select clause into the output string.
4519f28614bf1a8387000d8df57a713fcf69e198145John McCall  DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
452af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
453af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
454af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// HandleIntegerSModifier - Handle the integer 's' modifier.  This adds the
455af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// letter 's' to the string if the value is not 1.  This is used in cases like
456af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// this:  "you idiot, you have %4 parameter%s4!".
457af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnerstatic void HandleIntegerSModifier(unsigned ValNo,
4585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                   SmallVectorImpl<char> &OutStr) {
459af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  if (ValNo != 1)
460af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    OutStr.push_back('s');
461af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
462af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
4633be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// HandleOrdinalModifier - Handle the integer 'ord' modifier.  This
4643be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// prints the ordinal form of the given integer, with 1 corresponding
4653be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// to the first ordinal.  Currently this is hard-coded to use the
4663be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// English form.
4673be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCallstatic void HandleOrdinalModifier(unsigned ValNo,
4685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                  SmallVectorImpl<char> &OutStr) {
4693be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  assert(ValNo != 0 && "ValNo must be strictly positive!");
4703be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
4713be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  llvm::raw_svector_ostream Out(OutStr);
4723be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
4733be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // We could use text forms for the first N ordinals, but the numeric
4743be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // forms are actually nicer in diagnostics because they stand out.
475615a092a511cd2dfe1a5364ebf5f80e55e33034dJordan Rose  Out << ValNo << llvm::getOrdinalSuffix(ValNo);
4763be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall}
4773be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
478af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
479e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// PluralNumber - Parse an unsigned integer and advance Start.
480d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic unsigned PluralNumber(const char *&Start, const char *End) {
481e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  // Programming 101: Parse a decimal number :-)
482e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned Val = 0;
483e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (Start != End && *Start >= '0' && *Start <= '9') {
484e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Val *= 10;
485e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Val += *Start - '0';
486e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    ++Start;
487e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
488e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return Val;
489e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
490e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
491e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
492d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
493e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  if (*Start != '[') {
494e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    unsigned Ref = PluralNumber(Start, End);
495e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    return Ref == Val;
496e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
497e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
498e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
499e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned Low = PluralNumber(Start, End);
500e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  assert(*Start == ',' && "Bad plural expression syntax: expected ,");
501e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
502e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned High = PluralNumber(Start, End);
503e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  assert(*Start == ']' && "Bad plural expression syntax: expected )");
504e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
505e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return Low <= Val && Val <= High;
506e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
507e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
508e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
509d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
510e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  // Empty condition?
511e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  if (*Start == ':')
512e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    return true;
513e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
514e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (1) {
515e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    char C = *Start;
516e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    if (C == '%') {
517e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      // Modulo expression
518e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++Start;
519e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      unsigned Arg = PluralNumber(Start, End);
520e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      assert(*Start == '=' && "Bad plural expression syntax: expected =");
521e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++Start;
522e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      unsigned ValMod = ValNo % Arg;
523e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      if (TestPluralRange(ValMod, Start, End))
524e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl        return true;
525e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    } else {
526e20653219732b03294130999415fc3aa92d2336aSebastian Redl      assert((C == '[' || (C >= '0' && C <= '9')) &&
527e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl             "Bad plural expression syntax: unexpected character");
528e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      // Range expression
529e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      if (TestPluralRange(ValNo, Start, End))
530e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl        return true;
531e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
532e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
533e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    // Scan for next or-expr part.
534e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Start = std::find(Start, End, ',');
5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (Start == End)
536e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      break;
537e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    ++Start;
538e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
539e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return false;
540e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
541e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
542e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
543e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// for complex plural forms, or in languages where all plurals are complex.
544e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
545e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// conditions that are tested in order, the form corresponding to the first
546e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// that applies being emitted. The empty condition is always true, making the
547e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// last form a default case.
548e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Conditions are simple boolean expressions, where n is the number argument.
549e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Here are the rules.
550e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// condition  := expression | empty
551e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// empty      :=                             -> always true
552e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// expression := numeric [',' expression]    -> logical or
553e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// numeric    := range                       -> true if n in range
554e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///             | '%' number '=' range        -> true if n % number in range
555e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// range      := number
556e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///             | '[' number ',' number ']'   -> ranges are inclusive both ends
557e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///
558e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Here are some examples from the GNU gettext manual written in this form:
559e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// English:
560e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|:form1}
561e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Latvian:
562e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
563e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Gaeilge:
564e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|2:form1|:form2}
565e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Romanian:
566e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|0,%100=[1,19]:form1|:form2}
567e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Lithuanian:
568e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
569e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Russian (requires repeated form):
570e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
571e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Slovak
572e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|[2,4]:form1|:form2}
573e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Polish (requires repeated form):
574e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
57540847cfb58acc3cac7d68727df9455ac45f2e118David Blaikiestatic void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
576e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl                                 const char *Argument, unsigned ArgumentLen,
5775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                                 SmallVectorImpl<char> &OutStr) {
578e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  const char *ArgumentEnd = Argument + ArgumentLen;
579e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (1) {
580e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    assert(Argument < ArgumentEnd && "Plural expression didn't match.");
581e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    const char *ExprEnd = Argument;
582e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    while (*ExprEnd != ':') {
583e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
584e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++ExprEnd;
585e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
586e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
587e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      Argument = ExprEnd + 1;
588909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
589e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall
590e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      // Recursively format the result of the plural clause into the
591e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      // output string.
592e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
593e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      return;
594e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
595909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
596e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
597e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
598e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
599651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// \brief Returns the friendly description for a token kind that will appear
600651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// without quotes in diagnostic messages. These strings may be translatable in
601651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines/// future.
602651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesstatic const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
603651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  switch (Kind) {
604651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  case tok::identifier:
605651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    return "identifier";
606651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  default:
6076bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
608651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
609651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines}
610e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
611f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// FormatDiagnostic - Format this diagnostic into a string, substituting the
612f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// formal arguments into the %0 slots.  The result is appended onto the Str
613f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// array.
61440847cfb58acc3cac7d68727df9455ac45f2e118David Blaikievoid Diagnostic::
6155f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerFormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
616e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  if (!StoredDiagMessage.empty()) {
617e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis    OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
618e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis    return;
619e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis  }
620e59abb56ce0e1c206fb80bd945a0c358b0abe1efArgyrios Kyrtzidis
6215f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef Diag =
622477aab6782795e7472055a54108d2df270ce1a89Argyrios Kyrtzidis    getDiags()->getDiagnosticIDs()->getDescription(getID());
6231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
624477aab6782795e7472055a54108d2df270ce1a89Argyrios Kyrtzidis  FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
6259f28614bf1a8387000d8df57a713fcf69e198145John McCall}
6269f28614bf1a8387000d8df57a713fcf69e198145John McCall
62740847cfb58acc3cac7d68727df9455ac45f2e118David Blaikievoid Diagnostic::
6289f28614bf1a8387000d8df57a713fcf69e198145John McCallFormatDiagnostic(const char *DiagStr, const char *DiagEnd,
6295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                 SmallVectorImpl<char> &OutStr) const {
6309f28614bf1a8387000d8df57a713fcf69e198145John McCall
631b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// FormattedArgs - Keep track of all of the arguments formatted by
632b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// ConvertArgToString and pass them into subsequent calls to
633b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// ConvertArgToString, allowing the implementation to avoid redundancies in
634b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// obvious cases.
635d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
6360673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth
6370673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth  /// QualTypeVals - Pass a vector of arrays so that QualType names can be
6380673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth  /// compared to see if more information is needed to be printed.
6395f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<intptr_t, 2> QualTypeVals;
640246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu  SmallVector<char, 64> Tree;
641246b6aa6763de8c617d564ef33123a8f3293a80eRichard Trieu
6420673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth  for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
643d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie    if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
6440673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth      QualTypeVals.push_back(getRawArg(i));
6450673cb30340aadaede7b795c763b00f6b64e611cChandler Carruth
646f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  while (DiagStr != DiagEnd) {
647f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner    if (DiagStr[0] != '%') {
648f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      // Append non-%0 substrings to Str if we have one.
649f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
650f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      OutStr.append(DiagStr, StrEnd);
651f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      DiagStr = StrEnd;
652af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      continue;
6533f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose    } else if (isPunctuation(DiagStr[1])) {
654909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      OutStr.push_back(DiagStr[1]);  // %% -> %.
655f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      DiagStr += 2;
656af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      continue;
657af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
659af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // Skip the %.
660af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    ++DiagStr;
6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
662af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // This must be a placeholder for a diagnostic argument.  The format for a
663af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
664af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // The digit is a number from 0-9 indicating which argument this comes from.
665af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // The modifier is a string of digits from the set [-a-z]+, arguments is a
666af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // brace enclosed string.
6676bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    const char *Modifier = nullptr, *Argument = nullptr;
668af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    unsigned ModifierLen = 0, ArgumentLen = 0;
669