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