clang 17.0.0git
BugReporter.cpp
Go to the documentation of this file.
1//===- BugReporter.cpp - Generate PathDiagnostics for bugs ----------------===//
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 defines BugReporter, a utility class for generating
10// PathDiagnostics.
11//
12//===----------------------------------------------------------------------===//
13
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclBase.h"
17#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprCXX.h"
20#include "clang/AST/ParentMap.h"
21#include "clang/AST/Stmt.h"
22#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtObjC.h"
25#include "clang/Analysis/CFG.h"
29#include "clang/Basic/LLVM.h"
45#include "llvm/ADT/ArrayRef.h"
46#include "llvm/ADT/DenseMap.h"
47#include "llvm/ADT/DenseSet.h"
48#include "llvm/ADT/FoldingSet.h"
49#include "llvm/ADT/STLExtras.h"
50#include "llvm/ADT/SmallPtrSet.h"
51#include "llvm/ADT/SmallString.h"
52#include "llvm/ADT/SmallVector.h"
53#include "llvm/ADT/Statistic.h"
54#include "llvm/ADT/StringExtras.h"
55#include "llvm/ADT/StringRef.h"
56#include "llvm/ADT/iterator_range.h"
57#include "llvm/Support/Casting.h"
58#include "llvm/Support/Compiler.h"
59#include "llvm/Support/ErrorHandling.h"
60#include "llvm/Support/MemoryBuffer.h"
61#include "llvm/Support/raw_ostream.h"
62#include <algorithm>
63#include <cassert>
64#include <cstddef>
65#include <iterator>
66#include <memory>
67#include <optional>
68#include <queue>
69#include <string>
70#include <tuple>
71#include <utility>
72#include <vector>
73
74using namespace clang;
75using namespace ento;
76using namespace llvm;
77
78#define DEBUG_TYPE "BugReporter"
79
80STATISTIC(MaxBugClassSize,
81 "The maximum number of bug reports in the same equivalence class");
82STATISTIC(MaxValidBugClassSize,
83 "The maximum number of bug reports in the same equivalence class "
84 "where at least one report is valid (not suppressed)");
85
87
88void BugReporterContext::anchor() {}
89
90//===----------------------------------------------------------------------===//
91// PathDiagnosticBuilder and its associated routines and helper objects.
92//===----------------------------------------------------------------------===//
93
94namespace {
95
96/// A (CallPiece, node assiciated with its CallEnter) pair.
97using CallWithEntry =
98 std::pair<PathDiagnosticCallPiece *, const ExplodedNode *>;
99using CallWithEntryStack = SmallVector<CallWithEntry, 6>;
100
101/// Map from each node to the diagnostic pieces visitors emit for them.
102using VisitorsDiagnosticsTy =
103 llvm::DenseMap<const ExplodedNode *, std::vector<PathDiagnosticPieceRef>>;
104
105/// A map from PathDiagnosticPiece to the LocationContext of the inlined
106/// function call it represents.
107using LocationContextMap =
108 llvm::DenseMap<const PathPieces *, const LocationContext *>;
109
110/// A helper class that contains everything needed to construct a
111/// PathDiagnostic object. It does no much more then providing convenient
112/// getters and some well placed asserts for extra security.
113class PathDiagnosticConstruct {
114 /// The consumer we're constructing the bug report for.
115 const PathDiagnosticConsumer *Consumer;
116 /// Our current position in the bug path, which is owned by
117 /// PathDiagnosticBuilder.
118 const ExplodedNode *CurrentNode;
119 /// A mapping from parts of the bug path (for example, a function call, which
120 /// would span backwards from a CallExit to a CallEnter with the nodes in
121 /// between them) with the location contexts it is associated with.
122 LocationContextMap LCM;
123 const SourceManager &SM;
124
125public:
126 /// We keep stack of calls to functions as we're ascending the bug path.
127 /// TODO: PathDiagnostic has a stack doing the same thing, shouldn't we use
128 /// that instead?
129 CallWithEntryStack CallStack;
130 /// The bug report we're constructing. For ease of use, this field is kept
131 /// public, though some "shortcut" getters are provided for commonly used
132 /// methods of PathDiagnostic.
133 std::unique_ptr<PathDiagnostic> PD;
134
135public:
136 PathDiagnosticConstruct(const PathDiagnosticConsumer *PDC,
137 const ExplodedNode *ErrorNode,
138 const PathSensitiveBugReport *R);
139
140 /// \returns the location context associated with the current position in the
141 /// bug path.
142 const LocationContext *getCurrLocationContext() const {
143 assert(CurrentNode && "Already reached the root!");
144 return CurrentNode->getLocationContext();
145 }
146
147 /// Same as getCurrLocationContext (they should always return the same
148 /// location context), but works after reaching the root of the bug path as
149 /// well.
150 const LocationContext *getLocationContextForActivePath() const {
151 return LCM.find(&PD->getActivePath())->getSecond();
152 }
153
154 const ExplodedNode *getCurrentNode() const { return CurrentNode; }
155
156 /// Steps the current node to its predecessor.
157 /// \returns whether we reached the root of the bug path.
158 bool ascendToPrevNode() {
159 CurrentNode = CurrentNode->getFirstPred();
160 return static_cast<bool>(CurrentNode);
161 }
162
163 const ParentMap &getParentMap() const {
164 return getCurrLocationContext()->getParentMap();
165 }
166
167 const SourceManager &getSourceManager() const { return SM; }
168
169 const Stmt *getParent(const Stmt *S) const {
170 return getParentMap().getParent(S);
171 }
172
173 void updateLocCtxMap(const PathPieces *Path, const LocationContext *LC) {
174 assert(Path && LC);
175 LCM[Path] = LC;
176 }
177
178 const LocationContext *getLocationContextFor(const PathPieces *Path) const {
179 assert(LCM.count(Path) &&
180 "Failed to find the context associated with these pieces!");
181 return LCM.find(Path)->getSecond();
182 }
183
184 bool isInLocCtxMap(const PathPieces *Path) const { return LCM.count(Path); }
185
186 PathPieces &getActivePath() { return PD->getActivePath(); }
187 PathPieces &getMutablePieces() { return PD->getMutablePieces(); }
188
189 bool shouldAddPathEdges() const { return Consumer->shouldAddPathEdges(); }
190 bool shouldAddControlNotes() const {
191 return Consumer->shouldAddControlNotes();
192 }
193 bool shouldGenerateDiagnostics() const {
194 return Consumer->shouldGenerateDiagnostics();
195 }
196 bool supportsLogicalOpControlFlow() const {
197 return Consumer->supportsLogicalOpControlFlow();
198 }
199};
200
201/// Contains every contextual information needed for constructing a
202/// PathDiagnostic object for a given bug report. This class and its fields are
203/// immutable, and passes a BugReportConstruct object around during the
204/// construction.
205class PathDiagnosticBuilder : public BugReporterContext {
206 /// A linear path from the error node to the root.
207 std::unique_ptr<const ExplodedGraph> BugPath;
208 /// The bug report we're describing. Visitors create their diagnostics with
209 /// them being the last entities being able to modify it (for example,
210 /// changing interestingness here would cause inconsistencies as to how this
211 /// file and visitors construct diagnostics), hence its const.
212 const PathSensitiveBugReport *R;
213 /// The leaf of the bug path. This isn't the same as the bug reports error
214 /// node, which refers to the *original* graph, not the bug path.
215 const ExplodedNode *const ErrorNode;
216 /// The diagnostic pieces visitors emitted, which is expected to be collected
217 /// by the time this builder is constructed.
218 std::unique_ptr<const VisitorsDiagnosticsTy> VisitorsDiagnostics;
219
220public:
221 /// Find a non-invalidated report for a given equivalence class, and returns
222 /// a PathDiagnosticBuilder able to construct bug reports for different
223 /// consumers. Returns std::nullopt if no valid report is found.
224 static std::optional<PathDiagnosticBuilder>
225 findValidReport(ArrayRef<PathSensitiveBugReport *> &bugReports,
226 PathSensitiveBugReporter &Reporter);
227
228 PathDiagnosticBuilder(
229 BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath,
230 PathSensitiveBugReport *r, const ExplodedNode *ErrorNode,
231 std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics);
232
233 /// This function is responsible for generating diagnostic pieces that are
234 /// *not* provided by bug report visitors.
235 /// These diagnostics may differ depending on the consumer's settings,
236 /// and are therefore constructed separately for each consumer.
237 ///
238 /// There are two path diagnostics generation modes: with adding edges (used
239 /// for plists) and without (used for HTML and text). When edges are added,
240 /// the path is modified to insert artificially generated edges.
241 /// Otherwise, more detailed diagnostics is emitted for block edges,
242 /// explaining the transitions in words.
243 std::unique_ptr<PathDiagnostic>
244 generate(const PathDiagnosticConsumer *PDC) const;
245
246private:
247 void updateStackPiecesWithMessage(PathDiagnosticPieceRef P,
248 const CallWithEntryStack &CallStack) const;
249 void generatePathDiagnosticsForNode(PathDiagnosticConstruct &C,
250 PathDiagnosticLocation &PrevLoc) const;
251
252 void generateMinimalDiagForBlockEdge(PathDiagnosticConstruct &C,
253 BlockEdge BE) const;
254
256 generateDiagForGotoOP(const PathDiagnosticConstruct &C, const Stmt *S,
257 PathDiagnosticLocation &Start) const;
258
260 generateDiagForSwitchOP(const PathDiagnosticConstruct &C, const CFGBlock *Dst,
261 PathDiagnosticLocation &Start) const;
262
264 generateDiagForBinaryOP(const PathDiagnosticConstruct &C, const Stmt *T,
265 const CFGBlock *Src, const CFGBlock *DstC) const;
266
268 ExecutionContinues(const PathDiagnosticConstruct &C) const;
269
271 ExecutionContinues(llvm::raw_string_ostream &os,
272 const PathDiagnosticConstruct &C) const;
273
274 const PathSensitiveBugReport *getBugReport() const { return R; }
275};
276
277} // namespace
278
279//===----------------------------------------------------------------------===//
280// Base implementation of stack hint generators.
281//===----------------------------------------------------------------------===//
282
284
286 if (!N)
288
290 CallExitEnd CExit = P.castAs<CallExitEnd>();
291
292 // FIXME: Use CallEvent to abstract this over all calls.
293 const Stmt *CallSite = CExit.getCalleeContext()->getCallSite();
294 const auto *CE = dyn_cast_or_null<CallExpr>(CallSite);
295 if (!CE)
296 return {};
297
298 // Check if one of the parameters are set to the interesting symbol.
299 unsigned ArgIndex = 0;
300 for (CallExpr::const_arg_iterator I = CE->arg_begin(),
301 E = CE->arg_end(); I != E; ++I, ++ArgIndex){
302 SVal SV = N->getSVal(*I);
303
304 // Check if the variable corresponding to the symbol is passed by value.
305 SymbolRef AS = SV.getAsLocSymbol();
306 if (AS == Sym) {
307 return getMessageForArg(*I, ArgIndex);
308 }
309
310 // Check if the parameter is a pointer to the symbol.
311 if (std::optional<loc::MemRegionVal> Reg = SV.getAs<loc::MemRegionVal>()) {
312 // Do not attempt to dereference void*.
313 if ((*I)->getType()->isVoidPointerType())
314 continue;
315 SVal PSV = N->getState()->getSVal(Reg->getRegion());
316 SymbolRef AS = PSV.getAsLocSymbol();
317 if (AS == Sym) {
318 return getMessageForArg(*I, ArgIndex);
319 }
320 }
321 }
322
323 // Check if we are returning the interesting symbol.
324 SVal SV = N->getSVal(CE);
325 SymbolRef RetSym = SV.getAsLocSymbol();
326 if (RetSym == Sym) {
327 return getMessageForReturn(CE);
328 }
329
331}
332
334 unsigned ArgIndex) {
335 // Printed parameters start at 1, not 0.
336 ++ArgIndex;
337
338 return (llvm::Twine(Msg) + " via " + std::to_string(ArgIndex) +
339 llvm::getOrdinalSuffix(ArgIndex) + " parameter").str();
340}
341
342//===----------------------------------------------------------------------===//
343// Diagnostic cleanup.
344//===----------------------------------------------------------------------===//
345
349 // Prefer diagnostics that come from ConditionBRVisitor over
350 // those that came from TrackConstraintBRVisitor,
351 // unless the one from ConditionBRVisitor is
352 // its generic fallback diagnostic.
353 const void *tagPreferred = ConditionBRVisitor::getTag();
354 const void *tagLesser = TrackConstraintBRVisitor::getTag();
355
356 if (X->getLocation() != Y->getLocation())
357 return nullptr;
358
359 if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
361
362 if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
364
365 return nullptr;
366}
367
368/// An optimization pass over PathPieces that removes redundant diagnostics
369/// generated by both ConditionBRVisitor and TrackConstraintBRVisitor. Both
370/// BugReporterVisitors use different methods to generate diagnostics, with
371/// one capable of emitting diagnostics in some cases but not in others. This
372/// can lead to redundant diagnostic pieces at the same point in a path.
373static void removeRedundantMsgs(PathPieces &path) {
374 unsigned N = path.size();
375 if (N < 2)
376 return;
377 // NOTE: this loop intentionally is not using an iterator. Instead, we
378 // are streaming the path and modifying it in place. This is done by
379 // grabbing the front, processing it, and if we decide to keep it append
380 // it to the end of the path. The entire path is processed in this way.
381 for (unsigned i = 0; i < N; ++i) {
382 auto piece = std::move(path.front());
383 path.pop_front();
384
385 switch (piece->getKind()) {
387 removeRedundantMsgs(cast<PathDiagnosticCallPiece>(*piece).path);
388 break;
390 removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(*piece).subPieces);
391 break;
393 if (i == N-1)
394 break;
395
396 if (auto *nextEvent =
397 dyn_cast<PathDiagnosticEventPiece>(path.front().get())) {
398 auto *event = cast<PathDiagnosticEventPiece>(piece.get());
399 // Check to see if we should keep one of the two pieces. If we
400 // come up with a preference, record which piece to keep, and consume
401 // another piece from the path.
402 if (auto *pieceToKeep =
403 eventsDescribeSameCondition(event, nextEvent)) {
404 piece = std::move(pieceToKeep == event ? piece : path.front());
405 path.pop_front();
406 ++i;
407 }
408 }
409 break;
410 }
414 break;
415 }
416 path.push_back(std::move(piece));
417 }
418}
419
420/// Recursively scan through a path and prune out calls and macros pieces
421/// that aren't needed. Return true if afterwards the path contains
422/// "interesting stuff" which means it shouldn't be pruned from the parent path.
423static bool removeUnneededCalls(const PathDiagnosticConstruct &C,
424 PathPieces &pieces,
425 const PathSensitiveBugReport *R,
426 bool IsInteresting = false) {
427 bool containsSomethingInteresting = IsInteresting;
428 const unsigned N = pieces.size();
429
430 for (unsigned i = 0 ; i < N ; ++i) {
431 // Remove the front piece from the path. If it is still something we
432 // want to keep once we are done, we will push it back on the end.
433 auto piece = std::move(pieces.front());
434 pieces.pop_front();
435
436 switch (piece->getKind()) {
438 auto &call = cast<PathDiagnosticCallPiece>(*piece);
439 // Check if the location context is interesting.
441 C, call.path, R,
442 R->isInteresting(C.getLocationContextFor(&call.path))))
443 continue;
444
445 containsSomethingInteresting = true;
446 break;
447 }
449 auto &macro = cast<PathDiagnosticMacroPiece>(*piece);
450 if (!removeUnneededCalls(C, macro.subPieces, R, IsInteresting))
451 continue;
452 containsSomethingInteresting = true;
453 break;
454 }
456 auto &event = cast<PathDiagnosticEventPiece>(*piece);
457
458 // We never throw away an event, but we do throw it away wholesale
459 // as part of a path if we throw the entire path away.
460 containsSomethingInteresting |= !event.isPrunable();
461 break;
462 }
466 break;
467 }
468
469 pieces.push_back(std::move(piece));
470 }
471
472 return containsSomethingInteresting;
473}
474
475/// Same logic as above to remove extra pieces.
476static void removePopUpNotes(PathPieces &Path) {
477 for (unsigned int i = 0; i < Path.size(); ++i) {
478 auto Piece = std::move(Path.front());
479 Path.pop_front();
480 if (!isa<PathDiagnosticPopUpPiece>(*Piece))
481 Path.push_back(std::move(Piece));
482 }
483}
484
485/// Returns true if the given decl has been implicitly given a body, either by
486/// the analyzer or by the compiler proper.
487static bool hasImplicitBody(const Decl *D) {
488 assert(D);
489 return D->isImplicit() || !D->hasBody();
490}
491
492/// Recursively scan through a path and make sure that all call pieces have
493/// valid locations.
494static void
496 PathDiagnosticLocation *LastCallLocation = nullptr) {
497 for (const auto &I : Pieces) {
498 auto *Call = dyn_cast<PathDiagnosticCallPiece>(I.get());
499
500 if (!Call)
501 continue;
502
503 if (LastCallLocation) {
504 bool CallerIsImplicit = hasImplicitBody(Call->getCaller());
505 if (CallerIsImplicit || !Call->callEnter.asLocation().isValid())
506 Call->callEnter = *LastCallLocation;
507 if (CallerIsImplicit || !Call->callReturn.asLocation().isValid())
508 Call->callReturn = *LastCallLocation;
509 }
510
511 // Recursively clean out the subclass. Keep this call around if
512 // it contains any informative diagnostics.
513 PathDiagnosticLocation *ThisCallLocation;
514 if (Call->callEnterWithin.asLocation().isValid() &&
515 !hasImplicitBody(Call->getCallee()))
516 ThisCallLocation = &Call->callEnterWithin;
517 else
518 ThisCallLocation = &Call->callEnter;
519
520 assert(ThisCallLocation && "Outermost call has an invalid location");
521 adjustCallLocations(Call->path, ThisCallLocation);
522 }
523}
524
525/// Remove edges in and out of C++ default initializer expressions. These are
526/// for fields that have in-class initializers, as opposed to being initialized
527/// explicitly in a constructor or braced list.
529 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
530 if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
532
533 if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
535
536 if (auto *CF = dyn_cast<PathDiagnosticControlFlowPiece>(I->get())) {
537 const Stmt *Start = CF->getStartLocation().asStmt();
538 const Stmt *End = CF->getEndLocation().asStmt();
539 if (isa_and_nonnull<CXXDefaultInitExpr>(Start)) {
540 I = Pieces.erase(I);
541 continue;
542 } else if (isa_and_nonnull<CXXDefaultInitExpr>(End)) {
543 PathPieces::iterator Next = std::next(I);
544 if (Next != E) {
545 if (auto *NextCF =
546 dyn_cast<PathDiagnosticControlFlowPiece>(Next->get())) {
547 NextCF->setStartLocation(CF->getStartLocation());
548 }
549 }
550 I = Pieces.erase(I);
551 continue;
552 }
553 }
554
555 I++;
556 }
557}
558
559/// Remove all pieces with invalid locations as these cannot be serialized.
560/// We might have pieces with invalid locations as a result of inlining Body
561/// Farm generated functions.
563 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
564 if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
566
567 if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
569
570 if (!(*I)->getLocation().isValid() ||
571 !(*I)->getLocation().asLocation().isValid()) {
572 I = Pieces.erase(I);
573 continue;
574 }
575 I++;
576 }
577}
578
579PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues(
580 const PathDiagnosticConstruct &C) const {
581 if (const Stmt *S = C.getCurrentNode()->getNextStmtForDiagnostics())
582 return PathDiagnosticLocation(S, getSourceManager(),
583 C.getCurrLocationContext());
584
585 return PathDiagnosticLocation::createDeclEnd(C.getCurrLocationContext(),
586 getSourceManager());
587}
588
589PathDiagnosticLocation PathDiagnosticBuilder::ExecutionContinues(
590 llvm::raw_string_ostream &os, const PathDiagnosticConstruct &C) const {
591 // Slow, but probably doesn't matter.
592 if (os.str().empty())
593 os << ' ';
594
595 const PathDiagnosticLocation &Loc = ExecutionContinues(C);
596
597 if (Loc.asStmt())
598 os << "Execution continues on line "
599 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
600 << '.';
601 else {
602 os << "Execution jumps to the end of the ";
603 const Decl *D = C.getCurrLocationContext()->getDecl();
604 if (isa<ObjCMethodDecl>(D))
605 os << "method";
606 else if (isa<FunctionDecl>(D))
607 os << "function";
608 else {
609 assert(isa<BlockDecl>(D));
610 os << "anonymous block";
611 }
612 os << '.';
613 }
614
615 return Loc;
616}
617
618static const Stmt *getEnclosingParent(const Stmt *S, const ParentMap &PM) {
619 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
620 return PM.getParentIgnoreParens(S);
621
622 const Stmt *Parent = PM.getParentIgnoreParens(S);
623 if (!Parent)
624 return nullptr;
625
626 switch (Parent->getStmtClass()) {
627 case Stmt::ForStmtClass:
628 case Stmt::DoStmtClass:
629 case Stmt::WhileStmtClass:
630 case Stmt::ObjCForCollectionStmtClass:
631 case Stmt::CXXForRangeStmtClass:
632 return Parent;
633 default:
634 break;
635 }
636
637 return nullptr;
638}
639
642 bool allowNestedContexts = false) {
643 if (!S)
644 return {};
645
646 const SourceManager &SMgr = LC->getDecl()->getASTContext().getSourceManager();
647
648 while (const Stmt *Parent = getEnclosingParent(S, LC->getParentMap())) {
649 switch (Parent->getStmtClass()) {
650 case Stmt::BinaryOperatorClass: {
651 const auto *B = cast<BinaryOperator>(Parent);
652 if (B->isLogicalOp())
653 return PathDiagnosticLocation(allowNestedContexts ? B : S, SMgr, LC);
654 break;
655 }
656 case Stmt::CompoundStmtClass:
657 case Stmt::StmtExprClass:
658 return PathDiagnosticLocation(S, SMgr, LC);
659 case Stmt::ChooseExprClass:
660 // Similar to '?' if we are referring to condition, just have the edge
661 // point to the entire choose expression.
662 if (allowNestedContexts || cast<ChooseExpr>(Parent)->getCond() == S)
663 return PathDiagnosticLocation(Parent, SMgr, LC);
664 else
665 return PathDiagnosticLocation(S, SMgr, LC);
666 case Stmt::BinaryConditionalOperatorClass:
667 case Stmt::ConditionalOperatorClass:
668 // For '?', if we are referring to condition, just have the edge point
669 // to the entire '?' expression.
670 if (allowNestedContexts ||
671 cast<AbstractConditionalOperator>(Parent)->getCond() == S)
672 return PathDiagnosticLocation(Parent, SMgr, LC);
673 else
674 return PathDiagnosticLocation(S, SMgr, LC);
675 case Stmt::CXXForRangeStmtClass:
676 if (cast<CXXForRangeStmt>(Parent)->getBody() == S)
677 return PathDiagnosticLocation(S, SMgr, LC);
678 break;
679 case Stmt::DoStmtClass:
680 return PathDiagnosticLocation(S, SMgr, LC);
681 case Stmt::ForStmtClass:
682 if (cast<ForStmt>(Parent)->getBody() == S)
683 return PathDiagnosticLocation(S, SMgr, LC);
684 break;
685 case Stmt::IfStmtClass:
686 if (cast<IfStmt>(Parent)->getCond() != S)
687 return PathDiagnosticLocation(S, SMgr, LC);
688 break;
689 case Stmt::ObjCForCollectionStmtClass:
690 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
691 return PathDiagnosticLocation(S, SMgr, LC);
692 break;
693 case Stmt::WhileStmtClass:
694 if (cast<WhileStmt>(Parent)->getCond() != S)
695 return PathDiagnosticLocation(S, SMgr, LC);
696 break;
697 default:
698 break;
699 }
700
701 S = Parent;
702 }
703
704 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
705
706 return PathDiagnosticLocation(S, SMgr, LC);
707}
708
709//===----------------------------------------------------------------------===//
710// "Minimal" path diagnostic generation algorithm.
711//===----------------------------------------------------------------------===//
712
713/// If the piece contains a special message, add it to all the call pieces on
714/// the active stack. For example, my_malloc allocated memory, so MallocChecker
715/// will construct an event at the call to malloc(), and add a stack hint that
716/// an allocated memory was returned. We'll use this hint to construct a message
717/// when returning from the call to my_malloc
718///
719/// void *my_malloc() { return malloc(sizeof(int)); }
720/// void fishy() {
721/// void *ptr = my_malloc(); // returned allocated memory
722/// } // leak
723void PathDiagnosticBuilder::updateStackPiecesWithMessage(
724 PathDiagnosticPieceRef P, const CallWithEntryStack &CallStack) const {
725 if (R->hasCallStackHint(P))
726 for (const auto &I : CallStack) {
727 PathDiagnosticCallPiece *CP = I.first;
728 const ExplodedNode *N = I.second;
729 std::string stackMsg = R->getCallStackMessage(P, N);
730
731 // The last message on the path to final bug is the most important
732 // one. Since we traverse the path backwards, do not add the message
733 // if one has been previously added.
734 if (!CP->hasCallStackMessage())
735 CP->setCallStackMessage(stackMsg);
736 }
737}
738
739static void CompactMacroExpandedPieces(PathPieces &path,
740 const SourceManager& SM);
741
742PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForSwitchOP(
743 const PathDiagnosticConstruct &C, const CFGBlock *Dst,
744 PathDiagnosticLocation &Start) const {
745
746 const SourceManager &SM = getSourceManager();
747 // Figure out what case arm we took.
748 std::string sbuf;
749 llvm::raw_string_ostream os(sbuf);
751
752 if (const Stmt *S = Dst->getLabel()) {
753 End = PathDiagnosticLocation(S, SM, C.getCurrLocationContext());
754
755 switch (S->getStmtClass()) {
756 default:
757 os << "No cases match in the switch statement. "
758 "Control jumps to line "
759 << End.asLocation().getExpansionLineNumber();
760 break;
761 case Stmt::DefaultStmtClass:
762 os << "Control jumps to the 'default' case at line "
763 << End.asLocation().getExpansionLineNumber();
764 break;
765
766 case Stmt::CaseStmtClass: {
767 os << "Control jumps to 'case ";
768 const auto *Case = cast<CaseStmt>(S);
769 const Expr *LHS = Case->getLHS()->IgnoreParenImpCasts();
770
771 // Determine if it is an enum.
772 bool GetRawInt = true;
773
774 if (const auto *DR = dyn_cast<DeclRefExpr>(LHS)) {
775 // FIXME: Maybe this should be an assertion. Are there cases
776 // were it is not an EnumConstantDecl?
777 const auto *D = dyn_cast<EnumConstantDecl>(DR->getDecl());
778
779 if (D) {
780 GetRawInt = false;
781 os << *D;
782 }
783 }
784
785 if (GetRawInt)
786 os << LHS->EvaluateKnownConstInt(getASTContext());
787
788 os << ":' at line " << End.asLocation().getExpansionLineNumber();
789 break;
790 }
791 }
792 } else {
793 os << "'Default' branch taken. ";
794 End = ExecutionContinues(os, C);
795 }
796 return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
797 os.str());
798}
799
800PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForGotoOP(
801 const PathDiagnosticConstruct &C, const Stmt *S,
802 PathDiagnosticLocation &Start) const {
803 std::string sbuf;
804 llvm::raw_string_ostream os(sbuf);
805 const PathDiagnosticLocation &End =
806 getEnclosingStmtLocation(S, C.getCurrLocationContext());
807 os << "Control jumps to line " << End.asLocation().getExpansionLineNumber();
808 return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str());
809}
810
811PathDiagnosticPieceRef PathDiagnosticBuilder::generateDiagForBinaryOP(
812 const PathDiagnosticConstruct &C, const Stmt *T, const CFGBlock *Src,
813 const CFGBlock *Dst) const {
814
815 const SourceManager &SM = getSourceManager();
816
817 const auto *B = cast<BinaryOperator>(T);
818 std::string sbuf;
819 llvm::raw_string_ostream os(sbuf);
820 os << "Left side of '";
821 PathDiagnosticLocation Start, End;
822
823 if (B->getOpcode() == BO_LAnd) {
824 os << "&&"
825 << "' is ";
826
827 if (*(Src->succ_begin() + 1) == Dst) {
828 os << "false";
829 End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
830 Start =
832 } else {
833 os << "true";
834 Start =
835 PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
836 End = ExecutionContinues(C);
837 }
838 } else {
839 assert(B->getOpcode() == BO_LOr);
840 os << "||"
841 << "' is ";
842
843 if (*(Src->succ_begin() + 1) == Dst) {
844 os << "false";
845 Start =
846 PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
847 End = ExecutionContinues(C);
848 } else {
849 os << "true";
850 End = PathDiagnosticLocation(B->getLHS(), SM, C.getCurrLocationContext());
851 Start =
853 }
854 }
855 return std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
856 os.str());
857}
858
859void PathDiagnosticBuilder::generateMinimalDiagForBlockEdge(
860 PathDiagnosticConstruct &C, BlockEdge BE) const {
861 const SourceManager &SM = getSourceManager();
862 const LocationContext *LC = C.getCurrLocationContext();
863 const CFGBlock *Src = BE.getSrc();
864 const CFGBlock *Dst = BE.getDst();
865 const Stmt *T = Src->getTerminatorStmt();
866 if (!T)
867 return;
868
869 auto Start = PathDiagnosticLocation::createBegin(T, SM, LC);
870 switch (T->getStmtClass()) {
871 default:
872 break;
873
874 case Stmt::GotoStmtClass:
875 case Stmt::IndirectGotoStmtClass: {
876 if (const Stmt *S = C.getCurrentNode()->getNextStmtForDiagnostics())
877 C.getActivePath().push_front(generateDiagForGotoOP(C, S, Start));
878 break;
879 }
880
881 case Stmt::SwitchStmtClass: {
882 C.getActivePath().push_front(generateDiagForSwitchOP(C, Dst, Start));
883 break;
884 }
885
886 case Stmt::BreakStmtClass:
887 case Stmt::ContinueStmtClass: {
888 std::string sbuf;
889 llvm::raw_string_ostream os(sbuf);
890 PathDiagnosticLocation End = ExecutionContinues(os, C);
891 C.getActivePath().push_front(
892 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
893 break;
894 }
895
896 // Determine control-flow for ternary '?'.
897 case Stmt::BinaryConditionalOperatorClass:
898 case Stmt::ConditionalOperatorClass: {
899 std::string sbuf;
900 llvm::raw_string_ostream os(sbuf);
901 os << "'?' condition is ";
902
903 if (*(Src->succ_begin() + 1) == Dst)
904 os << "false";
905 else
906 os << "true";
907
908 PathDiagnosticLocation End = ExecutionContinues(C);
909
910 if (const Stmt *S = End.asStmt())
911 End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
912
913 C.getActivePath().push_front(
914 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End, os.str()));
915 break;
916 }
917
918 // Determine control-flow for short-circuited '&&' and '||'.
919 case Stmt::BinaryOperatorClass: {
920 if (!C.supportsLogicalOpControlFlow())
921 break;
922
923 C.getActivePath().push_front(generateDiagForBinaryOP(C, T, Src, Dst));
924 break;
925 }
926
927 case Stmt::DoStmtClass:
928 if (*(Src->succ_begin()) == Dst) {
929 std::string sbuf;
930 llvm::raw_string_ostream os(sbuf);
931
932 os << "Loop condition is true. ";
933 PathDiagnosticLocation End = ExecutionContinues(os, C);
934
935 if (const Stmt *S = End.asStmt())
936 End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
937
938 C.getActivePath().push_front(
939 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
940 os.str()));
941 } else {
942 PathDiagnosticLocation End = ExecutionContinues(C);
943
944 if (const Stmt *S = End.asStmt())
945 End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
946
947 C.getActivePath().push_front(
948 std::make_shared<PathDiagnosticControlFlowPiece>(
949 Start, End, "Loop condition is false. Exiting loop"));
950 }
951 break;
952
953 case Stmt::WhileStmtClass:
954 case Stmt::ForStmtClass:
955 if (*(Src->succ_begin() + 1) == Dst) {
956 std::string sbuf;
957 llvm::raw_string_ostream os(sbuf);
958
959 os << "Loop condition is false. ";
960 PathDiagnosticLocation End = ExecutionContinues(os, C);
961 if (const Stmt *S = End.asStmt())
962 End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
963
964 C.getActivePath().push_front(
965 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
966 os.str()));
967 } else {
968 PathDiagnosticLocation End = ExecutionContinues(C);
969 if (const Stmt *S = End.asStmt())
970 End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
971
972 C.getActivePath().push_front(
973 std::make_shared<PathDiagnosticControlFlowPiece>(
974 Start, End, "Loop condition is true. Entering loop body"));
975 }
976
977 break;
978
979 case Stmt::IfStmtClass: {
980 PathDiagnosticLocation End = ExecutionContinues(C);
981
982 if (const Stmt *S = End.asStmt())
983 End = getEnclosingStmtLocation(S, C.getCurrLocationContext());
984
985 if (*(Src->succ_begin() + 1) == Dst)
986 C.getActivePath().push_front(
987 std::make_shared<PathDiagnosticControlFlowPiece>(
988 Start, End, "Taking false branch"));
989 else
990 C.getActivePath().push_front(
991 std::make_shared<PathDiagnosticControlFlowPiece>(
992 Start, End, "Taking true branch"));
993
994 break;
995 }
996 }
997}
998
999//===----------------------------------------------------------------------===//
1000// Functions for determining if a loop was executed 0 times.
1001//===----------------------------------------------------------------------===//
1002
1003static bool isLoop(const Stmt *Term) {
1004 switch (Term->getStmtClass()) {
1005 case Stmt::ForStmtClass:
1006 case Stmt::WhileStmtClass:
1007 case Stmt::ObjCForCollectionStmtClass:
1008 case Stmt::CXXForRangeStmtClass:
1009 return true;
1010 default:
1011 // Note that we intentionally do not include do..while here.
1012 return false;
1013 }
1014}
1015
1016static bool isJumpToFalseBranch(const BlockEdge *BE) {
1017 const CFGBlock *Src = BE->getSrc();
1018 assert(Src->succ_size() == 2);
1019 return (*(Src->succ_begin()+1) == BE->getDst());
1020}
1021
1022static bool isContainedByStmt(const ParentMap &PM, const Stmt *S,
1023 const Stmt *SubS) {
1024 while (SubS) {
1025 if (SubS == S)
1026 return true;
1027 SubS = PM.getParent(SubS);
1028 }
1029 return false;
1030}
1031
1032static const Stmt *getStmtBeforeCond(const ParentMap &PM, const Stmt *Term,
1033 const ExplodedNode *N) {
1034 while (N) {
1035 std::optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>();
1036 if (SP) {
1037 const Stmt *S = SP->getStmt();
1038 if (!isContainedByStmt(PM, Term, S))
1039 return S;
1040 }
1041 N = N->getFirstPred();
1042 }
1043 return nullptr;
1044}
1045
1046static bool isInLoopBody(const ParentMap &PM, const Stmt *S, const Stmt *Term) {
1047 const Stmt *LoopBody = nullptr;
1048 switch (Term->getStmtClass()) {
1049 case Stmt::CXXForRangeStmtClass: {
1050 const auto *FR = cast<CXXForRangeStmt>(Term);
1051 if (isContainedByStmt(PM, FR->getInc(), S))
1052 return true;
1053 if (isContainedByStmt(PM, FR->getLoopVarStmt(), S))
1054 return true;
1055 LoopBody = FR->getBody();
1056 break;
1057 }
1058 case Stmt::ForStmtClass: {
1059 const auto *FS = cast<ForStmt>(Term);
1060 if (isContainedByStmt(PM, FS->getInc(), S))
1061 return true;
1062 LoopBody = FS->getBody();
1063 break;
1064 }
1065 case Stmt::ObjCForCollectionStmtClass: {
1066 const auto *FC = cast<ObjCForCollectionStmt>(Term);
1067 LoopBody = FC->getBody();
1068 break;
1069 }
1070 case Stmt::WhileStmtClass:
1071 LoopBody = cast<WhileStmt>(Term)->getBody();
1072 break;
1073 default:
1074 return false;
1075 }
1076 return isContainedByStmt(PM, LoopBody, S);
1077}
1078
1079/// Adds a sanitized control-flow diagnostic edge to a path.
1080static void addEdgeToPath(PathPieces &path,
1081 PathDiagnosticLocation &PrevLoc,
1082 PathDiagnosticLocation NewLoc) {
1083 if (!NewLoc.isValid())
1084 return;
1085
1086 SourceLocation NewLocL = NewLoc.asLocation();
1087 if (NewLocL.isInvalid())
1088 return;
1089
1090 if (!PrevLoc.isValid() || !PrevLoc.asLocation().isValid()) {
1091 PrevLoc = NewLoc;
1092 return;
1093 }
1094
1095 // Ignore self-edges, which occur when there are multiple nodes at the same
1096 // statement.
1097 if (NewLoc.asStmt() && NewLoc.asStmt() == PrevLoc.asStmt())
1098 return;
1099
1100 path.push_front(
1101 std::make_shared<PathDiagnosticControlFlowPiece>(NewLoc, PrevLoc));
1102 PrevLoc = NewLoc;
1103}
1104
1105/// A customized wrapper for CFGBlock::getTerminatorCondition()
1106/// which returns the element for ObjCForCollectionStmts.
1107static const Stmt *getTerminatorCondition(const CFGBlock *B) {
1108 const Stmt *S = B->getTerminatorCondition();
1109 if (const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(S))
1110 return FS->getElement();
1111 return S;
1112}
1113
1114constexpr llvm::StringLiteral StrEnteringLoop = "Entering loop body";
1115constexpr llvm::StringLiteral StrLoopBodyZero = "Loop body executed 0 times";
1116constexpr llvm::StringLiteral StrLoopRangeEmpty =
1117 "Loop body skipped when range is empty";
1118constexpr llvm::StringLiteral StrLoopCollectionEmpty =
1119 "Loop body skipped when collection is empty";
1120
1121static std::unique_ptr<FilesToLineNumsMap>
1123
1124void PathDiagnosticBuilder::generatePathDiagnosticsForNode(
1125 PathDiagnosticConstruct &C, PathDiagnosticLocation &PrevLoc) const {
1126 ProgramPoint P = C.getCurrentNode()->getLocation();
1127 const SourceManager &SM = getSourceManager();
1128
1129 // Have we encountered an entrance to a call? It may be
1130 // the case that we have not encountered a matching
1131 // call exit before this point. This means that the path
1132 // terminated within the call itself.
1133 if (auto CE = P.getAs<CallEnter>()) {
1134
1135 if (C.shouldAddPathEdges()) {
1136 // Add an edge to the start of the function.
1137 const StackFrameContext *CalleeLC = CE->getCalleeContext();
1138 const Decl *D = CalleeLC->getDecl();
1139 // Add the edge only when the callee has body. We jump to the beginning
1140 // of the *declaration*, however we expect it to be followed by the
1141 // body. This isn't the case for autosynthesized property accessors in
1142 // Objective-C. No need for a similar extra check for CallExit points
1143 // because the exit edge comes from a statement (i.e. return),
1144 // not from declaration.
1145 if (D->hasBody())
1146 addEdgeToPath(C.getActivePath(), PrevLoc,
1148 }
1149
1150 // Did we visit an entire call?
1151 bool VisitedEntireCall = C.PD->isWithinCall();
1152 C.PD->popActivePath();
1153
1155 if (VisitedEntireCall) {
1156 Call = cast<PathDiagnosticCallPiece>(C.getActivePath().front().get());
1157 } else {
1158 // The path terminated within a nested location context, create a new
1159 // call piece to encapsulate the rest of the path pieces.
1160 const Decl *Caller = CE->getLocationContext()->getDecl();
1161 Call = PathDiagnosticCallPiece::construct(C.getActivePath(), Caller);
1162 assert(C.getActivePath().size() == 1 &&
1163 C.getActivePath().front().get() == Call);
1164
1165 // Since we just transferred the path over to the call piece, reset the
1166 // mapping of the active path to the current location context.
1167 assert(C.isInLocCtxMap(&C.getActivePath()) &&
1168 "When we ascend to a previously unvisited call, the active path's "
1169 "address shouldn't change, but rather should be compacted into "
1170 "a single CallEvent!");
1171 C.updateLocCtxMap(&C.getActivePath(), C.getCurrLocationContext());
1172
1173 // Record the location context mapping for the path within the call.
1174 assert(!C.isInLocCtxMap(&Call->path) &&
1175 "When we ascend to a previously unvisited call, this must be the "
1176 "first time we encounter the caller context!");
1177 C.updateLocCtxMap(&Call->path, CE->getCalleeContext());
1178 }
1179 Call->setCallee(*CE, SM);
1180
1181 // Update the previous location in the active path.
1182 PrevLoc = Call->getLocation();
1183
1184 if (!C.CallStack.empty()) {
1185 assert(C.CallStack.back().first == Call);
1186 C.CallStack.pop_back();
1187 }
1188 return;
1189 }
1190
1191 assert(C.getCurrLocationContext() == C.getLocationContextForActivePath() &&
1192 "The current position in the bug path is out of sync with the "
1193 "location context associated with the active path!");
1194
1195 // Have we encountered an exit from a function call?
1196 if (std::optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1197
1198 // We are descending into a call (backwards). Construct
1199 // a new call piece to contain the path pieces for that call.
1201 // Record the mapping from call piece to LocationContext.
1202 assert(!C.isInLocCtxMap(&Call->path) &&
1203 "We just entered a call, this must've been the first time we "
1204 "encounter its context!");
1205 C.updateLocCtxMap(&Call->path, CE->getCalleeContext());
1206
1207 if (C.shouldAddPathEdges()) {
1208 // Add the edge to the return site.
1209 addEdgeToPath(C.getActivePath(), PrevLoc, Call->callReturn);
1210 PrevLoc.invalidate();
1211 }
1212
1213 auto *P = Call.get();
1214 C.getActivePath().push_front(std::move(Call));
1215
1216 // Make the contents of the call the active path for now.
1217 C.PD->pushActivePath(&P->path);
1218 C.CallStack.push_back(CallWithEntry(P, C.getCurrentNode()));
1219 return;
1220 }
1221
1222 if (auto PS = P.getAs<PostStmt>()) {
1223 if (!C.shouldAddPathEdges())
1224 return;
1225
1226 // Add an edge. If this is an ObjCForCollectionStmt do
1227 // not add an edge here as it appears in the CFG both
1228 // as a terminator and as a terminator condition.
1229 if (!isa<ObjCForCollectionStmt>(PS->getStmt())) {
1231 PathDiagnosticLocation(PS->getStmt(), SM, C.getCurrLocationContext());
1232 addEdgeToPath(C.getActivePath(), PrevLoc, L);
1233 }
1234
1235 } else if (auto BE = P.getAs<BlockEdge>()) {
1236
1237 if (C.shouldAddControlNotes()) {
1238 generateMinimalDiagForBlockEdge(C, *BE);
1239 }
1240
1241 if (!C.shouldAddPathEdges()) {
1242 return;
1243 }
1244
1245 // Are we jumping to the head of a loop? Add a special diagnostic.
1246 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1247 PathDiagnosticLocation L(Loop, SM, C.getCurrLocationContext());
1248 const Stmt *Body = nullptr;
1249
1250 if (const auto *FS = dyn_cast<ForStmt>(Loop))
1251 Body = FS->getBody();
1252 else if (const auto *WS = dyn_cast<WhileStmt>(Loop))
1253 Body = WS->getBody();
1254 else if (const auto *OFS = dyn_cast<ObjCForCollectionStmt>(Loop)) {
1255 Body = OFS->getBody();
1256 } else if (const auto *FRS = dyn_cast<CXXForRangeStmt>(Loop)) {
1257 Body = FRS->getBody();
1258 }
1259 // do-while statements are explicitly excluded here
1260
1261 auto p = std::make_shared<PathDiagnosticEventPiece>(
1262 L, "Looping back to the head of the loop");
1263 p->setPrunable(true);
1264
1265 addEdgeToPath(C.getActivePath(), PrevLoc, p->getLocation());
1266 // We might've added a very similar control node already
1267 if (!C.shouldAddControlNotes()) {
1268 C.getActivePath().push_front(std::move(p));
1269 }
1270
1271 if (const auto *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
1272 addEdgeToPath(C.getActivePath(), PrevLoc,
1274 }
1275 }
1276
1277 const CFGBlock *BSrc = BE->getSrc();
1278 const ParentMap &PM = C.getParentMap();
1279
1280 if (const Stmt *Term = BSrc->getTerminatorStmt()) {
1281 // Are we jumping past the loop body without ever executing the
1282 // loop (because the condition was false)?
1283 if (isLoop(Term)) {
1284 const Stmt *TermCond = getTerminatorCondition(BSrc);
1285 bool IsInLoopBody = isInLoopBody(
1286 PM, getStmtBeforeCond(PM, TermCond, C.getCurrentNode()), Term);
1287
1288 StringRef str;
1289
1290 if (isJumpToFalseBranch(&*BE)) {
1291 if (!IsInLoopBody) {
1292 if (isa<ObjCForCollectionStmt>(Term)) {
1294 } else if (isa<CXXForRangeStmt>(Term)) {
1295 str = StrLoopRangeEmpty;
1296 } else {
1297 str = StrLoopBodyZero;
1298 }
1299 }
1300 } else {
1301 str = StrEnteringLoop;
1302 }
1303
1304 if (!str.empty()) {
1305 PathDiagnosticLocation L(TermCond ? TermCond : Term, SM,
1306 C.getCurrLocationContext());
1307 auto PE = std::make_shared<PathDiagnosticEventPiece>(L, str);
1308 PE->setPrunable(true);
1309 addEdgeToPath(C.getActivePath(), PrevLoc, PE->getLocation());
1310
1311 // We might've added a very similar control node already
1312 if (!C.shouldAddControlNotes()) {
1313 C.getActivePath().push_front(std::move(PE));
1314 }
1315 }
1316 } else if (isa<BreakStmt, ContinueStmt, GotoStmt>(Term)) {
1317 PathDiagnosticLocation L(Term, SM, C.getCurrLocationContext());
1318 addEdgeToPath(C.getActivePath(), PrevLoc, L);
1319 }
1320 }
1321 }
1322}
1323
1324static std::unique_ptr<PathDiagnostic>
1326 const BugType &BT = R->getBugType();
1327 return std::make_unique<PathDiagnostic>(
1329 R->getDescription(), R->getShortDescription(/*UseFallback=*/false),
1331 std::make_unique<FilesToLineNumsMap>());
1332}
1333
1334static std::unique_ptr<PathDiagnostic>
1336 const SourceManager &SM) {
1337 const BugType &BT = R->getBugType();
1338 return std::make_unique<PathDiagnostic>(
1340 R->getDescription(), R->getShortDescription(/*UseFallback=*/false),
1343}
1344
1345static const Stmt *getStmtParent(const Stmt *S, const ParentMap &PM) {
1346 if (!S)
1347 return nullptr;
1348
1349 while (true) {
1350 S = PM.getParentIgnoreParens(S);
1351
1352 if (!S)
1353 break;
1354
1355 if (isa<FullExpr, CXXBindTemporaryExpr, SubstNonTypeTemplateParmExpr>(S))
1356 continue;
1357
1358 break;
1359 }
1360
1361 return S;
1362}
1363
1364static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) {
1365 switch (S->getStmtClass()) {
1366 case Stmt::BinaryOperatorClass: {
1367 const auto *BO = cast<BinaryOperator>(S);
1368 if (!BO->isLogicalOp())
1369 return false;
1370 return BO->getLHS() == Cond || BO->getRHS() == Cond;
1371 }
1372 case Stmt::IfStmtClass:
1373 return cast<IfStmt>(S)->getCond() == Cond;
1374 case Stmt::ForStmtClass:
1375 return cast<ForStmt>(S)->getCond() == Cond;
1376 case Stmt::WhileStmtClass:
1377 return cast<WhileStmt>(S)->getCond() == Cond;
1378 case Stmt::DoStmtClass:
1379 return cast<DoStmt>(S)->getCond() == Cond;
1380 case Stmt::ChooseExprClass:
1381 return cast<ChooseExpr>(S)->getCond() == Cond;
1382 case Stmt::IndirectGotoStmtClass:
1383 return cast<IndirectGotoStmt>(S)->getTarget() == Cond;
1384 case Stmt::SwitchStmtClass:
1385 return cast<SwitchStmt>(S)->getCond() == Cond;
1386 case Stmt::BinaryConditionalOperatorClass:
1387 return cast<BinaryConditionalOperator>(S)->getCond() == Cond;
1388 case Stmt::ConditionalOperatorClass: {
1389 const auto *CO = cast<ConditionalOperator>(S);
1390 return CO->getCond() == Cond ||
1391 CO->getLHS() == Cond ||
1392 CO->getRHS() == Cond;
1393 }
1394 case Stmt::ObjCForCollectionStmtClass:
1395 return cast<ObjCForCollectionStmt>(S)->getElement() == Cond;
1396 case Stmt::CXXForRangeStmtClass: {
1397 const auto *FRS = cast<CXXForRangeStmt>(S);
1398 return FRS->getCond() == Cond || FRS->getRangeInit() == Cond;
1399 }
1400 default:
1401 return false;
1402 }
1403}
1404
1405static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) {
1406 if (const auto *FS = dyn_cast<ForStmt>(FL))
1407 return FS->getInc() == S || FS->getInit() == S;
1408 if (const auto *FRS = dyn_cast<CXXForRangeStmt>(FL))
1409 return FRS->getInc() == S || FRS->getRangeStmt() == S ||
1410 FRS->getLoopVarStmt() || FRS->getRangeInit() == S;
1411 return false;
1412}
1413
1415
1416/// Adds synthetic edges from top-level statements to their subexpressions.
1417///
1418/// This avoids a "swoosh" effect, where an edge from a top-level statement A
1419/// points to a sub-expression B.1 that's not at the start of B. In these cases,
1420/// we'd like to see an edge from A to B, then another one from B to B.1.
1421static void addContextEdges(PathPieces &pieces, const LocationContext *LC) {
1422 const ParentMap &PM = LC->getParentMap();
1423 PathPieces::iterator Prev = pieces.end();
1424 for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E;
1425 Prev = I, ++I) {
1426 auto *Piece = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1427
1428 if (!Piece)
1429 continue;
1430
1431 PathDiagnosticLocation SrcLoc = Piece->getStartLocation();
1433
1434 PathDiagnosticLocation NextSrcContext = SrcLoc;
1435 const Stmt *InnerStmt = nullptr;
1436 while (NextSrcContext.isValid() && NextSrcContext.asStmt() != InnerStmt) {
1437 SrcContexts.push_back(NextSrcContext);
1438 InnerStmt = NextSrcContext.asStmt();
1439 NextSrcContext = getEnclosingStmtLocation(InnerStmt, LC,
1440 /*allowNested=*/true);
1441 }
1442
1443 // Repeatedly split the edge as necessary.
1444 // This is important for nested logical expressions (||, &&, ?:) where we
1445 // want to show all the levels of context.
1446 while (true) {
1447 const Stmt *Dst = Piece->getEndLocation().getStmtOrNull();
1448
1449 // We are looking at an edge. Is the destination within a larger
1450 // expression?
1451 PathDiagnosticLocation DstContext =
1452 getEnclosingStmtLocation(Dst, LC, /*allowNested=*/true);
1453 if (!DstContext.isValid() || DstContext.asStmt() == Dst)
1454 break;
1455
1456 // If the source is in the same context, we're already good.
1457 if (llvm::is_contained(SrcContexts, DstContext))
1458 break;
1459
1460 // Update the subexpression node to point to the context edge.
1461 Piece->setStartLocation(DstContext);
1462
1463 // Try to extend the previous edge if it's at the same level as the source
1464 // context.
1465 if (Prev != E) {
1466 auto *PrevPiece = dyn_cast<PathDiagnosticControlFlowPiece>(Prev->get());
1467
1468 if (PrevPiece) {
1469 if (const Stmt *PrevSrc =
1470 PrevPiece->getStartLocation().getStmtOrNull()) {
1471 const Stmt *PrevSrcParent = getStmtParent(PrevSrc, PM);
1472 if (PrevSrcParent ==
1473 getStmtParent(DstContext.getStmtOrNull(), PM)) {
1474 PrevPiece->setEndLocation(DstContext);
1475 break;
1476 }
1477 }
1478 }
1479 }
1480
1481 // Otherwise, split the current edge into a context edge and a
1482 // subexpression edge. Note that the context statement may itself have
1483 // context.
1484 auto P =
1485 std::make_shared<PathDiagnosticControlFlowPiece>(SrcLoc, DstContext);
1486 Piece = P.get();
1487 I = pieces.insert(I, std::move(P));
1488 }
1489 }
1490}
1491
1492/// Move edges from a branch condition to a branch target
1493/// when the condition is simple.
1494///
1495/// This restructures some of the work of addContextEdges. That function
1496/// creates edges this may destroy, but they work together to create a more
1497/// aesthetically set of edges around branches. After the call to
1498/// addContextEdges, we may have (1) an edge to the branch, (2) an edge from
1499/// the branch to the branch condition, and (3) an edge from the branch
1500/// condition to the branch target. We keep (1), but may wish to remove (2)
1501/// and move the source of (3) to the branch if the branch condition is simple.
1503 for (PathPieces::iterator I = pieces.begin(), E = pieces.end(); I != E; ++I) {
1504 const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1505
1506 if (!PieceI)
1507 continue;
1508
1509 const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1510 const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull();
1511
1512 if (!s1Start || !s1End)
1513 continue;
1514
1515 PathPieces::iterator NextI = I; ++NextI;
1516 if (NextI == E)
1517 break;
1518
1519 PathDiagnosticControlFlowPiece *PieceNextI = nullptr;
1520
1521 while (true) {
1522 if (NextI == E)
1523 break;
1524
1525 const auto *EV = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
1526 if (EV) {
1527 StringRef S = EV->getString();
1528 if (S == StrEnteringLoop || S == StrLoopBodyZero ||
1530 ++NextI;
1531 continue;
1532 }
1533 break;
1534 }
1535
1536 PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1537 break;
1538 }
1539
1540 if (!PieceNextI)
1541 continue;
1542
1543 const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1544 const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull();
1545
1546 if (!s2Start || !s2End || s1End != s2Start)
1547 continue;
1548
1549 // We only perform this transformation for specific branch kinds.
1550 // We don't want to do this for do..while, for example.
1552 CXXForRangeStmt>(s1Start))
1553 continue;
1554
1555 // Is s1End the branch condition?
1556 if (!isConditionForTerminator(s1Start, s1End))
1557 continue;
1558
1559 // Perform the hoisting by eliminating (2) and changing the start
1560 // location of (3).
1561 PieceNextI->setStartLocation(PieceI->getStartLocation());
1562 I = pieces.erase(I);
1563 }
1564}
1565
1566/// Returns the number of bytes in the given (character-based) SourceRange.
1567///
1568/// If the locations in the range are not on the same line, returns
1569/// std::nullopt.
1570///
1571/// Note that this does not do a precise user-visible character or column count.
1572static std::optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
1574 SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()),
1575 SM.getExpansionRange(Range.getEnd()).getEnd());
1576
1577 FileID FID = SM.getFileID(ExpansionRange.getBegin());
1578 if (FID != SM.getFileID(ExpansionRange.getEnd()))
1579 return std::nullopt;
1580
1581 std::optional<MemoryBufferRef> Buffer = SM.getBufferOrNone(FID);
1582 if (!Buffer)
1583 return std::nullopt;
1584
1585 unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin());
1586 unsigned EndOffset = SM.getFileOffset(ExpansionRange.getEnd());
1587 StringRef Snippet = Buffer->getBuffer().slice(BeginOffset, EndOffset);
1588
1589 // We're searching the raw bytes of the buffer here, which might include
1590 // escaped newlines and such. That's okay; we're trying to decide whether the
1591 // SourceRange is covering a large or small amount of space in the user's
1592 // editor.
1593 if (Snippet.find_first_of("\r\n") != StringRef::npos)
1594 return std::nullopt;
1595
1596 // This isn't Unicode-aware, but it doesn't need to be.
1597 return Snippet.size();
1598}
1599
1600/// \sa getLengthOnSingleLine(SourceManager, SourceRange)
1601static std::optional<size_t> getLengthOnSingleLine(const SourceManager &SM,
1602 const Stmt *S) {
1603 return getLengthOnSingleLine(SM, S->getSourceRange());
1604}
1605
1606/// Eliminate two-edge cycles created by addContextEdges().
1607///
1608/// Once all the context edges are in place, there are plenty of cases where
1609/// there's a single edge from a top-level statement to a subexpression,
1610/// followed by a single path note, and then a reverse edge to get back out to
1611/// the top level. If the statement is simple enough, the subexpression edges
1612/// just add noise and make it harder to understand what's going on.
1613///
1614/// This function only removes edges in pairs, because removing only one edge
1615/// might leave other edges dangling.
1616///
1617/// This will not remove edges in more complicated situations:
1618/// - if there is more than one "hop" leading to or from a subexpression.
1619/// - if there is an inlined call between the edges instead of a single event.
1620/// - if the whole statement is large enough that having subexpression arrows
1621/// might be helpful.
1623 for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) {
1624 // Pattern match the current piece and its successor.
1625 const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1626
1627 if (!PieceI) {
1628 ++I;
1629 continue;
1630 }
1631
1632 const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1633 const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull();
1634
1635 PathPieces::iterator NextI = I; ++NextI;
1636 if (NextI == E)
1637 break;
1638
1639 const auto *PieceNextI =
1640 dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1641
1642 if (!PieceNextI) {
1643 if (isa<PathDiagnosticEventPiece>(NextI->get())) {
1644 ++NextI;
1645 if (NextI == E)
1646 break;
1647 PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1648 }
1649
1650 if (!PieceNextI) {
1651 ++I;
1652 continue;
1653 }
1654 }
1655
1656 const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1657 const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull();
1658
1659 if (s1Start && s2Start && s1Start == s2End && s2Start == s1End) {
1660 const size_t MAX_SHORT_LINE_LENGTH = 80;
1661 std::optional<size_t> s1Length = getLengthOnSingleLine(SM, s1Start);
1662 if (s1Length && *s1Length <= MAX_SHORT_LINE_LENGTH) {
1663 std::optional<size_t> s2Length = getLengthOnSingleLine(SM, s2Start);
1664 if (s2Length && *s2Length <= MAX_SHORT_LINE_LENGTH) {
1665 Path.erase(I);
1666 I = Path.erase(NextI);
1667 continue;
1668 }
1669 }
1670 }
1671
1672 ++I;
1673 }
1674}
1675
1676/// Return true if X is contained by Y.
1677static bool lexicalContains(const ParentMap &PM, const Stmt *X, const Stmt *Y) {
1678 while (X) {
1679 if (X == Y)
1680 return true;
1681 X = PM.getParent(X);
1682 }
1683 return false;
1684}
1685
1686// Remove short edges on the same line less than 3 columns in difference.
1687static void removePunyEdges(PathPieces &path, const SourceManager &SM,
1688 const ParentMap &PM) {
1689 bool erased = false;
1690
1691 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E;
1692 erased ? I : ++I) {
1693 erased = false;
1694
1695 const auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1696
1697 if (!PieceI)
1698 continue;
1699
1700 const Stmt *start = PieceI->getStartLocation().getStmtOrNull();
1701 const Stmt *end = PieceI->getEndLocation().getStmtOrNull();
1702
1703 if (!start || !end)
1704 continue;
1705
1706 const Stmt *endParent = PM.getParent(end);
1707 if (!endParent)
1708 continue;
1709
1710 if (isConditionForTerminator(end, endParent))
1711 continue;
1712
1713 SourceLocation FirstLoc = start->getBeginLoc();
1714 SourceLocation SecondLoc = end->getBeginLoc();
1715
1716 if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc))
1717 continue;
1718 if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc))
1719 std::swap(SecondLoc, FirstLoc);
1720
1721 SourceRange EdgeRange(FirstLoc, SecondLoc);
1722 std::optional<size_t> ByteWidth = getLengthOnSingleLine(SM, EdgeRange);
1723
1724 // If the statements are on different lines, continue.
1725 if (!ByteWidth)
1726 continue;
1727
1728 const size_t MAX_PUNY_EDGE_LENGTH = 2;
1729 if (*ByteWidth <= MAX_PUNY_EDGE_LENGTH) {
1730 // FIXME: There are enough /bytes/ between the endpoints of the edge, but
1731 // there might not be enough /columns/. A proper user-visible column count
1732 // is probably too expensive, though.
1733 I = path.erase(I);
1734 erased = true;
1735 continue;
1736 }
1737 }
1738}
1739
1741 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I) {
1742 const auto *PieceI = dyn_cast<PathDiagnosticEventPiece>(I->get());
1743
1744 if (!PieceI)
1745 continue;
1746
1747 PathPieces::iterator NextI = I; ++NextI;
1748 if (NextI == E)
1749 return;
1750
1751 const auto *PieceNextI = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
1752
1753 if (!PieceNextI)
1754 continue;
1755
1756 // Erase the second piece if it has the same exact message text.
1757 if (PieceI->getString() == PieceNextI->getString()) {
1758 path.erase(NextI);
1759 }
1760 }
1761}
1762
1763static bool optimizeEdges(const PathDiagnosticConstruct &C, PathPieces &path,
1764 OptimizedCallsSet &OCS) {
1765 bool hasChanges = false;
1766 const LocationContext *LC = C.getLocationContextFor(&path);
1767 assert(LC);
1768 const ParentMap &PM = LC->getParentMap();
1769 const SourceManager &SM = C.getSourceManager();
1770
1771 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) {
1772 // Optimize subpaths.
1773 if (auto *CallI = dyn_cast<PathDiagnosticCallPiece>(I->get())) {
1774 // Record the fact that a call has been optimized so we only do the
1775 // effort once.
1776 if (!OCS.count(CallI)) {
1777 while (optimizeEdges(C, CallI->path, OCS)) {
1778 }
1779 OCS.insert(CallI);
1780 }
1781 ++I;
1782 continue;
1783 }
1784
1785 // Pattern match the current piece and its successor.
1786 auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
1787
1788 if (!PieceI) {
1789 ++I;
1790 continue;
1791 }
1792
1793 const Stmt *s1Start = PieceI->getStartLocation().getStmtOrNull();
1794 const Stmt *s1End = PieceI->getEndLocation().getStmtOrNull();
1795 const Stmt *level1 = getStmtParent(s1Start, PM);
1796 const Stmt *level2 = getStmtParent(s1End, PM);
1797
1798 PathPieces::iterator NextI = I; ++NextI;
1799 if (NextI == E)
1800 break;
1801
1802 const auto *PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
1803
1804 if (!PieceNextI) {
1805 ++I;
1806 continue;
1807 }
1808
1809 const Stmt *s2Start = PieceNextI->getStartLocation().getStmtOrNull();
1810 const Stmt *s2End = PieceNextI->getEndLocation().getStmtOrNull();
1811 const Stmt *level3 = getStmtParent(s2Start, PM);
1812 const Stmt *level4 = getStmtParent(s2End, PM);
1813
1814 // Rule I.
1815 //
1816 // If we have two consecutive control edges whose end/begin locations
1817 // are at the same level (e.g. statements or top-level expressions within
1818 // a compound statement, or siblings share a single ancestor expression),
1819 // then merge them if they have no interesting intermediate event.
1820 //
1821 // For example:
1822 //
1823 // (1.1 -> 1.2) -> (1.2 -> 1.3) becomes (1.1 -> 1.3) because the common
1824 // parent is '1'. Here 'x.y.z' represents the hierarchy of statements.
1825 //
1826 // NOTE: this will be limited later in cases where we add barriers
1827 // to prevent this optimization.
1828 if (level1 && level1 == level2 && level1 == level3 && level1 == level4) {
1829 PieceI->setEndLocation(PieceNextI->getEndLocation());
1830 path.erase(NextI);
1831 hasChanges = true;
1832 continue;
1833 }
1834
1835 // Rule II.
1836 //
1837 // Eliminate edges between subexpressions and parent expressions
1838 // when the subexpression is consumed.
1839 //
1840 // NOTE: this will be limited later in cases where we add barriers
1841 // to prevent this optimization.
1842 if (s1End && s1End == s2Start && level2) {
1843 bool removeEdge = false;
1844 // Remove edges into the increment or initialization of a
1845 // loop that have no interleaving event. This means that
1846 // they aren't interesting.
1847 if (isIncrementOrInitInForLoop(s1End, level2))
1848 removeEdge = true;
1849 // Next only consider edges that are not anchored on
1850 // the condition of a terminator. This are intermediate edges
1851 // that we might want to trim.
1852 else if (!isConditionForTerminator(level2, s1End)) {
1853 // Trim edges on expressions that are consumed by
1854 // the parent expression.
1855 if (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End))) {
1856 removeEdge = true;
1857 }
1858 // Trim edges where a lexical containment doesn't exist.
1859 // For example:
1860 //
1861 // X -> Y -> Z
1862 //
1863 // If 'Z' lexically contains Y (it is an ancestor) and
1864 // 'X' does not lexically contain Y (it is a descendant OR
1865 // it has no lexical relationship at all) then trim.
1866 //
1867 // This can eliminate edges where we dive into a subexpression
1868 // and then pop back out, etc.
1869 else if (s1Start && s2End &&
1870 lexicalContains(PM, s2Start, s2End) &&
1871 !lexicalContains(PM, s1End, s1Start)) {
1872 removeEdge = true;
1873 }
1874 // Trim edges from a subexpression back to the top level if the
1875 // subexpression is on a different line.
1876 //
1877 // A.1 -> A -> B
1878 // becomes
1879 // A.1 -> B
1880 //
1881 // These edges just look ugly and don't usually add anything.
1882 else if (s1Start && s2End &&
1883 lexicalContains(PM, s1Start, s1End)) {
1884 SourceRange EdgeRange(PieceI->getEndLocation().asLocation(),
1885 PieceI->getStartLocation().asLocation());
1886 if (!getLengthOnSingleLine(SM, EdgeRange))
1887 removeEdge = true;
1888 }
1889 }
1890
1891 if (removeEdge) {
1892 PieceI->setEndLocation(PieceNextI->getEndLocation());
1893 path.erase(NextI);
1894 hasChanges = true;
1895 continue;
1896 }
1897 }
1898
1899 // Optimize edges for ObjC fast-enumeration loops.
1900 //
1901 // (X -> collection) -> (collection -> element)
1902 //
1903 // becomes:
1904 //
1905 // (X -> element)
1906 if (s1End == s2Start) {
1907 const auto *FS = dyn_cast_or_null<ObjCForCollectionStmt>(level3);
1908 if (FS && FS->getCollection()->IgnoreParens() == s2Start &&
1909 s2End == FS->getElement()) {
1910 PieceI->setEndLocation(PieceNextI->getEndLocation());
1911 path.erase(NextI);
1912 hasChanges = true;
1913 continue;
1914 }
1915 }
1916
1917 // No changes at this index? Move to the next one.
1918 ++I;
1919 }
1920
1921 if (!hasChanges) {
1922 // Adjust edges into subexpressions to make them more uniform
1923 // and aesthetically pleasing.
1924 addContextEdges(path, LC);
1925 // Remove "cyclical" edges that include one or more context edges.
1926 removeContextCycles(path, SM);
1927 // Hoist edges originating from branch conditions to branches
1928 // for simple branches.
1930 // Remove any puny edges left over after primary optimization pass.
1931 removePunyEdges(path, SM, PM);
1932 // Remove identical events.
1934 }
1935
1936 return hasChanges;
1937}
1938
1939/// Drop the very first edge in a path, which should be a function entry edge.
1940///
1941/// If the first edge is not a function entry edge (say, because the first
1942/// statement had an invalid source location), this function does nothing.
1943// FIXME: We should just generate invalid edges anyway and have the optimizer
1944// deal with them.
1945static void dropFunctionEntryEdge(const PathDiagnosticConstruct &C,
1946 PathPieces &Path) {
1947 const auto *FirstEdge =
1948 dyn_cast<PathDiagnosticControlFlowPiece>(Path.front().get());
1949 if (!FirstEdge)
1950 return;
1951
1952 const Decl *D = C.getLocationContextFor(&Path)->getDecl();
1953 PathDiagnosticLocation EntryLoc =
1954 PathDiagnosticLocation::createBegin(D, C.getSourceManager());
1955 if (FirstEdge->getStartLocation() != EntryLoc)
1956 return;
1957
1958 Path.pop_front();
1959}
1960
1961/// Populate executes lines with lines containing at least one diagnostics.
1963
1964 PathPieces path = PD.path.flatten(/*ShouldFlattenMacros=*/true);
1965 FilesToLineNumsMap &ExecutedLines = PD.getExecutedLines();
1966
1967 for (const auto &P : path) {
1968 FullSourceLoc Loc = P->getLocation().asLocation().getExpansionLoc();
1969 FileID FID = Loc.getFileID();
1970 unsigned LineNo = Loc.getLineNumber();
1971 assert(FID.isValid());
1972 ExecutedLines[FID].insert(LineNo);
1973 }
1974}
1975
1976PathDiagnosticConstruct::PathDiagnosticConstruct(
1977 const PathDiagnosticConsumer *PDC, const ExplodedNode *ErrorNode,
1978 const PathSensitiveBugReport *R)
1979 : Consumer(PDC), CurrentNode(ErrorNode),
1980 SM(CurrentNode->getCodeDecl().getASTContext().getSourceManager()),
1981 PD(generateEmptyDiagnosticForReport(R, getSourceManager())) {
1982 LCM[&PD->getActivePath()] = ErrorNode->getLocationContext();
1983}
1984
1985PathDiagnosticBuilder::PathDiagnosticBuilder(
1986 BugReporterContext BRC, std::unique_ptr<ExplodedGraph> BugPath,
1987 PathSensitiveBugReport *r, const ExplodedNode *ErrorNode,
1988 std::unique_ptr<VisitorsDiagnosticsTy> VisitorsDiagnostics)
1989 : BugReporterContext(BRC), BugPath(std::move(BugPath)), R(r),
1990 ErrorNode(ErrorNode),
1991 VisitorsDiagnostics(std::move(VisitorsDiagnostics)) {}
1992
1993std::unique_ptr<PathDiagnostic>
1994PathDiagnosticBuilder::generate(const PathDiagnosticConsumer *PDC) const {
1995 PathDiagnosticConstruct Construct(PDC, ErrorNode, R);
1996
1997 const SourceManager &SM = getSourceManager();
1998 const AnalyzerOptions &Opts = getAnalyzerOptions();
1999
2000 if (!PDC->shouldGenerateDiagnostics())
2001 return generateEmptyDiagnosticForReport(R, getSourceManager());
2002
2003 // Construct the final (warning) event for the bug report.
2004 auto EndNotes = VisitorsDiagnostics->find(ErrorNode);
2005 PathDiagnosticPieceRef LastPiece;
2006 if (EndNotes != VisitorsDiagnostics->end()) {
2007 assert(!EndNotes->second.empty());
2008 LastPiece = EndNotes->second[0];
2009 } else {
2010 LastPiece = BugReporterVisitor::getDefaultEndPath(*this, ErrorNode,
2011 *getBugReport());
2012 }
2013 Construct.PD->setEndOfPath(LastPiece);
2014
2015 PathDiagnosticLocation PrevLoc = Construct.PD->getLocation();
2016 // From the error node to the root, ascend the bug path and construct the bug
2017 // report.
2018 while (Construct.ascendToPrevNode()) {
2019 generatePathDiagnosticsForNode(Construct, PrevLoc);
2020
2021 auto VisitorNotes = VisitorsDiagnostics->find(Construct.getCurrentNode());
2022 if (VisitorNotes == VisitorsDiagnostics->end())
2023 continue;
2024
2025 // This is a workaround due to inability to put shared PathDiagnosticPiece
2026 // into a FoldingSet.
2027 std::set<llvm::FoldingSetNodeID> DeduplicationSet;
2028
2029 // Add pieces from custom visitors.
2030 for (const PathDiagnosticPieceRef &Note : VisitorNotes->second) {
2031 llvm::FoldingSetNodeID ID;
2032 Note->Profile(ID);
2033 if (!DeduplicationSet.insert(ID).second)
2034 continue;
2035
2036 if (PDC->shouldAddPathEdges())
2037 addEdgeToPath(Construct.getActivePath(), PrevLoc, Note->getLocation());
2038 updateStackPiecesWithMessage(Note, Construct.CallStack);
2039 Construct.getActivePath().push_front(Note);
2040 }
2041 }
2042
2043 if (PDC->shouldAddPathEdges()) {
2044 // Add an edge to the start of the function.
2045 // We'll prune it out later, but it helps make diagnostics more uniform.
2046 const StackFrameContext *CalleeLC =
2047 Construct.getLocationContextForActivePath()->getStackFrame();
2048 const Decl *D = CalleeLC->getDecl();
2049 addEdgeToPath(Construct.getActivePath(), PrevLoc,
2051 }
2052
2053
2054 // Finally, prune the diagnostic path of uninteresting stuff.
2055 if (!Construct.PD->path.empty()) {
2056 if (R->shouldPrunePath() && Opts.ShouldPrunePaths) {
2057 bool stillHasNotes =
2058 removeUnneededCalls(Construct, Construct.getMutablePieces(), R);
2059 assert(stillHasNotes);
2060 (void)stillHasNotes;
2061 }
2062
2063 // Remove pop-up notes if needed.
2064 if (!Opts.ShouldAddPopUpNotes)
2065 removePopUpNotes(Construct.getMutablePieces());
2066
2067 // Redirect all call pieces to have valid locations.
2068 adjustCallLocations(Construct.getMutablePieces());
2069 removePiecesWithInvalidLocations(Construct.getMutablePieces());
2070
2071 if (PDC->shouldAddPathEdges()) {
2072
2073 // Reduce the number of edges from a very conservative set
2074 // to an aesthetically pleasing subset that conveys the
2075 // necessary information.
2077 while (optimizeEdges(Construct, Construct.getMutablePieces(), OCS)) {
2078 }
2079
2080 // Drop the very first function-entry edge. It's not really necessary
2081 // for top-level functions.
2082 dropFunctionEntryEdge(Construct, Construct.getMutablePieces());
2083 }
2084
2085 // Remove messages that are basically the same, and edges that may not
2086 // make sense.
2087 // We have to do this after edge optimization in the Extensive mode.
2088 removeRedundantMsgs(Construct.getMutablePieces());
2089 removeEdgesToDefaultInitializers(Construct.getMutablePieces());
2090 }
2091
2092 if (Opts.ShouldDisplayMacroExpansions)
2093 CompactMacroExpandedPieces(Construct.getMutablePieces(), SM);
2094
2095 return std::move(Construct.PD);
2096}
2097
2098//===----------------------------------------------------------------------===//
2099// Methods for BugType and subclasses.
2100//===----------------------------------------------------------------------===//
2101
2102void BugType::anchor() {}
2103
2104void BuiltinBug::anchor() {}
2105
2106//===----------------------------------------------------------------------===//
2107// Methods for BugReport and subclasses.
2108//===----------------------------------------------------------------------===//
2109
2110LLVM_ATTRIBUTE_USED static bool
2111isDependency(const CheckerRegistryData &Registry, StringRef CheckerName) {
2112 for (const std::pair<StringRef, StringRef> &Pair : Registry.Dependencies) {
2113 if (Pair.second == CheckerName)
2114 return true;
2115 }
2116 return false;
2117}
2118
2119LLVM_ATTRIBUTE_USED static bool isHidden(const CheckerRegistryData &Registry,
2120 StringRef CheckerName) {
2121 for (const CheckerInfo &Checker : Registry.Checkers) {
2122 if (Checker.FullName == CheckerName)
2123 return Checker.IsHidden;
2124 }
2125 llvm_unreachable(
2126 "Checker name not found in CheckerRegistry -- did you retrieve it "
2127 "correctly from CheckerManager::getCurrentCheckerName?");
2128}
2129
2131 const BugType &bt, StringRef shortDesc, StringRef desc,
2132 const ExplodedNode *errorNode, PathDiagnosticLocation LocationToUnique,
2133 const Decl *DeclToUnique)
2134 : BugReport(Kind::PathSensitive, bt, shortDesc, desc), ErrorNode(errorNode),
2135 ErrorNodeRange(getStmt() ? getStmt()->getSourceRange() : SourceRange()),
2136 UniqueingLocation(LocationToUnique), UniqueingDecl(DeclToUnique) {
2138 ->getAnalysisManager()
2139 .getCheckerManager()
2140 ->getCheckerRegistryData(),
2141 bt.getCheckerName()) &&
2142 "Some checkers depend on this one! We don't allow dependency "
2143 "checkers to emit warnings, because checkers should depend on "
2144 "*modeling*, not *diagnostics*.");
2145
2146 assert(
2147 (bt.getCheckerName().startswith("debug") ||
2149 ->getAnalysisManager()
2150 .getCheckerManager()
2151 ->getCheckerRegistryData(),
2152 bt.getCheckerName())) &&
2153 "Hidden checkers musn't emit diagnostics as they are by definition "
2154 "non-user facing!");
2155}
2156
2158 std::unique_ptr<BugReporterVisitor> visitor) {
2159 if (!visitor)
2160 return;
2161
2162 llvm::FoldingSetNodeID ID;
2163 visitor->Profile(ID);
2164
2165 void *InsertPos = nullptr;
2166 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) {
2167 return;
2168 }
2169
2170 Callbacks.push_back(std::move(visitor));
2171}
2172
2174 Callbacks.clear();
2175}
2176
2178 const ExplodedNode *N = getErrorNode();
2179 if (!N)
2180 return nullptr;
2181
2182 const LocationContext *LC = N->getLocationContext();
2183 return LC->getStackFrame()->getDecl();
2184}
2185
2186void BasicBugReport::Profile(llvm::FoldingSetNodeID& hash) const {
2187 hash.AddInteger(static_cast<int>(getKind()));
2188 hash.AddPointer(&BT);
2189 hash.AddString(Description);
2190 assert(Location.isValid());
2191 Location.Profile(hash);
2192
2193 for (SourceRange range : Ranges) {
2194 if (!range.isValid())
2195 continue;
2196 hash.Add(range.getBegin());
2197 hash.Add(range.getEnd());
2198 }
2199}
2200
2201void PathSensitiveBugReport::Profile(llvm::FoldingSetNodeID &hash) const {
2202 hash.AddInteger(static_cast<int>(getKind()));
2203 hash.AddPointer(&BT);
2204 hash.AddString(Description);
2206 if (UL.isValid()) {
2207 UL.Profile(hash);
2208 } else {
2209 // TODO: The statement may be null if the report was emitted before any
2210 // statements were executed. In particular, some checkers by design
2211 // occasionally emit their reports in empty functions (that have no
2212 // statements in their body). Do we profile correctly in this case?
2214 }
2215
2216 for (SourceRange range : Ranges) {
2217 if (!range.isValid())
2218 continue;
2219 hash.Add(range.getBegin());
2220 hash.Add(range.getEnd());
2221 }
2222}
2223
2224template <class T>
2226 llvm::DenseMap<T, bugreporter::TrackingKind> &InterestingnessMap, T Val,
2228 auto Result = InterestingnessMap.insert({Val, TKind});
2229
2230 if (Result.second)
2231 return;
2232
2233 // Even if this symbol/region was already marked as interesting as a
2234 // condition, if we later mark it as interesting again but with
2235 // thorough tracking, overwrite it. Entities marked with thorough
2236 // interestiness are the most important (or most interesting, if you will),
2237 // and we wouldn't like to downplay their importance.
2238
2239 switch (TKind) {
2241 Result.first->getSecond() = bugreporter::TrackingKind::Thorough;
2242 return;
2244 return;
2245 }
2246
2247 llvm_unreachable(
2248 "BugReport::markInteresting currently can only handle 2 different "
2249 "tracking kinds! Please define what tracking kind should this entitiy"
2250 "have, if it was already marked as interesting with a different kind!");
2251}
2252
2255 if (!sym)
2256 return;
2257
2259
2260 // FIXME: No tests exist for this code and it is questionable:
2261 // How to handle multiple metadata for the same region?
2262 if (const auto *meta = dyn_cast<SymbolMetadata>(sym))
2263 markInteresting(meta->getRegion(), TKind);
2264}
2265
2267 if (!sym)
2268 return;
2269 InterestingSymbols.erase(sym);
2270
2271 // The metadata part of markInteresting is not reversed here.
2272 // Just making the same region not interesting is incorrect
2273 // in specific cases.
2274 if (const auto *meta = dyn_cast<SymbolMetadata>(sym))
2275 markNotInteresting(meta->getRegion());
2276}
2277
2280 if (!R)
2281 return;
2282
2283 R = R->getBaseRegion();
2285
2286 if (const auto *SR = dyn_cast<SymbolicRegion>(R))
2287 markInteresting(SR->getSymbol(), TKind);
2288}
2289
2291 if (!R)
2292 return;
2293
2294 R = R->getBaseRegion();
2295 InterestingRegions.erase(R);
2296
2297 if (const auto *SR = dyn_cast<SymbolicRegion>(R))
2298 markNotInteresting(SR->getSymbol());
2299}
2300
2303 markInteresting(V.getAsRegion(), TKind);
2304 markInteresting(V.getAsSymbol(), TKind);
2305}
2306
2308 if (!LC)
2309 return;
2310 InterestingLocationContexts.insert(LC);
2311}
2312
2313std::optional<bugreporter::TrackingKind>
2315 auto RKind = getInterestingnessKind(V.getAsRegion());
2316 auto SKind = getInterestingnessKind(V.getAsSymbol());
2317 if (!RKind)
2318 return SKind;
2319 if (!SKind)
2320 return RKind;
2321
2322 // If either is marked with throrough tracking, return that, we wouldn't like
2323 // to downplay a note's importance by 'only' mentioning it as a condition.
2324 switch(*RKind) {
2326 return RKind;
2328 return SKind;
2329 }
2330
2331 llvm_unreachable(
2332 "BugReport::getInterestingnessKind currently can only handle 2 different "
2333 "tracking kinds! Please define what tracking kind should we return here "
2334 "when the kind of getAsRegion() and getAsSymbol() is different!");
2335 return std::nullopt;
2336}
2337
2338std::optional<bugreporter::TrackingKind>
2340 if (!sym)
2341 return std::nullopt;
2342 // We don't currently consider metadata symbols to be interesting
2343 // even if we know their region is interesting. Is that correct behavior?
2344 auto It = InterestingSymbols.find(sym);
2345 if (It == InterestingSymbols.end())
2346 return std::nullopt;
2347 return It->getSecond();
2348}
2349
2350std::optional<bugreporter::TrackingKind>
2352 if (!R)
2353 return std::nullopt;
2354
2355 R = R->getBaseRegion();
2356 auto It = InterestingRegions.find(R);
2357 if (It != InterestingRegions.end())
2358 return It->getSecond();
2359
2360 if (const auto *SR = dyn_cast<SymbolicRegion>(R))
2361 return getInterestingnessKind(SR->getSymbol());
2362 return std::nullopt;
2363}
2364
2366 return getInterestingnessKind(V).has_value();
2367}
2368
2370 return getInterestingnessKind(sym).has_value();
2371}
2372
2374 return getInterestingnessKind(R).has_value();
2375}
2376
2378 if (!LC)
2379 return false;
2380 return InterestingLocationContexts.count(LC);
2381}
2382
2384 if (!ErrorNode)
2385 return nullptr;
2386
2388 const Stmt *S = nullptr;
2389
2390 if (std::optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) {
2391 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
2392 if (BE->getBlock() == &Exit)
2394 }
2395 if (!S)
2397
2398 return S;
2399}
2400
2403 // If no custom ranges, add the range of the statement corresponding to
2404 // the error node.
2405 if (Ranges.empty() && isa_and_nonnull<Expr>(getStmt()))
2406 return ErrorNodeRange;
2407
2408 return Ranges;
2409}
2410
2413 assert(ErrorNode && "Cannot create a location with a null node.");
2414 const Stmt *S = ErrorNode->getStmtForDiagnostics();
2416 const LocationContext *LC = P.getLocationContext();
2417 SourceManager &SM =
2418 ErrorNode->getState()->getStateManager().getContext().getSourceManager();
2419
2420 if (!S) {
2421 // If this is an implicit call, return the implicit call point location.
2422 if (std::optional<PreImplicitCall> PIE = P.getAs<PreImplicitCall>())
2423 return PathDiagnosticLocation(PIE->getLocation(), SM);
2424 if (auto FE = P.getAs<FunctionExitPoint>()) {
2425 if (const ReturnStmt *RS = FE->getStmt())
2427 }
2429 }
2430
2431 if (S) {
2432 // For member expressions, return the location of the '.' or '->'.
2433 if (const auto *ME = dyn_cast<MemberExpr>(S))
2435
2436 // For binary operators, return the location of the operator.
2437 if (const auto *B = dyn_cast<BinaryOperator>(S))
2439
2440 if (P.getAs<PostStmtPurgeDeadSymbols>())
2442
2443 if (S->getBeginLoc().isValid())
2444 return PathDiagnosticLocation(S, SM, LC);
2445
2448 }
2449
2451 SM);
2452}
2453
2454//===----------------------------------------------------------------------===//
2455// Methods for BugReporter and subclasses.
2456//===----------------------------------------------------------------------===//
2457
2459 return Eng.getGraph();
2460}
2461
2463 return Eng.getStateManager();
2464}
2465
2468 // Make sure reports are flushed.
2469 assert(StrBugTypes.empty() &&
2470 "Destroying BugReporter before diagnostics are emitted!");
2471
2472 // Free the bug reports we are tracking.
2473 for (const auto I : EQClassesVector)
2474 delete I;
2475}
2476
2478 // We need to flush reports in deterministic order to ensure the order
2479 // of the reports is consistent between runs.
2480 for (const auto EQ : EQClassesVector)
2481 FlushReport(*EQ);
2482
2483 // BugReporter owns and deletes only BugTypes created implicitly through
2484 // EmitBasicReport.
2485 // FIXME: There are leaks from checkers that assume that the BugTypes they
2486 // create will be destroyed by the BugReporter.
2487 StrBugTypes.clear();
2488}
2489
2490//===----------------------------------------------------------------------===//
2491// PathDiagnostics generation.
2492//===----------------------------------------------------------------------===//
2493
2494namespace {
2495
2496/// A wrapper around an ExplodedGraph that contains a single path from the root
2497/// to the error node.
2498class BugPathInfo {
2499public:
2500 std::unique_ptr<ExplodedGraph> BugPath;
2501 PathSensitiveBugReport *Report;
2502 const ExplodedNode *ErrorNode;
2503};
2504
2505/// A wrapper around an ExplodedGraph whose leafs are all error nodes. Can
2506/// conveniently retrieve bug paths from a single error node to the root.
2507class BugPathGetter {
2508 std::unique_ptr<ExplodedGraph> TrimmedGraph;
2509
2510 using PriorityMapTy = llvm::DenseMap<const ExplodedNode *, unsigned>;
2511
2512 /// Assign each node with its distance from the root.
2513 PriorityMapTy PriorityMap;
2514
2515 /// Since the getErrorNode() or BugReport refers to the original ExplodedGraph,
2516 /// we need to pair it to the error node of the constructed trimmed graph.
2517 using ReportNewNodePair =
2518 std::pair<PathSensitiveBugReport *, const ExplodedNode *>;
2520
2521 BugPathInfo CurrentBugPath;
2522
2523 /// A helper class for sorting ExplodedNodes by priority.
2524 template <bool Descending>
2525 class PriorityCompare {
2526 const PriorityMapTy &PriorityMap;
2527
2528 public:
2529 PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {}
2530
2531 bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const {
2532 PriorityMapTy::const_iterator LI = PriorityMap.find(LHS);
2533 PriorityMapTy::const_iterator RI = PriorityMap.find(RHS);
2534 PriorityMapTy::const_iterator E = PriorityMap.end();
2535
2536 if (LI == E)
2537 return Descending;
2538 if (RI == E)
2539 return !Descending;
2540
2541 return Descending ? LI->second > RI->second
2542 : LI->second < RI->second;
2543 }
2544
2545 bool operator()(const ReportNewNodePair &LHS,
2546 const ReportNewNodePair &RHS) const {
2547 return (*this)(LHS.second, RHS.second);
2548 }
2549 };
2550
2551public:
2552 BugPathGetter(const ExplodedGraph *OriginalGraph,
2554
2555 BugPathInfo *getNextBugPath();
2556};
2557
2558} // namespace
2559
2560BugPathGetter::BugPathGetter(const ExplodedGraph *OriginalGraph,
2563 for (const auto I : bugReports) {
2564 assert(I->isValid() &&
2565 "We only allow BugReporterVisitors and BugReporter itself to "
2566 "invalidate reports!");
2567 Nodes.emplace_back(I->getErrorNode());
2568 }
2569
2570 // The trimmed graph is created in the body of the constructor to ensure
2571 // that the DenseMaps have been initialized already.
2572 InterExplodedGraphMap ForwardMap;
2573 TrimmedGraph = OriginalGraph->trim(Nodes, &ForwardMap);
2574
2575 // Find the (first) error node in the trimmed graph. We just need to consult
2576 // the node map which maps from nodes in the original graph to nodes
2577 // in the new graph.
2579
2580 for (PathSensitiveBugReport *Report : bugReports) {
2581 const ExplodedNode *NewNode = ForwardMap.lookup(Report->getErrorNode());
2582 assert(NewNode &&
2583 "Failed to construct a trimmed graph that contains this error "
2584 "node!");
2585 ReportNodes.emplace_back(Report, NewNode);
2586 RemainingNodes.insert(NewNode);
2587 }
2588
2589 assert(!RemainingNodes.empty() && "No error node found in the trimmed graph");
2590
2591 // Perform a forward BFS to find all the shortest paths.
2592 std::queue<const ExplodedNode *> WS;
2593
2594 assert(TrimmedGraph->num_roots() == 1);
2595 WS.push(*TrimmedGraph->roots_begin());
2596 unsigned Priority = 0;
2597
2598 while (!WS.empty()) {
2599 const ExplodedNode *Node = WS.front();
2600 WS.pop();
2601
2602 PriorityMapTy::iterator PriorityEntry;
2603 bool IsNew;
2604 std::tie(PriorityEntry, IsNew) = PriorityMap.insert({Node, Priority});
2605 ++Priority;
2606
2607 if (!IsNew) {
2608 assert(PriorityEntry->second <= Priority);
2609 continue;
2610 }
2611
2612 if (RemainingNodes.erase(Node))
2613 if (RemainingNodes.empty())
2614 break;
2615
2616 for (const ExplodedNode *Succ : Node->succs())
2617 WS.push(Succ);
2618 }
2619
2620 // Sort the error paths from longest to shortest.
2621 llvm::sort(ReportNodes, PriorityCompare<true>(PriorityMap));
2622}
2623
2624BugPathInfo *BugPathGetter::getNextBugPath() {
2625 if (ReportNodes.empty())
2626 return nullptr;
2627
2628 const ExplodedNode *OrigN;
2629 std::tie(CurrentBugPath.Report, OrigN) = ReportNodes.pop_back_val();
2630 assert(PriorityMap.contains(OrigN) && "error node not accessible from root");
2631
2632 // Create a new graph with a single path. This is the graph that will be
2633 // returned to the caller.
2634 auto GNew = std::make_unique<ExplodedGraph>();
2635
2636 // Now walk from the error node up the BFS path, always taking the
2637 // predeccessor with the lowest number.
2638 ExplodedNode *Succ = nullptr;
2639 while (true) {
2640 // Create the equivalent node in the new graph with the same state
2641 // and location.
2642 ExplodedNode *NewN = GNew->createUncachedNode(
2643 OrigN->getLocation(), OrigN->getState(),
2644 OrigN->getID(), OrigN->isSink());
2645
2646 // Link up the new node with the previous node.
2647 if (Succ)
2648 Succ->addPredecessor(NewN, *GNew);
2649 else
2650 CurrentBugPath.ErrorNode = NewN;
2651
2652 Succ = NewN;
2653
2654 // Are we at the final node?
2655 if (OrigN->pred_empty()) {
2656 GNew->addRoot(NewN);
2657 break;
2658 }
2659
2660 // Find the next predeccessor node. We choose the node that is marked
2661 // with the lowest BFS number.
2662 OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(),
2663 PriorityCompare<false>(PriorityMap));
2664 }
2665
2666 CurrentBugPath.BugPath = std::move(GNew);
2667
2668 return &CurrentBugPath;
2669}
2670
2671/// CompactMacroExpandedPieces - This function postprocesses a PathDiagnostic
2672/// object and collapses PathDiagosticPieces that are expanded by macros.
2674 const SourceManager& SM) {
2675 using MacroStackTy = std::vector<
2676 std::pair<std::shared_ptr<PathDiagnosticMacroPiece>, SourceLocation>>;
2677
2678 using PiecesTy = std::vector<PathDiagnosticPieceRef>;
2679
2680 MacroStackTy MacroStack;
2681 PiecesTy Pieces;
2682
2683 for (PathPieces::const_iterator I = path.begin(), E = path.end();
2684 I != E; ++I) {
2685 const auto &piece = *I;
2686
2687 // Recursively compact calls.
2688 if (auto *call = dyn_cast<PathDiagnosticCallPiece>(&*piece)) {
2689 CompactMacroExpandedPieces(call->path, SM);
2690 }
2691
2692 // Get the location of the PathDiagnosticPiece.
2693 const FullSourceLoc Loc = piece->getLocation().asLocation();
2694
2695 // Determine the instantiation location, which is the location we group
2696 // related PathDiagnosticPieces.
2697 SourceLocation InstantiationLoc = Loc.isMacroID() ?
2698 SM.getExpansionLoc(Loc) :
2700
2701 if (Loc.isFileID()) {
2702 MacroStack.clear();
2703 Pieces.push_back(piece);
2704 continue;
2705 }
2706
2707 assert(Loc.isMacroID());
2708
2709 // Is the PathDiagnosticPiece within the same macro group?
2710 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
2711 MacroStack.back().first->subPieces.push_back(piece);
2712 continue;
2713 }
2714
2715 // We aren't in the same group. Are we descending into a new macro
2716 // or are part of an old one?
2717 std::shared_ptr<PathDiagnosticMacroPiece> MacroGroup;
2718
2719 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
2720 SM.getExpansionLoc(Loc) :
2722
2723 // Walk the entire macro stack.
2724 while (!MacroStack.empty()) {
2725 if (InstantiationLoc == MacroStack.back().second) {
2726 MacroGroup = MacroStack.back().first;
2727 break;
2728 }
2729
2730 if (ParentInstantiationLoc == MacroStack.back().second) {
2731 MacroGroup = MacroStack.back().first;
2732 break;
2733 }
2734
2735 MacroStack.pop_back();
2736 }
2737
2738 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
2739 // Create a new macro group and add it to the stack.
2740 auto NewGroup = std::make_shared<PathDiagnosticMacroPiece>(
2741 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
2742
2743 if (MacroGroup)
2744 MacroGroup->subPieces.push_back(NewGroup);
2745 else {
2746 assert(InstantiationLoc.isFileID());
2747 Pieces.push_back(NewGroup);
2748 }
2749
2750 MacroGroup = NewGroup;
2751 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
2752 }
2753
2754 // Finally, add the PathDiagnosticPiece to the group.
2755 MacroGroup->subPieces.push_back(piece);
2756 }
2757
2758 // Now take the pieces and construct a new PathDiagnostic.
2759 path.clear();
2760
2761 path.insert(path.end(), Pieces.begin(), Pieces.end());
2762}
2763
2764/// Generate notes from all visitors.
2765/// Notes associated with @c ErrorNode are generated using
2766/// @c getEndPath, and the rest are generated with @c VisitNode.
2767static std::unique_ptr<VisitorsDiagnosticsTy>
2769 const ExplodedNode *ErrorNode,
2770 BugReporterContext &BRC) {
2771 std::unique_ptr<VisitorsDiagnosticsTy> Notes =
2772 std::make_unique<VisitorsDiagnosticsTy>();
2774
2775 // Run visitors on all nodes starting from the node *before* the last one.
2776 // The last node is reserved for notes generated with @c getEndPath.
2777 const ExplodedNode *NextNode = ErrorNode->getFirstPred();
2778 while (NextNode) {
2779
2780 // At each iteration, move all visitors from report to visitor list. This is
2781 // important, because the Profile() functions of the visitors make sure that
2782 // a visitor isn't added multiple times for the same node, but it's fine
2783 // to add the a visitor with Profile() for different nodes (e.g. tracking
2784 // a region at different points of the symbolic execution).
2785 for (std::unique_ptr<BugReporterVisitor> &Visitor : R->visitors())
2786 visitors.push_back(std::move(Visitor));
2787
2788 R->clearVisitors();
2789
2790 const ExplodedNode *Pred = NextNode->getFirstPred();
2791 if (!Pred) {
2792 PathDiagnosticPieceRef LastPiece;
2793 for (auto &V : visitors) {
2794 V->finalizeVisitor(BRC, ErrorNode, *R);
2795
2796 if (auto Piece = V->getEndPath(BRC, ErrorNode, *R)) {
2797 assert(!LastPiece &&
2798 "There can only be one final piece in a diagnostic.");
2799 assert(Piece->getKind() == PathDiagnosticPiece::Kind::Event &&
2800 "The final piece must contain a message!");
2801 LastPiece = std::move(Piece);
2802 (*Notes)[ErrorNode].push_back(LastPiece);
2803 }
2804 }
2805 break;
2806 }
2807
2808 for (auto &V : visitors) {
2809 auto P = V->VisitNode(NextNode, BRC, *R);
2810 if (P)
2811 (*Notes)[NextNode].push_back(std::move(P));
2812 }
2813
2814 if (!R->isValid())
2815 break;
2816
2817 NextNode = Pred;
2818 }
2819
2820 return Notes;
2821}
2822
2823std::optional<PathDiagnosticBuilder> PathDiagnosticBuilder::findValidReport(
2825 PathSensitiveBugReporter &Reporter) {
2826
2827 BugPathGetter BugGraph(&Reporter.getGraph(), bugReports);
2828
2829 while (BugPathInfo *BugPath = BugGraph.getNextBugPath()) {
2830 // Find the BugReport with the original location.
2831 PathSensitiveBugReport *R = BugPath->Report;
2832 assert(R && "No original report found for sliced graph.");
2833 assert(R->isValid() && "Report selected by trimmed graph marked invalid.");
2834 const ExplodedNode *ErrorNode = BugPath->ErrorNode;
2835
2836 // Register refutation visitors first, if they mark the bug invalid no
2837 // further analysis is required
2839
2840 // Register additional node visitors.
2843 R->addVisitor<TagVisitor>();
2844
2845 BugReporterContext BRC(Reporter);
2846
2847 // Run all visitors on a given graph, once.
2848 std::unique_ptr<VisitorsDiagnosticsTy> visitorNotes =
2849 generateVisitorsDiagnostics(R, ErrorNode, BRC);
2850
2851 if (R->isValid()) {
2852 if (Reporter.getAnalyzerOptions().ShouldCrosscheckWithZ3) {
2853 // If crosscheck is enabled, remove all visitors, add the refutation
2854 // visitor and check again
2855 R->clearVisitors();
2857
2858 // We don't overwrite the notes inserted by other visitors because the
2859 // refutation manager does not add any new note to the path
2860 generateVisitorsDiagnostics(R, BugPath->ErrorNode, BRC);
2861 }
2862
2863 // Check if the bug is still valid
2864 if (R->isValid())
2865 return PathDiagnosticBuilder(
2866 std::move(BRC), std::move(BugPath->BugPath), BugPath->Report,
2867 BugPath->ErrorNode, std::move(visitorNotes));
2868 }
2869 }
2870
2871 return {};
2872}
2873
2874std::unique_ptr<DiagnosticForConsumerMapTy>
2878 assert(!bugReports.empty());
2879
2880 auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
2881
2882 std::optional<PathDiagnosticBuilder> PDB =
2883 PathDiagnosticBuilder::findValidReport(bugReports, *this);
2884
2885 if (PDB) {
2886 for (PathDiagnosticConsumer *PC : consumers) {
2887 if (std::unique_ptr<PathDiagnostic> PD = PDB->generate(PC)) {
2888 (*Out)[PC] = std::move(PD);
2889 }
2890 }
2891 }
2892
2893 return Out;
2894}
2895
2896void BugReporter::emitReport(std::unique_ptr<BugReport> R) {
2897 bool ValidSourceLoc = R->getLocation().isValid();
2898 assert(ValidSourceLoc);
2899 // If we mess up in a release build, we'd still prefer to just drop the bug
2900 // instead of trying to go on.
2901 if (!ValidSourceLoc)
2902 return;
2903
2904 // Compute the bug report's hash to determine its equivalence class.
2905 llvm::FoldingSetNodeID ID;
2906 R->Profile(ID);
2907
2908 // Lookup the equivance class. If there isn't one, create it.
2909 void *InsertPos;
2910 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
2911
2912 if (!EQ) {
2913 EQ = new BugReportEquivClass(std::move(R));
2914 EQClasses.InsertNode(EQ, InsertPos);
2915 EQClassesVector.push_back(EQ);
2916 } else
2917 EQ->AddReport(std::move(R));
2918}
2919
2920void PathSensitiveBugReporter::emitReport(std::unique_ptr<BugReport> R) {
2921 if (auto PR = dyn_cast<PathSensitiveBugReport>(R.get()))
2922 if (const ExplodedNode *E = PR->getErrorNode()) {
2923 // An error node must either be a sink or have a tag, otherwise
2924 // it could get reclaimed before the path diagnostic is created.
2925 assert((E->isSink() || E->getLocation().getTag()) &&
2926 "Error node must either be a sink or have a tag");
2927
2928 const AnalysisDeclContext *DeclCtx =
2929 E->getLocationContext()->getAnalysisDeclContext();
2930 // The source of autosynthesized body can be handcrafted AST or a model
2931 // file. The locations from handcrafted ASTs have no valid source
2932 // locations and have to be discarded. Locations from model files should
2933 // be preserved for processing and reporting.
2934 if (DeclCtx->isBodyAutosynthesized() &&
2936 return;
2937 }
2938
2939 BugReporter::emitReport(std::move(R));
2940}
2941
2942//===----------------------------------------------------------------------===//
2943// Emitting reports in equivalence classes.
2944//===----------------------------------------------------------------------===//
2945
2946namespace {
2947
2948struct FRIEC_WLItem {
2949 const ExplodedNode *N;
2951
2952 FRIEC_WLItem(const ExplodedNode *n)
2953 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
2954};
2955
2956} // namespace
2957
2958BugReport *PathSensitiveBugReporter::findReportInEquivalenceClass(
2960 // If we don't need to suppress any of the nodes because they are
2961 // post-dominated by a sink, simply add all the nodes in the equivalence class
2962 // to 'Nodes'. Any of the reports will serve as a "representative" report.
2963 assert(EQ.getReports().size() > 0);
2964 const BugType& BT = EQ.getReports()[0]->getBugType();
2965 if (!BT.isSuppressOnSink()) {
2966 BugReport *R = EQ.getReports()[0].get();
2967 for (auto &J : EQ.getReports()) {
2968 if (auto *PR = dyn_cast<PathSensitiveBugReport>(J.get())) {
2969 R = PR;
2970 bugReports.push_back(PR);
2971 }
2972 }
2973 return R;
2974 }
2975
2976 // For bug reports that should be suppressed when all paths are post-dominated
2977 // by a sink node, iterate through the reports in the equivalence class
2978 // until we find one that isn't post-dominated (if one exists). We use a
2979 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
2980 // this as a recursive function, but we don't want to risk blowing out the
2981 // stack for very long paths.
2982 BugReport *exampleReport = nullptr;
2983
2984 for (const auto &I: EQ.getReports()) {
2985 auto *R = dyn_cast<PathSensitiveBugReport>(I.get());
2986 if (!R)
2987 continue;
2988
2989 const ExplodedNode *errorNode = R->getErrorNode();
2990 if (errorNode->isSink()) {
2991 llvm_unreachable(
2992 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
2993 }
2994 // No successors? By definition this nodes isn't post-dominated by a sink.
2995 if (errorNode->succ_empty()) {
2996 bugReports.push_back(R);
2997 if (!exampleReport)
2998 exampleReport = R;
2999 continue;
3000 }
3001
3002 // See if we are in a no-return CFG block. If so, treat this similarly
3003 // to being post-dominated by a sink. This works better when the analysis
3004 // is incomplete and we have never reached the no-return function call(s)
3005 // that we'd inevitably bump into on this path.
3006 if (const CFGBlock *ErrorB = errorNode->getCFGBlock())
3007 if (ErrorB->isInevitablySinking())
3008 continue;
3009
3010 // At this point we know that 'N' is not a sink and it has at least one
3011 // successor. Use a DFS worklist to find a non-sink end-of-path node.
3012 using WLItem = FRIEC_WLItem;
3013 using DFSWorkList = SmallVector<WLItem, 10>;
3014
3015 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
3016
3017 DFSWorkList WL;
3018 WL.push_back(errorNode);
3019 Visited[errorNode] = 1;
3020
3021 while (!WL.empty()) {
3022 WLItem &WI = WL.back();
3023 assert(!WI.N->succ_empty());
3024
3025 for (; WI.I != WI.E; ++WI.I) {
3026 const ExplodedNode *Succ = *WI.I;
3027 // End-of-path node?
3028 if (Succ->succ_empty()) {
3029 // If we found an end-of-path node that is not a sink.
3030 if (!Succ->isSink()) {
3031 bugReports.push_back(R);
3032 if (!exampleReport)
3033 exampleReport = R;
3034 WL.clear();
3035 break;
3036 }
3037 // Found a sink? Continue on to the next successor.
3038 continue;
3039 }
3040 // Mark the successor as visited. If it hasn't been explored,
3041 // enqueue it to the DFS worklist.
3042 unsigned &mark = Visited[Succ];
3043 if (!mark) {
3044 mark = 1;
3045 WL.push_back(Succ);
3046 break;
3047 }
3048 }
3049
3050 // The worklist may have been cleared at this point. First
3051 // check if it is empty before checking the last item.
3052 if (!WL.empty() && &WL.back() == &WI)
3053 WL.pop_back();
3054 }
3055 }
3056
3057 // ExampleReport will be NULL if all the nodes in the equivalence class
3058 // were post-dominated by sinks.
3059 return exampleReport;
3060}
3061
3062void BugReporter::FlushReport(BugReportEquivClass& EQ) {
3063 SmallVector<BugReport*, 10> bugReports;
3064 BugReport *report = findReportInEquivalenceClass(EQ, bugReports);
3065 if (!report)
3066 return;
3067
3068 // See whether we need to silence the checker/package.
3069 for (const std::string &CheckerOrPackage :
3070 getAnalyzerOptions().SilencedCheckersAndPackages) {
3071 if (report->getBugType().getCheckerName().startswith(
3072 CheckerOrPackage))
3073 return;
3074 }
3075
3077 std::unique_ptr<DiagnosticForConsumerMapTy> Diagnostics =
3078 generateDiagnosticForConsumerMap(report, Consumers, bugReports);
3079
3080 for (auto &P : *Diagnostics) {
3081 PathDiagnosticConsumer *Consumer = P.first;
3082 std::unique_ptr<PathDiagnostic> &PD = P.second;
3083
3084 // If the path is empty, generate a single step path with the location
3085 // of the issue.
3086 if (PD->path.empty()) {
3087 PathDiagnosticLocation L = report->getLocation();
3088 auto piece = std::make_unique<PathDiagnosticEventPiece>(
3089 L, report->getDescription());
3090 for (SourceRange Range : report->getRanges())
3091 piece->addRange(Range);
3092 PD->setEndOfPath(std::move(piece));
3093 }
3094
3095 PathPieces &Pieces = PD->getMutablePieces();
3096 if (getAnalyzerOptions().ShouldDisplayNotesAsEvents) {
3097 // For path diagnostic consumers that don't support extra notes,
3098 // we may optionally convert those to path notes.
3099 for (const auto &I : llvm::reverse(report->getNotes())) {
3100 PathDiagnosticNotePiece *Piece = I.get();
3101 auto ConvertedPiece = std::make_shared<PathDiagnosticEventPiece>(
3102 Piece->getLocation(), Piece->getString());
3103 for (const auto &R: Piece->getRanges())
3104 ConvertedPiece->addRange(R);
3105
3106 Pieces.push_front(std::move(ConvertedPiece));
3107 }
3108 } else {
3109 for (const auto &I : llvm::reverse(report->getNotes()))
3110 Pieces.push_front(I);
3111 }
3112
3113 for (const auto &I : report->getFixits())
3114 Pieces.back()->addFixit(I);
3115
3117 Consumer->HandlePathDiagnostic(std::move(PD));
3118 }
3119}
3120
3121/// Insert all lines participating in the function signature \p Signature
3122/// into \p ExecutedLines.
3124 const Decl *Signature, const SourceManager &SM,
3125 FilesToLineNumsMap &ExecutedLines) {
3126 SourceRange SignatureSourceRange;
3127 const Stmt* Body = Signature->getBody();
3128 if (const auto FD = dyn_cast<FunctionDecl>(Signature)) {
3129 SignatureSourceRange = FD->getSourceRange();
3130 } else if (const auto OD = dyn_cast<ObjCMethodDecl>(Signature)) {
3131 SignatureSourceRange = OD->getSourceRange();
3132 } else {
3133 return;
3134 }
3135 SourceLocation Start = SignatureSourceRange.getBegin();
3136 SourceLocation End = Body ? Body->getSourceRange().getBegin()
3137 : SignatureSourceRange.getEnd();
3138 if (!Start.isValid() || !End.isValid())
3139 return;
3140 unsigned StartLine = SM.getExpansionLineNumber(Start);
3141 unsigned EndLine = SM.getExpansionLineNumber(End);
3142
3143 FileID FID = SM.getFileID(SM.getExpansionLoc(Start));
3144 for (unsigned Line = StartLine; Line <= EndLine; Line++)
3145 ExecutedLines[FID].insert(Line);
3146}
3147
3149 const Stmt *S, const SourceManager &SM,
3150 FilesToLineNumsMap &ExecutedLines) {
3151 SourceLocation Loc = S->getSourceRange().getBegin();
3152 if (!Loc.isValid())
3153 return;
3154 SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);
3155 FileID FID = SM.getFileID(ExpansionLoc);
3156 unsigned LineNo = SM.getExpansionLineNumber(ExpansionLoc);
3157 ExecutedLines[FID].insert(LineNo);
3158}
3159
3160/// \return all executed lines including function signatures on the path
3161/// starting from \p N.
3162static std::unique_ptr<FilesToLineNumsMap>
3164 auto ExecutedLines = std::make_unique<FilesToLineNumsMap>();
3165
3166 while (N) {
3167 if (N->getFirstPred() == nullptr) {
3168 // First node: show signature of the entrance point.
3169 const Decl *D = N->getLocationContext()->getDecl();
3171 } else if (auto CE = N->getLocationAs<CallEnter>()) {
3172 // Inlined function: show signature.
3173 const Decl* D = CE->getCalleeContext()->getDecl();
3175 } else if (const Stmt *S = N->getStmtForDiagnostics()) {
3176 populateExecutedLinesWithStmt(S, SM, *ExecutedLines);
3177
3178 // Show extra context for some parent kinds.
3179 const Stmt *P = N->getParentMap().getParent(S);
3180
3181 // The path exploration can die before the node with the associated
3182 // return statement is generated, but we do want to show the whole
3183 // return.
3184 if (const auto *RS = dyn_cast_or_null<ReturnStmt>(P)) {
3185 populateExecutedLinesWithStmt(RS, SM, *ExecutedLines);
3186 P = N->getParentMap().getParent(RS);
3187 }
3188
3189 if (isa_and_nonnull<SwitchCase, LabelStmt>(P))
3190 populateExecutedLinesWithStmt(P, SM, *ExecutedLines);
3191 }
3192
3193 N = N->getFirstPred();
3194 }
3195 return ExecutedLines;
3196}
3197
3198std::unique_ptr<DiagnosticForConsumerMapTy>
3200 BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3201 ArrayRef<BugReport *> bugReports) {
3202 auto *basicReport = cast<BasicBugReport>(exampleReport);
3203 auto Out = std::make_unique<DiagnosticForConsumerMapTy>();
3204 for (auto *Consumer : consumers)
3205 (*Out)[Consumer] = generateDiagnosticForBasicReport(basicReport);
3206 return Out;
3207}
3208
3211 const SourceManager &SMgr) {
3212 SourceLocation CallLoc = CP->callEnter.asLocation();
3213
3214 // If the call is within a macro, don't do anything (for now).
3215 if (CallLoc.isMacroID())
3216 return nullptr;
3217
3218 assert(AnalysisManager::isInCodeFile(CallLoc, SMgr) &&
3219 "The call piece should not be in a header file.");
3220
3221 // Check if CP represents a path through a function outside of the main file.
3223 return CP;
3224
3225 const PathPieces &Path = CP->path;
3226 if (Path.empty())
3227 return nullptr;
3228
3229 // Check if the last piece in the callee path is a call to a function outside
3230 // of the main file.
3231 if (auto *CPInner = dyn_cast<PathDiagnosticCallPiece>(Path.back().get()))
3232 return getFirstStackedCallToHeaderFile(CPInner, SMgr);
3233
3234 // Otherwise, the last piece is in the main file.
3235 return nullptr;
3236}
3237
3239 if (PD.path.empty())
3240 return;
3241
3242 PathDiagnosticPiece *LastP = PD.path.back().get();
3243 assert(LastP);
3244 const SourceManager &SMgr = LastP->getLocation().getManager();
3245
3246 // We only need to check if the report ends inside headers, if the last piece
3247 // is a call piece.
3248 if (auto *CP = dyn_cast<PathDiagnosticCallPiece>(LastP)) {
3249 CP = getFirstStackedCallToHeaderFile(CP, SMgr);
3250 if (CP) {
3251 // Mark the piece.
3253
3254 // Update the path diagnostic message.
3255 const auto *ND = dyn_cast<NamedDecl>(CP->getCallee());
3256 if (ND) {
3257 SmallString<200> buf;
3258 llvm::raw_svector_ostream os(buf);
3259 os << " (within a call to '" << ND->getDeclName() << "')";
3260 PD.appendToDesc(os.str());
3261 }
3262
3263 // Reset the report containing declaration and location.
3264 PD.setDeclWithIssue(CP->getCaller());
3265 PD.setLocation(CP->getLocation());
3266
3267 return;
3268 }
3269 }
3270}
3271
3272
3273
3274std::unique_ptr<DiagnosticForConsumerMapTy>
3275PathSensitiveBugReporter::generateDiagnosticForConsumerMap(
3276 BugReport *exampleReport, ArrayRef<PathDiagnosticConsumer *> consumers,
3277 ArrayRef<BugReport *> bugReports) {
3278 std::vector<BasicBugReport *> BasicBugReports;
3279 std::vector<PathSensitiveBugReport *> PathSensitiveBugReports;
3280 if (isa<BasicBugReport>(exampleReport))
3282 consumers, bugReports);
3283
3284 // Generate the full path sensitive diagnostic, using the generation scheme
3285 // specified by the PathDiagnosticConsumer. Note that we have to generate
3286 // path diagnostics even for consumers which do not support paths, because
3287 // the BugReporterVisitors may mark this bug as a false positive.
3288 assert(!bugReports.empty());
3289 MaxBugClassSize.updateMax(bugReports.size());
3290
3291 // Avoid copying the whole array because there may be a lot of reports.
3292 ArrayRef<PathSensitiveBugReport *> convertedArrayOfReports(
3293 reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.begin()),
3294 reinterpret_cast<PathSensitiveBugReport *const *>(&*bugReports.end()));
3295 std::unique_ptr<DiagnosticForConsumerMapTy> Out = generatePathDiagnostics(
3296 consumers, convertedArrayOfReports);
3297
3298 if (Out->empty())
3299 return Out;
3300
3301 MaxValidBugClassSize.updateMax(bugReports.size());
3302
3303 // Examine the report and see if the last piece is in a header. Reset the
3304 // report location to the last piece in the main source file.
3305 const AnalyzerOptions &Opts = getAnalyzerOptions();
3306 for (auto const &P : *Out)
3307 if (Opts.ShouldReportIssuesInMainSourceFile && !Opts.AnalyzeAll)
3309
3310 return Out;
3311}
3312
3313void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3314 const CheckerBase *Checker, StringRef Name,
3315 StringRef Category, StringRef Str,
3317 ArrayRef<SourceRange> Ranges,
3318 ArrayRef<FixItHint> Fixits) {
3319 EmitBasicReport(DeclWithIssue, Checker->getCheckerName(), Name, Category, Str,
3320 Loc, Ranges, Fixits);
3321}
3322
3323void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3324 CheckerNameRef CheckName,
3325 StringRef name, StringRef category,
3326 StringRef str, PathDiagnosticLocation Loc,
3327 ArrayRef<SourceRange> Ranges,
3328 ArrayRef<FixItHint> Fixits) {
3329 // 'BT' is owned by BugReporter.
3330 BugType *BT = getBugTypeForName(CheckName, name, category);
3331 auto R = std::make_unique<BasicBugReport>(*BT, str, Loc);
3332 R->setDeclWithIssue(DeclWithIssue);
3333 for (const auto &SR : Ranges)
3334 R->addRange(SR);
3335 for (const auto &FH : Fixits)
3336 R->addFixItHint(FH);
3337 emitReport(std::move(R));
3338}
3339
3340BugType *BugReporter::getBugTypeForName(CheckerNameRef CheckName,
3341 StringRef name, StringRef category) {
3342 SmallString<136> fullDesc;
3343 llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name
3344 << ":" << category;
3345 std::unique_ptr<BugType> &BT = StrBugTypes[fullDesc];
3346 if (!BT)
3347 BT = std::make_unique<BugType>(CheckName, name, category);
3348 return BT.get();
3349}
#define V(N, I)
Definition: ASTContext.h:3217
NodeId Parent
Definition: ASTDiff.cpp:191
BoundNodesTreeBuilder Nodes
DynTypedNode Node
StringRef P
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
#define SM(sm)
Definition: Cuda.cpp:78
static void dropFunctionEntryEdge(const PathDiagnosticConstruct &C, PathPieces &Path)
Drop the very first edge in a path, which should be a function entry edge.
constexpr llvm::StringLiteral StrLoopRangeEmpty
static PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S, const LocationContext *LC, bool allowNestedContexts=false)
static std::unique_ptr< FilesToLineNumsMap > findExecutedLines(const SourceManager &SM, const ExplodedNode *N)
static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond)
static void updateExecutedLinesWithDiagnosticPieces(PathDiagnostic &PD)
Populate executes lines with lines containing at least one diagnostics.
static void removeRedundantMsgs(PathPieces &path)
An optimization pass over PathPieces that removes redundant diagnostics generated by both ConditionBR...
constexpr llvm::StringLiteral StrLoopCollectionEmpty
static void adjustCallLocations(PathPieces &Pieces, PathDiagnosticLocation *LastCallLocation=nullptr)
Recursively scan through a path and make sure that all call pieces have valid locations.
static void removeIdenticalEvents(PathPieces &path)
static const Stmt * getTerminatorCondition(const CFGBlock *B)
A customized wrapper for CFGBlock::getTerminatorCondition() which returns the element for ObjCForColl...
static std::unique_ptr< VisitorsDiagnosticsTy > generateVisitorsDiagnostics(PathSensitiveBugReport *R, const ExplodedNode *ErrorNode, BugReporterContext &BRC)
Generate notes from all visitors.
static bool removeUnneededCalls(const PathDiagnosticConstruct &C, PathPieces &pieces, const PathSensitiveBugReport *R, bool IsInteresting=false)
Recursively scan through a path and prune out calls and macros pieces that aren't needed.
static void populateExecutedLinesWithStmt(const Stmt *S, const SourceManager &SM, FilesToLineNumsMap &ExecutedLines)
static bool isJumpToFalseBranch(const BlockEdge *BE)
static std::optional< size_t > getLengthOnSingleLine(const SourceManager &SM, SourceRange Range)
Returns the number of bytes in the given (character-based) SourceRange.
static bool isLoop(const Stmt *Term)
static bool isContainedByStmt(const ParentMap &PM, const Stmt *S, const Stmt *SubS)
constexpr llvm::StringLiteral StrEnteringLoop
static std::unique_ptr< PathDiagnostic > generateEmptyDiagnosticForReport(const PathSensitiveBugReport *R, const SourceManager &SM)
static void addEdgeToPath(PathPieces &path, PathDiagnosticLocation &PrevLoc, PathDiagnosticLocation NewLoc)
Adds a sanitized control-flow diagnostic edge to a path.
static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL)
static std::unique_ptr< PathDiagnostic > generateDiagnosticForBasicReport(const BasicBugReport *R)
static void removeContextCycles(PathPieces &Path, const SourceManager &SM)
Eliminate two-edge cycles created by addContextEdges().
static bool lexicalContains(const ParentMap &PM, const Stmt *X, const Stmt *Y)
Return true if X is contained by Y.
static void removePopUpNotes(PathPieces &Path)
Same logic as above to remove extra pieces.
STATISTIC(MaxBugClassSize, "The maximum number of bug reports in the same equivalence class")
static void insertToInterestingnessMap(llvm::DenseMap< T, bugreporter::TrackingKind > &InterestingnessMap, T Val, bugreporter::TrackingKind TKind)
constexpr llvm::StringLiteral StrLoopBodyZero
static const Stmt * getEnclosingParent(const Stmt *S, const ParentMap &PM)
static void removePunyEdges(PathPieces &path, const SourceManager &SM, const ParentMap &PM)
static const Stmt * getStmtParent(const Stmt *S, const ParentMap &PM)
static void CompactMacroExpandedPieces(PathPieces &path, const SourceManager &SM)
CompactMacroExpandedPieces - This function postprocesses a PathDiagnostic object and collapses PathDi...
static void simplifySimpleBranches(PathPieces &pieces)
Move edges from a branch condition to a branch target when the condition is simple.
static void populateExecutedLinesWithFunctionSignature(const Decl *Signature, const SourceManager &SM, FilesToLineNumsMap &ExecutedLines)
Insert all lines participating in the function signature Signature into ExecutedLines.
static void resetDiagnosticLocationToMainFile(PathDiagnostic &PD)
static bool optimizeEdges(const PathDiagnosticConstruct &C, PathPieces &path, OptimizedCallsSet &OCS)
static bool hasImplicitBody(const Decl *D)
Returns true if the given decl has been implicitly given a body, either by the analyzer or by the com...
static PathDiagnosticCallPiece * getFirstStackedCallToHeaderFile(PathDiagnosticCallPiece *CP, const SourceManager &SMgr)
static void addContextEdges(PathPieces &pieces, const LocationContext *LC)
Adds synthetic edges from top-level statements to their subexpressions.
static LLVM_ATTRIBUTE_USED bool isDependency(const CheckerRegistryData &Registry, StringRef CheckerName)
static PathDiagnosticEventPiece * eventsDescribeSameCondition(PathDiagnosticEventPiece *X, PathDiagnosticEventPiece *Y)
static bool isInLoopBody(const ParentMap &PM, const Stmt *S, const Stmt *Term)
static void removeEdgesToDefaultInitializers(PathPieces &Pieces)
Remove edges in and out of C++ default initializer expressions.
static const Stmt * getStmtBeforeCond(const ParentMap &PM, const Stmt *Term, const ExplodedNode *N)
static void removePiecesWithInvalidLocations(PathPieces &Pieces)
Remove all pieces with invalid locations as these cannot be serialized.
static LLVM_ATTRIBUTE_USED bool isHidden(const CheckerRegistryData &Registry, StringRef CheckerName)
static CompilationDatabasePluginRegistry::Add< FixedCompilationDatabasePlugin > X("fixed-compilation-database", "Reads plain-text flags file")
Defines the clang::Expr interface and subclasses for C++ expressions.
int Priority
Definition: Format.cpp:2780
int Category
Definition: Format.cpp:2779
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
Defines the Objective-C statement AST node classes.
SourceManager & getSourceManager()
Definition: ASTContext.h:692
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Stores options for the analyzer from the command line.
const CFGBlock * getSrc() const
Definition: ProgramPoint.h:506
const CFGBlock * getDst() const
Definition: ProgramPoint.h:510
Represents a single basic block in a source-level CFG.
Definition: CFG.h:576
Stmt * getLabel()
Definition: CFG.h:1069
succ_iterator succ_begin()
Definition: CFG.h:955
Stmt * getTerminatorStmt()
Definition: CFG.h:1050
const Stmt * getLoopTarget() const
Definition: CFG.h:1067
Stmt * getTerminatorCondition(bool StripParens=true)
Definition: CFG.cpp:6251
unsigned succ_size() const
Definition: CFG.h:973
CFGBlock & getExit()
Definition: CFG.h:1333
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
Definition: StmtCXX.h:134
Represents a point when we begin processing an inlined call.
Definition: ProgramPoint.h:628
Represents a point when we finish the call exit sequence (for inlined call).
Definition: ProgramPoint.h:686
const StackFrameContext * getCalleeContext() const
Definition: ProgramPoint.h:693
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
ASTContext & getASTContext() const LLVM_READONLY
Definition: DeclBase.cpp:428
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:576
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: DeclBase.h:1052
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
Definition: DeclBase.h:1058
This represents one expression.
Definition: Expr.h:110
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx, SmallVectorImpl< PartialDiagnosticAt > *Diag=nullptr) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition: Expr.cpp:3046
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isValid() const
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition: Stmt.h:2578
A SourceLocation and its associated SourceManager.
IfStmt - This represents an if/then/else.
Definition: Stmt.h:1950
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
const Decl * getDecl() const
const ParentMap & getParentMap() const
const StackFrameContext * getStackFrame() const
Represents Objective-C's collection statement.
Definition: StmtObjC.h:23
bool isConsumedExpr(Expr *E) const
Definition: ParentMap.cpp:172
Stmt * getParent(Stmt *) const
Definition: ParentMap.cpp:136
Stmt * getParentIgnoreParens(Stmt *) const
Definition: ParentMap.cpp:141
Represents a point after we ran remove dead bindings AFTER processing the given statement.
Definition: ProgramPoint.h:484
Represents a program point just before an implicit call event.
Definition: ProgramPoint.h:579
std::optional< T > getAs() const
Convert to the specified ProgramPoint type, returning std::nullopt if this ProgramPoint is not of the...
Definition: ProgramPoint.h:147
const LocationContext * getLocationContext() const
Definition: ProgramPoint.h:175
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition: Stmt.h:2806
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
It represents a stack frame of the call stack (based on CallEvent).
const Stmt * getCallSite() const
Stmt - This represents one statement.
Definition: Stmt.h:72
StmtClass getStmtClass() const
Definition: Stmt.h:1177
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:325
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:337
WhileStmt - This represents a 'while' stmt.
Definition: Stmt.h:2386
static bool isInCodeFile(SourceLocation SL, const SourceManager &SM)
const Decl * getDeclWithIssue() const override
The smallest declaration that contains the bug location.
Definition: BugReporter.h:268
PathDiagnosticLocation getUniqueingLocation() const override
Get the location on which the report should be uniqued.
Definition: BugReporter.h:272
void Profile(llvm::FoldingSetNodeID &hash) const override
Reports are uniqued to ensure that we do not emit multiple diagnostics for each bug.
const Decl * getUniqueingDecl() const override
Get the declaration that corresponds to (usually contains) the uniqueing location.
Definition: BugReporter.h:276
This class provides an interface through which checkers can create individual bug reports.
Definition: BugReporter.h:119
llvm::ArrayRef< FixItHint > getFixits() const
Definition: BugReporter.h:244
void addRange(SourceRange R)
Add a range to a bug report.
Definition: BugReporter.h:222
SmallVector< SourceRange, 4 > Ranges
Definition: BugReporter.h:132
std::string Description
Definition: BugReporter.h:130
virtual PathDiagnosticLocation getLocation() const =0
The primary location of the bug report that points at the undesirable behavior in the code.
ArrayRef< std::shared_ptr< PathDiagnosticNotePiece > > getNotes()
Definition: BugReporter.h:211
void addFixItHint(const FixItHint &F)
Add a fix-it hint to the bug report.
Definition: BugReporter.h:240
StringRef getDescription() const
A verbose warning message that is appropriate for displaying next to the source code that introduces ...
Definition: BugReporter.h:157
const BugType & BT
Definition: BugReporter.h:128
const BugType & getBugType() const
Definition: BugReporter.h:149
StringRef getShortDescription(bool UseFallback=true) const
A short general warning message that is appropriate for displaying in the list of all reported bugs.
Definition: BugReporter.h:163
Kind getKind() const
Definition: BugReporter.h:147
virtual ArrayRef< SourceRange > getRanges() const
Get the SourceRanges associated with the report.
Definition: BugReporter.h:229
static PathDiagnosticPieceRef getDefaultEndPath(const BugReporterContext &BRC, const ExplodedNode *N, const PathSensitiveBugReport &BR)
Generates the default final diagnostic piece.
virtual std::unique_ptr< DiagnosticForConsumerMapTy > generateDiagnosticForConsumerMap(BugReport *exampleReport, ArrayRef< PathDiagnosticConsumer * > consumers, ArrayRef< BugReport * > bugReports)
Generate the diagnostics for the given bug report.
void FlushReports()
Generate and flush diagnostics for all bug reports.
BugReporter(BugReporterData &d)
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges=std::nullopt, ArrayRef< FixItHint > Fixits=std::nullopt)
const AnalyzerOptions & getAnalyzerOptions()
Definition: BugReporter.h:618
virtual void emitReport(std::unique_ptr< BugReport > R)
Add the given report to the set of reports tracked by BugReporter.
ArrayRef< PathDiagnosticConsumer * > getPathDiagnosticConsumers()
Definition: BugReporter.h:605
bool isSuppressOnSink() const
isSuppressOnSink - Returns true if bug reports associated with this bug type should be suppressed if ...
Definition: BugType.h:64
StringRef getCategory() const
Definition: BugType.h:49
StringRef getDescription() const
Definition: BugType.h:48
StringRef getCheckerName() const
Definition: BugType.h:50
CheckerNameRef getCheckerName() const
Definition: Checker.cpp:25
This wrapper is used to ensure that only StringRefs originating from the CheckerRegistry are used as ...
Visitor that tries to report interesting diagnostics from conditions.
static bool isPieceMessageGeneric(const PathDiagnosticPiece *Piece)
static const char * getTag()
Return the tag associated with this visitor.
bool isValid() const =delete
std::unique_ptr< ExplodedGraph > trim(ArrayRef< const NodeTy * > Nodes, InterExplodedGraphMap *ForwardMap=nullptr, InterExplodedGraphMap *InverseMap=nullptr) const
Creates a trimmed version of the graph that only contains paths leading to the given nodes.
const CFGBlock * getCFGBlock() const
const ProgramStateRef & getState() const
pred_iterator pred_end()
pred_iterator pred_begin()
const Stmt * getStmtForDiagnostics() const
If the node's program point corresponds to a statement, retrieve that statement.
const Stmt * getPreviousStmtForDiagnostics() const
Find the statement that was executed immediately before this node.
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
void addPredecessor(ExplodedNode *V, ExplodedGraph &G)
addPredeccessor - Adds a predecessor to the current node, and in tandem add this node as a successor ...
const Stmt * getNextStmtForDiagnostics() const
Find the next statement that was executed on this node's execution path.
const ParentMap & getParentMap() const
SVal getSVal(const Stmt *S) const
Get the value of an arbitrary expression at this node.
const LocationContext * getLocationContext() const
std::optional< T > getLocationAs() const &
const Stmt * getCurrentOrPreviousStmtForDiagnostics() const
Find the statement that was executed at or immediately before this node.
ExplodedNode * getFirstPred()
const ExplodedNode *const * const_succ_iterator
ProgramStateManager & getStateManager()
Definition: ExprEngine.h:423
ExplodedGraph & getGraph()
Definition: ExprEngine.h:269
The bug visitor will walk all the nodes in a path and collect all the constraints.
Suppress reports that might lead to known false positives.
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:95
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
Definition: MemRegion.cpp:1307
Prints path notes when a message is sent to a nil receiver.
PathDiagnosticLocation getLocation() const override
static std::shared_ptr< PathDiagnosticCallPiece > construct(const CallExitEnd &CE, const SourceManager &SM)
PathDiagnosticLocation callEnterWithin
virtual bool supportsLogicalOpControlFlow() const
void HandlePathDiagnostic(std::unique_ptr< PathDiagnostic > D)
PathDiagnosticLocation getStartLocation() const
void setStartLocation(const PathDiagnosticLocation &L)
PathDiagnosticLocation getEndLocation() const
static PathDiagnosticLocation createMemberLoc(const MemberExpr *ME, const SourceManager &SM)
For member expressions, return the location of the '.
void Profile(llvm::FoldingSetNodeID &ID) const
const SourceManager & getManager() const
static PathDiagnosticLocation createOperatorLoc(const BinaryOperator *BO, const SourceManager &SM)
Create the location for the operator of the binary expression.
static PathDiagnosticLocation createEndBrace(const CompoundStmt *CS, const SourceManager &SM)
Create a location for the end of the compound statement.
static SourceLocation getValidSourceLocation(const Stmt *S, LocationOrAnalysisDeclContext LAC, bool UseEndOfStatement=false)
Construct a source location that corresponds to either the beginning or the end of the given statemen...
static PathDiagnosticLocation createEnd(const Stmt *S, const SourceManager &SM, const LocationOrAnalysisDeclContext LAC)
Create a location for the end of the statement.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
static PathDiagnosticLocation createDeclEnd(const LocationContext *LC, const SourceManager &SM)
Constructs a location for the end of the enclosing declaration body.
static PathDiagnosticLocation createSingleLocation(const PathDiagnosticLocation &PDL)
Convert the given location into a single kind location.
ArrayRef< SourceRange > getRanges() const
Return the SourceRanges associated with this PathDiagnosticPiece.
virtual PathDiagnosticLocation getLocation() const =0
const void * getTag() const
Return the opaque tag (if any) on the PathDiagnosticPiece.
PathDiagnosticLocation getLocation() const override
PathDiagnostic - PathDiagnostic objects represent a single path-sensitive diagnostic.
void setDeclWithIssue(const Decl *D)
void appendToDesc(StringRef S)
void setLocation(PathDiagnosticLocation NewLoc)
const FilesToLineNumsMap & getExecutedLines() const
PathPieces flatten(bool ShouldFlattenMacros) const
llvm::SmallSet< const LocationContext *, 2 > InterestingLocationContexts
A set of location contexts that correspoind to call sites which should be considered "interesting".
Definition: BugReporter.h:323
void markInteresting(SymbolRef sym, bugreporter::TrackingKind TKind=bugreporter::TrackingKind::Thorough)
Marks a symbol as interesting.
PathDiagnosticLocation getUniqueingLocation() const override
Get the location on which the report should be uniqued.
Definition: BugReporter.h:412
VisitorList Callbacks
A set of custom visitors which generate "event" diagnostics at interesting points in the path.
Definition: BugReporter.h:327
PathDiagnosticLocation getLocation() const override
The primary location of the bug report that points at the undesirable behavior in the code.
const Decl * getDeclWithIssue() const override
The smallest declaration that contains the bug location.
bool shouldPrunePath() const
Indicates whether or not any path pruning should take place when generating a PathDiagnostic from thi...
Definition: BugReporter.h:406
ArrayRef< SourceRange > getRanges() const override
Get the SourceRanges associated with the report.
llvm::DenseMap< SymbolRef, bugreporter::TrackingKind > InterestingSymbols
Profile to identify equivalent bug reports for error report coalescing.
Definition: BugReporter.h:311
const Decl * getUniqueingDecl() const override
Get the declaration containing the uniqueing location.
Definition: BugReporter.h:417
const ExplodedNode * getErrorNode() const
Definition: BugReporter.h:402
PathSensitiveBugReport(const BugType &bt, StringRef desc, const ExplodedNode *errorNode)
Definition: BugReporter.h:369
const ExplodedNode * ErrorNode
The ExplodedGraph node against which the report was thrown.
Definition: BugReporter.h:298
void Profile(llvm::FoldingSetNodeID &hash) const override
Profile to identify equivalent bug reports for error report coalescing.
void clearVisitors()
Remove all visitors attached to this bug report.
void addVisitor(std::unique_ptr< BugReporterVisitor > visitor)
Add custom or predefined bug report visitors to this report.
bool isValid() const
Returns whether or not this report should be considered valid.
Definition: BugReporter.h:468
std::optional< bugreporter::TrackingKind > getInterestingnessKind(SymbolRef sym) const
void markNotInteresting(SymbolRef sym)
llvm::DenseMap< const MemRegion *, bugreporter::TrackingKind > InterestingRegions
A (stack of) set of regions that are registered with this report as being "interesting",...
Definition: BugReporter.h:319
bool isInteresting(SymbolRef sym) const
const SourceRange ErrorNodeRange
The range that corresponds to ErrorNode's program point.
Definition: BugReporter.h:302
llvm::FoldingSet< BugReporterVisitor > CallbacksSet
Used for ensuring the visitors are only added once.
Definition: BugReporter.h:330
GRBugReporter is used for generating path-sensitive reports.
Definition: BugReporter.h:664
const ExplodedGraph & getGraph() const
getGraph - Get the exploded graph created by the analysis engine for the analyzed method or function.
std::unique_ptr< DiagnosticForConsumerMapTy > generatePathDiagnostics(ArrayRef< PathDiagnosticConsumer * > consumers, ArrayRef< PathSensitiveBugReport * > &bugReports)
bugReports A set of bug reports within a single equivalence class
void emitReport(std::unique_ptr< BugReport > R) override
Add the given report to the set of reports tracked by BugReporter.
ProgramStateManager & getStateManager() const
getStateManager - Return the state manager used by the analysis engine.
A Range represents the closed range [from, to].
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:72
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition: SVals.h:103
SymbolRef getAsLocSymbol(bool IncludeBaseRegions=false) const
If this SVal is a location and wraps a symbol, return that SymbolRef.
Definition: SVals.cpp:68
std::string getMessage(const ExplodedNode *N) override
Search the call expression for the symbol Sym and dispatch the 'getMessageForX()' methods to construc...
virtual std::string getMessageForSymbolNotFound()
Definition: BugReporter.h:112
virtual std::string getMessageForReturn(const CallExpr *CallExpr)
Definition: BugReporter.h:108
virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex)
Produces the message of the following form: 'Msg via Nth parameter'.
Symbolic value.
Definition: SymExpr.h:29
The visitor detects NoteTags and displays the event notes they contain.
static const char * getTag()
Return the tag associated with this visitor.
TrackingKind
Specifies the type of tracking for an expression.
@ Thorough
Default tracking kind – specifies that as much information should be gathered about the tracked expre...
@ Condition
Specifies that a more moderate tracking should be used for the expression value.
llvm::DenseMap< const ExplodedNode *, const ExplodedNode * > InterExplodedGraphMap
std::map< FileID, std::set< unsigned > > FilesToLineNumsMap
File IDs mapped to sets of line numbers.
@ CF
Indicates that the tracked object is a CF object.
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
bool Call(InterpState &S, CodePtr &PC, const Function *Func)
Definition: Interp.h:1494
bool EQ(InterpState &S, CodePtr OpPC)
Definition: Interp.h:608
bool isa(CodeGen::Address addr)
Definition: Address.h:154
@ C
Languages that the frontend can parse and compile.
@ Result
The result type of a method or function.
YAML serialization mapping.
Definition: Dominators.h:30
Definition: Format.h:4674
Iterator for iterating over Stmt * arrays that contain only T *.
Definition: Stmt.h:1135
Specifies a checker.
llvm::SmallVector< std::pair< StringRef, StringRef >, 0 > Dependencies