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 | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame^] | 17 | #include "clang/Checker/PathSensitive/BugReporter.h" |
| 18 | #include "clang/Checker/PathDiagnostic.h" |
| 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()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +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 | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 116 | if (Node->getState()->getSVal(R) != V) |
| 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; |
| 132 | std::string sbuf; |
| 133 | llvm::raw_string_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)) { |
| 139 | os << "Variable '" << VR->getDecl()->getNameAsString() << "' "; |
| 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)) { |
| 201 | os << '\'' << VR->getDecl()->getNameAsString() << '\''; |
| 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 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 242 | PathDiagnosticPiece* VisitNode(const ExplodedNode *N, |
| 243 | const ExplodedNode *PrevN, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 244 | BugReporterContext& BRC) { |
| 245 | if (isSatisfied) |
| 246 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 248 | // Check if in the previous state it was feasible for this constraint |
| 249 | // to *not* be true. |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 250 | if (PrevN->getState()->Assume(Constraint, !Assumption)) { |
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 | isSatisfied = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 254 | // As a sanity check, make sure that the negation of the constraint |
| 255 | // was infeasible in the current state. If it is feasible, we somehow |
| 256 | // missed the transition point. |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 257 | if (N->getState()->Assume(Constraint, !Assumption)) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 258 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 260 | // We found the transition point for the constraint. We now need to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | // pretty-print the constraint. (work-in-progress) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 262 | std::string sbuf; |
| 263 | llvm::raw_string_ostream os(sbuf); |
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 | if (isa<Loc>(Constraint)) { |
| 266 | os << "Assuming pointer value is "; |
| 267 | os << (Assumption ? "non-null" : "null"); |
| 268 | } |
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 (os.str().empty()) |
| 271 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 273 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 275 | ProgramPoint P = N->getLocation(); |
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 (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 278 | CFGBlock *BSrc = BE->getSrc(); |
| 279 | S = BSrc->getTerminatorCondition(); |
| 280 | } |
| 281 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 282 | S = PS->getStmt(); |
| 283 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 285 | if (!S) |
| 286 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 288 | // Construct a new PathDiagnosticPiece. |
| 289 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 290 | return new PathDiagnosticEventPiece(L, os.str()); |
| 291 | } |
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 | 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 | }; |
| 296 | } // end anonymous namespace |
| 297 | |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 298 | static void registerTrackConstraint(BugReporterContext& BRC, |
| 299 | DefinedSVal Constraint, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 300 | bool Assumption) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | BRC.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void clang::bugreporter::registerTrackNullOrUndefValue(BugReporterContext& BRC, |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 305 | const void *data, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 306 | const ExplodedNode* N) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 308 | const Stmt *S = static_cast<const Stmt*>(data); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 310 | if (!S) |
| 311 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 313 | GRStateManager &StateMgr = BRC.getStateManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | const GRState *state = N->getState(); |
| 315 | |
| 316 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) { |
| 317 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 318 | const VarRegion *R = |
Ted Kremenek | d17da2b | 2009-08-21 22:28:32 +0000 | [diff] [blame] | 319 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 321 | // What did we load? |
| 322 | SVal V = state->getSVal(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
| 324 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 325 | || V.isUndef()) { |
| 326 | registerFindLastStore(BRC, R, V); |
| 327 | } |
| 328 | } |
| 329 | } |
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 | SVal V = state->getSValAsScalarOrLoc(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 333 | // Uncomment this to find cases where we aren't properly getting the |
| 334 | // base value that was dereferenced. |
| 335 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 337 | // Is it a symbolic value? |
| 338 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
| 339 | const SubRegion *R = cast<SubRegion>(L->getRegion()); |
| 340 | while (R && !isa<SymbolicRegion>(R)) { |
| 341 | R = dyn_cast<SubRegion>(R->getSuperRegion()); |
| 342 | } |
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 | if (R) { |
| 345 | assert(isa<SymbolicRegion>(R)); |
| 346 | registerTrackConstraint(BRC, loc::MemRegionVal(R), false); |
| 347 | } |
| 348 | } |
| 349 | } |