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/Basic/SourceManager.h" |
| 18 | #include "clang/Basic/SourceLocation.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/CFG.h" |
| 21 | #include "clang/AST/Expr.h" |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 22 | #include "clang/AST/ParentMap.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 | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | // static functions. |
| 35 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 36 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 37 | static inline Stmt* GetStmt(ProgramPoint P) { |
| 38 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 39 | return PS->getStmt(); |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 40 | else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 41 | return BE->getSrc()->getTerminator(); |
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 | return 0; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 46 | static inline const ExplodedNode<GRState>* |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 47 | GetPredecessorNode(const ExplodedNode<GRState>* N) { |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 48 | return N->pred_empty() ? NULL : *(N->pred_begin()); |
| 49 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 51 | static inline const ExplodedNode<GRState>* |
| 52 | GetSuccessorNode(const ExplodedNode<GRState>* N) { |
| 53 | return N->succ_empty() ? NULL : *(N->succ_begin()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 56 | static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) { |
| 57 | for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N)) |
| 58 | if (Stmt *S = GetStmt(N->getLocation())) |
| 59 | return S; |
| 60 | |
| 61 | return 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 64 | static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) { |
| 65 | for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N)) |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 66 | if (Stmt *S = GetStmt(N->getLocation())) { |
| 67 | // Check if the statement is '?' or '&&'/'||'. These are "merges", |
| 68 | // not actual statement points. |
| 69 | switch (S->getStmtClass()) { |
| 70 | case Stmt::ChooseExprClass: |
| 71 | case Stmt::ConditionalOperatorClass: continue; |
| 72 | case Stmt::BinaryOperatorClass: { |
| 73 | BinaryOperator::Opcode Op = cast<BinaryOperator>(S)->getOpcode(); |
| 74 | if (Op == BinaryOperator::LAnd || Op == BinaryOperator::LOr) |
| 75 | continue; |
| 76 | break; |
| 77 | } |
| 78 | default: |
| 79 | break; |
| 80 | } |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 81 | return S; |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) { |
| 88 | if (Stmt *S = GetStmt(N->getLocation())) |
| 89 | return S; |
| 90 | |
| 91 | return GetPreviousStmt(N); |
| 92 | } |
| 93 | |
| 94 | static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) { |
| 95 | if (Stmt *S = GetStmt(N->getLocation())) |
| 96 | return S; |
| 97 | |
| 98 | return GetNextStmt(N); |
| 99 | } |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | // Diagnostics for 'execution continues on line XXX'. |
| 103 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 105 | typedef llvm::DenseMap<const ExplodedNode<GRState>*, |
| 106 | const ExplodedNode<GRState>*> NodeBackMap; |
| 107 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 108 | namespace { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 109 | class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver { |
| 110 | NodeBackMap& M; |
| 111 | public: |
| 112 | NodeMapClosure(NodeBackMap *m) : M(*m) {} |
| 113 | ~NodeMapClosure() {} |
| 114 | |
| 115 | const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) { |
| 116 | NodeBackMap::iterator I = M.find(N); |
| 117 | return I == M.end() ? 0 : I->second; |
| 118 | } |
| 119 | }; |
| 120 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 121 | class VISIBILITY_HIDDEN PathDiagnosticBuilder { |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 122 | GRBugReporter &BR; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 123 | SourceManager &SMgr; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 124 | ExplodedGraph<GRState> *ReportGraph; |
| 125 | BugReport *R; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 126 | const Decl& CodeDecl; |
| 127 | PathDiagnosticClient *PDC; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 128 | llvm::OwningPtr<ParentMap> PM; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 129 | NodeMapClosure NMC; |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 130 | public: |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 131 | PathDiagnosticBuilder(GRBugReporter &br, ExplodedGraph<GRState> *reportGraph, |
| 132 | BugReport *r, NodeBackMap *Backmap, |
| 133 | const Decl& codedecl, PathDiagnosticClient *pdc) |
| 134 | : BR(br), SMgr(BR.getSourceManager()), ReportGraph(reportGraph), R(r), |
| 135 | CodeDecl(codedecl), PDC(pdc), NMC(Backmap) {} |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 137 | PathDiagnosticLocation ExecutionContinues(const ExplodedNode<GRState>* N); |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 139 | PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream& os, |
| 140 | const ExplodedNode<GRState>* N); |
| 141 | |
| 142 | ParentMap& getParentMap() { |
| 143 | if (PM.get() == 0) PM.reset(new ParentMap(CodeDecl.getBody())); |
| 144 | return *PM.get(); |
| 145 | } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 146 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 147 | ExplodedGraph<GRState>& getGraph() { return *ReportGraph; } |
| 148 | NodeMapClosure& getNodeMapClosure() { return NMC; } |
| 149 | ASTContext& getContext() { return BR.getContext(); } |
| 150 | SourceManager& getSourceManager() { return SMgr; } |
| 151 | BugReport& getReport() { return *R; } |
| 152 | GRBugReporter& getBugReporter() { return BR; } |
| 153 | GRStateManager& getStateManager() { return BR.getStateManager(); } |
| 154 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 155 | PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S); |
| 156 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 157 | PathDiagnosticClient::PathGenerationScheme getGenerationScheme() const { |
| 158 | return PDC ? PDC->getGenerationScheme() : PathDiagnosticClient::Extensive; |
| 159 | } |
| 160 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 161 | bool supportsLogicalOpControlFlow() const { |
| 162 | return PDC ? PDC->supportsLogicalOpControlFlow() : true; |
| 163 | } |
| 164 | }; |
| 165 | } // end anonymous namespace |
| 166 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 167 | PathDiagnosticLocation |
| 168 | PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode<GRState>* N) { |
| 169 | if (Stmt *S = GetNextStmt(N)) |
| 170 | return PathDiagnosticLocation(S, SMgr); |
| 171 | |
| 172 | return FullSourceLoc(CodeDecl.getBody()->getRBracLoc(), SMgr); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 175 | PathDiagnosticLocation |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 176 | PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream& os, |
| 177 | const ExplodedNode<GRState>* N) { |
| 178 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 179 | // Slow, but probably doesn't matter. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 180 | if (os.str().empty()) |
| 181 | os << ' '; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 183 | const PathDiagnosticLocation &Loc = ExecutionContinues(N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 185 | if (Loc.asStmt()) |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 186 | os << "Execution continues on line " |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 187 | << SMgr.getInstantiationLineNumber(Loc.asLocation()) << '.'; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 188 | else |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 189 | os << "Execution jumps to the end of the " |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 190 | << (isa<ObjCMethodDecl>(CodeDecl) ? "method" : "function") << '.'; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 191 | |
| 192 | return Loc; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 195 | PathDiagnosticLocation |
| 196 | PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) { |
| 197 | assert(S && "Null Stmt* passed to getEnclosingStmtLocation"); |
| 198 | ParentMap &P = getParentMap(); |
| 199 | while (isa<Expr>(S)) { |
| 200 | const Stmt *Parent = P.getParent(S); |
| 201 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 202 | if (!Parent) |
| 203 | break; |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 205 | switch (Parent->getStmtClass()) { |
| 206 | case Stmt::CompoundStmtClass: |
| 207 | case Stmt::StmtExprClass: |
Ted Kremenek | 1d9a23a | 2009-03-28 04:08:14 +0000 | [diff] [blame] | 208 | return PathDiagnosticLocation(S, SMgr); |
| 209 | case Stmt::ChooseExprClass: |
| 210 | // Similar to '?' if we are referring to condition, just have the edge |
| 211 | // point to the entire choose expression. |
| 212 | if (cast<ChooseExpr>(Parent)->getCond() == S) |
| 213 | return PathDiagnosticLocation(Parent, SMgr); |
| 214 | else |
| 215 | return PathDiagnosticLocation(S, SMgr); |
| 216 | case Stmt::ConditionalOperatorClass: |
| 217 | // For '?', if we are referring to condition, just have the edge point |
| 218 | // to the entire '?' expression. |
| 219 | if (cast<ConditionalOperator>(Parent)->getCond() == S) |
| 220 | return PathDiagnosticLocation(Parent, SMgr); |
| 221 | else |
| 222 | return PathDiagnosticLocation(S, SMgr); |
Ted Kremenek | af3e3d5 | 2009-03-28 03:37:59 +0000 | [diff] [blame] | 223 | case Stmt::DoStmtClass: |
| 224 | if (cast<DoStmt>(Parent)->getCond() != S) |
| 225 | return PathDiagnosticLocation(S, SMgr); |
| 226 | break; |
| 227 | case Stmt::ForStmtClass: |
| 228 | if (cast<ForStmt>(Parent)->getBody() == S) |
| 229 | return PathDiagnosticLocation(S, SMgr); |
| 230 | break; |
| 231 | case Stmt::IfStmtClass: |
| 232 | if (cast<IfStmt>(Parent)->getCond() != S) |
| 233 | return PathDiagnosticLocation(S, SMgr); |
| 234 | break; |
| 235 | case Stmt::ObjCForCollectionStmtClass: |
| 236 | if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S) |
| 237 | return PathDiagnosticLocation(S, SMgr); |
| 238 | break; |
| 239 | case Stmt::WhileStmtClass: |
| 240 | if (cast<WhileStmt>(Parent)->getCond() != S) |
| 241 | return PathDiagnosticLocation(S, SMgr); |
| 242 | break; |
| 243 | default: |
| 244 | break; |
| 245 | } |
| 246 | |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 247 | S = Parent; |
| 248 | } |
| 249 | |
| 250 | assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); |
| 251 | return PathDiagnosticLocation(S, SMgr); |
| 252 | } |
| 253 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 254 | //===----------------------------------------------------------------------===// |
| 255 | // Methods for BugType and subclasses. |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | BugType::~BugType() {} |
| 258 | void BugType::FlushReports(BugReporter &BR) {} |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 259 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 260 | //===----------------------------------------------------------------------===// |
| 261 | // Methods for BugReport and subclasses. |
| 262 | //===----------------------------------------------------------------------===// |
| 263 | BugReport::~BugReport() {} |
| 264 | RangedBugReport::~RangedBugReport() {} |
| 265 | |
| 266 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 267 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 268 | Stmt *S = NULL; |
| 269 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 270 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 271 | if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 272 | } |
| 273 | if (!S) S = GetStmt(ProgP); |
| 274 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 275 | return S; |
| 276 | } |
| 277 | |
| 278 | PathDiagnosticPiece* |
| 279 | BugReport::getEndPath(BugReporter& BR, |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 280 | const ExplodedNode<GRState>* EndPathNode) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 281 | |
| 282 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 283 | |
| 284 | if (!S) |
| 285 | return NULL; |
| 286 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 287 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 288 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 290 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 291 | getRanges(BR, Beg, End); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 293 | for (; Beg != End; ++Beg) |
| 294 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 295 | |
| 296 | return P; |
| 297 | } |
| 298 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 299 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 300 | const SourceRange*& end) { |
| 301 | |
| 302 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 303 | R = E->getSourceRange(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 304 | assert(R.isValid()); |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 305 | beg = &R; |
| 306 | end = beg+1; |
| 307 | } |
| 308 | else |
| 309 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 312 | SourceLocation BugReport::getLocation() const { |
| 313 | if (EndNode) |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 314 | if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) { |
| 315 | // For member expressions, return the location of the '.' or '->'. |
| 316 | if (MemberExpr* ME = dyn_cast<MemberExpr>(S)) |
| 317 | return ME->getMemberLoc(); |
| 318 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 319 | return S->getLocStart(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 320 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 321 | |
| 322 | return FullSourceLoc(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 325 | PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N, |
| 326 | const ExplodedNode<GRState>* PrevN, |
| 327 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 328 | BugReporter& BR, |
| 329 | NodeResolver &NR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 330 | return NULL; |
| 331 | } |
| 332 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 333 | //===----------------------------------------------------------------------===// |
| 334 | // Methods for BugReporter and subclasses. |
| 335 | //===----------------------------------------------------------------------===// |
| 336 | |
| 337 | BugReportEquivClass::~BugReportEquivClass() { |
| 338 | for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; |
| 339 | } |
| 340 | |
| 341 | GRBugReporter::~GRBugReporter() { FlushReports(); } |
| 342 | BugReporterData::~BugReporterData() {} |
| 343 | |
| 344 | ExplodedGraph<GRState>& |
| 345 | GRBugReporter::getGraph() { return Eng.getGraph(); } |
| 346 | |
| 347 | GRStateManager& |
| 348 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
| 349 | |
| 350 | BugReporter::~BugReporter() { FlushReports(); } |
| 351 | |
| 352 | void BugReporter::FlushReports() { |
| 353 | if (BugTypes.isEmpty()) |
| 354 | return; |
| 355 | |
| 356 | // First flush the warnings for each BugType. This may end up creating new |
| 357 | // warnings and new BugTypes. Because ImmutableSet is a functional data |
| 358 | // structure, we do not need to worry about the iterators being invalidated. |
| 359 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 360 | const_cast<BugType*>(*I)->FlushReports(*this); |
| 361 | |
| 362 | // Iterate through BugTypes a second time. BugTypes may have been updated |
| 363 | // with new BugType objects and new warnings. |
| 364 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) { |
| 365 | BugType *BT = const_cast<BugType*>(*I); |
| 366 | |
| 367 | typedef llvm::FoldingSet<BugReportEquivClass> SetTy; |
| 368 | SetTy& EQClasses = BT->EQClasses; |
| 369 | |
| 370 | for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ |
| 371 | BugReportEquivClass& EQ = *EI; |
| 372 | FlushReport(EQ); |
| 373 | } |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 375 | // Delete the BugType object. This will also delete the equivalence |
| 376 | // classes. |
| 377 | delete BT; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 378 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 379 | |
| 380 | // Remove all references to the BugType objects. |
| 381 | BugTypes = F.GetEmptySet(); |
| 382 | } |
| 383 | |
| 384 | //===----------------------------------------------------------------------===// |
| 385 | // PathDiagnostics generation. |
| 386 | //===----------------------------------------------------------------------===// |
| 387 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 388 | static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 389 | std::pair<ExplodedNode<GRState>*, unsigned> > |
| 390 | MakeReportGraph(const ExplodedGraph<GRState>* G, |
| 391 | const ExplodedNode<GRState>** NStart, |
| 392 | const ExplodedNode<GRState>** NEnd) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 394 | // Create the trimmed graph. It will contain the shortest paths from the |
| 395 | // error nodes to the root. In the new graph we should only have one |
| 396 | // error node unless there are two or more error nodes with the same minimum |
| 397 | // path length. |
| 398 | ExplodedGraph<GRState>* GTrim; |
| 399 | InterExplodedGraphMap<GRState>* NMap; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 400 | |
| 401 | llvm::DenseMap<const void*, const void*> InverseMap; |
| 402 | llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 403 | |
| 404 | // Create owning pointers for GTrim and NMap just to ensure that they are |
| 405 | // released when this function exists. |
| 406 | llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim); |
| 407 | llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap); |
| 408 | |
| 409 | // Find the (first) error node in the trimmed graph. We just need to consult |
| 410 | // the node map (NMap) which maps from nodes in the original graph to nodes |
| 411 | // in the new graph. |
| 412 | const ExplodedNode<GRState>* N = 0; |
| 413 | unsigned NodeIndex = 0; |
| 414 | |
| 415 | for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I) |
| 416 | if ((N = NMap->getMappedNode(*I))) { |
| 417 | NodeIndex = (I - NStart) / sizeof(*I); |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | assert(N && "No error node found in the trimmed graph."); |
| 422 | |
| 423 | // Create a new (third!) graph with a single path. This is the graph |
| 424 | // that will be returned to the caller. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 425 | ExplodedGraph<GRState> *GNew = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 426 | new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 427 | GTrim->getContext()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 428 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 429 | // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 430 | // to the root node, and then construct a new graph that contains only |
| 431 | // a single path. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 432 | llvm::DenseMap<const void*,unsigned> Visited; |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 433 | std::queue<const ExplodedNode<GRState>*> WS; |
| 434 | WS.push(N); |
| 435 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 436 | unsigned cnt = 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 437 | const ExplodedNode<GRState>* Root = 0; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 439 | while (!WS.empty()) { |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 440 | const ExplodedNode<GRState>* Node = WS.front(); |
| 441 | WS.pop(); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 442 | |
| 443 | if (Visited.find(Node) != Visited.end()) |
| 444 | continue; |
| 445 | |
| 446 | Visited[Node] = cnt++; |
| 447 | |
| 448 | if (Node->pred_empty()) { |
| 449 | Root = Node; |
| 450 | break; |
| 451 | } |
| 452 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 453 | for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(), |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 454 | E=Node->pred_end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 455 | WS.push(*I); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 456 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 457 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 458 | assert (Root); |
| 459 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 460 | // 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] | 461 | // with the lowest number. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 462 | ExplodedNode<GRState> *Last = 0, *First = 0; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 463 | NodeBackMap *BM = new NodeBackMap(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 465 | for ( N = Root ;;) { |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 466 | // Lookup the number associated with the current node. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 467 | llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 468 | assert (I != Visited.end()); |
| 469 | |
| 470 | // Create the equivalent node in the new graph with the same state |
| 471 | // and location. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 472 | ExplodedNode<GRState>* NewN = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 473 | GNew->getNode(N->getLocation(), N->getState()); |
| 474 | |
| 475 | // Store the mapping to the original node. |
| 476 | llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N); |
| 477 | assert(IMitr != InverseMap.end() && "No mapping to original node."); |
| 478 | (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 479 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 480 | // Link up the new node with the previous node. |
| 481 | if (Last) |
| 482 | NewN->addPredecessor(Last); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 483 | |
| 484 | Last = NewN; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 486 | // Are we at the final node? |
| 487 | if (I->second == 0) { |
| 488 | First = NewN; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 489 | break; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 490 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 491 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 492 | // Find the next successor node. We choose the node that is marked |
| 493 | // with the lowest DFS number. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 494 | ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin(); |
| 495 | ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end(); |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 496 | N = 0; |
| 497 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 498 | for (unsigned MinVal = 0; SI != SE; ++SI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 499 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 500 | I = Visited.find(*SI); |
| 501 | |
| 502 | if (I == Visited.end()) |
| 503 | continue; |
| 504 | |
| 505 | if (!N || I->second < MinVal) { |
| 506 | N = *SI; |
| 507 | MinVal = I->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 508 | } |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 509 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 510 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 511 | assert (N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 512 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 513 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 514 | assert (First); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 515 | return std::make_pair(std::make_pair(GNew, BM), |
| 516 | std::make_pair(First, NodeIndex)); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 519 | static const VarDecl* |
| 520 | GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N, |
| 521 | GRStateManager& VMgr, SVal X) { |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 522 | |
| 523 | for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) { |
| 524 | |
| 525 | ProgramPoint P = N->getLocation(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 526 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 527 | if (!isa<PostStmt>(P)) |
| 528 | continue; |
| 529 | |
| 530 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 531 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 532 | if (!DR) |
| 533 | continue; |
| 534 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 535 | SVal Y = VMgr.GetSVal(N->getState(), DR); |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 536 | |
| 537 | if (X != Y) |
| 538 | continue; |
| 539 | |
| 540 | VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 541 | |
| 542 | if (!VD) |
| 543 | continue; |
| 544 | |
| 545 | return VD; |
| 546 | } |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 551 | namespace { |
| 552 | class VISIBILITY_HIDDEN NotableSymbolHandler |
| 553 | : public StoreManager::BindingsHandler { |
| 554 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 555 | SymbolRef Sym; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 556 | const GRState* PrevSt; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 557 | const Stmt* S; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 558 | GRStateManager& VMgr; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 559 | const ExplodedNode<GRState>* Pred; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 560 | PathDiagnostic& PD; |
| 561 | BugReporter& BR; |
| 562 | |
| 563 | public: |
| 564 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 565 | NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s, |
| 566 | GRStateManager& vmgr, const ExplodedNode<GRState>* pred, |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 567 | PathDiagnostic& pd, BugReporter& br) |
| 568 | : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {} |
| 569 | |
Ted Kremenek | 0297ee0 | 2009-03-30 18:39:15 +0000 | [diff] [blame] | 570 | bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, |
| 571 | SVal V) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 572 | |
Ted Kremenek | 0297ee0 | 2009-03-30 18:39:15 +0000 | [diff] [blame] | 573 | SymbolRef ScanSym = V.getAsSymbol(); |
| 574 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 575 | if (ScanSym != Sym) |
| 576 | return true; |
| 577 | |
| 578 | // Check if the previous state has this binding. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 579 | SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R)); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 580 | |
| 581 | if (X == V) // Same binding? |
| 582 | return true; |
| 583 | |
| 584 | // Different binding. Only handle assignments for now. We don't pull |
| 585 | // this check out of the loop because we will eventually handle other |
| 586 | // cases. |
| 587 | |
| 588 | VarDecl *VD = 0; |
| 589 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 590 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 591 | if (!B->isAssignmentOp()) |
| 592 | return true; |
| 593 | |
| 594 | // What variable did we assign to? |
| 595 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts()); |
| 596 | |
| 597 | if (!DR) |
| 598 | return true; |
| 599 | |
| 600 | VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 601 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 602 | else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) { |
Ted Kremenek | f21a4b4 | 2008-10-06 18:37:46 +0000 | [diff] [blame] | 603 | // FIXME: Eventually CFGs won't have DeclStmts. Right now we |
| 604 | // assume that each DeclStmt has a single Decl. This invariant |
| 605 | // holds by contruction in the CFG. |
| 606 | VD = dyn_cast<VarDecl>(*DS->decl_begin()); |
| 607 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 608 | |
| 609 | if (!VD) |
| 610 | return true; |
| 611 | |
| 612 | // What is the most recently referenced variable with this binding? |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 613 | const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 614 | |
| 615 | if (!MostRecent) |
| 616 | return true; |
| 617 | |
| 618 | // Create the diagnostic. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 619 | FullSourceLoc L(S->getLocStart(), BR.getSourceManager()); |
| 620 | |
Ted Kremenek | 3daea0a | 2009-02-26 20:29:19 +0000 | [diff] [blame] | 621 | if (Loc::IsLocType(VD->getType())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 622 | std::string msg = "'" + std::string(VD->getNameAsString()) + |
| 623 | "' now aliases '" + MostRecent->getNameAsString() + "'"; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 624 | |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 625 | PD.push_front(new PathDiagnosticEventPiece(L, msg)); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return true; |
| 629 | } |
| 630 | }; |
| 631 | } |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 632 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 633 | static void HandleNotableSymbol(const ExplodedNode<GRState>* N, |
| 634 | const Stmt* S, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 635 | SymbolRef Sym, BugReporter& BR, |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 636 | PathDiagnostic& PD) { |
| 637 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 638 | const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin(); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 639 | const GRState* PrevSt = Pred ? Pred->getState() : 0; |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 640 | |
| 641 | if (!PrevSt) |
| 642 | return; |
| 643 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 644 | // Look at the region bindings of the current state that map to the |
| 645 | // specified symbol. Are any of them not in the previous state? |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 646 | GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 647 | NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR); |
| 648 | cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H); |
| 649 | } |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 650 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 651 | namespace { |
| 652 | class VISIBILITY_HIDDEN ScanNotableSymbols |
| 653 | : public StoreManager::BindingsHandler { |
| 654 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 655 | llvm::SmallSet<SymbolRef, 10> AlreadyProcessed; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 656 | const ExplodedNode<GRState>* N; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 657 | Stmt* S; |
| 658 | GRBugReporter& BR; |
| 659 | PathDiagnostic& PD; |
| 660 | |
| 661 | public: |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 662 | ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br, |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 663 | PathDiagnostic& pd) |
| 664 | : N(n), S(s), BR(br), PD(pd) {} |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 665 | |
Ted Kremenek | be91224 | 2009-03-05 16:31:07 +0000 | [diff] [blame] | 666 | bool HandleBinding(StoreManager& SMgr, Store store, |
| 667 | const MemRegion* R, SVal V) { |
Ted Kremenek | 93e7145 | 2009-03-30 19:53:37 +0000 | [diff] [blame] | 668 | |
| 669 | SymbolRef ScanSym = V.getAsSymbol(); |
| 670 | |
| 671 | if (!ScanSym) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 672 | return true; |
| 673 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 674 | if (!BR.isNotable(ScanSym)) |
| 675 | return true; |
| 676 | |
| 677 | if (AlreadyProcessed.count(ScanSym)) |
| 678 | return true; |
| 679 | |
| 680 | AlreadyProcessed.insert(ScanSym); |
| 681 | |
| 682 | HandleNotableSymbol(N, S, ScanSym, BR, PD); |
| 683 | return true; |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 684 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 685 | }; |
| 686 | } // end anonymous namespace |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 687 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 688 | /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object |
| 689 | /// and collapses PathDiagosticPieces that are expanded by macros. |
| 690 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) { |
| 691 | typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> > |
| 692 | MacroStackTy; |
| 693 | |
| 694 | typedef std::vector<PathDiagnosticPiece*> |
| 695 | PiecesTy; |
| 696 | |
| 697 | MacroStackTy MacroStack; |
| 698 | PiecesTy Pieces; |
| 699 | |
| 700 | for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) { |
| 701 | // Get the location of the PathDiagnosticPiece. |
| 702 | const FullSourceLoc Loc = I->getLocation(); |
| 703 | |
| 704 | // Determine the instantiation location, which is the location we group |
| 705 | // related PathDiagnosticPieces. |
| 706 | SourceLocation InstantiationLoc = Loc.isMacroID() ? |
| 707 | SM.getInstantiationLoc(Loc) : |
| 708 | SourceLocation(); |
| 709 | |
| 710 | if (Loc.isFileID()) { |
| 711 | MacroStack.clear(); |
| 712 | Pieces.push_back(&*I); |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | assert(Loc.isMacroID()); |
| 717 | |
| 718 | // Is the PathDiagnosticPiece within the same macro group? |
| 719 | if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { |
| 720 | MacroStack.back().first->push_back(&*I); |
| 721 | continue; |
| 722 | } |
| 723 | |
| 724 | // We aren't in the same group. Are we descending into a new macro |
| 725 | // or are part of an old one? |
| 726 | PathDiagnosticMacroPiece *MacroGroup = 0; |
| 727 | |
| 728 | SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? |
| 729 | SM.getInstantiationLoc(Loc) : |
| 730 | SourceLocation(); |
| 731 | |
| 732 | // Walk the entire macro stack. |
| 733 | while (!MacroStack.empty()) { |
| 734 | if (InstantiationLoc == MacroStack.back().second) { |
| 735 | MacroGroup = MacroStack.back().first; |
| 736 | break; |
| 737 | } |
| 738 | |
| 739 | if (ParentInstantiationLoc == MacroStack.back().second) { |
| 740 | MacroGroup = MacroStack.back().first; |
| 741 | break; |
| 742 | } |
| 743 | |
| 744 | MacroStack.pop_back(); |
| 745 | } |
| 746 | |
| 747 | if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { |
| 748 | // Create a new macro group and add it to the stack. |
| 749 | PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc); |
| 750 | |
| 751 | if (MacroGroup) |
| 752 | MacroGroup->push_back(NewGroup); |
| 753 | else { |
| 754 | assert(InstantiationLoc.isFileID()); |
| 755 | Pieces.push_back(NewGroup); |
| 756 | } |
| 757 | |
| 758 | MacroGroup = NewGroup; |
| 759 | MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); |
| 760 | } |
| 761 | |
| 762 | // Finally, add the PathDiagnosticPiece to the group. |
| 763 | MacroGroup->push_back(&*I); |
| 764 | } |
| 765 | |
| 766 | // Now take the pieces and construct a new PathDiagnostic. |
| 767 | PD.resetPath(false); |
| 768 | |
| 769 | for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) { |
| 770 | if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 771 | if (!MP->containsEvent()) { |
| 772 | delete MP; |
| 773 | continue; |
| 774 | } |
| 775 | |
| 776 | PD.push_back(*I); |
| 777 | } |
| 778 | } |
| 779 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 780 | static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD, |
| 781 | PathDiagnosticBuilder &PDB, |
| 782 | const ExplodedNode<GRState> *N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 783 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 784 | void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
| 785 | BugReportEquivClass& EQ) { |
| 786 | |
| 787 | std::vector<const ExplodedNode<GRState>*> Nodes; |
| 788 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 789 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { |
| 790 | const ExplodedNode<GRState>* N = I->getEndNode(); |
| 791 | if (N) Nodes.push_back(N); |
| 792 | } |
| 793 | |
| 794 | if (Nodes.empty()) |
| 795 | return; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 796 | |
| 797 | // 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] | 798 | // node to a root. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 799 | const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 800 | std::pair<ExplodedNode<GRState>*, unsigned> >& |
| 801 | GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size()); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 802 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 803 | // Find the BugReport with the original location. |
| 804 | BugReport *R = 0; |
| 805 | unsigned i = 0; |
| 806 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i) |
| 807 | if (i == GPair.second.second) { R = *I; break; } |
| 808 | |
| 809 | assert(R && "No original report found for sliced graph."); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 810 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 811 | llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first); |
| 812 | llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 813 | const ExplodedNode<GRState> *N = GPair.second.first; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 814 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 815 | // Start building the path diagnostic... |
| 816 | if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N)) |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 817 | PD.push_back(Piece); |
| 818 | else |
| 819 | return; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 820 | |
| 821 | PathDiagnosticBuilder PDB(*this, ReportGraph.get(), R, BackMap.get(), |
| 822 | getStateManager().getCodeDecl(), |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 823 | getPathDiagnosticClient()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 824 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 825 | switch (PDB.getGenerationScheme()) { |
| 826 | case PathDiagnosticClient::Extensive: |
| 827 | case PathDiagnosticClient::Minimal: |
| 828 | GenerateMinimalPathDiagnostic(PD, PDB, N); |
| 829 | break; |
| 830 | } |
| 831 | |
| 832 | // After constructing the full PathDiagnostic, do a pass over it to compact |
| 833 | // PathDiagnosticPieces that occur within a macro. |
| 834 | CompactPathDiagnostic(PD, PDB.getSourceManager()); |
| 835 | } |
| 836 | |
| 837 | static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD, |
| 838 | PathDiagnosticBuilder &PDB, |
| 839 | const ExplodedNode<GRState> *N) { |
| 840 | |
| 841 | |
| 842 | ASTContext& Ctx = PDB.getContext(); |
| 843 | SourceManager& SMgr = PDB.getSourceManager(); |
| 844 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 845 | ? NULL : *(N->pred_begin()); |
| 846 | |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 847 | while (NextNode) { |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 848 | N = NextNode; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 849 | NextNode = GetPredecessorNode(N); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 850 | |
| 851 | ProgramPoint P = N->getLocation(); |
| 852 | |
| 853 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 854 | CFGBlock* Src = BE->getSrc(); |
| 855 | CFGBlock* Dst = BE->getDst(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 856 | Stmt* T = Src->getTerminator(); |
| 857 | |
| 858 | if (!T) |
| 859 | continue; |
| 860 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 861 | FullSourceLoc Start(T->getLocStart(), SMgr); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 862 | |
| 863 | switch (T->getStmtClass()) { |
| 864 | default: |
| 865 | break; |
| 866 | |
| 867 | case Stmt::GotoStmtClass: |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 868 | case Stmt::IndirectGotoStmtClass: { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 869 | Stmt* S = GetNextStmt(N); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 870 | |
| 871 | if (!S) |
| 872 | continue; |
| 873 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 874 | std::string sbuf; |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 875 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 876 | const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 877 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 878 | os << "Control jumps to line " |
| 879 | << End.asLocation().getInstantiationLineNumber(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 880 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 881 | os.str())); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 882 | break; |
| 883 | } |
| 884 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 885 | case Stmt::SwitchStmtClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 886 | // Figure out what case arm we took. |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 887 | std::string sbuf; |
| 888 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 889 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 890 | if (Stmt* S = Dst->getLabel()) { |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 891 | PathDiagnosticLocation End(S, SMgr); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 892 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 893 | switch (S->getStmtClass()) { |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 894 | default: |
| 895 | os << "No cases match in the switch statement. " |
| 896 | "Control jumps to line " |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 897 | << End.asLocation().getInstantiationLineNumber(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 898 | break; |
| 899 | case Stmt::DefaultStmtClass: |
| 900 | os << "Control jumps to the 'default' case at line " |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 901 | << End.asLocation().getInstantiationLineNumber(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 902 | break; |
| 903 | |
| 904 | case Stmt::CaseStmtClass: { |
| 905 | os << "Control jumps to 'case "; |
| 906 | CaseStmt* Case = cast<CaseStmt>(S); |
| 907 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
| 908 | |
| 909 | // Determine if it is an enum. |
| 910 | bool GetRawInt = true; |
| 911 | |
| 912 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 913 | // FIXME: Maybe this should be an assertion. Are there cases |
| 914 | // were it is not an EnumConstantDecl? |
| 915 | EnumConstantDecl* D = |
| 916 | dyn_cast<EnumConstantDecl>(DR->getDecl()); |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 917 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 918 | if (D) { |
| 919 | GetRawInt = false; |
| 920 | os << D->getNameAsString(); |
| 921 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 922 | } |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 923 | |
| 924 | if (GetRawInt) { |
| 925 | |
| 926 | // Not an enum. |
| 927 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 928 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 929 | llvm::APSInt V(bits, false); |
| 930 | |
| 931 | if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) { |
| 932 | assert (false && "Case condition must be constant."); |
| 933 | continue; |
| 934 | } |
| 935 | |
| 936 | os << V; |
| 937 | } |
| 938 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 939 | os << ":' at line " |
| 940 | << End.asLocation().getInstantiationLineNumber(); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 941 | break; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 942 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 943 | } |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 944 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 945 | os.str())); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 946 | } |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 947 | else { |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 948 | os << "'Default' branch taken. "; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 949 | const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N); |
| 950 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 951 | os.str())); |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 952 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 953 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 954 | break; |
| 955 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 956 | |
| 957 | case Stmt::BreakStmtClass: |
| 958 | case Stmt::ContinueStmtClass: { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 959 | std::string sbuf; |
| 960 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 961 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 962 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 963 | os.str())); |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 964 | break; |
| 965 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 966 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 967 | // Determine control-flow for ternary '?'. |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 968 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 969 | std::string sbuf; |
| 970 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 1d9a23a | 2009-03-28 04:08:14 +0000 | [diff] [blame] | 971 | os << "'?' condition is "; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 972 | |
| 973 | if (*(Src->succ_begin()+1) == Dst) |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 974 | os << "false"; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 975 | else |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 976 | os << "true"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 977 | |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 978 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 979 | |
Ted Kremenek | 1d9a23a | 2009-03-28 04:08:14 +0000 | [diff] [blame] | 980 | if (const Stmt *S = End.asStmt()) |
| 981 | End = PDB.getEnclosingStmtLocation(S); |
| 982 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 983 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 984 | os.str())); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 985 | break; |
| 986 | } |
| 987 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 988 | // Determine control-flow for short-circuited '&&' and '||'. |
| 989 | case Stmt::BinaryOperatorClass: { |
| 990 | if (!PDB.supportsLogicalOpControlFlow()) |
| 991 | break; |
| 992 | |
| 993 | BinaryOperator *B = cast<BinaryOperator>(T); |
| 994 | std::string sbuf; |
| 995 | llvm::raw_string_ostream os(sbuf); |
| 996 | os << "Left side of '"; |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 997 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 998 | if (B->getOpcode() == BinaryOperator::LAnd) { |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 999 | os << "&&" << "' is "; |
| 1000 | |
| 1001 | if (*(Src->succ_begin()+1) == Dst) { |
| 1002 | os << "false"; |
| 1003 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 1004 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 1005 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1006 | os.str())); |
| 1007 | } |
| 1008 | else { |
| 1009 | os << "true"; |
| 1010 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 1011 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 1012 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1013 | os.str())); |
| 1014 | } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 1015 | } |
| 1016 | else { |
| 1017 | assert(B->getOpcode() == BinaryOperator::LOr); |
Ted Kremenek | f5ab8e6 | 2009-03-28 17:33:57 +0000 | [diff] [blame] | 1018 | os << "||" << "' is "; |
| 1019 | |
| 1020 | if (*(Src->succ_begin()+1) == Dst) { |
| 1021 | os << "false"; |
| 1022 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 1023 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 1024 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1025 | os.str())); |
| 1026 | } |
| 1027 | else { |
| 1028 | os << "true"; |
| 1029 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 1030 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 1031 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1032 | os.str())); |
| 1033 | } |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | } |
| 1038 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1039 | case Stmt::DoStmtClass: { |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1040 | if (*(Src->succ_begin()) == Dst) { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 1041 | std::string sbuf; |
| 1042 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1043 | |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 1044 | os << "Loop condition is true. "; |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1045 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 1046 | |
| 1047 | if (const Stmt *S = End.asStmt()) |
| 1048 | End = PDB.getEnclosingStmtLocation(S); |
| 1049 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1050 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1051 | os.str())); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1052 | } |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1053 | else { |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 1054 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1055 | |
| 1056 | if (const Stmt *S = End.asStmt()) |
| 1057 | End = PDB.getEnclosingStmtLocation(S); |
| 1058 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1059 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1060 | "Loop condition is false. Exiting loop")); |
| 1061 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1062 | |
| 1063 | break; |
| 1064 | } |
| 1065 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1066 | case Stmt::WhileStmtClass: |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1067 | case Stmt::ForStmtClass: { |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1068 | if (*(Src->succ_begin()+1) == Dst) { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 1069 | std::string sbuf; |
| 1070 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1071 | |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 1072 | os << "Loop condition is false. "; |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 1073 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1074 | if (const Stmt *S = End.asStmt()) |
| 1075 | End = PDB.getEnclosingStmtLocation(S); |
| 1076 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1077 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1078 | os.str())); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1079 | } |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1080 | else { |
Ted Kremenek | 00605e0 | 2009-03-27 20:55:39 +0000 | [diff] [blame] | 1081 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1082 | if (const Stmt *S = End.asStmt()) |
| 1083 | End = PDB.getEnclosingStmtLocation(S); |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1084 | |
| 1085 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1086 | "Loop condition is true. Entering loop body")); |
| 1087 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 1088 | |
| 1089 | break; |
| 1090 | } |
| 1091 | |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1092 | case Stmt::IfStmtClass: { |
Ted Kremenek | d8c938b | 2009-03-27 21:16:25 +0000 | [diff] [blame] | 1093 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 1094 | |
| 1095 | if (const Stmt *S = End.asStmt()) |
| 1096 | End = PDB.getEnclosingStmtLocation(S); |
| 1097 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1098 | if (*(Src->succ_begin()+1) == Dst) |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1099 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1100 | "Taking false branch")); |
Ted Kremenek | 025fedc | 2009-03-02 21:41:18 +0000 | [diff] [blame] | 1101 | else |
Ted Kremenek | 082cb8d | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 1102 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 1103 | "Taking true branch")); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1104 | |
| 1105 | break; |
| 1106 | } |
| 1107 | } |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 1108 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 1109 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 1110 | if (PathDiagnosticPiece* p = |
| 1111 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 1112 | PDB.getBugReporter(), |
| 1113 | PDB.getNodeMapClosure())) { |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1114 | PD.push_front(p); |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 1115 | } |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1116 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1117 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 1118 | // Scan the region bindings, and see if a "notable" symbol has a new |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1119 | // lval binding. |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame^] | 1120 | ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD); |
| 1121 | PDB.getStateManager().iterBindings(N->getState(), SNS); |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1122 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 1126 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1127 | void BugReporter::Register(BugType *BT) { |
| 1128 | BugTypes = F.Add(BugTypes, BT); |
Ted Kremenek | 76d90c8 | 2008-05-16 18:33:14 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1131 | void BugReporter::EmitReport(BugReport* R) { |
| 1132 | // Compute the bug report's hash to determine its equivalence class. |
| 1133 | llvm::FoldingSetNodeID ID; |
| 1134 | R->Profile(ID); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1135 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1136 | // Lookup the equivance class. If there isn't one, create it. |
| 1137 | BugType& BT = R->getBugType(); |
| 1138 | Register(&BT); |
| 1139 | void *InsertPos; |
| 1140 | BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos); |
| 1141 | |
| 1142 | if (!EQ) { |
| 1143 | EQ = new BugReportEquivClass(R); |
| 1144 | BT.EQClasses.InsertNode(EQ, InsertPos); |
| 1145 | } |
| 1146 | else |
| 1147 | EQ->AddReport(R); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1150 | void BugReporter::FlushReport(BugReportEquivClass& EQ) { |
| 1151 | assert(!EQ.Reports.empty()); |
| 1152 | BugReport &R = **EQ.begin(); |
| 1153 | |
| 1154 | // FIXME: Make sure we use the 'R' for the path that was actually used. |
| 1155 | // Probably doesn't make a difference in practice. |
| 1156 | BugType& BT = R.getBugType(); |
| 1157 | |
| 1158 | llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(), |
| 1159 | R.getDescription(), |
| 1160 | BT.getCategory())); |
| 1161 | GeneratePathDiagnostic(*D.get(), EQ); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1162 | |
| 1163 | // Get the meta data. |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1164 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1165 | 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] | 1166 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1167 | // Emit a summary diagnostic to the regular Diagnostics engine. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 1168 | PathDiagnosticClient* PD = getPathDiagnosticClient(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1169 | const SourceRange *Beg = 0, *End = 0; |
| 1170 | R.getRanges(*this, Beg, End); |
| 1171 | Diagnostic& Diag = getDiagnostic(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1172 | FullSourceLoc L(R.getLocation(), getSourceManager()); |
Ted Kremenek | d90e708 | 2009-02-07 22:36:41 +0000 | [diff] [blame] | 1173 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 1174 | R.getDescription().c_str()); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1175 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1176 | switch (End-Beg) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1177 | default: assert(0 && "Don't handle this many ranges yet!"); |
| 1178 | case 0: Diag.Report(L, ErrorDiag); break; |
| 1179 | case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break; |
| 1180 | case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break; |
| 1181 | 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] | 1182 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1183 | |
| 1184 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
| 1185 | if (!PD) |
| 1186 | return; |
| 1187 | |
| 1188 | if (D->empty()) { |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 1189 | PathDiagnosticPiece* piece = |
| 1190 | new PathDiagnosticEventPiece(L, R.getDescription()); |
| 1191 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 1192 | for ( ; Beg != End; ++Beg) piece->addRange(*Beg); |
| 1193 | D->push_back(piece); |
| 1194 | } |
| 1195 | |
| 1196 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 1197 | } |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1198 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1199 | void BugReporter::EmitBasicReport(const char* name, const char* str, |
| 1200 | SourceLocation Loc, |
| 1201 | SourceRange* RBeg, unsigned NumRanges) { |
| 1202 | EmitBasicReport(name, "", str, Loc, RBeg, NumRanges); |
| 1203 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1204 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 1205 | void BugReporter::EmitBasicReport(const char* name, const char* category, |
| 1206 | const char* str, SourceLocation Loc, |
| 1207 | SourceRange* RBeg, unsigned NumRanges) { |
| 1208 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1209 | // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'. |
| 1210 | BugType *BT = new BugType(name, category); |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 1211 | FullSourceLoc L = getContext().getFullLoc(Loc); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1212 | RangedBugReport *R = new DiagBugReport(*BT, str, L); |
| 1213 | for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg); |
| 1214 | EmitReport(R); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 1215 | } |