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 | |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 95 | virtual void Profile(llvm::FoldingSetNodeID &ID) const { |
| 96 | static int tag = 0; |
| 97 | ID.AddPointer(&tag); |
| 98 | ID.AddPointer(R); |
| 99 | ID.Add(V); |
| 100 | } |
| 101 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 102 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 103 | const ExplodedNode *PrevN, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 104 | BugReporterContext& BRC) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 106 | if (satisfied) |
| 107 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
| 109 | if (!StoreSite) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 110 | const ExplodedNode *Node = N, *Last = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 112 | for ( ; Node ; Last = Node, Node = Node->getFirstPred()) { |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 114 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 115 | if (const PostStmt *P = Node->getLocationAs<PostStmt>()) |
| 116 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) |
| 117 | if (DS->getSingleDecl() == VR->getDecl()) { |
| 118 | Last = Node; |
| 119 | break; |
| 120 | } |
| 121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 123 | if (Node->getState()->getSVal(R) != V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 124 | break; |
| 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 (!Node || !Last) { |
| 128 | satisfied = true; |
| 129 | return NULL; |
| 130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 132 | StoreSite = Last; |
| 133 | } |
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 (StoreSite != N) |
| 136 | return NULL; |
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 | satisfied = true; |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 139 | llvm::SmallString<256> sbuf; |
| 140 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 142 | if (const PostStmt *PS = N->getLocationAs<PostStmt>()) { |
| 143 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 145 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 146 | os << "Variable '" << VR->getDecl()->getNameAsString() << "' "; |
| 147 | } |
| 148 | else |
| 149 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 151 | if (isa<loc::ConcreteInt>(V)) { |
| 152 | bool b = false; |
| 153 | ASTContext &C = BRC.getASTContext(); |
| 154 | if (R->isBoundable()) { |
| 155 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 156 | if (TR->getValueType(C)->isObjCObjectPointerType()) { |
| 157 | os << "initialized to nil"; |
| 158 | b = true; |
| 159 | } |
| 160 | } |
| 161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 163 | if (!b) |
| 164 | os << "initialized to a null pointer value"; |
| 165 | } |
| 166 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 167 | os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue(); |
| 168 | } |
| 169 | else if (V.isUndef()) { |
| 170 | if (isa<VarRegion>(R)) { |
| 171 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 172 | if (VD->getInit()) |
| 173 | os << "initialized to a garbage value"; |
| 174 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | os << "declared without an initial value"; |
| 176 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
| 181 | if (os.str().empty()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 182 | if (isa<loc::ConcreteInt>(V)) { |
| 183 | bool b = false; |
| 184 | ASTContext &C = BRC.getASTContext(); |
| 185 | if (R->isBoundable()) { |
| 186 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
| 187 | if (TR->getValueType(C)->isObjCObjectPointerType()) { |
| 188 | os << "nil object reference stored to "; |
| 189 | b = true; |
| 190 | } |
| 191 | } |
| 192 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 194 | if (!b) |
| 195 | os << "Null pointer value stored to "; |
| 196 | } |
| 197 | else if (V.isUndef()) { |
| 198 | os << "Uninitialized value stored to "; |
| 199 | } |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 200 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 201 | os << "The value " << cast<nonloc::ConcreteInt>(V).getValue() |
| 202 | << " is assigned to "; |
| 203 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 204 | else |
| 205 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 207 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 208 | os << '\'' << VR->getDecl()->getNameAsString() << '\''; |
| 209 | } |
| 210 | else |
| 211 | return NULL; |
| 212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 214 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 215 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 216 | ProgramPoint P = N->getLocation(); |
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 (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 219 | CFGBlock *BSrc = BE->getSrc(); |
| 220 | S = BSrc->getTerminatorCondition(); |
| 221 | } |
| 222 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 223 | S = PS->getStmt(); |
| 224 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 226 | if (!S) |
| 227 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 229 | // Construct a new PathDiagnosticPiece. |
| 230 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 231 | return new PathDiagnosticEventPiece(L, os.str()); |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | |
| 236 | static void registerFindLastStore(BugReporterContext& BRC, const MemRegion *R, |
| 237 | SVal V) { |
| 238 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 239 | } |
| 240 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 241 | class TrackConstraintBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 242 | DefinedSVal Constraint; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 243 | const bool Assumption; |
| 244 | bool isSatisfied; |
| 245 | public: |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 246 | TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 247 | : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 249 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 250 | static int tag = 0; |
| 251 | ID.AddPointer(&tag); |
| 252 | ID.AddBoolean(Assumption); |
| 253 | ID.Add(Constraint); |
| 254 | } |
| 255 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 256 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 257 | const ExplodedNode *PrevN, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 258 | BugReporterContext& BRC) { |
| 259 | if (isSatisfied) |
| 260 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 262 | // Check if in the previous state it was feasible for this constraint |
| 263 | // to *not* be true. |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 264 | if (PrevN->getState()->Assume(Constraint, !Assumption)) { |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 266 | isSatisfied = true; |
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 | // As a sanity check, make sure that the negation of the constraint |
| 269 | // was infeasible in the current state. If it is feasible, we somehow |
| 270 | // missed the transition point. |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 271 | if (N->getState()->Assume(Constraint, !Assumption)) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 272 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 274 | // We found the transition point for the constraint. We now need to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | // pretty-print the constraint. (work-in-progress) |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 276 | std::string sbuf; |
| 277 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 279 | if (isa<Loc>(Constraint)) { |
| 280 | os << "Assuming pointer value is "; |
| 281 | os << (Assumption ? "non-null" : "null"); |
| 282 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 284 | if (os.str().empty()) |
| 285 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 287 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 289 | ProgramPoint P = N->getLocation(); |
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 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 292 | CFGBlock *BSrc = BE->getSrc(); |
| 293 | S = BSrc->getTerminatorCondition(); |
| 294 | } |
| 295 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 296 | S = PS->getStmt(); |
| 297 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 299 | if (!S) |
| 300 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 302 | // Construct a new PathDiagnosticPiece. |
| 303 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 304 | return new PathDiagnosticEventPiece(L, os.str()); |
| 305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 307 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 309 | }; |
| 310 | } // end anonymous namespace |
| 311 | |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 312 | static void registerTrackConstraint(BugReporterContext& BRC, |
| 313 | DefinedSVal Constraint, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 314 | bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC, |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 319 | const void *data, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 320 | const ExplodedNode* N) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 322 | const Stmt *S = static_cast<const Stmt*>(data); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 324 | if (!S) |
| 325 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 327 | GRStateManager &StateMgr = BRC.getStateManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | const GRState *state = N->getState(); |
| 329 | |
| 330 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) { |
| 331 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 332 | const VarRegion *R = |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 333 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
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 | // What did we load? |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 336 | SVal V = state->getSVal(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | |
| 338 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 339 | || V.isUndef()) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 340 | ::registerFindLastStore(BRC, R, V); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 345 | SVal V = state->getSValAsScalarOrLoc(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 347 | // Uncomment this to find cases where we aren't properly getting the |
| 348 | // base value that was dereferenced. |
| 349 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 351 | // Is it a symbolic value? |
| 352 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
| 353 | const SubRegion *R = cast<SubRegion>(L->getRegion()); |
| 354 | while (R && !isa<SymbolicRegion>(R)) { |
| 355 | R = dyn_cast<SubRegion>(R->getSuperRegion()); |
| 356 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 358 | if (R) { |
| 359 | assert(isa<SymbolicRegion>(R)); |
| 360 | registerTrackConstraint(BRC, loc::MemRegionVal(R), false); |
| 361 | } |
| 362 | } |
| 363 | } |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 364 | |
| 365 | void clang::bugreporter::registerFindLastStore(BugReporterContext& BRC, |
| 366 | const void *data, |
| 367 | const ExplodedNode* N) { |
| 368 | |
| 369 | const MemRegion *R = static_cast<const MemRegion*>(data); |
| 370 | |
| 371 | if (!R) |
| 372 | return; |
| 373 | |
| 374 | const GRState *state = N->getState(); |
| 375 | SVal V = state->getSVal(R); |
| 376 | |
| 377 | if (V.isUnknown()) |
| 378 | return; |
| 379 | |
| 380 | BRC.addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 381 | } |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame^] | 382 | |
| 383 | |
| 384 | namespace { |
| 385 | class NilReceiverVisitor : public BugReporterVisitor { |
| 386 | public: |
| 387 | NilReceiverVisitor() {} |
| 388 | |
| 389 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 390 | static int x = 0; |
| 391 | ID.AddPointer(&x); |
| 392 | } |
| 393 | |
| 394 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 395 | const ExplodedNode *PrevN, |
| 396 | BugReporterContext& BRC) { |
| 397 | |
| 398 | const PostStmt *P = N->getLocationAs<PostStmt>(); |
| 399 | if (!P) |
| 400 | return 0; |
| 401 | const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>(); |
| 402 | if (!ME) |
| 403 | return 0; |
| 404 | const Expr *Receiver = ME->getReceiver(); |
| 405 | if (!Receiver) |
| 406 | return 0; |
| 407 | const GRState *state = N->getState(); |
| 408 | const SVal &V = state->getSVal(Receiver); |
| 409 | const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V); |
| 410 | if (!DV) |
| 411 | return 0; |
| 412 | state = state->Assume(*DV, true); |
| 413 | if (state) |
| 414 | return 0; |
| 415 | |
| 416 | // The receiver was nil, and hence the method was skipped. |
| 417 | // Register a BugReporterVisitor to issue a message telling us how |
| 418 | // the receiver was null. |
| 419 | bugreporter::registerTrackNullOrUndefValue(BRC, Receiver, N); |
| 420 | // Issue a message saying that the method was skipped. |
| 421 | PathDiagnosticLocation L(Receiver, BRC.getSourceManager()); |
| 422 | return new PathDiagnosticEventPiece(L, "No method actually called " |
| 423 | "because the receiver is nil"); |
| 424 | } |
| 425 | }; |
| 426 | } // end anonymous namespace |
| 427 | |
| 428 | void clang::bugreporter::registerNilReceiverVisitor(BugReporterContext &BRC) { |
| 429 | BRC.addVisitor(new NilReceiverVisitor()); |
| 430 | } |