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