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