1 // BugReporterVisitors.cpp - Helpers for reporting 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 a set of BugReporter "visitors" which can be used to
11 // enhance the diagnostics reported for a bug.
13 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprObjC.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
23 #include "llvm/ADT/SmallString.h"
25 using namespace clang;
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) {
33 // Pattern match for a few useful cases (do something smarter later):
35 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
37 if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) {
38 if (U->getOpcode() == UO_Deref)
39 return U->getSubExpr()->IgnoreParenCasts();
41 else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) {
42 return ME->getBase()->IgnoreParenCasts();
44 else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) {
51 const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) {
52 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
53 if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S))
58 const Stmt *bugreporter::GetCalleeExpr(const ExplodedNode *N) {
59 // Callee is checked as a PreVisit to the CallExpr.
60 const Stmt *S = N->getLocationAs<PreStmt>()->getStmt();
61 if (const CallExpr *CE = dyn_cast<CallExpr>(S))
62 return CE->getCallee();
66 const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) {
67 const Stmt *S = N->getLocationAs<PostStmt>()->getStmt();
68 if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S))
69 return RS->getRetValue();
73 //===----------------------------------------------------------------------===//
74 // Definitions for bug reporter visitors.
75 //===----------------------------------------------------------------------===//
78 BugReporterVisitor::getEndPath(BugReporterContext &BRC,
79 const ExplodedNode *EndPathNode,
85 BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC,
86 const ExplodedNode *EndPathNode,
88 PathDiagnosticLocation L =
89 PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager());
91 BugReport::ranges_iterator Beg, End;
92 llvm::tie(Beg, End) = BR.getRanges();
94 // Only add the statement itself as a range if we didn't specify any
95 // special ranges for this report.
96 PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L,
99 for (; Beg != End; ++Beg)
106 void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const {
113 PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N,
114 const ExplodedNode *PrevN,
115 BugReporterContext &BRC,
122 const ExplodedNode *Node = N, *Last = NULL;
124 for ( ; Node ; Node = Node->getFirstPred()) {
126 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
127 if (const PostStmt *P = Node->getLocationAs<PostStmt>())
128 if (const DeclStmt *DS = P->getStmtAs<DeclStmt>())
129 if (DS->getSingleDecl() == VR->getDecl()) {
130 // Record the last seen initialization point.
136 // Does the region still bind to value V? If not, we are done
137 // looking for store sites.
138 if (Node->getState()->getSVal(R) != V)
142 if (!Node || !Last) {
154 SmallString<256> sbuf;
155 llvm::raw_svector_ostream os(sbuf);
157 if (const PostStmt *PS = N->getLocationAs<PostStmt>()) {
158 if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) {
160 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
161 os << "Variable '" << *VR->getDecl() << "' ";
166 if (isa<loc::ConcreteInt>(V)) {
168 if (R->isBoundable()) {
169 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
170 if (TR->getValueType()->isObjCObjectPointerType()) {
171 os << "initialized to nil";
178 os << "initialized to a null pointer value";
180 else if (isa<nonloc::ConcreteInt>(V)) {
181 os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue();
183 else if (V.isUndef()) {
184 if (isa<VarRegion>(R)) {
185 const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl());
187 os << "initialized to a garbage value";
189 os << "declared without an initial value";
195 if (os.str().empty()) {
196 if (isa<loc::ConcreteInt>(V)) {
198 if (R->isBoundable()) {
199 if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
200 if (TR->getValueType()->isObjCObjectPointerType()) {
201 os << "nil object reference stored to ";
208 os << "Null pointer value stored to ";
210 else if (V.isUndef()) {
211 os << "Uninitialized value stored to ";
213 else if (isa<nonloc::ConcreteInt>(V)) {
214 os << "The value " << cast<nonloc::ConcreteInt>(V).getValue()
215 << " is assigned to ";
220 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
221 os << '\'' << *VR->getDecl() << '\'';
227 // Construct a new PathDiagnosticPiece.
228 ProgramPoint P = N->getLocation();
229 PathDiagnosticLocation L =
230 PathDiagnosticLocation::create(P, BRC.getSourceManager());
233 return new PathDiagnosticEventPiece(L, os.str());
236 void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
239 ID.AddBoolean(Assumption);
243 PathDiagnosticPiece *
244 TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N,
245 const ExplodedNode *PrevN,
246 BugReporterContext &BRC,
251 // Check if in the previous state it was feasible for this constraint
253 if (PrevN->getState()->assume(Constraint, !Assumption)) {
257 // As a sanity check, make sure that the negation of the constraint
258 // was infeasible in the current state. If it is feasible, we somehow
259 // missed the transition point.
260 if (N->getState()->assume(Constraint, !Assumption))
263 // We found the transition point for the constraint. We now need to
264 // pretty-print the constraint. (work-in-progress)
266 llvm::raw_string_ostream os(sbuf);
268 if (isa<Loc>(Constraint)) {
269 os << "Assuming pointer value is ";
270 os << (Assumption ? "non-null" : "null");
273 if (os.str().empty())
276 // Construct a new PathDiagnosticPiece.
277 ProgramPoint P = N->getLocation();
278 PathDiagnosticLocation L =
279 PathDiagnosticLocation::create(P, BRC.getSourceManager());
282 return new PathDiagnosticEventPiece(L, os.str());
289 bugreporter::getTrackNullOrUndefValueVisitor(const ExplodedNode *N,
295 ProgramStateManager &StateMgr = N->getState()->getStateManager();
297 // Walk through nodes until we get one that matches the statement
300 const ProgramPoint &pp = N->getLocation();
301 if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) {
302 if (ps->getStmt() == S)
305 N = N->getFirstPred();
311 ProgramStateRef state = N->getState();
313 // Walk through lvalue-to-rvalue conversions.
314 const Expr *Ex = dyn_cast<Expr>(S);
316 Ex = Ex->IgnoreParenLValueCasts();
317 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
318 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
320 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
323 SVal V = state->getSVal(loc::MemRegionVal(R));
324 report->markInteresting(R);
325 report->markInteresting(V);
326 return new FindLastStoreBRVisitor(V, R);
331 SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext());
333 // Uncomment this to find cases where we aren't properly getting the
334 // base value that was dereferenced.
335 // assert(!V.isUnknownOrUndef());
337 // Is it a symbolic value?
338 if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) {
339 const SubRegion *R = cast<SubRegion>(L->getRegion());
340 while (R && !isa<SymbolicRegion>(R)) {
341 R = dyn_cast<SubRegion>(R->getSuperRegion());
345 report->markInteresting(R);
346 return new TrackConstraintBRVisitor(loc::MemRegionVal(R), false);
354 FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N,
355 const MemRegion *R) {
356 assert(R && "The memory region is null.");
358 ProgramStateRef state = N->getState();
359 SVal V = state->getSVal(R);
363 return new FindLastStoreBRVisitor(V, R);
367 PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N,
368 const ExplodedNode *PrevN,
369 BugReporterContext &BRC,
371 const PostStmt *P = N->getLocationAs<PostStmt>();
374 const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>();
377 const Expr *Receiver = ME->getInstanceReceiver();
380 ProgramStateRef state = N->getState();
381 const SVal &V = state->getSVal(Receiver, N->getLocationContext());
382 const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V);
385 state = state->assume(*DV, true);
389 // The receiver was nil, and hence the method was skipped.
390 // Register a BugReporterVisitor to issue a message telling us how
391 // the receiver was null.
392 BR.addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Receiver, &BR));
393 // Issue a message saying that the method was skipped.
394 PathDiagnosticLocation L(Receiver, BRC.getSourceManager(),
395 N->getLocationContext());
396 return new PathDiagnosticEventPiece(L, "No method is called "
397 "because the receiver is nil");
400 // Registers every VarDecl inside a Stmt with a last store visitor.
401 void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR,
403 const ExplodedNode *N = BR.getErrorNode();
404 std::deque<const Stmt *> WorkList;
405 WorkList.push_back(S);
407 while (!WorkList.empty()) {
408 const Stmt *Head = WorkList.front();
409 WorkList.pop_front();
411 ProgramStateRef state = N->getState();
412 ProgramStateManager &StateMgr = state->getStateManager();
414 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) {
415 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
417 StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext());
420 SVal V = state->getSVal(S, N->getLocationContext());
422 if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) {
423 // Register a new visitor with the BugReport.
424 BR.addVisitor(new FindLastStoreBRVisitor(V, R));
429 for (Stmt::const_child_iterator I = Head->child_begin();
430 I != Head->child_end(); ++I)
431 WorkList.push_back(*I);
435 //===----------------------------------------------------------------------===//
436 // Visitor that tries to report interesting diagnostics from conditions.
437 //===----------------------------------------------------------------------===//
438 PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N,
439 const ExplodedNode *Prev,
440 BugReporterContext &BRC,
442 PathDiagnosticPiece *piece = VisitNodeImpl(N, Prev, BRC, BR);
443 if (PathDiagnosticEventPiece *ev =
444 dyn_cast_or_null<PathDiagnosticEventPiece>(piece))
445 ev->setPrunable(true, /* override */ false);
449 PathDiagnosticPiece *ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N,
450 const ExplodedNode *Prev,
451 BugReporterContext &BRC,
454 const ProgramPoint &progPoint = N->getLocation();
456 ProgramStateRef CurrentState = N->getState();
457 ProgramStateRef PrevState = Prev->getState();
459 // Compare the GDMs of the state, because that is where constraints
460 // are managed. Note that ensure that we only look at nodes that
461 // were generated by the analyzer engine proper, not checkers.
462 if (CurrentState->getGDM().getRoot() ==
463 PrevState->getGDM().getRoot())
466 // If an assumption was made on a branch, it should be caught
467 // here by looking at the state transition.
468 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) {
469 const CFGBlock *srcBlk = BE->getSrc();
470 if (const Stmt *term = srcBlk->getTerminator())
471 return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC);
475 if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) {
476 // FIXME: Assuming that BugReporter is a GRBugReporter is a layering
478 const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags =
479 cast<GRBugReporter>(BRC.getBugReporter()).
480 getEngine().getEagerlyAssumeTags();
482 const ProgramPointTag *tag = PS->getTag();
483 if (tag == tags.first)
484 return VisitTrueTest(cast<Expr>(PS->getStmt()), true,
486 if (tag == tags.second)
487 return VisitTrueTest(cast<Expr>(PS->getStmt()), false,
496 PathDiagnosticPiece *
497 ConditionBRVisitor::VisitTerminator(const Stmt *Term,
498 const ExplodedNode *N,
499 const CFGBlock *srcBlk,
500 const CFGBlock *dstBlk,
502 BugReporterContext &BRC) {
503 const Expr *Cond = 0;
505 switch (Term->getStmtClass()) {
508 case Stmt::IfStmtClass:
509 Cond = cast<IfStmt>(Term)->getCond();
511 case Stmt::ConditionalOperatorClass:
512 Cond = cast<ConditionalOperator>(Term)->getCond();
517 assert(srcBlk->succ_size() == 2);
518 const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk;
519 return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()),
520 tookTrue, BRC, R, N);
523 PathDiagnosticPiece *
524 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
526 BugReporterContext &BRC,
528 const ExplodedNode *N) {
530 const Expr *Ex = Cond;
533 Ex = Ex->IgnoreParens();
534 switch (Ex->getStmtClass()) {
537 case Stmt::BinaryOperatorClass:
538 return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC,
540 case Stmt::DeclRefExprClass:
541 return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC,
543 case Stmt::UnaryOperatorClass: {
544 const UnaryOperator *UO = cast<UnaryOperator>(Ex);
545 if (UO->getOpcode() == UO_LNot) {
546 tookTrue = !tookTrue;
547 Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext());
556 bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out,
557 BugReporterContext &BRC,
559 const ExplodedNode *N,
560 llvm::Optional<bool> &prunable) {
561 const Expr *OriginalExpr = Ex;
562 Ex = Ex->IgnoreParenCasts();
564 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) {
565 const bool quotes = isa<VarDecl>(DR->getDecl());
568 const LocationContext *LCtx = N->getLocationContext();
569 const ProgramState *state = N->getState().getPtr();
570 if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()),
571 LCtx).getAsRegion()) {
572 if (report.isInteresting(R))
575 const ProgramState *state = N->getState().getPtr();
576 SVal V = state->getSVal(R);
577 if (report.isInteresting(V))
582 Out << DR->getDecl()->getDeclName().getAsString();
588 if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) {
589 QualType OriginalTy = OriginalExpr->getType();
590 if (OriginalTy->isPointerType()) {
591 if (IL->getValue() == 0) {
596 else if (OriginalTy->isObjCObjectPointerType()) {
597 if (IL->getValue() == 0) {
603 Out << IL->getValue();
610 PathDiagnosticPiece *
611 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
612 const BinaryOperator *BExpr,
614 BugReporterContext &BRC,
616 const ExplodedNode *N) {
618 bool shouldInvert = false;
619 llvm::Optional<bool> shouldPrune;
621 SmallString<128> LhsString, RhsString;
623 llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString);
624 const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N,
626 const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N,
629 shouldInvert = !isVarLHS && isVarRHS;
632 BinaryOperator::Opcode Op = BExpr->getOpcode();
634 if (BinaryOperator::isAssignmentOp(Op)) {
635 // For assignment operators, all that we care about is that the LHS
636 // evaluates to "true" or "false".
637 return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue,
641 // For non-assignment operations, we require that we can understand
642 // both the LHS and RHS.
643 if (LhsString.empty() || RhsString.empty())
646 // Should we invert the strings if the LHS is not a variable name?
647 SmallString<256> buf;
648 llvm::raw_svector_ostream Out(buf);
649 Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is ";
651 // Do we need to invert the opcode?
655 case BO_LT: Op = BO_GT; break;
656 case BO_GT: Op = BO_LT; break;
657 case BO_LE: Op = BO_GE; break;
658 case BO_GE: Op = BO_LE; break;
663 case BO_EQ: Op = BO_NE; break;
664 case BO_NE: Op = BO_EQ; break;
665 case BO_LT: Op = BO_GE; break;
666 case BO_GT: Op = BO_LE; break;
667 case BO_LE: Op = BO_GT; break;
668 case BO_GE: Op = BO_LT; break;
678 Out << "not equal to ";
681 Out << BinaryOperator::getOpcodeStr(Op) << ' ';
685 Out << (shouldInvert ? LhsString : RhsString);
686 const LocationContext *LCtx = N->getLocationContext();
687 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
688 PathDiagnosticEventPiece *event =
689 new PathDiagnosticEventPiece(Loc, Out.str());
690 if (shouldPrune.hasValue())
691 event->setPrunable(shouldPrune.getValue());
695 PathDiagnosticPiece *
696 ConditionBRVisitor::VisitConditionVariable(StringRef LhsString,
697 const Expr *CondVarExpr,
699 BugReporterContext &BRC,
701 const ExplodedNode *N) {
702 SmallString<256> buf;
703 llvm::raw_svector_ostream Out(buf);
704 Out << "Assuming " << LhsString << " is ";
706 QualType Ty = CondVarExpr->getType();
708 if (Ty->isPointerType())
709 Out << (tookTrue ? "not null" : "null");
710 else if (Ty->isObjCObjectPointerType())
711 Out << (tookTrue ? "not nil" : "nil");
712 else if (Ty->isBooleanType())
713 Out << (tookTrue ? "true" : "false");
714 else if (Ty->isIntegerType())
715 Out << (tookTrue ? "non-zero" : "zero");
719 const LocationContext *LCtx = N->getLocationContext();
720 PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx);
721 PathDiagnosticEventPiece *event =
722 new PathDiagnosticEventPiece(Loc, Out.str());
724 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) {
725 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
726 const ProgramState *state = N->getState().getPtr();
727 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
728 if (report.isInteresting(R))
729 event->setPrunable(false);
737 PathDiagnosticPiece *
738 ConditionBRVisitor::VisitTrueTest(const Expr *Cond,
739 const DeclRefExpr *DR,
741 BugReporterContext &BRC,
743 const ExplodedNode *N) {
745 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
749 SmallString<256> Buf;
750 llvm::raw_svector_ostream Out(Buf);
753 VD->getDeclName().printName(Out);
756 QualType VDTy = VD->getType();
758 if (VDTy->isPointerType())
759 Out << (tookTrue ? "non-null" : "null");
760 else if (VDTy->isObjCObjectPointerType())
761 Out << (tookTrue ? "non-nil" : "nil");
762 else if (VDTy->isScalarType())
763 Out << (tookTrue ? "not equal to 0" : "0");
767 const LocationContext *LCtx = N->getLocationContext();
768 PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx);
769 PathDiagnosticEventPiece *event =
770 new PathDiagnosticEventPiece(Loc, Out.str());
772 const ProgramState *state = N->getState().getPtr();
773 if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) {
774 if (report.isInteresting(R))
775 event->setPrunable(false);
777 SVal V = state->getSVal(R);
778 if (report.isInteresting(V))
779 event->setPrunable(false);