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/CFGStmtMap.h"
25 #include "clang/Analysis/ProgramPoint.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
28 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
29 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/IntrusiveRefCntPtr.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Support/raw_ostream.h"
39 using namespace clang;
42 #define DEBUG_TYPE "BugReporter"
44 STATISTIC(MaxBugClassSize,
45 "The maximum number of bug reports in the same equivalence class");
46 STATISTIC(MaxValidBugClassSize,
47 "The maximum number of bug reports in the same equivalence class "
48 "where at least one report is valid (not suppressed)");
50 BugReporterVisitor::~BugReporterVisitor() {}
52 void BugReporterContext::anchor() {}
54 //===----------------------------------------------------------------------===//
55 // Helper routines for walking the ExplodedGraph and fetching statements.
56 //===----------------------------------------------------------------------===//
58 static const Stmt *GetPreviousStmt(const ExplodedNode *N) {
59 for (N = N->getFirstPred(); N; N = N->getFirstPred())
60 if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
66 static inline const Stmt*
67 GetCurrentOrPreviousStmt(const ExplodedNode *N) {
68 if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
71 return GetPreviousStmt(N);
74 //===----------------------------------------------------------------------===//
75 // Diagnostic cleanup.
76 //===----------------------------------------------------------------------===//
78 static PathDiagnosticEventPiece *
79 eventsDescribeSameCondition(PathDiagnosticEventPiece *X,
80 PathDiagnosticEventPiece *Y) {
81 // Prefer diagnostics that come from ConditionBRVisitor over
82 // those that came from TrackConstraintBRVisitor,
83 // unless the one from ConditionBRVisitor is
84 // its generic fallback diagnostic.
85 const void *tagPreferred = ConditionBRVisitor::getTag();
86 const void *tagLesser = TrackConstraintBRVisitor::getTag();
88 if (X->getLocation() != Y->getLocation())
91 if (X->getTag() == tagPreferred && Y->getTag() == tagLesser)
92 return ConditionBRVisitor::isPieceMessageGeneric(X) ? Y : X;
94 if (Y->getTag() == tagPreferred && X->getTag() == tagLesser)
95 return ConditionBRVisitor::isPieceMessageGeneric(Y) ? X : Y;
100 /// An optimization pass over PathPieces that removes redundant diagnostics
101 /// generated by both ConditionBRVisitor and TrackConstraintBRVisitor. Both
102 /// BugReporterVisitors use different methods to generate diagnostics, with
103 /// one capable of emitting diagnostics in some cases but not in others. This
104 /// can lead to redundant diagnostic pieces at the same point in a path.
105 static void removeRedundantMsgs(PathPieces &path) {
106 unsigned N = path.size();
109 // NOTE: this loop intentionally is not using an iterator. Instead, we
110 // are streaming the path and modifying it in place. This is done by
111 // grabbing the front, processing it, and if we decide to keep it append
112 // it to the end of the path. The entire path is processed in this way.
113 for (unsigned i = 0; i < N; ++i) {
114 auto piece = std::move(path.front());
117 switch (piece->getKind()) {
118 case PathDiagnosticPiece::Call:
119 removeRedundantMsgs(cast<PathDiagnosticCallPiece>(*piece).path);
121 case PathDiagnosticPiece::Macro:
122 removeRedundantMsgs(cast<PathDiagnosticMacroPiece>(*piece).subPieces);
124 case PathDiagnosticPiece::ControlFlow:
126 case PathDiagnosticPiece::Event: {
130 if (PathDiagnosticEventPiece *nextEvent =
131 dyn_cast<PathDiagnosticEventPiece>(path.front().get())) {
132 PathDiagnosticEventPiece *event =
133 cast<PathDiagnosticEventPiece>(piece.get());
134 // Check to see if we should keep one of the two pieces. If we
135 // come up with a preference, record which piece to keep, and consume
136 // another piece from the path.
137 if (auto *pieceToKeep =
138 eventsDescribeSameCondition(event, nextEvent)) {
139 piece = std::move(pieceToKeep == event ? piece : path.front());
146 case PathDiagnosticPiece::Note:
149 path.push_back(std::move(piece));
153 /// A map from PathDiagnosticPiece to the LocationContext of the inlined
154 /// function call it represents.
155 typedef llvm::DenseMap<const PathPieces *, const LocationContext *>
158 /// Recursively scan through a path and prune out calls and macros pieces
159 /// that aren't needed. Return true if afterwards the path contains
160 /// "interesting stuff" which means it shouldn't be pruned from the parent path.
161 static bool removeUnneededCalls(PathPieces &pieces, BugReport *R,
162 LocationContextMap &LCM) {
163 bool containsSomethingInteresting = false;
164 const unsigned N = pieces.size();
166 for (unsigned i = 0 ; i < N ; ++i) {
167 // Remove the front piece from the path. If it is still something we
168 // want to keep once we are done, we will push it back on the end.
169 auto piece = std::move(pieces.front());
172 switch (piece->getKind()) {
173 case PathDiagnosticPiece::Call: {
174 auto &call = cast<PathDiagnosticCallPiece>(*piece);
175 // Check if the location context is interesting.
176 assert(LCM.count(&call.path));
177 if (R->isInteresting(LCM[&call.path])) {
178 containsSomethingInteresting = true;
182 if (!removeUnneededCalls(call.path, R, LCM))
185 containsSomethingInteresting = true;
188 case PathDiagnosticPiece::Macro: {
189 auto ¯o = cast<PathDiagnosticMacroPiece>(*piece);
190 if (!removeUnneededCalls(macro.subPieces, R, LCM))
192 containsSomethingInteresting = true;
195 case PathDiagnosticPiece::Event: {
196 auto &event = cast<PathDiagnosticEventPiece>(*piece);
198 // We never throw away an event, but we do throw it away wholesale
199 // as part of a path if we throw the entire path away.
200 containsSomethingInteresting |= !event.isPrunable();
203 case PathDiagnosticPiece::ControlFlow:
206 case PathDiagnosticPiece::Note:
210 pieces.push_back(std::move(piece));
213 return containsSomethingInteresting;
216 /// Returns true if the given decl has been implicitly given a body, either by
217 /// the analyzer or by the compiler proper.
218 static bool hasImplicitBody(const Decl *D) {
220 return D->isImplicit() || !D->hasBody();
223 /// Recursively scan through a path and make sure that all call pieces have
226 adjustCallLocations(PathPieces &Pieces,
227 PathDiagnosticLocation *LastCallLocation = nullptr) {
228 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E; ++I) {
229 PathDiagnosticCallPiece *Call = dyn_cast<PathDiagnosticCallPiece>(I->get());
235 if (LastCallLocation) {
236 bool CallerIsImplicit = hasImplicitBody(Call->getCaller());
237 if (CallerIsImplicit || !Call->callEnter.asLocation().isValid())
238 Call->callEnter = *LastCallLocation;
239 if (CallerIsImplicit || !Call->callReturn.asLocation().isValid())
240 Call->callReturn = *LastCallLocation;
243 // Recursively clean out the subclass. Keep this call around if
244 // it contains any informative diagnostics.
245 PathDiagnosticLocation *ThisCallLocation;
246 if (Call->callEnterWithin.asLocation().isValid() &&
247 !hasImplicitBody(Call->getCallee()))
248 ThisCallLocation = &Call->callEnterWithin;
250 ThisCallLocation = &Call->callEnter;
252 assert(ThisCallLocation && "Outermost call has an invalid location");
253 adjustCallLocations(Call->path, ThisCallLocation);
257 /// Remove edges in and out of C++ default initializer expressions. These are
258 /// for fields that have in-class initializers, as opposed to being initialized
259 /// explicitly in a constructor or braced list.
260 static void removeEdgesToDefaultInitializers(PathPieces &Pieces) {
261 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
262 if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
263 removeEdgesToDefaultInitializers(C->path);
265 if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
266 removeEdgesToDefaultInitializers(M->subPieces);
268 if (auto *CF = dyn_cast<PathDiagnosticControlFlowPiece>(I->get())) {
269 const Stmt *Start = CF->getStartLocation().asStmt();
270 const Stmt *End = CF->getEndLocation().asStmt();
271 if (Start && isa<CXXDefaultInitExpr>(Start)) {
274 } else if (End && isa<CXXDefaultInitExpr>(End)) {
275 PathPieces::iterator Next = std::next(I);
278 dyn_cast<PathDiagnosticControlFlowPiece>(Next->get())) {
279 NextCF->setStartLocation(CF->getStartLocation());
291 /// Remove all pieces with invalid locations as these cannot be serialized.
292 /// We might have pieces with invalid locations as a result of inlining Body
293 /// Farm generated functions.
294 static void removePiecesWithInvalidLocations(PathPieces &Pieces) {
295 for (PathPieces::iterator I = Pieces.begin(), E = Pieces.end(); I != E;) {
296 if (auto *C = dyn_cast<PathDiagnosticCallPiece>(I->get()))
297 removePiecesWithInvalidLocations(C->path);
299 if (auto *M = dyn_cast<PathDiagnosticMacroPiece>(I->get()))
300 removePiecesWithInvalidLocations(M->subPieces);
302 if (!(*I)->getLocation().isValid() ||
303 !(*I)->getLocation().asLocation().isValid()) {
311 //===----------------------------------------------------------------------===//
312 // PathDiagnosticBuilder and its associated routines and helper objects.
313 //===----------------------------------------------------------------------===//
316 class NodeMapClosure : public BugReport::NodeResolver {
317 InterExplodedGraphMap &M;
319 NodeMapClosure(InterExplodedGraphMap &m) : M(m) {}
321 const ExplodedNode *getOriginalNode(const ExplodedNode *N) override {
326 class PathDiagnosticBuilder : public BugReporterContext {
328 PathDiagnosticConsumer *PDC;
331 const LocationContext *LC;
333 PathDiagnosticBuilder(GRBugReporter &br,
334 BugReport *r, InterExplodedGraphMap &Backmap,
335 PathDiagnosticConsumer *pdc)
336 : BugReporterContext(br),
337 R(r), PDC(pdc), NMC(Backmap), LC(r->getErrorNode()->getLocationContext())
340 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N);
342 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os,
343 const ExplodedNode *N);
345 BugReport *getBugReport() { return R; }
347 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); }
349 ParentMap& getParentMap() { return LC->getParentMap(); }
351 const Stmt *getParent(const Stmt *S) {
352 return getParentMap().getParent(S);
355 NodeMapClosure& getNodeResolver() override { return NMC; }
357 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S);
359 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const {
360 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive;
363 bool supportsLogicalOpControlFlow() const {
364 return PDC ? PDC->supportsLogicalOpControlFlow() : true;
367 } // end anonymous namespace
369 PathDiagnosticLocation
370 PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) {
371 if (const Stmt *S = PathDiagnosticLocation::getNextStmt(N))
372 return PathDiagnosticLocation(S, getSourceManager(), LC);
374 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(),
378 PathDiagnosticLocation
379 PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os,
380 const ExplodedNode *N) {
382 // Slow, but probably doesn't matter.
383 if (os.str().empty())
386 const PathDiagnosticLocation &Loc = ExecutionContinues(N);
389 os << "Execution continues on line "
390 << getSourceManager().getExpansionLineNumber(Loc.asLocation())
393 os << "Execution jumps to the end of the ";
394 const Decl *D = N->getLocationContext()->getDecl();
395 if (isa<ObjCMethodDecl>(D))
397 else if (isa<FunctionDecl>(D))
400 assert(isa<BlockDecl>(D));
401 os << "anonymous block";
409 static const Stmt *getEnclosingParent(const Stmt *S, const ParentMap &PM) {
410 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S)))
411 return PM.getParentIgnoreParens(S);
413 const Stmt *Parent = PM.getParentIgnoreParens(S);
417 switch (Parent->getStmtClass()) {
418 case Stmt::ForStmtClass:
419 case Stmt::DoStmtClass:
420 case Stmt::WhileStmtClass:
421 case Stmt::ObjCForCollectionStmtClass:
422 case Stmt::CXXForRangeStmtClass:
431 static PathDiagnosticLocation
432 getEnclosingStmtLocation(const Stmt *S, SourceManager &SMgr, const ParentMap &P,
433 const LocationContext *LC, bool allowNestedContexts) {
435 return PathDiagnosticLocation();
437 while (const Stmt *Parent = getEnclosingParent(S, P)) {
438 switch (Parent->getStmtClass()) {
439 case Stmt::BinaryOperatorClass: {
440 const BinaryOperator *B = cast<BinaryOperator>(Parent);
441 if (B->isLogicalOp())
442 return PathDiagnosticLocation(allowNestedContexts ? B : S, SMgr, LC);
445 case Stmt::CompoundStmtClass:
446 case Stmt::StmtExprClass:
447 return PathDiagnosticLocation(S, SMgr, LC);
448 case Stmt::ChooseExprClass:
449 // Similar to '?' if we are referring to condition, just have the edge
450 // point to the entire choose expression.
451 if (allowNestedContexts || cast<ChooseExpr>(Parent)->getCond() == S)
452 return PathDiagnosticLocation(Parent, SMgr, LC);
454 return PathDiagnosticLocation(S, SMgr, LC);
455 case Stmt::BinaryConditionalOperatorClass:
456 case Stmt::ConditionalOperatorClass:
457 // For '?', if we are referring to condition, just have the edge point
458 // to the entire '?' expression.
459 if (allowNestedContexts ||
460 cast<AbstractConditionalOperator>(Parent)->getCond() == S)
461 return PathDiagnosticLocation(Parent, SMgr, LC);
463 return PathDiagnosticLocation(S, SMgr, LC);
464 case Stmt::CXXForRangeStmtClass:
465 if (cast<CXXForRangeStmt>(Parent)->getBody() == S)
466 return PathDiagnosticLocation(S, SMgr, LC);
468 case Stmt::DoStmtClass:
469 return PathDiagnosticLocation(S, SMgr, LC);
470 case Stmt::ForStmtClass:
471 if (cast<ForStmt>(Parent)->getBody() == S)
472 return PathDiagnosticLocation(S, SMgr, LC);
474 case Stmt::IfStmtClass:
475 if (cast<IfStmt>(Parent)->getCond() != S)
476 return PathDiagnosticLocation(S, SMgr, LC);
478 case Stmt::ObjCForCollectionStmtClass:
479 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S)
480 return PathDiagnosticLocation(S, SMgr, LC);
482 case Stmt::WhileStmtClass:
483 if (cast<WhileStmt>(Parent)->getCond() != S)
484 return PathDiagnosticLocation(S, SMgr, LC);
493 assert(S && "Cannot have null Stmt for PathDiagnosticLocation");
495 return PathDiagnosticLocation(S, SMgr, LC);
498 PathDiagnosticLocation
499 PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) {
500 assert(S && "Null Stmt passed to getEnclosingStmtLocation");
501 return ::getEnclosingStmtLocation(S, getSourceManager(), getParentMap(), LC,
502 /*allowNestedContexts=*/false);
505 //===----------------------------------------------------------------------===//
506 // "Visitors only" path diagnostic generation algorithm.
507 //===----------------------------------------------------------------------===//
508 static bool GenerateVisitorsOnlyPathDiagnostic(
509 PathDiagnostic &PD, PathDiagnosticBuilder &PDB, const ExplodedNode *N,
510 ArrayRef<std::unique_ptr<BugReporterVisitor>> visitors) {
511 // All path generation skips the very first node (the error node).
512 // This is because there is special handling for the end-of-path note.
513 N = N->getFirstPred();
517 BugReport *R = PDB.getBugReport();
518 while (const ExplodedNode *Pred = N->getFirstPred()) {
519 for (auto &V : visitors)
520 // Visit all the node pairs, but throw the path pieces away.
521 V->VisitNode(N, Pred, PDB, *R);
529 //===----------------------------------------------------------------------===//
530 // "Minimal" path diagnostic generation algorithm.
531 //===----------------------------------------------------------------------===//
532 typedef std::pair<PathDiagnosticCallPiece*, const ExplodedNode*> StackDiagPair;
533 typedef SmallVector<StackDiagPair, 6> StackDiagVector;
535 static void updateStackPiecesWithMessage(PathDiagnosticPiece &P,
536 StackDiagVector &CallStack) {
537 // If the piece contains a special message, add it to all the call
538 // pieces on the active stack.
539 if (PathDiagnosticEventPiece *ep = dyn_cast<PathDiagnosticEventPiece>(&P)) {
541 if (ep->hasCallStackHint())
542 for (StackDiagVector::iterator I = CallStack.begin(),
543 E = CallStack.end(); I != E; ++I) {
544 PathDiagnosticCallPiece *CP = I->first;
545 const ExplodedNode *N = I->second;
546 std::string stackMsg = ep->getCallStackMessage(N);
548 // The last message on the path to final bug is the most important
549 // one. Since we traverse the path backwards, do not add the message
550 // if one has been previously added.
551 if (!CP->hasCallStackMessage())
552 CP->setCallStackMessage(stackMsg);
557 static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM);
559 static bool GenerateMinimalPathDiagnostic(
560 PathDiagnostic &PD, PathDiagnosticBuilder &PDB, const ExplodedNode *N,
561 LocationContextMap &LCM,
562 ArrayRef<std::unique_ptr<BugReporterVisitor>> visitors) {
564 SourceManager& SMgr = PDB.getSourceManager();
565 const LocationContext *LC = PDB.LC;
566 const ExplodedNode *NextNode = N->pred_empty()
567 ? nullptr : *(N->pred_begin());
569 StackDiagVector CallStack;
573 PDB.LC = N->getLocationContext();
574 NextNode = N->getFirstPred();
576 ProgramPoint P = N->getLocation();
579 if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
580 auto C = PathDiagnosticCallPiece::construct(N, *CE, SMgr);
581 // Record the mapping from call piece to LocationContext.
582 LCM[&C->path] = CE->getCalleeContext();
584 PD.getActivePath().push_front(std::move(C));
585 PD.pushActivePath(&P->path);
586 CallStack.push_back(StackDiagPair(P, N));
590 if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
591 // Flush all locations, and pop the active path.
592 bool VisitedEntireCall = PD.isWithinCall();
595 // Either we just added a bunch of stuff to the top-level path, or
596 // we have a previous CallExitEnd. If the former, it means that the
597 // path terminated within a function call. We must then take the
598 // current contents of the active path and place it within
599 // a new PathDiagnosticCallPiece.
600 PathDiagnosticCallPiece *C;
601 if (VisitedEntireCall) {
602 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front().get());
604 const Decl *Caller = CE->getLocationContext()->getDecl();
605 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
606 // Record the mapping from call piece to LocationContext.
607 LCM[&C->path] = CE->getCalleeContext();
610 C->setCallee(*CE, SMgr);
611 if (!CallStack.empty()) {
612 assert(CallStack.back().first == C);
613 CallStack.pop_back();
618 if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
619 const CFGBlock *Src = BE->getSrc();
620 const CFGBlock *Dst = BE->getDst();
621 const Stmt *T = Src->getTerminator();
626 PathDiagnosticLocation Start =
627 PathDiagnosticLocation::createBegin(T, SMgr,
628 N->getLocationContext());
630 switch (T->getStmtClass()) {
634 case Stmt::GotoStmtClass:
635 case Stmt::IndirectGotoStmtClass: {
636 const Stmt *S = PathDiagnosticLocation::getNextStmt(N);
642 llvm::raw_string_ostream os(sbuf);
643 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S);
645 os << "Control jumps to line "
646 << End.asLocation().getExpansionLineNumber();
647 PD.getActivePath().push_front(
648 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
653 case Stmt::SwitchStmtClass: {
654 // Figure out what case arm we took.
656 llvm::raw_string_ostream os(sbuf);
658 if (const Stmt *S = Dst->getLabel()) {
659 PathDiagnosticLocation End(S, SMgr, LC);
661 switch (S->getStmtClass()) {
663 os << "No cases match in the switch statement. "
664 "Control jumps to line "
665 << End.asLocation().getExpansionLineNumber();
667 case Stmt::DefaultStmtClass:
668 os << "Control jumps to the 'default' case at line "
669 << End.asLocation().getExpansionLineNumber();
672 case Stmt::CaseStmtClass: {
673 os << "Control jumps to 'case ";
674 const CaseStmt *Case = cast<CaseStmt>(S);
675 const Expr *LHS = Case->getLHS()->IgnoreParenCasts();
677 // Determine if it is an enum.
678 bool GetRawInt = true;
680 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) {
681 // FIXME: Maybe this should be an assertion. Are there cases
682 // were it is not an EnumConstantDecl?
683 const EnumConstantDecl *D =
684 dyn_cast<EnumConstantDecl>(DR->getDecl());
693 os << LHS->EvaluateKnownConstInt(PDB.getASTContext());
696 << End.asLocation().getExpansionLineNumber();
700 PD.getActivePath().push_front(
701 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
705 os << "'Default' branch taken. ";
706 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N);
707 PD.getActivePath().push_front(
708 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
715 case Stmt::BreakStmtClass:
716 case Stmt::ContinueStmtClass: {
718 llvm::raw_string_ostream os(sbuf);
719 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
720 PD.getActivePath().push_front(
721 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
726 // Determine control-flow for ternary '?'.
727 case Stmt::BinaryConditionalOperatorClass:
728 case Stmt::ConditionalOperatorClass: {
730 llvm::raw_string_ostream os(sbuf);
731 os << "'?' condition is ";
733 if (*(Src->succ_begin()+1) == Dst)
738 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
740 if (const Stmt *S = End.asStmt())
741 End = PDB.getEnclosingStmtLocation(S);
743 PD.getActivePath().push_front(
744 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
749 // Determine control-flow for short-circuited '&&' and '||'.
750 case Stmt::BinaryOperatorClass: {
751 if (!PDB.supportsLogicalOpControlFlow())
754 const BinaryOperator *B = cast<BinaryOperator>(T);
756 llvm::raw_string_ostream os(sbuf);
757 os << "Left side of '";
759 if (B->getOpcode() == BO_LAnd) {
760 os << "&&" << "' is ";
762 if (*(Src->succ_begin()+1) == Dst) {
764 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
765 PathDiagnosticLocation Start =
766 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
767 PD.getActivePath().push_front(
768 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
773 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
774 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
775 PD.getActivePath().push_front(
776 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
781 assert(B->getOpcode() == BO_LOr);
782 os << "||" << "' is ";
784 if (*(Src->succ_begin()+1) == Dst) {
786 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC);
787 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
788 PD.getActivePath().push_front(
789 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
794 PathDiagnosticLocation End(B->getLHS(), SMgr, LC);
795 PathDiagnosticLocation Start =
796 PathDiagnosticLocation::createOperatorLoc(B, SMgr);
797 PD.getActivePath().push_front(
798 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
806 case Stmt::DoStmtClass: {
807 if (*(Src->succ_begin()) == Dst) {
809 llvm::raw_string_ostream os(sbuf);
811 os << "Loop condition is true. ";
812 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
814 if (const Stmt *S = End.asStmt())
815 End = PDB.getEnclosingStmtLocation(S);
817 PD.getActivePath().push_front(
818 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
822 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
824 if (const Stmt *S = End.asStmt())
825 End = PDB.getEnclosingStmtLocation(S);
827 PD.getActivePath().push_front(
828 std::make_shared<PathDiagnosticControlFlowPiece>(
829 Start, End, "Loop condition is false. Exiting loop"));
835 case Stmt::WhileStmtClass:
836 case Stmt::ForStmtClass: {
837 if (*(Src->succ_begin()+1) == Dst) {
839 llvm::raw_string_ostream os(sbuf);
841 os << "Loop condition is false. ";
842 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N);
843 if (const Stmt *S = End.asStmt())
844 End = PDB.getEnclosingStmtLocation(S);
846 PD.getActivePath().push_front(
847 std::make_shared<PathDiagnosticControlFlowPiece>(Start, End,
851 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
852 if (const Stmt *S = End.asStmt())
853 End = PDB.getEnclosingStmtLocation(S);
855 PD.getActivePath().push_front(
856 std::make_shared<PathDiagnosticControlFlowPiece>(
857 Start, End, "Loop condition is true. Entering loop body"));
863 case Stmt::IfStmtClass: {
864 PathDiagnosticLocation End = PDB.ExecutionContinues(N);
866 if (const Stmt *S = End.asStmt())
867 End = PDB.getEnclosingStmtLocation(S);
869 if (*(Src->succ_begin()+1) == Dst)
870 PD.getActivePath().push_front(
871 std::make_shared<PathDiagnosticControlFlowPiece>(
872 Start, End, "Taking false branch"));
874 PD.getActivePath().push_front(
875 std::make_shared<PathDiagnosticControlFlowPiece>(
876 Start, End, "Taking true branch"));
885 // Add diagnostic pieces from custom visitors.
886 BugReport *R = PDB.getBugReport();
887 llvm::FoldingSet<PathDiagnosticPiece> DeduplicationSet;
888 for (auto &V : visitors) {
889 if (auto p = V->VisitNode(N, NextNode, PDB, *R)) {
890 if (DeduplicationSet.GetOrInsertNode(p.get()) != p.get())
893 updateStackPiecesWithMessage(*p, CallStack);
894 PD.getActivePath().push_front(std::move(p));
900 if (!PDB.getBugReport()->isValid())
903 // After constructing the full PathDiagnostic, do a pass over it to compact
904 // PathDiagnosticPieces that occur within a macro.
905 CompactPathDiagnostic(PD.getMutablePieces(), PDB.getSourceManager());
909 //===----------------------------------------------------------------------===//
910 // "Extensive" PathDiagnostic generation.
911 //===----------------------------------------------------------------------===//
913 static bool IsControlFlowExpr(const Stmt *S) {
914 const Expr *E = dyn_cast<Expr>(S);
919 E = E->IgnoreParenCasts();
921 if (isa<AbstractConditionalOperator>(E))
924 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E))
925 if (B->isLogicalOp())
932 class ContextLocation : public PathDiagnosticLocation {
935 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false)
936 : PathDiagnosticLocation(L), IsDead(isdead) {}
938 void markDead() { IsDead = true; }
939 bool isDead() const { return IsDead; }
942 static PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L,
943 const LocationContext *LC,
944 bool firstCharOnly = false) {
945 if (const Stmt *S = L.asStmt()) {
946 const Stmt *Original = S;
948 // Adjust the location for some expressions that are best referenced
949 // by one of their subexpressions.
950 switch (S->getStmtClass()) {
953 case Stmt::ParenExprClass:
954 case Stmt::GenericSelectionExprClass:
955 S = cast<Expr>(S)->IgnoreParens();
956 firstCharOnly = true;
958 case Stmt::BinaryConditionalOperatorClass:
959 case Stmt::ConditionalOperatorClass:
960 S = cast<AbstractConditionalOperator>(S)->getCond();
961 firstCharOnly = true;
963 case Stmt::ChooseExprClass:
964 S = cast<ChooseExpr>(S)->getCond();
965 firstCharOnly = true;
967 case Stmt::BinaryOperatorClass:
968 S = cast<BinaryOperator>(S)->getLHS();
969 firstCharOnly = true;
977 L = PathDiagnosticLocation(S, L.getManager(), LC);
981 L = PathDiagnosticLocation::createSingleLocation(L);
987 std::vector<ContextLocation> CLocs;
988 typedef std::vector<ContextLocation>::iterator iterator;
990 PathDiagnosticBuilder &PDB;
991 PathDiagnosticLocation PrevLoc;
993 bool IsConsumedExpr(const PathDiagnosticLocation &L);
995 bool containsLocation(const PathDiagnosticLocation &Container,
996 const PathDiagnosticLocation &Containee);
998 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L);
1002 void popLocation() {
1003 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) {
1004 // For contexts, we only one the first character as the range.
1005 rawAddEdge(cleanUpLocation(CLocs.back(), PDB.LC, true));
1011 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb)
1012 : PD(pd), PDB(pdb) {
1014 // If the PathDiagnostic already has pieces, add the enclosing statement
1015 // of the first piece as a context as well.
1016 if (!PD.path.empty()) {
1017 PrevLoc = (*PD.path.begin())->getLocation();
1019 if (const Stmt *S = PrevLoc.asStmt())
1020 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1025 while (!CLocs.empty()) popLocation();
1027 // Finally, add an initial edge from the start location of the first
1028 // statement (if it doesn't already exist).
1029 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin(
1031 PDB.getSourceManager());
1036 void flushLocations() {
1037 while (!CLocs.empty())
1039 PrevLoc = PathDiagnosticLocation();
1042 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false,
1043 bool IsPostJump = false);
1045 void rawAddEdge(PathDiagnosticLocation NewLoc);
1047 void addContext(const Stmt *S);
1048 void addContext(const PathDiagnosticLocation &L);
1049 void addExtendedContext(const Stmt *S);
1051 } // end anonymous namespace
1054 PathDiagnosticLocation
1055 EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) {
1056 if (const Stmt *S = L.asStmt()) {
1057 if (IsControlFlowExpr(S))
1060 return PDB.getEnclosingStmtLocation(S);
1066 bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container,
1067 const PathDiagnosticLocation &Containee) {
1069 if (Container == Containee)
1072 if (Container.asDecl())
1075 if (const Stmt *S = Containee.asStmt())
1076 if (const Stmt *ContainerS = Container.asStmt()) {
1078 if (S == ContainerS)
1080 S = PDB.getParent(S);
1085 // Less accurate: compare using source ranges.
1086 SourceRange ContainerR = Container.asRange();
1087 SourceRange ContaineeR = Containee.asRange();
1089 SourceManager &SM = PDB.getSourceManager();
1090 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin());
1091 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd());
1092 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin());
1093 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd());
1095 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg);
1096 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd);
1097 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg);
1098 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd);
1100 assert(ContainerBegLine <= ContainerEndLine);
1101 assert(ContaineeBegLine <= ContaineeEndLine);
1103 return (ContainerBegLine <= ContaineeBegLine &&
1104 ContainerEndLine >= ContaineeEndLine &&
1105 (ContainerBegLine != ContaineeBegLine ||
1106 SM.getExpansionColumnNumber(ContainerRBeg) <=
1107 SM.getExpansionColumnNumber(ContaineeRBeg)) &&
1108 (ContainerEndLine != ContaineeEndLine ||
1109 SM.getExpansionColumnNumber(ContainerREnd) >=
1110 SM.getExpansionColumnNumber(ContaineeREnd)));
1113 void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) {
1114 if (!PrevLoc.isValid()) {
1119 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc, PDB.LC);
1120 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc, PDB.LC);
1122 if (PrevLocClean.asLocation().isInvalid()) {
1127 if (NewLocClean.asLocation() == PrevLocClean.asLocation())
1130 // FIXME: Ignore intra-macro edges for now.
1131 if (NewLocClean.asLocation().getExpansionLoc() ==
1132 PrevLocClean.asLocation().getExpansionLoc())
1135 PD.getActivePath().push_front(
1136 std::make_shared<PathDiagnosticControlFlowPiece>(NewLocClean,
1141 void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd,
1144 if (!alwaysAdd && NewLoc.asLocation().isMacroID())
1147 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc);
1149 while (!CLocs.empty()) {
1150 ContextLocation &TopContextLoc = CLocs.back();
1152 // Is the top location context the same as the one for the new location?
1153 if (TopContextLoc == CLoc) {
1155 if (IsConsumedExpr(TopContextLoc))
1156 TopContextLoc.markDead();
1162 TopContextLoc.markDead();
1166 if (containsLocation(TopContextLoc, CLoc)) {
1170 if (IsConsumedExpr(CLoc)) {
1171 CLocs.push_back(ContextLocation(CLoc, /*IsDead=*/true));
1176 CLocs.push_back(ContextLocation(CLoc, /*IsDead=*/IsPostJump));
1180 // Context does not contain the location. Flush it.
1184 // If we reach here, there is no enclosing context. Just add the edge.
1188 bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) {
1189 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt()))
1190 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X);
1195 void EdgeBuilder::addExtendedContext(const Stmt *S) {
1199 const Stmt *Parent = PDB.getParent(S);
1201 if (isa<CompoundStmt>(Parent))
1202 Parent = PDB.getParent(Parent);
1208 switch (Parent->getStmtClass()) {
1209 case Stmt::DoStmtClass:
1210 case Stmt::ObjCAtSynchronizedStmtClass:
1220 void EdgeBuilder::addContext(const Stmt *S) {
1224 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.LC);
1228 void EdgeBuilder::addContext(const PathDiagnosticLocation &L) {
1229 while (!CLocs.empty()) {
1230 const PathDiagnosticLocation &TopContextLoc = CLocs.back();
1232 // Is the top location context the same as the one for the new location?
1233 if (TopContextLoc == L)
1236 if (containsLocation(TopContextLoc, L)) {
1241 // Context does not contain the location. Flush it.
1248 // Cone-of-influence: support the reverse propagation of "interesting" symbols
1249 // and values by tracing interesting calculations backwards through evaluated
1250 // expressions along a path. This is probably overly complicated, but the idea
1251 // is that if an expression computed an "interesting" value, the child
1252 // expressions are are also likely to be "interesting" as well (which then
1253 // propagates to the values they in turn compute). This reverse propagation
1254 // is needed to track interesting correlations across function call boundaries,
1255 // where formal arguments bind to actual arguments, etc. This is also needed
1256 // because the constraint solver sometimes simplifies certain symbolic values
1257 // into constants when appropriate, and this complicates reasoning about
1258 // interesting values.
1259 typedef llvm::DenseSet<const Expr *> InterestingExprs;
1261 static void reversePropagateIntererstingSymbols(BugReport &R,
1262 InterestingExprs &IE,
1263 const ProgramState *State,
1265 const LocationContext *LCtx) {
1266 SVal V = State->getSVal(Ex, LCtx);
1267 if (!(R.isInteresting(V) || IE.count(Ex)))
1270 switch (Ex->getStmtClass()) {
1272 if (!isa<CastExpr>(Ex))
1275 case Stmt::BinaryOperatorClass:
1276 case Stmt::UnaryOperatorClass: {
1277 for (const Stmt *SubStmt : Ex->children()) {
1278 if (const Expr *child = dyn_cast_or_null<Expr>(SubStmt)) {
1280 SVal ChildV = State->getSVal(child, LCtx);
1281 R.markInteresting(ChildV);
1288 R.markInteresting(V);
1291 static void reversePropagateInterestingSymbols(BugReport &R,
1292 InterestingExprs &IE,
1293 const ProgramState *State,
1294 const LocationContext *CalleeCtx,
1295 const LocationContext *CallerCtx)
1297 // FIXME: Handle non-CallExpr-based CallEvents.
1298 const StackFrameContext *Callee = CalleeCtx->getCurrentStackFrame();
1299 const Stmt *CallSite = Callee->getCallSite();
1300 if (const CallExpr *CE = dyn_cast_or_null<CallExpr>(CallSite)) {
1301 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeCtx->getDecl())) {
1302 FunctionDecl::param_const_iterator PI = FD->param_begin(),
1303 PE = FD->param_end();
1304 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1305 for (; AI != AE && PI != PE; ++AI, ++PI) {
1306 if (const Expr *ArgE = *AI) {
1307 if (const ParmVarDecl *PD = *PI) {
1308 Loc LV = State->getLValue(PD, CalleeCtx);
1309 if (R.isInteresting(LV) || R.isInteresting(State->getRawSVal(LV)))
1318 //===----------------------------------------------------------------------===//
1319 // Functions for determining if a loop was executed 0 times.
1320 //===----------------------------------------------------------------------===//
1322 static bool isLoop(const Stmt *Term) {
1323 switch (Term->getStmtClass()) {
1324 case Stmt::ForStmtClass:
1325 case Stmt::WhileStmtClass:
1326 case Stmt::ObjCForCollectionStmtClass:
1327 case Stmt::CXXForRangeStmtClass:
1330 // Note that we intentionally do not include do..while here.
1335 static bool isJumpToFalseBranch(const BlockEdge *BE) {
1336 const CFGBlock *Src = BE->getSrc();
1337 assert(Src->succ_size() == 2);
1338 return (*(Src->succ_begin()+1) == BE->getDst());
1341 /// Return true if the terminator is a loop and the destination is the
1343 static bool isLoopJumpPastBody(const Stmt *Term, const BlockEdge *BE) {
1347 // Did we take the false branch?
1348 return isJumpToFalseBranch(BE);
1351 static bool isContainedByStmt(ParentMap &PM, const Stmt *S, const Stmt *SubS) {
1355 SubS = PM.getParent(SubS);
1360 static const Stmt *getStmtBeforeCond(ParentMap &PM, const Stmt *Term,
1361 const ExplodedNode *N) {
1363 Optional<StmtPoint> SP = N->getLocation().getAs<StmtPoint>();
1365 const Stmt *S = SP->getStmt();
1366 if (!isContainedByStmt(PM, Term, S))
1369 N = N->getFirstPred();
1374 static bool isInLoopBody(ParentMap &PM, const Stmt *S, const Stmt *Term) {
1375 const Stmt *LoopBody = nullptr;
1376 switch (Term->getStmtClass()) {
1377 case Stmt::CXXForRangeStmtClass: {
1378 const CXXForRangeStmt *FR = cast<CXXForRangeStmt>(Term);
1379 if (isContainedByStmt(PM, FR->getInc(), S))
1381 if (isContainedByStmt(PM, FR->getLoopVarStmt(), S))
1383 LoopBody = FR->getBody();
1386 case Stmt::ForStmtClass: {
1387 const ForStmt *FS = cast<ForStmt>(Term);
1388 if (isContainedByStmt(PM, FS->getInc(), S))
1390 LoopBody = FS->getBody();
1393 case Stmt::ObjCForCollectionStmtClass: {
1394 const ObjCForCollectionStmt *FC = cast<ObjCForCollectionStmt>(Term);
1395 LoopBody = FC->getBody();
1398 case Stmt::WhileStmtClass:
1399 LoopBody = cast<WhileStmt>(Term)->getBody();
1404 return isContainedByStmt(PM, LoopBody, S);
1407 //===----------------------------------------------------------------------===//
1408 // Top-level logic for generating extensive path diagnostics.
1409 //===----------------------------------------------------------------------===//
1411 static bool GenerateExtensivePathDiagnostic(
1412 PathDiagnostic &PD, PathDiagnosticBuilder &PDB, const ExplodedNode *N,
1413 LocationContextMap &LCM,
1414 ArrayRef<std::unique_ptr<BugReporterVisitor>> visitors) {
1415 EdgeBuilder EB(PD, PDB);
1416 const SourceManager& SM = PDB.getSourceManager();
1417 StackDiagVector CallStack;
1418 InterestingExprs IE;
1420 const ExplodedNode *NextNode = N->pred_empty() ? nullptr : *(N->pred_begin());
1423 NextNode = N->getFirstPred();
1424 ProgramPoint P = N->getLocation();
1427 if (Optional<PostStmt> PS = P.getAs<PostStmt>()) {
1428 if (const Expr *Ex = PS->getStmtAs<Expr>())
1429 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1430 N->getState().get(), Ex,
1431 N->getLocationContext());
1434 if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1435 const Stmt *S = CE->getCalleeContext()->getCallSite();
1436 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
1437 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1438 N->getState().get(), Ex,
1439 N->getLocationContext());
1442 auto C = PathDiagnosticCallPiece::construct(N, *CE, SM);
1443 LCM[&C->path] = CE->getCalleeContext();
1445 EB.addEdge(C->callReturn, /*AlwaysAdd=*/true, /*IsPostJump=*/true);
1446 EB.flushLocations();
1449 PD.getActivePath().push_front(std::move(C));
1450 PD.pushActivePath(&P->path);
1451 CallStack.push_back(StackDiagPair(P, N));
1455 // Pop the call hierarchy if we are done walking the contents
1456 // of a function call.
1457 if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
1458 // Add an edge to the start of the function.
1459 const Decl *D = CE->getCalleeContext()->getDecl();
1460 PathDiagnosticLocation pos =
1461 PathDiagnosticLocation::createBegin(D, SM);
1464 // Flush all locations, and pop the active path.
1465 bool VisitedEntireCall = PD.isWithinCall();
1466 EB.flushLocations();
1468 PDB.LC = N->getLocationContext();
1470 // Either we just added a bunch of stuff to the top-level path, or
1471 // we have a previous CallExitEnd. If the former, it means that the
1472 // path terminated within a function call. We must then take the
1473 // current contents of the active path and place it within
1474 // a new PathDiagnosticCallPiece.
1475 PathDiagnosticCallPiece *C;
1476 if (VisitedEntireCall) {
1477 C = cast<PathDiagnosticCallPiece>(PD.getActivePath().front().get());
1479 const Decl *Caller = CE->getLocationContext()->getDecl();
1480 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1481 LCM[&C->path] = CE->getCalleeContext();
1484 C->setCallee(*CE, SM);
1485 EB.addContext(C->getLocation());
1487 if (!CallStack.empty()) {
1488 assert(CallStack.back().first == C);
1489 CallStack.pop_back();
1494 // Note that is important that we update the LocationContext
1495 // after looking at CallExits. CallExit basically adds an
1496 // edge in the *caller*, so we don't want to update the LocationContext
1498 PDB.LC = N->getLocationContext();
1501 if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
1502 // Does this represent entering a call? If so, look at propagating
1503 // interesting symbols across call boundaries.
1505 const LocationContext *CallerCtx = NextNode->getLocationContext();
1506 const LocationContext *CalleeCtx = PDB.LC;
1507 if (CallerCtx != CalleeCtx) {
1508 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1509 N->getState().get(),
1510 CalleeCtx, CallerCtx);
1514 // Are we jumping to the head of a loop? Add a special diagnostic.
1515 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1516 PathDiagnosticLocation L(Loop, SM, PDB.LC);
1517 const CompoundStmt *CS = nullptr;
1519 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1520 CS = dyn_cast<CompoundStmt>(FS->getBody());
1521 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1522 CS = dyn_cast<CompoundStmt>(WS->getBody());
1524 auto p = std::make_shared<PathDiagnosticEventPiece>(
1525 L, "Looping back to the head of the loop");
1526 p->setPrunable(true);
1528 EB.addEdge(p->getLocation(), true);
1529 PD.getActivePath().push_front(std::move(p));
1532 PathDiagnosticLocation BL =
1533 PathDiagnosticLocation::createEndBrace(CS, SM);
1538 const CFGBlock *BSrc = BE->getSrc();
1539 ParentMap &PM = PDB.getParentMap();
1541 if (const Stmt *Term = BSrc->getTerminator()) {
1542 // Are we jumping past the loop body without ever executing the
1543 // loop (because the condition was false)?
1544 if (isLoopJumpPastBody(Term, &*BE) &&
1546 getStmtBeforeCond(PM,
1547 BSrc->getTerminatorCondition(),
1550 PathDiagnosticLocation L(Term, SM, PDB.LC);
1551 auto PE = std::make_shared<PathDiagnosticEventPiece>(
1552 L, "Loop body executed 0 times");
1553 PE->setPrunable(true);
1555 EB.addEdge(PE->getLocation(), true);
1556 PD.getActivePath().push_front(std::move(PE));
1559 // In any case, add the terminator as the current statement
1560 // context for control edges.
1561 EB.addContext(Term);
1567 if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) {
1568 Optional<CFGElement> First = BE->getFirstElement();
1569 if (Optional<CFGStmt> S = First ? First->getAs<CFGStmt>() : None) {
1570 const Stmt *stmt = S->getStmt();
1571 if (IsControlFlowExpr(stmt)) {
1572 // Add the proper context for '&&', '||', and '?'.
1573 EB.addContext(stmt);
1576 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt());
1588 // Add pieces from custom visitors.
1589 BugReport *R = PDB.getBugReport();
1590 llvm::FoldingSet<PathDiagnosticPiece> DeduplicationSet;
1591 for (auto &V : visitors) {
1592 if (auto p = V->VisitNode(N, NextNode, PDB, *R)) {
1593 if (DeduplicationSet.GetOrInsertNode(p.get()) != p.get())
1596 const PathDiagnosticLocation &Loc = p->getLocation();
1597 EB.addEdge(Loc, true);
1598 updateStackPiecesWithMessage(*p, CallStack);
1599 PD.getActivePath().push_front(std::move(p));
1601 if (const Stmt *S = Loc.asStmt())
1602 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt());
1607 return PDB.getBugReport()->isValid();
1610 /// \brief Adds a sanitized control-flow diagnostic edge to a path.
1611 static void addEdgeToPath(PathPieces &path,
1612 PathDiagnosticLocation &PrevLoc,
1613 PathDiagnosticLocation NewLoc,
1614 const LocationContext *LC) {
1615 if (!NewLoc.isValid())
1618 SourceLocation NewLocL = NewLoc.asLocation();
1619 if (NewLocL.isInvalid())
1622 if (!PrevLoc.isValid() || !PrevLoc.asLocation().isValid()) {
1627 // Ignore self-edges, which occur when there are multiple nodes at the same
1629 if (NewLoc.asStmt() && NewLoc.asStmt() == PrevLoc.asStmt())
1633 std::make_shared<PathDiagnosticControlFlowPiece>(NewLoc, PrevLoc));
1637 /// A customized wrapper for CFGBlock::getTerminatorCondition()
1638 /// which returns the element for ObjCForCollectionStmts.
1639 static const Stmt *getTerminatorCondition(const CFGBlock *B) {
1640 const Stmt *S = B->getTerminatorCondition();
1641 if (const ObjCForCollectionStmt *FS =
1642 dyn_cast_or_null<ObjCForCollectionStmt>(S))
1643 return FS->getElement();
1647 static const char StrEnteringLoop[] = "Entering loop body";
1648 static const char StrLoopBodyZero[] = "Loop body executed 0 times";
1649 static const char StrLoopRangeEmpty[] =
1650 "Loop body skipped when range is empty";
1651 static const char StrLoopCollectionEmpty[] =
1652 "Loop body skipped when collection is empty";
1654 static bool GenerateAlternateExtensivePathDiagnostic(
1655 PathDiagnostic &PD, PathDiagnosticBuilder &PDB, const ExplodedNode *N,
1656 LocationContextMap &LCM,
1657 ArrayRef<std::unique_ptr<BugReporterVisitor>> visitors) {
1659 BugReport *report = PDB.getBugReport();
1660 const SourceManager& SM = PDB.getSourceManager();
1661 StackDiagVector CallStack;
1662 InterestingExprs IE;
1664 PathDiagnosticLocation PrevLoc = PD.getLocation();
1666 const ExplodedNode *NextNode = N->getFirstPred();
1669 NextNode = N->getFirstPred();
1670 ProgramPoint P = N->getLocation();
1673 // Have we encountered an entrance to a call? It may be
1674 // the case that we have not encountered a matching
1675 // call exit before this point. This means that the path
1676 // terminated within the call itself.
1677 if (Optional<CallEnter> CE = P.getAs<CallEnter>()) {
1678 // Add an edge to the start of the function.
1679 const StackFrameContext *CalleeLC = CE->getCalleeContext();
1680 const Decl *D = CalleeLC->getDecl();
1681 // Add the edge only when the callee has body. We jump to the beginning
1682 // of the *declaration*, however we expect it to be followed by the
1683 // body. This isn't the case for autosynthesized property accessors in
1684 // Objective-C. No need for a similar extra check for CallExit points
1685 // because the exit edge comes from a statement (i.e. return),
1686 // not from declaration.
1688 addEdgeToPath(PD.getActivePath(), PrevLoc,
1689 PathDiagnosticLocation::createBegin(D, SM), CalleeLC);
1691 // Did we visit an entire call?
1692 bool VisitedEntireCall = PD.isWithinCall();
1695 PathDiagnosticCallPiece *C;
1696 if (VisitedEntireCall) {
1697 PathDiagnosticPiece *P = PD.getActivePath().front().get();
1698 C = cast<PathDiagnosticCallPiece>(P);
1700 const Decl *Caller = CE->getLocationContext()->getDecl();
1701 C = PathDiagnosticCallPiece::construct(PD.getActivePath(), Caller);
1703 // Since we just transferred the path over to the call piece,
1704 // reset the mapping from active to location context.
1705 assert(PD.getActivePath().size() == 1 &&
1706 PD.getActivePath().front().get() == C);
1707 LCM[&PD.getActivePath()] = nullptr;
1709 // Record the location context mapping for the path within
1711 assert(LCM[&C->path] == nullptr ||
1712 LCM[&C->path] == CE->getCalleeContext());
1713 LCM[&C->path] = CE->getCalleeContext();
1715 // If this is the first item in the active path, record
1716 // the new mapping from active path to location context.
1717 const LocationContext *&NewLC = LCM[&PD.getActivePath()];
1719 NewLC = N->getLocationContext();
1723 C->setCallee(*CE, SM);
1725 // Update the previous location in the active path.
1726 PrevLoc = C->getLocation();
1728 if (!CallStack.empty()) {
1729 assert(CallStack.back().first == C);
1730 CallStack.pop_back();
1735 // Query the location context here and the previous location
1736 // as processing CallEnter may change the active path.
1737 PDB.LC = N->getLocationContext();
1739 // Record the mapping from the active path to the location
1741 assert(!LCM[&PD.getActivePath()] ||
1742 LCM[&PD.getActivePath()] == PDB.LC);
1743 LCM[&PD.getActivePath()] = PDB.LC;
1745 // Have we encountered an exit from a function call?
1746 if (Optional<CallExitEnd> CE = P.getAs<CallExitEnd>()) {
1747 const Stmt *S = CE->getCalleeContext()->getCallSite();
1748 // Propagate the interesting symbols accordingly.
1749 if (const Expr *Ex = dyn_cast_or_null<Expr>(S)) {
1750 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1751 N->getState().get(), Ex,
1752 N->getLocationContext());
1755 // We are descending into a call (backwards). Construct
1756 // a new call piece to contain the path pieces for that call.
1757 auto C = PathDiagnosticCallPiece::construct(N, *CE, SM);
1759 // Record the location context for this call piece.
1760 LCM[&C->path] = CE->getCalleeContext();
1762 // Add the edge to the return site.
1763 addEdgeToPath(PD.getActivePath(), PrevLoc, C->callReturn, PDB.LC);
1765 PD.getActivePath().push_front(std::move(C));
1766 PrevLoc.invalidate();
1768 // Make the contents of the call the active path for now.
1769 PD.pushActivePath(&P->path);
1770 CallStack.push_back(StackDiagPair(P, N));
1774 if (Optional<PostStmt> PS = P.getAs<PostStmt>()) {
1775 // For expressions, make sure we propagate the
1776 // interesting symbols correctly.
1777 if (const Expr *Ex = PS->getStmtAs<Expr>())
1778 reversePropagateIntererstingSymbols(*PDB.getBugReport(), IE,
1779 N->getState().get(), Ex,
1780 N->getLocationContext());
1782 // Add an edge. If this is an ObjCForCollectionStmt do
1783 // not add an edge here as it appears in the CFG both
1784 // as a terminator and as a terminator condition.
1785 if (!isa<ObjCForCollectionStmt>(PS->getStmt())) {
1786 PathDiagnosticLocation L =
1787 PathDiagnosticLocation(PS->getStmt(), SM, PDB.LC);
1788 addEdgeToPath(PD.getActivePath(), PrevLoc, L, PDB.LC);
1794 if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) {
1795 // Does this represent entering a call? If so, look at propagating
1796 // interesting symbols across call boundaries.
1798 const LocationContext *CallerCtx = NextNode->getLocationContext();
1799 const LocationContext *CalleeCtx = PDB.LC;
1800 if (CallerCtx != CalleeCtx) {
1801 reversePropagateInterestingSymbols(*PDB.getBugReport(), IE,
1802 N->getState().get(),
1803 CalleeCtx, CallerCtx);
1807 // Are we jumping to the head of a loop? Add a special diagnostic.
1808 if (const Stmt *Loop = BE->getSrc()->getLoopTarget()) {
1809 PathDiagnosticLocation L(Loop, SM, PDB.LC);
1810 const Stmt *Body = nullptr;
1812 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop))
1813 Body = FS->getBody();
1814 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop))
1815 Body = WS->getBody();
1816 else if (const ObjCForCollectionStmt *OFS =
1817 dyn_cast<ObjCForCollectionStmt>(Loop)) {
1818 Body = OFS->getBody();
1819 } else if (const CXXForRangeStmt *FRS =
1820 dyn_cast<CXXForRangeStmt>(Loop)) {
1821 Body = FRS->getBody();
1823 // do-while statements are explicitly excluded here
1825 auto p = std::make_shared<PathDiagnosticEventPiece>(
1826 L, "Looping back to the head "
1828 p->setPrunable(true);
1830 addEdgeToPath(PD.getActivePath(), PrevLoc, p->getLocation(), PDB.LC);
1831 PD.getActivePath().push_front(std::move(p));
1833 if (const CompoundStmt *CS = dyn_cast_or_null<CompoundStmt>(Body)) {
1834 addEdgeToPath(PD.getActivePath(), PrevLoc,
1835 PathDiagnosticLocation::createEndBrace(CS, SM),
1840 const CFGBlock *BSrc = BE->getSrc();
1841 ParentMap &PM = PDB.getParentMap();
1843 if (const Stmt *Term = BSrc->getTerminator()) {
1844 // Are we jumping past the loop body without ever executing the
1845 // loop (because the condition was false)?
1847 const Stmt *TermCond = getTerminatorCondition(BSrc);
1849 isInLoopBody(PM, getStmtBeforeCond(PM, TermCond, N), Term);
1851 const char *str = nullptr;
1853 if (isJumpToFalseBranch(&*BE)) {
1854 if (!IsInLoopBody) {
1855 if (isa<ObjCForCollectionStmt>(Term)) {
1856 str = StrLoopCollectionEmpty;
1857 } else if (isa<CXXForRangeStmt>(Term)) {
1858 str = StrLoopRangeEmpty;
1860 str = StrLoopBodyZero;
1864 str = StrEnteringLoop;
1868 PathDiagnosticLocation L(TermCond ? TermCond : Term, SM, PDB.LC);
1869 auto PE = std::make_shared<PathDiagnosticEventPiece>(L, str);
1870 PE->setPrunable(true);
1871 addEdgeToPath(PD.getActivePath(), PrevLoc,
1872 PE->getLocation(), PDB.LC);
1873 PD.getActivePath().push_front(std::move(PE));
1875 } else if (isa<BreakStmt>(Term) || isa<ContinueStmt>(Term) ||
1876 isa<GotoStmt>(Term)) {
1877 PathDiagnosticLocation L(Term, SM, PDB.LC);
1878 addEdgeToPath(PD.getActivePath(), PrevLoc, L, PDB.LC);
1888 // Add pieces from custom visitors.
1889 llvm::FoldingSet<PathDiagnosticPiece> DeduplicationSet;
1890 for (auto &V : visitors) {
1891 if (auto p = V->VisitNode(N, NextNode, PDB, *report)) {
1892 if (DeduplicationSet.GetOrInsertNode(p.get()) != p.get())
1895 addEdgeToPath(PD.getActivePath(), PrevLoc, p->getLocation(), PDB.LC);
1896 updateStackPiecesWithMessage(*p, CallStack);
1897 PD.getActivePath().push_front(std::move(p));
1902 // Add an edge to the start of the function.
1903 // We'll prune it out later, but it helps make diagnostics more uniform.
1904 const StackFrameContext *CalleeLC = PDB.LC->getCurrentStackFrame();
1905 const Decl *D = CalleeLC->getDecl();
1906 addEdgeToPath(PD.getActivePath(), PrevLoc,
1907 PathDiagnosticLocation::createBegin(D, SM),
1910 return report->isValid();
1913 static const Stmt *getLocStmt(PathDiagnosticLocation L) {
1919 static const Stmt *getStmtParent(const Stmt *S, const ParentMap &PM) {
1924 S = PM.getParentIgnoreParens(S);
1929 if (isa<ExprWithCleanups>(S) ||
1930 isa<CXXBindTemporaryExpr>(S) ||
1931 isa<SubstNonTypeTemplateParmExpr>(S))
1940 static bool isConditionForTerminator(const Stmt *S, const Stmt *Cond) {
1941 switch (S->getStmtClass()) {
1942 case Stmt::BinaryOperatorClass: {
1943 const BinaryOperator *BO = cast<BinaryOperator>(S);
1944 if (!BO->isLogicalOp())
1946 return BO->getLHS() == Cond || BO->getRHS() == Cond;
1948 case Stmt::IfStmtClass:
1949 return cast<IfStmt>(S)->getCond() == Cond;
1950 case Stmt::ForStmtClass:
1951 return cast<ForStmt>(S)->getCond() == Cond;
1952 case Stmt::WhileStmtClass:
1953 return cast<WhileStmt>(S)->getCond() == Cond;
1954 case Stmt::DoStmtClass:
1955 return cast<DoStmt>(S)->getCond() == Cond;
1956 case Stmt::ChooseExprClass:
1957 return cast<ChooseExpr>(S)->getCond() == Cond;
1958 case Stmt::IndirectGotoStmtClass:
1959 return cast<IndirectGotoStmt>(S)->getTarget() == Cond;
1960 case Stmt::SwitchStmtClass:
1961 return cast<SwitchStmt>(S)->getCond() == Cond;
1962 case Stmt::BinaryConditionalOperatorClass:
1963 return cast<BinaryConditionalOperator>(S)->getCond() == Cond;
1964 case Stmt::ConditionalOperatorClass: {
1965 const ConditionalOperator *CO = cast<ConditionalOperator>(S);
1966 return CO->getCond() == Cond ||
1967 CO->getLHS() == Cond ||
1968 CO->getRHS() == Cond;
1970 case Stmt::ObjCForCollectionStmtClass:
1971 return cast<ObjCForCollectionStmt>(S)->getElement() == Cond;
1972 case Stmt::CXXForRangeStmtClass: {
1973 const CXXForRangeStmt *FRS = cast<CXXForRangeStmt>(S);
1974 return FRS->getCond() == Cond || FRS->getRangeInit() == Cond;
1981 static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) {
1982 if (const ForStmt *FS = dyn_cast<ForStmt>(FL))
1983 return FS->getInc() == S || FS->getInit() == S;
1984 if (const CXXForRangeStmt *FRS = dyn_cast<CXXForRangeStmt>(FL))
1985 return FRS->getInc() == S || FRS->getRangeStmt() == S ||
1986 FRS->getLoopVarStmt() || FRS->getRangeInit() == S;
1990 typedef llvm::DenseSet<const PathDiagnosticCallPiece *>
1993 /// Adds synthetic edges from top-level statements to their subexpressions.
1995 /// This avoids a "swoosh" effect, where an edge from a top-level statement A
1996 /// points to a sub-expression B.1 that's not at the start of B. In these cases,
1997 /// we'd like to see an edge from A to B, then another one from B to B.1.
1998 static void addContextEdges(PathPieces &pieces, SourceManager &SM,
1999 const ParentMap &PM, const LocationContext *LCtx) {
2000 PathPieces::iterator Prev = pieces.end();
2001 for (PathPieces::iterator I = pieces.begin(), E = Prev; I != E;
2003 PathDiagnosticControlFlowPiece *Piece =
2004 dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
2009 PathDiagnosticLocation SrcLoc = Piece->getStartLocation();
2010 SmallVector<PathDiagnosticLocation, 4> SrcContexts;
2012 PathDiagnosticLocation NextSrcContext = SrcLoc;
2013 const Stmt *InnerStmt = nullptr;
2014 while (NextSrcContext.isValid() && NextSrcContext.asStmt() != InnerStmt) {
2015 SrcContexts.push_back(NextSrcContext);
2016 InnerStmt = NextSrcContext.asStmt();
2017 NextSrcContext = getEnclosingStmtLocation(InnerStmt, SM, PM, LCtx,
2018 /*allowNested=*/true);
2021 // Repeatedly split the edge as necessary.
2022 // This is important for nested logical expressions (||, &&, ?:) where we
2023 // want to show all the levels of context.
2025 const Stmt *Dst = getLocStmt(Piece->getEndLocation());
2027 // We are looking at an edge. Is the destination within a larger
2029 PathDiagnosticLocation DstContext =
2030 getEnclosingStmtLocation(Dst, SM, PM, LCtx, /*allowNested=*/true);
2031 if (!DstContext.isValid() || DstContext.asStmt() == Dst)
2034 // If the source is in the same context, we're already good.
2035 if (std::find(SrcContexts.begin(), SrcContexts.end(), DstContext) !=
2039 // Update the subexpression node to point to the context edge.
2040 Piece->setStartLocation(DstContext);
2042 // Try to extend the previous edge if it's at the same level as the source
2045 auto *PrevPiece = dyn_cast<PathDiagnosticControlFlowPiece>(Prev->get());
2048 if (const Stmt *PrevSrc = getLocStmt(PrevPiece->getStartLocation())) {
2049 const Stmt *PrevSrcParent = getStmtParent(PrevSrc, PM);
2050 if (PrevSrcParent == getStmtParent(getLocStmt(DstContext), PM)) {
2051 PrevPiece->setEndLocation(DstContext);
2058 // Otherwise, split the current edge into a context edge and a
2059 // subexpression edge. Note that the context statement may itself have
2062 std::make_shared<PathDiagnosticControlFlowPiece>(SrcLoc, DstContext);
2064 I = pieces.insert(I, std::move(P));
2069 /// \brief Move edges from a branch condition to a branch target
2070 /// when the condition is simple.
2072 /// This restructures some of the work of addContextEdges. That function
2073 /// creates edges this may destroy, but they work together to create a more
2074 /// aesthetically set of edges around branches. After the call to
2075 /// addContextEdges, we may have (1) an edge to the branch, (2) an edge from
2076 /// the branch to the branch condition, and (3) an edge from the branch
2077 /// condition to the branch target. We keep (1), but may wish to remove (2)
2078 /// and move the source of (3) to the branch if the branch condition is simple.
2080 static void simplifySimpleBranches(PathPieces &pieces) {
2081 for (PathPieces::iterator I = pieces.begin(), E = pieces.end(); I != E; ++I) {
2083 auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
2088 const Stmt *s1Start = getLocStmt(PieceI->getStartLocation());
2089 const Stmt *s1End = getLocStmt(PieceI->getEndLocation());
2091 if (!s1Start || !s1End)
2094 PathPieces::iterator NextI = I; ++NextI;
2098 PathDiagnosticControlFlowPiece *PieceNextI = nullptr;
2104 auto *EV = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
2106 StringRef S = EV->getString();
2107 if (S == StrEnteringLoop || S == StrLoopBodyZero ||
2108 S == StrLoopCollectionEmpty || S == StrLoopRangeEmpty) {
2115 PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
2122 const Stmt *s2Start = getLocStmt(PieceNextI->getStartLocation());
2123 const Stmt *s2End = getLocStmt(PieceNextI->getEndLocation());
2125 if (!s2Start || !s2End || s1End != s2Start)
2128 // We only perform this transformation for specific branch kinds.
2129 // We don't want to do this for do..while, for example.
2130 if (!(isa<ForStmt>(s1Start) || isa<WhileStmt>(s1Start) ||
2131 isa<IfStmt>(s1Start) || isa<ObjCForCollectionStmt>(s1Start) ||
2132 isa<CXXForRangeStmt>(s1Start)))
2135 // Is s1End the branch condition?
2136 if (!isConditionForTerminator(s1Start, s1End))
2139 // Perform the hoisting by eliminating (2) and changing the start
2141 PieceNextI->setStartLocation(PieceI->getStartLocation());
2142 I = pieces.erase(I);
2146 /// Returns the number of bytes in the given (character-based) SourceRange.
2148 /// If the locations in the range are not on the same line, returns None.
2150 /// Note that this does not do a precise user-visible character or column count.
2151 static Optional<size_t> getLengthOnSingleLine(SourceManager &SM,
2152 SourceRange Range) {
2153 SourceRange ExpansionRange(SM.getExpansionLoc(Range.getBegin()),
2154 SM.getExpansionRange(Range.getEnd()).second);
2156 FileID FID = SM.getFileID(ExpansionRange.getBegin());
2157 if (FID != SM.getFileID(ExpansionRange.getEnd()))
2161 const llvm::MemoryBuffer *Buffer = SM.getBuffer(FID, &Invalid);
2165 unsigned BeginOffset = SM.getFileOffset(ExpansionRange.getBegin());
2166 unsigned EndOffset = SM.getFileOffset(ExpansionRange.getEnd());
2167 StringRef Snippet = Buffer->getBuffer().slice(BeginOffset, EndOffset);
2169 // We're searching the raw bytes of the buffer here, which might include
2170 // escaped newlines and such. That's okay; we're trying to decide whether the
2171 // SourceRange is covering a large or small amount of space in the user's
2173 if (Snippet.find_first_of("\r\n") != StringRef::npos)
2176 // This isn't Unicode-aware, but it doesn't need to be.
2177 return Snippet.size();
2180 /// \sa getLengthOnSingleLine(SourceManager, SourceRange)
2181 static Optional<size_t> getLengthOnSingleLine(SourceManager &SM,
2183 return getLengthOnSingleLine(SM, S->getSourceRange());
2186 /// Eliminate two-edge cycles created by addContextEdges().
2188 /// Once all the context edges are in place, there are plenty of cases where
2189 /// there's a single edge from a top-level statement to a subexpression,
2190 /// followed by a single path note, and then a reverse edge to get back out to
2191 /// the top level. If the statement is simple enough, the subexpression edges
2192 /// just add noise and make it harder to understand what's going on.
2194 /// This function only removes edges in pairs, because removing only one edge
2195 /// might leave other edges dangling.
2197 /// This will not remove edges in more complicated situations:
2198 /// - if there is more than one "hop" leading to or from a subexpression.
2199 /// - if there is an inlined call between the edges instead of a single event.
2200 /// - if the whole statement is large enough that having subexpression arrows
2201 /// might be helpful.
2202 static void removeContextCycles(PathPieces &Path, SourceManager &SM,
2204 for (PathPieces::iterator I = Path.begin(), E = Path.end(); I != E; ) {
2205 // Pattern match the current piece and its successor.
2206 PathDiagnosticControlFlowPiece *PieceI =
2207 dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
2214 const Stmt *s1Start = getLocStmt(PieceI->getStartLocation());
2215 const Stmt *s1End = getLocStmt(PieceI->getEndLocation());
2217 PathPieces::iterator NextI = I; ++NextI;
2221 PathDiagnosticControlFlowPiece *PieceNextI =
2222 dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
2225 if (isa<PathDiagnosticEventPiece>(NextI->get())) {
2229 PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
2238 const Stmt *s2Start = getLocStmt(PieceNextI->getStartLocation());
2239 const Stmt *s2End = getLocStmt(PieceNextI->getEndLocation());
2241 if (s1Start && s2Start && s1Start == s2End && s2Start == s1End) {
2242 const size_t MAX_SHORT_LINE_LENGTH = 80;
2243 Optional<size_t> s1Length = getLengthOnSingleLine(SM, s1Start);
2244 if (s1Length && *s1Length <= MAX_SHORT_LINE_LENGTH) {
2245 Optional<size_t> s2Length = getLengthOnSingleLine(SM, s2Start);
2246 if (s2Length && *s2Length <= MAX_SHORT_LINE_LENGTH) {
2248 I = Path.erase(NextI);
2258 /// \brief Return true if X is contained by Y.
2259 static bool lexicalContains(ParentMap &PM,
2265 X = PM.getParent(X);
2270 // Remove short edges on the same line less than 3 columns in difference.
2271 static void removePunyEdges(PathPieces &path,
2275 bool erased = false;
2277 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E;
2282 auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
2287 const Stmt *start = getLocStmt(PieceI->getStartLocation());
2288 const Stmt *end = getLocStmt(PieceI->getEndLocation());
2293 const Stmt *endParent = PM.getParent(end);
2297 if (isConditionForTerminator(end, endParent))
2300 SourceLocation FirstLoc = start->getLocStart();
2301 SourceLocation SecondLoc = end->getLocStart();
2303 if (!SM.isWrittenInSameFile(FirstLoc, SecondLoc))
2305 if (SM.isBeforeInTranslationUnit(SecondLoc, FirstLoc))
2306 std::swap(SecondLoc, FirstLoc);
2308 SourceRange EdgeRange(FirstLoc, SecondLoc);
2309 Optional<size_t> ByteWidth = getLengthOnSingleLine(SM, EdgeRange);
2311 // If the statements are on different lines, continue.
2315 const size_t MAX_PUNY_EDGE_LENGTH = 2;
2316 if (*ByteWidth <= MAX_PUNY_EDGE_LENGTH) {
2317 // FIXME: There are enough /bytes/ between the endpoints of the edge, but
2318 // there might not be enough /columns/. A proper user-visible column count
2319 // is probably too expensive, though.
2327 static void removeIdenticalEvents(PathPieces &path) {
2328 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ++I) {
2329 auto *PieceI = dyn_cast<PathDiagnosticEventPiece>(I->get());
2334 PathPieces::iterator NextI = I; ++NextI;
2338 auto *PieceNextI = dyn_cast<PathDiagnosticEventPiece>(NextI->get());
2343 // Erase the second piece if it has the same exact message text.
2344 if (PieceI->getString() == PieceNextI->getString()) {
2350 static bool optimizeEdges(PathPieces &path, SourceManager &SM,
2351 OptimizedCallsSet &OCS,
2352 LocationContextMap &LCM) {
2353 bool hasChanges = false;
2354 const LocationContext *LC = LCM[&path];
2356 ParentMap &PM = LC->getParentMap();
2358 for (PathPieces::iterator I = path.begin(), E = path.end(); I != E; ) {
2359 // Optimize subpaths.
2360 if (auto *CallI = dyn_cast<PathDiagnosticCallPiece>(I->get())) {
2361 // Record the fact that a call has been optimized so we only do the
2363 if (!OCS.count(CallI)) {
2364 while (optimizeEdges(CallI->path, SM, OCS, LCM)) {}
2371 // Pattern match the current piece and its successor.
2372 auto *PieceI = dyn_cast<PathDiagnosticControlFlowPiece>(I->get());
2379 const Stmt *s1Start = getLocStmt(PieceI->getStartLocation());
2380 const Stmt *s1End = getLocStmt(PieceI->getEndLocation());
2381 const Stmt *level1 = getStmtParent(s1Start, PM);
2382 const Stmt *level2 = getStmtParent(s1End, PM);
2384 PathPieces::iterator NextI = I; ++NextI;
2388 auto *PieceNextI = dyn_cast<PathDiagnosticControlFlowPiece>(NextI->get());
2395 const Stmt *s2Start = getLocStmt(PieceNextI->getStartLocation());
2396 const Stmt *s2End = getLocStmt(PieceNextI->getEndLocation());
2397 const Stmt *level3 = getStmtParent(s2Start, PM);
2398 const Stmt *level4 = getStmtParent(s2End, PM);
2402 // If we have two consecutive control edges whose end/begin locations
2403 // are at the same level (e.g. statements or top-level expressions within
2404 // a compound statement, or siblings share a single ancestor expression),
2405 // then merge them if they have no interesting intermediate event.
2409 // (1.1 -> 1.2) -> (1.2 -> 1.3) becomes (1.1 -> 1.3) because the common
2410 // parent is '1'. Here 'x.y.z' represents the hierarchy of statements.
2412 // NOTE: this will be limited later in cases where we add barriers
2413 // to prevent this optimization.
2415 if (level1 && level1 == level2 && level1 == level3 && level1 == level4) {
2416 PieceI->setEndLocation(PieceNextI->getEndLocation());
2424 // Eliminate edges between subexpressions and parent expressions
2425 // when the subexpression is consumed.
2427 // NOTE: this will be limited later in cases where we add barriers
2428 // to prevent this optimization.
2430 if (s1End && s1End == s2Start && level2) {
2431 bool removeEdge = false;
2432 // Remove edges into the increment or initialization of a
2433 // loop that have no interleaving event. This means that
2434 // they aren't interesting.
2435 if (isIncrementOrInitInForLoop(s1End, level2))
2437 // Next only consider edges that are not anchored on
2438 // the condition of a terminator. This are intermediate edges
2439 // that we might want to trim.
2440 else if (!isConditionForTerminator(level2, s1End)) {
2441 // Trim edges on expressions that are consumed by
2442 // the parent expression.
2443 if (isa<Expr>(s1End) && PM.isConsumedExpr(cast<Expr>(s1End))) {
2446 // Trim edges where a lexical containment doesn't exist.
2451 // If 'Z' lexically contains Y (it is an ancestor) and
2452 // 'X' does not lexically contain Y (it is a descendant OR
2453 // it has no lexical relationship at all) then trim.
2455 // This can eliminate edges where we dive into a subexpression
2456 // and then pop back out, etc.
2457 else if (s1Start && s2End &&
2458 lexicalContains(PM, s2Start, s2End) &&
2459 !lexicalContains(PM, s1End, s1Start)) {
2462 // Trim edges from a subexpression back to the top level if the
2463 // subexpression is on a different line.
2469 // These edges just look ugly and don't usually add anything.
2470 else if (s1Start && s2End &&
2471 lexicalContains(PM, s1Start, s1End)) {
2472 SourceRange EdgeRange(PieceI->getEndLocation().asLocation(),
2473 PieceI->getStartLocation().asLocation());
2474 if (!getLengthOnSingleLine(SM, EdgeRange).hasValue())
2480 PieceI->setEndLocation(PieceNextI->getEndLocation());
2487 // Optimize edges for ObjC fast-enumeration loops.
2489 // (X -> collection) -> (collection -> element)
2494 if (s1End == s2Start) {
2495 const ObjCForCollectionStmt *FS =
2496 dyn_cast_or_null<ObjCForCollectionStmt>(level3);
2497 if (FS && FS->getCollection()->IgnoreParens() == s2Start &&
2498 s2End == FS->getElement()) {
2499 PieceI->setEndLocation(PieceNextI->getEndLocation());
2506 // No changes at this index? Move to the next one.
2511 // Adjust edges into subexpressions to make them more uniform
2512 // and aesthetically pleasing.
2513 addContextEdges(path, SM, PM, LC);
2514 // Remove "cyclical" edges that include one or more context edges.
2515 removeContextCycles(path, SM, PM);
2516 // Hoist edges originating from branch conditions to branches
2517 // for simple branches.
2518 simplifySimpleBranches(path);
2519 // Remove any puny edges left over after primary optimization pass.
2520 removePunyEdges(path, SM, PM);
2521 // Remove identical events.
2522 removeIdenticalEvents(path);
2528 /// Drop the very first edge in a path, which should be a function entry edge.
2530 /// If the first edge is not a function entry edge (say, because the first
2531 /// statement had an invalid source location), this function does nothing.
2532 // FIXME: We should just generate invalid edges anyway and have the optimizer
2534 static void dropFunctionEntryEdge(PathPieces &Path,
2535 LocationContextMap &LCM,
2536 SourceManager &SM) {
2537 const auto *FirstEdge =
2538 dyn_cast<PathDiagnosticControlFlowPiece>(Path.front().get());
2542 const Decl *D = LCM[&Path]->getDecl();
2543 PathDiagnosticLocation EntryLoc = PathDiagnosticLocation::createBegin(D, SM);
2544 if (FirstEdge->getStartLocation() != EntryLoc)
2551 //===----------------------------------------------------------------------===//
2552 // Methods for BugType and subclasses.
2553 //===----------------------------------------------------------------------===//
2554 void BugType::anchor() { }
2556 void BugType::FlushReports(BugReporter &BR) {}
2558 void BuiltinBug::anchor() {}
2560 //===----------------------------------------------------------------------===//
2561 // Methods for BugReport and subclasses.
2562 //===----------------------------------------------------------------------===//
2564 void BugReport::NodeResolver::anchor() {}
2566 void BugReport::addVisitor(std::unique_ptr<BugReporterVisitor> visitor) {
2570 llvm::FoldingSetNodeID ID;
2571 visitor->Profile(ID);
2574 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos))
2577 CallbacksSet.InsertNode(visitor.get(), InsertPos);
2578 Callbacks.push_back(std::move(visitor));
2579 ++ConfigurationChangeToken;
2582 BugReport::~BugReport() {
2583 while (!interestingSymbols.empty()) {
2584 popInterestingSymbolsAndRegions();
2588 const Decl *BugReport::getDeclWithIssue() const {
2590 return DeclWithIssue;
2592 const ExplodedNode *N = getErrorNode();
2596 const LocationContext *LC = N->getLocationContext();
2597 return LC->getCurrentStackFrame()->getDecl();
2600 void BugReport::Profile(llvm::FoldingSetNodeID& hash) const {
2601 hash.AddPointer(&BT);
2602 hash.AddString(Description);
2603 PathDiagnosticLocation UL = getUniqueingLocation();
2606 } else if (Location.isValid()) {
2607 Location.Profile(hash);
2610 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode));
2613 for (SourceRange range : Ranges) {
2614 if (!range.isValid())
2616 hash.AddInteger(range.getBegin().getRawEncoding());
2617 hash.AddInteger(range.getEnd().getRawEncoding());
2621 void BugReport::markInteresting(SymbolRef sym) {
2625 // If the symbol wasn't already in our set, note a configuration change.
2626 if (getInterestingSymbols().insert(sym).second)
2627 ++ConfigurationChangeToken;
2629 if (const SymbolMetadata *meta = dyn_cast<SymbolMetadata>(sym))
2630 getInterestingRegions().insert(meta->getRegion());
2633 void BugReport::markInteresting(const MemRegion *R) {
2637 // If the base region wasn't already in our set, note a configuration change.
2638 R = R->getBaseRegion();
2639 if (getInterestingRegions().insert(R).second)
2640 ++ConfigurationChangeToken;
2642 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
2643 getInterestingSymbols().insert(SR->getSymbol());
2646 void BugReport::markInteresting(SVal V) {
2647 markInteresting(V.getAsRegion());
2648 markInteresting(V.getAsSymbol());
2651 void BugReport::markInteresting(const LocationContext *LC) {
2654 InterestingLocationContexts.insert(LC);
2657 bool BugReport::isInteresting(SVal V) {
2658 return isInteresting(V.getAsRegion()) || isInteresting(V.getAsSymbol());
2661 bool BugReport::isInteresting(SymbolRef sym) {
2664 // We don't currently consider metadata symbols to be interesting
2665 // even if we know their region is interesting. Is that correct behavior?
2666 return getInterestingSymbols().count(sym);
2669 bool BugReport::isInteresting(const MemRegion *R) {
2672 R = R->getBaseRegion();
2673 bool b = getInterestingRegions().count(R);
2676 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
2677 return getInterestingSymbols().count(SR->getSymbol());
2681 bool BugReport::isInteresting(const LocationContext *LC) {
2684 return InterestingLocationContexts.count(LC);
2687 void BugReport::lazyInitializeInterestingSets() {
2688 if (interestingSymbols.empty()) {
2689 interestingSymbols.push_back(new Symbols());
2690 interestingRegions.push_back(new Regions());
2694 BugReport::Symbols &BugReport::getInterestingSymbols() {
2695 lazyInitializeInterestingSets();
2696 return *interestingSymbols.back();
2699 BugReport::Regions &BugReport::getInterestingRegions() {
2700 lazyInitializeInterestingSets();
2701 return *interestingRegions.back();
2704 void BugReport::pushInterestingSymbolsAndRegions() {
2705 interestingSymbols.push_back(new Symbols(getInterestingSymbols()));
2706 interestingRegions.push_back(new Regions(getInterestingRegions()));
2709 void BugReport::popInterestingSymbolsAndRegions() {
2710 delete interestingSymbols.pop_back_val();
2711 delete interestingRegions.pop_back_val();
2714 const Stmt *BugReport::getStmt() const {
2718 ProgramPoint ProgP = ErrorNode->getLocation();
2719 const Stmt *S = nullptr;
2721 if (Optional<BlockEntrance> BE = ProgP.getAs<BlockEntrance>()) {
2722 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit();
2723 if (BE->getBlock() == &Exit)
2724 S = GetPreviousStmt(ErrorNode);
2727 S = PathDiagnosticLocation::getStmt(ErrorNode);
2732 llvm::iterator_range<BugReport::ranges_iterator> BugReport::getRanges() {
2733 // If no custom ranges, add the range of the statement corresponding to
2735 if (Ranges.empty()) {
2736 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt()))
2737 addRange(E->getSourceRange());
2739 return llvm::make_range(ranges_iterator(), ranges_iterator());
2742 // User-specified absence of range info.
2743 if (Ranges.size() == 1 && !Ranges.begin()->isValid())
2744 return llvm::make_range(ranges_iterator(), ranges_iterator());
2746 return llvm::make_range(Ranges.begin(), Ranges.end());
2749 PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const {
2751 assert(!Location.isValid() &&
2752 "Either Location or ErrorNode should be specified but not both.");
2753 return PathDiagnosticLocation::createEndOfPath(ErrorNode, SM);
2756 assert(Location.isValid());
2760 //===----------------------------------------------------------------------===//
2761 // Methods for BugReporter and subclasses.
2762 //===----------------------------------------------------------------------===//
2764 BugReportEquivClass::~BugReportEquivClass() { }
2765 GRBugReporter::~GRBugReporter() { }
2766 BugReporterData::~BugReporterData() {}
2768 ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
2770 ProgramStateManager&
2771 GRBugReporter::getStateManager() { return Eng.getStateManager(); }
2773 BugReporter::~BugReporter() {
2776 // Free the bug reports we are tracking.
2777 typedef std::vector<BugReportEquivClass *> ContTy;
2778 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
2784 void BugReporter::FlushReports() {
2785 if (BugTypes.isEmpty())
2788 // First flush the warnings for each BugType. This may end up creating new
2789 // warnings and new BugTypes.
2790 // FIXME: Only NSErrorChecker needs BugType's FlushReports.
2791 // Turn NSErrorChecker into a proper checker and remove this.
2792 SmallVector<const BugType *, 16> bugTypes(BugTypes.begin(), BugTypes.end());
2793 for (SmallVectorImpl<const BugType *>::iterator
2794 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I)
2795 const_cast<BugType*>(*I)->FlushReports(*this);
2797 // We need to flush reports in deterministic order to ensure the order
2798 // of the reports is consistent between runs.
2799 typedef std::vector<BugReportEquivClass *> ContVecTy;
2800 for (ContVecTy::iterator EI=EQClassesVector.begin(), EE=EQClassesVector.end();
2802 BugReportEquivClass& EQ = **EI;
2806 // BugReporter owns and deletes only BugTypes created implicitly through
2808 // FIXME: There are leaks from checkers that assume that the BugTypes they
2809 // create will be destroyed by the BugReporter.
2810 llvm::DeleteContainerSeconds(StrBugTypes);
2812 // Remove all references to the BugType objects.
2813 BugTypes = F.getEmptySet();
2816 //===----------------------------------------------------------------------===//
2817 // PathDiagnostics generation.
2818 //===----------------------------------------------------------------------===//
2821 /// A wrapper around a report graph, which contains only a single path, and its
2825 InterExplodedGraphMap BackMap;
2826 std::unique_ptr<ExplodedGraph> Graph;
2827 const ExplodedNode *ErrorNode;
2831 /// A wrapper around a trimmed graph and its node maps.
2832 class TrimmedGraph {
2833 InterExplodedGraphMap InverseMap;
2835 typedef llvm::DenseMap<const ExplodedNode *, unsigned> PriorityMapTy;
2836 PriorityMapTy PriorityMap;
2838 typedef std::pair<const ExplodedNode *, size_t> NodeIndexPair;
2839 SmallVector<NodeIndexPair, 32> ReportNodes;
2841 std::unique_ptr<ExplodedGraph> G;
2843 /// A helper class for sorting ExplodedNodes by priority.
2844 template <bool Descending>
2845 class PriorityCompare {
2846 const PriorityMapTy &PriorityMap;
2849 PriorityCompare(const PriorityMapTy &M) : PriorityMap(M) {}
2851 bool operator()(const ExplodedNode *LHS, const ExplodedNode *RHS) const {
2852 PriorityMapTy::const_iterator LI = PriorityMap.find(LHS);
2853 PriorityMapTy::const_iterator RI = PriorityMap.find(RHS);
2854 PriorityMapTy::const_iterator E = PriorityMap.end();
2861 return Descending ? LI->second > RI->second
2862 : LI->second < RI->second;
2865 bool operator()(const NodeIndexPair &LHS, const NodeIndexPair &RHS) const {
2866 return (*this)(LHS.first, RHS.first);
2871 TrimmedGraph(const ExplodedGraph *OriginalGraph,
2872 ArrayRef<const ExplodedNode *> Nodes);
2874 bool popNextReportGraph(ReportGraph &GraphWrapper);
2878 TrimmedGraph::TrimmedGraph(const ExplodedGraph *OriginalGraph,
2879 ArrayRef<const ExplodedNode *> Nodes) {
2880 // The trimmed graph is created in the body of the constructor to ensure
2881 // that the DenseMaps have been initialized already.
2882 InterExplodedGraphMap ForwardMap;
2883 G = OriginalGraph->trim(Nodes, &ForwardMap, &InverseMap);
2885 // Find the (first) error node in the trimmed graph. We just need to consult
2886 // the node map which maps from nodes in the original graph to nodes
2887 // in the new graph.
2888 llvm::SmallPtrSet<const ExplodedNode *, 32> RemainingNodes;
2890 for (unsigned i = 0, count = Nodes.size(); i < count; ++i) {
2891 if (const ExplodedNode *NewNode = ForwardMap.lookup(Nodes[i])) {
2892 ReportNodes.push_back(std::make_pair(NewNode, i));
2893 RemainingNodes.insert(NewNode);
2897 assert(!RemainingNodes.empty() && "No error node found in the trimmed graph");
2899 // Perform a forward BFS to find all the shortest paths.
2900 std::queue<const ExplodedNode *> WS;
2902 assert(G->num_roots() == 1);
2903 WS.push(*G->roots_begin());
2904 unsigned Priority = 0;
2906 while (!WS.empty()) {
2907 const ExplodedNode *Node = WS.front();
2910 PriorityMapTy::iterator PriorityEntry;
2912 std::tie(PriorityEntry, IsNew) =
2913 PriorityMap.insert(std::make_pair(Node, Priority));
2917 assert(PriorityEntry->second <= Priority);
2921 if (RemainingNodes.erase(Node))
2922 if (RemainingNodes.empty())
2925 for (ExplodedNode::const_pred_iterator I = Node->succ_begin(),
2926 E = Node->succ_end();
2931 // Sort the error paths from longest to shortest.
2932 std::sort(ReportNodes.begin(), ReportNodes.end(),
2933 PriorityCompare<true>(PriorityMap));
2936 bool TrimmedGraph::popNextReportGraph(ReportGraph &GraphWrapper) {
2937 if (ReportNodes.empty())
2940 const ExplodedNode *OrigN;
2941 std::tie(OrigN, GraphWrapper.Index) = ReportNodes.pop_back_val();
2942 assert(PriorityMap.find(OrigN) != PriorityMap.end() &&
2943 "error node not accessible from root");
2945 // Create a new graph with a single path. This is the graph
2946 // that will be returned to the caller.
2947 auto GNew = llvm::make_unique<ExplodedGraph>();
2948 GraphWrapper.BackMap.clear();
2950 // Now walk from the error node up the BFS path, always taking the
2951 // predeccessor with the lowest number.
2952 ExplodedNode *Succ = nullptr;
2954 // Create the equivalent node in the new graph with the same state
2956 ExplodedNode *NewN = GNew->createUncachedNode(OrigN->getLocation(), OrigN->getState(),
2959 // Store the mapping to the original node.
2960 InterExplodedGraphMap::const_iterator IMitr = InverseMap.find(OrigN);
2961 assert(IMitr != InverseMap.end() && "No mapping to original node.");
2962 GraphWrapper.BackMap[NewN] = IMitr->second;
2964 // Link up the new node with the previous node.
2966 Succ->addPredecessor(NewN, *GNew);
2968 GraphWrapper.ErrorNode = NewN;
2972 // Are we at the final node?
2973 if (OrigN->pred_empty()) {
2974 GNew->addRoot(NewN);
2978 // Find the next predeccessor node. We choose the node that is marked
2979 // with the lowest BFS number.
2980 OrigN = *std::min_element(OrigN->pred_begin(), OrigN->pred_end(),
2981 PriorityCompare<false>(PriorityMap));
2984 GraphWrapper.Graph = std::move(GNew);
2990 /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object
2991 /// and collapses PathDiagosticPieces that are expanded by macros.
2992 static void CompactPathDiagnostic(PathPieces &path, const SourceManager& SM) {
2993 typedef std::vector<
2994 std::pair<std::shared_ptr<PathDiagnosticMacroPiece>, SourceLocation>>
2997 typedef std::vector<std::shared_ptr<PathDiagnosticPiece>> PiecesTy;
2999 MacroStackTy MacroStack;
3002 for (PathPieces::const_iterator I = path.begin(), E = path.end();
3007 // Recursively compact calls.
3008 if (auto *call = dyn_cast<PathDiagnosticCallPiece>(&*piece)) {
3009 CompactPathDiagnostic(call->path, SM);
3012 // Get the location of the PathDiagnosticPiece.
3013 const FullSourceLoc Loc = piece->getLocation().asLocation();
3015 // Determine the instantiation location, which is the location we group
3016 // related PathDiagnosticPieces.
3017 SourceLocation InstantiationLoc = Loc.isMacroID() ?
3018 SM.getExpansionLoc(Loc) :
3021 if (Loc.isFileID()) {
3023 Pieces.push_back(piece);
3027 assert(Loc.isMacroID());
3029 // Is the PathDiagnosticPiece within the same macro group?
3030 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) {
3031 MacroStack.back().first->subPieces.push_back(piece);
3035 // We aren't in the same group. Are we descending into a new macro
3036 // or are part of an old one?
3037 std::shared_ptr<PathDiagnosticMacroPiece> MacroGroup;
3039 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ?
3040 SM.getExpansionLoc(Loc) :
3043 // Walk the entire macro stack.
3044 while (!MacroStack.empty()) {
3045 if (InstantiationLoc == MacroStack.back().second) {
3046 MacroGroup = MacroStack.back().first;
3050 if (ParentInstantiationLoc == MacroStack.back().second) {
3051 MacroGroup = MacroStack.back().first;
3055 MacroStack.pop_back();
3058 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) {
3059 // Create a new macro group and add it to the stack.
3060 auto NewGroup = std::make_shared<PathDiagnosticMacroPiece>(
3061 PathDiagnosticLocation::createSingleLocation(piece->getLocation()));
3064 MacroGroup->subPieces.push_back(NewGroup);
3066 assert(InstantiationLoc.isFileID());
3067 Pieces.push_back(NewGroup);
3070 MacroGroup = NewGroup;
3071 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc));
3074 // Finally, add the PathDiagnosticPiece to the group.
3075 MacroGroup->subPieces.push_back(piece);
3078 // Now take the pieces and construct a new PathDiagnostic.
3081 path.insert(path.end(), Pieces.begin(), Pieces.end());
3084 bool GRBugReporter::generatePathDiagnostic(PathDiagnostic& PD,
3085 PathDiagnosticConsumer &PC,
3086 ArrayRef<BugReport *> &bugReports) {
3087 assert(!bugReports.empty());
3089 bool HasValid = false;
3090 bool HasInvalid = false;
3091 SmallVector<const ExplodedNode *, 32> errorNodes;
3092 for (ArrayRef<BugReport*>::iterator I = bugReports.begin(),
3093 E = bugReports.end(); I != E; ++I) {
3094 if ((*I)->isValid()) {
3096 errorNodes.push_back((*I)->getErrorNode());
3098 // Keep the errorNodes list in sync with the bugReports list.
3100 errorNodes.push_back(nullptr);
3104 // If all the reports have been marked invalid by a previous path generation,
3109 typedef PathDiagnosticConsumer::PathGenerationScheme PathGenerationScheme;
3110 PathGenerationScheme ActiveScheme = PC.getGenerationScheme();
3112 if (ActiveScheme == PathDiagnosticConsumer::Extensive) {
3113 AnalyzerOptions &options = getAnalyzerOptions();
3114 if (options.getBooleanOption("path-diagnostics-alternate", true)) {
3115 ActiveScheme = PathDiagnosticConsumer::AlternateExtensive;
3119 TrimmedGraph TrimG(&getGraph(), errorNodes);
3120 ReportGraph ErrorGraph;
3122 while (TrimG.popNextReportGraph(ErrorGraph)) {
3123 // Find the BugReport with the original location.
3124 assert(ErrorGraph.Index < bugReports.size());
3125 BugReport *R = bugReports[ErrorGraph.Index];
3126 assert(R && "No original report found for sliced graph.");
3127 assert(R->isValid() && "Report selected by trimmed graph marked invalid.");
3129 // Start building the path diagnostic...
3130 PathDiagnosticBuilder PDB(*this, R, ErrorGraph.BackMap, &PC);
3131 const ExplodedNode *N = ErrorGraph.ErrorNode;
3133 // Register additional node visitors.
3134 R->addVisitor(llvm::make_unique<NilReceiverBRVisitor>());
3135 R->addVisitor(llvm::make_unique<ConditionBRVisitor>());
3136 R->addVisitor(llvm::make_unique<LikelyFalsePositiveSuppressionBRVisitor>());
3137 R->addVisitor(llvm::make_unique<CXXSelfAssignmentBRVisitor>());
3139 BugReport::VisitorList visitors;
3140 unsigned origReportConfigToken, finalReportConfigToken;
3141 LocationContextMap LCM;
3143 // While generating diagnostics, it's possible the visitors will decide
3144 // new symbols and regions are interesting, or add other visitors based on
3145 // the information they find. If they do, we need to regenerate the path
3146 // based on our new report configuration.
3148 // Get a clean copy of all the visitors.
3149 for (BugReport::visitor_iterator I = R->visitor_begin(),
3150 E = R->visitor_end(); I != E; ++I)
3151 visitors.push_back((*I)->clone());
3153 // Clear out the active path from any previous work.
3155 origReportConfigToken = R->getConfigurationChangeToken();
3157 // Generate the very last diagnostic piece - the piece is visible before
3158 // the trace is expanded.
3159 std::unique_ptr<PathDiagnosticPiece> LastPiece;
3160 for (BugReport::visitor_iterator I = visitors.begin(), E = visitors.end();
3162 if (std::unique_ptr<PathDiagnosticPiece> Piece =
3163 (*I)->getEndPath(PDB, N, *R)) {
3164 assert (!LastPiece &&
3165 "There can only be one final piece in a diagnostic.");
3166 LastPiece = std::move(Piece);
3170 if (ActiveScheme != PathDiagnosticConsumer::None) {
3172 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R);
3174 PD.setEndOfPath(std::move(LastPiece));
3177 // Make sure we get a clean location context map so we don't
3178 // hold onto old mappings.
3181 switch (ActiveScheme) {
3182 case PathDiagnosticConsumer::AlternateExtensive:
3183 GenerateAlternateExtensivePathDiagnostic(PD, PDB, N, LCM, visitors);
3185 case PathDiagnosticConsumer::Extensive:
3186 GenerateExtensivePathDiagnostic(PD, PDB, N, LCM, visitors);
3188 case PathDiagnosticConsumer::Minimal:
3189 GenerateMinimalPathDiagnostic(PD, PDB, N, LCM, visitors);
3191 case PathDiagnosticConsumer::None:
3192 GenerateVisitorsOnlyPathDiagnostic(PD, PDB, N, visitors);
3196 // Clean up the visitors we used.
3199 // Did anything change while generating this path?
3200 finalReportConfigToken = R->getConfigurationChangeToken();
3201 } while (finalReportConfigToken != origReportConfigToken);
3206 // Finally, prune the diagnostic path of uninteresting stuff.
3207 if (!PD.path.empty()) {
3208 if (R->shouldPrunePath() && getAnalyzerOptions().shouldPrunePaths()) {
3209 bool stillHasNotes = removeUnneededCalls(PD.getMutablePieces(), R, LCM);
3210 assert(stillHasNotes);
3211 (void)stillHasNotes;
3214 // Redirect all call pieces to have valid locations.
3215 adjustCallLocations(PD.getMutablePieces());
3216 removePiecesWithInvalidLocations(PD.getMutablePieces());
3218 if (ActiveScheme == PathDiagnosticConsumer::AlternateExtensive) {
3219 SourceManager &SM = getSourceManager();
3221 // Reduce the number of edges from a very conservative set
3222 // to an aesthetically pleasing subset that conveys the
3223 // necessary information.
3224 OptimizedCallsSet OCS;
3225 while (optimizeEdges(PD.getMutablePieces(), SM, OCS, LCM)) {}
3227 // Drop the very first function-entry edge. It's not really necessary
3228 // for top-level functions.
3229 dropFunctionEntryEdge(PD.getMutablePieces(), LCM, SM);
3232 // Remove messages that are basically the same, and edges that may not
3234 // We have to do this after edge optimization in the Extensive mode.
3235 removeRedundantMsgs(PD.getMutablePieces());
3236 removeEdgesToDefaultInitializers(PD.getMutablePieces());
3239 // We found a report and didn't suppress it.
3243 // We suppressed all the reports in this equivalence class.
3244 assert(!HasInvalid && "Inconsistent suppression");
3249 void BugReporter::Register(BugType *BT) {
3250 BugTypes = F.add(BugTypes, BT);
3253 void BugReporter::emitReport(std::unique_ptr<BugReport> R) {
3254 if (const ExplodedNode *E = R->getErrorNode()) {
3255 // An error node must either be a sink or have a tag, otherwise
3256 // it could get reclaimed before the path diagnostic is created.
3257 assert((E->isSink() || E->getLocation().getTag()) &&
3258 "Error node must either be a sink or have a tag");
3260 const AnalysisDeclContext *DeclCtx =
3261 E->getLocationContext()->getAnalysisDeclContext();
3262 // The source of autosynthesized body can be handcrafted AST or a model
3263 // file. The locations from handcrafted ASTs have no valid source locations
3264 // and have to be discarded. Locations from model files should be preserved
3265 // for processing and reporting.
3266 if (DeclCtx->isBodyAutosynthesized() &&
3267 !DeclCtx->isBodyAutosynthesizedFromModelFile())
3271 bool ValidSourceLoc = R->getLocation(getSourceManager()).isValid();
3272 assert(ValidSourceLoc);
3273 // If we mess up in a release build, we'd still prefer to just drop the bug
3274 // instead of trying to go on.
3275 if (!ValidSourceLoc)
3278 // Compute the bug report's hash to determine its equivalence class.
3279 llvm::FoldingSetNodeID ID;
3282 // Lookup the equivance class. If there isn't one, create it.
3283 BugType& BT = R->getBugType();
3286 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos);
3289 EQ = new BugReportEquivClass(std::move(R));
3290 EQClasses.InsertNode(EQ, InsertPos);
3291 EQClassesVector.push_back(EQ);
3293 EQ->AddReport(std::move(R));
3297 //===----------------------------------------------------------------------===//
3298 // Emitting reports in equivalence classes.
3299 //===----------------------------------------------------------------------===//
3302 struct FRIEC_WLItem {
3303 const ExplodedNode *N;
3304 ExplodedNode::const_succ_iterator I, E;
3306 FRIEC_WLItem(const ExplodedNode *n)
3307 : N(n), I(N->succ_begin()), E(N->succ_end()) {}
3311 static const CFGBlock *findBlockForNode(const ExplodedNode *N) {
3312 ProgramPoint P = N->getLocation();
3313 if (auto BEP = P.getAs<BlockEntrance>())
3314 return BEP->getBlock();
3316 // Find the node's current statement in the CFG.
3317 if (const Stmt *S = PathDiagnosticLocation::getStmt(N))
3318 return N->getLocationContext()->getAnalysisDeclContext()
3319 ->getCFGStmtMap()->getBlock(S);
3324 // Returns true if by simply looking at the block, we can be sure that it
3325 // results in a sink during analysis. This is useful to know when the analysis
3326 // was interrupted, and we try to figure out if it would sink eventually.
3327 // There may be many more reasons why a sink would appear during analysis
3328 // (eg. checkers may generate sinks arbitrarily), but here we only consider
3329 // sinks that would be obvious by looking at the CFG.
3330 static bool isImmediateSinkBlock(const CFGBlock *Blk) {
3331 if (Blk->hasNoReturnElement())
3334 // FIXME: Throw-expressions are currently generating sinks during analysis:
3335 // they're not supported yet, and also often used for actually terminating
3336 // the program. So we should treat them as sinks in this analysis as well,
3337 // at least for now, but once we have better support for exceptions,
3338 // we'd need to carefully handle the case when the throw is being
3339 // immediately caught.
3340 if (std::any_of(Blk->begin(), Blk->end(), [](const CFGElement &Elm) {
3341 if (Optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>())
3342 if (isa<CXXThrowExpr>(StmtElm->getStmt()))
3351 // Returns true if by looking at the CFG surrounding the node's program
3352 // point, we can be sure that any analysis starting from this point would
3353 // eventually end with a sink. We scan the child CFG blocks in a depth-first
3354 // manner and see if all paths eventually end up in an immediate sink block.
3355 static bool isInevitablySinking(const ExplodedNode *N) {
3356 const CFG &Cfg = N->getCFG();
3358 const CFGBlock *StartBlk = findBlockForNode(N);
3361 if (isImmediateSinkBlock(StartBlk))
3364 llvm::SmallVector<const CFGBlock *, 32> DFSWorkList;
3365 llvm::SmallPtrSet<const CFGBlock *, 32> Visited;
3367 DFSWorkList.push_back(StartBlk);
3368 while (!DFSWorkList.empty()) {
3369 const CFGBlock *Blk = DFSWorkList.back();
3370 DFSWorkList.pop_back();
3371 Visited.insert(Blk);
3373 for (const auto &Succ : Blk->succs()) {
3374 if (const CFGBlock *SuccBlk = Succ.getReachableBlock()) {
3375 if (SuccBlk == &Cfg.getExit()) {
3376 // If at least one path reaches the CFG exit, it means that control is
3377 // returned to the caller. For now, say that we are not sure what
3378 // happens next. If necessary, this can be improved to analyze
3379 // the parent StackFrameContext's call site in a similar manner.
3383 if (!isImmediateSinkBlock(SuccBlk) && !Visited.count(SuccBlk)) {
3384 // If the block has reachable child blocks that aren't no-return,
3385 // add them to the worklist.
3386 DFSWorkList.push_back(SuccBlk);
3392 // Nothing reached the exit. It can only mean one thing: there's no return.
3397 FindReportInEquivalenceClass(BugReportEquivClass& EQ,
3398 SmallVectorImpl<BugReport*> &bugReports) {
3400 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end();
3402 BugType& BT = I->getBugType();
3404 // If we don't need to suppress any of the nodes because they are
3405 // post-dominated by a sink, simply add all the nodes in the equivalence class
3406 // to 'Nodes'. Any of the reports will serve as a "representative" report.
3407 if (!BT.isSuppressOnSink()) {
3409 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) {
3410 const ExplodedNode *N = I->getErrorNode();
3413 bugReports.push_back(R);
3419 // For bug reports that should be suppressed when all paths are post-dominated
3420 // by a sink node, iterate through the reports in the equivalence class
3421 // until we find one that isn't post-dominated (if one exists). We use a
3422 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write
3423 // this as a recursive function, but we don't want to risk blowing out the
3424 // stack for very long paths.
3425 BugReport *exampleReport = nullptr;
3427 for (; I != E; ++I) {
3428 const ExplodedNode *errorNode = I->getErrorNode();
3432 if (errorNode->isSink()) {
3434 "BugType::isSuppressSink() should not be 'true' for sink end nodes");
3436 // No successors? By definition this nodes isn't post-dominated by a sink.
3437 if (errorNode->succ_empty()) {
3438 bugReports.push_back(&*I);
3440 exampleReport = &*I;
3444 // See if we are in a no-return CFG block. If so, treat this similarly
3445 // to being post-dominated by a sink. This works better when the analysis
3446 // is incomplete and we have never reached the no-return function call(s)
3447 // that we'd inevitably bump into on this path.
3448 if (isInevitablySinking(errorNode))
3451 // At this point we know that 'N' is not a sink and it has at least one
3452 // successor. Use a DFS worklist to find a non-sink end-of-path node.
3453 typedef FRIEC_WLItem WLItem;
3454 typedef SmallVector<WLItem, 10> DFSWorkList;
3455 llvm::DenseMap<const ExplodedNode *, unsigned> Visited;
3458 WL.push_back(errorNode);
3459 Visited[errorNode] = 1;
3461 while (!WL.empty()) {
3462 WLItem &WI = WL.back();
3463 assert(!WI.N->succ_empty());
3465 for (; WI.I != WI.E; ++WI.I) {
3466 const ExplodedNode *Succ = *WI.I;
3467 // End-of-path node?
3468 if (Succ->succ_empty()) {
3469 // If we found an end-of-path node that is not a sink.
3470 if (!Succ->isSink()) {
3471 bugReports.push_back(&*I);
3473 exampleReport = &*I;
3477 // Found a sink? Continue on to the next successor.
3480 // Mark the successor as visited. If it hasn't been explored,
3481 // enqueue it to the DFS worklist.
3482 unsigned &mark = Visited[Succ];
3490 // The worklist may have been cleared at this point. First
3491 // check if it is empty before checking the last item.
3492 if (!WL.empty() && &WL.back() == &WI)
3497 // ExampleReport will be NULL if all the nodes in the equivalence class
3498 // were post-dominated by sinks.
3499 return exampleReport;
3502 void BugReporter::FlushReport(BugReportEquivClass& EQ) {
3503 SmallVector<BugReport*, 10> bugReports;
3504 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports);
3505 if (exampleReport) {
3506 for (PathDiagnosticConsumer *PDC : getPathDiagnosticConsumers()) {
3507 FlushReport(exampleReport, *PDC, bugReports);
3512 void BugReporter::FlushReport(BugReport *exampleReport,
3513 PathDiagnosticConsumer &PD,
3514 ArrayRef<BugReport*> bugReports) {
3516 // FIXME: Make sure we use the 'R' for the path that was actually used.
3517 // Probably doesn't make a difference in practice.
3518 BugType& BT = exampleReport->getBugType();
3520 std::unique_ptr<PathDiagnostic> D(new PathDiagnostic(
3521 exampleReport->getBugType().getCheckName(),
3522 exampleReport->getDeclWithIssue(), exampleReport->getBugType().getName(),
3523 exampleReport->getDescription(),
3524 exampleReport->getShortDescription(/*Fallback=*/false), BT.getCategory(),
3525 exampleReport->getUniqueingLocation(),
3526 exampleReport->getUniqueingDecl()));
3528 if (exampleReport->isPathSensitive()) {
3529 // Generate the full path diagnostic, using the generation scheme
3530 // specified by the PathDiagnosticConsumer. Note that we have to generate
3531 // path diagnostics even for consumers which do not support paths, because
3532 // the BugReporterVisitors may mark this bug as a false positive.
3533 assert(!bugReports.empty());
3535 MaxBugClassSize.updateMax(bugReports.size());
3537 if (!generatePathDiagnostic(*D.get(), PD, bugReports))
3540 MaxValidBugClassSize.updateMax(bugReports.size());
3542 // Examine the report and see if the last piece is in a header. Reset the
3543 // report location to the last piece in the main source file.
3544 AnalyzerOptions &Opts = getAnalyzerOptions();
3545 if (Opts.shouldReportIssuesInMainSourceFile() && !Opts.AnalyzeAll)
3546 D->resetDiagnosticLocationToMainFile();
3549 // If the path is empty, generate a single step path with the location
3551 if (D->path.empty()) {
3552 PathDiagnosticLocation L = exampleReport->getLocation(getSourceManager());
3553 auto piece = llvm::make_unique<PathDiagnosticEventPiece>(
3554 L, exampleReport->getDescription());
3555 for (SourceRange Range : exampleReport->getRanges())
3556 piece->addRange(Range);
3557 D->setEndOfPath(std::move(piece));
3560 PathPieces &Pieces = D->getMutablePieces();
3561 if (getAnalyzerOptions().shouldDisplayNotesAsEvents()) {
3562 // For path diagnostic consumers that don't support extra notes,
3563 // we may optionally convert those to path notes.
3564 for (auto I = exampleReport->getNotes().rbegin(),
3565 E = exampleReport->getNotes().rend(); I != E; ++I) {
3566 PathDiagnosticNotePiece *Piece = I->get();
3567 auto ConvertedPiece = std::make_shared<PathDiagnosticEventPiece>(
3568 Piece->getLocation(), Piece->getString());
3569 for (const auto &R: Piece->getRanges())
3570 ConvertedPiece->addRange(R);
3572 Pieces.push_front(std::move(ConvertedPiece));
3575 for (auto I = exampleReport->getNotes().rbegin(),
3576 E = exampleReport->getNotes().rend(); I != E; ++I)
3577 Pieces.push_front(*I);
3580 // Get the meta data.
3581 const BugReport::ExtraTextList &Meta = exampleReport->getExtraText();
3582 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(),
3583 e = Meta.end(); i != e; ++i) {
3587 PD.HandlePathDiagnostic(std::move(D));
3590 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3591 const CheckerBase *Checker,
3592 StringRef Name, StringRef Category,
3593 StringRef Str, PathDiagnosticLocation Loc,
3594 ArrayRef<SourceRange> Ranges) {
3595 EmitBasicReport(DeclWithIssue, Checker->getCheckName(), Name, Category, Str,
3598 void BugReporter::EmitBasicReport(const Decl *DeclWithIssue,
3599 CheckName CheckName,
3600 StringRef name, StringRef category,
3601 StringRef str, PathDiagnosticLocation Loc,
3602 ArrayRef<SourceRange> Ranges) {
3604 // 'BT' is owned by BugReporter.
3605 BugType *BT = getBugTypeForName(CheckName, name, category);
3606 auto R = llvm::make_unique<BugReport>(*BT, str, Loc);
3607 R->setDeclWithIssue(DeclWithIssue);
3608 for (ArrayRef<SourceRange>::iterator I = Ranges.begin(), E = Ranges.end();
3611 emitReport(std::move(R));
3614 BugType *BugReporter::getBugTypeForName(CheckName CheckName, StringRef name,
3615 StringRef category) {
3616 SmallString<136> fullDesc;
3617 llvm::raw_svector_ostream(fullDesc) << CheckName.getName() << ":" << name
3619 BugType *&BT = StrBugTypes[fullDesc];
3621 BT = new BugType(CheckName, name, category);
3625 LLVM_DUMP_METHOD void PathPieces::dump() const {
3627 for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) {
3628 llvm::errs() << "[" << index++ << "] ";
3630 llvm::errs() << "\n";
3634 LLVM_DUMP_METHOD void PathDiagnosticCallPiece::dump() const {
3635 llvm::errs() << "CALL\n--------------\n";
3637 if (const Stmt *SLoc = getLocStmt(getLocation()))
3639 else if (const NamedDecl *ND = dyn_cast<NamedDecl>(getCallee()))
3640 llvm::errs() << *ND << "\n";
3642 getLocation().dump();
3645 LLVM_DUMP_METHOD void PathDiagnosticEventPiece::dump() const {
3646 llvm::errs() << "EVENT\n--------------\n";
3647 llvm::errs() << getString() << "\n";
3648 llvm::errs() << " ---- at ----\n";
3649 getLocation().dump();
3652 LLVM_DUMP_METHOD void PathDiagnosticControlFlowPiece::dump() const {
3653 llvm::errs() << "CONTROL\n--------------\n";
3654 getStartLocation().dump();
3655 llvm::errs() << " ---- to ----\n";
3656 getEndLocation().dump();
3659 LLVM_DUMP_METHOD void PathDiagnosticMacroPiece::dump() const {
3660 llvm::errs() << "MACRO\n--------------\n";
3661 // FIXME: Print which macro is being invoked.
3664 LLVM_DUMP_METHOD void PathDiagnosticNotePiece::dump() const {
3665 llvm::errs() << "NOTE\n--------------\n";
3666 llvm::errs() << getString() << "\n";
3667 llvm::errs() << " ---- at ----\n";
3668 getLocation().dump();
3671 LLVM_DUMP_METHOD void PathDiagnosticLocation::dump() const {
3673 llvm::errs() << "<INVALID>\n";
3679 // FIXME: actually print the range.
3680 llvm::errs() << "<range>\n";
3683 asLocation().dump();
3684 llvm::errs() << "\n";
3690 llvm::errs() << "<NULL STMT>\n";
3693 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
3694 llvm::errs() << *ND << "\n";
3695 else if (isa<BlockDecl>(D))
3696 // FIXME: Make this nicer.
3697 llvm::errs() << "<block>\n";
3699 llvm::errs() << "<unknown decl>\n";
3701 llvm::errs() << "<NULL DECL>\n";