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