Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Diagnostic-related interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 15 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 16 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 03201fb | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CrashRecoveryContext.h" |
| 20 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 23 | static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT, |
| 24 | const char *Modifier, unsigned ML, |
| 25 | const char *Argument, unsigned ArgLen, |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 26 | const Diagnostic::ArgumentValue *PrevArgs, |
| 27 | unsigned NumPrevArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 28 | SmallVectorImpl<char> &Output, |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 29 | void *Cookie, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 30 | SmallVectorImpl<intptr_t> &QualTypeVals) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 31 | const char *Str = "<can't format argument>"; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 32 | Output.append(Str, Str+strlen(Str)); |
| 33 | } |
| 34 | |
| 35 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 36 | Diagnostic::Diagnostic(const llvm::IntrusiveRefCntPtr<DiagnosticIDs> &diags, |
| 37 | DiagnosticClient *client, bool ShouldOwnClient) |
| 38 | : Diags(diags), Client(client), OwnsDiagClient(ShouldOwnClient), |
| 39 | SourceMgr(0) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 40 | ArgToStringFn = DummyArgToStringFn; |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 41 | ArgToStringCookie = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 43 | AllExtensionsSilenced = 0; |
| 44 | IgnoreAllWarnings = false; |
| 45 | WarningsAsErrors = false; |
| 46 | ErrorsAsFatal = false; |
| 47 | SuppressSystemWarnings = false; |
| 48 | SuppressAllDiagnostics = false; |
| 49 | ShowOverloads = Ovl_All; |
| 50 | ExtBehavior = Ext_Ignore; |
| 51 | |
| 52 | ErrorLimit = 0; |
| 53 | TemplateBacktraceLimit = 0; |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 54 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 55 | Reset(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 58 | Diagnostic::~Diagnostic() { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 59 | if (OwnsDiagClient) |
| 60 | delete Client; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Douglas Gregor | 4f5e21e | 2011-01-31 22:04:05 +0000 | [diff] [blame] | 63 | void Diagnostic::setClient(DiagnosticClient *client, bool ShouldOwnClient) { |
| 64 | if (OwnsDiagClient && Client) |
| 65 | delete Client; |
| 66 | |
| 67 | Client = client; |
| 68 | OwnsDiagClient = ShouldOwnClient; |
| 69 | } |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 70 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 71 | void Diagnostic::pushMappings(SourceLocation Loc) { |
| 72 | DiagStateOnPushStack.push_back(GetCurDiagState()); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 75 | bool Diagnostic::popMappings(SourceLocation Loc) { |
| 76 | if (DiagStateOnPushStack.empty()) |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 77 | return false; |
| 78 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 79 | if (DiagStateOnPushStack.back() != GetCurDiagState()) { |
| 80 | // State changed at some point between push/pop. |
| 81 | PushDiagStatePoint(DiagStateOnPushStack.back(), Loc); |
| 82 | } |
| 83 | DiagStateOnPushStack.pop_back(); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 84 | return true; |
| 85 | } |
| 86 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 87 | void Diagnostic::Reset() { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 88 | ErrorOccurred = false; |
| 89 | FatalErrorOccurred = false; |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 90 | UnrecoverableErrorOccurred = false; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 91 | |
| 92 | NumWarnings = 0; |
| 93 | NumErrors = 0; |
| 94 | NumErrorsSuppressed = 0; |
Douglas Gregor | 85bea97 | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 96 | CurDiagID = ~0U; |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 97 | // Set LastDiagLevel to an "unset" state. If we set it to 'Ignored', notes |
| 98 | // using a Diagnostic associated to a translation unit that follow |
| 99 | // diagnostics from a Diagnostic associated to anoter t.u. will not be |
| 100 | // displayed. |
| 101 | LastDiagLevel = (DiagnosticIDs::Level)-1; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 102 | DelayedDiagID = 0; |
Argyrios Kyrtzidis | dc0a2da | 2011-03-26 18:58:17 +0000 | [diff] [blame] | 103 | |
| 104 | // Clear state related to #pragma diagnostic. |
| 105 | DiagStates.clear(); |
| 106 | DiagStatePoints.clear(); |
| 107 | DiagStateOnPushStack.clear(); |
| 108 | |
| 109 | // Create a DiagState and DiagStatePoint representing diagnostic changes |
| 110 | // through command-line. |
| 111 | DiagStates.push_back(DiagState()); |
| 112 | PushDiagStatePoint(&DiagStates.back(), SourceLocation()); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 113 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 115 | void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1, |
| 116 | StringRef Arg2) { |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 117 | if (DelayedDiagID) |
| 118 | return; |
| 119 | |
| 120 | DelayedDiagID = DiagID; |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 121 | DelayedDiagArg1 = Arg1.str(); |
| 122 | DelayedDiagArg2 = Arg2.str(); |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void Diagnostic::ReportDelayed() { |
| 126 | Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2; |
| 127 | DelayedDiagID = 0; |
| 128 | DelayedDiagArg1.clear(); |
| 129 | DelayedDiagArg2.clear(); |
| 130 | } |
| 131 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 132 | Diagnostic::DiagStatePointsTy::iterator |
| 133 | Diagnostic::GetDiagStatePointForLoc(SourceLocation L) const { |
| 134 | assert(!DiagStatePoints.empty()); |
| 135 | assert(DiagStatePoints.front().Loc.isInvalid() && |
| 136 | "Should have created a DiagStatePoint for command-line"); |
| 137 | |
| 138 | FullSourceLoc Loc(L, *SourceMgr); |
| 139 | if (Loc.isInvalid()) |
| 140 | return DiagStatePoints.end() - 1; |
| 141 | |
| 142 | DiagStatePointsTy::iterator Pos = DiagStatePoints.end(); |
| 143 | FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; |
| 144 | if (LastStateChangePos.isValid() && |
| 145 | Loc.isBeforeInTranslationUnitThan(LastStateChangePos)) |
| 146 | Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(), |
| 147 | DiagStatePoint(0, Loc)); |
| 148 | --Pos; |
| 149 | return Pos; |
| 150 | } |
| 151 | |
| 152 | /// \brief This allows the client to specify that certain |
| 153 | /// warnings are ignored. Notes can never be mapped, errors can only be |
| 154 | /// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily. |
| 155 | /// |
| 156 | /// \param The source location that this change of diagnostic state should |
| 157 | /// take affect. It can be null if we are setting the latest state. |
| 158 | void Diagnostic::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map, |
| 159 | SourceLocation L) { |
| 160 | assert(Diag < diag::DIAG_UPPER_LIMIT && |
| 161 | "Can only map builtin diagnostics"); |
| 162 | assert((Diags->isBuiltinWarningOrExtension(Diag) || |
| 163 | (Map == diag::MAP_FATAL || Map == diag::MAP_ERROR)) && |
| 164 | "Cannot map errors into warnings!"); |
| 165 | assert(!DiagStatePoints.empty()); |
| 166 | |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 167 | bool isPragma = L.isValid(); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 168 | FullSourceLoc Loc(L, *SourceMgr); |
| 169 | FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; |
| 170 | |
| 171 | // Common case; setting all the diagnostics of a group in one place. |
| 172 | if (Loc.isInvalid() || Loc == LastStateChangePos) { |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 173 | setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
| 177 | // Another common case; modifying diagnostic state in a source location |
| 178 | // after the previous one. |
| 179 | if ((Loc.isValid() && LastStateChangePos.isInvalid()) || |
| 180 | LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) { |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 181 | // A diagnostic pragma occurred, create a new DiagState initialized with |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 182 | // the current one and a new DiagStatePoint to record at which location |
| 183 | // the new state became active. |
| 184 | DiagStates.push_back(*GetCurDiagState()); |
| 185 | PushDiagStatePoint(&DiagStates.back(), Loc); |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 186 | setDiagnosticMappingInternal(Diag, Map, GetCurDiagState(), true, isPragma); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
| 190 | // We allow setting the diagnostic state in random source order for |
| 191 | // completeness but it should not be actually happening in normal practice. |
| 192 | |
| 193 | DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc); |
| 194 | assert(Pos != DiagStatePoints.end()); |
| 195 | |
| 196 | // Update all diagnostic states that are active after the given location. |
| 197 | for (DiagStatePointsTy::iterator |
| 198 | I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) { |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 199 | setDiagnosticMappingInternal(Diag, Map, I->State, true, isPragma); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // If the location corresponds to an existing point, just update its state. |
| 203 | if (Pos->Loc == Loc) { |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 204 | setDiagnosticMappingInternal(Diag, Map, Pos->State, true, isPragma); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | |
| 208 | // Create a new state/point and fit it into the vector of DiagStatePoints |
| 209 | // so that the vector is always ordered according to location. |
| 210 | Pos->Loc.isBeforeInTranslationUnitThan(Loc); |
| 211 | DiagStates.push_back(*Pos->State); |
| 212 | DiagState *NewState = &DiagStates.back(); |
Argyrios Kyrtzidis | 3efd52c | 2011-01-14 20:54:07 +0000 | [diff] [blame] | 213 | setDiagnosticMappingInternal(Diag, Map, NewState, true, isPragma); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 214 | DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState, |
| 215 | FullSourceLoc(Loc, *SourceMgr))); |
| 216 | } |
| 217 | |
Argyrios Kyrtzidis | e59abb5 | 2011-05-05 07:54:59 +0000 | [diff] [blame] | 218 | void Diagnostic::Report(const StoredDiagnostic &storedDiag) { |
| 219 | assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!"); |
| 220 | |
| 221 | CurDiagLoc = storedDiag.getLocation(); |
| 222 | CurDiagID = storedDiag.getID(); |
| 223 | NumDiagArgs = 0; |
| 224 | |
| 225 | NumDiagRanges = storedDiag.range_size(); |
| 226 | assert(NumDiagRanges < sizeof(DiagRanges)/sizeof(DiagRanges[0]) && |
| 227 | "Too many arguments to diagnostic!"); |
| 228 | unsigned i = 0; |
| 229 | for (StoredDiagnostic::range_iterator |
| 230 | RI = storedDiag.range_begin(), |
| 231 | RE = storedDiag.range_end(); RI != RE; ++RI) |
| 232 | DiagRanges[i++] = *RI; |
| 233 | |
| 234 | NumFixItHints = storedDiag.fixit_size(); |
| 235 | assert(NumFixItHints < Diagnostic::MaxFixItHints && "Too many fix-it hints!"); |
| 236 | i = 0; |
| 237 | for (StoredDiagnostic::fixit_iterator |
| 238 | FI = storedDiag.fixit_begin(), |
| 239 | FE = storedDiag.fixit_end(); FI != FE; ++FI) |
| 240 | FixItHints[i++] = *FI; |
| 241 | |
| 242 | assert(Client && "DiagnosticClient not set!"); |
| 243 | Level DiagLevel = storedDiag.getLevel(); |
| 244 | DiagnosticInfo Info(this, storedDiag.getMessage()); |
| 245 | Client->HandleDiagnostic(DiagLevel, Info); |
| 246 | if (Client->IncludeInDiagnosticCounts()) { |
| 247 | if (DiagLevel == Diagnostic::Warning) |
| 248 | ++NumWarnings; |
| 249 | } |
| 250 | |
| 251 | CurDiagID = ~0U; |
| 252 | } |
| 253 | |
Douglas Gregor | b535041 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 254 | void DiagnosticBuilder::FlushCounts() { |
| 255 | DiagObj->NumDiagArgs = NumArgs; |
| 256 | DiagObj->NumDiagRanges = NumRanges; |
| 257 | DiagObj->NumFixItHints = NumFixItHints; |
| 258 | } |
| 259 | |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 260 | bool DiagnosticBuilder::Emit() { |
| 261 | // If DiagObj is null, then its soul was stolen by the copy ctor |
| 262 | // or the user called Emit(). |
| 263 | if (DiagObj == 0) return false; |
| 264 | |
| 265 | // When emitting diagnostics, we set the final argument count into |
| 266 | // the Diagnostic object. |
Douglas Gregor | b535041 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 267 | FlushCounts(); |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 268 | |
| 269 | // Process the diagnostic, sending the accumulated information to the |
| 270 | // DiagnosticClient. |
| 271 | bool Emitted = DiagObj->ProcessDiag(); |
| 272 | |
| 273 | // Clear out the current diagnostic object. |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 274 | unsigned DiagID = DiagObj->CurDiagID; |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 275 | DiagObj->Clear(); |
| 276 | |
| 277 | // If there was a delayed diagnostic, emit it now. |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 278 | if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID) |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 279 | DiagObj->ReportDelayed(); |
| 280 | |
| 281 | // This diagnostic is dead. |
| 282 | DiagObj = 0; |
| 283 | |
| 284 | return Emitted; |
| 285 | } |
| 286 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 287 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | DiagnosticClient::~DiagnosticClient() {} |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 289 | |
Argyrios Kyrtzidis | f2224d8 | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 290 | void DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 291 | const DiagnosticInfo &Info) { |
| 292 | if (!IncludeInDiagnosticCounts()) |
| 293 | return; |
| 294 | |
| 295 | if (DiagLevel == Diagnostic::Warning) |
| 296 | ++NumWarnings; |
| 297 | else if (DiagLevel >= Diagnostic::Error) |
| 298 | ++NumErrors; |
| 299 | } |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 300 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 301 | /// ModifierIs - Return true if the specified modifier matches specified string. |
| 302 | template <std::size_t StrLen> |
| 303 | static bool ModifierIs(const char *Modifier, unsigned ModifierLen, |
| 304 | const char (&Str)[StrLen]) { |
| 305 | return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); |
| 306 | } |
| 307 | |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 308 | /// ScanForward - Scans forward, looking for the given character, skipping |
| 309 | /// nested clauses and escaped characters. |
| 310 | static const char *ScanFormat(const char *I, const char *E, char Target) { |
| 311 | unsigned Depth = 0; |
| 312 | |
| 313 | for ( ; I != E; ++I) { |
| 314 | if (Depth == 0 && *I == Target) return I; |
| 315 | if (Depth != 0 && *I == '}') Depth--; |
| 316 | |
| 317 | if (*I == '%') { |
| 318 | I++; |
| 319 | if (I == E) break; |
| 320 | |
| 321 | // Escaped characters get implicitly skipped here. |
| 322 | |
| 323 | // Format specifier. |
| 324 | if (!isdigit(*I) && !ispunct(*I)) { |
| 325 | for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ; |
| 326 | if (I == E) break; |
| 327 | if (*I == '{') |
| 328 | Depth++; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | return E; |
| 333 | } |
| 334 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 335 | /// HandleSelectModifier - Handle the integer 'select' modifier. This is used |
| 336 | /// like this: %select{foo|bar|baz}2. This means that the integer argument |
| 337 | /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. |
| 338 | /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. |
| 339 | /// This is very useful for certain classes of variant diagnostics. |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 340 | static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo, |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 341 | const char *Argument, unsigned ArgumentLen, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 342 | SmallVectorImpl<char> &OutStr) { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 343 | const char *ArgumentEnd = Argument+ArgumentLen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 345 | // Skip over 'ValNo' |'s. |
| 346 | while (ValNo) { |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 347 | const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|'); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 348 | assert(NextVal != ArgumentEnd && "Value for integer select modifier was" |
| 349 | " larger than the number of options in the diagnostic string!"); |
| 350 | Argument = NextVal+1; // Skip this string. |
| 351 | --ValNo; |
| 352 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 354 | // Get the end of the value. This is either the } or the |. |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 355 | const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|'); |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 356 | |
| 357 | // Recursively format the result of the select clause into the output string. |
| 358 | DInfo.FormatDiagnostic(Argument, EndPtr, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the |
| 362 | /// letter 's' to the string if the value is not 1. This is used in cases like |
| 363 | /// this: "you idiot, you have %4 parameter%s4!". |
| 364 | static void HandleIntegerSModifier(unsigned ValNo, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 365 | SmallVectorImpl<char> &OutStr) { |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 366 | if (ValNo != 1) |
| 367 | OutStr.push_back('s'); |
| 368 | } |
| 369 | |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 370 | /// HandleOrdinalModifier - Handle the integer 'ord' modifier. This |
| 371 | /// prints the ordinal form of the given integer, with 1 corresponding |
| 372 | /// to the first ordinal. Currently this is hard-coded to use the |
| 373 | /// English form. |
| 374 | static void HandleOrdinalModifier(unsigned ValNo, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 375 | SmallVectorImpl<char> &OutStr) { |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 376 | assert(ValNo != 0 && "ValNo must be strictly positive!"); |
| 377 | |
| 378 | llvm::raw_svector_ostream Out(OutStr); |
| 379 | |
| 380 | // We could use text forms for the first N ordinals, but the numeric |
| 381 | // forms are actually nicer in diagnostics because they stand out. |
| 382 | Out << ValNo; |
| 383 | |
| 384 | // It is critically important that we do this perfectly for |
| 385 | // user-written sequences with over 100 elements. |
| 386 | switch (ValNo % 100) { |
| 387 | case 11: |
| 388 | case 12: |
| 389 | case 13: |
| 390 | Out << "th"; return; |
| 391 | default: |
| 392 | switch (ValNo % 10) { |
| 393 | case 1: Out << "st"; return; |
| 394 | case 2: Out << "nd"; return; |
| 395 | case 3: Out << "rd"; return; |
| 396 | default: Out << "th"; return; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 401 | |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 402 | /// PluralNumber - Parse an unsigned integer and advance Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 403 | static unsigned PluralNumber(const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 404 | // Programming 101: Parse a decimal number :-) |
| 405 | unsigned Val = 0; |
| 406 | while (Start != End && *Start >= '0' && *Start <= '9') { |
| 407 | Val *= 10; |
| 408 | Val += *Start - '0'; |
| 409 | ++Start; |
| 410 | } |
| 411 | return Val; |
| 412 | } |
| 413 | |
| 414 | /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 415 | static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 416 | if (*Start != '[') { |
| 417 | unsigned Ref = PluralNumber(Start, End); |
| 418 | return Ref == Val; |
| 419 | } |
| 420 | |
| 421 | ++Start; |
| 422 | unsigned Low = PluralNumber(Start, End); |
| 423 | assert(*Start == ',' && "Bad plural expression syntax: expected ,"); |
| 424 | ++Start; |
| 425 | unsigned High = PluralNumber(Start, End); |
| 426 | assert(*Start == ']' && "Bad plural expression syntax: expected )"); |
| 427 | ++Start; |
| 428 | return Low <= Val && Val <= High; |
| 429 | } |
| 430 | |
| 431 | /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 432 | static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 433 | // Empty condition? |
| 434 | if (*Start == ':') |
| 435 | return true; |
| 436 | |
| 437 | while (1) { |
| 438 | char C = *Start; |
| 439 | if (C == '%') { |
| 440 | // Modulo expression |
| 441 | ++Start; |
| 442 | unsigned Arg = PluralNumber(Start, End); |
| 443 | assert(*Start == '=' && "Bad plural expression syntax: expected ="); |
| 444 | ++Start; |
| 445 | unsigned ValMod = ValNo % Arg; |
| 446 | if (TestPluralRange(ValMod, Start, End)) |
| 447 | return true; |
| 448 | } else { |
Sebastian Redl | e206532 | 2008-11-27 07:28:14 +0000 | [diff] [blame] | 449 | assert((C == '[' || (C >= '0' && C <= '9')) && |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 450 | "Bad plural expression syntax: unexpected character"); |
| 451 | // Range expression |
| 452 | if (TestPluralRange(ValNo, Start, End)) |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | // Scan for next or-expr part. |
| 457 | Start = std::find(Start, End, ','); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | if (Start == End) |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 459 | break; |
| 460 | ++Start; |
| 461 | } |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used |
| 466 | /// for complex plural forms, or in languages where all plurals are complex. |
| 467 | /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are |
| 468 | /// conditions that are tested in order, the form corresponding to the first |
| 469 | /// that applies being emitted. The empty condition is always true, making the |
| 470 | /// last form a default case. |
| 471 | /// Conditions are simple boolean expressions, where n is the number argument. |
| 472 | /// Here are the rules. |
| 473 | /// condition := expression | empty |
| 474 | /// empty := -> always true |
| 475 | /// expression := numeric [',' expression] -> logical or |
| 476 | /// numeric := range -> true if n in range |
| 477 | /// | '%' number '=' range -> true if n % number in range |
| 478 | /// range := number |
| 479 | /// | '[' number ',' number ']' -> ranges are inclusive both ends |
| 480 | /// |
| 481 | /// Here are some examples from the GNU gettext manual written in this form: |
| 482 | /// English: |
| 483 | /// {1:form0|:form1} |
| 484 | /// Latvian: |
| 485 | /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} |
| 486 | /// Gaeilge: |
| 487 | /// {1:form0|2:form1|:form2} |
| 488 | /// Romanian: |
| 489 | /// {1:form0|0,%100=[1,19]:form1|:form2} |
| 490 | /// Lithuanian: |
| 491 | /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} |
| 492 | /// Russian (requires repeated form): |
| 493 | /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} |
| 494 | /// Slovak |
| 495 | /// {1:form0|[2,4]:form1|:form2} |
| 496 | /// Polish (requires repeated form): |
| 497 | /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 498 | static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo, |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 499 | const char *Argument, unsigned ArgumentLen, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 500 | SmallVectorImpl<char> &OutStr) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 501 | const char *ArgumentEnd = Argument + ArgumentLen; |
| 502 | while (1) { |
| 503 | assert(Argument < ArgumentEnd && "Plural expression didn't match."); |
| 504 | const char *ExprEnd = Argument; |
| 505 | while (*ExprEnd != ':') { |
| 506 | assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); |
| 507 | ++ExprEnd; |
| 508 | } |
| 509 | if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { |
| 510 | Argument = ExprEnd + 1; |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 511 | ExprEnd = ScanFormat(Argument, ArgumentEnd, '|'); |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 512 | |
| 513 | // Recursively format the result of the plural clause into the |
| 514 | // output string. |
| 515 | DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 516 | return; |
| 517 | } |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 518 | Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 523 | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
| 524 | /// formal arguments into the %0 slots. The result is appended onto the Str |
| 525 | /// array. |
| 526 | void DiagnosticInfo:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 527 | FormatDiagnostic(SmallVectorImpl<char> &OutStr) const { |
Argyrios Kyrtzidis | e59abb5 | 2011-05-05 07:54:59 +0000 | [diff] [blame] | 528 | if (!StoredDiagMessage.empty()) { |
| 529 | OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end()); |
| 530 | return; |
| 531 | } |
| 532 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 533 | StringRef Diag = |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 534 | getDiags()->getDiagnosticIDs()->getDescription(getID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Argyrios Kyrtzidis | 477aab6 | 2011-05-25 05:05:01 +0000 | [diff] [blame] | 536 | FormatDiagnostic(Diag.begin(), Diag.end(), OutStr); |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | void DiagnosticInfo:: |
| 540 | FormatDiagnostic(const char *DiagStr, const char *DiagEnd, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 541 | SmallVectorImpl<char> &OutStr) const { |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 542 | |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 543 | /// FormattedArgs - Keep track of all of the arguments formatted by |
| 544 | /// ConvertArgToString and pass them into subsequent calls to |
| 545 | /// ConvertArgToString, allowing the implementation to avoid redundancies in |
| 546 | /// obvious cases. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 547 | SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs; |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 548 | |
| 549 | /// QualTypeVals - Pass a vector of arrays so that QualType names can be |
| 550 | /// compared to see if more information is needed to be printed. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 551 | SmallVector<intptr_t, 2> QualTypeVals; |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 552 | for (unsigned i = 0, e = getNumArgs(); i < e; ++i) |
| 553 | if (getArgKind(i) == Diagnostic::ak_qualtype) |
| 554 | QualTypeVals.push_back(getRawArg(i)); |
| 555 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 556 | while (DiagStr != DiagEnd) { |
| 557 | if (DiagStr[0] != '%') { |
| 558 | // Append non-%0 substrings to Str if we have one. |
| 559 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
| 560 | OutStr.append(DiagStr, StrEnd); |
| 561 | DiagStr = StrEnd; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 562 | continue; |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 563 | } else if (ispunct(DiagStr[1])) { |
| 564 | OutStr.push_back(DiagStr[1]); // %% -> %. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 565 | DiagStr += 2; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 566 | continue; |
| 567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 569 | // Skip the %. |
| 570 | ++DiagStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 572 | // This must be a placeholder for a diagnostic argument. The format for a |
| 573 | // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". |
| 574 | // The digit is a number from 0-9 indicating which argument this comes from. |
| 575 | // The modifier is a string of digits from the set [-a-z]+, arguments is a |
| 576 | // brace enclosed string. |
| 577 | const char *Modifier = 0, *Argument = 0; |
| 578 | unsigned ModifierLen = 0, ArgumentLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 580 | // Check to see if we have a modifier. If so eat it. |
| 581 | if (!isdigit(DiagStr[0])) { |
| 582 | Modifier = DiagStr; |
| 583 | while (DiagStr[0] == '-' || |
| 584 | (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) |
| 585 | ++DiagStr; |
| 586 | ModifierLen = DiagStr-Modifier; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 587 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 588 | // If we have an argument, get it next. |
| 589 | if (DiagStr[0] == '{') { |
| 590 | ++DiagStr; // Skip {. |
| 591 | Argument = DiagStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 593 | DiagStr = ScanFormat(DiagStr, DiagEnd, '}'); |
| 594 | assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!"); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 595 | ArgumentLen = DiagStr-Argument; |
| 596 | ++DiagStr; // Skip }. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 597 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 598 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 600 | assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 601 | unsigned ArgNo = *DiagStr++ - '0'; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 602 | |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 603 | Diagnostic::ArgumentKind Kind = getArgKind(ArgNo); |
| 604 | |
| 605 | switch (Kind) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 606 | // ---- STRINGS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 607 | case Diagnostic::ak_std_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 608 | const std::string &S = getArgStdStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 609 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 610 | OutStr.append(S.begin(), S.end()); |
| 611 | break; |
| 612 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 613 | case Diagnostic::ak_c_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 614 | const char *S = getArgCStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 615 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Daniel Dunbar | e46e354 | 2009-04-20 06:13:16 +0000 | [diff] [blame] | 616 | |
| 617 | // Don't crash if get passed a null pointer by accident. |
| 618 | if (!S) |
| 619 | S = "(null)"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 621 | OutStr.append(S, S + strlen(S)); |
| 622 | break; |
| 623 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 624 | // ---- INTEGERS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 625 | case Diagnostic::ak_sint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 626 | int Val = getArgSInt(ArgNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 628 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 629 | HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 630 | OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 631 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 632 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 633 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 634 | HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 635 | OutStr); |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 636 | } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { |
| 637 | HandleOrdinalModifier((unsigned)Val, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 638 | } else { |
| 639 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 640 | llvm::raw_svector_ostream(OutStr) << Val; |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 641 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 642 | break; |
| 643 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 644 | case Diagnostic::ak_uint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 645 | unsigned Val = getArgUInt(ArgNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 647 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 648 | HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 649 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 650 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 651 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 652 | HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 653 | OutStr); |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 654 | } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { |
| 655 | HandleOrdinalModifier(Val, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 656 | } else { |
| 657 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 658 | llvm::raw_svector_ostream(OutStr) << Val; |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 659 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 660 | break; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 661 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 662 | // ---- NAMES and TYPES ---- |
| 663 | case Diagnostic::ak_identifierinfo: { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 664 | const IdentifierInfo *II = getArgIdentifier(ArgNo); |
| 665 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Daniel Dunbar | e46e354 | 2009-04-20 06:13:16 +0000 | [diff] [blame] | 666 | |
| 667 | // Don't crash if get passed a null pointer by accident. |
| 668 | if (!II) { |
| 669 | const char *S = "(null)"; |
| 670 | OutStr.append(S, S + strlen(S)); |
| 671 | continue; |
| 672 | } |
| 673 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 674 | llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\''; |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 675 | break; |
| 676 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 677 | case Diagnostic::ak_qualtype: |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 678 | case Diagnostic::ak_declarationname: |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 679 | case Diagnostic::ak_nameddecl: |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 680 | case Diagnostic::ak_nestednamespec: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 681 | case Diagnostic::ak_declcontext: |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 682 | getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo), |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 683 | Modifier, ModifierLen, |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 684 | Argument, ArgumentLen, |
| 685 | FormattedArgs.data(), FormattedArgs.size(), |
Chandler Carruth | 0673cb3 | 2011-07-11 17:49:21 +0000 | [diff] [blame] | 686 | OutStr, QualTypeVals); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 687 | break; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 689 | |
| 690 | // Remember this argument info for subsequent formatting operations. Turn |
| 691 | // std::strings into a null terminated string to make it be the same case as |
| 692 | // all the other ones. |
| 693 | if (Kind != Diagnostic::ak_std_string) |
| 694 | FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo))); |
| 695 | else |
| 696 | FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string, |
| 697 | (intptr_t)getArgStdStr(ArgNo).c_str())); |
| 698 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 699 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 700 | } |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 701 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 702 | StoredDiagnostic::StoredDiagnostic() { } |
| 703 | |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 704 | StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 705 | StringRef Message) |
Benjamin Kramer | a6a32e2 | 2010-11-19 17:36:51 +0000 | [diff] [blame] | 706 | : ID(ID), Level(Level), Loc(), Message(Message) { } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 707 | |
| 708 | StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, |
| 709 | const DiagnosticInfo &Info) |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 710 | : ID(Info.getID()), Level(Level) |
| 711 | { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 712 | assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) && |
| 713 | "Valid source location without setting a source manager for diagnostic"); |
| 714 | if (Info.getLocation().isValid()) |
| 715 | Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager()); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 716 | llvm::SmallString<64> Message; |
| 717 | Info.FormatDiagnostic(Message); |
| 718 | this->Message.assign(Message.begin(), Message.end()); |
| 719 | |
| 720 | Ranges.reserve(Info.getNumRanges()); |
| 721 | for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I) |
| 722 | Ranges.push_back(Info.getRange(I)); |
| 723 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 724 | FixIts.reserve(Info.getNumFixItHints()); |
| 725 | for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I) |
| 726 | FixIts.push_back(Info.getFixItHint(I)); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 729 | StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, unsigned ID, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame^] | 730 | StringRef Message, FullSourceLoc Loc, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 731 | llvm::ArrayRef<CharSourceRange> Ranges, |
| 732 | llvm::ArrayRef<FixItHint> Fixits) |
| 733 | : ID(ID), Level(Level), Loc(Loc), Message(Message) |
| 734 | { |
| 735 | this->Ranges.assign(Ranges.begin(), Ranges.end()); |
| 736 | this->FixIts.assign(FixIts.begin(), FixIts.end()); |
| 737 | } |
| 738 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 739 | StoredDiagnostic::~StoredDiagnostic() { } |
| 740 | |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 741 | /// IncludeInDiagnosticCounts - This method (whose default implementation |
| 742 | /// returns true) indicates whether the diagnostics handled by this |
| 743 | /// DiagnosticClient should be included in the number of diagnostics |
| 744 | /// reported by Diagnostic. |
| 745 | bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; } |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 746 | |
| 747 | PartialDiagnostic::StorageAllocator::StorageAllocator() { |
| 748 | for (unsigned I = 0; I != NumCached; ++I) |
| 749 | FreeList[I] = Cached + I; |
| 750 | NumFreeListEntries = NumCached; |
| 751 | } |
| 752 | |
| 753 | PartialDiagnostic::StorageAllocator::~StorageAllocator() { |
Ted Kremenek | 03201fb | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 754 | // Don't assert if we are in a CrashRecovery context, as this |
| 755 | // invariant may be invalidated during a crash. |
Ted Kremenek | cd1eecf | 2011-03-21 18:40:10 +0000 | [diff] [blame] | 756 | assert((NumFreeListEntries == NumCached || llvm::CrashRecoveryContext::isRecoveringFromCrash()) && "A partial is on the lamb"); |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 757 | } |