/src/llvm-project/clang/lib/Basic/Diagnostic.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- Diagnostic.cpp - C Language Family Diagnostic Handling -------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements the Diagnostic-related interfaces. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/Basic/Diagnostic.h" |
14 | | #include "clang/Basic/CharInfo.h" |
15 | | #include "clang/Basic/DiagnosticError.h" |
16 | | #include "clang/Basic/DiagnosticIDs.h" |
17 | | #include "clang/Basic/DiagnosticOptions.h" |
18 | | #include "clang/Basic/IdentifierTable.h" |
19 | | #include "clang/Basic/PartialDiagnostic.h" |
20 | | #include "clang/Basic/SourceLocation.h" |
21 | | #include "clang/Basic/SourceManager.h" |
22 | | #include "clang/Basic/Specifiers.h" |
23 | | #include "clang/Basic/TokenKinds.h" |
24 | | #include "llvm/ADT/SmallString.h" |
25 | | #include "llvm/ADT/SmallVector.h" |
26 | | #include "llvm/ADT/StringExtras.h" |
27 | | #include "llvm/ADT/StringRef.h" |
28 | | #include "llvm/Support/ConvertUTF.h" |
29 | | #include "llvm/Support/CrashRecoveryContext.h" |
30 | | #include "llvm/Support/Unicode.h" |
31 | | #include "llvm/Support/raw_ostream.h" |
32 | | #include <algorithm> |
33 | | #include <cassert> |
34 | | #include <cstddef> |
35 | | #include <cstdint> |
36 | | #include <cstring> |
37 | | #include <limits> |
38 | | #include <string> |
39 | | #include <utility> |
40 | | #include <vector> |
41 | | |
42 | | using namespace clang; |
43 | | |
44 | | const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB, |
45 | 0 | DiagNullabilityKind nullability) { |
46 | 0 | DB.AddString( |
47 | 0 | ("'" + |
48 | 0 | getNullabilitySpelling(nullability.first, |
49 | 0 | /*isContextSensitive=*/nullability.second) + |
50 | 0 | "'") |
51 | 0 | .str()); |
52 | 0 | return DB; |
53 | 0 | } |
54 | | |
55 | | const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB, |
56 | 0 | llvm::Error &&E) { |
57 | 0 | DB.AddString(toString(std::move(E))); |
58 | 0 | return DB; |
59 | 0 | } |
60 | | |
61 | | static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT, |
62 | | StringRef Modifier, StringRef Argument, |
63 | | ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs, |
64 | | SmallVectorImpl<char> &Output, |
65 | | void *Cookie, |
66 | 0 | ArrayRef<intptr_t> QualTypeVals) { |
67 | 0 | StringRef Str = "<can't format argument>"; |
68 | 0 | Output.append(Str.begin(), Str.end()); |
69 | 0 | } |
70 | | |
71 | | DiagnosticsEngine::DiagnosticsEngine( |
72 | | IntrusiveRefCntPtr<DiagnosticIDs> diags, |
73 | | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts, DiagnosticConsumer *client, |
74 | | bool ShouldOwnClient) |
75 | 2.03k | : Diags(std::move(diags)), DiagOpts(std::move(DiagOpts)) { |
76 | 2.03k | setClient(client, ShouldOwnClient); |
77 | 2.03k | ArgToStringFn = DummyArgToStringFn; |
78 | | |
79 | 2.03k | Reset(); |
80 | 2.03k | } |
81 | | |
82 | 2.03k | DiagnosticsEngine::~DiagnosticsEngine() { |
83 | | // If we own the diagnostic client, destroy it first so that it can access the |
84 | | // engine from its destructor. |
85 | 2.03k | setClient(nullptr); |
86 | 2.03k | } |
87 | | |
88 | 0 | void DiagnosticsEngine::dump() const { |
89 | 0 | DiagStatesByLoc.dump(*SourceMgr); |
90 | 0 | } |
91 | | |
92 | 0 | void DiagnosticsEngine::dump(StringRef DiagName) const { |
93 | 0 | DiagStatesByLoc.dump(*SourceMgr, DiagName); |
94 | 0 | } |
95 | | |
96 | | void DiagnosticsEngine::setClient(DiagnosticConsumer *client, |
97 | 5.50k | bool ShouldOwnClient) { |
98 | 5.50k | Owner.reset(ShouldOwnClient ? client : nullptr); |
99 | 5.50k | Client = client; |
100 | 5.50k | } |
101 | | |
102 | 0 | void DiagnosticsEngine::pushMappings(SourceLocation Loc) { |
103 | 0 | DiagStateOnPushStack.push_back(GetCurDiagState()); |
104 | 0 | } |
105 | | |
106 | 0 | bool DiagnosticsEngine::popMappings(SourceLocation Loc) { |
107 | 0 | if (DiagStateOnPushStack.empty()) |
108 | 0 | return false; |
109 | | |
110 | 0 | if (DiagStateOnPushStack.back() != GetCurDiagState()) { |
111 | | // State changed at some point between push/pop. |
112 | 0 | PushDiagStatePoint(DiagStateOnPushStack.back(), Loc); |
113 | 0 | } |
114 | 0 | DiagStateOnPushStack.pop_back(); |
115 | 0 | return true; |
116 | 0 | } |
117 | | |
118 | 2.03k | void DiagnosticsEngine::Reset(bool soft /*=false*/) { |
119 | 2.03k | ErrorOccurred = false; |
120 | 2.03k | UncompilableErrorOccurred = false; |
121 | 2.03k | FatalErrorOccurred = false; |
122 | 2.03k | UnrecoverableErrorOccurred = false; |
123 | | |
124 | 2.03k | NumWarnings = 0; |
125 | 2.03k | NumErrors = 0; |
126 | 2.03k | TrapNumErrorsOccurred = 0; |
127 | 2.03k | TrapNumUnrecoverableErrorsOccurred = 0; |
128 | | |
129 | 2.03k | CurDiagID = std::numeric_limits<unsigned>::max(); |
130 | 2.03k | LastDiagLevel = DiagnosticIDs::Ignored; |
131 | 2.03k | DelayedDiagID = 0; |
132 | | |
133 | 2.03k | if (!soft) { |
134 | | // Clear state related to #pragma diagnostic. |
135 | 2.03k | DiagStates.clear(); |
136 | 2.03k | DiagStatesByLoc.clear(); |
137 | 2.03k | DiagStateOnPushStack.clear(); |
138 | | |
139 | | // Create a DiagState and DiagStatePoint representing diagnostic changes |
140 | | // through command-line. |
141 | 2.03k | DiagStates.emplace_back(); |
142 | 2.03k | DiagStatesByLoc.appendFirst(&DiagStates.back()); |
143 | 2.03k | } |
144 | 2.03k | } |
145 | | |
146 | | void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1, |
147 | 0 | StringRef Arg2, StringRef Arg3) { |
148 | 0 | if (DelayedDiagID) |
149 | 0 | return; |
150 | | |
151 | 0 | DelayedDiagID = DiagID; |
152 | 0 | DelayedDiagArg1 = Arg1.str(); |
153 | 0 | DelayedDiagArg2 = Arg2.str(); |
154 | 0 | DelayedDiagArg3 = Arg3.str(); |
155 | 0 | } |
156 | | |
157 | 0 | void DiagnosticsEngine::ReportDelayed() { |
158 | 0 | unsigned ID = DelayedDiagID; |
159 | 0 | DelayedDiagID = 0; |
160 | 0 | Report(ID) << DelayedDiagArg1 << DelayedDiagArg2 << DelayedDiagArg3; |
161 | 0 | } |
162 | | |
163 | | DiagnosticMapping & |
164 | 12.8M | DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) { |
165 | 12.8M | std::pair<iterator, bool> Result = |
166 | 12.8M | DiagMap.insert(std::make_pair(Diag, DiagnosticMapping())); |
167 | | |
168 | | // Initialize the entry if we added it. |
169 | 12.8M | if (Result.second) |
170 | 2.17k | Result.first->second = DiagnosticIDs::getDefaultMapping(Diag); |
171 | | |
172 | 12.8M | return Result.first->second; |
173 | 12.8M | } |
174 | | |
175 | 2.03k | void DiagnosticsEngine::DiagStateMap::appendFirst(DiagState *State) { |
176 | 2.03k | assert(Files.empty() && "not first"); |
177 | 0 | FirstDiagState = CurDiagState = State; |
178 | 2.03k | CurDiagStateLoc = SourceLocation(); |
179 | 2.03k | } |
180 | | |
181 | | void DiagnosticsEngine::DiagStateMap::append(SourceManager &SrcMgr, |
182 | | SourceLocation Loc, |
183 | 0 | DiagState *State) { |
184 | 0 | CurDiagState = State; |
185 | 0 | CurDiagStateLoc = Loc; |
186 | |
|
187 | 0 | std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc); |
188 | 0 | unsigned Offset = Decomp.second; |
189 | 0 | for (File *F = getFile(SrcMgr, Decomp.first); F; |
190 | 0 | Offset = F->ParentOffset, F = F->Parent) { |
191 | 0 | F->HasLocalTransitions = true; |
192 | 0 | auto &Last = F->StateTransitions.back(); |
193 | 0 | assert(Last.Offset <= Offset && "state transitions added out of order"); |
194 | | |
195 | 0 | if (Last.Offset == Offset) { |
196 | 0 | if (Last.State == State) |
197 | 0 | break; |
198 | 0 | Last.State = State; |
199 | 0 | continue; |
200 | 0 | } |
201 | | |
202 | 0 | F->StateTransitions.push_back({State, Offset}); |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | DiagnosticsEngine::DiagState * |
207 | | DiagnosticsEngine::DiagStateMap::lookup(SourceManager &SrcMgr, |
208 | 12.8M | SourceLocation Loc) const { |
209 | | // Common case: we have not seen any diagnostic pragmas. |
210 | 12.8M | if (Files.empty()) |
211 | 12.8M | return FirstDiagState; |
212 | | |
213 | 0 | std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedLoc(Loc); |
214 | 0 | const File *F = getFile(SrcMgr, Decomp.first); |
215 | 0 | return F->lookup(Decomp.second); |
216 | 12.8M | } |
217 | | |
218 | | DiagnosticsEngine::DiagState * |
219 | 0 | DiagnosticsEngine::DiagStateMap::File::lookup(unsigned Offset) const { |
220 | 0 | auto OnePastIt = |
221 | 0 | llvm::partition_point(StateTransitions, [=](const DiagStatePoint &P) { |
222 | 0 | return P.Offset <= Offset; |
223 | 0 | }); |
224 | 0 | assert(OnePastIt != StateTransitions.begin() && "missing initial state"); |
225 | 0 | return OnePastIt[-1].State; |
226 | 0 | } |
227 | | |
228 | | DiagnosticsEngine::DiagStateMap::File * |
229 | | DiagnosticsEngine::DiagStateMap::getFile(SourceManager &SrcMgr, |
230 | 0 | FileID ID) const { |
231 | | // Get or insert the File for this ID. |
232 | 0 | auto Range = Files.equal_range(ID); |
233 | 0 | if (Range.first != Range.second) |
234 | 0 | return &Range.first->second; |
235 | 0 | auto &F = Files.insert(Range.first, std::make_pair(ID, File()))->second; |
236 | | |
237 | | // We created a new File; look up the diagnostic state at the start of it and |
238 | | // initialize it. |
239 | 0 | if (ID.isValid()) { |
240 | 0 | std::pair<FileID, unsigned> Decomp = SrcMgr.getDecomposedIncludedLoc(ID); |
241 | 0 | F.Parent = getFile(SrcMgr, Decomp.first); |
242 | 0 | F.ParentOffset = Decomp.second; |
243 | 0 | F.StateTransitions.push_back({F.Parent->lookup(Decomp.second), 0}); |
244 | 0 | } else { |
245 | | // This is the (imaginary) root file into which we pretend all top-level |
246 | | // files are included; it descends from the initial state. |
247 | | // |
248 | | // FIXME: This doesn't guarantee that we use the same ordering as |
249 | | // isBeforeInTranslationUnit in the cases where someone invented another |
250 | | // top-level file and added diagnostic pragmas to it. See the code at the |
251 | | // end of isBeforeInTranslationUnit for the quirks it deals with. |
252 | 0 | F.StateTransitions.push_back({FirstDiagState, 0}); |
253 | 0 | } |
254 | 0 | return &F; |
255 | 0 | } |
256 | | |
257 | | void DiagnosticsEngine::DiagStateMap::dump(SourceManager &SrcMgr, |
258 | 0 | StringRef DiagName) const { |
259 | 0 | llvm::errs() << "diagnostic state at "; |
260 | 0 | CurDiagStateLoc.print(llvm::errs(), SrcMgr); |
261 | 0 | llvm::errs() << ": " << CurDiagState << "\n"; |
262 | |
|
263 | 0 | for (auto &F : Files) { |
264 | 0 | FileID ID = F.first; |
265 | 0 | File &File = F.second; |
266 | |
|
267 | 0 | bool PrintedOuterHeading = false; |
268 | 0 | auto PrintOuterHeading = [&] { |
269 | 0 | if (PrintedOuterHeading) return; |
270 | 0 | PrintedOuterHeading = true; |
271 | |
|
272 | 0 | llvm::errs() << "File " << &File << " <FileID " << ID.getHashValue() |
273 | 0 | << ">: " << SrcMgr.getBufferOrFake(ID).getBufferIdentifier(); |
274 | |
|
275 | 0 | if (F.second.Parent) { |
276 | 0 | std::pair<FileID, unsigned> Decomp = |
277 | 0 | SrcMgr.getDecomposedIncludedLoc(ID); |
278 | 0 | assert(File.ParentOffset == Decomp.second); |
279 | 0 | llvm::errs() << " parent " << File.Parent << " <FileID " |
280 | 0 | << Decomp.first.getHashValue() << "> "; |
281 | 0 | SrcMgr.getLocForStartOfFile(Decomp.first) |
282 | 0 | .getLocWithOffset(Decomp.second) |
283 | 0 | .print(llvm::errs(), SrcMgr); |
284 | 0 | } |
285 | 0 | if (File.HasLocalTransitions) |
286 | 0 | llvm::errs() << " has_local_transitions"; |
287 | 0 | llvm::errs() << "\n"; |
288 | 0 | }; |
289 | |
|
290 | 0 | if (DiagName.empty()) |
291 | 0 | PrintOuterHeading(); |
292 | |
|
293 | 0 | for (DiagStatePoint &Transition : File.StateTransitions) { |
294 | 0 | bool PrintedInnerHeading = false; |
295 | 0 | auto PrintInnerHeading = [&] { |
296 | 0 | if (PrintedInnerHeading) return; |
297 | 0 | PrintedInnerHeading = true; |
298 | |
|
299 | 0 | PrintOuterHeading(); |
300 | 0 | llvm::errs() << " "; |
301 | 0 | SrcMgr.getLocForStartOfFile(ID) |
302 | 0 | .getLocWithOffset(Transition.Offset) |
303 | 0 | .print(llvm::errs(), SrcMgr); |
304 | 0 | llvm::errs() << ": state " << Transition.State << ":\n"; |
305 | 0 | }; |
306 | |
|
307 | 0 | if (DiagName.empty()) |
308 | 0 | PrintInnerHeading(); |
309 | |
|
310 | 0 | for (auto &Mapping : *Transition.State) { |
311 | 0 | StringRef Option = |
312 | 0 | DiagnosticIDs::getWarningOptionForDiag(Mapping.first); |
313 | 0 | if (!DiagName.empty() && DiagName != Option) |
314 | 0 | continue; |
315 | | |
316 | 0 | PrintInnerHeading(); |
317 | 0 | llvm::errs() << " "; |
318 | 0 | if (Option.empty()) |
319 | 0 | llvm::errs() << "<unknown " << Mapping.first << ">"; |
320 | 0 | else |
321 | 0 | llvm::errs() << Option; |
322 | 0 | llvm::errs() << ": "; |
323 | |
|
324 | 0 | switch (Mapping.second.getSeverity()) { |
325 | 0 | case diag::Severity::Ignored: llvm::errs() << "ignored"; break; |
326 | 0 | case diag::Severity::Remark: llvm::errs() << "remark"; break; |
327 | 0 | case diag::Severity::Warning: llvm::errs() << "warning"; break; |
328 | 0 | case diag::Severity::Error: llvm::errs() << "error"; break; |
329 | 0 | case diag::Severity::Fatal: llvm::errs() << "fatal"; break; |
330 | 0 | } |
331 | | |
332 | 0 | if (!Mapping.second.isUser()) |
333 | 0 | llvm::errs() << " default"; |
334 | 0 | if (Mapping.second.isPragma()) |
335 | 0 | llvm::errs() << " pragma"; |
336 | 0 | if (Mapping.second.hasNoWarningAsError()) |
337 | 0 | llvm::errs() << " no-error"; |
338 | 0 | if (Mapping.second.hasNoErrorAsFatal()) |
339 | 0 | llvm::errs() << " no-fatal"; |
340 | 0 | if (Mapping.second.wasUpgradedFromWarning()) |
341 | 0 | llvm::errs() << " overruled"; |
342 | 0 | llvm::errs() << "\n"; |
343 | 0 | } |
344 | 0 | } |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | | void DiagnosticsEngine::PushDiagStatePoint(DiagState *State, |
349 | 0 | SourceLocation Loc) { |
350 | 0 | assert(Loc.isValid() && "Adding invalid loc point"); |
351 | 0 | DiagStatesByLoc.append(*SourceMgr, Loc, State); |
352 | 0 | } |
353 | | |
354 | | void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map, |
355 | 0 | SourceLocation L) { |
356 | 0 | assert(Diag < diag::DIAG_UPPER_LIMIT && |
357 | 0 | "Can only map builtin diagnostics"); |
358 | 0 | assert((Diags->isBuiltinWarningOrExtension(Diag) || |
359 | 0 | (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) && |
360 | 0 | "Cannot map errors into warnings!"); |
361 | 0 | assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location"); |
362 | | |
363 | | // Don't allow a mapping to a warning override an error/fatal mapping. |
364 | 0 | bool WasUpgradedFromWarning = false; |
365 | 0 | if (Map == diag::Severity::Warning) { |
366 | 0 | DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag); |
367 | 0 | if (Info.getSeverity() == diag::Severity::Error || |
368 | 0 | Info.getSeverity() == diag::Severity::Fatal) { |
369 | 0 | Map = Info.getSeverity(); |
370 | 0 | WasUpgradedFromWarning = true; |
371 | 0 | } |
372 | 0 | } |
373 | 0 | DiagnosticMapping Mapping = makeUserMapping(Map, L); |
374 | 0 | Mapping.setUpgradedFromWarning(WasUpgradedFromWarning); |
375 | | |
376 | | // Make sure we propagate the NoWarningAsError flag from an existing |
377 | | // mapping (which may be the default mapping). |
378 | 0 | DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag); |
379 | 0 | Mapping.setNoWarningAsError(Info.hasNoWarningAsError() || |
380 | 0 | Mapping.hasNoWarningAsError()); |
381 | | |
382 | | // Common case; setting all the diagnostics of a group in one place. |
383 | 0 | if ((L.isInvalid() || L == DiagStatesByLoc.getCurDiagStateLoc()) && |
384 | 0 | DiagStatesByLoc.getCurDiagState()) { |
385 | | // FIXME: This is theoretically wrong: if the current state is shared with |
386 | | // some other location (via push/pop) we will change the state for that |
387 | | // other location as well. This cannot currently happen, as we can't update |
388 | | // the diagnostic state at the same location at which we pop. |
389 | 0 | DiagStatesByLoc.getCurDiagState()->setMapping(Diag, Mapping); |
390 | 0 | return; |
391 | 0 | } |
392 | | |
393 | | // A diagnostic pragma occurred, create a new DiagState initialized with |
394 | | // the current one and a new DiagStatePoint to record at which location |
395 | | // the new state became active. |
396 | 0 | DiagStates.push_back(*GetCurDiagState()); |
397 | 0 | DiagStates.back().setMapping(Diag, Mapping); |
398 | 0 | PushDiagStatePoint(&DiagStates.back(), L); |
399 | 0 | } |
400 | | |
401 | | bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor, |
402 | | StringRef Group, diag::Severity Map, |
403 | 0 | SourceLocation Loc) { |
404 | | // Get the diagnostics in this group. |
405 | 0 | SmallVector<diag::kind, 256> GroupDiags; |
406 | 0 | if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags)) |
407 | 0 | return true; |
408 | | |
409 | | // Set the mapping. |
410 | 0 | for (diag::kind Diag : GroupDiags) |
411 | 0 | setSeverity(Diag, Map, Loc); |
412 | |
|
413 | 0 | return false; |
414 | 0 | } |
415 | | |
416 | | bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor, |
417 | | diag::Group Group, |
418 | | diag::Severity Map, |
419 | 0 | SourceLocation Loc) { |
420 | 0 | return setSeverityForGroup(Flavor, Diags->getWarningOptionForGroup(Group), |
421 | 0 | Map, Loc); |
422 | 0 | } |
423 | | |
424 | | bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group, |
425 | 0 | bool Enabled) { |
426 | | // If we are enabling this feature, just set the diagnostic mappings to map to |
427 | | // errors. |
428 | 0 | if (Enabled) |
429 | 0 | return setSeverityForGroup(diag::Flavor::WarningOrError, Group, |
430 | 0 | diag::Severity::Error); |
431 | | |
432 | | // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and |
433 | | // potentially downgrade anything already mapped to be a warning. |
434 | | |
435 | | // Get the diagnostics in this group. |
436 | 0 | SmallVector<diag::kind, 8> GroupDiags; |
437 | 0 | if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group, |
438 | 0 | GroupDiags)) |
439 | 0 | return true; |
440 | | |
441 | | // Perform the mapping change. |
442 | 0 | for (diag::kind Diag : GroupDiags) { |
443 | 0 | DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag); |
444 | |
|
445 | 0 | if (Info.getSeverity() == diag::Severity::Error || |
446 | 0 | Info.getSeverity() == diag::Severity::Fatal) |
447 | 0 | Info.setSeverity(diag::Severity::Warning); |
448 | |
|
449 | 0 | Info.setNoWarningAsError(true); |
450 | 0 | } |
451 | |
|
452 | 0 | return false; |
453 | 0 | } |
454 | | |
455 | | bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group, |
456 | 0 | bool Enabled) { |
457 | | // If we are enabling this feature, just set the diagnostic mappings to map to |
458 | | // fatal errors. |
459 | 0 | if (Enabled) |
460 | 0 | return setSeverityForGroup(diag::Flavor::WarningOrError, Group, |
461 | 0 | diag::Severity::Fatal); |
462 | | |
463 | | // Otherwise, we want to set the diagnostic mapping's "no Wfatal-errors" bit, |
464 | | // and potentially downgrade anything already mapped to be a fatal error. |
465 | | |
466 | | // Get the diagnostics in this group. |
467 | 0 | SmallVector<diag::kind, 8> GroupDiags; |
468 | 0 | if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group, |
469 | 0 | GroupDiags)) |
470 | 0 | return true; |
471 | | |
472 | | // Perform the mapping change. |
473 | 0 | for (diag::kind Diag : GroupDiags) { |
474 | 0 | DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag); |
475 | |
|
476 | 0 | if (Info.getSeverity() == diag::Severity::Fatal) |
477 | 0 | Info.setSeverity(diag::Severity::Error); |
478 | |
|
479 | 0 | Info.setNoErrorAsFatal(true); |
480 | 0 | } |
481 | |
|
482 | 0 | return false; |
483 | 0 | } |
484 | | |
485 | | void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor, |
486 | | diag::Severity Map, |
487 | 0 | SourceLocation Loc) { |
488 | | // Get all the diagnostics. |
489 | 0 | std::vector<diag::kind> AllDiags; |
490 | 0 | DiagnosticIDs::getAllDiagnostics(Flavor, AllDiags); |
491 | | |
492 | | // Set the mapping. |
493 | 0 | for (diag::kind Diag : AllDiags) |
494 | 0 | if (Diags->isBuiltinWarningOrExtension(Diag)) |
495 | 0 | setSeverity(Diag, Map, Loc); |
496 | 0 | } |
497 | | |
498 | 0 | void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) { |
499 | 0 | assert(CurDiagID == std::numeric_limits<unsigned>::max() && |
500 | 0 | "Multiple diagnostics in flight at once!"); |
501 | | |
502 | 0 | CurDiagLoc = storedDiag.getLocation(); |
503 | 0 | CurDiagID = storedDiag.getID(); |
504 | 0 | DiagStorage.NumDiagArgs = 0; |
505 | |
|
506 | 0 | DiagStorage.DiagRanges.clear(); |
507 | 0 | DiagStorage.DiagRanges.append(storedDiag.range_begin(), |
508 | 0 | storedDiag.range_end()); |
509 | |
|
510 | 0 | DiagStorage.FixItHints.clear(); |
511 | 0 | DiagStorage.FixItHints.append(storedDiag.fixit_begin(), |
512 | 0 | storedDiag.fixit_end()); |
513 | |
|
514 | 0 | assert(Client && "DiagnosticConsumer not set!"); |
515 | 0 | Level DiagLevel = storedDiag.getLevel(); |
516 | 0 | Diagnostic Info(this, storedDiag.getMessage()); |
517 | 0 | Client->HandleDiagnostic(DiagLevel, Info); |
518 | 0 | if (Client->IncludeInDiagnosticCounts()) { |
519 | 0 | if (DiagLevel == DiagnosticsEngine::Warning) |
520 | 0 | ++NumWarnings; |
521 | 0 | } |
522 | |
|
523 | 0 | CurDiagID = std::numeric_limits<unsigned>::max(); |
524 | 0 | } |
525 | | |
526 | 12.4M | bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) { |
527 | 12.4M | assert(getClient() && "DiagnosticClient not set!"); |
528 | | |
529 | 0 | bool Emitted; |
530 | 12.4M | if (Force) { |
531 | 0 | Diagnostic Info(this); |
532 | | |
533 | | // Figure out the diagnostic level of this message. |
534 | 0 | DiagnosticIDs::Level DiagLevel |
535 | 0 | = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this); |
536 | |
|
537 | 0 | Emitted = (DiagLevel != DiagnosticIDs::Ignored); |
538 | 0 | if (Emitted) { |
539 | | // Emit the diagnostic regardless of suppression level. |
540 | 0 | Diags->EmitDiag(*this, DiagLevel); |
541 | 0 | } |
542 | 12.4M | } else { |
543 | | // Process the diagnostic, sending the accumulated information to the |
544 | | // DiagnosticConsumer. |
545 | 12.4M | Emitted = ProcessDiag(); |
546 | 12.4M | } |
547 | | |
548 | | // Clear out the current diagnostic object. |
549 | 12.4M | Clear(); |
550 | | |
551 | | // If there was a delayed diagnostic, emit it now. |
552 | 12.4M | if (!Force && DelayedDiagID) |
553 | 0 | ReportDelayed(); |
554 | | |
555 | 12.4M | return Emitted; |
556 | 12.4M | } |
557 | | |
558 | 1.44k | DiagnosticConsumer::~DiagnosticConsumer() = default; |
559 | | |
560 | | void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
561 | 0 | const Diagnostic &Info) { |
562 | 0 | if (!IncludeInDiagnosticCounts()) |
563 | 0 | return; |
564 | | |
565 | 0 | if (DiagLevel == DiagnosticsEngine::Warning) |
566 | 0 | ++NumWarnings; |
567 | 0 | else if (DiagLevel >= DiagnosticsEngine::Error) |
568 | 0 | ++NumErrors; |
569 | 0 | } |
570 | | |
571 | | /// ModifierIs - Return true if the specified modifier matches specified string. |
572 | | template <std::size_t StrLen> |
573 | | static bool ModifierIs(const char *Modifier, unsigned ModifierLen, |
574 | 0 | const char (&Str)[StrLen]) { |
575 | 0 | return StrLen-1 == ModifierLen && memcmp(Modifier, Str, StrLen-1) == 0; |
576 | 0 | } Unexecuted instantiation: Diagnostic.cpp:bool ModifierIs<5ul>(char const*, unsigned int, char const (&) [5ul]) Unexecuted instantiation: Diagnostic.cpp:bool ModifierIs<7ul>(char const*, unsigned int, char const (&) [7ul]) Unexecuted instantiation: Diagnostic.cpp:bool ModifierIs<2ul>(char const*, unsigned int, char const (&) [2ul]) Unexecuted instantiation: Diagnostic.cpp:bool ModifierIs<8ul>(char const*, unsigned int, char const (&) [8ul]) |
577 | | |
578 | | /// ScanForward - Scans forward, looking for the given character, skipping |
579 | | /// nested clauses and escaped characters. |
580 | 0 | static const char *ScanFormat(const char *I, const char *E, char Target) { |
581 | 0 | unsigned Depth = 0; |
582 | |
|
583 | 0 | for ( ; I != E; ++I) { |
584 | 0 | if (Depth == 0 && *I == Target) return I; |
585 | 0 | if (Depth != 0 && *I == '}') Depth--; |
586 | |
|
587 | 0 | if (*I == '%') { |
588 | 0 | I++; |
589 | 0 | if (I == E) break; |
590 | | |
591 | | // Escaped characters get implicitly skipped here. |
592 | | |
593 | | // Format specifier. |
594 | 0 | if (!isDigit(*I) && !isPunctuation(*I)) { |
595 | 0 | for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ; |
596 | 0 | if (I == E) break; |
597 | 0 | if (*I == '{') |
598 | 0 | Depth++; |
599 | 0 | } |
600 | 0 | } |
601 | 0 | } |
602 | 0 | return E; |
603 | 0 | } |
604 | | |
605 | | /// HandleSelectModifier - Handle the integer 'select' modifier. This is used |
606 | | /// like this: %select{foo|bar|baz}2. This means that the integer argument |
607 | | /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. |
608 | | /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. |
609 | | /// This is very useful for certain classes of variant diagnostics. |
610 | | static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo, |
611 | | const char *Argument, unsigned ArgumentLen, |
612 | 0 | SmallVectorImpl<char> &OutStr) { |
613 | 0 | const char *ArgumentEnd = Argument+ArgumentLen; |
614 | | |
615 | | // Skip over 'ValNo' |'s. |
616 | 0 | while (ValNo) { |
617 | 0 | const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|'); |
618 | 0 | assert(NextVal != ArgumentEnd && "Value for integer select modifier was" |
619 | 0 | " larger than the number of options in the diagnostic string!"); |
620 | 0 | Argument = NextVal+1; // Skip this string. |
621 | 0 | --ValNo; |
622 | 0 | } |
623 | | |
624 | | // Get the end of the value. This is either the } or the |. |
625 | 0 | const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|'); |
626 | | |
627 | | // Recursively format the result of the select clause into the output string. |
628 | 0 | DInfo.FormatDiagnostic(Argument, EndPtr, OutStr); |
629 | 0 | } |
630 | | |
631 | | /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the |
632 | | /// letter 's' to the string if the value is not 1. This is used in cases like |
633 | | /// this: "you idiot, you have %4 parameter%s4!". |
634 | | static void HandleIntegerSModifier(unsigned ValNo, |
635 | 0 | SmallVectorImpl<char> &OutStr) { |
636 | 0 | if (ValNo != 1) |
637 | 0 | OutStr.push_back('s'); |
638 | 0 | } |
639 | | |
640 | | /// HandleOrdinalModifier - Handle the integer 'ord' modifier. This |
641 | | /// prints the ordinal form of the given integer, with 1 corresponding |
642 | | /// to the first ordinal. Currently this is hard-coded to use the |
643 | | /// English form. |
644 | | static void HandleOrdinalModifier(unsigned ValNo, |
645 | 0 | SmallVectorImpl<char> &OutStr) { |
646 | 0 | assert(ValNo != 0 && "ValNo must be strictly positive!"); |
647 | | |
648 | 0 | llvm::raw_svector_ostream Out(OutStr); |
649 | | |
650 | | // We could use text forms for the first N ordinals, but the numeric |
651 | | // forms are actually nicer in diagnostics because they stand out. |
652 | 0 | Out << ValNo << llvm::getOrdinalSuffix(ValNo); |
653 | 0 | } |
654 | | |
655 | | /// PluralNumber - Parse an unsigned integer and advance Start. |
656 | 0 | static unsigned PluralNumber(const char *&Start, const char *End) { |
657 | | // Programming 101: Parse a decimal number :-) |
658 | 0 | unsigned Val = 0; |
659 | 0 | while (Start != End && *Start >= '0' && *Start <= '9') { |
660 | 0 | Val *= 10; |
661 | 0 | Val += *Start - '0'; |
662 | 0 | ++Start; |
663 | 0 | } |
664 | 0 | return Val; |
665 | 0 | } |
666 | | |
667 | | /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. |
668 | 0 | static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { |
669 | 0 | if (*Start != '[') { |
670 | 0 | unsigned Ref = PluralNumber(Start, End); |
671 | 0 | return Ref == Val; |
672 | 0 | } |
673 | | |
674 | 0 | ++Start; |
675 | 0 | unsigned Low = PluralNumber(Start, End); |
676 | 0 | assert(*Start == ',' && "Bad plural expression syntax: expected ,"); |
677 | 0 | ++Start; |
678 | 0 | unsigned High = PluralNumber(Start, End); |
679 | 0 | assert(*Start == ']' && "Bad plural expression syntax: expected )"); |
680 | 0 | ++Start; |
681 | 0 | return Low <= Val && Val <= High; |
682 | 0 | } |
683 | | |
684 | | /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. |
685 | 0 | static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { |
686 | | // Empty condition? |
687 | 0 | if (*Start == ':') |
688 | 0 | return true; |
689 | | |
690 | 0 | while (true) { |
691 | 0 | char C = *Start; |
692 | 0 | if (C == '%') { |
693 | | // Modulo expression |
694 | 0 | ++Start; |
695 | 0 | unsigned Arg = PluralNumber(Start, End); |
696 | 0 | assert(*Start == '=' && "Bad plural expression syntax: expected ="); |
697 | 0 | ++Start; |
698 | 0 | unsigned ValMod = ValNo % Arg; |
699 | 0 | if (TestPluralRange(ValMod, Start, End)) |
700 | 0 | return true; |
701 | 0 | } else { |
702 | 0 | assert((C == '[' || (C >= '0' && C <= '9')) && |
703 | 0 | "Bad plural expression syntax: unexpected character"); |
704 | | // Range expression |
705 | 0 | if (TestPluralRange(ValNo, Start, End)) |
706 | 0 | return true; |
707 | 0 | } |
708 | | |
709 | | // Scan for next or-expr part. |
710 | 0 | Start = std::find(Start, End, ','); |
711 | 0 | if (Start == End) |
712 | 0 | break; |
713 | 0 | ++Start; |
714 | 0 | } |
715 | 0 | return false; |
716 | 0 | } |
717 | | |
718 | | /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used |
719 | | /// for complex plural forms, or in languages where all plurals are complex. |
720 | | /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are |
721 | | /// conditions that are tested in order, the form corresponding to the first |
722 | | /// that applies being emitted. The empty condition is always true, making the |
723 | | /// last form a default case. |
724 | | /// Conditions are simple boolean expressions, where n is the number argument. |
725 | | /// Here are the rules. |
726 | | /// condition := expression | empty |
727 | | /// empty := -> always true |
728 | | /// expression := numeric [',' expression] -> logical or |
729 | | /// numeric := range -> true if n in range |
730 | | /// | '%' number '=' range -> true if n % number in range |
731 | | /// range := number |
732 | | /// | '[' number ',' number ']' -> ranges are inclusive both ends |
733 | | /// |
734 | | /// Here are some examples from the GNU gettext manual written in this form: |
735 | | /// English: |
736 | | /// {1:form0|:form1} |
737 | | /// Latvian: |
738 | | /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} |
739 | | /// Gaeilge: |
740 | | /// {1:form0|2:form1|:form2} |
741 | | /// Romanian: |
742 | | /// {1:form0|0,%100=[1,19]:form1|:form2} |
743 | | /// Lithuanian: |
744 | | /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} |
745 | | /// Russian (requires repeated form): |
746 | | /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} |
747 | | /// Slovak |
748 | | /// {1:form0|[2,4]:form1|:form2} |
749 | | /// Polish (requires repeated form): |
750 | | /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} |
751 | | static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo, |
752 | | const char *Argument, unsigned ArgumentLen, |
753 | 0 | SmallVectorImpl<char> &OutStr) { |
754 | 0 | const char *ArgumentEnd = Argument + ArgumentLen; |
755 | 0 | while (true) { |
756 | 0 | assert(Argument < ArgumentEnd && "Plural expression didn't match."); |
757 | 0 | const char *ExprEnd = Argument; |
758 | 0 | while (*ExprEnd != ':') { |
759 | 0 | assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); |
760 | 0 | ++ExprEnd; |
761 | 0 | } |
762 | 0 | if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { |
763 | 0 | Argument = ExprEnd + 1; |
764 | 0 | ExprEnd = ScanFormat(Argument, ArgumentEnd, '|'); |
765 | | |
766 | | // Recursively format the result of the plural clause into the |
767 | | // output string. |
768 | 0 | DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr); |
769 | 0 | return; |
770 | 0 | } |
771 | 0 | Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1; |
772 | 0 | } |
773 | 0 | } |
774 | | |
775 | | /// Returns the friendly description for a token kind that will appear |
776 | | /// without quotes in diagnostic messages. These strings may be translatable in |
777 | | /// future. |
778 | 0 | static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) { |
779 | 0 | switch (Kind) { |
780 | 0 | case tok::identifier: |
781 | 0 | return "identifier"; |
782 | 0 | default: |
783 | 0 | return nullptr; |
784 | 0 | } |
785 | 0 | } |
786 | | |
787 | | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
788 | | /// formal arguments into the %0 slots. The result is appended onto the Str |
789 | | /// array. |
790 | | void Diagnostic:: |
791 | 0 | FormatDiagnostic(SmallVectorImpl<char> &OutStr) const { |
792 | 0 | if (StoredDiagMessage.has_value()) { |
793 | 0 | OutStr.append(StoredDiagMessage->begin(), StoredDiagMessage->end()); |
794 | 0 | return; |
795 | 0 | } |
796 | | |
797 | 0 | StringRef Diag = |
798 | 0 | getDiags()->getDiagnosticIDs()->getDescription(getID()); |
799 | |
|
800 | 0 | FormatDiagnostic(Diag.begin(), Diag.end(), OutStr); |
801 | 0 | } |
802 | | |
803 | | /// EscapeStringForDiagnostic - Append Str to the diagnostic buffer, |
804 | | /// escaping non-printable characters and ill-formed code unit sequences. |
805 | | void clang::EscapeStringForDiagnostic(StringRef Str, |
806 | 0 | SmallVectorImpl<char> &OutStr) { |
807 | 0 | OutStr.reserve(OutStr.size() + Str.size()); |
808 | 0 | auto *Begin = reinterpret_cast<const unsigned char *>(Str.data()); |
809 | 0 | llvm::raw_svector_ostream OutStream(OutStr); |
810 | 0 | const unsigned char *End = Begin + Str.size(); |
811 | 0 | while (Begin != End) { |
812 | | // ASCII case |
813 | 0 | if (isPrintable(*Begin) || isWhitespace(*Begin)) { |
814 | 0 | OutStream << *Begin; |
815 | 0 | ++Begin; |
816 | 0 | continue; |
817 | 0 | } |
818 | 0 | if (llvm::isLegalUTF8Sequence(Begin, End)) { |
819 | 0 | llvm::UTF32 CodepointValue; |
820 | 0 | llvm::UTF32 *CpPtr = &CodepointValue; |
821 | 0 | const unsigned char *CodepointBegin = Begin; |
822 | 0 | const unsigned char *CodepointEnd = |
823 | 0 | Begin + llvm::getNumBytesForUTF8(*Begin); |
824 | 0 | llvm::ConversionResult Res = llvm::ConvertUTF8toUTF32( |
825 | 0 | &Begin, CodepointEnd, &CpPtr, CpPtr + 1, llvm::strictConversion); |
826 | 0 | (void)Res; |
827 | 0 | assert( |
828 | 0 | llvm::conversionOK == Res && |
829 | 0 | "the sequence is legal UTF-8 but we couldn't convert it to UTF-32"); |
830 | 0 | assert(Begin == CodepointEnd && |
831 | 0 | "we must be further along in the string now"); |
832 | 0 | if (llvm::sys::unicode::isPrintable(CodepointValue) || |
833 | 0 | llvm::sys::unicode::isFormatting(CodepointValue)) { |
834 | 0 | OutStr.append(CodepointBegin, CodepointEnd); |
835 | 0 | continue; |
836 | 0 | } |
837 | | // Unprintable code point. |
838 | 0 | OutStream << "<U+" << llvm::format_hex_no_prefix(CodepointValue, 4, true) |
839 | 0 | << ">"; |
840 | 0 | continue; |
841 | 0 | } |
842 | | // Invalid code unit. |
843 | 0 | OutStream << "<" << llvm::format_hex_no_prefix(*Begin, 2, true) << ">"; |
844 | 0 | ++Begin; |
845 | 0 | } |
846 | 0 | } |
847 | | |
848 | | void Diagnostic:: |
849 | | FormatDiagnostic(const char *DiagStr, const char *DiagEnd, |
850 | 0 | SmallVectorImpl<char> &OutStr) const { |
851 | | // When the diagnostic string is only "%0", the entire string is being given |
852 | | // by an outside source. Remove unprintable characters from this string |
853 | | // and skip all the other string processing. |
854 | 0 | if (DiagEnd - DiagStr == 2 && |
855 | 0 | StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") && |
856 | 0 | getArgKind(0) == DiagnosticsEngine::ak_std_string) { |
857 | 0 | const std::string &S = getArgStdStr(0); |
858 | 0 | EscapeStringForDiagnostic(S, OutStr); |
859 | 0 | return; |
860 | 0 | } |
861 | | |
862 | | /// FormattedArgs - Keep track of all of the arguments formatted by |
863 | | /// ConvertArgToString and pass them into subsequent calls to |
864 | | /// ConvertArgToString, allowing the implementation to avoid redundancies in |
865 | | /// obvious cases. |
866 | 0 | SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs; |
867 | | |
868 | | /// QualTypeVals - Pass a vector of arrays so that QualType names can be |
869 | | /// compared to see if more information is needed to be printed. |
870 | 0 | SmallVector<intptr_t, 2> QualTypeVals; |
871 | 0 | SmallString<64> Tree; |
872 | |
|
873 | 0 | for (unsigned i = 0, e = getNumArgs(); i < e; ++i) |
874 | 0 | if (getArgKind(i) == DiagnosticsEngine::ak_qualtype) |
875 | 0 | QualTypeVals.push_back(getRawArg(i)); |
876 | |
|
877 | 0 | while (DiagStr != DiagEnd) { |
878 | 0 | if (DiagStr[0] != '%') { |
879 | | // Append non-%0 substrings to Str if we have one. |
880 | 0 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
881 | 0 | OutStr.append(DiagStr, StrEnd); |
882 | 0 | DiagStr = StrEnd; |
883 | 0 | continue; |
884 | 0 | } else if (isPunctuation(DiagStr[1])) { |
885 | 0 | OutStr.push_back(DiagStr[1]); // %% -> %. |
886 | 0 | DiagStr += 2; |
887 | 0 | continue; |
888 | 0 | } |
889 | | |
890 | | // Skip the %. |
891 | 0 | ++DiagStr; |
892 | | |
893 | | // This must be a placeholder for a diagnostic argument. The format for a |
894 | | // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". |
895 | | // The digit is a number from 0-9 indicating which argument this comes from. |
896 | | // The modifier is a string of digits from the set [-a-z]+, arguments is a |
897 | | // brace enclosed string. |
898 | 0 | const char *Modifier = nullptr, *Argument = nullptr; |
899 | 0 | unsigned ModifierLen = 0, ArgumentLen = 0; |
900 | | |
901 | | // Check to see if we have a modifier. If so eat it. |
902 | 0 | if (!isDigit(DiagStr[0])) { |
903 | 0 | Modifier = DiagStr; |
904 | 0 | while (DiagStr[0] == '-' || |
905 | 0 | (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) |
906 | 0 | ++DiagStr; |
907 | 0 | ModifierLen = DiagStr-Modifier; |
908 | | |
909 | | // If we have an argument, get it next. |
910 | 0 | if (DiagStr[0] == '{') { |
911 | 0 | ++DiagStr; // Skip {. |
912 | 0 | Argument = DiagStr; |
913 | |
|
914 | 0 | DiagStr = ScanFormat(DiagStr, DiagEnd, '}'); |
915 | 0 | assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!"); |
916 | 0 | ArgumentLen = DiagStr-Argument; |
917 | 0 | ++DiagStr; // Skip }. |
918 | 0 | } |
919 | 0 | } |
920 | | |
921 | 0 | assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic"); |
922 | 0 | unsigned ArgNo = *DiagStr++ - '0'; |
923 | | |
924 | | // Only used for type diffing. |
925 | 0 | unsigned ArgNo2 = ArgNo; |
926 | |
|
927 | 0 | DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo); |
928 | 0 | if (ModifierIs(Modifier, ModifierLen, "diff")) { |
929 | 0 | assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) && |
930 | 0 | "Invalid format for diff modifier"); |
931 | 0 | ++DiagStr; // Comma. |
932 | 0 | ArgNo2 = *DiagStr++ - '0'; |
933 | 0 | DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2); |
934 | 0 | if (Kind == DiagnosticsEngine::ak_qualtype && |
935 | 0 | Kind2 == DiagnosticsEngine::ak_qualtype) |
936 | 0 | Kind = DiagnosticsEngine::ak_qualtype_pair; |
937 | 0 | else { |
938 | | // %diff only supports QualTypes. For other kinds of arguments, |
939 | | // use the default printing. For example, if the modifier is: |
940 | | // "%diff{compare $ to $|other text}1,2" |
941 | | // treat it as: |
942 | | // "compare %1 to %2" |
943 | 0 | const char *ArgumentEnd = Argument + ArgumentLen; |
944 | 0 | const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|'); |
945 | 0 | assert(ScanFormat(Pipe + 1, ArgumentEnd, '|') == ArgumentEnd && |
946 | 0 | "Found too many '|'s in a %diff modifier!"); |
947 | 0 | const char *FirstDollar = ScanFormat(Argument, Pipe, '$'); |
948 | 0 | const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$'); |
949 | 0 | const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) }; |
950 | 0 | const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) }; |
951 | 0 | FormatDiagnostic(Argument, FirstDollar, OutStr); |
952 | 0 | FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr); |
953 | 0 | FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr); |
954 | 0 | FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr); |
955 | 0 | FormatDiagnostic(SecondDollar + 1, Pipe, OutStr); |
956 | 0 | continue; |
957 | 0 | } |
958 | 0 | } |
959 | | |
960 | 0 | switch (Kind) { |
961 | | // ---- STRINGS ---- |
962 | 0 | case DiagnosticsEngine::ak_std_string: { |
963 | 0 | const std::string &S = getArgStdStr(ArgNo); |
964 | 0 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
965 | 0 | EscapeStringForDiagnostic(S, OutStr); |
966 | 0 | break; |
967 | 0 | } |
968 | 0 | case DiagnosticsEngine::ak_c_string: { |
969 | 0 | const char *S = getArgCStr(ArgNo); |
970 | 0 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
971 | | |
972 | | // Don't crash if get passed a null pointer by accident. |
973 | 0 | if (!S) |
974 | 0 | S = "(null)"; |
975 | 0 | EscapeStringForDiagnostic(S, OutStr); |
976 | 0 | break; |
977 | 0 | } |
978 | | // ---- INTEGERS ---- |
979 | 0 | case DiagnosticsEngine::ak_sint: { |
980 | 0 | int64_t Val = getArgSInt(ArgNo); |
981 | |
|
982 | 0 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
983 | 0 | HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
984 | 0 | OutStr); |
985 | 0 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
986 | 0 | HandleIntegerSModifier(Val, OutStr); |
987 | 0 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
988 | 0 | HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
989 | 0 | OutStr); |
990 | 0 | } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { |
991 | 0 | HandleOrdinalModifier((unsigned)Val, OutStr); |
992 | 0 | } else { |
993 | 0 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
994 | 0 | llvm::raw_svector_ostream(OutStr) << Val; |
995 | 0 | } |
996 | 0 | break; |
997 | 0 | } |
998 | 0 | case DiagnosticsEngine::ak_uint: { |
999 | 0 | uint64_t Val = getArgUInt(ArgNo); |
1000 | |
|
1001 | 0 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
1002 | 0 | HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr); |
1003 | 0 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
1004 | 0 | HandleIntegerSModifier(Val, OutStr); |
1005 | 0 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
1006 | 0 | HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
1007 | 0 | OutStr); |
1008 | 0 | } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { |
1009 | 0 | HandleOrdinalModifier(Val, OutStr); |
1010 | 0 | } else { |
1011 | 0 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
1012 | 0 | llvm::raw_svector_ostream(OutStr) << Val; |
1013 | 0 | } |
1014 | 0 | break; |
1015 | 0 | } |
1016 | | // ---- TOKEN SPELLINGS ---- |
1017 | 0 | case DiagnosticsEngine::ak_tokenkind: { |
1018 | 0 | tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo)); |
1019 | 0 | assert(ModifierLen == 0 && "No modifiers for token kinds yet"); |
1020 | | |
1021 | 0 | llvm::raw_svector_ostream Out(OutStr); |
1022 | 0 | if (const char *S = tok::getPunctuatorSpelling(Kind)) |
1023 | | // Quoted token spelling for punctuators. |
1024 | 0 | Out << '\'' << S << '\''; |
1025 | 0 | else if ((S = tok::getKeywordSpelling(Kind))) |
1026 | | // Unquoted token spelling for keywords. |
1027 | 0 | Out << S; |
1028 | 0 | else if ((S = getTokenDescForDiagnostic(Kind))) |
1029 | | // Unquoted translatable token name. |
1030 | 0 | Out << S; |
1031 | 0 | else if ((S = tok::getTokenName(Kind))) |
1032 | | // Debug name, shouldn't appear in user-facing diagnostics. |
1033 | 0 | Out << '<' << S << '>'; |
1034 | 0 | else |
1035 | 0 | Out << "(null)"; |
1036 | 0 | break; |
1037 | 0 | } |
1038 | | // ---- NAMES and TYPES ---- |
1039 | 0 | case DiagnosticsEngine::ak_identifierinfo: { |
1040 | 0 | const IdentifierInfo *II = getArgIdentifier(ArgNo); |
1041 | 0 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
1042 | | |
1043 | | // Don't crash if get passed a null pointer by accident. |
1044 | 0 | if (!II) { |
1045 | 0 | const char *S = "(null)"; |
1046 | 0 | OutStr.append(S, S + strlen(S)); |
1047 | 0 | continue; |
1048 | 0 | } |
1049 | | |
1050 | 0 | llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\''; |
1051 | 0 | break; |
1052 | 0 | } |
1053 | 0 | case DiagnosticsEngine::ak_addrspace: |
1054 | 0 | case DiagnosticsEngine::ak_qual: |
1055 | 0 | case DiagnosticsEngine::ak_qualtype: |
1056 | 0 | case DiagnosticsEngine::ak_declarationname: |
1057 | 0 | case DiagnosticsEngine::ak_nameddecl: |
1058 | 0 | case DiagnosticsEngine::ak_nestednamespec: |
1059 | 0 | case DiagnosticsEngine::ak_declcontext: |
1060 | 0 | case DiagnosticsEngine::ak_attr: |
1061 | 0 | getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo), |
1062 | 0 | StringRef(Modifier, ModifierLen), |
1063 | 0 | StringRef(Argument, ArgumentLen), |
1064 | 0 | FormattedArgs, |
1065 | 0 | OutStr, QualTypeVals); |
1066 | 0 | break; |
1067 | 0 | case DiagnosticsEngine::ak_qualtype_pair: { |
1068 | | // Create a struct with all the info needed for printing. |
1069 | 0 | TemplateDiffTypes TDT; |
1070 | 0 | TDT.FromType = getRawArg(ArgNo); |
1071 | 0 | TDT.ToType = getRawArg(ArgNo2); |
1072 | 0 | TDT.ElideType = getDiags()->ElideType; |
1073 | 0 | TDT.ShowColors = getDiags()->ShowColors; |
1074 | 0 | TDT.TemplateDiffUsed = false; |
1075 | 0 | intptr_t val = reinterpret_cast<intptr_t>(&TDT); |
1076 | |
|
1077 | 0 | const char *ArgumentEnd = Argument + ArgumentLen; |
1078 | 0 | const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|'); |
1079 | | |
1080 | | // Print the tree. If this diagnostic already has a tree, skip the |
1081 | | // second tree. |
1082 | 0 | if (getDiags()->PrintTemplateTree && Tree.empty()) { |
1083 | 0 | TDT.PrintFromType = true; |
1084 | 0 | TDT.PrintTree = true; |
1085 | 0 | getDiags()->ConvertArgToString(Kind, val, |
1086 | 0 | StringRef(Modifier, ModifierLen), |
1087 | 0 | StringRef(Argument, ArgumentLen), |
1088 | 0 | FormattedArgs, |
1089 | 0 | Tree, QualTypeVals); |
1090 | | // If there is no tree information, fall back to regular printing. |
1091 | 0 | if (!Tree.empty()) { |
1092 | 0 | FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr); |
1093 | 0 | break; |
1094 | 0 | } |
1095 | 0 | } |
1096 | | |
1097 | | // Non-tree printing, also the fall-back when tree printing fails. |
1098 | | // The fall-back is triggered when the types compared are not templates. |
1099 | 0 | const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$'); |
1100 | 0 | const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$'); |
1101 | | |
1102 | | // Append before text |
1103 | 0 | FormatDiagnostic(Argument, FirstDollar, OutStr); |
1104 | | |
1105 | | // Append first type |
1106 | 0 | TDT.PrintTree = false; |
1107 | 0 | TDT.PrintFromType = true; |
1108 | 0 | getDiags()->ConvertArgToString(Kind, val, |
1109 | 0 | StringRef(Modifier, ModifierLen), |
1110 | 0 | StringRef(Argument, ArgumentLen), |
1111 | 0 | FormattedArgs, |
1112 | 0 | OutStr, QualTypeVals); |
1113 | 0 | if (!TDT.TemplateDiffUsed) |
1114 | 0 | FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype, |
1115 | 0 | TDT.FromType)); |
1116 | | |
1117 | | // Append middle text |
1118 | 0 | FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr); |
1119 | | |
1120 | | // Append second type |
1121 | 0 | TDT.PrintFromType = false; |
1122 | 0 | getDiags()->ConvertArgToString(Kind, val, |
1123 | 0 | StringRef(Modifier, ModifierLen), |
1124 | 0 | StringRef(Argument, ArgumentLen), |
1125 | 0 | FormattedArgs, |
1126 | 0 | OutStr, QualTypeVals); |
1127 | 0 | if (!TDT.TemplateDiffUsed) |
1128 | 0 | FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype, |
1129 | 0 | TDT.ToType)); |
1130 | | |
1131 | | // Append end text |
1132 | 0 | FormatDiagnostic(SecondDollar + 1, Pipe, OutStr); |
1133 | 0 | break; |
1134 | 0 | } |
1135 | 0 | } |
1136 | | |
1137 | | // Remember this argument info for subsequent formatting operations. Turn |
1138 | | // std::strings into a null terminated string to make it be the same case as |
1139 | | // all the other ones. |
1140 | 0 | if (Kind == DiagnosticsEngine::ak_qualtype_pair) |
1141 | 0 | continue; |
1142 | 0 | else if (Kind != DiagnosticsEngine::ak_std_string) |
1143 | 0 | FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo))); |
1144 | 0 | else |
1145 | 0 | FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string, |
1146 | 0 | (intptr_t)getArgStdStr(ArgNo).c_str())); |
1147 | 0 | } |
1148 | | |
1149 | | // Append the type tree to the end of the diagnostics. |
1150 | 0 | OutStr.append(Tree.begin(), Tree.end()); |
1151 | 0 | } |
1152 | | |
1153 | | StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID, |
1154 | | StringRef Message) |
1155 | 0 | : ID(ID), Level(Level), Message(Message) {} |
1156 | | |
1157 | | StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, |
1158 | | const Diagnostic &Info) |
1159 | 0 | : ID(Info.getID()), Level(Level) { |
1160 | 0 | assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) && |
1161 | 0 | "Valid source location without setting a source manager for diagnostic"); |
1162 | 0 | if (Info.getLocation().isValid()) |
1163 | 0 | Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager()); |
1164 | 0 | SmallString<64> Message; |
1165 | 0 | Info.FormatDiagnostic(Message); |
1166 | 0 | this->Message.assign(Message.begin(), Message.end()); |
1167 | 0 | this->Ranges.assign(Info.getRanges().begin(), Info.getRanges().end()); |
1168 | 0 | this->FixIts.assign(Info.getFixItHints().begin(), Info.getFixItHints().end()); |
1169 | 0 | } |
1170 | | |
1171 | | StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID, |
1172 | | StringRef Message, FullSourceLoc Loc, |
1173 | | ArrayRef<CharSourceRange> Ranges, |
1174 | | ArrayRef<FixItHint> FixIts) |
1175 | | : ID(ID), Level(Level), Loc(Loc), Message(Message), |
1176 | | Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end()) |
1177 | 0 | { |
1178 | 0 | } |
1179 | | |
1180 | | llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS, |
1181 | 0 | const StoredDiagnostic &SD) { |
1182 | 0 | if (SD.getLocation().hasManager()) |
1183 | 0 | OS << SD.getLocation().printToString(SD.getLocation().getManager()) << ": "; |
1184 | 0 | OS << SD.getMessage(); |
1185 | 0 | return OS; |
1186 | 0 | } |
1187 | | |
1188 | | /// IncludeInDiagnosticCounts - This method (whose default implementation |
1189 | | /// returns true) indicates whether the diagnostics handled by this |
1190 | | /// DiagnosticConsumer should be included in the number of diagnostics |
1191 | | /// reported by DiagnosticsEngine. |
1192 | 18.3M | bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; } |
1193 | | |
1194 | 0 | void IgnoringDiagConsumer::anchor() {} |
1195 | | |
1196 | | ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() = default; |
1197 | | |
1198 | | void ForwardingDiagnosticConsumer::HandleDiagnostic( |
1199 | | DiagnosticsEngine::Level DiagLevel, |
1200 | 0 | const Diagnostic &Info) { |
1201 | 0 | Target.HandleDiagnostic(DiagLevel, Info); |
1202 | 0 | } |
1203 | | |
1204 | 0 | void ForwardingDiagnosticConsumer::clear() { |
1205 | 0 | DiagnosticConsumer::clear(); |
1206 | 0 | Target.clear(); |
1207 | 0 | } |
1208 | | |
1209 | 0 | bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const { |
1210 | 0 | return Target.IncludeInDiagnosticCounts(); |
1211 | 0 | } |
1212 | | |
1213 | 46 | PartialDiagnostic::DiagStorageAllocator::DiagStorageAllocator() { |
1214 | 782 | for (unsigned I = 0; I != NumCached; ++I) |
1215 | 736 | FreeList[I] = Cached + I; |
1216 | 46 | NumFreeListEntries = NumCached; |
1217 | 46 | } |
1218 | | |
1219 | 46 | PartialDiagnostic::DiagStorageAllocator::~DiagStorageAllocator() { |
1220 | | // Don't assert if we are in a CrashRecovery context, as this invariant may |
1221 | | // be invalidated during a crash. |
1222 | 46 | assert((NumFreeListEntries == NumCached || |
1223 | 46 | llvm::CrashRecoveryContext::isRecoveringFromCrash()) && |
1224 | 46 | "A partial is on the lam"); |
1225 | 46 | } |
1226 | | |
1227 | | char DiagnosticError::ID; |