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 | //===----------------------------------------------------------------------===// |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitor.h" |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 15 | |
| 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 19 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
| 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 26 | using namespace ento; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 27 | |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | // Utility functions. |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 32 | const Stmt *bugreporter::GetDerefExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 33 | // Pattern match for a few useful cases (do something smarter later): |
| 34 | // a[0], p->f, *p |
Jordan Rose | 3682f1e | 2012-08-25 01:06:23 +0000 | [diff] [blame] | 35 | const PostStmt *Loc = N->getLocationAs<PostStmt>(); |
| 36 | if (!Loc) |
| 37 | return 0; |
| 38 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 39 | const Expr *S = dyn_cast<Expr>(Loc->getStmt()); |
| 40 | if (!S) |
| 41 | return 0; |
| 42 | S = S->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | 11abcec | 2012-05-02 00:31:29 +0000 | [diff] [blame] | 44 | while (true) { |
| 45 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) { |
| 46 | assert(B->isAssignmentOp()); |
| 47 | S = B->getLHS()->IgnoreParenCasts(); |
| 48 | continue; |
| 49 | } |
| 50 | else if (const UnaryOperator *U = dyn_cast<UnaryOperator>(S)) { |
| 51 | if (U->getOpcode() == UO_Deref) |
| 52 | return U->getSubExpr()->IgnoreParenCasts(); |
| 53 | } |
| 54 | else if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) { |
| 55 | return ME->getBase()->IgnoreParenCasts(); |
| 56 | } |
| 57 | else if (const ArraySubscriptExpr *AE = dyn_cast<ArraySubscriptExpr>(S)) { |
| 58 | return AE->getBase(); |
| 59 | } |
| 60 | break; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 61 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
| 63 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 66 | const Stmt *bugreporter::GetDenomExpr(const ExplodedNode *N) { |
Zhongxing Xu | 6403b57 | 2009-09-02 13:26:26 +0000 | [diff] [blame] | 67 | const Stmt *S = N->getLocationAs<PreStmt>()->getStmt(); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 68 | if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S)) |
| 69 | return BE->getRHS(); |
| 70 | return NULL; |
| 71 | } |
| 72 | |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 73 | const Stmt *bugreporter::GetRetValExpr(const ExplodedNode *N) { |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 74 | const Stmt *S = N->getLocationAs<PostStmt>()->getStmt(); |
| 75 | if (const ReturnStmt *RS = dyn_cast<ReturnStmt>(S)) |
| 76 | return RS->getRetValue(); |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | // Definitions for bug reporter visitors. |
| 82 | //===----------------------------------------------------------------------===// |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 83 | |
| 84 | PathDiagnosticPiece* |
| 85 | BugReporterVisitor::getEndPath(BugReporterContext &BRC, |
| 86 | const ExplodedNode *EndPathNode, |
| 87 | BugReport &BR) { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | PathDiagnosticPiece* |
| 92 | BugReporterVisitor::getDefaultEndPath(BugReporterContext &BRC, |
| 93 | const ExplodedNode *EndPathNode, |
| 94 | BugReport &BR) { |
Anna Zaks | 5a0917d | 2012-02-16 03:41:01 +0000 | [diff] [blame] | 95 | PathDiagnosticLocation L = |
| 96 | PathDiagnosticLocation::createEndOfPath(EndPathNode,BRC.getSourceManager()); |
Anna Zaks | 23f395e | 2011-08-20 01:27:22 +0000 | [diff] [blame] | 97 | |
| 98 | BugReport::ranges_iterator Beg, End; |
| 99 | llvm::tie(Beg, End) = BR.getRanges(); |
| 100 | |
| 101 | // Only add the statement itself as a range if we didn't specify any |
| 102 | // special ranges for this report. |
| 103 | PathDiagnosticPiece *P = new PathDiagnosticEventPiece(L, |
| 104 | BR.getDescription(), |
| 105 | Beg == End); |
| 106 | for (; Beg != End; ++Beg) |
| 107 | P->addRange(*Beg); |
| 108 | |
| 109 | return P; |
| 110 | } |
| 111 | |
| 112 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 113 | void FindLastStoreBRVisitor ::Profile(llvm::FoldingSetNodeID &ID) const { |
| 114 | static int tag = 0; |
| 115 | ID.AddPointer(&tag); |
| 116 | ID.AddPointer(R); |
| 117 | ID.Add(V); |
| 118 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 119 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 120 | PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N, |
| 121 | const ExplodedNode *PrevN, |
| 122 | BugReporterContext &BRC, |
| 123 | BugReport &BR) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 125 | if (satisfied) |
| 126 | return NULL; |
| 127 | |
| 128 | if (!StoreSite) { |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 129 | // Make sure the region is actually bound to value V here. |
| 130 | // This is necessary because the region may not actually be live at the |
| 131 | // report's error node. |
| 132 | if (N->getState()->getSVal(R) != V) |
| 133 | return NULL; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 134 | |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 135 | const ExplodedNode *Node = N, *Last = N; |
| 136 | |
| 137 | // Now look for the store of V. |
Ted Kremenek | d2e7090 | 2012-01-25 22:18:04 +0000 | [diff] [blame] | 138 | for ( ; Node ; Node = Node->getFirstPred()) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 139 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 140 | if (const PostStmt *P = Node->getLocationAs<PostStmt>()) |
| 141 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) |
| 142 | if (DS->getSingleDecl() == VR->getDecl()) { |
Ted Kremenek | d2e7090 | 2012-01-25 22:18:04 +0000 | [diff] [blame] | 143 | // Record the last seen initialization point. |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 144 | Last = Node; |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
Ted Kremenek | d2e7090 | 2012-01-25 22:18:04 +0000 | [diff] [blame] | 149 | // Does the region still bind to value V? If not, we are done |
| 150 | // looking for store sites. |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 151 | if (Node->getState()->getSVal(R) != V) |
| 152 | break; |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 153 | |
| 154 | Last = Node; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 157 | if (!Node) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 158 | satisfied = true; |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | StoreSite = Last; |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 165 | if (StoreSite != N) |
| 166 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 168 | satisfied = true; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 169 | SmallString<256> sbuf; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 170 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 172 | if (const PostStmt *PS = N->getLocationAs<PostStmt>()) { |
| 173 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 175 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 176 | os << "Variable '" << *VR->getDecl() << "' "; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 177 | } |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 178 | else |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 179 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 181 | if (isa<loc::ConcreteInt>(V)) { |
| 182 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 183 | if (R->isBoundable()) { |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 184 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 185 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 186 | os << "initialized to nil"; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 187 | b = true; |
| 188 | } |
| 189 | } |
| 190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 192 | if (!b) |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 193 | os << "initialized to a null pointer value"; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 194 | } |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 195 | else if (isa<nonloc::ConcreteInt>(V)) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 196 | os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue(); |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 197 | } |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 198 | else if (V.isUndef()) { |
| 199 | if (isa<VarRegion>(R)) { |
| 200 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 201 | if (VD->getInit()) |
| 202 | os << "initialized to a garbage value"; |
| 203 | else |
| 204 | os << "declared without an initial value"; |
| 205 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 206 | } |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 207 | else { |
| 208 | os << "initialized here"; |
| 209 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 210 | } |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if (os.str().empty()) { |
| 214 | if (isa<loc::ConcreteInt>(V)) { |
| 215 | bool b = false; |
| 216 | if (R->isBoundable()) { |
| 217 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
| 218 | if (TR->getValueType()->isObjCObjectPointerType()) { |
| 219 | os << "nil object reference stored to "; |
| 220 | b = true; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if (!b) |
| 226 | os << "Null pointer value stored to "; |
| 227 | } |
| 228 | else if (V.isUndef()) { |
| 229 | os << "Uninitialized value stored to "; |
| 230 | } |
| 231 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 232 | os << "The value " << cast<nonloc::ConcreteInt>(V).getValue() |
| 233 | << " is assigned to "; |
| 234 | } |
| 235 | else |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 236 | os << "Value assigned to "; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 237 | |
| 238 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 239 | os << '\'' << *VR->getDecl() << '\''; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 240 | } |
| 241 | else |
| 242 | return NULL; |
| 243 | } |
| 244 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 245 | // Construct a new PathDiagnosticPiece. |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 246 | ProgramPoint P = N->getLocation(); |
Anna Zaks | 1531bb0 | 2011-09-20 01:38:47 +0000 | [diff] [blame] | 247 | PathDiagnosticLocation L = |
| 248 | PathDiagnosticLocation::create(P, BRC.getSourceManager()); |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 249 | if (!L.isValid()) |
| 250 | return NULL; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 251 | return new PathDiagnosticEventPiece(L, os.str()); |
| 252 | } |
| 253 | |
| 254 | void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const { |
| 255 | static int tag = 0; |
| 256 | ID.AddPointer(&tag); |
| 257 | ID.AddBoolean(Assumption); |
| 258 | ID.Add(Constraint); |
| 259 | } |
| 260 | |
| 261 | PathDiagnosticPiece * |
| 262 | TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N, |
| 263 | const ExplodedNode *PrevN, |
| 264 | BugReporterContext &BRC, |
| 265 | BugReport &BR) { |
| 266 | if (isSatisfied) |
| 267 | return NULL; |
| 268 | |
| 269 | // Check if in the previous state it was feasible for this constraint |
| 270 | // to *not* be true. |
| 271 | if (PrevN->getState()->assume(Constraint, !Assumption)) { |
| 272 | |
| 273 | isSatisfied = true; |
| 274 | |
| 275 | // As a sanity check, make sure that the negation of the constraint |
| 276 | // was infeasible in the current state. If it is feasible, we somehow |
| 277 | // missed the transition point. |
| 278 | if (N->getState()->assume(Constraint, !Assumption)) |
| 279 | return NULL; |
| 280 | |
| 281 | // We found the transition point for the constraint. We now need to |
| 282 | // pretty-print the constraint. (work-in-progress) |
| 283 | std::string sbuf; |
| 284 | llvm::raw_string_ostream os(sbuf); |
| 285 | |
| 286 | if (isa<Loc>(Constraint)) { |
| 287 | os << "Assuming pointer value is "; |
| 288 | os << (Assumption ? "non-null" : "null"); |
| 289 | } |
| 290 | |
| 291 | if (os.str().empty()) |
| 292 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 294 | // Construct a new PathDiagnosticPiece. |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 295 | ProgramPoint P = N->getLocation(); |
Anna Zaks | 1531bb0 | 2011-09-20 01:38:47 +0000 | [diff] [blame] | 296 | PathDiagnosticLocation L = |
| 297 | PathDiagnosticLocation::create(P, BRC.getSourceManager()); |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 298 | if (!L.isValid()) |
| 299 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 300 | return new PathDiagnosticEventPiece(L, os.str()); |
| 301 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 302 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 303 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Jordan Rose | 23df243 | 2012-08-24 16:34:31 +0000 | [diff] [blame] | 306 | namespace { |
| 307 | class ReturnNullVisitor : public BugReporterVisitorImpl<ReturnNullVisitor> { |
| 308 | const ExplodedNode *ReturnNode; |
| 309 | public: |
| 310 | ReturnNullVisitor(const ExplodedNode *N) : ReturnNode(N) {} |
| 311 | |
| 312 | virtual void Profile(llvm::FoldingSetNodeID &ID) const { |
| 313 | static int Tag = 0; |
| 314 | ID.AddPointer(&Tag); |
| 315 | ID.Add(*ReturnNode); |
| 316 | } |
| 317 | |
| 318 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
| 319 | const ExplodedNode *PrevN, |
| 320 | BugReporterContext &BRC, |
| 321 | BugReport &BR) { |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 322 | // Only print a message at the interesting return statement. |
Jordan Rose | 23df243 | 2012-08-24 16:34:31 +0000 | [diff] [blame] | 323 | if (ReturnNode != BRC.getNodeResolver().getOriginalNode(N)) |
| 324 | return 0; |
| 325 | |
| 326 | StmtPoint SP = cast<StmtPoint>(ReturnNode->getLocation()); |
| 327 | const ReturnStmt *Ret = cast<ReturnStmt>(SP.getStmt()); |
| 328 | PathDiagnosticLocation L(Ret, BRC.getSourceManager(), |
| 329 | N->getLocationContext()); |
| 330 | |
| 331 | SmallString<64> Msg; |
| 332 | llvm::raw_svector_ostream Out(Msg); |
| 333 | |
| 334 | if (Loc::isLocType(Ret->getRetValue()->getType())) |
| 335 | Out << "Returning null pointer"; |
| 336 | else |
| 337 | Out << "Returning zero"; |
| 338 | |
| 339 | // FIXME: We should have a more generalized printing mechanism. |
| 340 | const Expr *RetE = Ret->getRetValue()->IgnoreParenCasts(); |
| 341 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(RetE)) |
| 342 | if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(DR->getDecl())) |
| 343 | Out << " (loaded from '" << *DD << "')"; |
| 344 | |
| 345 | return new PathDiagnosticEventPiece(L, Out.str()); |
| 346 | } |
| 347 | }; |
| 348 | } // end anonymous namespace |
| 349 | |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 350 | void bugreporter::addTrackNullOrUndefValueVisitor(const ExplodedNode *N, |
| 351 | const Stmt *S, |
| 352 | BugReport *report) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 353 | if (!S || !N) |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 354 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 356 | ProgramStateManager &StateMgr = N->getState()->getStateManager(); |
| 357 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 358 | // Walk through nodes until we get one that matches the statement exactly. |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 359 | while (N) { |
| 360 | const ProgramPoint &pp = N->getLocation(); |
| 361 | if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) { |
| 362 | if (ps->getStmt() == S) |
| 363 | break; |
Jordan Rose | 23df243 | 2012-08-24 16:34:31 +0000 | [diff] [blame] | 364 | } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&pp)) { |
| 365 | if (CEE->getCalleeContext()->getCallSite() == S) |
| 366 | break; |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 367 | } |
Anna Zaks | b459cf3 | 2011-10-01 06:35:19 +0000 | [diff] [blame] | 368 | N = N->getFirstPred(); |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | if (!N) |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 372 | return; |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 373 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 374 | ProgramStateRef state = N->getState(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 376 | // See if the expression we're interested refers to a variable. |
| 377 | // If so, we can track both its contents and constraints on its value. |
| 378 | if (const Expr *Ex = dyn_cast<Expr>(S)) { |
| 379 | // Strip off parens and casts. Note that this will never have issues with |
| 380 | // C++ user-defined implicit conversions, because those have a constructor |
| 381 | // or function call inside. |
Jordan Rose | e6cd054 | 2012-08-16 00:03:33 +0000 | [diff] [blame] | 382 | Ex = Ex->IgnoreParenCasts(); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 383 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) { |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 384 | // FIXME: Right now we only track VarDecls because it's non-trivial to |
| 385 | // get a MemRegion for any other DeclRefExprs. <rdar://problem/12114812> |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 386 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 387 | const VarRegion *R = |
| 388 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 390 | // Mark both the variable region and its contents as interesting. |
Ted Kremenek | 11abcec | 2012-05-02 00:31:29 +0000 | [diff] [blame] | 391 | SVal V = state->getRawSVal(loc::MemRegionVal(R)); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 392 | report->markInteresting(R); |
| 393 | report->markInteresting(V); |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 394 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 395 | // If the contents are symbolic, find out when they became null. |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 396 | if (V.getAsLocSymbol()) { |
| 397 | BugReporterVisitor *ConstraintTracker |
| 398 | = new TrackConstraintBRVisitor(cast<loc::MemRegionVal>(V), false); |
| 399 | report->addVisitor(ConstraintTracker); |
| 400 | } |
| 401 | |
| 402 | report->addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 403 | return; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 408 | // If the expression does NOT refer to a variable, we can still track |
| 409 | // constraints on its contents. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 410 | SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 412 | // Uncomment this to find cases where we aren't properly getting the |
| 413 | // base value that was dereferenced. |
| 414 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 416 | // Is it a symbolic value? |
| 417 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 418 | const MemRegion *Base = L->getRegion()->getBaseRegion(); |
| 419 | if (isa<SymbolicRegion>(Base)) { |
| 420 | report->markInteresting(Base); |
| 421 | report->addVisitor(new TrackConstraintBRVisitor(loc::MemRegionVal(Base), |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 422 | false)); |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 423 | } |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 424 | } else if (isa<loc::ConcreteInt>(V)) { |
| 425 | // Otherwise, if it's a null constant, we want to know where it came from. |
| 426 | // In particular, if it came from an inlined function call, |
| 427 | // we need to make sure that function isn't pruned in our output. |
| 428 | |
Jordan Rose | 23df243 | 2012-08-24 16:34:31 +0000 | [diff] [blame] | 429 | // Walk backwards to just before the post-statement checks. |
| 430 | ProgramPoint PP = N->getLocation(); |
| 431 | while (N && isa<PostStmt>(PP = N->getLocation())) |
| 432 | N = N->getFirstPred(); |
| 433 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 434 | // If we found an inlined call before the post-statement checks, look |
| 435 | // for a return statement within the call. |
Jordan Rose | 23df243 | 2012-08-24 16:34:31 +0000 | [diff] [blame] | 436 | if (N && isa<CallExitEnd>(PP)) { |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 437 | // Walk backwards within the inlined function until we find a statement. |
| 438 | // This will look through multiple levels of inlining, but stop if the |
| 439 | // last thing that happened was an empty function being inlined. |
| 440 | // FIXME: This may not work in the presence of destructors. |
Jordan Rose | 23df243 | 2012-08-24 16:34:31 +0000 | [diff] [blame] | 441 | do { |
| 442 | N = N->getFirstPred(); |
| 443 | PP = N->getLocation(); |
| 444 | } while (!isa<StmtPoint>(PP) && !isa<CallEnter>(PP)); |
| 445 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame^] | 446 | // If the last statement we found is a 'return <expr>', make sure we |
| 447 | // show this return...and recursively track the value being returned. |
Jordan Rose | 23df243 | 2012-08-24 16:34:31 +0000 | [diff] [blame] | 448 | if (const StmtPoint *SP = dyn_cast<StmtPoint>(&PP)) { |
| 449 | if (const ReturnStmt *Ret = SP->getStmtAs<ReturnStmt>()) { |
| 450 | if (const Expr *RetE = Ret->getRetValue()) { |
| 451 | report->addVisitor(new ReturnNullVisitor(N)); |
| 452 | addTrackNullOrUndefValueVisitor(N, RetE, report); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 457 | } |
| 458 | } |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 459 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 460 | BugReporterVisitor * |
| 461 | FindLastStoreBRVisitor::createVisitorObject(const ExplodedNode *N, |
| 462 | const MemRegion *R) { |
| 463 | assert(R && "The memory region is null."); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 465 | ProgramStateRef state = N->getState(); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 466 | SVal V = state->getSVal(R); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 467 | if (V.isUnknown()) |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 468 | return 0; |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 469 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 470 | return new FindLastStoreBRVisitor(V, R); |
Ted Kremenek | 94fd0b8 | 2010-02-16 08:33:59 +0000 | [diff] [blame] | 471 | } |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 472 | |
| 473 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 474 | PathDiagnosticPiece *NilReceiverBRVisitor::VisitNode(const ExplodedNode *N, |
| 475 | const ExplodedNode *PrevN, |
| 476 | BugReporterContext &BRC, |
| 477 | BugReport &BR) { |
| 478 | const PostStmt *P = N->getLocationAs<PostStmt>(); |
| 479 | if (!P) |
| 480 | return 0; |
| 481 | const ObjCMessageExpr *ME = P->getStmtAs<ObjCMessageExpr>(); |
| 482 | if (!ME) |
| 483 | return 0; |
| 484 | const Expr *Receiver = ME->getInstanceReceiver(); |
| 485 | if (!Receiver) |
| 486 | return 0; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 487 | ProgramStateRef state = N->getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 488 | const SVal &V = state->getSVal(Receiver, N->getLocationContext()); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 489 | const DefinedOrUnknownSVal *DV = dyn_cast<DefinedOrUnknownSVal>(&V); |
| 490 | if (!DV) |
| 491 | return 0; |
| 492 | state = state->assume(*DV, true); |
| 493 | if (state) |
| 494 | return 0; |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 495 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 496 | // The receiver was nil, and hence the method was skipped. |
| 497 | // Register a BugReporterVisitor to issue a message telling us how |
| 498 | // the receiver was null. |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 499 | bugreporter::addTrackNullOrUndefValueVisitor(N, Receiver, &BR); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 500 | // Issue a message saying that the method was skipped. |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 501 | PathDiagnosticLocation L(Receiver, BRC.getSourceManager(), |
| 502 | N->getLocationContext()); |
Anna Zaks | 907344e | 2012-04-05 02:10:19 +0000 | [diff] [blame] | 503 | return new PathDiagnosticEventPiece(L, "No method is called " |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 504 | "because the receiver is nil"); |
Ted Kremenek | ff7f736 | 2010-03-20 18:02:01 +0000 | [diff] [blame] | 505 | } |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 506 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 507 | // Registers every VarDecl inside a Stmt with a last store visitor. |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 508 | void FindLastStoreBRVisitor::registerStatementVarDecls(BugReport &BR, |
| 509 | const Stmt *S) { |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 510 | const ExplodedNode *N = BR.getErrorNode(); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 511 | std::deque<const Stmt *> WorkList; |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 512 | WorkList.push_back(S); |
| 513 | |
| 514 | while (!WorkList.empty()) { |
| 515 | const Stmt *Head = WorkList.front(); |
| 516 | WorkList.pop_front(); |
| 517 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 518 | ProgramStateRef state = N->getState(); |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 519 | ProgramStateManager &StateMgr = state->getStateManager(); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 520 | |
| 521 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Head)) { |
| 522 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 523 | const VarRegion *R = |
| 524 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
| 525 | |
| 526 | // What did we load? |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 527 | SVal V = state->getSVal(S, N->getLocationContext()); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 528 | |
| 529 | if (isa<loc::ConcreteInt>(V) || isa<nonloc::ConcreteInt>(V)) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 530 | // Register a new visitor with the BugReport. |
| 531 | BR.addVisitor(new FindLastStoreBRVisitor(V, R)); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | for (Stmt::const_child_iterator I = Head->child_begin(); |
| 537 | I != Head->child_end(); ++I) |
| 538 | WorkList.push_back(*I); |
| 539 | } |
| 540 | } |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 541 | |
| 542 | //===----------------------------------------------------------------------===// |
| 543 | // Visitor that tries to report interesting diagnostics from conditions. |
| 544 | //===----------------------------------------------------------------------===// |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 545 | PathDiagnosticPiece *ConditionBRVisitor::VisitNode(const ExplodedNode *N, |
| 546 | const ExplodedNode *Prev, |
| 547 | BugReporterContext &BRC, |
| 548 | BugReport &BR) { |
Ted Kremenek | c89f4b0 | 2012-02-28 23:06:21 +0000 | [diff] [blame] | 549 | PathDiagnosticPiece *piece = VisitNodeImpl(N, Prev, BRC, BR); |
| 550 | if (PathDiagnosticEventPiece *ev = |
| 551 | dyn_cast_or_null<PathDiagnosticEventPiece>(piece)) |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 552 | ev->setPrunable(true, /* override */ false); |
Ted Kremenek | c89f4b0 | 2012-02-28 23:06:21 +0000 | [diff] [blame] | 553 | return piece; |
| 554 | } |
| 555 | |
| 556 | PathDiagnosticPiece *ConditionBRVisitor::VisitNodeImpl(const ExplodedNode *N, |
| 557 | const ExplodedNode *Prev, |
| 558 | BugReporterContext &BRC, |
| 559 | BugReport &BR) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 560 | |
| 561 | const ProgramPoint &progPoint = N->getLocation(); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 562 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 563 | ProgramStateRef CurrentState = N->getState(); |
| 564 | ProgramStateRef PrevState = Prev->getState(); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 565 | |
| 566 | // Compare the GDMs of the state, because that is where constraints |
| 567 | // are managed. Note that ensure that we only look at nodes that |
| 568 | // were generated by the analyzer engine proper, not checkers. |
| 569 | if (CurrentState->getGDM().getRoot() == |
| 570 | PrevState->getGDM().getRoot()) |
| 571 | return 0; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 572 | |
| 573 | // If an assumption was made on a branch, it should be caught |
| 574 | // here by looking at the state transition. |
| 575 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 576 | const CFGBlock *srcBlk = BE->getSrc(); |
| 577 | if (const Stmt *term = srcBlk->getTerminator()) |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 578 | return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) { |
| 583 | // FIXME: Assuming that BugReporter is a GRBugReporter is a layering |
| 584 | // violation. |
| 585 | const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags = |
| 586 | cast<GRBugReporter>(BRC.getBugReporter()). |
| 587 | getEngine().getEagerlyAssumeTags(); |
| 588 | |
| 589 | const ProgramPointTag *tag = PS->getTag(); |
| 590 | if (tag == tags.first) |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 591 | return VisitTrueTest(cast<Expr>(PS->getStmt()), true, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 592 | BRC, BR, N); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 593 | if (tag == tags.second) |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 594 | return VisitTrueTest(cast<Expr>(PS->getStmt()), false, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 595 | BRC, BR, N); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 596 | |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 604 | ConditionBRVisitor::VisitTerminator(const Stmt *Term, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 605 | const ExplodedNode *N, |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 606 | const CFGBlock *srcBlk, |
| 607 | const CFGBlock *dstBlk, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 608 | BugReport &R, |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 609 | BugReporterContext &BRC) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 610 | const Expr *Cond = 0; |
| 611 | |
| 612 | switch (Term->getStmtClass()) { |
| 613 | default: |
| 614 | return 0; |
| 615 | case Stmt::IfStmtClass: |
| 616 | Cond = cast<IfStmt>(Term)->getCond(); |
| 617 | break; |
| 618 | case Stmt::ConditionalOperatorClass: |
| 619 | Cond = cast<ConditionalOperator>(Term)->getCond(); |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | assert(Cond); |
| 624 | assert(srcBlk->succ_size() == 2); |
| 625 | const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk; |
| 626 | return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()), |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 627 | tookTrue, BRC, R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 631 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 632 | bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 633 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 634 | BugReport &R, |
| 635 | const ExplodedNode *N) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 636 | |
| 637 | const Expr *Ex = Cond; |
| 638 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 639 | while (true) { |
| 640 | Ex = Ex->IgnoreParens(); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 641 | switch (Ex->getStmtClass()) { |
| 642 | default: |
| 643 | return 0; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 644 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 645 | return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC, |
| 646 | R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 647 | case Stmt::DeclRefExprClass: |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 648 | return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC, |
| 649 | R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 650 | case Stmt::UnaryOperatorClass: { |
| 651 | const UnaryOperator *UO = cast<UnaryOperator>(Ex); |
| 652 | if (UO->getOpcode() == UO_LNot) { |
| 653 | tookTrue = !tookTrue; |
| 654 | Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext()); |
| 655 | continue; |
| 656 | } |
| 657 | return 0; |
| 658 | } |
| 659 | } |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 663 | bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 664 | BugReporterContext &BRC, |
| 665 | BugReport &report, |
| 666 | const ExplodedNode *N, |
| 667 | llvm::Optional<bool> &prunable) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 668 | const Expr *OriginalExpr = Ex; |
| 669 | Ex = Ex->IgnoreParenCasts(); |
| 670 | |
| 671 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) { |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 672 | const bool quotes = isa<VarDecl>(DR->getDecl()); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 673 | if (quotes) { |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 674 | Out << '\''; |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 675 | const LocationContext *LCtx = N->getLocationContext(); |
| 676 | const ProgramState *state = N->getState().getPtr(); |
| 677 | if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()), |
| 678 | LCtx).getAsRegion()) { |
| 679 | if (report.isInteresting(R)) |
| 680 | prunable = false; |
| 681 | else { |
| 682 | const ProgramState *state = N->getState().getPtr(); |
| 683 | SVal V = state->getSVal(R); |
| 684 | if (report.isInteresting(V)) |
| 685 | prunable = false; |
| 686 | } |
| 687 | } |
| 688 | } |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 689 | Out << DR->getDecl()->getDeclName().getAsString(); |
| 690 | if (quotes) |
| 691 | Out << '\''; |
| 692 | return quotes; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) { |
| 696 | QualType OriginalTy = OriginalExpr->getType(); |
| 697 | if (OriginalTy->isPointerType()) { |
| 698 | if (IL->getValue() == 0) { |
| 699 | Out << "null"; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 700 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | else if (OriginalTy->isObjCObjectPointerType()) { |
| 704 | if (IL->getValue() == 0) { |
| 705 | Out << "nil"; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 706 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | Out << IL->getValue(); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 711 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 712 | } |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 713 | |
| 714 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 718 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 719 | const BinaryOperator *BExpr, |
| 720 | const bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 721 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 722 | BugReport &R, |
| 723 | const ExplodedNode *N) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 724 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 725 | bool shouldInvert = false; |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 726 | llvm::Optional<bool> shouldPrune; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 727 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 728 | SmallString<128> LhsString, RhsString; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 729 | { |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 730 | llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString); |
| 731 | const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N, |
| 732 | shouldPrune); |
| 733 | const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N, |
| 734 | shouldPrune); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 735 | |
| 736 | shouldInvert = !isVarLHS && isVarRHS; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 739 | BinaryOperator::Opcode Op = BExpr->getOpcode(); |
| 740 | |
| 741 | if (BinaryOperator::isAssignmentOp(Op)) { |
| 742 | // For assignment operators, all that we care about is that the LHS |
| 743 | // evaluates to "true" or "false". |
| 744 | return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 745 | BRC, R, N); |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | // For non-assignment operations, we require that we can understand |
| 749 | // both the LHS and RHS. |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 750 | if (LhsString.empty() || RhsString.empty()) |
| 751 | return 0; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 752 | |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 753 | // Should we invert the strings if the LHS is not a variable name? |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 754 | SmallString<256> buf; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 755 | llvm::raw_svector_ostream Out(buf); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 756 | Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is "; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 757 | |
| 758 | // Do we need to invert the opcode? |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 759 | if (shouldInvert) |
| 760 | switch (Op) { |
| 761 | default: break; |
| 762 | case BO_LT: Op = BO_GT; break; |
| 763 | case BO_GT: Op = BO_LT; break; |
| 764 | case BO_LE: Op = BO_GE; break; |
| 765 | case BO_GE: Op = BO_LE; break; |
| 766 | } |
| 767 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 768 | if (!tookTrue) |
| 769 | switch (Op) { |
| 770 | case BO_EQ: Op = BO_NE; break; |
| 771 | case BO_NE: Op = BO_EQ; break; |
| 772 | case BO_LT: Op = BO_GE; break; |
| 773 | case BO_GT: Op = BO_LE; break; |
| 774 | case BO_LE: Op = BO_GT; break; |
Ted Kremenek | 4ee7c9c | 2011-08-16 03:44:38 +0000 | [diff] [blame] | 775 | case BO_GE: Op = BO_LT; break; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 776 | default: |
| 777 | return 0; |
| 778 | } |
| 779 | |
Ted Kremenek | 6ae3257 | 2011-12-20 22:00:25 +0000 | [diff] [blame] | 780 | switch (Op) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 781 | case BO_EQ: |
| 782 | Out << "equal to "; |
| 783 | break; |
| 784 | case BO_NE: |
| 785 | Out << "not equal to "; |
| 786 | break; |
| 787 | default: |
| 788 | Out << BinaryOperator::getOpcodeStr(Op) << ' '; |
| 789 | break; |
| 790 | } |
| 791 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 792 | Out << (shouldInvert ? LhsString : RhsString); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 793 | const LocationContext *LCtx = N->getLocationContext(); |
| 794 | PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); |
| 795 | PathDiagnosticEventPiece *event = |
| 796 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 797 | if (shouldPrune.hasValue()) |
| 798 | event->setPrunable(shouldPrune.getValue()); |
| 799 | return event; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 800 | } |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 801 | |
| 802 | PathDiagnosticPiece * |
| 803 | ConditionBRVisitor::VisitConditionVariable(StringRef LhsString, |
| 804 | const Expr *CondVarExpr, |
| 805 | const bool tookTrue, |
| 806 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 807 | BugReport &report, |
| 808 | const ExplodedNode *N) { |
Jordan Rose | b0e1bad | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 809 | // FIXME: If there's already a constraint tracker for this variable, |
| 810 | // we shouldn't emit anything here (c.f. the double note in |
| 811 | // test/Analysis/inlining/path-notes.c) |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 812 | SmallString<256> buf; |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 813 | llvm::raw_svector_ostream Out(buf); |
| 814 | Out << "Assuming " << LhsString << " is "; |
| 815 | |
| 816 | QualType Ty = CondVarExpr->getType(); |
| 817 | |
| 818 | if (Ty->isPointerType()) |
| 819 | Out << (tookTrue ? "not null" : "null"); |
| 820 | else if (Ty->isObjCObjectPointerType()) |
| 821 | Out << (tookTrue ? "not nil" : "nil"); |
| 822 | else if (Ty->isBooleanType()) |
| 823 | Out << (tookTrue ? "true" : "false"); |
| 824 | else if (Ty->isIntegerType()) |
| 825 | Out << (tookTrue ? "non-zero" : "zero"); |
| 826 | else |
| 827 | return 0; |
| 828 | |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 829 | const LocationContext *LCtx = N->getLocationContext(); |
| 830 | PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx); |
| 831 | PathDiagnosticEventPiece *event = |
| 832 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 833 | |
| 834 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) { |
| 835 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 836 | const ProgramState *state = N->getState().getPtr(); |
| 837 | if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) { |
| 838 | if (report.isInteresting(R)) |
| 839 | event->setPrunable(false); |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | return event; |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 845 | } |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 846 | |
| 847 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 848 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 849 | const DeclRefExpr *DR, |
| 850 | const bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 851 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 852 | BugReport &report, |
| 853 | const ExplodedNode *N) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 854 | |
| 855 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 856 | if (!VD) |
| 857 | return 0; |
| 858 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 859 | SmallString<256> Buf; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 860 | llvm::raw_svector_ostream Out(Buf); |
| 861 | |
| 862 | Out << "Assuming '"; |
| 863 | VD->getDeclName().printName(Out); |
| 864 | Out << "' is "; |
| 865 | |
| 866 | QualType VDTy = VD->getType(); |
| 867 | |
| 868 | if (VDTy->isPointerType()) |
| 869 | Out << (tookTrue ? "non-null" : "null"); |
| 870 | else if (VDTy->isObjCObjectPointerType()) |
| 871 | Out << (tookTrue ? "non-nil" : "nil"); |
| 872 | else if (VDTy->isScalarType()) |
| 873 | Out << (tookTrue ? "not equal to 0" : "0"); |
| 874 | else |
| 875 | return 0; |
| 876 | |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 877 | const LocationContext *LCtx = N->getLocationContext(); |
| 878 | PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); |
| 879 | PathDiagnosticEventPiece *event = |
| 880 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 881 | |
| 882 | const ProgramState *state = N->getState().getPtr(); |
| 883 | if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) { |
| 884 | if (report.isInteresting(R)) |
| 885 | event->setPrunable(false); |
| 886 | else { |
| 887 | SVal V = state->getSVal(R); |
| 888 | if (report.isInteresting(V)) |
| 889 | event->setPrunable(false); |
| 890 | } |
| 891 | } |
| 892 | return event; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 893 | } |
Ted Kremenek | 0cf3d47 | 2012-02-07 00:24:33 +0000 | [diff] [blame] | 894 | |