Diagnostic.cpp revision 0827408865e32789e0ec4b8113a302ccdc531423
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"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
213fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattnerstatic void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT,
223fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                               const char *Modifier, unsigned ML,
233fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                               const char *Argument, unsigned ArgLen,
24b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                               const Diagnostic::ArgumentValue *PrevArgs,
25b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                               unsigned NumPrevArgs,
2692dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner                               llvm::SmallVectorImpl<char> &Output,
2792dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner                               void *Cookie) {
283fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  const char *Str = "<can't format argument>";
2922caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner  Output.append(Str, Str+strlen(Str));
3022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner}
3122caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner
3222caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner
3333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios KyrtzidisDiagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags,
3433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis                       DiagnosticClient *client, bool ShouldOwnClient)
3533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient),
3633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    SourceMgr(0) {
373fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner  ArgToStringFn = DummyArgToStringFn;
3892dd386e3f05d176b45a638199d51f536bd9d1c4Chris Lattner  ArgToStringCookie = 0;
391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  AllExtensionsSilenced = 0;
41cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  IgnoreAllWarnings = false;
42cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  WarningsAsErrors = false;
43cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ErrorsAsFatal = false;
44cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  SuppressSystemWarnings = false;
45cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  SuppressAllDiagnostics = false;
46cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ShowOverloads = Ovl_All;
47cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ExtBehavior = Ext_Ignore;
48cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
49cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  ErrorLimit = 0;
50cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor  TemplateBacktraceLimit = 0;
51cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
520827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Create a DiagState and DiagStatePoint representing diagnostic changes
530827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // through command-line.
540827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStates.push_back(DiagState());
550827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  PushDiagStatePoint(&DiagStates.back(), SourceLocation());
56cc5888d833caf90ebda37f24da40d2cd06b4d820Douglas Gregor
57abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  Reset();
58182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner}
59182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner
60182745ae7892bca0842d9c023370ade5f8d1c6e8Chris LattnerDiagnostic::~Diagnostic() {
6133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (OwnsDiagClient)
6233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    delete Client;
63182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner}
64182745ae7892bca0842d9c023370ade5f8d1c6e8Chris Lattner
6504ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
660827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisvoid Diagnostic::pushMappings(SourceLocation Loc) {
670827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStateOnPushStack.push_back(GetCurDiagState());
6804ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner}
6904ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
700827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisbool Diagnostic::popMappings(SourceLocation Loc) {
710827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (DiagStateOnPushStack.empty())
7204ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner    return false;
7304ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
740827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (DiagStateOnPushStack.back() != GetCurDiagState()) {
750827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // State changed at some point between push/pop.
760827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
770827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
780827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStateOnPushStack.pop_back();
7904ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner  return true;
8004ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner}
8104ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner
82abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregorvoid Diagnostic::Reset() {
83abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  ErrorOccurred = false;
84abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  FatalErrorOccurred = false;
85abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor
86abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumWarnings = 0;
87abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumErrors = 0;
88abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  NumErrorsSuppressed = 0;
89abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  CurDiagID = ~0U;
9033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes
9133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  // using a Diagnostic associated to a translation unit that follow
9233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  // diagnostics from a Diagnostic associated to anoter t.u. will not be
9333e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  // displayed.
9433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  LastDiagLevel = (DiagnosticIDs::Level)-1;
95abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor  DelayedDiagID = 0;
96abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor}
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorvoid Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1,
9993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor                                      llvm::StringRef Arg2) {
10093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  if (DelayedDiagID)
10193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor    return;
10293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
10393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagID = DiagID;
1049e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  DelayedDiagArg1 = Arg1.str();
1059e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  DelayedDiagArg2 = Arg2.str();
10693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
10793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
10893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorvoid Diagnostic::ReportDelayed() {
10993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
11093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagID = 0;
11193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagArg1.clear();
11293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DelayedDiagArg2.clear();
11393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
11493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
1150827408865e32789e0ec4b8113a302ccdc531423Argyrios KyrtzidisDiagnostic::DiagStatePointsTy::iterator
1160827408865e32789e0ec4b8113a302ccdc531423Argyrios KyrtzidisDiagnostic::GetDiagStatePointForLoc(SourceLocation L) const {
1170827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(!DiagStatePoints.empty());
1180827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(DiagStatePoints.front().Loc.isInvalid() &&
1190827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Should have created a DiagStatePoint for command-line");
1200827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1210827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc Loc(L, *SourceMgr);
1220827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Loc.isInvalid())
1230827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return DiagStatePoints.end() - 1;
1240827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
1260827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
1270827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (LastStateChangePos.isValid() &&
1280827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
1290827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
1300827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                           DiagStatePoint(0, Loc));
1310827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  --Pos;
1320827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  return Pos;
1330827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis}
1340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// \brief This allows the client to specify that certain
1360827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// warnings are ignored.  Notes can never be mapped, errors can only be
1370827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
1380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis///
1390827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// \param The source location that this change of diagnostic state should
1400827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// take affect. It can be null if we are setting the latest state.
1410827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisvoid Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
1420827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                      SourceLocation L) {
1430827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(Diag < diag::DIAG_UPPER_LIMIT &&
1440827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Can only map builtin diagnostics");
1450827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert((Diags->isBuiltinWarningOrExtension(Diag) ||
1460827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis          (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) &&
1470827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         "Cannot map errors into warnings!");
1480827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(!DiagStatePoints.empty());
1490827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1500827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc Loc(L, *SourceMgr);
1510827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
1520827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1530827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Common case; setting all the diagnostics of a group in one place.
1540827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Loc.isInvalid() || Loc == LastStateChangePos) {
1550827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true);
1560827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
1570827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
1580827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1590827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Another common case; modifying diagnostic state in a source location
1600827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // after the previous one.
1610827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
1620827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis      LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
1630827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // A diagnostic pragma occured, create a new DiagState initialized with
1640827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // the current one and a new DiagStatePoint to record at which location
1650827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    // the new state became active.
1660827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    DiagStates.push_back(*GetCurDiagState());
1670827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    PushDiagStatePoint(&DiagStates.back(), Loc);
1680827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true);
1690827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
1700827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
1710827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1720827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // We allow setting the diagnostic state in random source order for
1730827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // completeness but it should not be actually happening in normal practice.
1740827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1750827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
1760827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  assert(Pos != DiagStatePoints.end());
1770827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1780827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Update all diagnostic states that are active after the given location.
1790827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  for (DiagStatePointsTy::iterator
1800827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis         I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
1810827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    setDiagnosticMappingInternal(Diag, Map, I->State, true);
1820827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
1830827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1840827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // If the location corresponds to an existing point, just update its state.
1850827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  if (Pos->Loc == Loc) {
1860827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    setDiagnosticMappingInternal(Diag, Map, Pos->State, true);
1870827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis    return;
1880827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  }
1890827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
1900827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // Create a new state/point and fit it into the vector of DiagStatePoints
1910827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  // so that the vector is always ordered according to location.
1920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  Pos->Loc.isBeforeInTranslationUnitThan(Loc);
1930827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStates.push_back(*Pos->State);
1940827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagState *NewState = &DiagStates.back();
1950827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  setDiagnosticMappingInternal(Diag, Map, NewState, true);
1960827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis  DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
1970827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis                                               FullSourceLoc(Loc, *SourceMgr)));
1980827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis}
1990827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis
200b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregorvoid DiagnosticBuilder::FlushCounts() {
201b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  DiagObj->NumDiagArgs = NumArgs;
202b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  DiagObj->NumDiagRanges = NumRanges;
203b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  DiagObj->NumFixItHints = NumFixItHints;
204b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor}
205b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor
20693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorbool DiagnosticBuilder::Emit() {
20793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // If DiagObj is null, then its soul was stolen by the copy ctor
20893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // or the user called Emit().
20993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  if (DiagObj == 0) return false;
21093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
21193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // When emitting diagnostics, we set the final argument count into
21293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // the Diagnostic object.
213b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor  FlushCounts();
21493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
21593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // Process the diagnostic, sending the accumulated information to the
21693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // DiagnosticClient.
21793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  bool Emitted = DiagObj->ProcessDiag();
21893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
21993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // Clear out the current diagnostic object.
2209e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  unsigned DiagID = DiagObj->CurDiagID;
22193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DiagObj->Clear();
22293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
22393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // If there was a delayed diagnostic, emit it now.
2249e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor  if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
22593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor    DiagObj->ReportDelayed();
22693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
22793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  // This diagnostic is dead.
22893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  DiagObj = 0;
22993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
23093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor  return Emitted;
23193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor}
23293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor
2337bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
2345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerDiagnosticClient::~DiagnosticClient() {}
2357bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber
236f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidisvoid DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
237f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis                                        const DiagnosticInfo &Info) {
238f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  if (!IncludeInDiagnosticCounts())
239f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    return;
240f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis
241f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  if (DiagLevel == Diagnostic::Warning)
242f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    ++NumWarnings;
243f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis  else if (DiagLevel >= Diagnostic::Error)
244f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis    ++NumErrors;
245f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis}
246f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
247af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// ModifierIs - Return true if the specified modifier matches specified string.
248af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnertemplate <std::size_t StrLen>
249af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnerstatic bool ModifierIs(const char *Modifier, unsigned ModifierLen,
250af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                       const char (&Str)[StrLen]) {
251af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
252af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
253af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
254909c182f6a0e6169475eef6a71add14555b6162cJohn McCall/// ScanForward - Scans forward, looking for the given character, skipping
255909c182f6a0e6169475eef6a71add14555b6162cJohn McCall/// nested clauses and escaped characters.
256909c182f6a0e6169475eef6a71add14555b6162cJohn McCallstatic const char *ScanFormat(const char *I, const char *E, char Target) {
257909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  unsigned Depth = 0;
258909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
259909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  for ( ; I != E; ++I) {
260909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (Depth == 0 && *I == Target) return I;
261909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (Depth != 0 && *I == '}') Depth--;
262909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
263909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    if (*I == '%') {
264909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      I++;
265909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      if (I == E) break;
266909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
267909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      // Escaped characters get implicitly skipped here.
268909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
269909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      // Format specifier.
270909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      if (!isdigit(*I) && !ispunct(*I)) {
271909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ;
272909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        if (I == E) break;
273909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        if (*I == '{')
274909c182f6a0e6169475eef6a71add14555b6162cJohn McCall          Depth++;
275909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      }
276909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    }
277909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  }
278909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  return E;
279909c182f6a0e6169475eef6a71add14555b6162cJohn McCall}
280909c182f6a0e6169475eef6a71add14555b6162cJohn McCall
281af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// HandleSelectModifier - Handle the integer 'select' modifier.  This is used
282af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// like this:  %select{foo|bar|baz}2.  This means that the integer argument
283af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// "%2" has a value from 0-2.  If the value is 0, the diagnostic prints 'foo'.
284af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// If the value is 1, it prints 'bar'.  If it has the value 2, it prints 'baz'.
285af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// This is very useful for certain classes of variant diagnostics.
2869f28614bf1a8387000d8df57a713fcf69e198145John McCallstatic void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
287af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                                 const char *Argument, unsigned ArgumentLen,
288af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                                 llvm::SmallVectorImpl<char> &OutStr) {
289af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  const char *ArgumentEnd = Argument+ArgumentLen;
2901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
291af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  // Skip over 'ValNo' |'s.
292af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  while (ValNo) {
293909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
294af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
295af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner           " larger than the number of options in the diagnostic string!");
296af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    Argument = NextVal+1;  // Skip this string.
297af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    --ValNo;
298af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  }
2991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
300af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  // Get the end of the value.  This is either the } or the |.
301909c182f6a0e6169475eef6a71add14555b6162cJohn McCall  const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
3029f28614bf1a8387000d8df57a713fcf69e198145John McCall
3039f28614bf1a8387000d8df57a713fcf69e198145John McCall  // Recursively format the result of the select clause into the output string.
3049f28614bf1a8387000d8df57a713fcf69e198145John McCall  DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
305af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
306af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
307af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// HandleIntegerSModifier - Handle the integer 's' modifier.  This adds the
308af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// letter 's' to the string if the value is not 1.  This is used in cases like
309af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// this:  "you idiot, you have %4 parameter%s4!".
310af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnerstatic void HandleIntegerSModifier(unsigned ValNo,
311af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner                                   llvm::SmallVectorImpl<char> &OutStr) {
312af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner  if (ValNo != 1)
313af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    OutStr.push_back('s');
314af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner}
315af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
3163be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// HandleOrdinalModifier - Handle the integer 'ord' modifier.  This
3173be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// prints the ordinal form of the given integer, with 1 corresponding
3183be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// to the first ordinal.  Currently this is hard-coded to use the
3193be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// English form.
3203be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCallstatic void HandleOrdinalModifier(unsigned ValNo,
3213be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall                                  llvm::SmallVectorImpl<char> &OutStr) {
3223be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  assert(ValNo != 0 && "ValNo must be strictly positive!");
3233be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
3243be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  llvm::raw_svector_ostream Out(OutStr);
3253be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
3263be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // We could use text forms for the first N ordinals, but the numeric
3273be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // forms are actually nicer in diagnostics because they stand out.
3283be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  Out << ValNo;
3293be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
3303be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // It is critically important that we do this perfectly for
3313be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  // user-written sequences with over 100 elements.
3323be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  switch (ValNo % 100) {
3333be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  case 11:
3343be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  case 12:
3353be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  case 13:
3363be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    Out << "th"; return;
3373be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  default:
3383be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    switch (ValNo % 10) {
3393be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    case 1: Out << "st"; return;
3403be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    case 2: Out << "nd"; return;
3413be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    case 3: Out << "rd"; return;
3423be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    default: Out << "th"; return;
3433be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall    }
3443be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall  }
3453be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall}
3463be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall
347af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
348e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// PluralNumber - Parse an unsigned integer and advance Start.
349d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic unsigned PluralNumber(const char *&Start, const char *End) {
350e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  // Programming 101: Parse a decimal number :-)
351e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned Val = 0;
352e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (Start != End && *Start >= '0' && *Start <= '9') {
353e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Val *= 10;
354e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Val += *Start - '0';
355e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    ++Start;
356e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
357e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return Val;
358e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
359e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
360e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
361d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
362e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  if (*Start != '[') {
363e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    unsigned Ref = PluralNumber(Start, End);
364e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    return Ref == Val;
365e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
366e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
367e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
368e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned Low = PluralNumber(Start, End);
369e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  assert(*Start == ',' && "Bad plural expression syntax: expected ,");
370e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
371e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  unsigned High = PluralNumber(Start, End);
372e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  assert(*Start == ']' && "Bad plural expression syntax: expected )");
373e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  ++Start;
374e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return Low <= Val && Val <= High;
375e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
376e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
377e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
378d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
379e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  // Empty condition?
380e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  if (*Start == ':')
381e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    return true;
382e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
383e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (1) {
384e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    char C = *Start;
385e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    if (C == '%') {
386e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      // Modulo expression
387e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++Start;
388e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      unsigned Arg = PluralNumber(Start, End);
389e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      assert(*Start == '=' && "Bad plural expression syntax: expected =");
390e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++Start;
391e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      unsigned ValMod = ValNo % Arg;
392e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      if (TestPluralRange(ValMod, Start, End))
393e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl        return true;
394e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    } else {
395e20653219732b03294130999415fc3aa92d2336aSebastian Redl      assert((C == '[' || (C >= '0' && C <= '9')) &&
396e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl             "Bad plural expression syntax: unexpected character");
397e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      // Range expression
398e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      if (TestPluralRange(ValNo, Start, End))
399e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl        return true;
400e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
401e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
402e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    // Scan for next or-expr part.
403e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    Start = std::find(Start, End, ',');
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (Start == End)
405e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      break;
406e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    ++Start;
407e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
408e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  return false;
409e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
410e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
411e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
412e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// for complex plural forms, or in languages where all plurals are complex.
413e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
414e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// conditions that are tested in order, the form corresponding to the first
415e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// that applies being emitted. The empty condition is always true, making the
416e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// last form a default case.
417e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Conditions are simple boolean expressions, where n is the number argument.
418e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Here are the rules.
419e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// condition  := expression | empty
420e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// empty      :=                             -> always true
421e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// expression := numeric [',' expression]    -> logical or
422e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// numeric    := range                       -> true if n in range
423e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///             | '%' number '=' range        -> true if n % number in range
424e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// range      := number
425e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///             | '[' number ',' number ']'   -> ranges are inclusive both ends
426e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl///
427e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Here are some examples from the GNU gettext manual written in this form:
428e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// English:
429e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|:form1}
430e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Latvian:
431e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
432e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Gaeilge:
433e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|2:form1|:form2}
434e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Romanian:
435e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|0,%100=[1,19]:form1|:form2}
436e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Lithuanian:
437e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
438e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Russian (requires repeated form):
439e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
440e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Slovak
441e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|[2,4]:form1|:form2}
442e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Polish (requires repeated form):
443e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
444e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCallstatic void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo,
445e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl                                 const char *Argument, unsigned ArgumentLen,
446b54b276a920246c595a0498da281821eb9d22996Chris Lattner                                 llvm::SmallVectorImpl<char> &OutStr) {
447e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  const char *ArgumentEnd = Argument + ArgumentLen;
448e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  while (1) {
449e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    assert(Argument < ArgumentEnd && "Plural expression didn't match.");
450e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    const char *ExprEnd = Argument;
451e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    while (*ExprEnd != ':') {
452e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
453e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      ++ExprEnd;
454e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
455e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
456e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      Argument = ExprEnd + 1;
457909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
458e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall
459e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      // Recursively format the result of the plural clause into the
460e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      // output string.
461e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall      DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
462e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      return;
463e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl    }
464909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
465e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl  }
466e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl}
467e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
468e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl
469f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// FormatDiagnostic - Format this diagnostic into a string, substituting the
470f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// formal arguments into the %0 slots.  The result is appended onto the Str
471f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// array.
472f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattnervoid DiagnosticInfo::
473f4c839657742b823cea1a95b18422f1ba74d3dddChris LattnerFormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
47433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  const char *DiagStr = getDiags()->getDiagnosticIDs()->getDescription(getID());
475f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  const char *DiagEnd = DiagStr+strlen(DiagStr);
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4779f28614bf1a8387000d8df57a713fcf69e198145John McCall  FormatDiagnostic(DiagStr, DiagEnd, OutStr);
4789f28614bf1a8387000d8df57a713fcf69e198145John McCall}
4799f28614bf1a8387000d8df57a713fcf69e198145John McCall
4809f28614bf1a8387000d8df57a713fcf69e198145John McCallvoid DiagnosticInfo::
4819f28614bf1a8387000d8df57a713fcf69e198145John McCallFormatDiagnostic(const char *DiagStr, const char *DiagEnd,
4829f28614bf1a8387000d8df57a713fcf69e198145John McCall                 llvm::SmallVectorImpl<char> &OutStr) const {
4839f28614bf1a8387000d8df57a713fcf69e198145John McCall
484b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// FormattedArgs - Keep track of all of the arguments formatted by
485b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// ConvertArgToString and pass them into subsequent calls to
486b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// ConvertArgToString, allowing the implementation to avoid redundancies in
487b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  /// obvious cases.
488b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner  llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs;
489b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner
490f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner  while (DiagStr != DiagEnd) {
491f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner    if (DiagStr[0] != '%') {
492f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      // Append non-%0 substrings to Str if we have one.
493f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
494f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      OutStr.append(DiagStr, StrEnd);
495f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      DiagStr = StrEnd;
496af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      continue;
497909c182f6a0e6169475eef6a71add14555b6162cJohn McCall    } else if (ispunct(DiagStr[1])) {
498909c182f6a0e6169475eef6a71add14555b6162cJohn McCall      OutStr.push_back(DiagStr[1]);  // %% -> %.
499f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      DiagStr += 2;
500af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      continue;
501af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
503af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // Skip the %.
504af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    ++DiagStr;
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
506af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // This must be a placeholder for a diagnostic argument.  The format for a
507af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
508af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // The digit is a number from 0-9 indicating which argument this comes from.
509af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // The modifier is a string of digits from the set [-a-z]+, arguments is a
510af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // brace enclosed string.
511af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    const char *Modifier = 0, *Argument = 0;
512af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    unsigned ModifierLen = 0, ArgumentLen = 0;
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
514af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    // Check to see if we have a modifier.  If so eat it.
515af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    if (!isdigit(DiagStr[0])) {
516af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      Modifier = DiagStr;
517af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      while (DiagStr[0] == '-' ||
518af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner             (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
519af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        ++DiagStr;
520af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      ModifierLen = DiagStr-Modifier;
521f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner
522af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      // If we have an argument, get it next.
523af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      if (DiagStr[0] == '{') {
524af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        ++DiagStr; // Skip {.
525af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        Argument = DiagStr;
5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
527909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
528909c182f6a0e6169475eef6a71add14555b6162cJohn McCall        assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
529af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        ArgumentLen = DiagStr-Argument;
530af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        ++DiagStr;  // Skip }.
531f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner      }
532af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
534af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic");
53522caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner    unsigned ArgNo = *DiagStr++ - '0';
536af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner
537b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    Diagnostic::ArgumentKind Kind = getArgKind(ArgNo);
538b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner
539b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    switch (Kind) {
54008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    // ---- STRINGS ----
5413cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    case Diagnostic::ak_std_string: {
54222caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      const std::string &S = getArgStdStr(ArgNo);
543af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      assert(ModifierLen == 0 && "No modifiers for strings yet");
544af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      OutStr.append(S.begin(), S.end());
545af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      break;
546af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
5473cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    case Diagnostic::ak_c_string: {
54822caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      const char *S = getArgCStr(ArgNo);
549af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      assert(ModifierLen == 0 && "No modifiers for strings yet");
550e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar
551e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      // Don't crash if get passed a null pointer by accident.
552e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      if (!S)
553e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar        S = "(null)";
5541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
555af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      OutStr.append(S, S + strlen(S));
556af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      break;
557af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
55808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    // ---- INTEGERS ----
5593cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    case Diagnostic::ak_sint: {
56022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      int Val = getArgSInt(ArgNo);
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
562af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      if (ModifierIs(Modifier, ModifierLen, "select")) {
563e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall        HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
564e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall                             OutStr);
565af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      } else if (ModifierIs(Modifier, ModifierLen, "s")) {
566af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        HandleIntegerSModifier(Val, OutStr);
567e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
568e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall        HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
569e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall                             OutStr);
5703be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall      } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
5713be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall        HandleOrdinalModifier((unsigned)Val, OutStr);
572af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      } else {
573af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        assert(ModifierLen == 0 && "Unknown integer modifier");
57423e47c6b6e8ccdd8daa378ab2a879644425c72d8Daniel Dunbar        llvm::raw_svector_ostream(OutStr) << Val;
57530bc96544346bea42921cf6837e66cef80d664b4Chris Lattner      }
576af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      break;
577af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
5783cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner    case Diagnostic::ak_uint: {
57922caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      unsigned Val = getArgUInt(ArgNo);
5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
581af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      if (ModifierIs(Modifier, ModifierLen, "select")) {
5829f28614bf1a8387000d8df57a713fcf69e198145John McCall        HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
583af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      } else if (ModifierIs(Modifier, ModifierLen, "s")) {
584af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        HandleIntegerSModifier(Val, OutStr);
585e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl      } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
586e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall        HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
587e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall                             OutStr);
5883be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall      } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
5893be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall        HandleOrdinalModifier(Val, OutStr);
590af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner      } else {
591af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner        assert(ModifierLen == 0 && "Unknown integer modifier");
59223e47c6b6e8ccdd8daa378ab2a879644425c72d8Daniel Dunbar        llvm::raw_svector_ostream(OutStr) << Val;
59330bc96544346bea42921cf6837e66cef80d664b4Chris Lattner      }
59422caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      break;
595af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner    }
59608631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    // ---- NAMES and TYPES ----
59708631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    case Diagnostic::ak_identifierinfo: {
59808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      const IdentifierInfo *II = getArgIdentifier(ArgNo);
59908631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      assert(ModifierLen == 0 && "No modifiers for strings yet");
600e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar
601e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      // Don't crash if get passed a null pointer by accident.
602e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      if (!II) {
603e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar        const char *S = "(null)";
604e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar        OutStr.append(S, S + strlen(S));
605e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar        continue;
606e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar      }
607e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar
60801eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar      llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
60908631c5fa053867146b5ee8be658c229f6bf127cChris Lattner      break;
61008631c5fa053867146b5ee8be658c229f6bf127cChris Lattner    }
61122caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner    case Diagnostic::ak_qualtype:
612011bb4edf731d529da1cbf71c7c2696aaf5a054fChris Lattner    case Diagnostic::ak_declarationname:
61347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor    case Diagnostic::ak_nameddecl:
614dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor    case Diagnostic::ak_nestednamespec:
6153f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor    case Diagnostic::ak_declcontext:
616b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner      getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
6173fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner                                     Modifier, ModifierLen,
618b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                     Argument, ArgumentLen,
619b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                     FormattedArgs.data(), FormattedArgs.size(),
620b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                     OutStr);
62122caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner      break;
6227bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber    }
623b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner
624b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    // Remember this argument info for subsequent formatting operations.  Turn
625b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    // std::strings into a null terminated string to make it be the same case as
626b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    // all the other ones.
627b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    if (Kind != Diagnostic::ak_std_string)
628b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner      FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
629b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner    else
630b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner      FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string,
631b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner                                        (intptr_t)getArgStdStr(ArgNo).c_str()));
632b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner
6337bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber  }
6347bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber}
635cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek
636a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::StoredDiagnostic() { }
637a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
638aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas GregorStoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID,
639a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                   llvm::StringRef Message)
640a6a32e295379570b489cc041053d2cd53bcafd1cBenjamin Kramer  : ID(ID), Level(Level), Loc(), Message(Message) { }
641a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
642a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level,
643a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                                   const DiagnosticInfo &Info)
644aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor  : ID(Info.getID()), Level(Level)
645aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor{
64633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
64733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis       "Valid source location without setting a source manager for diagnostic");
64833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis  if (Info.getLocation().isValid())
64933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis    Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
650a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  llvm::SmallString<64> Message;
651a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  Info.FormatDiagnostic(Message);
652a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  this->Message.assign(Message.begin(), Message.end());
653a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
654a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  Ranges.reserve(Info.getNumRanges());
655a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
656a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor    Ranges.push_back(Info.getRange(I));
657a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
658849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  FixIts.reserve(Info.getNumFixItHints());
659849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor  for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
660849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor    FixIts.push_back(Info.getFixItHint(I));
661a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor}
662a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
663a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::~StoredDiagnostic() { }
664a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
665cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek/// IncludeInDiagnosticCounts - This method (whose default implementation
666cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek///  returns true) indicates whether the diagnostics handled by this
667cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek///  DiagnosticClient should be included in the number of diagnostics
668cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek///  reported by Diagnostic.
669cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenekbool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; }
670fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
671fe6b2d481d91140923f4541f273b253291884214Douglas GregorPartialDiagnostic::StorageAllocator::StorageAllocator() {
672fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  for (unsigned I = 0; I != NumCached; ++I)
673fe6b2d481d91140923f4541f273b253291884214Douglas Gregor    FreeList[I] = Cached + I;
674fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  NumFreeListEntries = NumCached;
675fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
676fe6b2d481d91140923f4541f273b253291884214Douglas Gregor
677fe6b2d481d91140923f4541f273b253291884214Douglas GregorPartialDiagnostic::StorageAllocator::~StorageAllocator() {
678fe6b2d481d91140923f4541f273b253291884214Douglas Gregor  assert(NumFreeListEntries == NumCached && "A partial is on the lamb");
679fe6b2d481d91140923f4541f273b253291884214Douglas Gregor}
680