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