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