1 // BugReporter.cpp - Generate PathDiagnostics for Bugs ------------*- C++ -*--//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines BugReporter, a utility class for generating
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
16 #include "clang/AST/ASTContext.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/StmtCXX.h"
22 #include "clang/AST/StmtObjC.h"
23 #include "clang/Analysis/CFG.h"
24 #include "clang/Analysis/ProgramPoint.h"
25 #include "clang/Basic/SourceManager.h"
26 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
27 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/IntrusiveRefCntPtr.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/SmallString.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/Support/raw_ostream.h"
38 using namespace clang;
41 #define DEBUG_TYPE "BugReporter"
43 STATISTIC(MaxBugClassSize,
44 "The maximum number of bug reports in the same equivalence class");
45 STATISTIC(MaxValidBugClassSize,
46 "The maximum number of bug reports in the same equivalence class "
47 "where at least one report is valid (not suppressed)");
49 BugReporterVisitor::~BugReporterVisitor() {}
51 void BugReporterContext::anchor() {}
53 //===----------------------------------------------------------------------===//
54 // Helper routines for walking the ExplodedGraph and fetching statements.
55 //===----------------------------------------------------------------------===//
57 static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
58 for (N = N->getFirstPred(); N; N = N->getFirstPred())
59 if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
65 static inline const Stmt*
66 GetCurrentOrPreviousStmt(const ExplodedNode *N) {
67 if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
70 return GetPreviousStmt(N);
73 //===----------------------------------------------------------------------===//
74 // Diagnostic cleanup.
75 //===----------------------------------------------------------------------===//
77 static PathDiagnosticEventPiece *
78 eventsDescribeSameCondition(PathDiagnosticEventPiece *X,
79 PathDiagnosticEventPiece *Y) {
80 // Prefer diagnostics that come from ConditionBRVisitor over
81 // those that came from TrackConstraintBRVisitor.
82 const void *tagPreferred = ConditionBRVisitor::getTag();
83 const void *tagLesser = TrackConstraintBRVisitor::getTag();
85 if (X->getLocation() != Y->getLocation())
88 if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
91 if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
97 /// An optimization pass over PathPieces that removes redundant diagnostics
98 /// generated by both ConditionBRVisitor and TrackConstraintBRVisitor. Both
99 /// BugReporterVisitors use different methods to generate diagnostics, with
100 /// one capable of emitting diagnostics in some cases but not in others. This
101 /// can lead to redundant diagnostic pieces at the same point in a path.
102 static void removeRedundantMsgs(PathPieces &path) {
103 unsigned N = path.size();
106 // NOTE: this loop intentionally is not using an iterator. Instead, we
107 // are streaming the path and modifying it in place. This is done by
108 // grabbing the front, processing it, and if we decide to keep it append
109 // it to the end of the path. The entire path is processed in this way.
110 for (unsigned i = 0; i < N; ++i) {
111 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(path.front());
114 switch (piece->getKind()) {
115 case clang::ento::PathDiagnosticPiece::Call:
116 removeRedundantMsgs(cast<PathDiagnosticCallPiece>(piece)->path);
118 case clang::ento::PathDiagnosticPiece::Macro:
119 removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(piece)->subPieces);
121 case clang::ento::PathDiagnosticPiece::ControlFlow:
123 case clang::ento::PathDiagnosticPiece::Event: {
127 if (PathDiagnosticEventPiece *nextEvent =
128 dyn_cast<PathDiagnosticEventPiece>(path.front().get())) {
129 PathDiagnosticEventPiece *event =
130 cast<PathDiagnosticEventPiece>(piece);
131 // Check to see if we should keep one of the two pieces. If we
132 // come up with a preference, record which piece to keep, and consume
133 // another piece from the path.
134 if (PathDiagnosticEventPiece *pieceToKeep =
135 eventsDescribeSameCondition(event, nextEvent)) {
144 path.push_back(piece);
148 /// A map from PathDiagnosticPiece to the LocationContext of the inlined
149 /// function call it represents.
150 typedef llvm::DenseMap<const PathPieces *, const LocationContext *>
153 /// Recursively scan through a path and prune out calls and macros pieces
154 /// that aren't needed. Return true if afterwards the path contains
155 /// "interesting stuff" which means it shouldn't be pruned from the parent path.
156 static bool removeUnneededCalls(PathPieces &pieces, BugReport *R,
157 LocationContextMap &LCM) {
158 bool containsSomethingInteresting = false;
159 const unsigned N = pieces.size();
161 for (unsigned i = 0 ; i < N ; ++i) {
162 // Remove the front piece from the path. If it is still something we
163 // want to keep once we are done, we will push it back on the end.
164 IntrusiveRefCntPtr<PathDiagnosticPiece> piece(pieces.front());
167 switch (piece->getKind()) {
168 case PathDiagnosticPiece::Call: {
169 PathDiagnosticCallPiece *call = cast<PathDiagnosticCallPiece>(piece);
170 // Check if the location context is interesting.
171 assert(LCM.count(&call->path));
172 if (R->isInteresting(LCM[&call->path])) {
173 containsSomethingInteresting = true;
177 if (!removeUnneededCalls(call->path, R, LCM))
180 containsSomethingInteresting = true;
183 case PathDiagnosticPiece::Macro: {
184 PathDiagnosticMacroPiece *macro = cast<PathDiagnosticMacroPiece>(piece);
185 if (!removeUnneededCalls(macro->subPieces, R, LCM))
187 containsSomethingInteresting = true;
190 case PathDiagnosticPiece::Event: {
191 PathDiagnosticEventPiece *event = cast<PathDiagnosticEventPiece>(piece);
193 // We never throw away an event, but we do throw it away wholesale
194 // as part of a path if we throw the entire path away.
195 containsSomethingInteresting |= !event->isPrunable();
198 case PathDiagnosticPiece::ControlFlow:
202 pieces.push_back(piece);
205 return containsSomethingInteresting;
208 /// Returns true if the given decl has been implicitly given a body, either by
209 /// the analyzer or by the compiler proper.
210 static bool hasImplicitBody(const Decl *D) {
212 return D->isImplicit() || !D->hasBody();
215 /// Recursively scan through a path and make sure that all call pieces have
218 adjustCallLocations(PathPieces &Pieces,
219 PathDiagnosticLocation *LastCallLocation = nullptr) {
220 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E; ++I) {
221 PathDiagnosticCallPiece *Call = dyn_cast<PathDiagnosticCallPiece>(*I);
224 assert((*I)->getLocation().asLocation().isValid());
228 if (LastCallLocation) {
229 bool CallerIsImplicit = hasImplicitBody(Call->getCaller());
230 if (CallerIsImplicit || !Call->callEnter.asLocation().isValid())
231 Call->callEnter = *LastCallLocation;
232 if (CallerIsImplicit || !Call->callReturn.asLocation().isValid())
233 Call->callReturn = *LastCallLocation;
236 // Recursively clean out the subclass. Keep this call around if
237 // it contains any informative diagnostics.
238 PathDiagnosticLocation *ThisCallLocation;
239 if (Call->callEnterWithin.asLocation().isValid() &&
240 !hasImplicitBody(Call->getCallee()))
241 ThisCallLocation = &Call->callEnterWithin;
243 ThisCallLocation = &Call->callEnter;
245 assert(ThisCallLocation && "Outermost call has an invalid location");
246 adjustCallLocations(Call->path, ThisCallLocation);
250 /// Remove edges in and out of C++ default initializer expressions. These are
251 /// for fields that have in-class initializers, as opposed to being initialized
252 /// explicitly in a constructor or braced list.
253 static void removeEdgesToDefaultInitializers(PathPieces &Pieces) {
254 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
255 if (PathDiagnosticCallPiece *C = dyn_cast<PathDiagnosticCallPiece>(*I))
256 removeEdgesToDefaultInitializers(C->path);
258 if (PathDiagnosticMacroPiece *M = dyn_cast<PathDiagnosticMacroPiece>(*I))
259 removeEdgesToDefaultInitializers(M->subPieces);
261 if (PathDiagnosticControlFlowPiece *CF =
262 dyn_cast<PathDiagnosticControlFlowPiece>(*I)) {
263 const Stmt *Start = CF->getStartLocation().asStmt();
264 const Stmt *End = CF->getEndLocation().asStmt();
265 if (Start && isa<CXXDefaultInitExpr>(Start)) {
268 } else if (End && isa<CXXDefaultInitExpr>(End)) {
269 PathPieces::iterator Next = std::next(I);
271 if (PathDiagnosticControlFlowPiece *NextCF =
272 dyn_cast<PathDiagnosticControlFlowPiece>(*Next)) {
273 NextCF->setStartLocation(CF->getStartLocation());
285 /// Remove all pieces with invalid locations as these cannot be serialized.
286 /// We might have pieces with invalid locations as a result of inlining Body
287 /// Farm generated functions.
288 static void removePiecesWithInvalidLocations(PathPieces &Pieces) {
289 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
290 if (PathDiagnosticCallPiece *C = dyn_cast<PathDiagnosticCallPiece>(*I))
291 removePiecesWithInvalidLocations(C->path);
293 if (PathDiagnosticMacroPiece *M = dyn_cast<PathDiagnosticMacroPiece>(*I))
294 removePiecesWithInvalidLocations(M->subPieces);
296 if (!(*I)->getLocation().isValid() ||
297 !(*I)->getLocation().asLocation().isValid()) {
305 //===----------------------------------------------------------------------===//
306 // PathDiagnosticBuilder and its associated routines and helper objects.
307 //===----------------------------------------------------------------------===//
310 class NodeMapClosure : public BugReport::NodeResolver {
311 InterExplodedGraphMap &M;
313 NodeMapClosure(InterExplodedGraphMap &m) : M(m) {}
315 const ExplodedNode *getOriginalNode(const ExplodedNode *N) override {
320 class PathDiagnosticBuilder : public BugReporterContext {
322 PathDiagnosticConsumer *PDC;
325 const LocationContext *LC;
327 PathDiagnosticBuilder(GRBugReporter &br,
328 BugReport *r, InterExplodedGraphMap &Backmap,
329 PathDiagnosticConsumer *pdc)
330 : BugReporterContext(br),
331 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
334 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
336 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
337 const ExplodedNode *N);
339 BugReport *getBugReport() { return R; }
341 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
343 ParentMap& getParentMap() { return LC->getParentMap(); }
345 const Stmt *getParent(const Stmt *S) {
346 return getParentMap().getParent(S);
349 NodeMapClosure& getNodeResolver() override { return NMC; }
351 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
353 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
354 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
357 bool supportsLogicalOpControlFlow() const {
358 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
361 } // end anonymous namespace
363 PathDiagnosticLocation
364 PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
365 if (const Stmt *S = PathDiagnosticLocation::getNextStmt(N))
366 return PathDiagnosticLocation(S, getSourceManager(), LC);
368 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
372 PathDiagnosticLocation
373 PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
374 const ExplodedNode *N) {
376 // Slow, but probably doesn't matter.
377 if (os.str().empty())
380 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
383 os << "Execution continues on line "
384 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
387 os << "Execution jumps to the end of the ";
388 const Decl *D = N->getLocationContext()->getDecl();
389 if (isa<ObjCMethodDecl>(D))
391 else if (isa<FunctionDecl>(D))
394 assert(isa<BlockDecl>(D));
395 os << "anonymous block";
403 static const Stmt *getEnclosingParent(const Stmt *S, const ParentMap &PM) {
404 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
405 return PM.getParentIgnoreParens(S);
407 const Stmt *Parent = PM.getParentIgnoreParens(S);
411 switch (Parent->getStmtClass()) {
412 case Stmt::ForStmtClass:
413 case Stmt::DoStmtClass:
414 case Stmt::WhileStmtClass:
415 case Stmt::ObjCForCollectionStmtClass:
416 case Stmt::CXXForRangeStmtClass:
425 static PathDiagnosticLocation
426 getEnclosingStmtLocation(const Stmt *S, SourceManager &SMgr, const ParentMap &P,
427 const LocationContext *LC, bool allowNestedContexts) {
429 return PathDiagnosticLocation();
431 while (const Stmt *Parent = getEnclosingParent(S, P)) {
432 switch (Parent->getStmtClass()) {
433 case Stmt::BinaryOperatorClass: {
434 const BinaryOperator *B = cast<BinaryOperator>(Parent);
435 if (B->isLogicalOp())
436 return PathDiagnosticLocation(allowNestedContexts ? B : S, SMgr, LC);
439 case Stmt::CompoundStmtClass:
440 case Stmt::StmtExprClass:
441 return PathDiagnosticLocation(S, SMgr, LC);
442 case Stmt::ChooseExprClass:
443 // Similar to '?' if we are referring to condition, just have the edge
444 // point to the entire choose expression.
445 if (allowNestedContexts || cast<ChooseExpr>(Parent)->getCond() == S)
446 return PathDiagnosticLocation(Parent, SMgr, LC);
448 return PathDiagnosticLocation(S, SMgr, LC);
449 case Stmt::BinaryConditionalOperatorClass:
450 case Stmt::ConditionalOperatorClass:
451 // For '?', if we are referring to condition, just have the edge point
452 // to the entire '?' expression.
453 if (allowNestedContexts ||
454 cast<AbstractConditionalOperator>(Parent)->getCond() == S)
455 return PathDiagnosticLocation(Parent, SMgr, LC);
457 return PathDiagnosticLocation(S, SMgr, LC);
458 case Stmt::CXXForRangeStmtClass:
459 if (cast<CXXForRangeStmt>(Parent)->getBody() == S)
460 return PathDiagnosticLocation(S, SMgr, LC);
462 case Stmt::DoStmtClass:
463 return PathDiagnosticLocation(S, SMgr, LC);
464 case Stmt::ForStmtClass:
465 if (cast<ForStmt>(Parent)->getBody() == S)
466 return PathDiagnosticLocation(S, SMgr, LC);
468 case Stmt::IfStmtClass:
469 if (cast<IfStmt>(Parent)->getCond() != S)
470 return PathDiagnosticLocation(S, SMgr, LC);
472 case Stmt::ObjCForCollectionStmtClass:
473 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
474 return PathDiagnosticLocation(S, SMgr, LC);
476 case Stmt::WhileStmtClass:
477 if (cast<WhileStmt>(Parent)->getCond() != S)
478 return PathDiagnosticLocation(S, SMgr, LC);
487 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
489 return PathDiagnosticLocation(S, SMgr, LC);
492 PathDiagnosticLocation
493 PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
494 assert(S && "Null Stmt passed to getEnclosingStmtLocation");
495 return ::getEnclosingStmtLocation(S, getSourceManager(), getParentMap(), LC,
496 /*allowNestedContexts=*/false);
499 //===----------------------------------------------------------------------===//
500 // "Visitors only" path diagnostic generation algorithm.
501 //===----------------------------------------------------------------------===//
502 static bool GenerateVisitorsOnlyPathDiagnostic(
503 PathDiagnostic &PD, PathDiagnosticBuilder &PDB, const ExplodedNode *N,
504 ArrayRef<std::unique_ptr<BugReporterVisitor>> visitors) {
505 // All path generation skips the very first node (the error node).
506 // This is because there is special handling for the end-of-path note.
507 N = N->getFirstPred();
511 BugReport *R = PDB.getBugReport();
512 while (const ExplodedNode *Pred = N->getFirstPred()) {
513 for (auto &V : visitors) {
514 // Visit all the node pairs, but throw the path pieces away.
515 PathDiagnosticPiece *Piece = V->VisitNode(N, Pred, PDB, *R);
525 //===----------------------------------------------------------------------===//
526 // "Minimal" path diagnostic generation algorithm.
527 //===----------------------------------------------------------------------===//
528 typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
529 typedef SmallVector<StackDiagPair, 6> StackDiagVector;
531 static void updateStackPiecesWithMessage(PathDiagnosticPiece *P,
532 StackDiagVector &CallStack) {
533 // If the piece contains a special message, add it to all the call
534 // pieces on the active stack.
535 if (PathDiagnosticEventPiece *ep =
536 dyn_cast<PathDiagnosticEventPiece>(P)) {
538 if (ep->hasCallStackHint())
539 for (StackDiagVector::iterator I = CallStack.begin(),
540 E = CallStack.end(); I != E; ++I) {
541 PathDiagnosticCallPiece *CP = I->first;
542 const ExplodedNode *N = I->second;
543 std::string stackMsg = ep->getCallStackMessage(N);
545 // The last message on the path to final bug is the most important
546 // one. Since we traverse the path backwards, do not add the message
547 // if one has been previously added.
548 if (!CP->hasCallStackMessage())
549 CP->setCallStackMessage(stackMsg);
554 static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
556 static bool GenerateMinimalPathDiagnostic(
557 PathDiagnostic &PD, PathDiagnosticBuilder &PDB, const ExplodedNode *N,
558 LocationContextMap &LCM,
559 ArrayRef<std::unique_ptr<BugReporterVisitor>> visitors) {
561 SourceManager& SMgr = PDB.getSourceManager();
562 const LocationContext *LC = PDB.LC;
563 const ExplodedNode *NextNode = N->pred_empty()
564 ? nullptr : *(N->pred_begin());
566 StackDiagVector CallStack;
570 PDB.LC = N->getLocationContext();
571 NextNode = N->getFirstPred();
573 ProgramPoint P = N->getLocation();
576 if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
577 PathDiagnosticCallPiece *C =
578 PathDiagnosticCallPiece::construct(N, *CE, SMgr);
579 // Record the mapping from call piece to LocationContext.
580 LCM[&C->path] = CE->getCalleeContext();
581 PD.getActivePath().push_front(C);
582 PD.pushActivePath(&C->path);
583 CallStack.push_back(StackDiagPair(C, N));
587 if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
588 // Flush all locations, and pop the active path.
589 bool VisitedEntireCall = PD.isWithinCall();
592 // Either we just added a bunch of stuff to the top-level path, or
593 // we have a previous CallExitEnd. If the former, it means that the
594 // path terminated within a function call. We must then take the
595 // current contents of the active path and place it within
596 // a new PathDiagnosticCallPiece.
597 PathDiagnosticCallPiece *C;
598 if (VisitedEntireCall) {
599 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
601 const Decl *Caller = CE->getLocationContext()->getDecl();
602 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
603 // Record the mapping from call piece to LocationContext.
604 LCM[&C->path] = CE->getCalleeContext();
607 C->setCallee(*CE, SMgr);
608 if (!CallStack.empty()) {
609 assert(CallStack.back().first == C);
610 CallStack.pop_back();
615 if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
616 const CFGBlock *Src = BE->getSrc();
617 const CFGBlock *Dst = BE->getDst();
618 const Stmt *T = Src->getTerminator();
623 PathDiagnosticLocation Start =
624 PathDiagnosticLocation::createBegin(T, SMgr,
625 N->getLocationContext());
627 switch (T->getStmtClass()) {
631 case Stmt::GotoStmtClass:
632 case Stmt::IndirectGotoStmtClass: {
633 const Stmt *S = PathDiagnosticLocation::getNextStmt(N);
639 llvm::raw_string_ostream os(sbuf);
640 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
642 os << "Control jumps to line "
643 << End.asLocation().getExpansionLineNumber();
644 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
645 Start, End, os.str()));
649 case Stmt::SwitchStmtClass: {
650 // Figure out what case arm we took.
652 llvm::raw_string_ostream os(sbuf);
654 if (const Stmt *S = Dst->getLabel()) {
655 PathDiagnosticLocation End(S, SMgr, LC);
657 switch (S->getStmtClass()) {
659 os << "No cases match in the switch statement. "
660 "Control jumps to line "
661 << End.asLocation().getExpansionLineNumber();
663 case Stmt::DefaultStmtClass:
664 os << "Control jumps to the 'default' case at line "
665 << End.asLocation().getExpansionLineNumber();
668 case Stmt::CaseStmtClass: {
669 os << "Control jumps to 'case ";
670 const CaseStmt *Case = cast<CaseStmt>(S);
671 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
673 // Determine if it is an enum.
674 bool GetRawInt = true;
676 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
677 // FIXME: Maybe this should be an assertion. Are there cases
678 // were it is not an EnumConstantDecl?
679 const EnumConstantDecl *D =
680 dyn_cast<EnumConstantDecl>(DR->getDecl());
689 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
692 << End.asLocation().getExpansionLineNumber();
696 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
697 Start, End, os.str()));
700 os << "'Default' branch taken. ";
701 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
702 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
703 Start, End, os.str()));
709 case Stmt::BreakStmtClass:
710 case Stmt::ContinueStmtClass: {
712 llvm::raw_string_ostream os(sbuf);
713 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
714 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
715 Start, End, os.str()));
719 // Determine control-flow for ternary '?'.
720 case Stmt::BinaryConditionalOperatorClass:
721 case Stmt::ConditionalOperatorClass: {
723 llvm::raw_string_ostream os(sbuf);
724 os << "'?' condition is ";
726 if (*(Src->succ_begin()+1) == Dst)
731 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
733 if (const Stmt *S = End.asStmt())
734 End = PDB.getEnclosingStmtLocation(S);
736 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
737 Start, End, os.str()));
741 // Determine control-flow for short-circuited '&&' and '||'.
742 case Stmt::BinaryOperatorClass: {
743 if (!PDB.supportsLogicalOpControlFlow())
746 const BinaryOperator *B = cast<BinaryOperator>(T);
748 llvm::raw_string_ostream os(sbuf);
749 os << "Left side of '";
751 if (B->getOpcode() == BO_LAnd) {
752 os << "&&" << "' is ";
754 if (*(Src->succ_begin()+1) == Dst) {
756 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
757 PathDiagnosticLocation Start =
758 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
759 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
760 Start, End, os.str()));
764 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
765 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
766 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
767 Start, End, os.str()));
771 assert(B->getOpcode() == BO_LOr);
772 os << "||" << "' is ";
774 if (*(Src->succ_begin()+1) == Dst) {
776 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
777 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
778 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
779 Start, End, os.str()));
783 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
784 PathDiagnosticLocation Start =
785 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
786 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
787 Start, End, os.str()));
794 case Stmt::DoStmtClass: {
795 if (*(Src->succ_begin()) == Dst) {
797 llvm::raw_string_ostream os(sbuf);
799 os << "Loop condition is true. ";
800 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
802 if (const Stmt *S = End.asStmt())
803 End = PDB.getEnclosingStmtLocation(S);
805 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
806 Start, End, os.str()));
809 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
811 if (const Stmt *S = End.asStmt())
812 End = PDB.getEnclosingStmtLocation(S);
814 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
815 Start, End, "Loop condition is false. Exiting loop"));
821 case Stmt::WhileStmtClass:
822 case Stmt::ForStmtClass: {
823 if (*(Src->succ_begin()+1) == Dst) {
825 llvm::raw_string_ostream os(sbuf);
827 os << "Loop condition is false. ";
828 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
829 if (const Stmt *S = End.asStmt())
830 End = PDB.getEnclosingStmtLocation(S);
832 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
833 Start, End, os.str()));
836 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
837 if (const Stmt *S = End.asStmt())
838 End = PDB.getEnclosingStmtLocation(S);
840 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
841 Start, End, "Loop condition is true. Entering loop body"));
847 case Stmt::IfStmtClass: {
848 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
850 if (const Stmt *S = End.asStmt())
851 End = PDB.getEnclosingStmtLocation(S);
853 if (*(Src->succ_begin()+1) == Dst)
854 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
855 Start, End, "Taking false branch"));
857 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(
858 Start, End, "Taking true branch"));
867 // Add diagnostic pieces from custom visitors.
868 BugReport *R = PDB.getBugReport();
869 for (auto &V : visitors) {
870 if (PathDiagnosticPiece *p = V->VisitNode(N, NextNode, PDB, *R)) {
871 PD.getActivePath().push_front(p);
872 updateStackPiecesWithMessage(p, CallStack);
878 if (!PDB.getBugReport()->isValid())
881 // After constructing the full PathDiagnostic, do a pass over it to compact
882 // PathDiagnosticPieces that occur within a macro.
883 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
887 //===----------------------------------------------------------------------===//
888 // "Extensive" PathDiagnostic generation.
889 //===----------------------------------------------------------------------===//
891 static bool IsControlFlowExpr(const Stmt *S) {
892 const Expr *E = dyn_cast<Expr>(S);
897 E = E->IgnoreParenCasts();
899 if (isa<AbstractConditionalOperator>(E))
902 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
903 if (B->isLogicalOp())
910 class ContextLocation : public PathDiagnosticLocation {
913 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
914 : PathDiagnosticLocation(L), IsDead(isdead) {}
916 void markDead() { IsDead = true; }
917 bool isDead() const { return IsDead; }
920 static PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
921 const LocationContext *LC,
922 bool firstCharOnly = false) {
923 if (const Stmt *S = L.asStmt()) {
924 const Stmt *Original = S;
926 // Adjust the location for some expressions that are best referenced
927 // by one of their subexpressions.
928 switch (S->getStmtClass()) {
931 case Stmt::ParenExprClass:
932 case Stmt::GenericSelectionExprClass:
933 S = cast<Expr>(S)->IgnoreParens();
934 firstCharOnly = true;
936 case Stmt::BinaryConditionalOperatorClass:
937 case Stmt::ConditionalOperatorClass:
938 S = cast<AbstractConditionalOperator>(S)->getCond();
939 firstCharOnly = true;
941 case Stmt::ChooseExprClass:
942 S = cast<ChooseExpr>(S)->getCond();
943 firstCharOnly = true;
945 case Stmt::BinaryOperatorClass:
946 S = cast<BinaryOperator>(S)->getLHS();
947 firstCharOnly = true;
955 L = PathDiagnosticLocation(S, L.getManager(), LC);
959 L = PathDiagnosticLocation::createSingleLocation(L);
965 std::vector<ContextLocation> CLocs;
966 typedef std::vector<ContextLocation>::iterator iterator;
968 PathDiagnosticBuilder &PDB;
969 PathDiagnosticLocation PrevLoc;
971 bool IsConsumedExpr(const PathDiagnosticLocation &L);
973 bool containsLocation(const PathDiagnosticLocation &Container,
974 const PathDiagnosticLocation &Containee);
976 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
981 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
982 // For contexts, we only one the first character as the range.
983 rawAddEdge(cleanUpLocation(CLocs.back(), PDB.LC, true));
989 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
992 // If the PathDiagnostic already has pieces, add the enclosing statement
993 // of the first piece as a context as well.
994 if (!PD.path.empty()) {
995 PrevLoc = (*PD.path.begin())->getLocation();
997 if (const Stmt *S = PrevLoc.asStmt())
998 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1003 while (!CLocs.empty()) popLocation();
1005 // Finally, add an initial edge from the start location of the first
1006 // statement (if it doesn't already exist).
1007 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
1009 PDB.getSourceManager());
1014 void flushLocations() {
1015 while (!CLocs.empty())
1017 PrevLoc = PathDiagnosticLocation();
1020 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false,
1021 bool IsPostJump = false);
1023 void rawAddEdge(PathDiagnosticLocation NewLoc);
1025 void addContext(const Stmt *S);
1026 void addContext(const PathDiagnosticLocation &L);
1027 void addExtendedContext(const Stmt *S);
1029 } // end anonymous namespace
1032 PathDiagnosticLocation
1033 EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1034 if (const Stmt *S = L.asStmt()) {
1035 if (IsControlFlowExpr(S))
1038 return PDB.getEnclosingStmtLocation(S);
1044 bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1045 const PathDiagnosticLocation &Containee) {
1047 if (Container == Containee)
1050 if (Container.asDecl())
1053 if (const Stmt *S = Containee.asStmt())
1054 if (const Stmt *ContainerS = Container.asStmt()) {
1056 if (S == ContainerS)
1058 S = PDB.getParent(S);
1063 // Less accurate: compare using source ranges.
1064 SourceRange ContainerR = Container.asRange();
1065 SourceRange ContaineeR = Containee.asRange();
1067 SourceManager &SM = PDB.getSourceManager();
1068 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1069 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1070 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1071 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
1073 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1074 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1075 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1076 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
1078 assert(ContainerBegLine <= ContainerEndLine);
1079 assert(ContaineeBegLine <= ContaineeEndLine);
1081 return (ContainerBegLine <= ContaineeBegLine &&
1082 ContainerEndLine >= ContaineeEndLine &&
1083 (ContainerBegLine != ContaineeBegLine ||
1084 SM.getExpansionColumnNumber(ContainerRBeg) <=
1085 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
1086 (ContainerEndLine != ContaineeEndLine ||
1087 SM.getExpansionColumnNumber(ContainerREnd) >=
1088 SM.getExpansionColumnNumber(ContaineeREnd)));
1091 void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1092 if (!PrevLoc.isValid()) {
1097 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc, PDB.LC);
1098 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc, PDB.LC);
1100 if (PrevLocClean.asLocation().isInvalid()) {
1105 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
1108 // FIXME: Ignore intra-macro edges for now.
1109 if (NewLocClean.asLocation().getExpansionLoc() ==
1110 PrevLocClean.asLocation().getExpansionLoc())
1113 PD.getActivePath().push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean));
1117 void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd,
1120 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1123 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1125 while (!CLocs.empty()) {
1126 ContextLocation &TopContextLoc = CLocs.back();
1128 // Is the top location context the same as the one for the new location?
1129 if (TopContextLoc == CLoc) {
1131 if (IsConsumedExpr(TopContextLoc))
1132 TopContextLoc.markDead();
1138 TopContextLoc.markDead();
1142 if (containsLocation(TopContextLoc, CLoc)) {
1146 if (IsConsumedExpr(CLoc)) {
1147 CLocs.push_back(ContextLocation(CLoc, /*IsDead=*/true));
1152 CLocs.push_back(ContextLocation(CLoc, /*IsDead=*/IsPostJump));
1156 // Context does not contain the location. Flush it.
1160 // If we reach here, there is no enclosing context. Just add the edge.
1164 bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1165 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1166 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
1171 void EdgeBuilder::addExtendedContext(const Stmt *S) {
1175 const Stmt *Parent = PDB.getParent(S);
1177 if (isa<CompoundStmt>(Parent))
1178 Parent = PDB.getParent(Parent);
1184 switch (Parent->getStmtClass()) {
1185 case Stmt::DoStmtClass:
1186 case Stmt::ObjCAtSynchronizedStmtClass:
1196 void EdgeBuilder::addContext(const Stmt *S) {
1200 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
1204 void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
1205 while (!CLocs.empty()) {
1206 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1208 // Is the top location context the same as the one for the new location?
1209 if (TopContextLoc == L)
1212 if (containsLocation(TopContextLoc, L)) {
1217 // Context does not contain the location. Flush it.
1224 // Cone-of-influence: support the reverse propagation of "interesting" symbols
1225 // and values by tracing interesting calculations backwards through evaluated
1226 // expressions along a path. This is probably overly complicated, but the idea
1227 // is that if an expression computed an "interesting" value, the child
1228 // expressions are are also likely to be "interesting" as well (which then
1229 // propagates to the values they in turn compute). This reverse propagation
1230 // is needed to track interesting correlations across function call boundaries,
1231 // where formal arguments bind to actual arguments, etc. This is also needed
1232 // because the constraint solver sometimes simplifies certain symbolic values
1233 // into constants when appropriate, and this complicates reasoning about
1234 // interesting values.
1235 typedef llvm::DenseSet<const Expr *> InterestingExprs;
1237 static void reversePropagateIntererstingSymbols(BugReport &R,
1238 InterestingExprs &IE,
1239 const ProgramState *State,
1241 const LocationContext *LCtx) {
1242 SVal V = State->getSVal(Ex, LCtx);
1243 if (!(R.isInteresting(V) || IE.count(Ex)))
1246 switch (Ex->getStmtClass()) {
1248 if (!isa<CastExpr>(Ex))
1251 case Stmt::BinaryOperatorClass:
1252 case Stmt::UnaryOperatorClass: {
1253 for (const Stmt *SubStmt : Ex->children()) {
1254 if (const Expr *child = dyn_cast_or_null<Expr>(SubStmt)) {
1256 SVal ChildV = State->getSVal(child, LCtx);
1257 R.markInteresting(ChildV);
1264 R.markInteresting(V);
1267 static void reversePropagateInterestingSymbols(BugReport &R,
1268 InterestingExprs &IE,
1269 const ProgramState *State,
1270 const LocationContext *CalleeCtx,
1271 const LocationContext *CallerCtx)
1273 // FIXME: Handle non-CallExpr-based CallEvents.
1274 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1275 const Stmt *CallSite = Callee->getCallSite();
1276 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
1277 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1278 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1279 PE = FD->param_end();
1280 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1281 for (; AI != AE && PI != PE; ++AI, ++PI) {
1282 if (const Expr *ArgE = *AI) {
1283 if (const ParmVarDecl *PD = *PI) {
1284 Loc LV = State->getLValue(PD, CalleeCtx);
1285 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1294 //===----------------------------------------------------------------------===//
1295 // Functions for determining if a loop was executed 0 times.
1296 //===----------------------------------------------------------------------===//
1298 static bool isLoop(const Stmt *Term) {
1299 switch (Term->getStmtClass()) {
1300 case Stmt::ForStmtClass:
1301 case Stmt::WhileStmtClass:
1302 case Stmt::ObjCForCollectionStmtClass:
1303 case Stmt::CXXForRangeStmtClass:
1306 // Note that we intentionally do not include do..while here.
1311 static bool isJumpToFalseBranch(const BlockEdge *BE) {
1312 const CFGBlock *Src = BE->getSrc();
1313 assert(Src->succ_size() == 2);
1314 return (*(Src->succ_begin()+1) == BE->getDst());
1317 /// Return true if the terminator is a loop and the destination is the
1319 static bool isLoopJumpPastBody(const Stmt *Term, const BlockEdge *BE) {
1323 // Did we take the false branch?
1324 return isJumpToFalseBranch(BE);
1327 static bool isContainedByStmt(ParentMap &PM, const Stmt *S, const Stmt *SubS) {
1331 SubS = PM.getParent(SubS);
1336 static const Stmt *getStmtBeforeCond(ParentMap &PM, const Stmt *Term,
1337 const ExplodedNode *N) {
1339 Optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>();
1341 const Stmt *S = SP->getStmt();
1342 if (!isContainedByStmt(PM, Term, S))
1345 N = N->getFirstPred();
1350 static bool isInLoopBody(ParentMap &PM, const Stmt *S, const Stmt *Term) {
1351 const Stmt *LoopBody = nullptr;
1352 switch (Term->getStmtClass()) {
1353 case Stmt::CXXForRangeStmtClass: {
1354 const CXXForRangeStmt *FR = cast<CXXForRangeStmt>(Term);
1355 if (isContainedByStmt(PM, FR->getInc(), S))
1357 if (isContainedByStmt(PM, FR->getLoopVarStmt(), S))
1359 LoopBody = FR->getBody();
1362 case Stmt::ForStmtClass: {
1363 const ForStmt *FS = cast<ForStmt>(Term);
1364 if (isContainedByStmt(PM, FS->getInc(), S))
1366 LoopBody = FS->getBody();
1369 case Stmt::ObjCForCollectionStmtClass: {
1370 const ObjCForCollectionStmt *FC = cast<ObjCForCollectionStmt>(Term);
1371 LoopBody = FC->getBody();
1374 case Stmt::WhileStmtClass:
1375 LoopBody = cast<WhileStmt>(Term)->getBody();
1380 return isContainedByStmt(PM, LoopBody, S);
1383 //===----------------------------------------------------------------------===//
1384 // Top-level logic for generating extensive path diagnostics.
1385 //===----------------------------------------------------------------------===//
1387 static bool GenerateExtensivePathDiagnostic(
1388 PathDiagnostic &PD, PathDiagnosticBuilder &PDB, const ExplodedNode *N,
1389 LocationContextMap &LCM,
1390 ArrayRef<std::unique_ptr<BugReporterVisitor>> visitors) {
1391 EdgeBuilder EB(PD, PDB);
1392 const SourceManager& SM = PDB.getSourceManager();
1393 StackDiagVector CallStack;
1394 InterestingExprs IE;
1396 const ExplodedNode *NextNode = N->pred_empty() ? nullptr : *(N->pred_begin());
1399 NextNode = N->getFirstPred();
1400 ProgramPoint P = N->getLocation();
1403 if (Optional<PostStmt> PS = P.getAs<PostStmt>()) {
1404 if (const Expr *Ex = PS->getStmtAs<Expr>())
1405 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1406 N->getState().get(), Ex,
1407 N->getLocationContext());
1410 if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1411 const Stmt *S = CE->getCalleeContext()->getCallSite();
1412 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
1413 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1414 N->getState().get(), Ex,
1415 N->getLocationContext());
1418 PathDiagnosticCallPiece *C =
1419 PathDiagnosticCallPiece::construct(N, *CE, SM);
1420 LCM[&C->path] = CE->getCalleeContext();
1422 EB.addEdge(C->callReturn, /*AlwaysAdd=*/true, /*IsPostJump=*/true);
1423 EB.flushLocations();
1425 PD.getActivePath().push_front(C);
1426 PD.pushActivePath(&C->path);
1427 CallStack.push_back(StackDiagPair(C, N));
1431 // Pop the call hierarchy if we are done walking the contents
1432 // of a function call.
1433 if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
1434 // Add an edge to the start of the function.
1435 const Decl *D = CE->getCalleeContext()->getDecl();
1436 PathDiagnosticLocation pos =
1437 PathDiagnosticLocation::createBegin(D, SM);
1440 // Flush all locations, and pop the active path.
1441 bool VisitedEntireCall = PD.isWithinCall();
1442 EB.flushLocations();
1444 PDB.LC = N->getLocationContext();
1446 // Either we just added a bunch of stuff to the top-level path, or
1447 // we have a previous CallExitEnd. If the former, it means that the
1448 // path terminated within a function call. We must then take the
1449 // current contents of the active path and place it within
1450 // a new PathDiagnosticCallPiece.
1451 PathDiagnosticCallPiece *C;
1452 if (VisitedEntireCall) {
1453 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front());
1455 const Decl *Caller = CE->getLocationContext()->getDecl();
1456 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1457 LCM[&C->path] = CE->getCalleeContext();
1460 C->setCallee(*CE, SM);
1461 EB.addContext(C->getLocation());
1463 if (!CallStack.empty()) {
1464 assert(CallStack.back().first == C);
1465 CallStack.pop_back();
1470 // Note that is important that we update the LocationContext
1471 // after looking at CallExits. CallExit basically adds an
1472 // edge in the *caller*, so we don't want to update the LocationContext
1474 PDB.LC = N->getLocationContext();
1477 if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
1478 // Does this represent entering a call? If so, look at propagating
1479 // interesting symbols across call boundaries.
1481 const LocationContext *CallerCtx = NextNode->getLocationContext();
1482 const LocationContext *CalleeCtx = PDB.LC;
1483 if (CallerCtx != CalleeCtx) {
1484 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1485 N->getState().get(),
1486 CalleeCtx, CallerCtx);
1490 // Are we jumping to the head of a loop? Add a special diagnostic.
1491 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1492 PathDiagnosticLocation L(Loop, SM, PDB.LC);
1493 const CompoundStmt *CS = nullptr;
1495 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1496 CS = dyn_cast<CompoundStmt>(FS->getBody());
1497 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1498 CS = dyn_cast<CompoundStmt>(WS->getBody());
1500 PathDiagnosticEventPiece *p =
1501 new PathDiagnosticEventPiece(L,
1502 "Looping back to the head of the loop");
1503 p->setPrunable(true);
1505 EB.addEdge(p->getLocation(), true);
1506 PD.getActivePath().push_front(p);
1509 PathDiagnosticLocation BL =
1510 PathDiagnosticLocation::createEndBrace(CS, SM);
1515 const CFGBlock *BSrc = BE->getSrc();
1516 ParentMap &PM = PDB.getParentMap();
1518 if (const Stmt *Term = BSrc->getTerminator()) {
1519 // Are we jumping past the loop body without ever executing the
1520 // loop (because the condition was false)?
1521 if (isLoopJumpPastBody(Term, &*BE) &&
1523 getStmtBeforeCond(PM,
1524 BSrc->getTerminatorCondition(),
1527 PathDiagnosticLocation L(Term, SM, PDB.LC);
1528 PathDiagnosticEventPiece *PE =
1529 new PathDiagnosticEventPiece(L, "Loop body executed 0 times");
1530 PE->setPrunable(true);
1532 EB.addEdge(PE->getLocation(), true);
1533 PD.getActivePath().push_front(PE);
1536 // In any case, add the terminator as the current statement
1537 // context for control edges.
1538 EB.addContext(Term);
1544 if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
1545 Optional<CFGElement> First = BE->getFirstElement();
1546 if (Optional<CFGStmt> S = First ? First->getAs<CFGStmt>() : None) {
1547 const Stmt *stmt = S->getStmt();
1548 if (IsControlFlowExpr(stmt)) {
1549 // Add the proper context for '&&', '||', and '?'.
1550 EB.addContext(stmt);
1553 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
1565 // Add pieces from custom visitors.
1566 BugReport *R = PDB.getBugReport();
1567 for (auto &V : visitors) {
1568 if (PathDiagnosticPiece *p = V->VisitNode(N, NextNode, PDB, *R)) {
1569 const PathDiagnosticLocation &Loc = p->getLocation();
1570 EB.addEdge(Loc, true);
1571 PD.getActivePath().push_front(p);
1572 updateStackPiecesWithMessage(p, CallStack);
1574 if (const Stmt *S = Loc.asStmt())
1575 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1580 return PDB.getBugReport()->isValid();
1583 /// \brief Adds a sanitized control-flow diagnostic edge to a path.
1584 static void addEdgeToPath(PathPieces &path,
1585 PathDiagnosticLocation &PrevLoc,
1586 PathDiagnosticLocation NewLoc,
1587 const LocationContext *LC) {
1588 if (!NewLoc.isValid())
1591 SourceLocation NewLocL = NewLoc.asLocation();
1592 if (NewLocL.isInvalid())
1595 if (!PrevLoc.isValid() || !PrevLoc.asLocation().isValid()) {
1600 // Ignore self-edges, which occur when there are multiple nodes at the same
1602 if (NewLoc.asStmt() && NewLoc.asStmt() == PrevLoc.asStmt())
1605 path.push_front(new PathDiagnosticControlFlowPiece(NewLoc,
1610 /// A customized wrapper for CFGBlock::getTerminatorCondition()
1611 /// which returns the element for ObjCForCollectionStmts.
1612 static const Stmt *getTerminatorCondition(const CFGBlock *B) {
1613 const Stmt *S = B->getTerminatorCondition();
1614 if (const ObjCForCollectionStmt *FS =
1615 dyn_cast_or_null<ObjCForCollectionStmt>(S))
1616 return FS->getElement();
1620 static const char StrEnteringLoop[] = "Entering loop body";
1621 static const char StrLoopBodyZero[] = "Loop body executed 0 times";
1622 static const char StrLoopRangeEmpty[] =
1623 "Loop body skipped when range is empty";
1624 static const char StrLoopCollectionEmpty[] =
1625 "Loop body skipped when collection is empty";
1627 static bool GenerateAlternateExtensivePathDiagnostic(
1628 PathDiagnostic &PD, PathDiagnosticBuilder &PDB, const ExplodedNode *N,
1629 LocationContextMap &LCM,
1630 ArrayRef<std::unique_ptr<BugReporterVisitor>> visitors) {
1632 BugReport *report = PDB.getBugReport();
1633 const SourceManager& SM = PDB.getSourceManager();
1634 StackDiagVector CallStack;
1635 InterestingExprs IE;
1637 PathDiagnosticLocation PrevLoc = PD.getLocation();
1639 const ExplodedNode *NextNode = N->getFirstPred();
1642 NextNode = N->getFirstPred();
1643 ProgramPoint P = N->getLocation();
1646 // Have we encountered an entrance to a call? It may be
1647 // the case that we have not encountered a matching
1648 // call exit before this point. This means that the path
1649 // terminated within the call itself.
1650 if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
1651 // Add an edge to the start of the function.
1652 const StackFrameContext *CalleeLC = CE->getCalleeContext();
1653 const Decl *D = CalleeLC->getDecl();
1654 addEdgeToPath(PD.getActivePath(), PrevLoc,
1655 PathDiagnosticLocation::createBegin(D, SM),
1658 // Did we visit an entire call?
1659 bool VisitedEntireCall = PD.isWithinCall();
1662 PathDiagnosticCallPiece *C;
1663 if (VisitedEntireCall) {
1664 PathDiagnosticPiece *P = PD.getActivePath().front().get();
1665 C = cast<PathDiagnosticCallPiece>(P);
1667 const Decl *Caller = CE->getLocationContext()->getDecl();
1668 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1670 // Since we just transferred the path over to the call piece,
1671 // reset the mapping from active to location context.
1672 assert(PD.getActivePath().size() == 1 &&
1673 PD.getActivePath().front() == C);
1674 LCM[&PD.getActivePath()] = nullptr;
1676 // Record the location context mapping for the path within
1678 assert(LCM[&C->path] == nullptr ||
1679 LCM[&C->path] == CE->getCalleeContext());
1680 LCM[&C->path] = CE->getCalleeContext();
1682 // If this is the first item in the active path, record
1683 // the new mapping from active path to location context.
1684 const LocationContext *&NewLC = LCM[&PD.getActivePath()];
1686 NewLC = N->getLocationContext();
1690 C->setCallee(*CE, SM);
1692 // Update the previous location in the active path.
1693 PrevLoc = C->getLocation();
1695 if (!CallStack.empty()) {
1696 assert(CallStack.back().first == C);
1697 CallStack.pop_back();
1702 // Query the location context here and the previous location
1703 // as processing CallEnter may change the active path.
1704 PDB.LC = N->getLocationContext();
1706 // Record the mapping from the active path to the location
1708 assert(!LCM[&PD.getActivePath()] ||
1709 LCM[&PD.getActivePath()] == PDB.LC);
1710 LCM[&PD.getActivePath()] = PDB.LC;
1712 // Have we encountered an exit from a function call?
1713 if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1714 const Stmt *S = CE->getCalleeContext()->getCallSite();
1715 // Propagate the interesting symbols accordingly.
1716 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
1717 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1718 N->getState().get(), Ex,
1719 N->getLocationContext());
1722 // We are descending into a call (backwards). Construct
1723 // a new call piece to contain the path pieces for that call.
1724 PathDiagnosticCallPiece *C =
1725 PathDiagnosticCallPiece::construct(N, *CE, SM);
1727 // Record the location context for this call piece.
1728 LCM[&C->path] = CE->getCalleeContext();
1730 // Add the edge to the return site.
1731 addEdgeToPath(PD.getActivePath(), PrevLoc, C->callReturn, PDB.LC);
1732 PD.getActivePath().push_front(C);
1733 PrevLoc.invalidate();
1735 // Make the contents of the call the active path for now.
1736 PD.pushActivePath(&C->path);
1737 CallStack.push_back(StackDiagPair(C, N));
1741 if (Optional<PostStmt> PS = P.getAs<PostStmt>()) {
1742 // For expressions, make sure we propagate the
1743 // interesting symbols correctly.
1744 if (const Expr *Ex = PS->getStmtAs<Expr>())
1745 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1746 N->getState().get(), Ex,
1747 N->getLocationContext());
1749 // Add an edge. If this is an ObjCForCollectionStmt do
1750 // not add an edge here as it appears in the CFG both
1751 // as a terminator and as a terminator condition.
1752 if (!isa<ObjCForCollectionStmt>(PS->getStmt())) {
1753 PathDiagnosticLocation L =
1754 PathDiagnosticLocation(PS->getStmt(), SM, PDB.LC);
1755 addEdgeToPath(PD.getActivePath(), PrevLoc, L, PDB.LC);
1761 if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
1762 // Does this represent entering a call? If so, look at propagating
1763 // interesting symbols across call boundaries.
1765 const LocationContext *CallerCtx = NextNode->getLocationContext();
1766 const LocationContext *CalleeCtx = PDB.LC;
1767 if (CallerCtx != CalleeCtx) {
1768 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1769 N->getState().get(),
1770 CalleeCtx, CallerCtx);
1774 // Are we jumping to the head of a loop? Add a special diagnostic.
1775 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1776 PathDiagnosticLocation L(Loop, SM, PDB.LC);
1777 const Stmt *Body = nullptr;
1779 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1780 Body = FS->getBody();
1781 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1782 Body = WS->getBody();
1783 else if (const ObjCForCollectionStmt *OFS =
1784 dyn_cast<ObjCForCollectionStmt>(Loop)) {
1785 Body = OFS->getBody();
1786 } else if (const CXXForRangeStmt *FRS =
1787 dyn_cast<CXXForRangeStmt>(Loop)) {
1788 Body = FRS->getBody();
1790 // do-while statements are explicitly excluded here
1792 PathDiagnosticEventPiece *p =
1793 new PathDiagnosticEventPiece(L, "Looping back to the head "
1795 p->setPrunable(true);
1797 addEdgeToPath(PD.getActivePath(), PrevLoc, p->getLocation(), PDB.LC);
1798 PD.getActivePath().push_front(p);
1800 if (const CompoundStmt *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
1801 addEdgeToPath(PD.getActivePath(), PrevLoc,
1802 PathDiagnosticLocation::createEndBrace(CS, SM),
1807 const CFGBlock *BSrc = BE->getSrc();
1808 ParentMap &PM = PDB.getParentMap();
1810 if (const Stmt *Term = BSrc->getTerminator()) {
1811 // Are we jumping past the loop body without ever executing the
1812 // loop (because the condition was false)?
1814 const Stmt *TermCond = getTerminatorCondition(BSrc);
1816 isInLoopBody(PM, getStmtBeforeCond(PM, TermCond, N), Term);
1818 const char *str = nullptr;
1820 if (isJumpToFalseBranch(&*BE)) {
1821 if (!IsInLoopBody) {
1822 if (isa<ObjCForCollectionStmt>(Term)) {
1823 str = StrLoopCollectionEmpty;
1824 } else if (isa<CXXForRangeStmt>(Term)) {
1825 str = StrLoopRangeEmpty;
1827 str = StrLoopBodyZero;
1831 str = StrEnteringLoop;
1835 PathDiagnosticLocation L(TermCond ? TermCond : Term, SM, PDB.LC);
1836 PathDiagnosticEventPiece *PE =
1837 new PathDiagnosticEventPiece(L, str);
1838 PE->setPrunable(true);
1839 addEdgeToPath(PD.getActivePath(), PrevLoc,
1840 PE->getLocation(), PDB.LC);
1841 PD.getActivePath().push_front(PE);
1843 } else if (isa<BreakStmt>(Term) || isa<ContinueStmt>(Term) ||
1844 isa<GotoStmt>(Term)) {
1845 PathDiagnosticLocation L(Term, SM, PDB.LC);
1846 addEdgeToPath(PD.getActivePath(), PrevLoc, L, PDB.LC);
1856 // Add pieces from custom visitors.
1857 for (auto &V : visitors) {
1858 if (PathDiagnosticPiece *p = V->VisitNode(N, NextNode, PDB, *report)) {
1859 addEdgeToPath(PD.getActivePath(), PrevLoc, p->getLocation(), PDB.LC);
1860 PD.getActivePath().push_front(p);
1861 updateStackPiecesWithMessage(p, CallStack);
1866 // Add an edge to the start of the function.
1867 // We'll prune it out later, but it helps make diagnostics more uniform.
1868 const StackFrameContext *CalleeLC = PDB.LC->getCurrentStackFrame();
1869 const Decl *D = CalleeLC->getDecl();
1870 addEdgeToPath(PD.getActivePath(), PrevLoc,
1871 PathDiagnosticLocation::createBegin(D, SM),
1874 return report->isValid();
1877 static const Stmt *getLocStmt(PathDiagnosticLocation L) {
1883 static const Stmt *getStmtParent(const Stmt *S, const ParentMap &PM) {
1888 S = PM.getParentIgnoreParens(S);
1893 if (isa<ExprWithCleanups>(S) ||
1894 isa<CXXBindTemporaryExpr>(S) ||
1895 isa<SubstNonTypeTemplateParmExpr>(S))
1904 static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) {
1905 switch (S->getStmtClass()) {
1906 case Stmt::BinaryOperatorClass: {
1907 const BinaryOperator *BO = cast<BinaryOperator>(S);
1908 if (!BO->isLogicalOp())
1910 return BO->getLHS() == Cond || BO->getRHS() == Cond;
1912 case Stmt::IfStmtClass:
1913 return cast<IfStmt>(S)->getCond() == Cond;
1914 case Stmt::ForStmtClass:
1915 return cast<ForStmt>(S)->getCond() == Cond;
1916 case Stmt::WhileStmtClass:
1917 return cast<WhileStmt>(S)->getCond() == Cond;
1918 case Stmt::DoStmtClass:
1919 return cast<DoStmt>(S)->getCond() == Cond;
1920 case Stmt::ChooseExprClass:
1921 return cast<ChooseExpr>(S)->getCond() == Cond;
1922 case Stmt::IndirectGotoStmtClass:
1923 return cast<IndirectGotoStmt>(S)->getTarget() == Cond;
1924 case Stmt::SwitchStmtClass:
1925 return cast<SwitchStmt>(S)->getCond() == Cond;
1926 case Stmt::BinaryConditionalOperatorClass:
1927 return cast<BinaryConditionalOperator>(S)->getCond() == Cond;
1928 case Stmt::ConditionalOperatorClass: {
1929 const ConditionalOperator *CO = cast<ConditionalOperator>(S);
1930 return CO->getCond() == Cond ||
1931 CO->getLHS() == Cond ||
1932 CO->getRHS() == Cond;
1934 case Stmt::ObjCForCollectionStmtClass:
1935 return cast<ObjCForCollectionStmt>(S)->getElement() == Cond;
1936 case Stmt::CXXForRangeStmtClass: {
1937 const CXXForRangeStmt *FRS = cast<CXXForRangeStmt>(S);
1938 return FRS->getCond() == Cond || FRS->getRangeInit() == Cond;
1945 static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) {
1946 if (const ForStmt *FS = dyn_cast<ForStmt>(FL))
1947 return FS->getInc() == S || FS->getInit() == S;
1948 if (const CXXForRangeStmt *FRS = dyn_cast<CXXForRangeStmt>(FL))
1949 return FRS->getInc() == S || FRS->getRangeStmt() == S ||
1950 FRS->getLoopVarStmt() || FRS->getRangeInit() == S;
1954 typedef llvm::DenseSet<const PathDiagnosticCallPiece *>
1957 /// Adds synthetic edges from top-level statements to their subexpressions.
1959 /// This avoids a "swoosh" effect, where an edge from a top-level statement A
1960 /// points to a sub-expression B.1 that's not at the start of B. In these cases,
1961 /// we'd like to see an edge from A to B, then another one from B to B.1.
1962 static void addContextEdges(PathPieces &pieces, SourceManager &SM,
1963 const ParentMap &PM, const LocationContext *LCtx) {
1964 PathPieces::iterator Prev = pieces.end();
1965 for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E;
1967 PathDiagnosticControlFlowPiece *Piece =
1968 dyn_cast<PathDiagnosticControlFlowPiece>(*I);
1973 PathDiagnosticLocation SrcLoc = Piece->getStartLocation();
1974 SmallVector<PathDiagnosticLocation, 4> SrcContexts;
1976 PathDiagnosticLocation NextSrcContext = SrcLoc;
1977 const Stmt *InnerStmt = nullptr;
1978 while (NextSrcContext.isValid() && NextSrcContext.asStmt() != InnerStmt) {
1979 SrcContexts.push_back(NextSrcContext);
1980 InnerStmt = NextSrcContext.asStmt();
1981 NextSrcContext = getEnclosingStmtLocation(InnerStmt, SM, PM, LCtx,
1982 /*allowNested=*/true);
1985 // Repeatedly split the edge as necessary.
1986 // This is important for nested logical expressions (||, &&, ?:) where we
1987 // want to show all the levels of context.
1989 const Stmt *Dst = getLocStmt(Piece->getEndLocation());
1991 // We are looking at an edge. Is the destination within a larger
1993 PathDiagnosticLocation DstContext =
1994 getEnclosingStmtLocation(Dst, SM, PM, LCtx, /*allowNested=*/true);
1995 if (!DstContext.isValid() || DstContext.asStmt() == Dst)
1998 // If the source is in the same context, we're already good.
1999 if (std::find(SrcContexts.begin(), SrcContexts.end(), DstContext) !=
2003 // Update the subexpression node to point to the context edge.
2004 Piece->setStartLocation(DstContext);
2006 // Try to extend the previous edge if it's at the same level as the source
2009 PathDiagnosticControlFlowPiece *PrevPiece =
2010 dyn_cast<PathDiagnosticControlFlowPiece>(*Prev);
2013 if (const Stmt *PrevSrc = getLocStmt(PrevPiece->getStartLocation())) {
2014 const Stmt *PrevSrcParent = getStmtParent(PrevSrc, PM);
2015 if (PrevSrcParent == getStmtParent(getLocStmt(DstContext), PM)) {
2016 PrevPiece->setEndLocation(DstContext);
2023 // Otherwise, split the current edge into a context edge and a
2024 // subexpression edge. Note that the context statement may itself have
2026 Piece = new PathDiagnosticControlFlowPiece(SrcLoc, DstContext);
2027 I = pieces.insert(I, Piece);
2032 /// \brief Move edges from a branch condition to a branch target
2033 /// when the condition is simple.
2035 /// This restructures some of the work of addContextEdges. That function
2036 /// creates edges this may destroy, but they work together to create a more
2037 /// aesthetically set of edges around branches. After the call to
2038 /// addContextEdges, we may have (1) an edge to the branch, (2) an edge from
2039 /// the branch to the branch condition, and (3) an edge from the branch
2040 /// condition to the branch target. We keep (1), but may wish to remove (2)
2041 /// and move the source of (3) to the branch if the branch condition is simple.
2043 static void simplifySimpleBranches(PathPieces &pieces) {
2044 for (PathPieces::iterator I = pieces.begin(), E = pieces.end(); I != E; ++I) {
2046 PathDiagnosticControlFlowPiece *PieceI =
2047 dyn_cast<PathDiagnosticControlFlowPiece>(*I);
2052 const Stmt *s1Start = getLocStmt(PieceI->getStartLocation());
2053 const Stmt *s1End = getLocStmt(PieceI->getEndLocation());
2055 if (!s1Start || !s1End)
2058 PathPieces::iterator NextI = I; ++NextI;
2062 PathDiagnosticControlFlowPiece *PieceNextI = nullptr;
2068 PathDiagnosticEventPiece *EV = dyn_cast<PathDiagnosticEventPiece>(*NextI);
2070 StringRef S = EV->getString();
2071 if (S == StrEnteringLoop || S == StrLoopBodyZero ||
2072 S == StrLoopCollectionEmpty || S == StrLoopRangeEmpty) {
2079 PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(*NextI);
2086 const Stmt *s2Start = getLocStmt(PieceNextI->getStartLocation());
2087 const Stmt *s2End = getLocStmt(PieceNextI->getEndLocation());
2089 if (!s2Start || !s2End || s1End != s2Start)
2092 // We only perform this transformation for specific branch kinds.
2093 // We don't want to do this for do..while, for example.
2094 if (!(isa<ForStmt>(s1Start) || isa<WhileStmt>(s1Start) ||
2095 isa<IfStmt>(s1Start) || isa<ObjCForCollectionStmt>(s1Start) ||
2096 isa<CXXForRangeStmt>(s1Start)))
2099 // Is s1End the branch condition?
2100 if (!isConditionForTerminator(s1Start, s1End))
2103 // Perform the hoisting by eliminating (2) and changing the start
2105 PieceNextI->setStartLocation(PieceI->getStartLocation());
2106 I = pieces.erase(I);
2110 /// Returns the number of bytes in the given (character-based) SourceRange.
2112 /// If the locations in the range are not on the same line, returns None.
2114 /// Note that this does not do a precise user-visible character or column count.
2115 static Optional<size_t> getLengthOnSingleLine(SourceManager &SM,
2116 SourceRange Range) {
2117 SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()),
2118 SM.getExpansionRange(Range.getEnd()).second);
2120 FileID FID = SM.getFileID(ExpansionRange.getBegin());
2121 if (FID != SM.getFileID(ExpansionRange.getEnd()))
2125 const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, &Invalid);
2129 unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin());
2130 unsigned EndOffset = SM.getFileOffset(ExpansionRange.getEnd());
2131 StringRef Snippet = Buffer->getBuffer().slice(BeginOffset, EndOffset);
2133 // We're searching the raw bytes of the buffer here, which might include
2134 // escaped newlines and such. That's okay; we're trying to decide whether the
2135 // SourceRange is covering a large or small amount of space in the user's
2137 if (Snippet.find_first_of("\r\n") != StringRef::npos)
2140 // This isn't Unicode-aware, but it doesn't need to be.
2141 return Snippet.size();
2144 /// \sa getLengthOnSingleLine(SourceManager, SourceRange)
2145 static Optional<size_t> getLengthOnSingleLine(SourceManager &SM,
2147 return getLengthOnSingleLine(SM, S->getSourceRange());
2150 /// Eliminate two-edge cycles created by addContextEdges().
2152 /// Once all the context edges are in place, there are plenty of cases where
2153 /// there's a single edge from a top-level statement to a subexpression,
2154 /// followed by a single path note, and then a reverse edge to get back out to
2155 /// the top level. If the statement is simple enough, the subexpression edges
2156 /// just add noise and make it harder to understand what's going on.
2158 /// This function only removes edges in pairs, because removing only one edge
2159 /// might leave other edges dangling.
2161 /// This will not remove edges in more complicated situations:
2162 /// - if there is more than one "hop" leading to or from a subexpression.
2163 /// - if there is an inlined call between the edges instead of a single event.
2164 /// - if the whole statement is large enough that having subexpression arrows
2165 /// might be helpful.
2166 static void removeContextCycles(PathPieces &Path, SourceManager &SM,
2168 for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) {
2169 // Pattern match the current piece and its successor.
2170 PathDiagnosticControlFlowPiece *PieceI =
2171 dyn_cast<PathDiagnosticControlFlowPiece>(*I);
2178 const Stmt *s1Start = getLocStmt(PieceI->getStartLocation());
2179 const Stmt *s1End = getLocStmt(PieceI->getEndLocation());
2181 PathPieces::iterator NextI = I; ++NextI;
2185 PathDiagnosticControlFlowPiece *PieceNextI =
2186 dyn_cast<PathDiagnosticControlFlowPiece>(*NextI);
2189 if (isa<PathDiagnosticEventPiece>(*NextI)) {
2193 PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(*NextI);
2202 const Stmt *s2Start = getLocStmt(PieceNextI->getStartLocation());
2203 const Stmt *s2End = getLocStmt(PieceNextI->getEndLocation());
2205 if (s1Start && s2Start && s1Start == s2End && s2Start == s1End) {
2206 const size_t MAX_SHORT_LINE_LENGTH = 80;
2207 Optional<size_t> s1Length = getLengthOnSingleLine(SM, s1Start);
2208 if (s1Length && *s1Length <= MAX_SHORT_LINE_LENGTH) {
2209 Optional<size_t> s2Length = getLengthOnSingleLine(SM, s2Start);
2210 if (s2Length && *s2Length <= MAX_SHORT_LINE_LENGTH) {
2212 I = Path.erase(NextI);
2222 /// \brief Return true if X is contained by Y.
2223 static bool lexicalContains(ParentMap &PM,
2229 X = PM.getParent(X);
2234 // Remove short edges on the same line less than 3 columns in difference.
2235 static void removePunyEdges(PathPieces &path,
2239 bool erased = false;
2241 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E;
2246 PathDiagnosticControlFlowPiece *PieceI =
2247 dyn_cast<PathDiagnosticControlFlowPiece>(*I);
2252 const Stmt *start = getLocStmt(PieceI->getStartLocation());
2253 const Stmt *end = getLocStmt(PieceI->getEndLocation());
2258 const Stmt *endParent = PM.getParent(end);
2262 if (isConditionForTerminator(end, endParent))
2265 SourceLocation FirstLoc = start->getLocStart();
2266 SourceLocation SecondLoc = end->getLocStart();
2268 if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc))
2270 if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc))
2271 std::swap(SecondLoc, FirstLoc);
2273 SourceRange EdgeRange(FirstLoc, SecondLoc);
2274 Optional<size_t> ByteWidth = getLengthOnSingleLine(SM, EdgeRange);
2276 // If the statements are on different lines, continue.
2280 const size_t MAX_PUNY_EDGE_LENGTH = 2;
2281 if (*ByteWidth <= MAX_PUNY_EDGE_LENGTH) {
2282 // FIXME: There are enough /bytes/ between the endpoints of the edge, but
2283 // there might not be enough /columns/. A proper user-visible column count
2284 // is probably too expensive, though.
2292 static void removeIdenticalEvents(PathPieces &path) {
2293 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I) {
2294 PathDiagnosticEventPiece *PieceI =
2295 dyn_cast<PathDiagnosticEventPiece>(*I);
2300 PathPieces::iterator NextI = I; ++NextI;
2304 PathDiagnosticEventPiece *PieceNextI =
2305 dyn_cast<PathDiagnosticEventPiece>(*NextI);
2310 // Erase the second piece if it has the same exact message text.
2311 if (PieceI->getString() == PieceNextI->getString()) {
2317 static bool optimizeEdges(PathPieces &path, SourceManager &SM,
2318 OptimizedCallsSet &OCS,
2319 LocationContextMap &LCM) {
2320 bool hasChanges = false;
2321 const LocationContext *LC = LCM[&path];
2323 ParentMap &PM = LC->getParentMap();
2325 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) {
2326 // Optimize subpaths.
2327 if (PathDiagnosticCallPiece *CallI = dyn_cast<PathDiagnosticCallPiece>(*I)){
2328 // Record the fact that a call has been optimized so we only do the
2330 if (!OCS.count(CallI)) {
2331 while (optimizeEdges(CallI->path, SM, OCS, LCM)) {}
2338 // Pattern match the current piece and its successor.
2339 PathDiagnosticControlFlowPiece *PieceI =
2340 dyn_cast<PathDiagnosticControlFlowPiece>(*I);
2347 const Stmt *s1Start = getLocStmt(PieceI->getStartLocation());
2348 const Stmt *s1End = getLocStmt(PieceI->getEndLocation());
2349 const Stmt *level1 = getStmtParent(s1Start, PM);
2350 const Stmt *level2 = getStmtParent(s1End, PM);
2352 PathPieces::iterator NextI = I; ++NextI;
2356 PathDiagnosticControlFlowPiece *PieceNextI =
2357 dyn_cast<PathDiagnosticControlFlowPiece>(*NextI);
2364 const Stmt *s2Start = getLocStmt(PieceNextI->getStartLocation());
2365 const Stmt *s2End = getLocStmt(PieceNextI->getEndLocation());
2366 const Stmt *level3 = getStmtParent(s2Start, PM);
2367 const Stmt *level4 = getStmtParent(s2End, PM);
2371 // If we have two consecutive control edges whose end/begin locations
2372 // are at the same level (e.g. statements or top-level expressions within
2373 // a compound statement, or siblings share a single ancestor expression),
2374 // then merge them if they have no interesting intermediate event.
2378 // (1.1 -> 1.2) -> (1.2 -> 1.3) becomes (1.1 -> 1.3) because the common
2379 // parent is '1'. Here 'x.y.z' represents the hierarchy of statements.
2381 // NOTE: this will be limited later in cases where we add barriers
2382 // to prevent this optimization.
2384 if (level1 && level1 == level2 && level1 == level3 && level1 == level4) {
2385 PieceI->setEndLocation(PieceNextI->getEndLocation());
2393 // Eliminate edges between subexpressions and parent expressions
2394 // when the subexpression is consumed.
2396 // NOTE: this will be limited later in cases where we add barriers
2397 // to prevent this optimization.
2399 if (s1End && s1End == s2Start && level2) {
2400 bool removeEdge = false;
2401 // Remove edges into the increment or initialization of a
2402 // loop that have no interleaving event. This means that
2403 // they aren't interesting.
2404 if (isIncrementOrInitInForLoop(s1End, level2))
2406 // Next only consider edges that are not anchored on
2407 // the condition of a terminator. This are intermediate edges
2408 // that we might want to trim.
2409 else if (!isConditionForTerminator(level2, s1End)) {
2410 // Trim edges on expressions that are consumed by
2411 // the parent expression.
2412 if (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End))) {
2415 // Trim edges where a lexical containment doesn't exist.
2420 // If 'Z' lexically contains Y (it is an ancestor) and
2421 // 'X' does not lexically contain Y (it is a descendant OR
2422 // it has no lexical relationship at all) then trim.
2424 // This can eliminate edges where we dive into a subexpression
2425 // and then pop back out, etc.
2426 else if (s1Start && s2End &&
2427 lexicalContains(PM, s2Start, s2End) &&
2428 !lexicalContains(PM, s1End, s1Start)) {
2431 // Trim edges from a subexpression back to the top level if the
2432 // subexpression is on a different line.
2438 // These edges just look ugly and don't usually add anything.
2439 else if (s1Start && s2End &&
2440 lexicalContains(PM, s1Start, s1End)) {
2441 SourceRange EdgeRange(PieceI->getEndLocation().asLocation(),
2442 PieceI->getStartLocation().asLocation());
2443 if (!getLengthOnSingleLine(SM, EdgeRange).hasValue())
2449 PieceI->setEndLocation(PieceNextI->getEndLocation());
2456 // Optimize edges for ObjC fast-enumeration loops.
2458 // (X -> collection) -> (collection -> element)
2463 if (s1End == s2Start) {
2464 const ObjCForCollectionStmt *FS =
2465 dyn_cast_or_null<ObjCForCollectionStmt>(level3);
2466 if (FS && FS->getCollection()->IgnoreParens() == s2Start &&
2467 s2End == FS->getElement()) {
2468 PieceI->setEndLocation(PieceNextI->getEndLocation());
2475 // No changes at this index? Move to the next one.
2480 // Adjust edges into subexpressions to make them more uniform
2481 // and aesthetically pleasing.
2482 addContextEdges(path, SM, PM, LC);
2483 // Remove "cyclical" edges that include one or more context edges.
2484 removeContextCycles(path, SM, PM);
2485 // Hoist edges originating from branch conditions to branches
2486 // for simple branches.
2487 simplifySimpleBranches(path);
2488 // Remove any puny edges left over after primary optimization pass.
2489 removePunyEdges(path, SM, PM);
2490 // Remove identical events.
2491 removeIdenticalEvents(path);
2497 /// Drop the very first edge in a path, which should be a function entry edge.
2499 /// If the first edge is not a function entry edge (say, because the first
2500 /// statement had an invalid source location), this function does nothing.
2501 // FIXME: We should just generate invalid edges anyway and have the optimizer
2503 static void dropFunctionEntryEdge(PathPieces &Path,
2504 LocationContextMap &LCM,
2505 SourceManager &SM) {
2506 const PathDiagnosticControlFlowPiece *FirstEdge =
2507 dyn_cast<PathDiagnosticControlFlowPiece>(Path.front());
2511 const Decl *D = LCM[&Path]->getDecl();
2512 PathDiagnosticLocation EntryLoc = PathDiagnosticLocation::createBegin(D, SM);
2513 if (FirstEdge->getStartLocation() != EntryLoc)
2520 //===----------------------------------------------------------------------===//
2521 // Methods for BugType and subclasses.
2522 //===----------------------------------------------------------------------===//
2523 void BugType::anchor() { }
2525 void BugType::FlushReports(BugReporter &BR) {}
2527 void BuiltinBug::anchor() {}
2529 //===----------------------------------------------------------------------===//
2530 // Methods for BugReport and subclasses.
2531 //===----------------------------------------------------------------------===//
2533 void BugReport::NodeResolver::anchor() {}
2535 void BugReport::addVisitor(std::unique_ptr<BugReporterVisitor> visitor) {
2539 llvm::FoldingSetNodeID ID;
2540 visitor->Profile(ID);
2543 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos))
2546 CallbacksSet.InsertNode(visitor.get(), InsertPos);
2547 Callbacks.push_back(std::move(visitor));
2548 ++ConfigurationChangeToken;
2551 BugReport::~BugReport() {
2552 while (!interestingSymbols.empty()) {
2553 popInterestingSymbolsAndRegions();
2557 const Decl *BugReport::getDeclWithIssue() const {
2559 return DeclWithIssue;
2561 const ExplodedNode *N = getErrorNode();
2565 const LocationContext *LC = N->getLocationContext();
2566 return LC->getCurrentStackFrame()->getDecl();
2569 void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
2570 hash.AddPointer(&BT);
2571 hash.AddString(Description);
2572 PathDiagnosticLocation UL = getUniqueingLocation();
2575 } else if (Location.isValid()) {
2576 Location.Profile(hash);
2579 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
2582 for (SmallVectorImpl<SourceRange>::const_iterator I =
2583 Ranges.begin(), E = Ranges.end(); I != E; ++I) {
2584 const SourceRange range = *I;
2585 if (!range.isValid())
2587 hash.AddInteger(range.getBegin().getRawEncoding());
2588 hash.AddInteger(range.getEnd().getRawEncoding());
2592 void BugReport::markInteresting(SymbolRef sym) {
2596 // If the symbol wasn't already in our set, note a configuration change.
2597 if (getInterestingSymbols().insert(sym).second)
2598 ++ConfigurationChangeToken;
2600 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
2601 getInterestingRegions().insert(meta->getRegion());
2604 void BugReport::markInteresting(const MemRegion *R) {
2608 // If the base region wasn't already in our set, note a configuration change.
2609 R = R->getBaseRegion();
2610 if (getInterestingRegions().insert(R).second)
2611 ++ConfigurationChangeToken;
2613 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
2614 getInterestingSymbols().insert(SR->getSymbol());
2617 void BugReport::markInteresting(SVal V) {
2618 markInteresting(V.getAsRegion());
2619 markInteresting(V.getAsSymbol());
2622 void BugReport::markInteresting(const LocationContext *LC) {
2625 InterestingLocationContexts.insert(LC);
2628 bool BugReport::isInteresting(SVal V) {
2629 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
2632 bool BugReport::isInteresting(SymbolRef sym) {
2635 // We don't currently consider metadata symbols to be interesting
2636 // even if we know their region is interesting. Is that correct behavior?
2637 return getInterestingSymbols().count(sym);
2640 bool BugReport::isInteresting(const MemRegion *R) {
2643 R = R->getBaseRegion();
2644 bool b = getInterestingRegions().count(R);
2647 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
2648 return getInterestingSymbols().count(SR->getSymbol());
2652 bool BugReport::isInteresting(const LocationContext *LC) {
2655 return InterestingLocationContexts.count(LC);
2658 void BugReport::lazyInitializeInterestingSets() {
2659 if (interestingSymbols.empty()) {
2660 interestingSymbols.push_back(new Symbols());
2661 interestingRegions.push_back(new Regions());
2665 BugReport::Symbols &BugReport::getInterestingSymbols() {
2666 lazyInitializeInterestingSets();
2667 return *interestingSymbols.back();
2670 BugReport::Regions &BugReport::getInterestingRegions() {
2671 lazyInitializeInterestingSets();
2672 return *interestingRegions.back();
2675 void BugReport::pushInterestingSymbolsAndRegions() {
2676 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
2677 interestingRegions.push_back(new Regions(getInterestingRegions()));
2680 void BugReport::popInterestingSymbolsAndRegions() {
2681 delete interestingSymbols.pop_back_val();
2682 delete interestingRegions.pop_back_val();
2685 const Stmt *BugReport::getStmt() const {
2689 ProgramPoint ProgP = ErrorNode->getLocation();
2690 const Stmt *S = nullptr;
2692 if (Optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) {
2693 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
2694 if (BE->getBlock() == &Exit)
2695 S = GetPreviousStmt(ErrorNode);
2698 S = PathDiagnosticLocation::getStmt(ErrorNode);
2703 llvm::iterator_range<BugReport::ranges_iterator> BugReport::getRanges() {
2704 // If no custom ranges, add the range of the statement corresponding to
2706 if (Ranges.empty()) {
2707 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
2708 addRange(E->getSourceRange());
2710 return llvm::make_range(ranges_iterator(), ranges_iterator());
2713 // User-specified absence of range info.
2714 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
2715 return llvm::make_range(ranges_iterator(), ranges_iterator());
2717 return llvm::iterator_range<BugReport::ranges_iterator>(Ranges.begin(),
2721 PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
2723 assert(!Location.isValid() &&
2724 "Either Location or ErrorNode should be specified but not both.");
2725 return PathDiagnosticLocation::createEndOfPath(ErrorNode, SM);
2728 assert(Location.isValid());
2732 //===----------------------------------------------------------------------===//
2733 // Methods for BugReporter and subclasses.
2734 //===----------------------------------------------------------------------===//
2736 BugReportEquivClass::~BugReportEquivClass() { }
2737 GRBugReporter::~GRBugReporter() { }
2738 BugReporterData::~BugReporterData() {}
2740 ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
2742 ProgramStateManager&
2743 GRBugReporter::getStateManager() { return Eng.getStateManager(); }
2745 BugReporter::~BugReporter() {
2748 // Free the bug reports we are tracking.
2749 typedef std::vector<BugReportEquivClass *> ContTy;
2750 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
2756 void BugReporter::FlushReports() {
2757 if (BugTypes.isEmpty())
2760 // First flush the warnings for each BugType. This may end up creating new
2761 // warnings and new BugTypes.
2762 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
2763 // Turn NSErrorChecker into a proper checker and remove this.
2764 SmallVector<const BugType *, 16> bugTypes(BugTypes.begin(), BugTypes.end());
2765 for (SmallVectorImpl<const BugType *>::iterator
2766 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
2767 const_cast<BugType*>(*I)->FlushReports(*this);
2769 // We need to flush reports in deterministic order to ensure the order
2770 // of the reports is consistent between runs.
2771 typedef std::vector<BugReportEquivClass *> ContVecTy;
2772 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
2774 BugReportEquivClass& EQ = **EI;
2778 // BugReporter owns and deletes only BugTypes created implicitly through
2780 // FIXME: There are leaks from checkers that assume that the BugTypes they
2781 // create will be destroyed by the BugReporter.
2782 llvm::DeleteContainerSeconds(StrBugTypes);
2784 // Remove all references to the BugType objects.
2785 BugTypes = F.getEmptySet();
2788 //===----------------------------------------------------------------------===//
2789 // PathDiagnostics generation.
2790 //===----------------------------------------------------------------------===//
2793 /// A wrapper around a report graph, which contains only a single path, and its
2797 InterExplodedGraphMap BackMap;
2798 std::unique_ptr<ExplodedGraph> Graph;
2799 const ExplodedNode *ErrorNode;
2803 /// A wrapper around a trimmed graph and its node maps.
2804 class TrimmedGraph {
2805 InterExplodedGraphMap InverseMap;
2807 typedef llvm::DenseMap<const ExplodedNode *, unsigned> PriorityMapTy;
2808 PriorityMapTy PriorityMap;
2810 typedef std::pair<const ExplodedNode *, size_t> NodeIndexPair;
2811 SmallVector<NodeIndexPair, 32> ReportNodes;
2813 std::unique_ptr<ExplodedGraph> G;
2815 /// A helper class for sorting ExplodedNodes by priority.
2816 template <bool Descending>
2817 class PriorityCompare {
2818 const PriorityMapTy &PriorityMap;
2821 PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {}
2823 bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const {
2824 PriorityMapTy::const_iterator LI = PriorityMap.find(LHS);
2825 PriorityMapTy::const_iterator RI = PriorityMap.find(RHS);
2826 PriorityMapTy::const_iterator E = PriorityMap.end();
2833 return Descending ? LI->second > RI->second
2834 : LI->second < RI->second;
2837 bool operator()(const NodeIndexPair &LHS, const NodeIndexPair &RHS) const {
2838 return (*this)(LHS.first, RHS.first);
2843 TrimmedGraph(const ExplodedGraph *OriginalGraph,
2844 ArrayRef<const ExplodedNode *> Nodes);
2846 bool popNextReportGraph(ReportGraph &GraphWrapper);
2850 TrimmedGraph::TrimmedGraph(const ExplodedGraph *OriginalGraph,
2851 ArrayRef<const ExplodedNode *> Nodes) {
2852 // The trimmed graph is created in the body of the constructor to ensure
2853 // that the DenseMaps have been initialized already.
2854 InterExplodedGraphMap ForwardMap;
2855 G = OriginalGraph->trim(Nodes, &ForwardMap, &InverseMap);
2857 // Find the (first) error node in the trimmed graph. We just need to consult
2858 // the node map which maps from nodes in the original graph to nodes
2859 // in the new graph.
2860 llvm::SmallPtrSet<const ExplodedNode *, 32> RemainingNodes;
2862 for (unsigned i = 0, count = Nodes.size(); i < count; ++i) {
2863 if (const ExplodedNode *NewNode = ForwardMap.lookup(Nodes[i])) {
2864 ReportNodes.push_back(std::make_pair(NewNode, i));
2865 RemainingNodes.insert(NewNode);
2869 assert(!RemainingNodes.empty() && "No error node found in the trimmed graph");
2871 // Perform a forward BFS to find all the shortest paths.
2872 std::queue<const ExplodedNode *> WS;
2874 assert(G->num_roots() == 1);
2875 WS.push(*G->roots_begin());
2876 unsigned Priority = 0;
2878 while (!WS.empty()) {
2879 const ExplodedNode *Node = WS.front();
2882 PriorityMapTy::iterator PriorityEntry;
2884 std::tie(PriorityEntry, IsNew) =
2885 PriorityMap.insert(std::make_pair(Node, Priority));
2889 assert(PriorityEntry->second <= Priority);
2893 if (RemainingNodes.erase(Node))
2894 if (RemainingNodes.empty())
2897 for (ExplodedNode::const_pred_iterator I = Node->succ_begin(),
2898 E = Node->succ_end();
2903 // Sort the error paths from longest to shortest.
2904 std::sort(ReportNodes.begin(), ReportNodes.end(),
2905 PriorityCompare<true>(PriorityMap));
2908 bool TrimmedGraph::popNextReportGraph(ReportGraph &GraphWrapper) {
2909 if (ReportNodes.empty())
2912 const ExplodedNode *OrigN;
2913 std::tie(OrigN, GraphWrapper.Index) = ReportNodes.pop_back_val();
2914 assert(PriorityMap.find(OrigN) != PriorityMap.end() &&
2915 "error node not accessible from root");
2917 // Create a new graph with a single path. This is the graph
2918 // that will be returned to the caller.
2919 auto GNew = llvm::make_unique<ExplodedGraph>();
2920 GraphWrapper.BackMap.clear();
2922 // Now walk from the error node up the BFS path, always taking the
2923 // predeccessor with the lowest number.
2924 ExplodedNode *Succ = nullptr;
2926 // Create the equivalent node in the new graph with the same state
2928 ExplodedNode *NewN = GNew->getNode(OrigN->getLocation(), OrigN->getState(),
2931 // Store the mapping to the original node.
2932 InterExplodedGraphMap::const_iterator IMitr = InverseMap.find(OrigN);
2933 assert(IMitr != InverseMap.end() && "No mapping to original node.");
2934 GraphWrapper.BackMap[NewN] = IMitr->second;
2936 // Link up the new node with the previous node.
2938 Succ->addPredecessor(NewN, *GNew);
2940 GraphWrapper.ErrorNode = NewN;
2944 // Are we at the final node?
2945 if (OrigN->pred_empty()) {
2946 GNew->addRoot(NewN);
2950 // Find the next predeccessor node. We choose the node that is marked
2951 // with the lowest BFS number.
2952 OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(),
2953 PriorityCompare<false>(PriorityMap));
2956 GraphWrapper.Graph = std::move(GNew);
2962 /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
2963 /// and collapses PathDiagosticPieces that are expanded by macros.
2964 static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
2965 typedef std::vector<std::pair<IntrusiveRefCntPtr<PathDiagnosticMacroPiece>,
2966 SourceLocation> > MacroStackTy;
2968 typedef std::vector<IntrusiveRefCntPtr<PathDiagnosticPiece> >
2971 MacroStackTy MacroStack;
2974 for (PathPieces::const_iterator I = path.begin(), E = path.end();
2977 PathDiagnosticPiece *piece = I->get();
2979 // Recursively compact calls.
2980 if (PathDiagnosticCallPiece *call=dyn_cast<PathDiagnosticCallPiece>(piece)){
2981 CompactPathDiagnostic(call->path, SM);
2984 // Get the location of the PathDiagnosticPiece.
2985 const FullSourceLoc Loc = piece->getLocation().asLocation();
2987 // Determine the instantiation location, which is the location we group
2988 // related PathDiagnosticPieces.
2989 SourceLocation InstantiationLoc = Loc.isMacroID() ?
2990 SM.getExpansionLoc(Loc) :
2993 if (Loc.isFileID()) {
2995 Pieces.push_back(piece);
2999 assert(Loc.isMacroID());
3001 // Is the PathDiagnosticPiece within the same macro group?
3002 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
3003 MacroStack.back().first->subPieces.push_back(piece);
3007 // We aren't in the same group. Are we descending into a new macro
3008 // or are part of an old one?
3009 IntrusiveRefCntPtr<PathDiagnosticMacroPiece> MacroGroup;
3011 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
3012 SM.getExpansionLoc(Loc) :
3015 // Walk the entire macro stack.
3016 while (!MacroStack.empty()) {
3017 if (InstantiationLoc == MacroStack.back().second) {
3018 MacroGroup = MacroStack.back().first;
3022 if (ParentInstantiationLoc == MacroStack.back().second) {
3023 MacroGroup = MacroStack.back().first;
3027 MacroStack.pop_back();
3030 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
3031 // Create a new macro group and add it to the stack.
3032 PathDiagnosticMacroPiece *NewGroup =
3033 new PathDiagnosticMacroPiece(
3034 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
3037 MacroGroup->subPieces.push_back(NewGroup);
3039 assert(InstantiationLoc.isFileID());
3040 Pieces.push_back(NewGroup);
3043 MacroGroup = NewGroup;
3044 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
3047 // Finally, add the PathDiagnosticPiece to the group.
3048 MacroGroup->subPieces.push_back(piece);
3051 // Now take the pieces and construct a new PathDiagnostic.
3054 path.insert(path.end(), Pieces.begin(), Pieces.end());
3057 bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
3058 PathDiagnosticConsumer &PC,
3059 ArrayRef<BugReport *> &bugReports) {
3060 assert(!bugReports.empty());
3062 bool HasValid = false;
3063 bool HasInvalid = false;
3064 SmallVector<const ExplodedNode *, 32> errorNodes;
3065 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
3066 E = bugReports.end(); I != E; ++I) {
3067 if ((*I)->isValid()) {
3069 errorNodes.push_back((*I)->getErrorNode());
3071 // Keep the errorNodes list in sync with the bugReports list.
3073 errorNodes.push_back(nullptr);
3077 // If all the reports have been marked invalid by a previous path generation,
3082 typedef PathDiagnosticConsumer::PathGenerationScheme PathGenerationScheme;
3083 PathGenerationScheme ActiveScheme = PC.getGenerationScheme();
3085 if (ActiveScheme == PathDiagnosticConsumer::Extensive) {
3086 AnalyzerOptions &options = getAnalyzerOptions();
3087 if (options.getBooleanOption("path-diagnostics-alternate", true)) {
3088 ActiveScheme = PathDiagnosticConsumer::AlternateExtensive;
3092 TrimmedGraph TrimG(&getGraph(), errorNodes);
3093 ReportGraph ErrorGraph;
3095 while (TrimG.popNextReportGraph(ErrorGraph)) {
3096 // Find the BugReport with the original location.
3097 assert(ErrorGraph.Index < bugReports.size());
3098 BugReport *R = bugReports[ErrorGraph.Index];
3099 assert(R && "No original report found for sliced graph.");
3100 assert(R->isValid() && "Report selected by trimmed graph marked invalid.");
3102 // Start building the path diagnostic...
3103 PathDiagnosticBuilder PDB(*this, R, ErrorGraph.BackMap, &PC);
3104 const ExplodedNode *N = ErrorGraph.ErrorNode;
3106 // Register additional node visitors.
3107 R->addVisitor(llvm::make_unique<NilReceiverBRVisitor>());
3108 R->addVisitor(llvm::make_unique<ConditionBRVisitor>());
3109 R->addVisitor(llvm::make_unique<LikelyFalsePositiveSuppressionBRVisitor>());
3111 BugReport::VisitorList visitors;
3112 unsigned origReportConfigToken, finalReportConfigToken;
3113 LocationContextMap LCM;
3115 // While generating diagnostics, it's possible the visitors will decide
3116 // new symbols and regions are interesting, or add other visitors based on
3117 // the information they find. If they do, we need to regenerate the path
3118 // based on our new report configuration.
3120 // Get a clean copy of all the visitors.
3121 for (BugReport::visitor_iterator I = R->visitor_begin(),
3122 E = R->visitor_end(); I != E; ++I)
3123 visitors.push_back((*I)->clone());
3125 // Clear out the active path from any previous work.
3127 origReportConfigToken = R->getConfigurationChangeToken();
3129 // Generate the very last diagnostic piece - the piece is visible before
3130 // the trace is expanded.
3131 std::unique_ptr<PathDiagnosticPiece> LastPiece;
3132 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
3134 if (std::unique_ptr<PathDiagnosticPiece> Piece =
3135 (*I)->getEndPath(PDB, N, *R)) {
3136 assert (!LastPiece &&
3137 "There can only be one final piece in a diagnostic.");
3138 LastPiece = std::move(Piece);
3142 if (ActiveScheme != PathDiagnosticConsumer::None) {
3144 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
3146 PD.setEndOfPath(std::move(LastPiece));
3149 // Make sure we get a clean location context map so we don't
3150 // hold onto old mappings.
3153 switch (ActiveScheme) {
3154 case PathDiagnosticConsumer::AlternateExtensive:
3155 GenerateAlternateExtensivePathDiagnostic(PD, PDB, N, LCM, visitors);
3157 case PathDiagnosticConsumer::Extensive:
3158 GenerateExtensivePathDiagnostic(PD, PDB, N, LCM, visitors);
3160 case PathDiagnosticConsumer::Minimal:
3161 GenerateMinimalPathDiagnostic(PD, PDB, N, LCM, visitors);
3163 case PathDiagnosticConsumer::None:
3164 GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors);
3168 // Clean up the visitors we used.
3171 // Did anything change while generating this path?
3172 finalReportConfigToken = R->getConfigurationChangeToken();
3173 } while (finalReportConfigToken != origReportConfigToken);
3178 // Finally, prune the diagnostic path of uninteresting stuff.
3179 if (!PD.path.empty()) {
3180 if (R->shouldPrunePath() && getAnalyzerOptions().shouldPrunePaths()) {
3181 bool stillHasNotes = removeUnneededCalls(PD.getMutablePieces(), R, LCM);
3182 assert(stillHasNotes);
3183 (void)stillHasNotes;
3186 // Redirect all call pieces to have valid locations.
3187 adjustCallLocations(PD.getMutablePieces());
3188 removePiecesWithInvalidLocations(PD.getMutablePieces());
3190 if (ActiveScheme == PathDiagnosticConsumer::AlternateExtensive) {
3191 SourceManager &SM = getSourceManager();
3193 // Reduce the number of edges from a very conservative set
3194 // to an aesthetically pleasing subset that conveys the
3195 // necessary information.
3196 OptimizedCallsSet OCS;
3197 while (optimizeEdges(PD.getMutablePieces(), SM, OCS, LCM)) {}
3199 // Drop the very first function-entry edge. It's not really necessary
3200 // for top-level functions.
3201 dropFunctionEntryEdge(PD.getMutablePieces(), LCM, SM);
3204 // Remove messages that are basically the same, and edges that may not
3206 // We have to do this after edge optimization in the Extensive mode.
3207 removeRedundantMsgs(PD.getMutablePieces());
3208 removeEdgesToDefaultInitializers(PD.getMutablePieces());
3211 // We found a report and didn't suppress it.
3215 // We suppressed all the reports in this equivalence class.
3216 assert(!HasInvalid && "Inconsistent suppression");
3221 void BugReporter::Register(BugType *BT) {
3222 BugTypes = F.add(BugTypes, BT);
3225 void BugReporter::emitReport(std::unique_ptr<BugReport> R) {
3226 if (const ExplodedNode *E = R->getErrorNode()) {
3227 const AnalysisDeclContext *DeclCtx =
3228 E->getLocationContext()->getAnalysisDeclContext();
3229 // The source of autosynthesized body can be handcrafted AST or a model
3230 // file. The locations from handcrafted ASTs have no valid source locations
3231 // and have to be discarded. Locations from model files should be preserved
3232 // for processing and reporting.
3233 if (DeclCtx->isBodyAutosynthesized() &&
3234 !DeclCtx->isBodyAutosynthesizedFromModelFile())
3238 bool ValidSourceLoc = R->getLocation(getSourceManager()).isValid();
3239 assert(ValidSourceLoc);
3240 // If we mess up in a release build, we'd still prefer to just drop the bug
3241 // instead of trying to go on.
3242 if (!ValidSourceLoc)
3245 // Compute the bug report's hash to determine its equivalence class.
3246 llvm::FoldingSetNodeID ID;
3249 // Lookup the equivance class. If there isn't one, create it.
3250 BugType& BT = R->getBugType();
3253 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
3256 EQ = new BugReportEquivClass(std::move(R));
3257 EQClasses.InsertNode(EQ, InsertPos);
3258 EQClassesVector.push_back(EQ);
3260 EQ->AddReport(std::move(R));
3264 //===----------------------------------------------------------------------===//
3265 // Emitting reports in equivalence classes.
3266 //===----------------------------------------------------------------------===//
3269 struct FRIEC_WLItem {
3270 const ExplodedNode *N;
3271 ExplodedNode::const_succ_iterator I, E;
3273 FRIEC_WLItem(const ExplodedNode *n)
3274 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
3279 FindReportInEquivalenceClass(BugReportEquivClass& EQ,
3280 SmallVectorImpl<BugReport*> &bugReports) {
3282 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
3284 BugType& BT = I->getBugType();
3286 // If we don't need to suppress any of the nodes because they are
3287 // post-dominated by a sink, simply add all the nodes in the equivalence class
3288 // to 'Nodes'. Any of the reports will serve as a "representative" report.
3289 if (!BT.isSuppressOnSink()) {
3291 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
3292 const ExplodedNode *N = I->getErrorNode();
3295 bugReports.push_back(R);
3301 // For bug reports that should be suppressed when all paths are post-dominated
3302 // by a sink node, iterate through the reports in the equivalence class
3303 // until we find one that isn't post-dominated (if one exists). We use a
3304 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
3305 // this as a recursive function, but we don't want to risk blowing out the
3306 // stack for very long paths.
3307 BugReport *exampleReport = nullptr;
3309 for (; I != E; ++I) {
3310 const ExplodedNode *errorNode = I->getErrorNode();
3314 if (errorNode->isSink()) {
3316 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
3318 // No successors? By definition this nodes isn't post-dominated by a sink.
3319 if (errorNode->succ_empty()) {
3320 bugReports.push_back(I);
3326 // At this point we know that 'N' is not a sink and it has at least one
3327 // successor. Use a DFS worklist to find a non-sink end-of-path node.
3328 typedef FRIEC_WLItem WLItem;
3329 typedef SmallVector<WLItem, 10> DFSWorkList;
3330 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
3333 WL.push_back(errorNode);
3334 Visited[errorNode] = 1;
3336 while (!WL.empty()) {
3337 WLItem &WI = WL.back();
3338 assert(!WI.N->succ_empty());
3340 for (; WI.I != WI.E; ++WI.I) {
3341 const ExplodedNode *Succ = *WI.I;
3342 // End-of-path node?
3343 if (Succ->succ_empty()) {
3344 // If we found an end-of-path node that is not a sink.
3345 if (!Succ->isSink()) {
3346 bugReports.push_back(I);
3352 // Found a sink? Continue on to the next successor.
3355 // Mark the successor as visited. If it hasn't been explored,
3356 // enqueue it to the DFS worklist.
3357 unsigned &mark = Visited[Succ];
3365 // The worklist may have been cleared at this point. First
3366 // check if it is empty before checking the last item.
3367 if (!WL.empty() && &WL.back() == &WI)
3372 // ExampleReport will be NULL if all the nodes in the equivalence class
3373 // were post-dominated by sinks.
3374 return exampleReport;
3377 void BugReporter::FlushReport(BugReportEquivClass& EQ) {
3378 SmallVector<BugReport*, 10> bugReports;
3379 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
3380 if (exampleReport) {
3381 for (PathDiagnosticConsumer *PDC : getPathDiagnosticConsumers()) {
3382 FlushReport(exampleReport, *PDC, bugReports);
3387 void BugReporter::FlushReport(BugReport *exampleReport,
3388 PathDiagnosticConsumer &PD,
3389 ArrayRef<BugReport*> bugReports) {
3391 // FIXME: Make sure we use the 'R' for the path that was actually used.
3392 // Probably doesn't make a difference in practice.
3393 BugType& BT = exampleReport->getBugType();
3395 std::unique_ptr<PathDiagnostic> D(new PathDiagnostic(
3396 exampleReport->getBugType().getCheckName(),
3397 exampleReport->getDeclWithIssue(), exampleReport->getBugType().getName(),
3398 exampleReport->getDescription(),
3399 exampleReport->getShortDescription(/*Fallback=*/false), BT.getCategory(),
3400 exampleReport->getUniqueingLocation(),
3401 exampleReport->getUniqueingDecl()));
3403 MaxBugClassSize = std::max(bugReports.size(),
3404 static_cast<size_t>(MaxBugClassSize));
3406 // Generate the full path diagnostic, using the generation scheme
3407 // specified by the PathDiagnosticConsumer. Note that we have to generate
3408 // path diagnostics even for consumers which do not support paths, because
3409 // the BugReporterVisitors may mark this bug as a false positive.
3410 if (!bugReports.empty())
3411 if (!generatePathDiagnostic(*D.get(), PD, bugReports))
3414 MaxValidBugClassSize = std::max(bugReports.size(),
3415 static_cast<size_t>(MaxValidBugClassSize));
3417 // Examine the report and see if the last piece is in a header. Reset the
3418 // report location to the last piece in the main source file.
3419 AnalyzerOptions& Opts = getAnalyzerOptions();
3420 if (Opts.shouldReportIssuesInMainSourceFile() && !Opts.AnalyzeAll)
3421 D->resetDiagnosticLocationToMainFile();
3423 // If the path is empty, generate a single step path with the location
3425 if (D->path.empty()) {
3426 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
3427 auto piece = llvm::make_unique<PathDiagnosticEventPiece>(
3428 L, exampleReport->getDescription());
3429 for (const SourceRange &Range : exampleReport->getRanges())
3430 piece->addRange(Range);
3431 D->setEndOfPath(std::move(piece));
3434 // Get the meta data.
3435 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
3436 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
3437 e = Meta.end(); i != e; ++i) {
3441 PD.HandlePathDiagnostic(std::move(D));
3444 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3445 const CheckerBase *Checker,
3446 StringRef Name, StringRef Category,
3447 StringRef Str, PathDiagnosticLocation Loc,
3448 ArrayRef<SourceRange> Ranges) {
3449 EmitBasicReport(DeclWithIssue, Checker->getCheckName(), Name, Category, Str,
3452 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3453 CheckName CheckName,
3454 StringRef name, StringRef category,
3455 StringRef str, PathDiagnosticLocation Loc,
3456 ArrayRef<SourceRange> Ranges) {
3458 // 'BT' is owned by BugReporter.
3459 BugType *BT = getBugTypeForName(CheckName, name, category);
3460 auto R = llvm::make_unique<BugReport>(*BT, str, Loc);
3461 R->setDeclWithIssue(DeclWithIssue);
3462 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
3465 emitReport(std::move(R));
3468 BugType *BugReporter::getBugTypeForName(CheckName CheckName, StringRef name,
3469 StringRef category) {
3470 SmallString<136> fullDesc;
3471 llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name
3473 BugType *&BT = StrBugTypes[fullDesc];
3475 BT = new BugType(CheckName, name, category);
3479 LLVM_DUMP_METHOD void PathPieces::dump() const {
3481 for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) {
3482 llvm::errs() << "[" << index++ << "] ";
3484 llvm::errs() << "\n";
3488 void PathDiagnosticCallPiece::dump() const {
3489 llvm::errs() << "CALL\n--------------\n";
3491 if (const Stmt *SLoc = getLocStmt(getLocation()))
3493 else if (const NamedDecl *ND = dyn_cast<NamedDecl>(getCallee()))
3494 llvm::errs() << *ND << "\n";
3496 getLocation().dump();
3499 void PathDiagnosticEventPiece::dump() const {
3500 llvm::errs() << "EVENT\n--------------\n";
3501 llvm::errs() << getString() << "\n";
3502 llvm::errs() << " ---- at ----\n";
3503 getLocation().dump();
3506 void PathDiagnosticControlFlowPiece::dump() const {
3507 llvm::errs() << "CONTROL\n--------------\n";
3508 getStartLocation().dump();
3509 llvm::errs() << " ---- to ----\n";
3510 getEndLocation().dump();
3513 void PathDiagnosticMacroPiece::dump() const {
3514 llvm::errs() << "MACRO\n--------------\n";
3515 // FIXME: Print which macro is being invoked.
3518 void PathDiagnosticLocation::dump() const {
3520 llvm::errs() << "<INVALID>\n";
3526 // FIXME: actually print the range.
3527 llvm::errs() << "<range>\n";
3530 asLocation().dump();
3531 llvm::errs() << "\n";
3537 llvm::errs() << "<NULL STMT>\n";
3540 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
3541 llvm::errs() << *ND << "\n";
3542 else if (isa<BlockDecl>(D))
3543 // FIXME: Make this nicer.
3544 llvm::errs() << "<block>\n";
3546 llvm::errs() << "<unknown decl>\n";
3548 llvm::errs() << "<NULL DECL>\n";