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