Bug Summary

File:build/source/clang/lib/Basic/Diagnostic.cpp
Warning:line 686, column 31
Division by zero

Annotated Source Code

Press '?' to see keyboard shortcuts

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