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