Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 1 | // BugReporterVisitors.cpp - Helpers for reporting bugs -----------*- C++ -*--// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a set of BugReporter "visitors" which can be used to |
| 11 | // enhance the diagnostics reported for a bug. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/Expr.h" |
| 16 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 6b67630 | 2010-01-25 17:10:22 +0000 | [diff] [blame] | 17 | #include "clang/Checker/BugReporter/BugReporter.h" |
| 18 | #include "clang/Checker/BugReporter/PathDiagnostic.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 19 | #include "clang/Checker/PathSensitive/ExplodedGraph.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 20 | #include "clang/Checker/PathSensitive/GRState.h" |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Utility functions. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 28 | const Stmt *clang::bugreporter::GetDerefExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 29 | // Pattern match for a few useful cases (do something smarter later): |
| 30 | // a[0], p->f, *p |
| 31 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 33 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 34 | if (U->getOpcode() == UO_Deref) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 35 | return U->getSubExpr()->IgnoreParenCasts(); |
| 36 | } |
| 37 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 38 | return ME->getBase()->IgnoreParenCasts(); |
| 39 | } |
| 40 | else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) { |
| 41 | // Retrieve the base for arrays since BasicStoreManager doesn't know how |
| 42 | // to reason about them. |
| 43 | return AE->getBase(); |
| 44 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
| 46 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | const Stmt* |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 50 | clang::bugreporter::GetDenomExpr(const ExplodedNode *N) { |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 51 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 52 | if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S)) |
| 53 | return BE->getRHS(); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | const Stmt* |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 58 | clang::bugreporter::GetCalleeExpr(const ExplodedNode *N) { |
Zhongxing Xu | d99f361 | 2009-09-02 08:10:35 +0000 | [diff] [blame] | 59 | // Callee is checked as a PreVisit to the CallExpr. |
| 60 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 61 | if (const CallExpr *CE = dyn_cast<CallExpr>(S)) |
| 62 | return CE->getCallee(); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | const Stmt* |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 67 | clang::bugreporter::GetRetValExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 68 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 69 | if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S)) |
| 70 | return RS->getRetValue(); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | // Definitions for bug reporter visitors. |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | |
| 78 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 79 | class FindLastStoreBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 80 | const MemRegion *R; |
| 81 | SVal V; |
| 82 | bool satisfied; |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 83 | const ExplodedNode *StoreSite; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 84 | public: |
| 85 | FindLastStoreBRVisitor(SVal v, const MemRegion *r) |
| 86 | : R(r), V(v), satisfied(false), StoreSite(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 88 | virtual void Profile(llvm::FoldingSetNodeID &ID) const { |
| 89 | static int tag = 0; |
| 90 | ID.AddPointer(&tag); |
| 91 | ID.AddPointer(R); |
| 92 | ID.Add(V); |
| 93 | } |
| 94 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 95 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 96 | const ExplodedNode *PrevN, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 97 | BugReporterContext& BRC) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 99 | if (satisfied) |
| 100 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
| 102 | if (!StoreSite) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 103 | const ExplodedNode *Node = N, *Last = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 105 | for ( ; Node ; Last = Node, Node = Node->getFirstPred()) { |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 106 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 107 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 108 | if (const PostStmt *P = Node->getLocationAs<PostStmt>()) |
| 109 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) |
| 110 | if (DS->getSingleDecl() == VR->getDecl()) { |
| 111 | Last = Node; |
| 112 | break; |
| 113 | } |
| 114 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 116 | if (Node->getState()->getSVal(R) != V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 117 | break; |
| 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 120 | if (!Node || !Last) { |
| 121 | satisfied = true; |
| 122 | return NULL; |
| 123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 125 | StoreSite = Last; |
| 126 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 128 | if (StoreSite != N) |
| 129 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 131 | satisfied = true; |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 132 | llvm::SmallString<256> sbuf; |
| 133 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 135 | if (const PostStmt *PS = N->getLocationAs<PostStmt>()) { |
| 136 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 138 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 139 | os << "Variable '" << VR->getDecl() << "' "; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 140 | } |
| 141 | else |
| 142 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 144 | if (isa<loc::ConcreteInt>(V)) { |
| 145 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 146 | if (R->isBoundable()) { |
| 147 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 148 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 149 | os << "initialized to nil"; |
| 150 | b = true; |
| 151 | } |
| 152 | } |
| 153 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 155 | if (!b) |
| 156 | os << "initialized to a null pointer value"; |
| 157 | } |
| 158 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 159 | os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue(); |
| 160 | } |
| 161 | else if (V.isUndef()) { |
| 162 | if (isa<VarRegion>(R)) { |
| 163 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 164 | if (VD->getInit()) |
| 165 | os << "initialized to a garbage value"; |
| 166 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | os << "declared without an initial value"; |
| 168 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
| 173 | if (os.str().empty()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 174 | if (isa<loc::ConcreteInt>(V)) { |
| 175 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 176 | if (R->isBoundable()) { |
| 177 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 178 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 179 | os << "nil object reference stored to "; |
| 180 | b = true; |
| 181 | } |
| 182 | } |
| 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 185 | if (!b) |
| 186 | os << "Null pointer value stored to "; |
| 187 | } |
| 188 | else if (V.isUndef()) { |
| 189 | os << "Uninitialized value stored to "; |
| 190 | } |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 191 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 192 | os << "The value " << cast<nonloc::ConcreteInt>(V).getValue() |
| 193 | << " is assigned to "; |
| 194 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 195 | else |
| 196 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 198 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 199 | os << '\'' << VR->getDecl() << '\''; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 200 | } |
| 201 | else |
| 202 | return NULL; |
| 203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 205 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 207 | ProgramPoint P = N->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 209 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 210 | const CFGBlock *BSrc = BE->getSrc(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 211 | S = BSrc->getTerminatorCondition(); |
| 212 | } |
| 213 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 214 | S = PS->getStmt(); |
| 215 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 217 | if (!S) |
| 218 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 220 | // Construct a new PathDiagnosticPiece. |
| 221 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 222 | return new PathDiagnosticEventPiece(L, os.str()); |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | |
| 227 | static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R, |
| 228 | SVal V) { |
| 229 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 230 | } |
| 231 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 232 | class TrackConstraintBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 233 | DefinedSVal Constraint; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 234 | const bool Assumption; |
| 235 | bool isSatisfied; |
| 236 | public: |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 237 | TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 238 | : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 240 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 241 | static int tag = 0; |
| 242 | ID.AddPointer(&tag); |
| 243 | ID.AddBoolean(Assumption); |
| 244 | ID.Add(Constraint); |
| 245 | } |
| 246 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 247 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 248 | const ExplodedNode *PrevN, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 249 | BugReporterContext& BRC) { |
| 250 | if (isSatisfied) |
| 251 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 253 | // Check if in the previous state it was feasible for this constraint |
| 254 | // to *not* be true. |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 255 | if (PrevN->getState()->Assume(Constraint, !Assumption)) { |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 256 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 257 | isSatisfied = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 259 | // As a sanity check, make sure that the negation of the constraint |
| 260 | // was infeasible in the current state. If it is feasible, we somehow |
| 261 | // missed the transition point. |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 262 | if (N->getState()->Assume(Constraint, !Assumption)) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 263 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 265 | // We found the transition point for the constraint. We now need to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | // pretty-print the constraint. (work-in-progress) |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 267 | std::string sbuf; |
| 268 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 270 | if (isa<Loc>(Constraint)) { |
| 271 | os << "Assuming pointer value is "; |
| 272 | os << (Assumption ? "non-null" : "null"); |
| 273 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 275 | if (os.str().empty()) |
| 276 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 278 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 280 | ProgramPoint P = N->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 282 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 283 | const CFGBlock *BSrc = BE->getSrc(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 284 | S = BSrc->getTerminatorCondition(); |
| 285 | } |
| 286 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 287 | S = PS->getStmt(); |
| 288 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 290 | if (!S) |
| 291 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 293 | // Construct a new PathDiagnosticPiece. |
| 294 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 295 | return new PathDiagnosticEventPiece(L, os.str()); |
| 296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 298 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 300 | }; |
| 301 | } // end anonymous namespace |
| 302 | |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 303 | static void registerTrackConstraint(BugReporterContext& BRC, |
| 304 | DefinedSVal Constraint, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 305 | bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC, |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 310 | const void *data, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 311 | const ExplodedNode* N) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 313 | const Stmt *S = static_cast<const Stmt*>(data); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 315 | if (!S) |
| 316 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 318 | GRStateManager &StateMgr = BRC.getStateManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | const GRState *state = N->getState(); |
| 320 | |
| 321 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) { |
| 322 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 323 | const VarRegion *R = |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 324 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 326 | // What did we load? |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 327 | SVal V = state->getSVal(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
| 329 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 330 | || V.isUndef()) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 331 | ::registerFindLastStore(BRC, R, V); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 335 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 336 | SVal V = state->getSValAsScalarOrLoc(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 338 | // Uncomment this to find cases where we aren't properly getting the |
| 339 | // base value that was dereferenced. |
| 340 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 342 | // Is it a symbolic value? |
| 343 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
| 344 | const SubRegion *R = cast<SubRegion>(L->getRegion()); |
| 345 | while (R && !isa<SymbolicRegion>(R)) { |
| 346 | R = dyn_cast<SubRegion>(R->getSuperRegion()); |
| 347 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 349 | if (R) { |
| 350 | assert(isa<SymbolicRegion>(R)); |
| 351 | registerTrackConstraint(BRC, loc::MemRegionVal(R), false); |
| 352 | } |
| 353 | } |
| 354 | } |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 355 | |
| 356 | void clang::bugreporter::registerFindLastStore(BugReporterContext& BRC, |
| 357 | const void *data, |
| 358 | const ExplodedNode* N) { |
| 359 | |
| 360 | const MemRegion *R = static_cast<const MemRegion*>(data); |
| 361 | |
| 362 | if (!R) |
| 363 | return; |
| 364 | |
| 365 | const GRState *state = N->getState(); |
| 366 | SVal V = state->getSVal(R); |
| 367 | |
| 368 | if (V.isUnknown()) |
| 369 | return; |
| 370 | |
| 371 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 372 | } |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 373 | |
| 374 | |
| 375 | namespace { |
| 376 | class NilReceiverVisitor : public BugReporterVisitor { |
| 377 | public: |
| 378 | NilReceiverVisitor() {} |
| 379 | |
| 380 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 381 | static int x = 0; |
| 382 | ID.AddPointer(&x); |
| 383 | } |
| 384 | |
| 385 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 386 | const ExplodedNode *PrevN, |
| 387 | BugReporterContext& BRC) { |
| 388 | |
| 389 | const PostStmt *P = N->getLocationAs<PostStmt>(); |
| 390 | if (!P) |
| 391 | return 0; |
| 392 | const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>(); |
| 393 | if (!ME) |
| 394 | return 0; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 395 | const Expr *Receiver = ME->getInstanceReceiver(); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 396 | if (!Receiver) |
| 397 | return 0; |
| 398 | const GRState *state = N->getState(); |
| 399 | const SVal &V = state->getSVal(Receiver); |
| 400 | const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V); |
| 401 | if (!DV) |
| 402 | return 0; |
| 403 | state = state->Assume(*DV, true); |
| 404 | if (state) |
| 405 | return 0; |
| 406 | |
| 407 | // The receiver was nil, and hence the method was skipped. |
| 408 | // Register a BugReporterVisitor to issue a message telling us how |
| 409 | // the receiver was null. |
| 410 | bugreporter::registerTrackNullOrUndefValue(BRC, Receiver, N); |
| 411 | // Issue a message saying that the method was skipped. |
| 412 | PathDiagnosticLocation L(Receiver, BRC.getSourceManager()); |
| 413 | return new PathDiagnosticEventPiece(L, "No method actually called " |
| 414 | "because the receiver is nil"); |
| 415 | } |
| 416 | }; |
| 417 | } // end anonymous namespace |
| 418 | |
| 419 | void clang::bugreporter::registerNilReceiverVisitor(BugReporterContext &BRC) { |
| 420 | BRC.addVisitor(new NilReceiverVisitor()); |
| 421 | } |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame^] | 422 | |
| 423 | // Registers every VarDecl inside a Stmt with a last store vistor. |
| 424 | void clang::bugreporter::registerVarDeclsLastStore(BugReporterContext &BRC, |
| 425 | const void *stmt, |
| 426 | const ExplodedNode *N) { |
| 427 | const Stmt *S = static_cast<const Stmt *>(stmt); |
| 428 | |
| 429 | std::deque<const Stmt *> WorkList; |
| 430 | |
| 431 | WorkList.push_back(S); |
| 432 | |
| 433 | while (!WorkList.empty()) { |
| 434 | const Stmt *Head = WorkList.front(); |
| 435 | WorkList.pop_front(); |
| 436 | |
| 437 | GRStateManager &StateMgr = BRC.getStateManager(); |
| 438 | const GRState *state = N->getState(); |
| 439 | |
| 440 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) { |
| 441 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 442 | const VarRegion *R = |
| 443 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
| 444 | |
| 445 | // What did we load? |
| 446 | SVal V = state->getSVal(S); |
| 447 | |
| 448 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) { |
| 449 | ::registerFindLastStore(BRC, R, V); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | for (Stmt::const_child_iterator I = Head->child_begin(); |
| 455 | I != Head->child_end(); ++I) |
| 456 | WorkList.push_back(*I); |
| 457 | } |
| 458 | } |