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 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 248 | PathDiagnosticPiece *FindLastStoreBRVisitor::VisitNode(const ExplodedNode *N, |
| 249 | const ExplodedNode *PrevN, |
| 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 | |
| 256 | if (!StoreSite) { |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 257 | // Make sure the region is actually bound to value V here. |
| 258 | // This is necessary because the region may not actually be live at the |
| 259 | // report's error node. |
| 260 | if (N->getState()->getSVal(R) != V) |
| 261 | return NULL; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 262 | |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 263 | const ExplodedNode *Node = N, *Last = N; |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame^] | 264 | const Expr *InitE = 0; |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 265 | |
| 266 | // Now look for the store of V. |
Ted Kremenek | d2e7090 | 2012-01-25 22:18:04 +0000 | [diff] [blame] | 267 | for ( ; Node ; Node = Node->getFirstPred()) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 268 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
| 269 | if (const PostStmt *P = Node->getLocationAs<PostStmt>()) |
| 270 | if (const DeclStmt *DS = P->getStmtAs<DeclStmt>()) |
| 271 | if (DS->getSingleDecl() == VR->getDecl()) { |
Ted Kremenek | d2e7090 | 2012-01-25 22:18:04 +0000 | [diff] [blame] | 272 | // Record the last seen initialization point. |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 273 | Last = Node; |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame^] | 274 | InitE = VR->getDecl()->getInit(); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
Ted Kremenek | d2e7090 | 2012-01-25 22:18:04 +0000 | [diff] [blame] | 279 | // Does the region still bind to value V? If not, we are done |
| 280 | // looking for store sites. |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame^] | 281 | SVal Current = Node->getState()->getSVal(R); |
| 282 | if (Current != V) { |
| 283 | // If this is an assignment expression, we can track the value |
| 284 | // being assigned. |
| 285 | if (const StmtPoint *SP = Last->getLocationAs<StmtPoint>()) { |
| 286 | const Stmt *S = SP->getStmt(); |
| 287 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) |
| 288 | if (BO->isAssignmentOp()) |
| 289 | InitE = BO->getRHS(); |
| 290 | } |
| 291 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 292 | break; |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame^] | 293 | } |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 294 | |
| 295 | Last = Node; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Jordan Rose | 20165e7 | 2012-08-03 23:08:42 +0000 | [diff] [blame] | 298 | if (!Node) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 299 | satisfied = true; |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | StoreSite = Last; |
Jordan Rose | 7aba117 | 2012-08-28 00:50:42 +0000 | [diff] [blame^] | 304 | |
| 305 | // If the value that was stored came from an inlined call, make sure we |
| 306 | // step into the call. |
| 307 | if (InitE) { |
| 308 | InitE = InitE->IgnoreParenCasts(); |
| 309 | ReturnVisitor::addVisitorIfNecessary(StoreSite, InitE, BR); |
| 310 | } |
Ted Kremenek | 1b43102 | 2010-03-20 18:01:57 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 313 | if (StoreSite != N) |
| 314 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 316 | satisfied = true; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 317 | SmallString<256> sbuf; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 318 | llvm::raw_svector_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 320 | if (const PostStmt *PS = N->getLocationAs<PostStmt>()) { |
| 321 | if (const DeclStmt *DS = PS->getStmtAs<DeclStmt>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 323 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 324 | os << "Variable '" << *VR->getDecl() << "' "; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 325 | } |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 326 | else |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 327 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 328 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 329 | if (isa<loc::ConcreteInt>(V)) { |
| 330 | bool b = false; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 331 | if (R->isBoundable()) { |
Ted Kremenek | 9697934 | 2011-08-12 20:02:48 +0000 | [diff] [blame] | 332 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 333 | if (TR->getValueType()->isObjCObjectPointerType()) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 334 | os << "initialized to nil"; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 335 | b = true; |
| 336 | } |
| 337 | } |
| 338 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 340 | if (!b) |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 341 | os << "initialized to a null pointer value"; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 342 | } |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 343 | else if (isa<nonloc::ConcreteInt>(V)) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 344 | os << "initialized to " << cast<nonloc::ConcreteInt>(V).getValue(); |
Ted Kremenek | 592362b | 2009-08-18 01:05:30 +0000 | [diff] [blame] | 345 | } |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 346 | else if (V.isUndef()) { |
| 347 | if (isa<VarRegion>(R)) { |
| 348 | const VarDecl *VD = cast<VarDecl>(DS->getSingleDecl()); |
| 349 | if (VD->getInit()) |
| 350 | os << "initialized to a garbage value"; |
| 351 | else |
| 352 | os << "declared without an initial value"; |
| 353 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 354 | } |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 355 | else { |
| 356 | os << "initialized here"; |
| 357 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 358 | } |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | if (os.str().empty()) { |
| 362 | if (isa<loc::ConcreteInt>(V)) { |
| 363 | bool b = false; |
| 364 | if (R->isBoundable()) { |
| 365 | if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) { |
| 366 | if (TR->getValueType()->isObjCObjectPointerType()) { |
| 367 | os << "nil object reference stored to "; |
| 368 | b = true; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (!b) |
| 374 | os << "Null pointer value stored to "; |
| 375 | } |
| 376 | else if (V.isUndef()) { |
| 377 | os << "Uninitialized value stored to "; |
| 378 | } |
| 379 | else if (isa<nonloc::ConcreteInt>(V)) { |
| 380 | os << "The value " << cast<nonloc::ConcreteInt>(V).getValue() |
| 381 | << " is assigned to "; |
| 382 | } |
| 383 | else |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 384 | os << "Value assigned to "; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 385 | |
| 386 | if (const VarRegion *VR = dyn_cast<VarRegion>(R)) { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 387 | os << '\'' << *VR->getDecl() << '\''; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 388 | } |
| 389 | else |
| 390 | return NULL; |
| 391 | } |
| 392 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 393 | // Construct a new PathDiagnosticPiece. |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 394 | ProgramPoint P = N->getLocation(); |
Anna Zaks | 1531bb0 | 2011-09-20 01:38:47 +0000 | [diff] [blame] | 395 | PathDiagnosticLocation L = |
| 396 | PathDiagnosticLocation::create(P, BRC.getSourceManager()); |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 397 | if (!L.isValid()) |
| 398 | return NULL; |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 399 | return new PathDiagnosticEventPiece(L, os.str()); |
| 400 | } |
| 401 | |
| 402 | void TrackConstraintBRVisitor::Profile(llvm::FoldingSetNodeID &ID) const { |
| 403 | static int tag = 0; |
| 404 | ID.AddPointer(&tag); |
| 405 | ID.AddBoolean(Assumption); |
| 406 | ID.Add(Constraint); |
| 407 | } |
| 408 | |
| 409 | PathDiagnosticPiece * |
| 410 | TrackConstraintBRVisitor::VisitNode(const ExplodedNode *N, |
| 411 | const ExplodedNode *PrevN, |
| 412 | BugReporterContext &BRC, |
| 413 | BugReport &BR) { |
| 414 | if (isSatisfied) |
| 415 | return NULL; |
| 416 | |
| 417 | // Check if in the previous state it was feasible for this constraint |
| 418 | // to *not* be true. |
| 419 | if (PrevN->getState()->assume(Constraint, !Assumption)) { |
| 420 | |
| 421 | isSatisfied = true; |
| 422 | |
| 423 | // As a sanity check, make sure that the negation of the constraint |
| 424 | // was infeasible in the current state. If it is feasible, we somehow |
| 425 | // missed the transition point. |
| 426 | if (N->getState()->assume(Constraint, !Assumption)) |
| 427 | return NULL; |
| 428 | |
| 429 | // We found the transition point for the constraint. We now need to |
| 430 | // pretty-print the constraint. (work-in-progress) |
| 431 | std::string sbuf; |
| 432 | llvm::raw_string_ostream os(sbuf); |
| 433 | |
| 434 | if (isa<Loc>(Constraint)) { |
| 435 | os << "Assuming pointer value is "; |
| 436 | os << (Assumption ? "non-null" : "null"); |
| 437 | } |
| 438 | |
| 439 | if (os.str().empty()) |
| 440 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 442 | // Construct a new PathDiagnosticPiece. |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 443 | ProgramPoint P = N->getLocation(); |
Anna Zaks | 1531bb0 | 2011-09-20 01:38:47 +0000 | [diff] [blame] | 444 | PathDiagnosticLocation L = |
| 445 | PathDiagnosticLocation::create(P, BRC.getSourceManager()); |
Anna Zaks | 4fdf97b | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 446 | if (!L.isValid()) |
| 447 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 448 | return new PathDiagnosticEventPiece(L, os.str()); |
| 449 | } |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 450 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 451 | return NULL; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 454 | void bugreporter::addTrackNullOrUndefValueVisitor(const ExplodedNode *N, |
| 455 | const Stmt *S, |
| 456 | BugReport *report) { |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 457 | if (!S || !N) |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 458 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Anna Zaks | 8e6431a | 2011-08-18 22:37:56 +0000 | [diff] [blame] | 460 | ProgramStateManager &StateMgr = N->getState()->getStateManager(); |
| 461 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 462 | // Walk through nodes until we get one that matches the statement exactly. |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 463 | while (N) { |
| 464 | const ProgramPoint &pp = N->getLocation(); |
| 465 | if (const PostStmt *ps = dyn_cast<PostStmt>(&pp)) { |
| 466 | if (ps->getStmt() == S) |
| 467 | break; |
Jordan Rose | 23df243 | 2012-08-24 16:34:31 +0000 | [diff] [blame] | 468 | } else if (const CallExitEnd *CEE = dyn_cast<CallExitEnd>(&pp)) { |
| 469 | if (CEE->getCalleeContext()->getCallSite() == S) |
| 470 | break; |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 471 | } |
Anna Zaks | b459cf3 | 2011-10-01 06:35:19 +0000 | [diff] [blame] | 472 | N = N->getFirstPred(); |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | if (!N) |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 476 | return; |
Ted Kremenek | 8829989 | 2011-07-28 23:07:59 +0000 | [diff] [blame] | 477 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 478 | ProgramStateRef state = N->getState(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 480 | // See if the expression we're interested refers to a variable. |
| 481 | // If so, we can track both its contents and constraints on its value. |
| 482 | if (const Expr *Ex = dyn_cast<Expr>(S)) { |
| 483 | // Strip off parens and casts. Note that this will never have issues with |
| 484 | // C++ user-defined implicit conversions, because those have a constructor |
| 485 | // or function call inside. |
Jordan Rose | e6cd054 | 2012-08-16 00:03:33 +0000 | [diff] [blame] | 486 | Ex = Ex->IgnoreParenCasts(); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 487 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) { |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 488 | // FIXME: Right now we only track VarDecls because it's non-trivial to |
| 489 | // get a MemRegion for any other DeclRefExprs. <rdar://problem/12114812> |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 490 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 491 | const VarRegion *R = |
| 492 | StateMgr.getRegionManager().getVarRegion(VD, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 494 | // Mark both the variable region and its contents as interesting. |
Ted Kremenek | 11abcec | 2012-05-02 00:31:29 +0000 | [diff] [blame] | 495 | SVal V = state->getRawSVal(loc::MemRegionVal(R)); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 496 | report->markInteresting(R); |
| 497 | report->markInteresting(V); |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 498 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 499 | // If the contents are symbolic, find out when they became null. |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 500 | if (V.getAsLocSymbol()) { |
| 501 | BugReporterVisitor *ConstraintTracker |
| 502 | = new TrackConstraintBRVisitor(cast<loc::MemRegionVal>(V), false); |
| 503 | report->addVisitor(ConstraintTracker); |
| 504 | } |
| 505 | |
| 506 | report->addVisitor(new FindLastStoreBRVisitor(V, R)); |
| 507 | return; |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 512 | // If the expression does NOT refer to a variable, we can still track |
| 513 | // constraints on its contents. |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 514 | SVal V = state->getSValAsScalarOrLoc(S, N->getLocationContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 516 | // Uncomment this to find cases where we aren't properly getting the |
| 517 | // base value that was dereferenced. |
| 518 | // assert(!V.isUnknownOrUndef()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Ted Kremenek | 5350066 | 2009-07-22 17:55:28 +0000 | [diff] [blame] | 520 | // Is it a symbolic value? |
| 521 | if (loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&V)) { |
Jordan Rose | 364b9f9 | 2012-08-27 20:18:30 +0000 | [diff] [blame] | 522 | const MemRegion *Base = L->getRegion()->getBaseRegion(); |
| 523 | if (isa<SymbolicRegion>(Base)) { |
| 524 | report->markInteresting(Base); |
| 525 | report->addVisitor(new TrackConstraintBRVisitor(loc::MemRegionVal(Base), |
Jordan Rose | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 526 | 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. |
| 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 | 6853799 | 2012-08-03 23:09:01 +0000 | [diff] [blame] | 574 | bugreporter::addTrackNullOrUndefValueVisitor(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 | |
| 636 | const ProgramPoint &progPoint = N->getLocation(); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 637 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 638 | ProgramStateRef CurrentState = N->getState(); |
| 639 | ProgramStateRef PrevState = Prev->getState(); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 640 | |
| 641 | // Compare the GDMs of the state, because that is where constraints |
| 642 | // are managed. Note that ensure that we only look at nodes that |
| 643 | // were generated by the analyzer engine proper, not checkers. |
| 644 | if (CurrentState->getGDM().getRoot() == |
| 645 | PrevState->getGDM().getRoot()) |
| 646 | return 0; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 647 | |
| 648 | // If an assumption was made on a branch, it should be caught |
| 649 | // here by looking at the state transition. |
| 650 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&progPoint)) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 651 | const CFGBlock *srcBlk = BE->getSrc(); |
| 652 | if (const Stmt *term = srcBlk->getTerminator()) |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 653 | return VisitTerminator(term, N, srcBlk, BE->getDst(), BR, BRC); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | if (const PostStmt *PS = dyn_cast<PostStmt>(&progPoint)) { |
| 658 | // FIXME: Assuming that BugReporter is a GRBugReporter is a layering |
| 659 | // violation. |
| 660 | const std::pair<const ProgramPointTag *, const ProgramPointTag *> &tags = |
| 661 | cast<GRBugReporter>(BRC.getBugReporter()). |
| 662 | getEngine().getEagerlyAssumeTags(); |
| 663 | |
| 664 | const ProgramPointTag *tag = PS->getTag(); |
| 665 | if (tag == tags.first) |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 666 | return VisitTrueTest(cast<Expr>(PS->getStmt()), true, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 667 | BRC, BR, N); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 668 | if (tag == tags.second) |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 669 | return VisitTrueTest(cast<Expr>(PS->getStmt()), false, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 670 | BRC, BR, N); |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 671 | |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 679 | ConditionBRVisitor::VisitTerminator(const Stmt *Term, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 680 | const ExplodedNode *N, |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 681 | const CFGBlock *srcBlk, |
| 682 | const CFGBlock *dstBlk, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 683 | BugReport &R, |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 684 | BugReporterContext &BRC) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 685 | const Expr *Cond = 0; |
| 686 | |
| 687 | switch (Term->getStmtClass()) { |
| 688 | default: |
| 689 | return 0; |
| 690 | case Stmt::IfStmtClass: |
| 691 | Cond = cast<IfStmt>(Term)->getCond(); |
| 692 | break; |
| 693 | case Stmt::ConditionalOperatorClass: |
| 694 | Cond = cast<ConditionalOperator>(Term)->getCond(); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | assert(Cond); |
| 699 | assert(srcBlk->succ_size() == 2); |
| 700 | const bool tookTrue = *(srcBlk->succ_begin()) == dstBlk; |
| 701 | return VisitTrueTest(Cond->IgnoreParenNoopCasts(BRC.getASTContext()), |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 702 | tookTrue, BRC, R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 706 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 707 | bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 708 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 709 | BugReport &R, |
| 710 | const ExplodedNode *N) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 711 | |
| 712 | const Expr *Ex = Cond; |
| 713 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 714 | while (true) { |
| 715 | Ex = Ex->IgnoreParens(); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 716 | switch (Ex->getStmtClass()) { |
| 717 | default: |
| 718 | return 0; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 719 | case Stmt::BinaryOperatorClass: |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 720 | return VisitTrueTest(Cond, cast<BinaryOperator>(Ex), tookTrue, BRC, |
| 721 | R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 722 | case Stmt::DeclRefExprClass: |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 723 | return VisitTrueTest(Cond, cast<DeclRefExpr>(Ex), tookTrue, BRC, |
| 724 | R, N); |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 725 | case Stmt::UnaryOperatorClass: { |
| 726 | const UnaryOperator *UO = cast<UnaryOperator>(Ex); |
| 727 | if (UO->getOpcode() == UO_LNot) { |
| 728 | tookTrue = !tookTrue; |
| 729 | Ex = UO->getSubExpr()->IgnoreParenNoopCasts(BRC.getASTContext()); |
| 730 | continue; |
| 731 | } |
| 732 | return 0; |
| 733 | } |
| 734 | } |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 738 | bool ConditionBRVisitor::patternMatch(const Expr *Ex, llvm::raw_ostream &Out, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 739 | BugReporterContext &BRC, |
| 740 | BugReport &report, |
| 741 | const ExplodedNode *N, |
| 742 | llvm::Optional<bool> &prunable) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 743 | const Expr *OriginalExpr = Ex; |
| 744 | Ex = Ex->IgnoreParenCasts(); |
| 745 | |
| 746 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Ex)) { |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 747 | const bool quotes = isa<VarDecl>(DR->getDecl()); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 748 | if (quotes) { |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 749 | Out << '\''; |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 750 | const LocationContext *LCtx = N->getLocationContext(); |
| 751 | const ProgramState *state = N->getState().getPtr(); |
| 752 | if (const MemRegion *R = state->getLValue(cast<VarDecl>(DR->getDecl()), |
| 753 | LCtx).getAsRegion()) { |
| 754 | if (report.isInteresting(R)) |
| 755 | prunable = false; |
| 756 | else { |
| 757 | const ProgramState *state = N->getState().getPtr(); |
| 758 | SVal V = state->getSVal(R); |
| 759 | if (report.isInteresting(V)) |
| 760 | prunable = false; |
| 761 | } |
| 762 | } |
| 763 | } |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 764 | Out << DR->getDecl()->getDeclName().getAsString(); |
| 765 | if (quotes) |
| 766 | Out << '\''; |
| 767 | return quotes; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | if (const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(Ex)) { |
| 771 | QualType OriginalTy = OriginalExpr->getType(); |
| 772 | if (OriginalTy->isPointerType()) { |
| 773 | if (IL->getValue() == 0) { |
| 774 | Out << "null"; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 775 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | else if (OriginalTy->isObjCObjectPointerType()) { |
| 779 | if (IL->getValue() == 0) { |
| 780 | Out << "nil"; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 781 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
| 785 | Out << IL->getValue(); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 786 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 787 | } |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 788 | |
| 789 | return false; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 793 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 794 | const BinaryOperator *BExpr, |
| 795 | const bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 796 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 797 | BugReport &R, |
| 798 | const ExplodedNode *N) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 799 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 800 | bool shouldInvert = false; |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 801 | llvm::Optional<bool> shouldPrune; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 802 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 803 | SmallString<128> LhsString, RhsString; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 804 | { |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 805 | llvm::raw_svector_ostream OutLHS(LhsString), OutRHS(RhsString); |
| 806 | const bool isVarLHS = patternMatch(BExpr->getLHS(), OutLHS, BRC, R, N, |
| 807 | shouldPrune); |
| 808 | const bool isVarRHS = patternMatch(BExpr->getRHS(), OutRHS, BRC, R, N, |
| 809 | shouldPrune); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 810 | |
| 811 | shouldInvert = !isVarLHS && isVarRHS; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 814 | BinaryOperator::Opcode Op = BExpr->getOpcode(); |
| 815 | |
| 816 | if (BinaryOperator::isAssignmentOp(Op)) { |
| 817 | // For assignment operators, all that we care about is that the LHS |
| 818 | // evaluates to "true" or "false". |
| 819 | return VisitConditionVariable(LhsString, BExpr->getLHS(), tookTrue, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 820 | BRC, R, N); |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | // For non-assignment operations, we require that we can understand |
| 824 | // both the LHS and RHS. |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 825 | if (LhsString.empty() || RhsString.empty()) |
| 826 | return 0; |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 827 | |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 828 | // 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] | 829 | SmallString<256> buf; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 830 | llvm::raw_svector_ostream Out(buf); |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 831 | Out << "Assuming " << (shouldInvert ? RhsString : LhsString) << " is "; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 832 | |
| 833 | // Do we need to invert the opcode? |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 834 | if (shouldInvert) |
| 835 | switch (Op) { |
| 836 | default: break; |
| 837 | case BO_LT: Op = BO_GT; break; |
| 838 | case BO_GT: Op = BO_LT; break; |
| 839 | case BO_LE: Op = BO_GE; break; |
| 840 | case BO_GE: Op = BO_LE; break; |
| 841 | } |
| 842 | |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 843 | if (!tookTrue) |
| 844 | switch (Op) { |
| 845 | case BO_EQ: Op = BO_NE; break; |
| 846 | case BO_NE: Op = BO_EQ; break; |
| 847 | case BO_LT: Op = BO_GE; break; |
| 848 | case BO_GT: Op = BO_LE; break; |
| 849 | case BO_LE: Op = BO_GT; break; |
Ted Kremenek | 4ee7c9c | 2011-08-16 03:44:38 +0000 | [diff] [blame] | 850 | case BO_GE: Op = BO_LT; break; |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 851 | default: |
| 852 | return 0; |
| 853 | } |
| 854 | |
Ted Kremenek | 6ae3257 | 2011-12-20 22:00:25 +0000 | [diff] [blame] | 855 | switch (Op) { |
Ted Kremenek | 681bc11 | 2011-08-16 01:53:41 +0000 | [diff] [blame] | 856 | case BO_EQ: |
| 857 | Out << "equal to "; |
| 858 | break; |
| 859 | case BO_NE: |
| 860 | Out << "not equal to "; |
| 861 | break; |
| 862 | default: |
| 863 | Out << BinaryOperator::getOpcodeStr(Op) << ' '; |
| 864 | break; |
| 865 | } |
| 866 | |
Ted Kremenek | 3b9e8e4 | 2011-08-16 10:57:37 +0000 | [diff] [blame] | 867 | Out << (shouldInvert ? LhsString : RhsString); |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 868 | const LocationContext *LCtx = N->getLocationContext(); |
| 869 | PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); |
| 870 | PathDiagnosticEventPiece *event = |
| 871 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 872 | if (shouldPrune.hasValue()) |
| 873 | event->setPrunable(shouldPrune.getValue()); |
| 874 | return event; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 875 | } |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 876 | |
| 877 | PathDiagnosticPiece * |
| 878 | ConditionBRVisitor::VisitConditionVariable(StringRef LhsString, |
| 879 | const Expr *CondVarExpr, |
| 880 | const bool tookTrue, |
| 881 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 882 | BugReport &report, |
| 883 | const ExplodedNode *N) { |
Jordan Rose | b0e1bad | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 884 | // FIXME: If there's already a constraint tracker for this variable, |
| 885 | // we shouldn't emit anything here (c.f. the double note in |
| 886 | // test/Analysis/inlining/path-notes.c) |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 887 | SmallString<256> buf; |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 888 | llvm::raw_svector_ostream Out(buf); |
| 889 | Out << "Assuming " << LhsString << " is "; |
| 890 | |
| 891 | QualType Ty = CondVarExpr->getType(); |
| 892 | |
| 893 | if (Ty->isPointerType()) |
| 894 | Out << (tookTrue ? "not null" : "null"); |
| 895 | else if (Ty->isObjCObjectPointerType()) |
| 896 | Out << (tookTrue ? "not nil" : "nil"); |
| 897 | else if (Ty->isBooleanType()) |
| 898 | Out << (tookTrue ? "true" : "false"); |
| 899 | else if (Ty->isIntegerType()) |
| 900 | Out << (tookTrue ? "non-zero" : "zero"); |
| 901 | else |
| 902 | return 0; |
| 903 | |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 904 | const LocationContext *LCtx = N->getLocationContext(); |
| 905 | PathDiagnosticLocation Loc(CondVarExpr, BRC.getSourceManager(), LCtx); |
| 906 | PathDiagnosticEventPiece *event = |
| 907 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 908 | |
| 909 | if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(CondVarExpr)) { |
| 910 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
| 911 | const ProgramState *state = N->getState().getPtr(); |
| 912 | if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) { |
| 913 | if (report.isInteresting(R)) |
| 914 | event->setPrunable(false); |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | return event; |
Ted Kremenek | d1247c5 | 2012-01-04 08:18:09 +0000 | [diff] [blame] | 920 | } |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 921 | |
| 922 | PathDiagnosticPiece * |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 923 | ConditionBRVisitor::VisitTrueTest(const Expr *Cond, |
| 924 | const DeclRefExpr *DR, |
| 925 | const bool tookTrue, |
Anna Zaks | 220ac8c | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 926 | BugReporterContext &BRC, |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 927 | BugReport &report, |
| 928 | const ExplodedNode *N) { |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 929 | |
| 930 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 931 | if (!VD) |
| 932 | return 0; |
| 933 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 934 | SmallString<256> Buf; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 935 | llvm::raw_svector_ostream Out(Buf); |
| 936 | |
| 937 | Out << "Assuming '"; |
| 938 | VD->getDeclName().printName(Out); |
| 939 | Out << "' is "; |
| 940 | |
| 941 | QualType VDTy = VD->getType(); |
| 942 | |
| 943 | if (VDTy->isPointerType()) |
| 944 | Out << (tookTrue ? "non-null" : "null"); |
| 945 | else if (VDTy->isObjCObjectPointerType()) |
| 946 | Out << (tookTrue ? "non-nil" : "nil"); |
| 947 | else if (VDTy->isScalarType()) |
| 948 | Out << (tookTrue ? "not equal to 0" : "0"); |
| 949 | else |
| 950 | return 0; |
| 951 | |
Ted Kremenek | 76aadc3 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 952 | const LocationContext *LCtx = N->getLocationContext(); |
| 953 | PathDiagnosticLocation Loc(Cond, BRC.getSourceManager(), LCtx); |
| 954 | PathDiagnosticEventPiece *event = |
| 955 | new PathDiagnosticEventPiece(Loc, Out.str()); |
| 956 | |
| 957 | const ProgramState *state = N->getState().getPtr(); |
| 958 | if (const MemRegion *R = state->getLValue(VD, LCtx).getAsRegion()) { |
| 959 | if (report.isInteresting(R)) |
| 960 | event->setPrunable(false); |
| 961 | else { |
| 962 | SVal V = state->getSVal(R); |
| 963 | if (report.isInteresting(V)) |
| 964 | event->setPrunable(false); |
| 965 | } |
| 966 | } |
| 967 | return event; |
Ted Kremenek | 993124e | 2011-08-06 06:54:45 +0000 | [diff] [blame] | 968 | } |
Ted Kremenek | 0cf3d47 | 2012-02-07 00:24:33 +0000 | [diff] [blame] | 969 | |