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