Diagnostic.cpp revision 4f5e21e24fb9e6ec473a13f83b5c9a2c41501a70
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 654f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregorvoid Diagnostic::setClient(DiagnosticClient *client, bool ShouldOwnClient) { 664f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor if (OwnsDiagClient && Client) 674f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor delete Client; 684f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor 694f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor Client = client; 704f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor OwnsDiagClient = ShouldOwnClient; 714f5e21e24fb9e6ec473a13f83b5c9a2c41501a70Douglas Gregor} 7204ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner 730827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisvoid Diagnostic::pushMappings(SourceLocation Loc) { 740827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagStateOnPushStack.push_back(GetCurDiagState()); 7504ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner} 7604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner 770827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisbool Diagnostic::popMappings(SourceLocation Loc) { 780827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if (DiagStateOnPushStack.empty()) 7904ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner return false; 8004ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner 810827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if (DiagStateOnPushStack.back() != GetCurDiagState()) { 820827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // State changed at some point between push/pop. 830827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis PushDiagStatePoint(DiagStateOnPushStack.back(), Loc); 840827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis } 850827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagStateOnPushStack.pop_back(); 8604ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner return true; 8704ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner} 8804ae2df026b275aae5dddfc0db5ca55ff4e62179Chris Lattner 89abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregorvoid Diagnostic::Reset() { 90abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor ErrorOccurred = false; 91abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor FatalErrorOccurred = false; 92abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor 93abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor NumWarnings = 0; 94abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor NumErrors = 0; 95abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor NumErrorsSuppressed = 0; 96abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor CurDiagID = ~0U; 9733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes 9833e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis // using a Diagnostic associated to a translation unit that follow 9933e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis // diagnostics from a Diagnostic associated to anoter t.u. will not be 10033e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis // displayed. 10133e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis LastDiagLevel = (DiagnosticIDs::Level)-1; 102abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor DelayedDiagID = 0; 103abc563f554951259bbe0315055cad92ee14d87e4Douglas Gregor} 1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 10593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorvoid Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1, 10693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor llvm::StringRef Arg2) { 10793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor if (DelayedDiagID) 10893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor return; 10993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 11093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor DelayedDiagID = DiagID; 1119e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor DelayedDiagArg1 = Arg1.str(); 1129e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor DelayedDiagArg2 = Arg2.str(); 11393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor} 11493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 11593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorvoid Diagnostic::ReportDelayed() { 11693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2; 11793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor DelayedDiagID = 0; 11893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor DelayedDiagArg1.clear(); 11993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor DelayedDiagArg2.clear(); 12093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor} 12193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 1220827408865e32789e0ec4b8113a302ccdc531423Argyrios KyrtzidisDiagnostic::DiagStatePointsTy::iterator 1230827408865e32789e0ec4b8113a302ccdc531423Argyrios KyrtzidisDiagnostic::GetDiagStatePointForLoc(SourceLocation L) const { 1240827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis assert(!DiagStatePoints.empty()); 1250827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis assert(DiagStatePoints.front().Loc.isInvalid() && 1260827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis "Should have created a DiagStatePoint for command-line"); 1270827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1280827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis FullSourceLoc Loc(L, *SourceMgr); 1290827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if (Loc.isInvalid()) 1300827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis return DiagStatePoints.end() - 1; 1310827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1320827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagStatePointsTy::iterator Pos = DiagStatePoints.end(); 1330827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; 1340827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if (LastStateChangePos.isValid() && 1350827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis Loc.isBeforeInTranslationUnitThan(LastStateChangePos)) 1360827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(), 1370827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagStatePoint(0, Loc)); 1380827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis --Pos; 1390827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis return Pos; 1400827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis} 1410827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1420827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// \brief This allows the client to specify that certain 1430827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// warnings are ignored. Notes can never be mapped, errors can only be 1440827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily. 1450827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// 1460827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// \param The source location that this change of diagnostic state should 1470827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis/// take affect. It can be null if we are setting the latest state. 1480827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidisvoid Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map, 1490827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis SourceLocation L) { 1500827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis assert(Diag < diag::DIAG_UPPER_LIMIT && 1510827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis "Can only map builtin diagnostics"); 1520827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis assert((Diags->isBuiltinWarningOrExtension(Diag) || 1530827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) && 1540827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis "Cannot map errors into warnings!"); 1550827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis assert(!DiagStatePoints.empty()); 1560827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1573efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis bool isPragma = L.isValid(); 1580827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis FullSourceLoc Loc(L, *SourceMgr); 1590827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; 1600827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1610827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // Common case; setting all the diagnostics of a group in one place. 1620827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if (Loc.isInvalid() || Loc == LastStateChangePos) { 1633efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma); 1640827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis return; 1650827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis } 1660827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1670827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // Another common case; modifying diagnostic state in a source location 1680827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // after the previous one. 1690827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if ((Loc.isValid() && LastStateChangePos.isInvalid()) || 1700827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) { 1710827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // A diagnostic pragma occured, create a new DiagState initialized with 1720827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // the current one and a new DiagStatePoint to record at which location 1730827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // the new state became active. 1740827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagStates.push_back(*GetCurDiagState()); 1750827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis PushDiagStatePoint(&DiagStates.back(), Loc); 1763efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma); 1770827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis return; 1780827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis } 1790827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1800827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // We allow setting the diagnostic state in random source order for 1810827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // completeness but it should not be actually happening in normal practice. 1820827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1830827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc); 1840827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis assert(Pos != DiagStatePoints.end()); 1850827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1860827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // Update all diagnostic states that are active after the given location. 1870827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis for (DiagStatePointsTy::iterator 1880827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) { 1893efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis setDiagnosticMappingInternal(Diag, Map, I->State, true, isPragma); 1900827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis } 1910827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1920827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // If the location corresponds to an existing point, just update its state. 1930827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis if (Pos->Loc == Loc) { 1943efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis setDiagnosticMappingInternal(Diag, Map, Pos->State, true, isPragma); 1950827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis return; 1960827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis } 1970827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 1980827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // Create a new state/point and fit it into the vector of DiagStatePoints 1990827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis // so that the vector is always ordered according to location. 2000827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis Pos->Loc.isBeforeInTranslationUnitThan(Loc); 2010827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagStates.push_back(*Pos->State); 2020827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagState *NewState = &DiagStates.back(); 2033efd52cf8f4e57c5571bd8cc3168264c3bc46a1eArgyrios Kyrtzidis setDiagnosticMappingInternal(Diag, Map, NewState, true, isPragma); 2040827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState, 2050827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis FullSourceLoc(Loc, *SourceMgr))); 2060827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis} 2070827408865e32789e0ec4b8113a302ccdc531423Argyrios Kyrtzidis 208b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregorvoid DiagnosticBuilder::FlushCounts() { 209b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor DiagObj->NumDiagArgs = NumArgs; 210b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor DiagObj->NumDiagRanges = NumRanges; 211b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor DiagObj->NumFixItHints = NumFixItHints; 212b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor} 213b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor 21493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregorbool DiagnosticBuilder::Emit() { 21593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // If DiagObj is null, then its soul was stolen by the copy ctor 21693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // or the user called Emit(). 21793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor if (DiagObj == 0) return false; 21893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 21993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // When emitting diagnostics, we set the final argument count into 22093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // the Diagnostic object. 221b535041ee33c5eff255832bc5541c8d52aae8254Douglas Gregor FlushCounts(); 22293ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 22393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // Process the diagnostic, sending the accumulated information to the 22493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // DiagnosticClient. 22593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor bool Emitted = DiagObj->ProcessDiag(); 22693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 22793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // Clear out the current diagnostic object. 2289e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor unsigned DiagID = DiagObj->CurDiagID; 22993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor DiagObj->Clear(); 23093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 23193ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // If there was a delayed diagnostic, emit it now. 2329e2dac9c9b6bc4384c816a447cca6516a03c89f2Douglas Gregor if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID) 23393ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor DiagObj->ReportDelayed(); 23493ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 23593ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor // This diagnostic is dead. 23693ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor DiagObj = 0; 23793ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 23893ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor return Emitted; 23993ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor} 24093ea5cb0edf8e509c5113e70cb05ee247c9bdf6bDouglas Gregor 2417bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber 2425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerDiagnosticClient::~DiagnosticClient() {} 2437bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber 244f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidisvoid DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, 245f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis const DiagnosticInfo &Info) { 246f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis if (!IncludeInDiagnosticCounts()) 247f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis return; 248f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis 249f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis if (DiagLevel == Diagnostic::Warning) 250f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis ++NumWarnings; 251f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis else if (DiagLevel >= Diagnostic::Error) 252f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis ++NumErrors; 253f2224d89a6ae65a3839529e26d0f6d025d83d6bbArgyrios Kyrtzidis} 254f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner 255af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// ModifierIs - Return true if the specified modifier matches specified string. 256af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnertemplate <std::size_t StrLen> 257af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnerstatic bool ModifierIs(const char *Modifier, unsigned ModifierLen, 258af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner const char (&Str)[StrLen]) { 259af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); 260af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner} 261af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner 262909c182f6a0e6169475eef6a71add14555b6162cJohn McCall/// ScanForward - Scans forward, looking for the given character, skipping 263909c182f6a0e6169475eef6a71add14555b6162cJohn McCall/// nested clauses and escaped characters. 264909c182f6a0e6169475eef6a71add14555b6162cJohn McCallstatic const char *ScanFormat(const char *I, const char *E, char Target) { 265909c182f6a0e6169475eef6a71add14555b6162cJohn McCall unsigned Depth = 0; 266909c182f6a0e6169475eef6a71add14555b6162cJohn McCall 267909c182f6a0e6169475eef6a71add14555b6162cJohn McCall for ( ; I != E; ++I) { 268909c182f6a0e6169475eef6a71add14555b6162cJohn McCall if (Depth == 0 && *I == Target) return I; 269909c182f6a0e6169475eef6a71add14555b6162cJohn McCall if (Depth != 0 && *I == '}') Depth--; 270909c182f6a0e6169475eef6a71add14555b6162cJohn McCall 271909c182f6a0e6169475eef6a71add14555b6162cJohn McCall if (*I == '%') { 272909c182f6a0e6169475eef6a71add14555b6162cJohn McCall I++; 273909c182f6a0e6169475eef6a71add14555b6162cJohn McCall if (I == E) break; 274909c182f6a0e6169475eef6a71add14555b6162cJohn McCall 275909c182f6a0e6169475eef6a71add14555b6162cJohn McCall // Escaped characters get implicitly skipped here. 276909c182f6a0e6169475eef6a71add14555b6162cJohn McCall 277909c182f6a0e6169475eef6a71add14555b6162cJohn McCall // Format specifier. 278909c182f6a0e6169475eef6a71add14555b6162cJohn McCall if (!isdigit(*I) && !ispunct(*I)) { 279909c182f6a0e6169475eef6a71add14555b6162cJohn McCall for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ; 280909c182f6a0e6169475eef6a71add14555b6162cJohn McCall if (I == E) break; 281909c182f6a0e6169475eef6a71add14555b6162cJohn McCall if (*I == '{') 282909c182f6a0e6169475eef6a71add14555b6162cJohn McCall Depth++; 283909c182f6a0e6169475eef6a71add14555b6162cJohn McCall } 284909c182f6a0e6169475eef6a71add14555b6162cJohn McCall } 285909c182f6a0e6169475eef6a71add14555b6162cJohn McCall } 286909c182f6a0e6169475eef6a71add14555b6162cJohn McCall return E; 287909c182f6a0e6169475eef6a71add14555b6162cJohn McCall} 288909c182f6a0e6169475eef6a71add14555b6162cJohn McCall 289af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// HandleSelectModifier - Handle the integer 'select' modifier. This is used 290af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// like this: %select{foo|bar|baz}2. This means that the integer argument 291af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. 292af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. 293af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// This is very useful for certain classes of variant diagnostics. 2949f28614bf1a8387000d8df57a713fcf69e198145John McCallstatic void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo, 295af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner const char *Argument, unsigned ArgumentLen, 296af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner llvm::SmallVectorImpl<char> &OutStr) { 297af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner const char *ArgumentEnd = Argument+ArgumentLen; 2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 299af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // Skip over 'ValNo' |'s. 300af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner while (ValNo) { 301909c182f6a0e6169475eef6a71add14555b6162cJohn McCall const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|'); 302af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner assert(NextVal != ArgumentEnd && "Value for integer select modifier was" 303af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner " larger than the number of options in the diagnostic string!"); 304af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner Argument = NextVal+1; // Skip this string. 305af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner --ValNo; 306af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } 3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 308af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // Get the end of the value. This is either the } or the |. 309909c182f6a0e6169475eef6a71add14555b6162cJohn McCall const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|'); 3109f28614bf1a8387000d8df57a713fcf69e198145John McCall 3119f28614bf1a8387000d8df57a713fcf69e198145John McCall // Recursively format the result of the select clause into the output string. 3129f28614bf1a8387000d8df57a713fcf69e198145John McCall DInfo.FormatDiagnostic(Argument, EndPtr, OutStr); 313af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner} 314af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner 315af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the 316af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// letter 's' to the string if the value is not 1. This is used in cases like 317af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner/// this: "you idiot, you have %4 parameter%s4!". 318af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattnerstatic void HandleIntegerSModifier(unsigned ValNo, 319af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner llvm::SmallVectorImpl<char> &OutStr) { 320af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner if (ValNo != 1) 321af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner OutStr.push_back('s'); 322af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner} 323af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner 3243be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// HandleOrdinalModifier - Handle the integer 'ord' modifier. This 3253be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// prints the ordinal form of the given integer, with 1 corresponding 3263be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// to the first ordinal. Currently this is hard-coded to use the 3273be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall/// English form. 3283be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCallstatic void HandleOrdinalModifier(unsigned ValNo, 3293be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall llvm::SmallVectorImpl<char> &OutStr) { 3303be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall assert(ValNo != 0 && "ValNo must be strictly positive!"); 3313be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall 3323be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall llvm::raw_svector_ostream Out(OutStr); 3333be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall 3343be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall // We could use text forms for the first N ordinals, but the numeric 3353be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall // forms are actually nicer in diagnostics because they stand out. 3363be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall Out << ValNo; 3373be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall 3383be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall // It is critically important that we do this perfectly for 3393be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall // user-written sequences with over 100 elements. 3403be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall switch (ValNo % 100) { 3413be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall case 11: 3423be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall case 12: 3433be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall case 13: 3443be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall Out << "th"; return; 3453be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall default: 3463be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall switch (ValNo % 10) { 3473be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall case 1: Out << "st"; return; 3483be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall case 2: Out << "nd"; return; 3493be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall case 3: Out << "rd"; return; 3503be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall default: Out << "th"; return; 3513be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall } 3523be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall } 3533be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall} 3543be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall 355af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner 356e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// PluralNumber - Parse an unsigned integer and advance Start. 357d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic unsigned PluralNumber(const char *&Start, const char *End) { 358e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl // Programming 101: Parse a decimal number :-) 359e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl unsigned Val = 0; 360e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl while (Start != End && *Start >= '0' && *Start <= '9') { 361e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl Val *= 10; 362e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl Val += *Start - '0'; 363e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl ++Start; 364e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } 365e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl return Val; 366e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl} 367e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl 368e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// TestPluralRange - Test if Val is in the parsed range. Modifies Start. 369d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { 370e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl if (*Start != '[') { 371e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl unsigned Ref = PluralNumber(Start, End); 372e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl return Ref == Val; 373e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } 374e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl 375e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl ++Start; 376e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl unsigned Low = PluralNumber(Start, End); 377e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl assert(*Start == ',' && "Bad plural expression syntax: expected ,"); 378e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl ++Start; 379e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl unsigned High = PluralNumber(Start, End); 380e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl assert(*Start == ']' && "Bad plural expression syntax: expected )"); 381e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl ++Start; 382e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl return Low <= Val && Val <= High; 383e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl} 384e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl 385e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. 386d2aa7c90e7646c509f3493fa8548635ccf4a2d0aChris Lattnerstatic bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { 387e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl // Empty condition? 388e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl if (*Start == ':') 389e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl return true; 390e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl 391e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl while (1) { 392e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl char C = *Start; 393e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl if (C == '%') { 394e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl // Modulo expression 395e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl ++Start; 396e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl unsigned Arg = PluralNumber(Start, End); 397e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl assert(*Start == '=' && "Bad plural expression syntax: expected ="); 398e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl ++Start; 399e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl unsigned ValMod = ValNo % Arg; 400e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl if (TestPluralRange(ValMod, Start, End)) 401e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl return true; 402e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } else { 403e20653219732b03294130999415fc3aa92d2336aSebastian Redl assert((C == '[' || (C >= '0' && C <= '9')) && 404e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl "Bad plural expression syntax: unexpected character"); 405e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl // Range expression 406e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl if (TestPluralRange(ValNo, Start, End)) 407e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl return true; 408e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } 409e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl 410e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl // Scan for next or-expr part. 411e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl Start = std::find(Start, End, ','); 4121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (Start == End) 413e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl break; 414e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl ++Start; 415e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } 416e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl return false; 417e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl} 418e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl 419e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// HandlePluralModifier - Handle the integer 'plural' modifier. This is used 420e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// for complex plural forms, or in languages where all plurals are complex. 421e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are 422e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// conditions that are tested in order, the form corresponding to the first 423e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// that applies being emitted. The empty condition is always true, making the 424e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// last form a default case. 425e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Conditions are simple boolean expressions, where n is the number argument. 426e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Here are the rules. 427e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// condition := expression | empty 428e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// empty := -> always true 429e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// expression := numeric [',' expression] -> logical or 430e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// numeric := range -> true if n in range 431e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// | '%' number '=' range -> true if n % number in range 432e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// range := number 433e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// | '[' number ',' number ']' -> ranges are inclusive both ends 434e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// 435e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Here are some examples from the GNU gettext manual written in this form: 436e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// English: 437e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|:form1} 438e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Latvian: 439e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} 440e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Gaeilge: 441e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|2:form1|:form2} 442e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Romanian: 443e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|0,%100=[1,19]:form1|:form2} 444e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Lithuanian: 445e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} 446e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Russian (requires repeated form): 447e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} 448e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Slovak 449e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|[2,4]:form1|:form2} 450e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// Polish (requires repeated form): 451e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl/// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} 452e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCallstatic void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo, 453e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl const char *Argument, unsigned ArgumentLen, 454b54b276a920246c595a0498da281821eb9d22996Chris Lattner llvm::SmallVectorImpl<char> &OutStr) { 455e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl const char *ArgumentEnd = Argument + ArgumentLen; 456e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl while (1) { 457e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl assert(Argument < ArgumentEnd && "Plural expression didn't match."); 458e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl const char *ExprEnd = Argument; 459e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl while (*ExprEnd != ':') { 460e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); 461e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl ++ExprEnd; 462e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } 463e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { 464e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl Argument = ExprEnd + 1; 465909c182f6a0e6169475eef6a71add14555b6162cJohn McCall ExprEnd = ScanFormat(Argument, ArgumentEnd, '|'); 466e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall 467e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall // Recursively format the result of the plural clause into the 468e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall // output string. 469e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr); 470e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl return; 471e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } 472909c182f6a0e6169475eef6a71add14555b6162cJohn McCall Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1; 473e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } 474e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl} 475e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl 476e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl 477f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// FormatDiagnostic - Format this diagnostic into a string, substituting the 478f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// formal arguments into the %0 slots. The result is appended onto the Str 479f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner/// array. 480f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattnervoid DiagnosticInfo:: 481f4c839657742b823cea1a95b18422f1ba74d3dddChris LattnerFormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { 48233e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis const char *DiagStr = getDiags()->getDiagnosticIDs()->getDescription(getID()); 483f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner const char *DiagEnd = DiagStr+strlen(DiagStr); 4841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4859f28614bf1a8387000d8df57a713fcf69e198145John McCall FormatDiagnostic(DiagStr, DiagEnd, OutStr); 4869f28614bf1a8387000d8df57a713fcf69e198145John McCall} 4879f28614bf1a8387000d8df57a713fcf69e198145John McCall 4889f28614bf1a8387000d8df57a713fcf69e198145John McCallvoid DiagnosticInfo:: 4899f28614bf1a8387000d8df57a713fcf69e198145John McCallFormatDiagnostic(const char *DiagStr, const char *DiagEnd, 4909f28614bf1a8387000d8df57a713fcf69e198145John McCall llvm::SmallVectorImpl<char> &OutStr) const { 4919f28614bf1a8387000d8df57a713fcf69e198145John McCall 492b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner /// FormattedArgs - Keep track of all of the arguments formatted by 493b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner /// ConvertArgToString and pass them into subsequent calls to 494b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner /// ConvertArgToString, allowing the implementation to avoid redundancies in 495b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner /// obvious cases. 496b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs; 497b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner 498f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner while (DiagStr != DiagEnd) { 499f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner if (DiagStr[0] != '%') { 500f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner // Append non-%0 substrings to Str if we have one. 501f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); 502f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner OutStr.append(DiagStr, StrEnd); 503f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner DiagStr = StrEnd; 504af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner continue; 505909c182f6a0e6169475eef6a71add14555b6162cJohn McCall } else if (ispunct(DiagStr[1])) { 506909c182f6a0e6169475eef6a71add14555b6162cJohn McCall OutStr.push_back(DiagStr[1]); // %% -> %. 507f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner DiagStr += 2; 508af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner continue; 509af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } 5101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 511af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // Skip the %. 512af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner ++DiagStr; 5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 514af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // This must be a placeholder for a diagnostic argument. The format for a 515af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". 516af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // The digit is a number from 0-9 indicating which argument this comes from. 517af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // The modifier is a string of digits from the set [-a-z]+, arguments is a 518af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // brace enclosed string. 519af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner const char *Modifier = 0, *Argument = 0; 520af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner unsigned ModifierLen = 0, ArgumentLen = 0; 5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 522af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // Check to see if we have a modifier. If so eat it. 523af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner if (!isdigit(DiagStr[0])) { 524af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner Modifier = DiagStr; 525af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner while (DiagStr[0] == '-' || 526af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) 527af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner ++DiagStr; 528af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner ModifierLen = DiagStr-Modifier; 529f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner 530af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner // If we have an argument, get it next. 531af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner if (DiagStr[0] == '{') { 532af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner ++DiagStr; // Skip {. 533af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner Argument = DiagStr; 5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 535909c182f6a0e6169475eef6a71add14555b6162cJohn McCall DiagStr = ScanFormat(DiagStr, DiagEnd, '}'); 536909c182f6a0e6169475eef6a71add14555b6162cJohn McCall assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!"); 537af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner ArgumentLen = DiagStr-Argument; 538af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner ++DiagStr; // Skip }. 539f4c839657742b823cea1a95b18422f1ba74d3dddChris Lattner } 540af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } 5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 542af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); 54322caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner unsigned ArgNo = *DiagStr++ - '0'; 544af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner 545b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner Diagnostic::ArgumentKind Kind = getArgKind(ArgNo); 546b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner 547b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner switch (Kind) { 54808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner // ---- STRINGS ---- 5493cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner case Diagnostic::ak_std_string: { 55022caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner const std::string &S = getArgStdStr(ArgNo); 551af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner assert(ModifierLen == 0 && "No modifiers for strings yet"); 552af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner OutStr.append(S.begin(), S.end()); 553af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner break; 554af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } 5553cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner case Diagnostic::ak_c_string: { 55622caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner const char *S = getArgCStr(ArgNo); 557af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner assert(ModifierLen == 0 && "No modifiers for strings yet"); 558e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar 559e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar // Don't crash if get passed a null pointer by accident. 560e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar if (!S) 561e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar S = "(null)"; 5621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 563af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner OutStr.append(S, S + strlen(S)); 564af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner break; 565af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } 56608631c5fa053867146b5ee8be658c229f6bf127cChris Lattner // ---- INTEGERS ---- 5673cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner case Diagnostic::ak_sint: { 56822caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner int Val = getArgSInt(ArgNo); 5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 570af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner if (ModifierIs(Modifier, ModifierLen, "select")) { 571e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, 572e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall OutStr); 573af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } else if (ModifierIs(Modifier, ModifierLen, "s")) { 574af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner HandleIntegerSModifier(Val, OutStr); 575e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } else if (ModifierIs(Modifier, ModifierLen, "plural")) { 576e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, 577e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall OutStr); 5783be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { 5793be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall HandleOrdinalModifier((unsigned)Val, OutStr); 580af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } else { 581af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner assert(ModifierLen == 0 && "Unknown integer modifier"); 58223e47c6b6e8ccdd8daa378ab2a879644425c72d8Daniel Dunbar llvm::raw_svector_ostream(OutStr) << Val; 58330bc96544346bea42921cf6837e66cef80d664b4Chris Lattner } 584af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner break; 585af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } 5863cbfe2c4159e0a219ae660d50625c013aa4afbd0Chris Lattner case Diagnostic::ak_uint: { 58722caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner unsigned Val = getArgUInt(ArgNo); 5881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 589af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner if (ModifierIs(Modifier, ModifierLen, "select")) { 5909f28614bf1a8387000d8df57a713fcf69e198145John McCall HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr); 591af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } else if (ModifierIs(Modifier, ModifierLen, "s")) { 592af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner HandleIntegerSModifier(Val, OutStr); 593e4c452c4c7b9124fe94a96f559ff077d59cdf996Sebastian Redl } else if (ModifierIs(Modifier, ModifierLen, "plural")) { 594e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, 595e53a44bcd342e964a3c69bc27734f01e23f5fec8John McCall OutStr); 5963be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { 5973be16b7d9d0ab075461ed3498b4c01b30b517c0eJohn McCall HandleOrdinalModifier(Val, OutStr); 598af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } else { 599af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner assert(ModifierLen == 0 && "Unknown integer modifier"); 60023e47c6b6e8ccdd8daa378ab2a879644425c72d8Daniel Dunbar llvm::raw_svector_ostream(OutStr) << Val; 60130bc96544346bea42921cf6837e66cef80d664b4Chris Lattner } 60222caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner break; 603af7ae4e8160fc5c23e471f2125b3fe5911e3532aChris Lattner } 60408631c5fa053867146b5ee8be658c229f6bf127cChris Lattner // ---- NAMES and TYPES ---- 60508631c5fa053867146b5ee8be658c229f6bf127cChris Lattner case Diagnostic::ak_identifierinfo: { 60608631c5fa053867146b5ee8be658c229f6bf127cChris Lattner const IdentifierInfo *II = getArgIdentifier(ArgNo); 60708631c5fa053867146b5ee8be658c229f6bf127cChris Lattner assert(ModifierLen == 0 && "No modifiers for strings yet"); 608e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar 609e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar // Don't crash if get passed a null pointer by accident. 610e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar if (!II) { 611e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar const char *S = "(null)"; 612e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar OutStr.append(S, S + strlen(S)); 613e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar continue; 614e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar } 615e46e354ecb9a04c8d3724ae2b0f95f4424e3f69cDaniel Dunbar 61601eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\''; 61708631c5fa053867146b5ee8be658c229f6bf127cChris Lattner break; 61808631c5fa053867146b5ee8be658c229f6bf127cChris Lattner } 61922caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner case Diagnostic::ak_qualtype: 620011bb4edf731d529da1cbf71c7c2696aaf5a054fChris Lattner case Diagnostic::ak_declarationname: 62147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor case Diagnostic::ak_nameddecl: 622dacd434c49658286c380c7b4c357d76d53cb4aa1Douglas Gregor case Diagnostic::ak_nestednamespec: 6233f09327b26033d0a9676d52d80cf92c48f581affDouglas Gregor case Diagnostic::ak_declcontext: 624b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo), 6253fdf4b071dc79fae778fb5f376485480756c76a3Chris Lattner Modifier, ModifierLen, 626b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner Argument, ArgumentLen, 627b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner FormattedArgs.data(), FormattedArgs.size(), 628b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner OutStr); 62922caddc91d2f6186739c6b20ec58ed38cd68e595Chris Lattner break; 6307bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber } 631b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner 632b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner // Remember this argument info for subsequent formatting operations. Turn 633b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner // std::strings into a null terminated string to make it be the same case as 634b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner // all the other ones. 635b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner if (Kind != Diagnostic::ak_std_string) 636b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo))); 637b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner else 638b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string, 639b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner (intptr_t)getArgStdStr(ArgNo).c_str())); 640b54d8af9a66cc20a6a9a9219c7eaea8df7ee7fd4Chris Lattner 6417bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber } 6427bfaaaecb3113f955db31e8d8a51acffd1bc0c27Nico Weber} 643cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek 644a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::StoredDiagnostic() { } 645a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor 646aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas GregorStoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID, 647a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor llvm::StringRef Message) 648a6a32e295379570b489cc041053d2cd53bcafd1cBenjamin Kramer : ID(ID), Level(Level), Loc(), Message(Message) { } 649a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor 650a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, 651a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor const DiagnosticInfo &Info) 652aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor : ID(Info.getID()), Level(Level) 653aa5f135f8db82b5e5fb1640fd51f8078e0b2d82dDouglas Gregor{ 65433e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) && 65533e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis "Valid source location without setting a source manager for diagnostic"); 65633e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis if (Info.getLocation().isValid()) 65733e4e70c8c0a17e0ccb7465d96556b077a68ecb1Argyrios Kyrtzidis Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager()); 658a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor llvm::SmallString<64> Message; 659a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor Info.FormatDiagnostic(Message); 660a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor this->Message.assign(Message.begin(), Message.end()); 661a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor 662a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor Ranges.reserve(Info.getNumRanges()); 663a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I) 664a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor Ranges.push_back(Info.getRange(I)); 665a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor 666849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor FixIts.reserve(Info.getNumFixItHints()); 667849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I) 668849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor FixIts.push_back(Info.getFixItHint(I)); 669a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor} 670a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor 671a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas GregorStoredDiagnostic::~StoredDiagnostic() { } 672a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor 673cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek/// IncludeInDiagnosticCounts - This method (whose default implementation 674cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek/// returns true) indicates whether the diagnostics handled by this 675cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek/// DiagnosticClient should be included in the number of diagnostics 676cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenek/// reported by Diagnostic. 677cabe66811fe43835b8c5a0854552768fc53261e3Ted Kremenekbool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; } 678fe6b2d481d91140923f4541f273b253291884214Douglas Gregor 679fe6b2d481d91140923f4541f273b253291884214Douglas GregorPartialDiagnostic::StorageAllocator::StorageAllocator() { 680fe6b2d481d91140923f4541f273b253291884214Douglas Gregor for (unsigned I = 0; I != NumCached; ++I) 681fe6b2d481d91140923f4541f273b253291884214Douglas Gregor FreeList[I] = Cached + I; 682fe6b2d481d91140923f4541f273b253291884214Douglas Gregor NumFreeListEntries = NumCached; 683fe6b2d481d91140923f4541f273b253291884214Douglas Gregor} 684fe6b2d481d91140923f4541f273b253291884214Douglas Gregor 685fe6b2d481d91140923f4541f273b253291884214Douglas GregorPartialDiagnostic::StorageAllocator::~StorageAllocator() { 686fe6b2d481d91140923f4541f273b253291884214Douglas Gregor assert(NumFreeListEntries == NumCached && "A partial is on the lamb"); 687fe6b2d481d91140923f4541f273b253291884214Douglas Gregor} 688