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