Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1 | // BugReporter.cpp - Generate PathDiagnostics for 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 BugReporter, a utility class for generating |
| 11 | // PathDiagnostics for analyses based on GRSimpleVals. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Basic/SourceLocation.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/CFG.h" |
| 21 | #include "clang/AST/Expr.h" |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 22 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/ProgramPoint.h" |
| 24 | #include "clang/Analysis/PathDiagnostic.h" |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/OwningPtr.h" |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 29 | #include <queue> |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
| 32 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | // static functions. |
| 35 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 37 | static inline Stmt* GetStmt(ProgramPoint P) { |
| 38 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 39 | return PS->getStmt(); |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 40 | else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 41 | return BE->getSrc()->getTerminator(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 43 | return 0; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 46 | static inline const ExplodedNode<GRState>* |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 47 | GetPredecessorNode(const ExplodedNode<GRState>* N) { |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 48 | return N->pred_empty() ? NULL : *(N->pred_begin()); |
| 49 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 51 | static inline const ExplodedNode<GRState>* |
| 52 | GetSuccessorNode(const ExplodedNode<GRState>* N) { |
| 53 | return N->succ_empty() ? NULL : *(N->succ_begin()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 56 | static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) { |
| 57 | for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N)) |
| 58 | if (Stmt *S = GetStmt(N->getLocation())) |
| 59 | return S; |
| 60 | |
| 61 | return 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 64 | static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) { |
| 65 | for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N)) |
| 66 | if (Stmt *S = GetStmt(N->getLocation())) |
| 67 | return S; |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) { |
| 73 | if (Stmt *S = GetStmt(N->getLocation())) |
| 74 | return S; |
| 75 | |
| 76 | return GetPreviousStmt(N); |
| 77 | } |
| 78 | |
| 79 | static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) { |
| 80 | if (Stmt *S = GetStmt(N->getLocation())) |
| 81 | return S; |
| 82 | |
| 83 | return GetNextStmt(N); |
| 84 | } |
| 85 | |
| 86 | //===----------------------------------------------------------------------===// |
| 87 | // Diagnostics for 'execution continues on line XXX'. |
| 88 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 90 | namespace { |
| 91 | class VISIBILITY_HIDDEN PathDiagnosticBuilder { |
| 92 | SourceManager &SMgr; |
| 93 | const Decl& CodeDecl; |
| 94 | PathDiagnosticClient *PDC; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 95 | llvm::OwningPtr<ParentMap> PM; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 96 | public: |
| 97 | PathDiagnosticBuilder(SourceManager &smgr, const Decl& codedecl, |
| 98 | PathDiagnosticClient *pdc) |
| 99 | : SMgr(smgr), CodeDecl(codedecl), PDC(pdc) {} |
| 100 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 101 | PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N); |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 103 | PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os, |
| 104 | const ExplodedNode<GRState>* N); |
| 105 | |
| 106 | ParentMap& getParentMap() { |
| 107 | if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody())); |
| 108 | return *PM.get(); |
| 109 | } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 111 | PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S); |
| 112 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 113 | bool supportsLogicalOpControlFlow() const { |
| 114 | return PDC ? PDC->supportsLogicalOpControlFlow() : true; |
| 115 | } |
| 116 | }; |
| 117 | } // end anonymous namespace |
| 118 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 119 | PathDiagnosticLocation |
| 120 | PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) { |
| 121 | if (Stmt *S = GetNextStmt(N)) |
| 122 | return PathDiagnosticLocation(S, SMgr); |
| 123 | |
| 124 | return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 127 | PathDiagnosticLocation |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 128 | PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os, |
| 129 | const ExplodedNode<GRState>* N) { |
| 130 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 131 | // Slow, but probably doesn't matter. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 132 | if (os.str().empty()) |
| 133 | os << ' '; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 135 | const PathDiagnosticLocation &Loc = ExecutionContinues(N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 137 | if (Loc.asStmt()) |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 138 | os << "Execution continues on line " |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 139 | << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.'; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 140 | else |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 141 | os << "Execution jumps to the end of the " |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 142 | << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.'; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 143 | |
| 144 | return Loc; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 147 | PathDiagnosticLocation |
| 148 | PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) { |
| 149 | assert(S && "Null Stmt* passed to getEnclosingStmtLocation"); |
| 150 | ParentMap &P = getParentMap(); |
| 151 | while (isa<Expr>(S)) { |
| 152 | const Stmt *Parent = P.getParent(S); |
| 153 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 154 | if (!Parent) |
| 155 | break; |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 157 | switch (Parent->getStmtClass()) { |
| 158 | case Stmt::CompoundStmtClass: |
| 159 | case Stmt::StmtExprClass: |
| 160 | return PathDiagnosticLocation(S, SMgr); |
| 161 | case Stmt::DoStmtClass: |
| 162 | if (cast<DoStmt>(Parent)->getCond() != S) |
| 163 | return PathDiagnosticLocation(S, SMgr); |
| 164 | break; |
| 165 | case Stmt::ForStmtClass: |
| 166 | if (cast<ForStmt>(Parent)->getBody() == S) |
| 167 | return PathDiagnosticLocation(S, SMgr); |
| 168 | break; |
| 169 | case Stmt::IfStmtClass: |
| 170 | if (cast<IfStmt>(Parent)->getCond() != S) |
| 171 | return PathDiagnosticLocation(S, SMgr); |
| 172 | break; |
| 173 | case Stmt::ObjCForCollectionStmtClass: |
| 174 | if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S) |
| 175 | return PathDiagnosticLocation(S, SMgr); |
| 176 | break; |
| 177 | case Stmt::WhileStmtClass: |
| 178 | if (cast<WhileStmt>(Parent)->getCond() != S) |
| 179 | return PathDiagnosticLocation(S, SMgr); |
| 180 | break; |
| 181 | default: |
| 182 | break; |
| 183 | } |
| 184 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 185 | S = Parent; |
| 186 | } |
| 187 | |
| 188 | assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); |
| 189 | return PathDiagnosticLocation(S, SMgr); |
| 190 | } |
| 191 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 192 | //===----------------------------------------------------------------------===// |
| 193 | // Methods for BugType and subclasses. |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | BugType::~BugType() {} |
| 196 | void BugType::FlushReports(BugReporter &BR) {} |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 197 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 198 | //===----------------------------------------------------------------------===// |
| 199 | // Methods for BugReport and subclasses. |
| 200 | //===----------------------------------------------------------------------===// |
| 201 | BugReport::~BugReport() {} |
| 202 | RangedBugReport::~RangedBugReport() {} |
| 203 | |
| 204 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 205 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 206 | Stmt *S = NULL; |
| 207 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 208 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 209 | if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 210 | } |
| 211 | if (!S) S = GetStmt(ProgP); |
| 212 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 213 | return S; |
| 214 | } |
| 215 | |
| 216 | PathDiagnosticPiece* |
| 217 | BugReport::getEndPath(BugReporter& BR, |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 218 | const ExplodedNode<GRState>* EndPathNode) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 219 | |
| 220 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 221 | |
| 222 | if (!S) |
| 223 | return NULL; |
| 224 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 225 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 226 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 228 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 229 | getRanges(BR, Beg, End); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 231 | for (; Beg != End; ++Beg) |
| 232 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 233 | |
| 234 | return P; |
| 235 | } |
| 236 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 237 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 238 | const SourceRange*& end) { |
| 239 | |
| 240 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 241 | R = E->getSourceRange(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 242 | assert(R.isValid()); |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 243 | beg = &R; |
| 244 | end = beg+1; |
| 245 | } |
| 246 | else |
| 247 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 250 | SourceLocation BugReport::getLocation() const { |
| 251 | if (EndNode) |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 252 | if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) { |
| 253 | // For member expressions, return the location of the '.' or '->'. |
| 254 | if (MemberExpr* ME = dyn_cast<MemberExpr>(S)) |
| 255 | return ME->getMemberLoc(); |
| 256 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 257 | return S->getLocStart(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 258 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 259 | |
| 260 | return FullSourceLoc(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 263 | PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N, |
| 264 | const ExplodedNode<GRState>* PrevN, |
| 265 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 266 | BugReporter& BR, |
| 267 | NodeResolver &NR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 268 | return NULL; |
| 269 | } |
| 270 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 271 | //===----------------------------------------------------------------------===// |
| 272 | // Methods for BugReporter and subclasses. |
| 273 | //===----------------------------------------------------------------------===// |
| 274 | |
| 275 | BugReportEquivClass::~BugReportEquivClass() { |
| 276 | for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; |
| 277 | } |
| 278 | |
| 279 | GRBugReporter::~GRBugReporter() { FlushReports(); } |
| 280 | BugReporterData::~BugReporterData() {} |
| 281 | |
| 282 | ExplodedGraph<GRState>& |
| 283 | GRBugReporter::getGraph() { return Eng.getGraph(); } |
| 284 | |
| 285 | GRStateManager& |
| 286 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
| 287 | |
| 288 | BugReporter::~BugReporter() { FlushReports(); } |
| 289 | |
| 290 | void BugReporter::FlushReports() { |
| 291 | if (BugTypes.isEmpty()) |
| 292 | return; |
| 293 | |
| 294 | // First flush the warnings for each BugType. This may end up creating new |
| 295 | // warnings and new BugTypes. Because ImmutableSet is a functional data |
| 296 | // structure, we do not need to worry about the iterators being invalidated. |
| 297 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 298 | const_cast<BugType*>(*I)->FlushReports(*this); |
| 299 | |
| 300 | // Iterate through BugTypes a second time. BugTypes may have been updated |
| 301 | // with new BugType objects and new warnings. |
| 302 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) { |
| 303 | BugType *BT = const_cast<BugType*>(*I); |
| 304 | |
| 305 | typedef llvm::FoldingSet<BugReportEquivClass> SetTy; |
| 306 | SetTy& EQClasses = BT->EQClasses; |
| 307 | |
| 308 | for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ |
| 309 | BugReportEquivClass& EQ = *EI; |
| 310 | FlushReport(EQ); |
| 311 | } |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 312 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 313 | // Delete the BugType object. This will also delete the equivalence |
| 314 | // classes. |
| 315 | delete BT; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 316 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 317 | |
| 318 | // Remove all references to the BugType objects. |
| 319 | BugTypes = F.GetEmptySet(); |
| 320 | } |
| 321 | |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | // PathDiagnostics generation. |
| 324 | //===----------------------------------------------------------------------===// |
| 325 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 326 | typedef llvm::DenseMap<const ExplodedNode<GRState>*, |
| 327 | const ExplodedNode<GRState>*> NodeBackMap; |
| 328 | |
| 329 | static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 330 | std::pair<ExplodedNode<GRState>*, unsigned> > |
| 331 | MakeReportGraph(const ExplodedGraph<GRState>* G, |
| 332 | const ExplodedNode<GRState>** NStart, |
| 333 | const ExplodedNode<GRState>** NEnd) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 334 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 335 | // Create the trimmed graph. It will contain the shortest paths from the |
| 336 | // error nodes to the root. In the new graph we should only have one |
| 337 | // error node unless there are two or more error nodes with the same minimum |
| 338 | // path length. |
| 339 | ExplodedGraph<GRState>* GTrim; |
| 340 | InterExplodedGraphMap<GRState>* NMap; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 341 | |
| 342 | llvm::DenseMap<const void*, const void*> InverseMap; |
| 343 | llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 344 | |
| 345 | // Create owning pointers for GTrim and NMap just to ensure that they are |
| 346 | // released when this function exists. |
| 347 | llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim); |
| 348 | llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap); |
| 349 | |
| 350 | // Find the (first) error node in the trimmed graph. We just need to consult |
| 351 | // the node map (NMap) which maps from nodes in the original graph to nodes |
| 352 | // in the new graph. |
| 353 | const ExplodedNode<GRState>* N = 0; |
| 354 | unsigned NodeIndex = 0; |
| 355 | |
| 356 | for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I) |
| 357 | if ((N = NMap->getMappedNode(*I))) { |
| 358 | NodeIndex = (I - NStart) / sizeof(*I); |
| 359 | break; |
| 360 | } |
| 361 | |
| 362 | assert(N && "No error node found in the trimmed graph."); |
| 363 | |
| 364 | // Create a new (third!) graph with a single path. This is the graph |
| 365 | // that will be returned to the caller. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 366 | ExplodedGraph<GRState> *GNew = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 367 | new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 368 | GTrim->getContext()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 370 | // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 371 | // to the root node, and then construct a new graph that contains only |
| 372 | // a single path. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 373 | llvm::DenseMap<const void*,unsigned> Visited; |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 374 | std::queue<const ExplodedNode<GRState>*> WS; |
| 375 | WS.push(N); |
| 376 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 377 | unsigned cnt = 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 378 | const ExplodedNode<GRState>* Root = 0; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 379 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 380 | while (!WS.empty()) { |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 381 | const ExplodedNode<GRState>* Node = WS.front(); |
| 382 | WS.pop(); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 383 | |
| 384 | if (Visited.find(Node) != Visited.end()) |
| 385 | continue; |
| 386 | |
| 387 | Visited[Node] = cnt++; |
| 388 | |
| 389 | if (Node->pred_empty()) { |
| 390 | Root = Node; |
| 391 | break; |
| 392 | } |
| 393 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 394 | for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(), |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 395 | E=Node->pred_end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 396 | WS.push(*I); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 397 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 399 | assert (Root); |
| 400 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 401 | // Now walk from the root down the BFS path, always taking the successor |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 402 | // with the lowest number. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 403 | ExplodedNode<GRState> *Last = 0, *First = 0; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 404 | NodeBackMap *BM = new NodeBackMap(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 406 | for ( N = Root ;;) { |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 407 | // Lookup the number associated with the current node. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 408 | llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 409 | assert (I != Visited.end()); |
| 410 | |
| 411 | // Create the equivalent node in the new graph with the same state |
| 412 | // and location. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 413 | ExplodedNode<GRState>* NewN = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 414 | GNew->getNode(N->getLocation(), N->getState()); |
| 415 | |
| 416 | // Store the mapping to the original node. |
| 417 | llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N); |
| 418 | assert(IMitr != InverseMap.end() && "No mapping to original node."); |
| 419 | (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 420 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 421 | // Link up the new node with the previous node. |
| 422 | if (Last) |
| 423 | NewN->addPredecessor(Last); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 424 | |
| 425 | Last = NewN; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 426 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 427 | // Are we at the final node? |
| 428 | if (I->second == 0) { |
| 429 | First = NewN; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 430 | break; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 431 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 432 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 433 | // Find the next successor node. We choose the node that is marked |
| 434 | // with the lowest DFS number. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 435 | ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin(); |
| 436 | ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end(); |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 437 | N = 0; |
| 438 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 439 | for (unsigned MinVal = 0; SI != SE; ++SI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 441 | I = Visited.find(*SI); |
| 442 | |
| 443 | if (I == Visited.end()) |
| 444 | continue; |
| 445 | |
| 446 | if (!N || I->second < MinVal) { |
| 447 | N = *SI; |
| 448 | MinVal = I->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 449 | } |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 450 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 451 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 452 | assert (N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 453 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 454 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 455 | assert (First); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 456 | return std::make_pair(std::make_pair(GNew, BM), |
| 457 | std::make_pair(First, NodeIndex)); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 460 | static const VarDecl* |
| 461 | GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N, |
| 462 | GRStateManager& VMgr, SVal X) { |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 463 | |
| 464 | for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) { |
| 465 | |
| 466 | ProgramPoint P = N->getLocation(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 467 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 468 | if (!isa<PostStmt>(P)) |
| 469 | continue; |
| 470 | |
| 471 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 473 | if (!DR) |
| 474 | continue; |
| 475 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 476 | SVal Y = VMgr.GetSVal(N->getState(), DR); |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 477 | |
| 478 | if (X != Y) |
| 479 | continue; |
| 480 | |
| 481 | VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 482 | |
| 483 | if (!VD) |
| 484 | continue; |
| 485 | |
| 486 | return VD; |
| 487 | } |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 492 | namespace { |
| 493 | class VISIBILITY_HIDDEN NotableSymbolHandler |
| 494 | : public StoreManager::BindingsHandler { |
| 495 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 496 | SymbolRef Sym; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 497 | const GRState* PrevSt; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 498 | const Stmt* S; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 499 | GRStateManager& VMgr; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 500 | const ExplodedNode<GRState>* Pred; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 501 | PathDiagnostic& PD; |
| 502 | BugReporter& BR; |
| 503 | |
| 504 | public: |
| 505 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 506 | NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s, |
| 507 | GRStateManager& vmgr, const ExplodedNode<GRState>* pred, |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 508 | PathDiagnostic& pd, BugReporter& br) |
| 509 | : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {} |
| 510 | |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame] | 511 | bool HandleBinding(StoreManager& SMgr, Store store, |
| 512 | const MemRegion* R, SVal V) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 513 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 514 | SymbolRef ScanSym = 0; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 515 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 516 | if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 517 | ScanSym = SV->getSymbol(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 518 | else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V)) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 519 | ScanSym = SV->getSymbol(); |
| 520 | else |
| 521 | return true; |
| 522 | |
| 523 | if (ScanSym != Sym) |
| 524 | return true; |
| 525 | |
| 526 | // Check if the previous state has this binding. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 527 | SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R)); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 528 | |
| 529 | if (X == V) // Same binding? |
| 530 | return true; |
| 531 | |
| 532 | // Different binding. Only handle assignments for now. We don't pull |
| 533 | // this check out of the loop because we will eventually handle other |
| 534 | // cases. |
| 535 | |
| 536 | VarDecl *VD = 0; |
| 537 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 538 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 539 | if (!B->isAssignmentOp()) |
| 540 | return true; |
| 541 | |
| 542 | // What variable did we assign to? |
| 543 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts()); |
| 544 | |
| 545 | if (!DR) |
| 546 | return true; |
| 547 | |
| 548 | VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 549 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 550 | else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) { |
Ted Kremenek | f21a4b4 | 2008-10-06 18:37:46 +0000 | [diff] [blame] | 551 | // FIXME: Eventually CFGs won't have DeclStmts. Right now we |
| 552 | // assume that each DeclStmt has a single Decl. This invariant |
| 553 | // holds by contruction in the CFG. |
| 554 | VD = dyn_cast<VarDecl>(*DS->decl_begin()); |
| 555 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 556 | |
| 557 | if (!VD) |
| 558 | return true; |
| 559 | |
| 560 | // What is the most recently referenced variable with this binding? |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 561 | const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 562 | |
| 563 | if (!MostRecent) |
| 564 | return true; |
| 565 | |
| 566 | // Create the diagnostic. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 567 | FullSourceLoc L(S->getLocStart(), BR.getSourceManager()); |
| 568 | |
Ted Kremenek | 3daea0a | 2009-02-26 20:29:19 +0000 | [diff] [blame] | 569 | if (Loc::IsLocType(VD->getType())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 570 | std::string msg = "'" + std::string(VD->getNameAsString()) + |
| 571 | "' now aliases '" + MostRecent->getNameAsString() + "'"; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 572 | |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 573 | PD.push_front(new PathDiagnosticEventPiece(L, msg)); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | return true; |
| 577 | } |
| 578 | }; |
| 579 | } |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 580 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 581 | static void HandleNotableSymbol(const ExplodedNode<GRState>* N, |
| 582 | const Stmt* S, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 583 | SymbolRef Sym, BugReporter& BR, |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 584 | PathDiagnostic& PD) { |
| 585 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 586 | const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin(); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 587 | const GRState* PrevSt = Pred ? Pred->getState() : 0; |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 588 | |
| 589 | if (!PrevSt) |
| 590 | return; |
| 591 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 592 | // Look at the region bindings of the current state that map to the |
| 593 | // specified symbol. Are any of them not in the previous state? |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 594 | GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 595 | NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR); |
| 596 | cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H); |
| 597 | } |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 598 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 599 | namespace { |
| 600 | class VISIBILITY_HIDDEN ScanNotableSymbols |
| 601 | : public StoreManager::BindingsHandler { |
| 602 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 603 | llvm::SmallSet<SymbolRef, 10> AlreadyProcessed; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 604 | const ExplodedNode<GRState>* N; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 605 | Stmt* S; |
| 606 | GRBugReporter& BR; |
| 607 | PathDiagnostic& PD; |
| 608 | |
| 609 | public: |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 610 | ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br, |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 611 | PathDiagnostic& pd) |
| 612 | : N(n), S(s), BR(br), PD(pd) {} |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 613 | |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame] | 614 | bool HandleBinding(StoreManager& SMgr, Store store, |
| 615 | const MemRegion* R, SVal V) { |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 616 | SymbolRef ScanSym = 0; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 617 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 618 | if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 619 | ScanSym = SV->getSymbol(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 620 | else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V)) |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 621 | ScanSym = SV->getSymbol(); |
| 622 | else |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 623 | return true; |
| 624 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 625 | assert (ScanSym); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 626 | |
| 627 | if (!BR.isNotable(ScanSym)) |
| 628 | return true; |
| 629 | |
| 630 | if (AlreadyProcessed.count(ScanSym)) |
| 631 | return true; |
| 632 | |
| 633 | AlreadyProcessed.insert(ScanSym); |
| 634 | |
| 635 | HandleNotableSymbol(N, S, ScanSym, BR, PD); |
| 636 | return true; |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 637 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 638 | }; |
| 639 | } // end anonymous namespace |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 641 | namespace { |
| 642 | class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver { |
| 643 | NodeBackMap& M; |
| 644 | public: |
| 645 | NodeMapClosure(NodeBackMap *m) : M(*m) {} |
| 646 | ~NodeMapClosure() {} |
| 647 | |
| 648 | const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) { |
| 649 | NodeBackMap::iterator I = M.find(N); |
| 650 | return I == M.end() ? 0 : I->second; |
| 651 | } |
| 652 | }; |
| 653 | } |
| 654 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 655 | /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object |
| 656 | /// and collapses PathDiagosticPieces that are expanded by macros. |
| 657 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) { |
| 658 | typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> > |
| 659 | MacroStackTy; |
| 660 | |
| 661 | typedef std::vector<PathDiagnosticPiece*> |
| 662 | PiecesTy; |
| 663 | |
| 664 | MacroStackTy MacroStack; |
| 665 | PiecesTy Pieces; |
| 666 | |
| 667 | for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) { |
| 668 | // Get the location of the PathDiagnosticPiece. |
| 669 | const FullSourceLoc Loc = I->getLocation(); |
| 670 | |
| 671 | // Determine the instantiation location, which is the location we group |
| 672 | // related PathDiagnosticPieces. |
| 673 | SourceLocation InstantiationLoc = Loc.isMacroID() ? |
| 674 | SM.getInstantiationLoc(Loc) : |
| 675 | SourceLocation(); |
| 676 | |
| 677 | if (Loc.isFileID()) { |
| 678 | MacroStack.clear(); |
| 679 | Pieces.push_back(&*I); |
| 680 | continue; |
| 681 | } |
| 682 | |
| 683 | assert(Loc.isMacroID()); |
| 684 | |
| 685 | // Is the PathDiagnosticPiece within the same macro group? |
| 686 | if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { |
| 687 | MacroStack.back().first->push_back(&*I); |
| 688 | continue; |
| 689 | } |
| 690 | |
| 691 | // We aren't in the same group. Are we descending into a new macro |
| 692 | // or are part of an old one? |
| 693 | PathDiagnosticMacroPiece *MacroGroup = 0; |
| 694 | |
| 695 | SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? |
| 696 | SM.getInstantiationLoc(Loc) : |
| 697 | SourceLocation(); |
| 698 | |
| 699 | // Walk the entire macro stack. |
| 700 | while (!MacroStack.empty()) { |
| 701 | if (InstantiationLoc == MacroStack.back().second) { |
| 702 | MacroGroup = MacroStack.back().first; |
| 703 | break; |
| 704 | } |
| 705 | |
| 706 | if (ParentInstantiationLoc == MacroStack.back().second) { |
| 707 | MacroGroup = MacroStack.back().first; |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | MacroStack.pop_back(); |
| 712 | } |
| 713 | |
| 714 | if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { |
| 715 | // Create a new macro group and add it to the stack. |
| 716 | PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc); |
| 717 | |
| 718 | if (MacroGroup) |
| 719 | MacroGroup->push_back(NewGroup); |
| 720 | else { |
| 721 | assert(InstantiationLoc.isFileID()); |
| 722 | Pieces.push_back(NewGroup); |
| 723 | } |
| 724 | |
| 725 | MacroGroup = NewGroup; |
| 726 | MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); |
| 727 | } |
| 728 | |
| 729 | // Finally, add the PathDiagnosticPiece to the group. |
| 730 | MacroGroup->push_back(&*I); |
| 731 | } |
| 732 | |
| 733 | // Now take the pieces and construct a new PathDiagnostic. |
| 734 | PD.resetPath(false); |
| 735 | |
| 736 | for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) { |
| 737 | if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 738 | if (!MP->containsEvent()) { |
| 739 | delete MP; |
| 740 | continue; |
| 741 | } |
| 742 | |
| 743 | PD.push_back(*I); |
| 744 | } |
| 745 | } |
| 746 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 747 | void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 748 | BugReportEquivClass& EQ) { |
| 749 | |
| 750 | std::vector<const ExplodedNode<GRState>*> Nodes; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 751 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 752 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { |
| 753 | const ExplodedNode<GRState>* N = I->getEndNode(); |
| 754 | if (N) Nodes.push_back(N); |
| 755 | } |
| 756 | |
| 757 | if (Nodes.empty()) |
| 758 | return; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 759 | |
| 760 | // Construct a new graph that contains only a single path from the error |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 761 | // node to a root. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 762 | const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 763 | std::pair<ExplodedNode<GRState>*, unsigned> >& |
| 764 | GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size()); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 765 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 766 | // Find the BugReport with the original location. |
| 767 | BugReport *R = 0; |
| 768 | unsigned i = 0; |
| 769 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i) |
| 770 | if (i == GPair.second.second) { R = *I; break; } |
| 771 | |
| 772 | assert(R && "No original report found for sliced graph."); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 773 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 774 | llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first); |
| 775 | llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 776 | const ExplodedNode<GRState> *N = GPair.second.first; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 777 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 778 | // Start building the path diagnostic... |
| 779 | if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N)) |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 780 | PD.push_back(Piece); |
| 781 | else |
| 782 | return; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 783 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 784 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 785 | ? NULL : *(N->pred_begin()); |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 786 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 787 | ASTContext& Ctx = getContext(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 788 | SourceManager& SMgr = Ctx.getSourceManager(); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 789 | NodeMapClosure NMC(BackMap.get()); |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 790 | PathDiagnosticBuilder PDB(SMgr, getStateManager().getCodeDecl(), |
| 791 | getPathDiagnosticClient()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 792 | |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 793 | while (NextNode) { |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 794 | N = NextNode; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 795 | NextNode = GetPredecessorNode(N); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 796 | |
| 797 | ProgramPoint P = N->getLocation(); |
| 798 | |
| 799 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 800 | CFGBlock* Src = BE->getSrc(); |
| 801 | CFGBlock* Dst = BE->getDst(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 802 | Stmt* T = Src->getTerminator(); |
| 803 | |
| 804 | if (!T) |
| 805 | continue; |
| 806 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 807 | FullSourceLoc Start(T->getLocStart(), SMgr); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 808 | |
| 809 | switch (T->getStmtClass()) { |
| 810 | default: |
| 811 | break; |
| 812 | |
| 813 | case Stmt::GotoStmtClass: |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 814 | case Stmt::IndirectGotoStmtClass: { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 815 | Stmt* S = GetNextStmt(N); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 816 | |
| 817 | if (!S) |
| 818 | continue; |
| 819 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 820 | std::string sbuf; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 821 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 822 | const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 823 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 824 | os << "Control jumps to line " |
| 825 | << End.asLocation().getInstantiationLineNumber(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 826 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 827 | os.str())); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 828 | break; |
| 829 | } |
| 830 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 831 | case Stmt::SwitchStmtClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 832 | // Figure out what case arm we took. |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 833 | std::string sbuf; |
| 834 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 835 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 836 | if (Stmt* S = Dst->getLabel()) { |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 837 | PathDiagnosticLocation End(S, SMgr); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 838 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 839 | switch (S->getStmtClass()) { |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 840 | default: |
| 841 | os << "No cases match in the switch statement. " |
| 842 | "Control jumps to line " |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 843 | << End.asLocation().getInstantiationLineNumber(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 844 | break; |
| 845 | case Stmt::DefaultStmtClass: |
| 846 | os << "Control jumps to the 'default' case at line " |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 847 | << End.asLocation().getInstantiationLineNumber(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 848 | break; |
| 849 | |
| 850 | case Stmt::CaseStmtClass: { |
| 851 | os << "Control jumps to 'case "; |
| 852 | CaseStmt* Case = cast<CaseStmt>(S); |
| 853 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
| 854 | |
| 855 | // Determine if it is an enum. |
| 856 | bool GetRawInt = true; |
| 857 | |
| 858 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 859 | // FIXME: Maybe this should be an assertion. Are there cases |
| 860 | // were it is not an EnumConstantDecl? |
| 861 | EnumConstantDecl* D = |
| 862 | dyn_cast<EnumConstantDecl>(DR->getDecl()); |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 863 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 864 | if (D) { |
| 865 | GetRawInt = false; |
| 866 | os << D->getNameAsString(); |
| 867 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 868 | } |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 869 | |
| 870 | if (GetRawInt) { |
| 871 | |
| 872 | // Not an enum. |
| 873 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 874 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 875 | llvm::APSInt V(bits, false); |
| 876 | |
| 877 | if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) { |
| 878 | assert (false && "Case condition must be constant."); |
| 879 | continue; |
| 880 | } |
| 881 | |
| 882 | os << V; |
| 883 | } |
| 884 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 885 | os << ":' at line " |
| 886 | << End.asLocation().getInstantiationLineNumber(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 887 | break; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 888 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 889 | } |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 890 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 891 | os.str())); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 892 | } |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 893 | else { |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 894 | os << "'Default' branch taken. "; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 895 | const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N); |
| 896 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 897 | os.str())); |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 898 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 899 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 900 | break; |
| 901 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 902 | |
| 903 | case Stmt::BreakStmtClass: |
| 904 | case Stmt::ContinueStmtClass: { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 905 | std::string sbuf; |
| 906 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 907 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 908 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 909 | os.str())); |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 910 | break; |
| 911 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 912 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 913 | // Determine control-flow for ternary '?'. |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 914 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 915 | std::string sbuf; |
| 916 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 917 | os << "'?' condition evaluates to "; |
| 918 | |
| 919 | if (*(Src->succ_begin()+1) == Dst) |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 920 | os << "false"; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 921 | else |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 922 | os << "true"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 923 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 924 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 925 | |
| 926 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 927 | os.str())); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 928 | break; |
| 929 | } |
| 930 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 931 | // Determine control-flow for short-circuited '&&' and '||'. |
| 932 | case Stmt::BinaryOperatorClass: { |
| 933 | if (!PDB.supportsLogicalOpControlFlow()) |
| 934 | break; |
| 935 | |
| 936 | BinaryOperator *B = cast<BinaryOperator>(T); |
| 937 | std::string sbuf; |
| 938 | llvm::raw_string_ostream os(sbuf); |
| 939 | os << "Left side of '"; |
| 940 | |
| 941 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 942 | os << "&&"; |
| 943 | } |
| 944 | else { |
| 945 | assert(B->getOpcode() == BinaryOperator::LOr); |
| 946 | os << "||"; |
| 947 | } |
| 948 | |
| 949 | os << "' is "; |
| 950 | if (*(Src->succ_begin()+1) == Dst) |
| 951 | os << (B->getOpcode() == BinaryOperator::LAnd |
| 952 | ? "false" : "true"); |
| 953 | else |
| 954 | os << (B->getOpcode() == BinaryOperator::LAnd |
| 955 | ? "true" : "false"); |
| 956 | |
| 957 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 958 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 959 | |
| 960 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 961 | os.str())); |
| 962 | break; |
| 963 | } |
| 964 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 965 | case Stmt::DoStmtClass: { |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 966 | if (*(Src->succ_begin()) == Dst) { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 967 | std::string sbuf; |
| 968 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 969 | |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 970 | os << "Loop condition is true. "; |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 971 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 972 | |
| 973 | if (const Stmt *S = End.asStmt()) |
| 974 | End = PDB.getEnclosingStmtLocation(S); |
| 975 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 976 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 977 | os.str())); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 978 | } |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 979 | else { |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 980 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 981 | |
| 982 | if (const Stmt *S = End.asStmt()) |
| 983 | End = PDB.getEnclosingStmtLocation(S); |
| 984 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 985 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 986 | "Loop condition is false. Exiting loop")); |
| 987 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 988 | |
| 989 | break; |
| 990 | } |
| 991 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 992 | case Stmt::WhileStmtClass: |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 993 | case Stmt::ForStmtClass: { |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 994 | if (*(Src->succ_begin()+1) == Dst) { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 995 | std::string sbuf; |
| 996 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 997 | |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 998 | os << "Loop condition is false. "; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 999 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1000 | if (const Stmt *S = End.asStmt()) |
| 1001 | End = PDB.getEnclosingStmtLocation(S); |
| 1002 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1003 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1004 | os.str())); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1005 | } |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1006 | else { |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 1007 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1008 | if (const Stmt *S = End.asStmt()) |
| 1009 | End = PDB.getEnclosingStmtLocation(S); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1010 | |
| 1011 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1012 | "Loop condition is true. Entering loop body")); |
| 1013 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1014 | |
| 1015 | break; |
| 1016 | } |
| 1017 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1018 | case Stmt::IfStmtClass: { |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1019 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 1020 | |
| 1021 | if (const Stmt *S = End.asStmt()) |
| 1022 | End = PDB.getEnclosingStmtLocation(S); |
| 1023 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1024 | if (*(Src->succ_begin()+1) == Dst) |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1025 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1026 | "Taking false branch")); |
Ted Kremenek | 025fedc | 2009-03-02 21:41:18 +0000 | [diff] [blame] | 1027 | else |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1028 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1029 | "Taking true branch")); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1030 | |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 1034 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 1035 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1036 | if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this, |
| 1037 | NMC)) |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1038 | PD.push_front(p); |
| 1039 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1040 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 1041 | // Scan the region bindings, and see if a "notable" symbol has a new |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1042 | // lval binding. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1043 | ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD); |
| 1044 | getStateManager().iterBindings(N->getState(), SNS); |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1045 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1046 | } |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1047 | |
| 1048 | // After constructing the full PathDiagnostic, do a pass over it to compact |
| 1049 | // PathDiagnosticPieces that occur within a macro. |
| 1050 | CompactPathDiagnostic(PD, getSourceManager()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1053 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1054 | void BugReporter::Register(BugType *BT) { |
| 1055 | BugTypes = F.Add(BugTypes, BT); |
Ted Kremenek | 76d90c8 | 2008-05-16 18:33:14 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1058 | void BugReporter::EmitReport(BugReport* R) { |
| 1059 | // Compute the bug report's hash to determine its equivalence class. |
| 1060 | llvm::FoldingSetNodeID ID; |
| 1061 | R->Profile(ID); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1062 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1063 | // Lookup the equivance class. If there isn't one, create it. |
| 1064 | BugType& BT = R->getBugType(); |
| 1065 | Register(&BT); |
| 1066 | void *InsertPos; |
| 1067 | BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos); |
| 1068 | |
| 1069 | if (!EQ) { |
| 1070 | EQ = new BugReportEquivClass(R); |
| 1071 | BT.EQClasses.InsertNode(EQ, InsertPos); |
| 1072 | } |
| 1073 | else |
| 1074 | EQ->AddReport(R); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1077 | void BugReporter::FlushReport(BugReportEquivClass& EQ) { |
| 1078 | assert(!EQ.Reports.empty()); |
| 1079 | BugReport &R = **EQ.begin(); |
| 1080 | |
| 1081 | // FIXME: Make sure we use the 'R' for the path that was actually used. |
| 1082 | // Probably doesn't make a difference in practice. |
| 1083 | BugType& BT = R.getBugType(); |
| 1084 | |
| 1085 | llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(), |
| 1086 | R.getDescription(), |
| 1087 | BT.getCategory())); |
| 1088 | GeneratePathDiagnostic(*D.get(), EQ); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1089 | |
| 1090 | // Get the meta data. |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1091 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1092 | for (const char** s = Meta.first; s != Meta.second; ++s) D->addMeta(*s); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 1093 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1094 | // Emit a summary diagnostic to the regular Diagnostics engine. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 1095 | PathDiagnosticClient* PD = getPathDiagnosticClient(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1096 | const SourceRange *Beg = 0, *End = 0; |
| 1097 | R.getRanges(*this, Beg, End); |
| 1098 | Diagnostic& Diag = getDiagnostic(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1099 | FullSourceLoc L(R.getLocation(), getSourceManager()); |
Ted Kremenek | d90e708 | 2009-02-07 22:36:41 +0000 | [diff] [blame] | 1100 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 1101 | R.getDescription().c_str()); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1102 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1103 | switch (End-Beg) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1104 | default: assert(0 && "Don't handle this many ranges yet!"); |
| 1105 | case 0: Diag.Report(L, ErrorDiag); break; |
| 1106 | case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break; |
| 1107 | case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break; |
| 1108 | case 3: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1] << Beg[2]; break; |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 1109 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
| 1112 | if (!PD) |
| 1113 | return; |
| 1114 | |
| 1115 | if (D->empty()) { |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 1116 | PathDiagnosticPiece* piece = |
| 1117 | new PathDiagnosticEventPiece(L, R.getDescription()); |
| 1118 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1119 | for ( ; Beg != End; ++Beg) piece->addRange(*Beg); |
| 1120 | D->push_back(piece); |
| 1121 | } |
| 1122 | |
| 1123 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1124 | } |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1125 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1126 | void BugReporter::EmitBasicReport(const char* name, const char* str, |
| 1127 | SourceLocation Loc, |
| 1128 | SourceRange* RBeg, unsigned NumRanges) { |
| 1129 | EmitBasicReport(name, "", str, Loc, RBeg, NumRanges); |
| 1130 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1131 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1132 | void BugReporter::EmitBasicReport(const char* name, const char* category, |
| 1133 | const char* str, SourceLocation Loc, |
| 1134 | SourceRange* RBeg, unsigned NumRanges) { |
| 1135 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1136 | // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'. |
| 1137 | BugType *BT = new BugType(name, category); |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1138 | FullSourceLoc L = getContext().getFullLoc(Loc); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1139 | RangedBugReport *R = new DiagBugReport(*BT, str, L); |
| 1140 | for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg); |
| 1141 | EmitReport(R); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1142 | } |