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