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" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 19 | #include "clang/Checker/PathSensitive/GRState.h" |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // Utility functions. |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 27 | const Stmt *clang::bugreporter::GetDerefExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 28 | // Pattern match for a few useful cases (do something smarter later): |
| 29 | // a[0], p->f, *p |
| 30 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 32 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) { |
| 33 | if (U->getOpcode() == UnaryOperator::Deref) |
| 34 | return U->getSubExpr()->IgnoreParenCasts(); |
| 35 | } |
| 36 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 37 | return ME->getBase()->IgnoreParenCasts(); |
| 38 | } |
| 39 | else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) { |
| 40 | // Retrieve the base for arrays since BasicStoreManager doesn't know how |
| 41 | // to reason about them. |
| 42 | return AE->getBase(); |
| 43 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
| 45 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | const Stmt* |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 49 | clang::bugreporter::GetReceiverExpr(const ExplodedNode *N){ |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 50 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 51 | if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) |
| 52 | return ME->getReceiver(); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | const Stmt* |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 57 | clang::bugreporter::GetDenomExpr(const ExplodedNode *N) { |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 58 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 59 | if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S)) |
| 60 | return BE->getRHS(); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | const Stmt* |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 65 | clang::bugreporter::GetCalleeExpr(const ExplodedNode *N) { |
Zhongxing Xu | d99f361 | 2009-09-02 08:10:35 +0000 | [diff] [blame] | 66 | // Callee is checked as a PreVisit to the CallExpr. |
| 67 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 68 | if (const CallExpr *CE = dyn_cast<CallExpr>(S)) |
| 69 | return CE->getCallee(); |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | const Stmt* |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 74 | clang::bugreporter::GetRetValExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 75 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 76 | if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S)) |
| 77 | return RS->getRetValue(); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | // Definitions for bug reporter visitors. |
| 83 | //===----------------------------------------------------------------------===// |
| 84 | |
| 85 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 86 | class FindLastStoreBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 87 | const MemRegion *R; |
| 88 | SVal V; |
| 89 | bool satisfied; |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 90 | const ExplodedNode *StoreSite; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 91 | public: |
| 92 | FindLastStoreBRVisitor(SVal v, const MemRegion *r) |
| 93 | : R(r), V(v), satisfied(false), StoreSite(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 106 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 107 | if (const PostStmt *P = Node->getLocationAs<PostStmt>()) |
| 108 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) |
| 109 | if (DS->getSingleDecl() == VR->getDecl()) { |
| 110 | Last = Node; |
| 111 | break; |
| 112 | } |
| 113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 115 | if (Node->getState()->getSVal(R) != V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 116 | break; |
| 117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 119 | if (!Node || !Last) { |
| 120 | satisfied = true; |
| 121 | return NULL; |
| 122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 123 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 124 | StoreSite = Last; |
| 125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 127 | if (StoreSite != N) |
| 128 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 130 | satisfied = true; |
| 131 | std::string sbuf; |
| 132 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 134 | if (const PostStmt *PS = N->getLocationAs<PostStmt>()) { |
| 135 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 137 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 138 | os << "Variable '" << VR->getDecl()->getNameAsString() << "' "; |
| 139 | } |
| 140 | else |
| 141 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 143 | if (isa<loc::ConcreteInt>(V)) { |
| 144 | bool b = false; |
| 145 | ASTContext &C = BRC.getASTContext(); |
| 146 | if (R->isBoundable()) { |
| 147 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 148 | if (TR->getValueType(C)->isObjCObjectPointerType()) { |
| 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; |
| 176 | ASTContext &C = BRC.getASTContext(); |
| 177 | if (R->isBoundable()) { |
| 178 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 179 | if (TR->getValueType(C)->isObjCObjectPointerType()) { |
| 180 | os << "nil object reference stored to "; |
| 181 | b = true; |
| 182 | } |
| 183 | } |
| 184 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 186 | if (!b) |
| 187 | os << "Null pointer value stored to "; |
| 188 | } |
| 189 | else if (V.isUndef()) { |
| 190 | os << "Uninitialized value stored to "; |
| 191 | } |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 192 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 193 | os << "The value " << cast<nonloc::ConcreteInt>(V).getValue() |
| 194 | << " is assigned to "; |
| 195 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 196 | else |
| 197 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 199 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 200 | os << '\'' << VR->getDecl()->getNameAsString() << '\''; |
| 201 | } |
| 202 | else |
| 203 | return NULL; |
| 204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 206 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 208 | ProgramPoint P = N->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 210 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 211 | CFGBlock *BSrc = BE->getSrc(); |
| 212 | S = BSrc->getTerminatorCondition(); |
| 213 | } |
| 214 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 215 | S = PS->getStmt(); |
| 216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 218 | if (!S) |
| 219 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 221 | // Construct a new PathDiagnosticPiece. |
| 222 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 223 | return new PathDiagnosticEventPiece(L, os.str()); |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | |
| 228 | static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R, |
| 229 | SVal V) { |
| 230 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 231 | } |
| 232 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 233 | class TrackConstraintBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 234 | DefinedSVal Constraint; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 235 | const bool Assumption; |
| 236 | bool isSatisfied; |
| 237 | public: |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 238 | TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 239 | : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 241 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 242 | const ExplodedNode *PrevN, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 243 | BugReporterContext& BRC) { |
| 244 | if (isSatisfied) |
| 245 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 247 | // Check if in the previous state it was feasible for this constraint |
| 248 | // to *not* be true. |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 249 | if (PrevN->getState()->Assume(Constraint, !Assumption)) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 250 | isSatisfied = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 252 | // As a sanity check, make sure that the negation of the constraint |
| 253 | // was infeasible in the current state. If it is feasible, we somehow |
| 254 | // missed the transition point. |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 255 | if (N->getState()->Assume(Constraint, !Assumption)) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 256 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 258 | // We found the transition point for the constraint. We now need to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | // pretty-print the constraint. (work-in-progress) |
Ted Kremenek | 2d46f4d | 2010-03-20 01:17:30 +0000 | [diff] [blame] | 260 | llvm::SmallString<256> sbuf; |
| 261 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 263 | if (isa<Loc>(Constraint)) { |
| 264 | os << "Assuming pointer value is "; |
| 265 | os << (Assumption ? "non-null" : "null"); |
| 266 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 268 | if (os.str().empty()) |
| 269 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 271 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 273 | ProgramPoint P = N->getLocation(); |
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 (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 276 | CFGBlock *BSrc = BE->getSrc(); |
| 277 | S = BSrc->getTerminatorCondition(); |
| 278 | } |
| 279 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 280 | S = PS->getStmt(); |
| 281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 283 | if (!S) |
| 284 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 286 | // Construct a new PathDiagnosticPiece. |
| 287 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 288 | return new PathDiagnosticEventPiece(L, os.str()); |
| 289 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 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 | }; |
| 294 | } // end anonymous namespace |
| 295 | |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 296 | static void registerTrackConstraint(BugReporterContext& BRC, |
| 297 | DefinedSVal Constraint, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 298 | bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC, |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 303 | const void *data, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 304 | const ExplodedNode* N) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 306 | const Stmt *S = static_cast<const Stmt*>(data); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 308 | if (!S) |
| 309 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 311 | GRStateManager &StateMgr = BRC.getStateManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | const GRState *state = N->getState(); |
| 313 | |
| 314 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) { |
| 315 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 316 | const VarRegion *R = |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 317 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 319 | // What did we load? |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 320 | SVal V = state->getSVal(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
| 322 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 323 | || V.isUndef()) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 324 | ::registerFindLastStore(BRC, R, V); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 329 | SVal V = state->getSValAsScalarOrLoc(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 331 | // Uncomment this to find cases where we aren't properly getting the |
| 332 | // base value that was dereferenced. |
| 333 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 335 | // Is it a symbolic value? |
| 336 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
| 337 | const SubRegion *R = cast<SubRegion>(L->getRegion()); |
| 338 | while (R && !isa<SymbolicRegion>(R)) { |
| 339 | R = dyn_cast<SubRegion>(R->getSuperRegion()); |
| 340 | } |
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 | if (R) { |
| 343 | assert(isa<SymbolicRegion>(R)); |
| 344 | registerTrackConstraint(BRC, loc::MemRegionVal(R), false); |
| 345 | } |
| 346 | } |
| 347 | } |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 348 | |
| 349 | void clang::bugreporter::registerFindLastStore(BugReporterContext& BRC, |
| 350 | const void *data, |
| 351 | const ExplodedNode* N) { |
| 352 | |
| 353 | const MemRegion *R = static_cast<const MemRegion*>(data); |
| 354 | |
| 355 | if (!R) |
| 356 | return; |
| 357 | |
| 358 | const GRState *state = N->getState(); |
| 359 | SVal V = state->getSVal(R); |
| 360 | |
| 361 | if (V.isUnknown()) |
| 362 | return; |
| 363 | |
| 364 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 365 | } |
Ted Kremenek | 2d46f4d | 2010-03-20 01:17:30 +0000 | [diff] [blame] | 366 | |
| 367 | namespace { |
| 368 | class NilReceiverVisitor : public BugReporterVisitor { |
| 369 | public: |
| 370 | NilReceiverVisitor() {} |
| 371 | |
| 372 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 373 | const ExplodedNode *PrevN, |
| 374 | BugReporterContext& BRC) { |
| 375 | |
| 376 | const PostStmt *P = N->getLocationAs<PostStmt>(); |
| 377 | if (!P) |
| 378 | return 0; |
| 379 | const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>(); |
| 380 | if (!ME) |
| 381 | return 0; |
| 382 | const Expr *Receiver = ME->getReceiver(); |
| 383 | if (!Receiver) |
| 384 | return 0; |
| 385 | const GRState *state = N->getState(); |
| 386 | const SVal &V = state->getSVal(Receiver); |
| 387 | const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V); |
| 388 | if (!DV) |
| 389 | return 0; |
| 390 | |
| 391 | state = state->Assume(*DV, true); |
| 392 | if (state) |
| 393 | return 0; |
| 394 | |
| 395 | // The receiver was nil, and hence the method was skipped. |
| 396 | // Register a BugReporterVisitor to issue a message telling us how |
| 397 | // the receiver was null. |
| 398 | bugreporter::registerTrackNullOrUndefValue(BRC, Receiver, N); |
| 399 | |
| 400 | //Issue a message saying that the method was skipped. |
| 401 | PathDiagnosticLocation L(Receiver, BRC.getSourceManager()); |
| 402 | return new PathDiagnosticEventPiece(L, "No method actually called " |
| 403 | "because the receiver is nil"); |
| 404 | } |
| 405 | }; |
| 406 | } // end anonymous namespace |
| 407 | |
| 408 | void clang::bugreporter::registerNilReceiverVisitor(BugReporterContext &BRC) { |
| 409 | BRC.addVisitor(new NilReceiverVisitor()); |
| 410 | } |