Diagnostic.cpp revision fc8f0e14ad142ed811e90fbd9a30e419e301c717
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
14ec55c941f2846db48bce4ed6dd2ce339e1a48962Ted Kremenek#include "clang/Basic/Diagnostic.h"
1543b628cd47ecdc3caf640d79b3ad7ecef0f2c285Chris Lattner#include "clang/Basic/IdentifierTable.h"
16ec55c941f2846db48bce4ed6dd2ce339e1a48962Ted Kremenek#include "clang/Basic/PartialDiagnostic.h"
17f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner#include "llvm/ADT/SmallVector.h"
1823e47c6b6e8ccdd8daa378ab2a879644425c72d8Daniel Dunbar#include "llvm/Support/raw_ostream.h"
1903201fbbdeb3eb7f465610b09c281ee6aa84e3caTed Kremenek#include "llvm/Support/CrashRecoveryContext.h"
2003201fbbdeb3eb7f465610b09c281ee6aa84e3caTed Kremenek
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
233fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattnerstatic void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
243fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                               const char *Modifier, unsigned ML,
253fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                               const char *Argument, unsigned ArgLen,
26b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                               const Diagnostic::ArgumentValue *PrevArgs,
27b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                               unsigned NumPrevArgs,
2892dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner                               llvm::SmallVectorImpl<char> &Output,
2992dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner                               void *Cookie) {
303fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  const char *Str = "<can't format argument>";
3122caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  Output.append(Str, Str+strlen(Str));
3222caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner}
3322caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner
3422caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner
3533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios KyrtzidisDiagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags,
3633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                       DiagnosticClient *client, bool ShouldOwnClient)
3733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
3833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    SourceMgr(0) {
393fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  ArgToStringFn = DummyArgToStringFn;
4092dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  ArgToStringCookie = 0;
411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  AllExtensionsSilenced = 0;
43cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  IgnoreAllWarnings = false;
44cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  WarningsAsErrors = false;
45cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ErrorsAsFatal = false;
46cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  SuppressSystemWarnings = false;
47cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  SuppressAllDiagnostics = false;
48cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ShowOverloads = Ovl_All;
49cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ExtBehavior = Ext_Ignore;
50cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
51cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ErrorLimit = 0;
52cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  TemplateBacktraceLimit = 0;
53cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
54abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  Reset();
55182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner}
56182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner
57182745ae7892bca0842d9c023370ade5f8d1c6e8Chris LattnerDiagnostic::~Diagnostic() {
5833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (OwnsDiagClient)
5933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    delete Client;
60182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner}
61182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner
624f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregorvoid Diagnostic::setClient(DiagnosticClient *client, bool ShouldOwnClient) {
634f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor  if (OwnsDiagClient && Client)
644f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor    delete Client;
654f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor
664f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor  Client = client;
674f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor  OwnsDiagClient = ShouldOwnClient;
684f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor}
6904ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
700827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisvoid Diagnostic::pushMappings(SourceLocation Loc) {
710827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStateOnPushStack.push_back(GetCurDiagState());
7204ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner}
7304ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
740827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisbool Diagnostic::popMappings(SourceLocation Loc) {
750827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (DiagStateOnPushStack.empty())
7604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner    return false;
7704ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
780827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (DiagStateOnPushStack.back() != GetCurDiagState()) {
790827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // State changed at some point between push/pop.
800827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
810827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
820827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStateOnPushStack.pop_back();
8304ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  return true;
8404ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner}
8504ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
86abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregorvoid Diagnostic::Reset() {
87abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ErrorOccurred = false;
88abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  FatalErrorOccurred = false;
89abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
90abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumWarnings = 0;
91abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumErrors = 0;
92abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumErrorsSuppressed = 0;
93abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  CurDiagID = ~0U;
9433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
9533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  // using a Diagnostic associated to a translation unit that follow
9633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  // diagnostics from a Diagnostic associated to anoter t.u. will not be
9733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  // displayed.
9833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  LastDiagLevel = (DiagnosticIDs::Level)-1;
99abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  DelayedDiagID = 0;
100dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis
101dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  // Clear state related to #pragma diagnostic.
102dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  DiagStates.clear();
103dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  DiagStatePoints.clear();
104dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  DiagStateOnPushStack.clear();
105dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis
106dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  // Create a DiagState and DiagStatePoint representing diagnostic changes
107dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  // through command-line.
108dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  DiagStates.push_back(DiagState());
109dc0a2da1038cc725ad23d070e6a0d03078b7300dArgyrios Kyrtzidis  PushDiagStatePoint(&DiagStates.back(), SourceLocation());
110abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor}
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorvoid Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
11393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor                                      llvm::StringRef Arg2) {
11493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  if (DelayedDiagID)
11593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor    return;
11693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
11793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagID = DiagID;
1189e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  DelayedDiagArg1 = Arg1.str();
1199e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  DelayedDiagArg2 = Arg2.str();
12093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
12193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
12293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorvoid Diagnostic::ReportDelayed() {
12393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
12493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagID = 0;
12593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagArg1.clear();
12693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagArg2.clear();
12793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
12893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
1290827408865e32789e0ec4b8113a302ccdc531423Argyrios KyrtzidisDiagnostic::DiagStatePointsTy::iterator
1300827408865e32789e0ec4b8113a302ccdc531423Argyrios KyrtzidisDiagnostic::GetDiagStatePointForLoc(SourceLocation L) const {
1310827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(!DiagStatePoints.empty());
1320827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(DiagStatePoints.front().Loc.isInvalid() &&
1330827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Should have created a DiagStatePoint for command-line");
1340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc Loc(L, *SourceMgr);
1360827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Loc.isInvalid())
1370827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return DiagStatePoints.end() - 1;
1380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1390827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
1400827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
1410827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (LastStateChangePos.isValid() &&
1420827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
1430827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
1440827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                           DiagStatePoint(0, Loc));
1450827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  --Pos;
1460827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  return Pos;
1470827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis}
1480827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1490827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// \brief This allows the client to specify that certain
1500827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// warnings are ignored.  Notes can never be mapped, errors can only be
1510827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
1520827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis///
1530827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// \param The source location that this change of diagnostic state should
1540827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// take affect. It can be null if we are setting the latest state.
1550827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisvoid Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
1560827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                      SourceLocation L) {
1570827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(Diag < diag::DIAG_UPPER_LIMIT &&
1580827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Can only map builtin diagnostics");
1590827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert((Diags->isBuiltinWarningOrExtension(Diag) ||
1600827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis          (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
1610827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Cannot map errors into warnings!");
1620827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(!DiagStatePoints.empty());
1630827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1643efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  bool isPragma = L.isValid();
1650827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc Loc(L, *SourceMgr);
1660827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
1670827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1680827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Common case; setting all the diagnostics of a group in one place.
1690827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Loc.isInvalid() || Loc == LastStateChangePos) {
1703efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
1710827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
1720827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
1730827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1740827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Another common case; modifying diagnostic state in a source location
1750827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // after the previous one.
1760827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
1770827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
178fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner    // A diagnostic pragma occurred, create a new DiagState initialized with
1790827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // the current one and a new DiagStatePoint to record at which location
1800827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // the new state became active.
1810827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagStates.push_back(*GetCurDiagState());
1820827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    PushDiagStatePoint(&DiagStates.back(), Loc);
1833efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma);
1840827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
1850827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
1860827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1870827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // We allow setting the diagnostic state in random source order for
1880827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // completeness but it should not be actually happening in normal practice.
1890827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1900827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
1910827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(Pos != DiagStatePoints.end());
1920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1930827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Update all diagnostic states that are active after the given location.
1940827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  for (DiagStatePointsTy::iterator
1950827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
1963efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    setDiagnosticMappingInternal(Diag, Map, I->State, true, isPragma);
1970827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
1980827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1990827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // If the location corresponds to an existing point, just update its state.
2000827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Pos->Loc == Loc) {
2013efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis    setDiagnosticMappingInternal(Diag, Map, Pos->State, true, isPragma);
2020827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
2030827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
2040827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
2050827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Create a new state/point and fit it into the vector of DiagStatePoints
2060827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // so that the vector is always ordered according to location.
2070827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  Pos->Loc.isBeforeInTranslationUnitThan(Loc);
2080827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStates.push_back(*Pos->State);
2090827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagState *NewState = &DiagStates.back();
2103efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis  setDiagnosticMappingInternal(Diag, Map, NewState, true, isPragma);
2110827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
2120827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                               FullSourceLoc(Loc, *SourceMgr)));
2130827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis}
2140827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
215b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregorvoid DiagnosticBuilder::FlushCounts() {
216b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  DiagObj->NumDiagArgs = NumArgs;
217b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  DiagObj->NumDiagRanges = NumRanges;
218b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  DiagObj->NumFixItHints = NumFixItHints;
219b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor}
220b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor
22193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorbool DiagnosticBuilder::Emit() {
22293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // If DiagObj is null, then its soul was stolen by the copy ctor
22393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // or the user called Emit().
22493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  if (DiagObj == 0) return false;
22593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
22693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // When emitting diagnostics, we set the final argument count into
22793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // the Diagnostic object.
228b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  FlushCounts();
22993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
23093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // Process the diagnostic, sending the accumulated information to the
23193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // DiagnosticClient.
23293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  bool Emitted = DiagObj->ProcessDiag();
23393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
23493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // Clear out the current diagnostic object.
2359e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  unsigned DiagID = DiagObj->CurDiagID;
23693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DiagObj->Clear();
23793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
23893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // If there was a delayed diagnostic, emit it now.
2399e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
24093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor    DiagObj->ReportDelayed();
24193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
24293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // This diagnostic is dead.
24393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DiagObj = 0;
24493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
24593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  return Emitted;
24693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
24793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
2487bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
2495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerDiagnosticClient::~DiagnosticClient() {}
2507bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
251f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidisvoid DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
252f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis                                        const DiagnosticInfo &Info) {
253f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  if (!IncludeInDiagnosticCounts())
254f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    return;
255f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
256f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  if (DiagLevel == Diagnostic::Warning)
257f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    ++NumWarnings;
258f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  else if (DiagLevel >= Diagnostic::Error)
259f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    ++NumErrors;
260f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis}
261f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
262af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// ModifierIs - Return true if the specified modifier matches specified string.
263af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnertemplate <std::size_t StrLen>
264af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnerstatic bool ModifierIs(const char *Modifier, unsigned ModifierLen,
265af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                       const char (&Str)[StrLen]) {
266af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
267af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
268af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
269909c182f6a0e6169475eef6a71add14555b6162cJohn McCall/// ScanForward - Scans forward, looking for the given character, skipping
270909c182f6a0e6169475eef6a71add14555b6162cJohn McCall/// nested clauses and escaped characters.
271909c182f6a0e6169475eef6a71add14555b6162cJohn McCallstatic const char *ScanFormat(const char *I, const char *E, char Target) {
272909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  unsigned Depth = 0;
273909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
274909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  for ( ; I != E; ++I) {
275909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (Depth == 0 && *I == Target) return I;
276909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (Depth != 0 && *I == '}') Depth--;
277909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
278909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (*I == '%') {
279909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      I++;
280909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      if (I == E) break;
281909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
282909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      // Escaped characters get implicitly skipped here.
283909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
284909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      // Format specifier.
285909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      if (!isdigit(*I) && !ispunct(*I)) {
286909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
287909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        if (I == E) break;
288909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        if (*I == '{')
289909c182f6a0e6169475eef6a71add14555b6162cJohn McCall          Depth++;
290909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      }
291909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    }
292909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  }
293909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  return E;
294909c182f6a0e6169475eef6a71add14555b6162cJohn McCall}
295909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
296af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// HandleSelectModifier - Handle the integer 'select' modifier.  This is used
297af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// like this:  %select{foo|bar|baz}2.  This means that the integer argument
298af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// "%2" has a value from 0-2.  If the value is 0, the diagnostic prints 'foo'.
299af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// If the value is 1, it prints 'bar'.  If it has the value 2, it prints 'baz'.
300af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// This is very useful for certain classes of variant diagnostics.
3019f28614bf1a8387000d8df57a713fcf69e198145John McCallstatic void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
302af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                                 const char *Argument, unsigned ArgumentLen,
303af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                                 llvm::SmallVectorImpl<char> &OutStr) {
304af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  const char *ArgumentEnd = Argument+ArgumentLen;
3051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
306af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  // Skip over 'ValNo' |'s.
307af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  while (ValNo) {
308909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
309af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
310af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner           " larger than the number of options in the diagnostic string!");
311af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    Argument = NextVal+1;  // Skip this string.
312af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    --ValNo;
313af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  }
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
315af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  // Get the end of the value.  This is either the } or the |.
316909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
3179f28614bf1a8387000d8df57a713fcf69e198145John McCall
3189f28614bf1a8387000d8df57a713fcf69e198145John McCall  // Recursively format the result of the select clause into the output string.
3199f28614bf1a8387000d8df57a713fcf69e198145John McCall  DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
320af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
321af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
322af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// HandleIntegerSModifier - Handle the integer 's' modifier.  This adds the
323af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// letter 's' to the string if the value is not 1.  This is used in cases like
324af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// this:  "you idiot, you have %4 parameter%s4!".
325af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnerstatic void HandleIntegerSModifier(unsigned ValNo,
326af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                                   llvm::SmallVectorImpl<char> &OutStr) {
327af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  if (ValNo != 1)
328af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    OutStr.push_back('s');
329af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
330af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
3313be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// HandleOrdinalModifier - Handle the integer 'ord' modifier.  This
3323be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// prints the ordinal form of the given integer, with 1 corresponding
3333be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// to the first ordinal.  Currently this is hard-coded to use the
3343be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// English form.
3353be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCallstatic void HandleOrdinalModifier(unsigned ValNo,
3363be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall                                  llvm::SmallVectorImpl<char> &OutStr) {
3373be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  assert(ValNo != 0 && "ValNo must be strictly positive!");
3383be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
3393be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  llvm::raw_svector_ostream Out(OutStr);
3403be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
3413be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // We could use text forms for the first N ordinals, but the numeric
3423be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // forms are actually nicer in diagnostics because they stand out.
3433be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  Out << ValNo;
3443be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
3453be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // It is critically important that we do this perfectly for
3463be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // user-written sequences with over 100 elements.
3473be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  switch (ValNo % 100) {
3483be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  case 11:
3493be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  case 12:
3503be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  case 13:
3513be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    Out << "th"; return;
3523be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  default:
3533be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    switch (ValNo % 10) {
3543be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    case 1: Out << "st"; return;
3553be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    case 2: Out << "nd"; return;
3563be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    case 3: Out << "rd"; return;
3573be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    default: Out << "th"; return;
3583be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    }
3593be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  }
3603be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall}
3613be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
362af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
363e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// PluralNumber - Parse an unsigned integer and advance Start.
364d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic unsigned PluralNumber(const char *&Start, const char *End) {
365e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  // Programming 101: Parse a decimal number :-)
366e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned Val = 0;
367e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (Start != End && *Start >= '0' && *Start <= '9') {
368e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Val *= 10;
369e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Val += *Start - '0';
370e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    ++Start;
371e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
372e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return Val;
373e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
374e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
375e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
376d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
377e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  if (*Start != '[') {
378e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    unsigned Ref = PluralNumber(Start, End);
379e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    return Ref == Val;
380e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
381e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
382e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
383e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned Low = PluralNumber(Start, End);
384e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  assert(*Start == ',' && "Bad plural expression syntax: expected ,");
385e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
386e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned High = PluralNumber(Start, End);
387e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  assert(*Start == ']' && "Bad plural expression syntax: expected )");
388e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
389e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return Low <= Val && Val <= High;
390e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
391e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
392e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
393d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
394e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  // Empty condition?
395e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  if (*Start == ':')
396e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    return true;
397e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
398e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (1) {
399e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    char C = *Start;
400e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    if (C == '%') {
401e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      // Modulo expression
402e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++Start;
403e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      unsigned Arg = PluralNumber(Start, End);
404e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      assert(*Start == '=' && "Bad plural expression syntax: expected =");
405e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++Start;
406e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      unsigned ValMod = ValNo % Arg;
407e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      if (TestPluralRange(ValMod, Start, End))
408e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl        return true;
409e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    } else {
410e20653219732b03294130999415fc3aa92d2336aSebastian Redl      assert((C == '[' || (C >= '0' && C <= '9')) &&
411e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl             "Bad plural expression syntax: unexpected character");
412e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      // Range expression
413e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      if (TestPluralRange(ValNo, Start, End))
414e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl        return true;
415e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
416e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
417e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    // Scan for next or-expr part.
418e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Start = std::find(Start, End, ',');
4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (Start == End)
420e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      break;
421e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    ++Start;
422e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
423e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return false;
424e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
425e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
426e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
427e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// for complex plural forms, or in languages where all plurals are complex.
428e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
429e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// conditions that are tested in order, the form corresponding to the first
430e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// that applies being emitted. The empty condition is always true, making the
431e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// last form a default case.
432e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Conditions are simple boolean expressions, where n is the number argument.
433e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Here are the rules.
434e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// condition  := expression | empty
435e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// empty      :=                             -> always true
436e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// expression := numeric [',' expression]    -> logical or
437e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// numeric    := range                       -> true if n in range
438e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///             | '%' number '=' range        -> true if n % number in range
439e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// range      := number
440e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///             | '[' number ',' number ']'   -> ranges are inclusive both ends
441e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///
442e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Here are some examples from the GNU gettext manual written in this form:
443e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// English:
444e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|:form1}
445e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Latvian:
446e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
447e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Gaeilge:
448e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|2:form1|:form2}
449e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Romanian:
450e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|0,%100=[1,19]:form1|:form2}
451e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Lithuanian:
452e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
453e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Russian (requires repeated form):
454e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
455e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Slovak
456e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|[2,4]:form1|:form2}
457e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Polish (requires repeated form):
458e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
459e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCallstatic void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
460e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl                                 const char *Argument, unsigned ArgumentLen,
461b54b276a920246c595a0498da281821eb9d22996Chris Lattner                                 llvm::SmallVectorImpl<char> &OutStr) {
462e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  const char *ArgumentEnd = Argument + ArgumentLen;
463e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (1) {
464e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    assert(Argument < ArgumentEnd && "Plural expression didn't match.");
465e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    const char *ExprEnd = Argument;
466e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    while (*ExprEnd != ':') {
467e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
468e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++ExprEnd;
469e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
470e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
471e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      Argument = ExprEnd + 1;
472909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
473e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall
474e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      // Recursively format the result of the plural clause into the
475e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      // output string.
476e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
477e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      return;
478e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
479909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
480e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
481e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
482e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
483e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
484f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// FormatDiagnostic - Format this diagnostic into a string, substituting the
485f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// formal arguments into the %0 slots.  The result is appended onto the Str
486f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// array.
487f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattnervoid DiagnosticInfo::
488f4c839657742b823cea1a95b18422f1ba74d3dddChris LattnerFormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
48933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  const char *DiagStr = getDiags()->getDiagnosticIDs()->getDescription(getID());
490f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  const char *DiagEnd = DiagStr+strlen(DiagStr);
4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4929f28614bf1a8387000d8df57a713fcf69e198145John McCall  FormatDiagnostic(DiagStr, DiagEnd, OutStr);
4939f28614bf1a8387000d8df57a713fcf69e198145John McCall}
4949f28614bf1a8387000d8df57a713fcf69e198145John McCall
4959f28614bf1a8387000d8df57a713fcf69e198145John McCallvoid DiagnosticInfo::
4969f28614bf1a8387000d8df57a713fcf69e198145John McCallFormatDiagnostic(const char *DiagStr, const char *DiagEnd,
4979f28614bf1a8387000d8df57a713fcf69e198145John McCall                 llvm::SmallVectorImpl<char> &OutStr) const {
4989f28614bf1a8387000d8df57a713fcf69e198145John McCall
499b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// FormattedArgs - Keep track of all of the arguments formatted by
500b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// ConvertArgToString and pass them into subsequent calls to
501b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// ConvertArgToString, allowing the implementation to avoid redundancies in
502b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// obvious cases.
503b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
504b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner
505f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  while (DiagStr != DiagEnd) {
506f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner    if (DiagStr[0] != '%') {
507f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      // Append non-%0 substrings to Str if we have one.
508f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
509f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      OutStr.append(DiagStr, StrEnd);
510f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      DiagStr = StrEnd;
511af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      continue;
512909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    } else if (ispunct(DiagStr[1])) {
513909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      OutStr.push_back(DiagStr[1]);  // %% -> %.
514f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      DiagStr += 2;
515af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      continue;
516af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
518af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // Skip the %.
519af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    ++DiagStr;
5201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
521af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // This must be a placeholder for a diagnostic argument.  The format for a
522af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
523af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // The digit is a number from 0-9 indicating which argument this comes from.
524af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // The modifier is a string of digits from the set [-a-z]+, arguments is a
525af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // brace enclosed string.
526af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    const char *Modifier = 0, *Argument = 0;
527af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    unsigned ModifierLen = 0, ArgumentLen = 0;
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
529af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // Check to see if we have a modifier.  If so eat it.
530af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    if (!isdigit(DiagStr[0])) {
531af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      Modifier = DiagStr;
532af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      while (DiagStr[0] == '-' ||
533af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner             (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
534af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        ++DiagStr;
535af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      ModifierLen = DiagStr-Modifier;
536f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
537af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      // If we have an argument, get it next.
538af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      if (DiagStr[0] == '{') {
539af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        ++DiagStr; // Skip {.
540af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        Argument = DiagStr;
5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
542909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
543909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
544af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        ArgumentLen = DiagStr-Argument;
545af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        ++DiagStr;  // Skip }.
546f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      }
547af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
549af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
55022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner    unsigned ArgNo = *DiagStr++ - '0';
551af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
552b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
553b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner
554b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    switch (Kind) {
55508631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    // ---- STRINGS ----
5563cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    case Diagnostic::ak_std_string: {
55722caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      const std::string &S = getArgStdStr(ArgNo);
558af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      assert(ModifierLen == 0 && "No modifiers for strings yet");
559af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      OutStr.append(S.begin(), S.end());
560af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      break;
561af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
5623cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    case Diagnostic::ak_c_string: {
56322caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      const char *S = getArgCStr(ArgNo);
564af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      assert(ModifierLen == 0 && "No modifiers for strings yet");
565e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar
566e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      // Don't crash if get passed a null pointer by accident.
567e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      if (!S)
568e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar        S = "(null)";
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
570af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      OutStr.append(S, S + strlen(S));
571af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      break;
572af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
57308631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    // ---- INTEGERS ----
5743cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    case Diagnostic::ak_sint: {
57522caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      int Val = getArgSInt(ArgNo);
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
577af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      if (ModifierIs(Modifier, ModifierLen, "select")) {
578e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall        HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
579e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall                             OutStr);
580af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      } else if (ModifierIs(Modifier, ModifierLen, "s")) {
581af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        HandleIntegerSModifier(Val, OutStr);
582e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
583e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall        HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
584e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall                             OutStr);
5853be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall      } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
5863be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall        HandleOrdinalModifier((unsigned)Val, OutStr);
587af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      } else {
588af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        assert(ModifierLen == 0 && "Unknown integer modifier");
58923e47c6b6e8ccdd8daa378ab2a879644425c72d8Daniel Dunbar        llvm::raw_svector_ostream(OutStr) << Val;
59030bc96544346bea42921cf6837e66cef80d664b4Chris Lattner      }
591af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      break;
592af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
5933cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    case Diagnostic::ak_uint: {
59422caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      unsigned Val = getArgUInt(ArgNo);
5951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
596af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      if (ModifierIs(Modifier, ModifierLen, "select")) {
5979f28614bf1a8387000d8df57a713fcf69e198145John McCall        HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
598af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      } else if (ModifierIs(Modifier, ModifierLen, "s")) {
599af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        HandleIntegerSModifier(Val, OutStr);
600e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
601e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall        HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
602e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall                             OutStr);
6033be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall      } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
6043be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall        HandleOrdinalModifier(Val, OutStr);
605af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      } else {
606af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        assert(ModifierLen == 0 && "Unknown integer modifier");
60723e47c6b6e8ccdd8daa378ab2a879644425c72d8Daniel Dunbar        llvm::raw_svector_ostream(OutStr) << Val;
60830bc96544346bea42921cf6837e66cef80d664b4Chris Lattner      }
60922caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      break;
610af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
61108631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    // ---- NAMES and TYPES ----
61208631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    case Diagnostic::ak_identifierinfo: {
61308631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      const IdentifierInfo *II = getArgIdentifier(ArgNo);
61408631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      assert(ModifierLen == 0 && "No modifiers for strings yet");
615e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar
616e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      // Don't crash if get passed a null pointer by accident.
617e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      if (!II) {
618e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar        const char *S = "(null)";
619e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar        OutStr.append(S, S + strlen(S));
620e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar        continue;
621e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      }
622e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar
62301eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar      llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
62408631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      break;
62508631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    }
62622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner    case Diagnostic::ak_qualtype:
627011bb4edf731d529da1cbf71c7c2696aaf5a054fChris Lattner    case Diagnostic::ak_declarationname:
62847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    case Diagnostic::ak_nameddecl:
629dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor    case Diagnostic::ak_nestednamespec:
6303f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor    case Diagnostic::ak_declcontext:
631b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner      getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
6323fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                                     Modifier, ModifierLen,
633b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                     Argument, ArgumentLen,
634b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                     FormattedArgs.data(), FormattedArgs.size(),
635b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                     OutStr);
63622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      break;
6377bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber    }
638b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner
639b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    // Remember this argument info for subsequent formatting operations.  Turn
640b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    // std::strings into a null terminated string to make it be the same case as
641b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    // all the other ones.
642b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    if (Kind != Diagnostic::ak_std_string)
643b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner      FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
644b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    else
645b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner      FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
646b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                        (intptr_t)getArgStdStr(ArgNo).c_str()));
647b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner
6487bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  }
6497bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber}
650cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek
651a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::StoredDiagnostic() { }
652a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
653aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas GregorStoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID,
654a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                   llvm::StringRef Message)
655a6a32e295379570b489cc041053d2cd53bcafd1cBenjamin Kramer  : ID(ID), Level(Level), Loc(), Message(Message) { }
656a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
657a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
658a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                   const DiagnosticInfo &Info)
659aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  : ID(Info.getID()), Level(Level)
660aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor{
66133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
66233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis       "Valid source location without setting a source manager for diagnostic");
66333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (Info.getLocation().isValid())
66433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
665a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  llvm::SmallString<64> Message;
666a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  Info.FormatDiagnostic(Message);
667a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  this->Message.assign(Message.begin(), Message.end());
668a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
669a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  Ranges.reserve(Info.getNumRanges());
670a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
671a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor    Ranges.push_back(Info.getRange(I));
672a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
673849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  FixIts.reserve(Info.getNumFixItHints());
674849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
675849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixIts.push_back(Info.getFixItHint(I));
676a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor}
677a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
678a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::~StoredDiagnostic() { }
679a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
680cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek/// IncludeInDiagnosticCounts - This method (whose default implementation
681cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek///  returns true) indicates whether the diagnostics handled by this
682cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek///  DiagnosticClient should be included in the number of diagnostics
683cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek///  reported by Diagnostic.
684cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenekbool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
685fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
686fe6b2d481d91140923f4541f273b253291884214Douglas GregorPartialDiagnostic::StorageAllocator::StorageAllocator() {
687fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  for (unsigned I = 0; I != NumCached; ++I)
688fe6b2d481d91140923f4541f273b253291884214Douglas Gregor    FreeList[I] = Cached + I;
689fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  NumFreeListEntries = NumCached;
690fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
691fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
692fe6b2d481d91140923f4541f273b253291884214Douglas GregorPartialDiagnostic::StorageAllocator::~StorageAllocator() {
69303201fbbdeb3eb7f465610b09c281ee6aa84e3caTed Kremenek  // Don't assert if we are in a CrashRecovery context, as this
69403201fbbdeb3eb7f465610b09c281ee6aa84e3caTed Kremenek  // invariant may be invalidated during a crash.
695cd1eecfe4f43a542c8f9a6e7d12fa69b28c23a67Ted Kremenek  assert((NumFreeListEntries == NumCached || llvm::CrashRecoveryContext::isRecoveringFromCrash()) && "A partial is on the lamb");
696fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
697