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