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