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 | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 18 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 24 | using namespace ento; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Utility functions. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 30 | const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 31 | // Pattern match for a few useful cases (do something smarter later): |
| 32 | // a[0], p->f, *p |
| 33 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 35 | if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 36 | if (U->getOpcode() == UO_Deref) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 37 | return U->getSubExpr()->IgnoreParenCasts(); |
| 38 | } |
| 39 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 40 | return ME->getBase()->IgnoreParenCasts(); |
| 41 | } |
| 42 | else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 49 | const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) { |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 50 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 51 | if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S)) |
| 52 | return BE->getRHS(); |
| 53 | return NULL; |
| 54 | } |
| 55 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 56 | const Stmt *bugreporter::GetCalleeExpr(const ExplodedNode *N) { |
Zhongxing Xu | d99f361 | 2009-09-02 08:10:35 +0000 | [diff] [blame] | 57 | // Callee is checked as a PreVisit to the CallExpr. |
| 58 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 59 | if (const CallExpr *CE = dyn_cast<CallExpr>(S)) |
| 60 | return CE->getCallee(); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 64 | const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 65 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 66 | if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S)) |
| 67 | return RS->getRetValue(); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | // Definitions for bug reporter visitors. |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | |
| 75 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 76 | class FindLastStoreBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 77 | const MemRegion *R; |
| 78 | SVal V; |
| 79 | bool satisfied; |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 80 | const ExplodedNode *StoreSite; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 81 | public: |
| 82 | FindLastStoreBRVisitor(SVal v, const MemRegion *r) |
| 83 | : R(r), V(v), satisfied(false), StoreSite(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 85 | virtual void Profile(llvm::FoldingSetNodeID &ID) const { |
| 86 | static int tag = 0; |
| 87 | ID.AddPointer(&tag); |
| 88 | ID.AddPointer(R); |
| 89 | ID.Add(V); |
| 90 | } |
| 91 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 92 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 93 | const ExplodedNode *PrevN, |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 94 | BugReporterContext &BRC, |
| 95 | BugReport &BR) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 97 | if (satisfied) |
| 98 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
| 100 | if (!StoreSite) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 101 | const ExplodedNode *Node = N, *Last = NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 103 | for ( ; Node ; Last = Node, Node = Node->getFirstPred()) { |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 105 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 106 | if (const PostStmt *P = Node->getLocationAs<PostStmt>()) |
| 107 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) |
| 108 | if (DS->getSingleDecl() == VR->getDecl()) { |
| 109 | Last = Node; |
| 110 | break; |
| 111 | } |
| 112 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 114 | if (Node->getState()->getSVal(R) != V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 115 | break; |
| 116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 118 | if (!Node || !Last) { |
| 119 | satisfied = true; |
| 120 | return NULL; |
| 121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 123 | StoreSite = Last; |
| 124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 126 | if (StoreSite != N) |
| 127 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 129 | satisfied = true; |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 130 | llvm::SmallString<256> sbuf; |
| 131 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 133 | if (const PostStmt *PS = N->getLocationAs<PostStmt>()) { |
| 134 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 136 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 137 | os << "Variable '" << VR->getDecl() << "' "; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 138 | } |
| 139 | else |
| 140 | return NULL; |
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 (isa<loc::ConcreteInt>(V)) { |
| 143 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 144 | if (R->isBoundable()) { |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 145 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 146 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 147 | os << "initialized to nil"; |
| 148 | b = true; |
| 149 | } |
| 150 | } |
| 151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 153 | if (!b) |
| 154 | os << "initialized to a null pointer value"; |
| 155 | } |
| 156 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 157 | os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue(); |
| 158 | } |
| 159 | else if (V.isUndef()) { |
| 160 | if (isa<VarRegion>(R)) { |
| 161 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 162 | if (VD->getInit()) |
| 163 | os << "initialized to a garbage value"; |
| 164 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | os << "declared without an initial value"; |
| 166 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
| 171 | if (os.str().empty()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 172 | if (isa<loc::ConcreteInt>(V)) { |
| 173 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 174 | if (R->isBoundable()) { |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 175 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 176 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 177 | os << "nil object reference stored to "; |
| 178 | b = true; |
| 179 | } |
| 180 | } |
| 181 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 183 | if (!b) |
| 184 | os << "Null pointer value stored to "; |
| 185 | } |
| 186 | else if (V.isUndef()) { |
| 187 | os << "Uninitialized value stored to "; |
| 188 | } |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 189 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 190 | os << "The value " << cast<nonloc::ConcreteInt>(V).getValue() |
| 191 | << " is assigned to "; |
| 192 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 193 | else |
| 194 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 196 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 197 | os << '\'' << VR->getDecl() << '\''; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 198 | } |
| 199 | else |
| 200 | return NULL; |
| 201 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 203 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 205 | ProgramPoint P = N->getLocation(); |
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 (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 208 | const CFGBlock *BSrc = BE->getSrc(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 209 | S = BSrc->getTerminatorCondition(); |
| 210 | } |
| 211 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 212 | S = PS->getStmt(); |
| 213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 215 | if (!S) |
| 216 | return NULL; |
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 | // Construct a new PathDiagnosticPiece. |
| 219 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 220 | return new PathDiagnosticEventPiece(L, os.str()); |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 225 | static void registerFindLastStore(BugReport &BR, const MemRegion *R, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 226 | SVal V) { |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 227 | BR.addVisitor(new FindLastStoreBRVisitor(V, R)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 230 | class TrackConstraintBRVisitor : public BugReporterVisitor { |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 231 | DefinedSVal Constraint; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 232 | const bool Assumption; |
| 233 | bool isSatisfied; |
| 234 | public: |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 235 | TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 236 | : Constraint(constraint), Assumption(assumption), isSatisfied(false) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 238 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 239 | static int tag = 0; |
| 240 | ID.AddPointer(&tag); |
| 241 | ID.AddBoolean(Assumption); |
| 242 | ID.Add(Constraint); |
| 243 | } |
| 244 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 245 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 246 | const ExplodedNode *PrevN, |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 247 | BugReporterContext &BRC, |
| 248 | BugReport &BR) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 249 | if (isSatisfied) |
| 250 | return NULL; |
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 | // Check if in the previous state it was feasible for this constraint |
| 253 | // to *not* be true. |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 254 | if (PrevN->getState()->assume(Constraint, !Assumption)) { |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 256 | isSatisfied = true; |
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 | // As a sanity check, make sure that the negation of the constraint |
| 259 | // was infeasible in the current state. If it is feasible, we somehow |
| 260 | // missed the transition point. |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 261 | if (N->getState()->assume(Constraint, !Assumption)) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 262 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 264 | // We found the transition point for the constraint. We now need to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | // pretty-print the constraint. (work-in-progress) |
Daniel Dunbar | 0f2c907 | 2010-03-20 04:28:39 +0000 | [diff] [blame] | 266 | std::string sbuf; |
| 267 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 269 | if (isa<Loc>(Constraint)) { |
| 270 | os << "Assuming pointer value is "; |
| 271 | os << (Assumption ? "non-null" : "null"); |
| 272 | } |
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 | if (os.str().empty()) |
| 275 | return NULL; |
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 | // FIXME: Refactor this into BugReporterContext. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | const Stmt *S = 0; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 279 | ProgramPoint P = N->getLocation(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 281 | if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
Zhongxing Xu | 03509ae | 2010-07-20 06:22:24 +0000 | [diff] [blame] | 282 | const CFGBlock *BSrc = BE->getSrc(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 283 | S = BSrc->getTerminatorCondition(); |
| 284 | } |
| 285 | else if (PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 286 | S = PS->getStmt(); |
| 287 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 289 | if (!S) |
| 290 | return NULL; |
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 | // Construct a new PathDiagnosticPiece. |
| 293 | PathDiagnosticLocation L(S, BRC.getSourceManager()); |
| 294 | return new PathDiagnosticEventPiece(L, os.str()); |
| 295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 297 | return NULL; |
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 | }; |
| 300 | } // end anonymous namespace |
| 301 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 302 | static void registerTrackConstraint(BugReport &BR, |
Ted Kremenek | 5b9bd21 | 2009-09-11 22:07:28 +0000 | [diff] [blame] | 303 | DefinedSVal Constraint, |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 304 | bool Assumption) { |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 305 | BR.addVisitor(new TrackConstraintBRVisitor(Constraint, Assumption)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 308 | void bugreporter::registerTrackNullOrUndefValue(BugReport &BR, |
| 309 | const void *data) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 311 | const Stmt *S = static_cast<const Stmt*>(data); |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 312 | const ExplodedNode *N = BR.getErrorNode(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 314 | if (!S) |
| 315 | return; |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 316 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 317 | ProgramStateManager &StateMgr = N->getState()->getStateManager(); |
| 318 | |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 319 | // Walk through nodes until we get one that matches the statement |
| 320 | // exactly. |
| 321 | while (N) { |
| 322 | const ProgramPoint &pp = N->getLocation(); |
| 323 | if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) { |
| 324 | if (ps->getStmt() == S) |
| 325 | break; |
| 326 | } |
| 327 | N = *N->pred_begin(); |
| 328 | } |
| 329 | |
| 330 | if (!N) |
| 331 | return; |
| 332 | |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 333 | const ProgramState *state = N->getState(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 335 | // Walk through lvalue-to-rvalue conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S)) { |
| 337 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 338 | const VarRegion *R = |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 339 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 341 | // What did we load? |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 342 | SVal V = state->getSVal(loc::MemRegionVal(R)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
| 344 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V) |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 345 | || V.isUndef()) { |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 346 | ::registerFindLastStore(BR, R, V); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 351 | SVal V = state->getSValAsScalarOrLoc(S); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 353 | // Uncomment this to find cases where we aren't properly getting the |
| 354 | // base value that was dereferenced. |
| 355 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 357 | // Is it a symbolic value? |
| 358 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
| 359 | const SubRegion *R = cast<SubRegion>(L->getRegion()); |
| 360 | while (R && !isa<SymbolicRegion>(R)) { |
| 361 | R = dyn_cast<SubRegion>(R->getSuperRegion()); |
| 362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 364 | if (R) { |
| 365 | assert(isa<SymbolicRegion>(R)); |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 366 | registerTrackConstraint(BR, loc::MemRegionVal(R), false); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 370 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 371 | void bugreporter::registerFindLastStore(BugReport &BR, |
| 372 | const void *data) { |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 373 | |
| 374 | const MemRegion *R = static_cast<const MemRegion*>(data); |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 375 | const ExplodedNode *N = BR.getErrorNode(); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 376 | |
| 377 | if (!R) |
| 378 | return; |
| 379 | |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 380 | const ProgramState *state = N->getState(); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 381 | SVal V = state->getSVal(R); |
| 382 | |
| 383 | if (V.isUnknown()) |
| 384 | return; |
| 385 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 386 | BR.addVisitor(new FindLastStoreBRVisitor(V, R)); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 387 | } |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 388 | |
| 389 | |
| 390 | namespace { |
| 391 | class NilReceiverVisitor : public BugReporterVisitor { |
| 392 | public: |
| 393 | NilReceiverVisitor() {} |
| 394 | |
| 395 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 396 | static int x = 0; |
| 397 | ID.AddPointer(&x); |
| 398 | } |
| 399 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 400 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 401 | const ExplodedNode *PrevN, |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 402 | BugReporterContext &BRC, |
| 403 | BugReport &BR) { |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 404 | |
| 405 | const PostStmt *P = N->getLocationAs<PostStmt>(); |
| 406 | if (!P) |
| 407 | return 0; |
| 408 | const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>(); |
| 409 | if (!ME) |
| 410 | return 0; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 411 | const Expr *Receiver = ME->getInstanceReceiver(); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 412 | if (!Receiver) |
| 413 | return 0; |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 414 | const ProgramState *state = N->getState(); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 415 | const SVal &V = state->getSVal(Receiver); |
| 416 | const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V); |
| 417 | if (!DV) |
| 418 | return 0; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 419 | state = state->assume(*DV, true); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 420 | if (state) |
| 421 | return 0; |
| 422 | |
| 423 | // The receiver was nil, and hence the method was skipped. |
| 424 | // Register a BugReporterVisitor to issue a message telling us how |
| 425 | // the receiver was null. |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 426 | bugreporter::registerTrackNullOrUndefValue(BR, Receiver); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 427 | // Issue a message saying that the method was skipped. |
| 428 | PathDiagnosticLocation L(Receiver, BRC.getSourceManager()); |
| 429 | return new PathDiagnosticEventPiece(L, "No method actually called " |
| 430 | "because the receiver is nil"); |
| 431 | } |
| 432 | }; |
| 433 | } // end anonymous namespace |
| 434 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 435 | void bugreporter::registerNilReceiverVisitor(BugReport &BR) { |
| 436 | BR.addVisitor(new NilReceiverVisitor()); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 437 | } |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 438 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 439 | // Registers every VarDecl inside a Stmt with a last store visitor. |
| 440 | void bugreporter::registerVarDeclsLastStore(BugReport &BR, |
| 441 | const void *stmt) { |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 442 | const Stmt *S = static_cast<const Stmt *>(stmt); |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 443 | const ExplodedNode *N = BR.getErrorNode(); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 444 | |
| 445 | std::deque<const Stmt *> WorkList; |
| 446 | |
| 447 | WorkList.push_back(S); |
| 448 | |
| 449 | while (!WorkList.empty()) { |
| 450 | const Stmt *Head = WorkList.front(); |
| 451 | WorkList.pop_front(); |
| 452 | |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 453 | const ProgramState *state = N->getState(); |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 454 | ProgramStateManager &StateMgr = state->getStateManager(); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 455 | |
| 456 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) { |
| 457 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 458 | const VarRegion *R = |
| 459 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
| 460 | |
| 461 | // What did we load? |
| 462 | SVal V = state->getSVal(S); |
| 463 | |
| 464 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) { |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 465 | ::registerFindLastStore(BR, R, V); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | for (Stmt::const_child_iterator I = Head->child_begin(); |
| 471 | I != Head->child_end(); ++I) |
| 472 | WorkList.push_back(*I); |
| 473 | } |
| 474 | } |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 475 | |
| 476 | //===----------------------------------------------------------------------===// |
| 477 | // Visitor that tries to report interesting diagnostics from conditions. |
| 478 | //===----------------------------------------------------------------------===// |
| 479 | |
| 480 | namespace { |
| 481 | class ConditionVisitor : public BugReporterVisitor { |
| 482 | public: |
| 483 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 484 | static int x = 0; |
| 485 | ID.AddPointer(&x); |
| 486 | } |
| 487 | |
| 488 | virtual PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
| 489 | const ExplodedNode *Prev, |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 490 | BugReporterContext &BRC, |
| 491 | BugReport &BR); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 492 | |
| 493 | PathDiagnosticPiece *VisitTerminator(const Stmt *Term, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 494 | const ProgramState *CurrentState, |
| 495 | const ProgramState *PrevState, |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 496 | const CFGBlock *srcBlk, |
| 497 | const CFGBlock *dstBlk, |
| 498 | BugReporterContext &BRC); |
| 499 | |
| 500 | PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, |
| 501 | bool tookTrue, |
| 502 | BugReporterContext &BRC); |
| 503 | |
| 504 | PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, |
| 505 | const DeclRefExpr *DR, |
| 506 | const bool tookTrue, |
| 507 | BugReporterContext &BRC); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 508 | |
| 509 | PathDiagnosticPiece *VisitTrueTest(const Expr *Cond, |
| 510 | const BinaryOperator *BExpr, |
| 511 | const bool tookTrue, |
| 512 | BugReporterContext &BRC); |
| 513 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 514 | bool patternMatch(const Expr *Ex, |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 515 | llvm::raw_ostream &Out, |
| 516 | BugReporterContext &BRC); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 517 | }; |
| 518 | } |
| 519 | |
| 520 | PathDiagnosticPiece *ConditionVisitor::VisitNode(const ExplodedNode *N, |
| 521 | const ExplodedNode *Prev, |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 522 | BugReporterContext &BRC, |
| 523 | BugReport &BR) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 524 | |
| 525 | const ProgramPoint &progPoint = N->getLocation(); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 526 | |
| 527 | const ProgramState *CurrentState = N->getState(); |
| 528 | const ProgramState *PrevState = Prev->getState(); |
| 529 | |
| 530 | // Compare the GDMs of the state, because that is where constraints |
| 531 | // are managed. Note that ensure that we only look at nodes that |
| 532 | // were generated by the analyzer engine proper, not checkers. |
| 533 | if (CurrentState->getGDM().getRoot() == |
| 534 | PrevState->getGDM().getRoot()) |
| 535 | return 0; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 536 | |
| 537 | // If an assumption was made on a branch, it should be caught |
| 538 | // here by looking at the state transition. |
| 539 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 540 | const CFGBlock *srcBlk = BE->getSrc(); |
| 541 | if (const Stmt *term = srcBlk->getTerminator()) |
| 542 | return VisitTerminator(term, CurrentState, PrevState, |
| 543 | srcBlk, BE->getDst(), BRC); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) { |
| 548 | // FIXME: Assuming that BugReporter is a GRBugReporter is a layering |
| 549 | // violation. |
| 550 | const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags = |
| 551 | cast<GRBugReporter>(BRC.getBugReporter()). |
| 552 | getEngine().getEagerlyAssumeTags(); |
| 553 | |
| 554 | const ProgramPointTag *tag = PS->getTag(); |
| 555 | if (tag == tags.first) |
| 556 | return VisitTrueTest(cast<Expr>(PS->getStmt()), true, BRC); |
| 557 | if (tag == tags.second) |
| 558 | return VisitTrueTest(cast<Expr>(PS->getStmt()), false, BRC); |
| 559 | |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | PathDiagnosticPiece * |
| 567 | ConditionVisitor::VisitTerminator(const Stmt *Term, |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 568 | const ProgramState *CurrentState, |
| 569 | const ProgramState *PrevState, |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 570 | const CFGBlock *srcBlk, |
| 571 | const CFGBlock *dstBlk, |
| 572 | BugReporterContext &BRC) { |
| 573 | |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 574 | const Expr *Cond = 0; |
| 575 | |
| 576 | switch (Term->getStmtClass()) { |
| 577 | default: |
| 578 | return 0; |
| 579 | case Stmt::IfStmtClass: |
| 580 | Cond = cast<IfStmt>(Term)->getCond(); |
| 581 | break; |
| 582 | case Stmt::ConditionalOperatorClass: |
| 583 | Cond = cast<ConditionalOperator>(Term)->getCond(); |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | assert(Cond); |
| 588 | assert(srcBlk->succ_size() == 2); |
| 589 | const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk; |
| 590 | return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()), |
| 591 | tookTrue, BRC); |
| 592 | } |
| 593 | |
| 594 | PathDiagnosticPiece * |
| 595 | ConditionVisitor::VisitTrueTest(const Expr *Cond, |
| 596 | bool tookTrue, |
| 597 | BugReporterContext &BRC) { |
| 598 | |
| 599 | const Expr *Ex = Cond; |
| 600 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 601 | while (true) { |
| 602 | Ex = Ex->IgnoreParens(); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 603 | switch (Ex->getStmtClass()) { |
| 604 | default: |
| 605 | return 0; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 606 | case Stmt::BinaryOperatorClass: |
| 607 | return VisitTrueTest(Cond, cast<BinaryOperator>(Cond), tookTrue, BRC); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 608 | case Stmt::DeclRefExprClass: |
| 609 | return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC); |
| 610 | case Stmt::UnaryOperatorClass: { |
| 611 | const UnaryOperator *UO = cast<UnaryOperator>(Ex); |
| 612 | if (UO->getOpcode() == UO_LNot) { |
| 613 | tookTrue = !tookTrue; |
| 614 | Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext()); |
| 615 | continue; |
| 616 | } |
| 617 | return 0; |
| 618 | } |
| 619 | } |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 623 | bool ConditionVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out, |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 624 | BugReporterContext &BRC) { |
| 625 | const Expr *OriginalExpr = Ex; |
| 626 | Ex = Ex->IgnoreParenCasts(); |
| 627 | |
| 628 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) { |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 629 | const bool quotes = isa<VarDecl>(DR->getDecl()); |
| 630 | if (quotes) |
| 631 | Out << '\''; |
| 632 | Out << DR->getDecl()->getDeclName().getAsString(); |
| 633 | if (quotes) |
| 634 | Out << '\''; |
| 635 | return quotes; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) { |
| 639 | QualType OriginalTy = OriginalExpr->getType(); |
| 640 | if (OriginalTy->isPointerType()) { |
| 641 | if (IL->getValue() == 0) { |
| 642 | Out << "null"; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 643 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | else if (OriginalTy->isObjCObjectPointerType()) { |
| 647 | if (IL->getValue() == 0) { |
| 648 | Out << "nil"; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 649 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | Out << IL->getValue(); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 654 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 655 | } |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 656 | |
| 657 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | PathDiagnosticPiece * |
| 661 | ConditionVisitor::VisitTrueTest(const Expr *Cond, |
| 662 | const BinaryOperator *BExpr, |
| 663 | const bool tookTrue, |
| 664 | BugReporterContext &BRC) { |
| 665 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 666 | bool shouldInvert = false; |
| 667 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 668 | llvm::SmallString<128> LhsString, RhsString; |
| 669 | { |
| 670 | llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 671 | const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC); |
| 672 | const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC); |
| 673 | |
| 674 | shouldInvert = !isVarLHS && isVarRHS; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | if (LhsString.empty() || RhsString.empty()) |
| 678 | return 0; |
| 679 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 680 | // Should we invert the strings if the LHS is not a variable name? |
| 681 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 682 | llvm::SmallString<256> buf; |
| 683 | llvm::raw_svector_ostream Out(buf); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 684 | Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is "; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 685 | |
| 686 | // Do we need to invert the opcode? |
| 687 | BinaryOperator::Opcode Op = BExpr->getOpcode(); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 688 | |
| 689 | if (shouldInvert) |
| 690 | switch (Op) { |
| 691 | default: break; |
| 692 | case BO_LT: Op = BO_GT; break; |
| 693 | case BO_GT: Op = BO_LT; break; |
| 694 | case BO_LE: Op = BO_GE; break; |
| 695 | case BO_GE: Op = BO_LE; break; |
| 696 | } |
| 697 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 698 | if (!tookTrue) |
| 699 | switch (Op) { |
| 700 | case BO_EQ: Op = BO_NE; break; |
| 701 | case BO_NE: Op = BO_EQ; break; |
| 702 | case BO_LT: Op = BO_GE; break; |
| 703 | case BO_GT: Op = BO_LE; break; |
| 704 | case BO_LE: Op = BO_GT; break; |
Ted Kremenek | 4ee7c9c | 2011-08-16 03:44:38 +0000 | [diff] [blame] | 705 | case BO_GE: Op = BO_LT; break; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 706 | default: |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | switch (BExpr->getOpcode()) { |
| 711 | case BO_EQ: |
| 712 | Out << "equal to "; |
| 713 | break; |
| 714 | case BO_NE: |
| 715 | Out << "not equal to "; |
| 716 | break; |
| 717 | default: |
| 718 | Out << BinaryOperator::getOpcodeStr(Op) << ' '; |
| 719 | break; |
| 720 | } |
| 721 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 722 | Out << (shouldInvert ? LhsString : RhsString); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 723 | |
| 724 | PathDiagnosticLocation Loc(Cond, BRC.getSourceManager()); |
| 725 | return new PathDiagnosticEventPiece(Loc, Out.str()); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | PathDiagnosticPiece * |
| 729 | ConditionVisitor::VisitTrueTest(const Expr *Cond, |
| 730 | const DeclRefExpr *DR, |
| 731 | const bool tookTrue, |
| 732 | BugReporterContext &BRC) { |
| 733 | |
| 734 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 735 | if (!VD) |
| 736 | return 0; |
| 737 | |
| 738 | llvm::SmallString<256> Buf; |
| 739 | llvm::raw_svector_ostream Out(Buf); |
| 740 | |
| 741 | Out << "Assuming '"; |
| 742 | VD->getDeclName().printName(Out); |
| 743 | Out << "' is "; |
| 744 | |
| 745 | QualType VDTy = VD->getType(); |
| 746 | |
| 747 | if (VDTy->isPointerType()) |
| 748 | Out << (tookTrue ? "non-null" : "null"); |
| 749 | else if (VDTy->isObjCObjectPointerType()) |
| 750 | Out << (tookTrue ? "non-nil" : "nil"); |
| 751 | else if (VDTy->isScalarType()) |
| 752 | Out << (tookTrue ? "not equal to 0" : "0"); |
| 753 | else |
| 754 | return 0; |
| 755 | |
| 756 | PathDiagnosticLocation Loc(Cond, BRC.getSourceManager()); |
| 757 | return new PathDiagnosticEventPiece(Loc, Out.str()); |
| 758 | } |
| 759 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame^] | 760 | void bugreporter::registerConditionVisitor(BugReport &BR) { |
| 761 | BR.addVisitor(new ConditionVisitor()); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 762 | } |
| 763 | |