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