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