Bug Summary

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