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