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/AST/ASTContext.h" |
| 18 | #include "clang/AST/CFG.h" |
| 19 | #include "clang/AST/Expr.h" |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/ParentMap.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtObjC.h" |
| 22 | #include "clang/Basic/SourceManager.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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 34 | // Helper routines for walking the ExplodedGraph and fetching statements. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 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)) |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 66 | if (Stmt *S = GetStmt(N->getLocation())) { |
| 67 | // Check if the statement is '?' or '&&'/'||'. These are "merges", |
| 68 | // not actual statement points. |
| 69 | switch (S->getStmtClass()) { |
| 70 | case Stmt::ChooseExprClass: |
| 71 | case Stmt::ConditionalOperatorClass: continue; |
| 72 | case Stmt::BinaryOperatorClass: { |
| 73 | BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode(); |
| 74 | if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr) |
| 75 | continue; |
| 76 | break; |
| 77 | } |
| 78 | default: |
| 79 | break; |
| 80 | } |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 81 | return S; |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) { |
| 88 | if (Stmt *S = GetStmt(N->getLocation())) |
| 89 | return S; |
| 90 | |
| 91 | return GetPreviousStmt(N); |
| 92 | } |
| 93 | |
| 94 | static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) { |
| 95 | if (Stmt *S = GetStmt(N->getLocation())) |
| 96 | return S; |
| 97 | |
| 98 | return GetNextStmt(N); |
| 99 | } |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 102 | // PathDiagnosticBuilder and its associated routines and helper objects. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 103 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 105 | typedef llvm::DenseMap<const ExplodedNode<GRState>*, |
| 106 | const ExplodedNode<GRState>*> NodeBackMap; |
| 107 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 108 | namespace { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 109 | class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver { |
| 110 | NodeBackMap& M; |
| 111 | public: |
| 112 | NodeMapClosure(NodeBackMap *m) : M(*m) {} |
| 113 | ~NodeMapClosure() {} |
| 114 | |
| 115 | const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) { |
| 116 | NodeBackMap::iterator I = M.find(N); |
| 117 | return I == M.end() ? 0 : I->second; |
| 118 | } |
| 119 | }; |
| 120 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 121 | class VISIBILITY_HIDDEN PathDiagnosticBuilder { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 122 | GRBugReporter &BR; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 123 | SourceManager &SMgr; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 124 | ExplodedGraph<GRState> *ReportGraph; |
| 125 | BugReport *R; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 126 | const Decl& CodeDecl; |
| 127 | PathDiagnosticClient *PDC; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 128 | llvm::OwningPtr<ParentMap> PM; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 129 | NodeMapClosure NMC; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 130 | public: |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 131 | PathDiagnosticBuilder(GRBugReporter &br, ExplodedGraph<GRState> *reportGraph, |
| 132 | BugReport *r, NodeBackMap *Backmap, |
| 133 | const Decl& codedecl, PathDiagnosticClient *pdc) |
| 134 | : BR(br), SMgr(BR.getSourceManager()), ReportGraph(reportGraph), R(r), |
| 135 | CodeDecl(codedecl), PDC(pdc), NMC(Backmap) {} |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 137 | PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N); |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 139 | PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os, |
| 140 | const ExplodedNode<GRState>* N); |
| 141 | |
| 142 | ParentMap& getParentMap() { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 143 | if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody(getContext()))); |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 144 | return *PM.get(); |
| 145 | } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 147 | const Stmt *getParent(const Stmt *S) { |
| 148 | return getParentMap().getParent(S); |
| 149 | } |
| 150 | |
Ted Kremenek | 51a735c | 2009-04-01 17:52:26 +0000 | [diff] [blame] | 151 | const CFG& getCFG() { |
| 152 | return *BR.getCFG(); |
| 153 | } |
| 154 | |
| 155 | const Decl& getCodeDecl() { |
| 156 | return BR.getStateManager().getCodeDecl(); |
| 157 | } |
| 158 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 159 | ExplodedGraph<GRState>& getGraph() { return *ReportGraph; } |
| 160 | NodeMapClosure& getNodeMapClosure() { return NMC; } |
| 161 | ASTContext& getContext() { return BR.getContext(); } |
| 162 | SourceManager& getSourceManager() { return SMgr; } |
| 163 | BugReport& getReport() { return *R; } |
| 164 | GRBugReporter& getBugReporter() { return BR; } |
| 165 | GRStateManager& getStateManager() { return BR.getStateManager(); } |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 167 | PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S); |
| 168 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 169 | PathDiagnosticLocation |
| 170 | getEnclosingStmtLocation(const PathDiagnosticLocation &L) { |
| 171 | if (const Stmt *S = L.asStmt()) |
| 172 | return getEnclosingStmtLocation(S); |
| 173 | |
| 174 | return L; |
| 175 | } |
| 176 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 177 | PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const { |
| 178 | return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive; |
| 179 | } |
| 180 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 181 | bool supportsLogicalOpControlFlow() const { |
| 182 | return PDC ? PDC->supportsLogicalOpControlFlow() : true; |
| 183 | } |
| 184 | }; |
| 185 | } // end anonymous namespace |
| 186 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 187 | PathDiagnosticLocation |
| 188 | PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) { |
| 189 | if (Stmt *S = GetNextStmt(N)) |
| 190 | return PathDiagnosticLocation(S, SMgr); |
| 191 | |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 192 | return FullSourceLoc(CodeDecl.getBodyRBrace(getContext()), SMgr); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 195 | PathDiagnosticLocation |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 196 | PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os, |
| 197 | const ExplodedNode<GRState>* N) { |
| 198 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 199 | // Slow, but probably doesn't matter. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 200 | if (os.str().empty()) |
| 201 | os << ' '; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 202 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 203 | const PathDiagnosticLocation &Loc = ExecutionContinues(N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 205 | if (Loc.asStmt()) |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 206 | os << "Execution continues on line " |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 207 | << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.'; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 208 | else |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 209 | os << "Execution jumps to the end of the " |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 210 | << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.'; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 211 | |
| 212 | return Loc; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 215 | PathDiagnosticLocation |
| 216 | PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) { |
| 217 | assert(S && "Null Stmt* passed to getEnclosingStmtLocation"); |
| 218 | ParentMap &P = getParentMap(); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 219 | |
| 220 | while (isa<DeclStmt>(S) || isa<Expr>(S)) { |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 221 | const Stmt *Parent = P.getParent(S); |
| 222 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 223 | if (!Parent) |
| 224 | break; |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 226 | switch (Parent->getStmtClass()) { |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 227 | case Stmt::BinaryOperatorClass: { |
| 228 | const BinaryOperator *B = cast<BinaryOperator>(Parent); |
| 229 | if (B->isLogicalOp()) |
| 230 | return PathDiagnosticLocation(S, SMgr); |
| 231 | break; |
| 232 | } |
| 233 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 234 | case Stmt::CompoundStmtClass: |
| 235 | case Stmt::StmtExprClass: |
Ted Kremenek | 1d9a23a | 2009-03-28 04:08:14 +0000 | [diff] [blame] | 236 | return PathDiagnosticLocation(S, SMgr); |
| 237 | case Stmt::ChooseExprClass: |
| 238 | // Similar to '?' if we are referring to condition, just have the edge |
| 239 | // point to the entire choose expression. |
| 240 | if (cast<ChooseExpr>(Parent)->getCond() == S) |
| 241 | return PathDiagnosticLocation(Parent, SMgr); |
| 242 | else |
| 243 | return PathDiagnosticLocation(S, SMgr); |
| 244 | case Stmt::ConditionalOperatorClass: |
| 245 | // For '?', if we are referring to condition, just have the edge point |
| 246 | // to the entire '?' expression. |
| 247 | if (cast<ConditionalOperator>(Parent)->getCond() == S) |
| 248 | return PathDiagnosticLocation(Parent, SMgr); |
| 249 | else |
| 250 | return PathDiagnosticLocation(S, SMgr); |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 251 | case Stmt::DoStmtClass: |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 252 | return PathDiagnosticLocation(S, SMgr); |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 253 | case Stmt::ForStmtClass: |
| 254 | if (cast<ForStmt>(Parent)->getBody() == S) |
| 255 | return PathDiagnosticLocation(S, SMgr); |
| 256 | break; |
| 257 | case Stmt::IfStmtClass: |
| 258 | if (cast<IfStmt>(Parent)->getCond() != S) |
| 259 | return PathDiagnosticLocation(S, SMgr); |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 260 | break; |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 261 | case Stmt::ObjCForCollectionStmtClass: |
| 262 | if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S) |
| 263 | return PathDiagnosticLocation(S, SMgr); |
| 264 | break; |
| 265 | case Stmt::WhileStmtClass: |
| 266 | if (cast<WhileStmt>(Parent)->getCond() != S) |
| 267 | return PathDiagnosticLocation(S, SMgr); |
| 268 | break; |
| 269 | default: |
| 270 | break; |
| 271 | } |
| 272 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 273 | S = Parent; |
| 274 | } |
| 275 | |
| 276 | assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); |
| 277 | return PathDiagnosticLocation(S, SMgr); |
| 278 | } |
| 279 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 280 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 281 | // ScanNotableSymbols: closure-like callback for scanning Store bindings. |
| 282 | //===----------------------------------------------------------------------===// |
| 283 | |
| 284 | static const VarDecl* |
| 285 | GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N, |
| 286 | GRStateManager& VMgr, SVal X) { |
| 287 | |
| 288 | for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) { |
| 289 | |
| 290 | ProgramPoint P = N->getLocation(); |
| 291 | |
| 292 | if (!isa<PostStmt>(P)) |
| 293 | continue; |
| 294 | |
| 295 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt()); |
| 296 | |
| 297 | if (!DR) |
| 298 | continue; |
| 299 | |
| 300 | SVal Y = VMgr.GetSVal(N->getState(), DR); |
| 301 | |
| 302 | if (X != Y) |
| 303 | continue; |
| 304 | |
| 305 | VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 306 | |
| 307 | if (!VD) |
| 308 | continue; |
| 309 | |
| 310 | return VD; |
| 311 | } |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | namespace { |
| 317 | class VISIBILITY_HIDDEN NotableSymbolHandler |
| 318 | : public StoreManager::BindingsHandler { |
| 319 | |
| 320 | SymbolRef Sym; |
| 321 | const GRState* PrevSt; |
| 322 | const Stmt* S; |
| 323 | GRStateManager& VMgr; |
| 324 | const ExplodedNode<GRState>* Pred; |
| 325 | PathDiagnostic& PD; |
| 326 | BugReporter& BR; |
| 327 | |
| 328 | public: |
| 329 | |
| 330 | NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s, |
| 331 | GRStateManager& vmgr, const ExplodedNode<GRState>* pred, |
| 332 | PathDiagnostic& pd, BugReporter& br) |
| 333 | : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {} |
| 334 | |
| 335 | bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, |
| 336 | SVal V) { |
| 337 | |
| 338 | SymbolRef ScanSym = V.getAsSymbol(); |
| 339 | |
| 340 | if (ScanSym != Sym) |
| 341 | return true; |
| 342 | |
| 343 | // Check if the previous state has this binding. |
| 344 | SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R)); |
| 345 | |
| 346 | if (X == V) // Same binding? |
| 347 | return true; |
| 348 | |
| 349 | // Different binding. Only handle assignments for now. We don't pull |
| 350 | // this check out of the loop because we will eventually handle other |
| 351 | // cases. |
| 352 | |
| 353 | VarDecl *VD = 0; |
| 354 | |
| 355 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 356 | if (!B->isAssignmentOp()) |
| 357 | return true; |
| 358 | |
| 359 | // What variable did we assign to? |
| 360 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts()); |
| 361 | |
| 362 | if (!DR) |
| 363 | return true; |
| 364 | |
| 365 | VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 366 | } |
| 367 | else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) { |
| 368 | // FIXME: Eventually CFGs won't have DeclStmts. Right now we |
| 369 | // assume that each DeclStmt has a single Decl. This invariant |
| 370 | // holds by contruction in the CFG. |
| 371 | VD = dyn_cast<VarDecl>(*DS->decl_begin()); |
| 372 | } |
| 373 | |
| 374 | if (!VD) |
| 375 | return true; |
| 376 | |
| 377 | // What is the most recently referenced variable with this binding? |
| 378 | const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V); |
| 379 | |
| 380 | if (!MostRecent) |
| 381 | return true; |
| 382 | |
| 383 | // Create the diagnostic. |
| 384 | FullSourceLoc L(S->getLocStart(), BR.getSourceManager()); |
| 385 | |
| 386 | if (Loc::IsLocType(VD->getType())) { |
| 387 | std::string msg = "'" + std::string(VD->getNameAsString()) + |
| 388 | "' now aliases '" + MostRecent->getNameAsString() + "'"; |
| 389 | |
| 390 | PD.push_front(new PathDiagnosticEventPiece(L, msg)); |
| 391 | } |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | }; |
| 396 | } |
| 397 | |
| 398 | static void HandleNotableSymbol(const ExplodedNode<GRState>* N, |
| 399 | const Stmt* S, |
| 400 | SymbolRef Sym, BugReporter& BR, |
| 401 | PathDiagnostic& PD) { |
| 402 | |
| 403 | const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin(); |
| 404 | const GRState* PrevSt = Pred ? Pred->getState() : 0; |
| 405 | |
| 406 | if (!PrevSt) |
| 407 | return; |
| 408 | |
| 409 | // Look at the region bindings of the current state that map to the |
| 410 | // specified symbol. Are any of them not in the previous state? |
| 411 | GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager(); |
| 412 | NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR); |
| 413 | cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H); |
| 414 | } |
| 415 | |
| 416 | namespace { |
| 417 | class VISIBILITY_HIDDEN ScanNotableSymbols |
| 418 | : public StoreManager::BindingsHandler { |
| 419 | |
| 420 | llvm::SmallSet<SymbolRef, 10> AlreadyProcessed; |
| 421 | const ExplodedNode<GRState>* N; |
| 422 | Stmt* S; |
| 423 | GRBugReporter& BR; |
| 424 | PathDiagnostic& PD; |
| 425 | |
| 426 | public: |
| 427 | ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br, |
| 428 | PathDiagnostic& pd) |
| 429 | : N(n), S(s), BR(br), PD(pd) {} |
| 430 | |
| 431 | bool HandleBinding(StoreManager& SMgr, Store store, |
| 432 | const MemRegion* R, SVal V) { |
| 433 | |
| 434 | SymbolRef ScanSym = V.getAsSymbol(); |
| 435 | |
| 436 | if (!ScanSym) |
| 437 | return true; |
| 438 | |
| 439 | if (!BR.isNotable(ScanSym)) |
| 440 | return true; |
| 441 | |
| 442 | if (AlreadyProcessed.count(ScanSym)) |
| 443 | return true; |
| 444 | |
| 445 | AlreadyProcessed.insert(ScanSym); |
| 446 | |
| 447 | HandleNotableSymbol(N, S, ScanSym, BR, PD); |
| 448 | return true; |
| 449 | } |
| 450 | }; |
| 451 | } // end anonymous namespace |
| 452 | |
| 453 | //===----------------------------------------------------------------------===// |
| 454 | // "Minimal" path diagnostic generation algorithm. |
| 455 | //===----------------------------------------------------------------------===// |
| 456 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 457 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM); |
| 458 | |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 459 | static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD, |
| 460 | PathDiagnosticBuilder &PDB, |
| 461 | const ExplodedNode<GRState> *N) { |
| 462 | ASTContext& Ctx = PDB.getContext(); |
| 463 | SourceManager& SMgr = PDB.getSourceManager(); |
| 464 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 465 | ? NULL : *(N->pred_begin()); |
| 466 | while (NextNode) { |
| 467 | N = NextNode; |
| 468 | NextNode = GetPredecessorNode(N); |
| 469 | |
| 470 | ProgramPoint P = N->getLocation(); |
| 471 | |
| 472 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 473 | CFGBlock* Src = BE->getSrc(); |
| 474 | CFGBlock* Dst = BE->getDst(); |
| 475 | Stmt* T = Src->getTerminator(); |
| 476 | |
| 477 | if (!T) |
| 478 | continue; |
| 479 | |
| 480 | FullSourceLoc Start(T->getLocStart(), SMgr); |
| 481 | |
| 482 | switch (T->getStmtClass()) { |
| 483 | default: |
| 484 | break; |
| 485 | |
| 486 | case Stmt::GotoStmtClass: |
| 487 | case Stmt::IndirectGotoStmtClass: { |
| 488 | Stmt* S = GetNextStmt(N); |
| 489 | |
| 490 | if (!S) |
| 491 | continue; |
| 492 | |
| 493 | std::string sbuf; |
| 494 | llvm::raw_string_ostream os(sbuf); |
| 495 | const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); |
| 496 | |
| 497 | os << "Control jumps to line " |
| 498 | << End.asLocation().getInstantiationLineNumber(); |
| 499 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 500 | os.str())); |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | case Stmt::SwitchStmtClass: { |
| 505 | // Figure out what case arm we took. |
| 506 | std::string sbuf; |
| 507 | llvm::raw_string_ostream os(sbuf); |
| 508 | |
| 509 | if (Stmt* S = Dst->getLabel()) { |
| 510 | PathDiagnosticLocation End(S, SMgr); |
| 511 | |
| 512 | switch (S->getStmtClass()) { |
| 513 | default: |
| 514 | os << "No cases match in the switch statement. " |
| 515 | "Control jumps to line " |
| 516 | << End.asLocation().getInstantiationLineNumber(); |
| 517 | break; |
| 518 | case Stmt::DefaultStmtClass: |
| 519 | os << "Control jumps to the 'default' case at line " |
| 520 | << End.asLocation().getInstantiationLineNumber(); |
| 521 | break; |
| 522 | |
| 523 | case Stmt::CaseStmtClass: { |
| 524 | os << "Control jumps to 'case "; |
| 525 | CaseStmt* Case = cast<CaseStmt>(S); |
| 526 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
| 527 | |
| 528 | // Determine if it is an enum. |
| 529 | bool GetRawInt = true; |
| 530 | |
| 531 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 532 | // FIXME: Maybe this should be an assertion. Are there cases |
| 533 | // were it is not an EnumConstantDecl? |
| 534 | EnumConstantDecl* D = |
| 535 | dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 536 | |
| 537 | if (D) { |
| 538 | GetRawInt = false; |
| 539 | os << D->getNameAsString(); |
| 540 | } |
| 541 | } |
Eli Friedman | 9ec64d6 | 2009-04-26 19:04:51 +0000 | [diff] [blame] | 542 | |
| 543 | if (GetRawInt) |
| 544 | os << LHS->EvaluateAsInt(Ctx); |
| 545 | |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 546 | os << ":' at line " |
| 547 | << End.asLocation().getInstantiationLineNumber(); |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 552 | os.str())); |
| 553 | } |
| 554 | else { |
| 555 | os << "'Default' branch taken. "; |
| 556 | const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N); |
| 557 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 558 | os.str())); |
| 559 | } |
| 560 | |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | case Stmt::BreakStmtClass: |
| 565 | case Stmt::ContinueStmtClass: { |
| 566 | std::string sbuf; |
| 567 | llvm::raw_string_ostream os(sbuf); |
| 568 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 569 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 570 | os.str())); |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | // Determine control-flow for ternary '?'. |
| 575 | case Stmt::ConditionalOperatorClass: { |
| 576 | std::string sbuf; |
| 577 | llvm::raw_string_ostream os(sbuf); |
| 578 | os << "'?' condition is "; |
| 579 | |
| 580 | if (*(Src->succ_begin()+1) == Dst) |
| 581 | os << "false"; |
| 582 | else |
| 583 | os << "true"; |
| 584 | |
| 585 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 586 | |
| 587 | if (const Stmt *S = End.asStmt()) |
| 588 | End = PDB.getEnclosingStmtLocation(S); |
| 589 | |
| 590 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 591 | os.str())); |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | // Determine control-flow for short-circuited '&&' and '||'. |
| 596 | case Stmt::BinaryOperatorClass: { |
| 597 | if (!PDB.supportsLogicalOpControlFlow()) |
| 598 | break; |
| 599 | |
| 600 | BinaryOperator *B = cast<BinaryOperator>(T); |
| 601 | std::string sbuf; |
| 602 | llvm::raw_string_ostream os(sbuf); |
| 603 | os << "Left side of '"; |
| 604 | |
| 605 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 606 | os << "&&" << "' is "; |
| 607 | |
| 608 | if (*(Src->succ_begin()+1) == Dst) { |
| 609 | os << "false"; |
| 610 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 611 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 612 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 613 | os.str())); |
| 614 | } |
| 615 | else { |
| 616 | os << "true"; |
| 617 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 618 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 619 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 620 | os.str())); |
| 621 | } |
| 622 | } |
| 623 | else { |
| 624 | assert(B->getOpcode() == BinaryOperator::LOr); |
| 625 | os << "||" << "' is "; |
| 626 | |
| 627 | if (*(Src->succ_begin()+1) == Dst) { |
| 628 | os << "false"; |
| 629 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 630 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 631 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 632 | os.str())); |
| 633 | } |
| 634 | else { |
| 635 | os << "true"; |
| 636 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 637 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 638 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 639 | os.str())); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | break; |
| 644 | } |
| 645 | |
| 646 | case Stmt::DoStmtClass: { |
| 647 | if (*(Src->succ_begin()) == Dst) { |
| 648 | std::string sbuf; |
| 649 | llvm::raw_string_ostream os(sbuf); |
| 650 | |
| 651 | os << "Loop condition is true. "; |
| 652 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 653 | |
| 654 | if (const Stmt *S = End.asStmt()) |
| 655 | End = PDB.getEnclosingStmtLocation(S); |
| 656 | |
| 657 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 658 | os.str())); |
| 659 | } |
| 660 | else { |
| 661 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 662 | |
| 663 | if (const Stmt *S = End.asStmt()) |
| 664 | End = PDB.getEnclosingStmtLocation(S); |
| 665 | |
| 666 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 667 | "Loop condition is false. Exiting loop")); |
| 668 | } |
| 669 | |
| 670 | break; |
| 671 | } |
| 672 | |
| 673 | case Stmt::WhileStmtClass: |
| 674 | case Stmt::ForStmtClass: { |
| 675 | if (*(Src->succ_begin()+1) == Dst) { |
| 676 | std::string sbuf; |
| 677 | llvm::raw_string_ostream os(sbuf); |
| 678 | |
| 679 | os << "Loop condition is false. "; |
| 680 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 681 | if (const Stmt *S = End.asStmt()) |
| 682 | End = PDB.getEnclosingStmtLocation(S); |
| 683 | |
| 684 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 685 | os.str())); |
| 686 | } |
| 687 | else { |
| 688 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 689 | if (const Stmt *S = End.asStmt()) |
| 690 | End = PDB.getEnclosingStmtLocation(S); |
| 691 | |
| 692 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 693 | "Loop condition is true. Entering loop body")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | break; |
| 697 | } |
| 698 | |
| 699 | case Stmt::IfStmtClass: { |
| 700 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 701 | |
| 702 | if (const Stmt *S = End.asStmt()) |
| 703 | End = PDB.getEnclosingStmtLocation(S); |
| 704 | |
| 705 | if (*(Src->succ_begin()+1) == Dst) |
| 706 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 707 | "Taking false branch")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 708 | else |
| 709 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 710 | "Taking true branch")); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 711 | |
| 712 | break; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | if (PathDiagnosticPiece* p = |
| 718 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 719 | PDB.getBugReporter(), |
| 720 | PDB.getNodeMapClosure())) { |
| 721 | PD.push_front(p); |
| 722 | } |
| 723 | |
| 724 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 725 | // Scan the region bindings, and see if a "notable" symbol has a new |
| 726 | // lval binding. |
| 727 | ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD); |
| 728 | PDB.getStateManager().iterBindings(N->getState(), SNS); |
| 729 | } |
| 730 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 731 | |
| 732 | // After constructing the full PathDiagnostic, do a pass over it to compact |
| 733 | // PathDiagnosticPieces that occur within a macro. |
| 734 | CompactPathDiagnostic(PD, PDB.getSourceManager()); |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 738 | // "Extensive" PathDiagnostic generation. |
| 739 | //===----------------------------------------------------------------------===// |
| 740 | |
| 741 | static bool IsControlFlowExpr(const Stmt *S) { |
| 742 | const Expr *E = dyn_cast<Expr>(S); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 743 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 744 | if (!E) |
| 745 | return false; |
| 746 | |
| 747 | E = E->IgnoreParenCasts(); |
| 748 | |
| 749 | if (isa<ConditionalOperator>(E)) |
| 750 | return true; |
| 751 | |
| 752 | if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E)) |
| 753 | if (B->isLogicalOp()) |
| 754 | return true; |
| 755 | |
| 756 | return false; |
| 757 | } |
| 758 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 759 | #if 1 |
| 760 | |
| 761 | namespace { |
| 762 | class VISIBILITY_HIDDEN EdgeBuilder { |
| 763 | std::vector<PathDiagnosticLocation> CLocs; |
| 764 | typedef std::vector<PathDiagnosticLocation>::iterator iterator; |
| 765 | PathDiagnostic &PD; |
| 766 | PathDiagnosticBuilder &PDB; |
| 767 | PathDiagnosticLocation PrevLoc; |
| 768 | |
| 769 | bool containsLocation(const PathDiagnosticLocation &Container, |
| 770 | const PathDiagnosticLocation &Containee); |
| 771 | |
| 772 | PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 773 | |
| 774 | void popLocation() { |
Ted Kremenek | 404dd7a | 2009-04-22 18:37:42 +0000 | [diff] [blame] | 775 | PathDiagnosticLocation L = CLocs.back(); |
Ted Kremenek | 5c7168c | 2009-04-22 20:36:26 +0000 | [diff] [blame] | 776 | if (L.asLocation().isFileID()) { |
Ted Kremenek | c2924d0 | 2009-04-23 16:19:29 +0000 | [diff] [blame] | 777 | |
Ted Kremenek | 6f13235 | 2009-04-23 16:44:22 +0000 | [diff] [blame] | 778 | if (const Stmt *S = L.asStmt()) { |
| 779 | while (1) { |
| 780 | // Adjust the location for some expressions that are best referenced |
| 781 | // by one of their subexpressions. |
| 782 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(S)) |
| 783 | S = PE->IgnoreParens(); |
| 784 | else if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(S)) |
| 785 | S = CO->getCond(); |
| 786 | else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(S)) |
| 787 | S = CE->getCond(); |
| 788 | else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(S)) |
| 789 | S = BE->getLHS(); |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 790 | else if (const DoStmt *DS = dyn_cast<DoStmt>(S)) |
| 791 | S = DS->getCond(); |
Ted Kremenek | 6f13235 | 2009-04-23 16:44:22 +0000 | [diff] [blame] | 792 | else |
| 793 | break; |
| 794 | } |
| 795 | |
Ted Kremenek | c2924d0 | 2009-04-23 16:19:29 +0000 | [diff] [blame] | 796 | L = PathDiagnosticLocation(S, L.getManager()); |
| 797 | } |
| 798 | |
Ted Kremenek | 5c7168c | 2009-04-22 20:36:26 +0000 | [diff] [blame] | 799 | // For contexts, we only one the first character as the range. |
| 800 | L = PathDiagnosticLocation(L.asLocation(), L.getManager()); |
Ted Kremenek | 4f5be3b | 2009-04-22 20:51:59 +0000 | [diff] [blame] | 801 | rawAddEdge(L); |
Ted Kremenek | 5c7168c | 2009-04-22 20:36:26 +0000 | [diff] [blame] | 802 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 803 | CLocs.pop_back(); |
| 804 | } |
| 805 | |
| 806 | PathDiagnosticLocation IgnoreParens(const PathDiagnosticLocation &L); |
| 807 | |
| 808 | public: |
| 809 | EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb) |
| 810 | : PD(pd), PDB(pdb) { |
Ted Kremenek | a301a67 | 2009-04-22 18:16:20 +0000 | [diff] [blame] | 811 | |
| 812 | // If the PathDiagnostic already has pieces, add the enclosing statement |
| 813 | // of the first piece as a context as well. |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 814 | if (!PD.empty()) { |
| 815 | PrevLoc = PD.begin()->getLocation(); |
| 816 | |
| 817 | if (const Stmt *S = PrevLoc.asStmt()) |
| 818 | addContext(PDB.getEnclosingStmtLocation(S).asStmt()); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | ~EdgeBuilder() { |
| 823 | while (!CLocs.empty()) popLocation(); |
Ted Kremenek | a301a67 | 2009-04-22 18:16:20 +0000 | [diff] [blame] | 824 | |
| 825 | // Finally, add an initial edge from the start location of the first |
| 826 | // statement (if it doesn't already exist). |
Sebastian Redl | d3a413d | 2009-04-26 20:35:05 +0000 | [diff] [blame] | 827 | // FIXME: Should handle CXXTryStmt if analyser starts supporting C++. |
| 828 | if (const CompoundStmt *CS = |
| 829 | PDB.getCodeDecl().getCompoundBody(PDB.getContext())) |
Ted Kremenek | a301a67 | 2009-04-22 18:16:20 +0000 | [diff] [blame] | 830 | if (!CS->body_empty()) { |
| 831 | SourceLocation Loc = (*CS->body_begin())->getLocStart(); |
| 832 | rawAddEdge(PathDiagnosticLocation(Loc, PDB.getSourceManager())); |
| 833 | } |
| 834 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false); |
| 838 | |
| 839 | void addEdge(const Stmt *S, bool alwaysAdd = false) { |
| 840 | addEdge(PathDiagnosticLocation(S, PDB.getSourceManager()), alwaysAdd); |
| 841 | } |
| 842 | |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 843 | void rawAddEdge(PathDiagnosticLocation NewLoc); |
| 844 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 845 | void addContext(const Stmt *S); |
| 846 | }; |
| 847 | } // end anonymous namespace |
| 848 | |
| 849 | |
| 850 | PathDiagnosticLocation |
| 851 | EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) { |
| 852 | if (const Stmt *S = L.asStmt()) { |
| 853 | if (IsControlFlowExpr(S)) |
| 854 | return L; |
| 855 | |
| 856 | return PDB.getEnclosingStmtLocation(S); |
| 857 | } |
| 858 | |
| 859 | return L; |
| 860 | } |
| 861 | |
| 862 | bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container, |
| 863 | const PathDiagnosticLocation &Containee) { |
| 864 | |
| 865 | if (Container == Containee) |
| 866 | return true; |
| 867 | |
| 868 | if (Container.asDecl()) |
| 869 | return true; |
| 870 | |
| 871 | if (const Stmt *S = Containee.asStmt()) |
| 872 | if (const Stmt *ContainerS = Container.asStmt()) { |
| 873 | while (S) { |
| 874 | if (S == ContainerS) |
| 875 | return true; |
| 876 | S = PDB.getParent(S); |
| 877 | } |
| 878 | return false; |
| 879 | } |
| 880 | |
| 881 | // Less accurate: compare using source ranges. |
| 882 | SourceRange ContainerR = Container.asRange(); |
| 883 | SourceRange ContaineeR = Containee.asRange(); |
| 884 | |
| 885 | SourceManager &SM = PDB.getSourceManager(); |
| 886 | SourceLocation ContainerRBeg = SM.getInstantiationLoc(ContainerR.getBegin()); |
| 887 | SourceLocation ContainerREnd = SM.getInstantiationLoc(ContainerR.getEnd()); |
| 888 | SourceLocation ContaineeRBeg = SM.getInstantiationLoc(ContaineeR.getBegin()); |
| 889 | SourceLocation ContaineeREnd = SM.getInstantiationLoc(ContaineeR.getEnd()); |
| 890 | |
| 891 | unsigned ContainerBegLine = SM.getInstantiationLineNumber(ContainerRBeg); |
| 892 | unsigned ContainerEndLine = SM.getInstantiationLineNumber(ContainerREnd); |
| 893 | unsigned ContaineeBegLine = SM.getInstantiationLineNumber(ContaineeRBeg); |
| 894 | unsigned ContaineeEndLine = SM.getInstantiationLineNumber(ContaineeREnd); |
| 895 | |
| 896 | assert(ContainerBegLine <= ContainerEndLine); |
| 897 | assert(ContaineeBegLine <= ContaineeEndLine); |
| 898 | |
| 899 | return (ContainerBegLine <= ContaineeBegLine && |
| 900 | ContainerEndLine >= ContaineeEndLine && |
| 901 | (ContainerBegLine != ContaineeBegLine || |
| 902 | SM.getInstantiationColumnNumber(ContainerRBeg) <= |
| 903 | SM.getInstantiationColumnNumber(ContaineeRBeg)) && |
| 904 | (ContainerEndLine != ContaineeEndLine || |
| 905 | SM.getInstantiationColumnNumber(ContainerREnd) >= |
| 906 | SM.getInstantiationColumnNumber(ContainerREnd))); |
| 907 | } |
| 908 | |
| 909 | PathDiagnosticLocation |
| 910 | EdgeBuilder::IgnoreParens(const PathDiagnosticLocation &L) { |
| 911 | if (const Expr* E = dyn_cast_or_null<Expr>(L.asStmt())) |
| 912 | return PathDiagnosticLocation(E->IgnoreParenCasts(), |
| 913 | PDB.getSourceManager()); |
| 914 | return L; |
| 915 | } |
| 916 | |
| 917 | void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) { |
| 918 | if (!PrevLoc.isValid()) { |
| 919 | PrevLoc = NewLoc; |
| 920 | return; |
| 921 | } |
| 922 | |
| 923 | if (NewLoc.asLocation() == PrevLoc.asLocation()) |
| 924 | return; |
| 925 | |
| 926 | // FIXME: Ignore intra-macro edges for now. |
| 927 | if (NewLoc.asLocation().getInstantiationLoc() == |
| 928 | PrevLoc.asLocation().getInstantiationLoc()) |
| 929 | return; |
| 930 | |
| 931 | PD.push_front(new PathDiagnosticControlFlowPiece(NewLoc, PrevLoc)); |
| 932 | PrevLoc = NewLoc; |
| 933 | } |
| 934 | |
| 935 | void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) { |
Ted Kremenek | a301a67 | 2009-04-22 18:16:20 +0000 | [diff] [blame] | 936 | |
| 937 | if (!alwaysAdd && NewLoc.asLocation().isMacroID()) |
| 938 | return; |
| 939 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 940 | const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc); |
| 941 | |
| 942 | while (!CLocs.empty()) { |
| 943 | const PathDiagnosticLocation &TopContextLoc = CLocs.back(); |
| 944 | |
| 945 | // Is the top location context the same as the one for the new location? |
| 946 | if (TopContextLoc == CLoc) { |
Ted Kremenek | e97386f | 2009-04-07 00:11:40 +0000 | [diff] [blame] | 947 | if (alwaysAdd) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 948 | rawAddEdge(NewLoc); |
| 949 | |
| 950 | return; |
| 951 | } |
| 952 | |
| 953 | if (containsLocation(TopContextLoc, CLoc)) { |
| 954 | if (alwaysAdd) |
| 955 | rawAddEdge(NewLoc); |
| 956 | |
| 957 | CLocs.push_back(CLoc); |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | // Context does not contain the location. Flush it. |
| 962 | popLocation(); |
| 963 | } |
Ted Kremenek | 5c7168c | 2009-04-22 20:36:26 +0000 | [diff] [blame] | 964 | |
| 965 | // If we reach here, there is no enclosing context. Just add the edge. |
| 966 | rawAddEdge(NewLoc); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | void EdgeBuilder::addContext(const Stmt *S) { |
| 970 | if (!S) |
| 971 | return; |
| 972 | |
| 973 | PathDiagnosticLocation L(S, PDB.getSourceManager()); |
| 974 | |
| 975 | while (!CLocs.empty()) { |
| 976 | const PathDiagnosticLocation &TopContextLoc = CLocs.back(); |
| 977 | |
| 978 | // Is the top location context the same as the one for the new location? |
| 979 | if (TopContextLoc == L) |
| 980 | return; |
| 981 | |
| 982 | if (containsLocation(TopContextLoc, L)) { |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 983 | CLocs.push_back(L); |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | // Context does not contain the location. Flush it. |
| 988 | popLocation(); |
| 989 | } |
| 990 | |
| 991 | CLocs.push_back(L); |
| 992 | } |
| 993 | |
| 994 | static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD, |
| 995 | PathDiagnosticBuilder &PDB, |
| 996 | const ExplodedNode<GRState> *N) { |
| 997 | |
| 998 | |
| 999 | EdgeBuilder EB(PD, PDB); |
| 1000 | |
| 1001 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 1002 | ? NULL : *(N->pred_begin()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1003 | while (NextNode) { |
| 1004 | N = NextNode; |
| 1005 | NextNode = GetPredecessorNode(N); |
| 1006 | ProgramPoint P = N->getLocation(); |
| 1007 | |
| 1008 | // Block edges. |
| 1009 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 1010 | const CFGBlock &Blk = *BE->getSrc(); |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 1011 | const Stmt *Term = Blk.getTerminator(); |
| 1012 | |
Ted Kremenek | a902d55 | 2009-04-28 04:28:12 +0000 | [diff] [blame^] | 1013 | if (Term && !isa<DoStmt>(Term)) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1014 | EB.addContext(Term); |
| 1015 | |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 1016 | // Are we jumping to the head of a loop? Add a special diagnostic. |
| 1017 | if (const Stmt *Loop = BE->getDst()->getLoopTarget()) { |
| 1018 | |
| 1019 | PathDiagnosticLocation L(Loop, PDB.getSourceManager()); |
| 1020 | PathDiagnosticEventPiece *p = |
| 1021 | new PathDiagnosticEventPiece(L, |
| 1022 | "Looping back to the head of the loop"); |
| 1023 | |
| 1024 | EB.addEdge(p->getLocation(), true); |
| 1025 | PD.push_front(p); |
| 1026 | |
| 1027 | if (!Term) { |
| 1028 | const CompoundStmt *CS = NULL; |
| 1029 | if (const ForStmt *FS = dyn_cast<ForStmt>(Loop)) |
| 1030 | CS = dyn_cast<CompoundStmt>(FS->getBody()); |
| 1031 | else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop)) |
| 1032 | CS = dyn_cast<CompoundStmt>(WS->getBody()); |
| 1033 | |
| 1034 | if (CS) |
| 1035 | EB.rawAddEdge(PathDiagnosticLocation(CS->getRBracLoc(), |
| 1036 | PDB.getSourceManager())); |
| 1037 | } |
| 1038 | } |
| 1039 | |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1040 | continue; |
| 1041 | } |
| 1042 | |
Ted Kremenek | 8bd4d03 | 2009-04-28 04:23:15 +0000 | [diff] [blame] | 1043 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1044 | if (const Stmt* S = BE->getFirstStmt()) { |
| 1045 | if (IsControlFlowExpr(S)) |
| 1046 | EB.addContext(S); |
| 1047 | else |
Ted Kremenek | 581329c | 2009-04-07 04:53:35 +0000 | [diff] [blame] | 1048 | EB.addContext(PDB.getEnclosingStmtLocation(S).asStmt()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | continue; |
| 1052 | } |
| 1053 | |
| 1054 | PathDiagnosticPiece* p = |
Ted Kremenek | 581329c | 2009-04-07 04:53:35 +0000 | [diff] [blame] | 1055 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 1056 | PDB.getBugReporter(), PDB.getNodeMapClosure()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (p) { |
| 1059 | EB.addEdge(p->getLocation(), true); |
| 1060 | PD.push_front(p); |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | #else |
| 1067 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1068 | static void GenExtAddEdge(PathDiagnostic& PD, |
| 1069 | PathDiagnosticBuilder &PDB, |
| 1070 | PathDiagnosticLocation NewLoc, |
| 1071 | PathDiagnosticLocation &PrevLoc, |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1072 | bool allowBlockJump = false) { |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1073 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1074 | if (const Stmt *S = NewLoc.asStmt()) { |
| 1075 | if (IsControlFlowExpr(S)) |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | if (!PrevLoc.isValid()) { |
| 1081 | PrevLoc = NewLoc; |
| 1082 | return; |
| 1083 | } |
| 1084 | |
| 1085 | if (NewLoc == PrevLoc) |
| 1086 | return; |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1087 | |
Ted Kremenek | 0dc65be | 2009-04-01 19:43:28 +0000 | [diff] [blame] | 1088 | // Are we jumping between statements within the same compound statement? |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1089 | if (!allowBlockJump) |
| 1090 | if (const Stmt *PS = PrevLoc.asStmt()) |
| 1091 | if (const Stmt *NS = NewLoc.asStmt()) { |
| 1092 | const Stmt *parentPS = PDB.getParent(PS); |
Ted Kremenek | 28de78b | 2009-04-02 03:30:55 +0000 | [diff] [blame] | 1093 | if (parentPS && isa<CompoundStmt>(parentPS) && |
| 1094 | parentPS == PDB.getParent(NS)) |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1095 | return; |
| 1096 | } |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1097 | |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1098 | // Add an extra edge when jumping between contexts. |
| 1099 | while (1) { |
| 1100 | if (const Stmt *PS = PrevLoc.asStmt()) |
| 1101 | if (const Stmt *NS = NewLoc.asStmt()) { |
| 1102 | PathDiagnosticLocation X = PDB.getEnclosingStmtLocation(PS); |
| 1103 | // FIXME: We need a version of getParent that ignores '()' and casts. |
| 1104 | const Stmt *parentX = PDB.getParent(X.asStmt()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1105 | |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1106 | const PathDiagnosticLocation &Y = PDB.getEnclosingStmtLocation(NS); |
| 1107 | // FIXME: We need a version of getParent that ignores '()' and casts. |
| 1108 | const Stmt *parentY = PDB.getParent(Y.asStmt()); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1109 | |
Ted Kremenek | 0ddaff3 | 2009-04-02 03:44:00 +0000 | [diff] [blame] | 1110 | if (parentX && IsControlFlowExpr(parentX)) { |
| 1111 | if (parentX == parentY) |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1112 | break; |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1113 | else { |
Ted Kremenek | 0ddaff3 | 2009-04-02 03:44:00 +0000 | [diff] [blame] | 1114 | if (const Stmt *grandparentX = PDB.getParent(parentX)) { |
| 1115 | const PathDiagnosticLocation &W = |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1116 | PDB.getEnclosingStmtLocation(grandparentX); |
Ted Kremenek | 0ddaff3 | 2009-04-02 03:44:00 +0000 | [diff] [blame] | 1117 | |
| 1118 | if (W != Y) X = W; |
| 1119 | } |
Ted Kremenek | 9e2d98d | 2009-04-01 21:12:06 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | if (X != Y && PrevLoc.asLocation() != X.asLocation()) { |
| 1124 | PD.push_front(new PathDiagnosticControlFlowPiece(X, PrevLoc)); |
| 1125 | PrevLoc = X; |
| 1126 | } |
| 1127 | } |
| 1128 | break; |
| 1129 | } |
| 1130 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1131 | PD.push_front(new PathDiagnosticControlFlowPiece(NewLoc, PrevLoc)); |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1132 | PrevLoc = NewLoc; |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static bool IsNestedDeclStmt(const Stmt *S, ParentMap &PM) { |
| 1136 | const DeclStmt *DS = dyn_cast<DeclStmt>(S); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1137 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1138 | if (!DS) |
| 1139 | return false; |
| 1140 | |
| 1141 | const Stmt *Parent = PM.getParent(DS); |
| 1142 | if (!Parent) |
| 1143 | return false; |
| 1144 | |
| 1145 | if (const ForStmt *FS = dyn_cast<ForStmt>(Parent)) |
| 1146 | return FS->getInit() == DS; |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1147 | |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 1148 | // FIXME: In the future IfStmt/WhileStmt may contain DeclStmts in their |
| 1149 | // condition. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1150 | |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1154 | static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD, |
| 1155 | PathDiagnosticBuilder &PDB, |
| 1156 | const ExplodedNode<GRState> *N) { |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1157 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1158 | SourceManager& SMgr = PDB.getSourceManager(); |
| 1159 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1160 | ? NULL : *(N->pred_begin()); |
| 1161 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1162 | PathDiagnosticLocation PrevLoc; |
| 1163 | |
| 1164 | while (NextNode) { |
| 1165 | N = NextNode; |
| 1166 | NextNode = GetPredecessorNode(N); |
| 1167 | ProgramPoint P = N->getLocation(); |
| 1168 | |
| 1169 | // Block edges. |
| 1170 | if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 1171 | const CFGBlock &Blk = *BE->getSrc(); |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1172 | |
Ted Kremenek | 51a735c | 2009-04-01 17:52:26 +0000 | [diff] [blame] | 1173 | // Add a special edge for the entrance into the function/method. |
| 1174 | if (&Blk == &PDB.getCFG().getEntry()) { |
| 1175 | FullSourceLoc L = FullSourceLoc(PDB.getCodeDecl().getLocation(), SMgr); |
| 1176 | GenExtAddEdge(PD, PDB, L.getSpellingLoc(), PrevLoc); |
| 1177 | continue; |
| 1178 | } |
| 1179 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1180 | if (const Stmt *Term = Blk.getTerminator()) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 1181 | const Stmt *Cond = Blk.getTerminatorCondition(); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1182 | if (!Cond || !IsControlFlowExpr(Cond)) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 1183 | // For terminators that are control-flow expressions like '&&', '?', |
| 1184 | // have the condition be the anchor point for the control-flow edge |
| 1185 | // instead of the terminator. |
| 1186 | const Stmt *X = isa<Expr>(Term) ? (Cond ? Cond : Term) : Term; |
| 1187 | GenExtAddEdge(PD, PDB, PathDiagnosticLocation(X, SMgr), PrevLoc,true); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1188 | continue; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | // Only handle blocks with more than 1 statement here, as the blocks |
| 1193 | // with one statement are handled at BlockEntrances. |
| 1194 | if (Blk.size() > 1) { |
| 1195 | const Stmt *S = *Blk.rbegin(); |
| 1196 | |
| 1197 | // We don't add control-flow edges for DeclStmt's that appear in |
| 1198 | // the condition of if/while/for or are control-flow merge expressions. |
| 1199 | if (!IsControlFlowExpr(S) && !IsNestedDeclStmt(S, PDB.getParentMap())) { |
| 1200 | GenExtAddEdge(PD, PDB, PathDiagnosticLocation(S, SMgr), PrevLoc); |
| 1201 | } |
| 1202 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1203 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1204 | continue; |
| 1205 | } |
| 1206 | |
| 1207 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { |
| 1208 | if (const Stmt* S = BE->getFirstStmt()) { |
| 1209 | if (!IsControlFlowExpr(S) && !IsNestedDeclStmt(S, PDB.getParentMap())) { |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1210 | if (PrevLoc.isValid()) { |
Ted Kremenek | 51a735c | 2009-04-01 17:52:26 +0000 | [diff] [blame] | 1211 | // Are we jumping within the same enclosing statement? |
Ted Kremenek | c3f83ad | 2009-04-01 17:18:21 +0000 | [diff] [blame] | 1212 | if (PDB.getEnclosingStmtLocation(S) == |
| 1213 | PDB.getEnclosingStmtLocation(PrevLoc)) |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1214 | continue; |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | GenExtAddEdge(PD, PDB, PDB.getEnclosingStmtLocation(S), PrevLoc); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | continue; |
| 1222 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1223 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1224 | PathDiagnosticPiece* p = |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1225 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 1226 | PDB.getBugReporter(), PDB.getNodeMapClosure()); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1227 | |
| 1228 | if (p) { |
Ted Kremenek | a42c4c9 | 2009-04-01 18:48:52 +0000 | [diff] [blame] | 1229 | GenExtAddEdge(PD, PDB, p->getLocation(), PrevLoc, true); |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1230 | PD.push_front(p); |
| 1231 | } |
| 1232 | } |
| 1233 | } |
Ted Kremenek | 14856d7 | 2009-04-06 23:06:54 +0000 | [diff] [blame] | 1234 | #endif |
| 1235 | |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1236 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1237 | // Methods for BugType and subclasses. |
| 1238 | //===----------------------------------------------------------------------===// |
| 1239 | BugType::~BugType() {} |
| 1240 | void BugType::FlushReports(BugReporter &BR) {} |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1241 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1242 | //===----------------------------------------------------------------------===// |
| 1243 | // Methods for BugReport and subclasses. |
| 1244 | //===----------------------------------------------------------------------===// |
| 1245 | BugReport::~BugReport() {} |
| 1246 | RangedBugReport::~RangedBugReport() {} |
| 1247 | |
| 1248 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 1249 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1250 | Stmt *S = NULL; |
| 1251 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1252 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 1253 | if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1254 | } |
| 1255 | if (!S) S = GetStmt(ProgP); |
| 1256 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1257 | return S; |
| 1258 | } |
| 1259 | |
| 1260 | PathDiagnosticPiece* |
| 1261 | BugReport::getEndPath(BugReporter& BR, |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1262 | const ExplodedNode<GRState>* EndPathNode) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1263 | |
| 1264 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1265 | |
| 1266 | if (!S) |
| 1267 | return NULL; |
| 1268 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1269 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 1270 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1271 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 1272 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1273 | getRanges(BR, Beg, End); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1274 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1275 | for (; Beg != End; ++Beg) |
| 1276 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1277 | |
| 1278 | return P; |
| 1279 | } |
| 1280 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1281 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 1282 | const SourceRange*& end) { |
| 1283 | |
| 1284 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 1285 | R = E->getSourceRange(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 1286 | assert(R.isValid()); |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1287 | beg = &R; |
| 1288 | end = beg+1; |
| 1289 | } |
| 1290 | else |
| 1291 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1294 | SourceLocation BugReport::getLocation() const { |
| 1295 | if (EndNode) |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 1296 | if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) { |
| 1297 | // For member expressions, return the location of the '.' or '->'. |
| 1298 | if (MemberExpr* ME = dyn_cast<MemberExpr>(S)) |
| 1299 | return ME->getMemberLoc(); |
| 1300 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1301 | return S->getLocStart(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 1302 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1303 | |
| 1304 | return FullSourceLoc(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1307 | PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N, |
| 1308 | const ExplodedNode<GRState>* PrevN, |
| 1309 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1310 | BugReporter& BR, |
| 1311 | NodeResolver &NR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 1312 | return NULL; |
| 1313 | } |
| 1314 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1315 | //===----------------------------------------------------------------------===// |
| 1316 | // Methods for BugReporter and subclasses. |
| 1317 | //===----------------------------------------------------------------------===// |
| 1318 | |
| 1319 | BugReportEquivClass::~BugReportEquivClass() { |
| 1320 | for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; |
| 1321 | } |
| 1322 | |
| 1323 | GRBugReporter::~GRBugReporter() { FlushReports(); } |
| 1324 | BugReporterData::~BugReporterData() {} |
| 1325 | |
| 1326 | ExplodedGraph<GRState>& |
| 1327 | GRBugReporter::getGraph() { return Eng.getGraph(); } |
| 1328 | |
| 1329 | GRStateManager& |
| 1330 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
| 1331 | |
| 1332 | BugReporter::~BugReporter() { FlushReports(); } |
| 1333 | |
| 1334 | void BugReporter::FlushReports() { |
| 1335 | if (BugTypes.isEmpty()) |
| 1336 | return; |
| 1337 | |
| 1338 | // First flush the warnings for each BugType. This may end up creating new |
| 1339 | // warnings and new BugTypes. Because ImmutableSet is a functional data |
| 1340 | // structure, we do not need to worry about the iterators being invalidated. |
| 1341 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 1342 | const_cast<BugType*>(*I)->FlushReports(*this); |
| 1343 | |
| 1344 | // Iterate through BugTypes a second time. BugTypes may have been updated |
| 1345 | // with new BugType objects and new warnings. |
| 1346 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) { |
| 1347 | BugType *BT = const_cast<BugType*>(*I); |
| 1348 | |
| 1349 | typedef llvm::FoldingSet<BugReportEquivClass> SetTy; |
| 1350 | SetTy& EQClasses = BT->EQClasses; |
| 1351 | |
| 1352 | for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ |
| 1353 | BugReportEquivClass& EQ = *EI; |
| 1354 | FlushReport(EQ); |
| 1355 | } |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1356 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1357 | // Delete the BugType object. This will also delete the equivalence |
| 1358 | // classes. |
| 1359 | delete BT; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 1360 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1361 | |
| 1362 | // Remove all references to the BugType objects. |
| 1363 | BugTypes = F.GetEmptySet(); |
| 1364 | } |
| 1365 | |
| 1366 | //===----------------------------------------------------------------------===// |
| 1367 | // PathDiagnostics generation. |
| 1368 | //===----------------------------------------------------------------------===// |
| 1369 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1370 | static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1371 | std::pair<ExplodedNode<GRState>*, unsigned> > |
| 1372 | MakeReportGraph(const ExplodedGraph<GRState>* G, |
| 1373 | const ExplodedNode<GRState>** NStart, |
| 1374 | const ExplodedNode<GRState>** NEnd) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 1375 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1376 | // Create the trimmed graph. It will contain the shortest paths from the |
| 1377 | // error nodes to the root. In the new graph we should only have one |
| 1378 | // error node unless there are two or more error nodes with the same minimum |
| 1379 | // path length. |
| 1380 | ExplodedGraph<GRState>* GTrim; |
| 1381 | InterExplodedGraphMap<GRState>* NMap; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1382 | |
| 1383 | llvm::DenseMap<const void*, const void*> InverseMap; |
| 1384 | llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1385 | |
| 1386 | // Create owning pointers for GTrim and NMap just to ensure that they are |
| 1387 | // released when this function exists. |
| 1388 | llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim); |
| 1389 | llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap); |
| 1390 | |
| 1391 | // Find the (first) error node in the trimmed graph. We just need to consult |
| 1392 | // the node map (NMap) which maps from nodes in the original graph to nodes |
| 1393 | // in the new graph. |
| 1394 | const ExplodedNode<GRState>* N = 0; |
| 1395 | unsigned NodeIndex = 0; |
| 1396 | |
| 1397 | for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I) |
| 1398 | if ((N = NMap->getMappedNode(*I))) { |
| 1399 | NodeIndex = (I - NStart) / sizeof(*I); |
| 1400 | break; |
| 1401 | } |
| 1402 | |
| 1403 | assert(N && "No error node found in the trimmed graph."); |
| 1404 | |
| 1405 | // Create a new (third!) graph with a single path. This is the graph |
| 1406 | // that will be returned to the caller. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1407 | ExplodedGraph<GRState> *GNew = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1408 | new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 1409 | GTrim->getContext()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1410 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1411 | // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1412 | // to the root node, and then construct a new graph that contains only |
| 1413 | // a single path. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1414 | llvm::DenseMap<const void*,unsigned> Visited; |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1415 | std::queue<const ExplodedNode<GRState>*> WS; |
| 1416 | WS.push(N); |
| 1417 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1418 | unsigned cnt = 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1419 | const ExplodedNode<GRState>* Root = 0; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1420 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1421 | while (!WS.empty()) { |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1422 | const ExplodedNode<GRState>* Node = WS.front(); |
| 1423 | WS.pop(); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1424 | |
| 1425 | if (Visited.find(Node) != Visited.end()) |
| 1426 | continue; |
| 1427 | |
| 1428 | Visited[Node] = cnt++; |
| 1429 | |
| 1430 | if (Node->pred_empty()) { |
| 1431 | Root = Node; |
| 1432 | break; |
| 1433 | } |
| 1434 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1435 | for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(), |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1436 | E=Node->pred_end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1437 | WS.push(*I); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1438 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1439 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1440 | assert (Root); |
| 1441 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 1442 | // 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] | 1443 | // with the lowest number. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1444 | ExplodedNode<GRState> *Last = 0, *First = 0; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1445 | NodeBackMap *BM = new NodeBackMap(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1446 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1447 | for ( N = Root ;;) { |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1448 | // Lookup the number associated with the current node. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1449 | llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1450 | assert (I != Visited.end()); |
| 1451 | |
| 1452 | // Create the equivalent node in the new graph with the same state |
| 1453 | // and location. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1454 | ExplodedNode<GRState>* NewN = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1455 | GNew->getNode(N->getLocation(), N->getState()); |
| 1456 | |
| 1457 | // Store the mapping to the original node. |
| 1458 | llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N); |
| 1459 | assert(IMitr != InverseMap.end() && "No mapping to original node."); |
| 1460 | (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1461 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1462 | // Link up the new node with the previous node. |
| 1463 | if (Last) |
| 1464 | NewN->addPredecessor(Last); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1465 | |
| 1466 | Last = NewN; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1467 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1468 | // Are we at the final node? |
| 1469 | if (I->second == 0) { |
| 1470 | First = NewN; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1471 | break; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1472 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1473 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1474 | // Find the next successor node. We choose the node that is marked |
| 1475 | // with the lowest DFS number. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1476 | ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin(); |
| 1477 | ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end(); |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1478 | N = 0; |
| 1479 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1480 | for (unsigned MinVal = 0; SI != SE; ++SI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1481 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1482 | I = Visited.find(*SI); |
| 1483 | |
| 1484 | if (I == Visited.end()) |
| 1485 | continue; |
| 1486 | |
| 1487 | if (!N || I->second < MinVal) { |
| 1488 | N = *SI; |
| 1489 | MinVal = I->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 1490 | } |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1491 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1492 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1493 | assert (N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1494 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1495 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 1496 | assert (First); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1497 | return std::make_pair(std::make_pair(GNew, BM), |
| 1498 | std::make_pair(First, NodeIndex)); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1501 | /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object |
| 1502 | /// and collapses PathDiagosticPieces that are expanded by macros. |
| 1503 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) { |
| 1504 | typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> > |
| 1505 | MacroStackTy; |
| 1506 | |
| 1507 | typedef std::vector<PathDiagnosticPiece*> |
| 1508 | PiecesTy; |
| 1509 | |
| 1510 | MacroStackTy MacroStack; |
| 1511 | PiecesTy Pieces; |
| 1512 | |
| 1513 | for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) { |
| 1514 | // Get the location of the PathDiagnosticPiece. |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1515 | const FullSourceLoc Loc = I->getLocation().asLocation(); |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 1516 | |
| 1517 | // Determine the instantiation location, which is the location we group |
| 1518 | // related PathDiagnosticPieces. |
| 1519 | SourceLocation InstantiationLoc = Loc.isMacroID() ? |
| 1520 | SM.getInstantiationLoc(Loc) : |
| 1521 | SourceLocation(); |
| 1522 | |
| 1523 | if (Loc.isFileID()) { |
| 1524 | MacroStack.clear(); |
| 1525 | Pieces.push_back(&*I); |
| 1526 | continue; |
| 1527 | } |
| 1528 | |
| 1529 | assert(Loc.isMacroID()); |
| 1530 | |
| 1531 | // Is the PathDiagnosticPiece within the same macro group? |
| 1532 | if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { |
| 1533 | MacroStack.back().first->push_back(&*I); |
| 1534 | continue; |
| 1535 | } |
| 1536 | |
| 1537 | // We aren't in the same group. Are we descending into a new macro |
| 1538 | // or are part of an old one? |
| 1539 | PathDiagnosticMacroPiece *MacroGroup = 0; |
| 1540 | |
| 1541 | SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? |
| 1542 | SM.getInstantiationLoc(Loc) : |
| 1543 | SourceLocation(); |
| 1544 | |
| 1545 | // Walk the entire macro stack. |
| 1546 | while (!MacroStack.empty()) { |
| 1547 | if (InstantiationLoc == MacroStack.back().second) { |
| 1548 | MacroGroup = MacroStack.back().first; |
| 1549 | break; |
| 1550 | } |
| 1551 | |
| 1552 | if (ParentInstantiationLoc == MacroStack.back().second) { |
| 1553 | MacroGroup = MacroStack.back().first; |
| 1554 | break; |
| 1555 | } |
| 1556 | |
| 1557 | MacroStack.pop_back(); |
| 1558 | } |
| 1559 | |
| 1560 | if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { |
| 1561 | // Create a new macro group and add it to the stack. |
| 1562 | PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc); |
| 1563 | |
| 1564 | if (MacroGroup) |
| 1565 | MacroGroup->push_back(NewGroup); |
| 1566 | else { |
| 1567 | assert(InstantiationLoc.isFileID()); |
| 1568 | Pieces.push_back(NewGroup); |
| 1569 | } |
| 1570 | |
| 1571 | MacroGroup = NewGroup; |
| 1572 | MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); |
| 1573 | } |
| 1574 | |
| 1575 | // Finally, add the PathDiagnosticPiece to the group. |
| 1576 | MacroGroup->push_back(&*I); |
| 1577 | } |
| 1578 | |
| 1579 | // Now take the pieces and construct a new PathDiagnostic. |
| 1580 | PD.resetPath(false); |
| 1581 | |
| 1582 | for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) { |
| 1583 | if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 1584 | if (!MP->containsEvent()) { |
| 1585 | delete MP; |
| 1586 | continue; |
| 1587 | } |
| 1588 | |
| 1589 | PD.push_back(*I); |
| 1590 | } |
| 1591 | } |
| 1592 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1593 | void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
| 1594 | BugReportEquivClass& EQ) { |
| 1595 | |
| 1596 | std::vector<const ExplodedNode<GRState>*> Nodes; |
| 1597 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1598 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { |
| 1599 | const ExplodedNode<GRState>* N = I->getEndNode(); |
| 1600 | if (N) Nodes.push_back(N); |
| 1601 | } |
| 1602 | |
| 1603 | if (Nodes.empty()) |
| 1604 | return; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1605 | |
| 1606 | // 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] | 1607 | // node to a root. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1608 | const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1609 | std::pair<ExplodedNode<GRState>*, unsigned> >& |
| 1610 | GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size()); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1611 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1612 | // Find the BugReport with the original location. |
| 1613 | BugReport *R = 0; |
| 1614 | unsigned i = 0; |
| 1615 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i) |
| 1616 | if (i == GPair.second.second) { R = *I; break; } |
| 1617 | |
| 1618 | assert(R && "No original report found for sliced graph."); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1619 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1620 | llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first); |
| 1621 | llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1622 | const ExplodedNode<GRState> *N = GPair.second.first; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1623 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1624 | // Start building the path diagnostic... |
| 1625 | if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N)) |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1626 | PD.push_back(Piece); |
| 1627 | else |
| 1628 | return; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1629 | |
| 1630 | PathDiagnosticBuilder PDB(*this, ReportGraph.get(), R, BackMap.get(), |
| 1631 | getStateManager().getCodeDecl(), |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 1632 | getPathDiagnosticClient()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1633 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1634 | switch (PDB.getGenerationScheme()) { |
| 1635 | case PathDiagnosticClient::Extensive: |
Ted Kremenek | 5fb5dfb | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 1636 | GenerateExtensivePathDiagnostic(PD,PDB, N); |
| 1637 | break; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1638 | case PathDiagnosticClient::Minimal: |
| 1639 | GenerateMinimalPathDiagnostic(PD, PDB, N); |
| 1640 | break; |
| 1641 | } |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1644 | void BugReporter::Register(BugType *BT) { |
| 1645 | BugTypes = F.Add(BugTypes, BT); |
Ted Kremenek | 76d90c8 | 2008-05-16 18:33:14 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1648 | void BugReporter::EmitReport(BugReport* R) { |
| 1649 | // Compute the bug report's hash to determine its equivalence class. |
| 1650 | llvm::FoldingSetNodeID ID; |
| 1651 | R->Profile(ID); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1652 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1653 | // Lookup the equivance class. If there isn't one, create it. |
| 1654 | BugType& BT = R->getBugType(); |
| 1655 | Register(&BT); |
| 1656 | void *InsertPos; |
| 1657 | BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos); |
| 1658 | |
| 1659 | if (!EQ) { |
| 1660 | EQ = new BugReportEquivClass(R); |
| 1661 | BT.EQClasses.InsertNode(EQ, InsertPos); |
| 1662 | } |
| 1663 | else |
| 1664 | EQ->AddReport(R); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1667 | void BugReporter::FlushReport(BugReportEquivClass& EQ) { |
| 1668 | assert(!EQ.Reports.empty()); |
| 1669 | BugReport &R = **EQ.begin(); |
| 1670 | |
| 1671 | // FIXME: Make sure we use the 'R' for the path that was actually used. |
| 1672 | // Probably doesn't make a difference in practice. |
| 1673 | BugType& BT = R.getBugType(); |
| 1674 | |
| 1675 | llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(), |
| 1676 | R.getDescription(), |
| 1677 | BT.getCategory())); |
| 1678 | GeneratePathDiagnostic(*D.get(), EQ); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Get the meta data. |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1681 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1682 | 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] | 1683 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1684 | // Emit a summary diagnostic to the regular Diagnostics engine. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 1685 | PathDiagnosticClient* PD = getPathDiagnosticClient(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1686 | const SourceRange *Beg = 0, *End = 0; |
| 1687 | R.getRanges(*this, Beg, End); |
| 1688 | Diagnostic& Diag = getDiagnostic(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1689 | FullSourceLoc L(R.getLocation(), getSourceManager()); |
Ted Kremenek | d90e708 | 2009-02-07 22:36:41 +0000 | [diff] [blame] | 1690 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 1691 | R.getDescription().c_str()); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1692 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1693 | switch (End-Beg) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1694 | default: assert(0 && "Don't handle this many ranges yet!"); |
| 1695 | case 0: Diag.Report(L, ErrorDiag); break; |
| 1696 | case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break; |
| 1697 | case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break; |
| 1698 | 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] | 1699 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1700 | |
| 1701 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
| 1702 | if (!PD) |
| 1703 | return; |
| 1704 | |
| 1705 | if (D->empty()) { |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 1706 | PathDiagnosticPiece* piece = |
| 1707 | new PathDiagnosticEventPiece(L, R.getDescription()); |
| 1708 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1709 | for ( ; Beg != End; ++Beg) piece->addRange(*Beg); |
| 1710 | D->push_back(piece); |
| 1711 | } |
| 1712 | |
| 1713 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1714 | } |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1715 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1716 | void BugReporter::EmitBasicReport(const char* name, const char* str, |
| 1717 | SourceLocation Loc, |
| 1718 | SourceRange* RBeg, unsigned NumRanges) { |
| 1719 | EmitBasicReport(name, "", str, Loc, RBeg, NumRanges); |
| 1720 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1721 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1722 | void BugReporter::EmitBasicReport(const char* name, const char* category, |
| 1723 | const char* str, SourceLocation Loc, |
| 1724 | SourceRange* RBeg, unsigned NumRanges) { |
| 1725 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1726 | // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'. |
| 1727 | BugType *BT = new BugType(name, category); |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1728 | FullSourceLoc L = getContext().getFullLoc(Loc); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1729 | RangedBugReport *R = new DiagBugReport(*BT, str, L); |
| 1730 | for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg); |
| 1731 | EmitReport(R); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1732 | } |