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