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