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