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