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