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