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