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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 34 | // Helper routines for walking the ExplodedGraph and fetching statements. |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 102 | // PathDiagnosticBuilder and its associated routines and helper objects. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 3106198 | 2009-03-31 23:00:32 +0000 | [diff] [blame] | 255 | // ScanNotableSymbols: closure-like callback for scanning Store bindings. |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | |
| 258 | static const VarDecl* |
| 259 | GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N, |
| 260 | GRStateManager& VMgr, SVal X) { |
| 261 | |
| 262 | for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) { |
| 263 | |
| 264 | ProgramPoint P = N->getLocation(); |
| 265 | |
| 266 | if (!isa<PostStmt>(P)) |
| 267 | continue; |
| 268 | |
| 269 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt()); |
| 270 | |
| 271 | if (!DR) |
| 272 | continue; |
| 273 | |
| 274 | SVal Y = VMgr.GetSVal(N->getState(), DR); |
| 275 | |
| 276 | if (X != Y) |
| 277 | continue; |
| 278 | |
| 279 | VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 280 | |
| 281 | if (!VD) |
| 282 | continue; |
| 283 | |
| 284 | return VD; |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | namespace { |
| 291 | class VISIBILITY_HIDDEN NotableSymbolHandler |
| 292 | : public StoreManager::BindingsHandler { |
| 293 | |
| 294 | SymbolRef Sym; |
| 295 | const GRState* PrevSt; |
| 296 | const Stmt* S; |
| 297 | GRStateManager& VMgr; |
| 298 | const ExplodedNode<GRState>* Pred; |
| 299 | PathDiagnostic& PD; |
| 300 | BugReporter& BR; |
| 301 | |
| 302 | public: |
| 303 | |
| 304 | NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s, |
| 305 | GRStateManager& vmgr, const ExplodedNode<GRState>* pred, |
| 306 | PathDiagnostic& pd, BugReporter& br) |
| 307 | : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {} |
| 308 | |
| 309 | bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, |
| 310 | SVal V) { |
| 311 | |
| 312 | SymbolRef ScanSym = V.getAsSymbol(); |
| 313 | |
| 314 | if (ScanSym != Sym) |
| 315 | return true; |
| 316 | |
| 317 | // Check if the previous state has this binding. |
| 318 | SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R)); |
| 319 | |
| 320 | if (X == V) // Same binding? |
| 321 | return true; |
| 322 | |
| 323 | // Different binding. Only handle assignments for now. We don't pull |
| 324 | // this check out of the loop because we will eventually handle other |
| 325 | // cases. |
| 326 | |
| 327 | VarDecl *VD = 0; |
| 328 | |
| 329 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
| 330 | if (!B->isAssignmentOp()) |
| 331 | return true; |
| 332 | |
| 333 | // What variable did we assign to? |
| 334 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts()); |
| 335 | |
| 336 | if (!DR) |
| 337 | return true; |
| 338 | |
| 339 | VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 340 | } |
| 341 | else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) { |
| 342 | // FIXME: Eventually CFGs won't have DeclStmts. Right now we |
| 343 | // assume that each DeclStmt has a single Decl. This invariant |
| 344 | // holds by contruction in the CFG. |
| 345 | VD = dyn_cast<VarDecl>(*DS->decl_begin()); |
| 346 | } |
| 347 | |
| 348 | if (!VD) |
| 349 | return true; |
| 350 | |
| 351 | // What is the most recently referenced variable with this binding? |
| 352 | const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V); |
| 353 | |
| 354 | if (!MostRecent) |
| 355 | return true; |
| 356 | |
| 357 | // Create the diagnostic. |
| 358 | FullSourceLoc L(S->getLocStart(), BR.getSourceManager()); |
| 359 | |
| 360 | if (Loc::IsLocType(VD->getType())) { |
| 361 | std::string msg = "'" + std::string(VD->getNameAsString()) + |
| 362 | "' now aliases '" + MostRecent->getNameAsString() + "'"; |
| 363 | |
| 364 | PD.push_front(new PathDiagnosticEventPiece(L, msg)); |
| 365 | } |
| 366 | |
| 367 | return true; |
| 368 | } |
| 369 | }; |
| 370 | } |
| 371 | |
| 372 | static void HandleNotableSymbol(const ExplodedNode<GRState>* N, |
| 373 | const Stmt* S, |
| 374 | SymbolRef Sym, BugReporter& BR, |
| 375 | PathDiagnostic& PD) { |
| 376 | |
| 377 | const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin(); |
| 378 | const GRState* PrevSt = Pred ? Pred->getState() : 0; |
| 379 | |
| 380 | if (!PrevSt) |
| 381 | return; |
| 382 | |
| 383 | // Look at the region bindings of the current state that map to the |
| 384 | // specified symbol. Are any of them not in the previous state? |
| 385 | GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager(); |
| 386 | NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR); |
| 387 | cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H); |
| 388 | } |
| 389 | |
| 390 | namespace { |
| 391 | class VISIBILITY_HIDDEN ScanNotableSymbols |
| 392 | : public StoreManager::BindingsHandler { |
| 393 | |
| 394 | llvm::SmallSet<SymbolRef, 10> AlreadyProcessed; |
| 395 | const ExplodedNode<GRState>* N; |
| 396 | Stmt* S; |
| 397 | GRBugReporter& BR; |
| 398 | PathDiagnostic& PD; |
| 399 | |
| 400 | public: |
| 401 | ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br, |
| 402 | PathDiagnostic& pd) |
| 403 | : N(n), S(s), BR(br), PD(pd) {} |
| 404 | |
| 405 | bool HandleBinding(StoreManager& SMgr, Store store, |
| 406 | const MemRegion* R, SVal V) { |
| 407 | |
| 408 | SymbolRef ScanSym = V.getAsSymbol(); |
| 409 | |
| 410 | if (!ScanSym) |
| 411 | return true; |
| 412 | |
| 413 | if (!BR.isNotable(ScanSym)) |
| 414 | return true; |
| 415 | |
| 416 | if (AlreadyProcessed.count(ScanSym)) |
| 417 | return true; |
| 418 | |
| 419 | AlreadyProcessed.insert(ScanSym); |
| 420 | |
| 421 | HandleNotableSymbol(N, S, ScanSym, BR, PD); |
| 422 | return true; |
| 423 | } |
| 424 | }; |
| 425 | } // end anonymous namespace |
| 426 | |
| 427 | //===----------------------------------------------------------------------===// |
| 428 | // "Minimal" path diagnostic generation algorithm. |
| 429 | //===----------------------------------------------------------------------===// |
| 430 | |
| 431 | static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD, |
| 432 | PathDiagnosticBuilder &PDB, |
| 433 | const ExplodedNode<GRState> *N) { |
| 434 | ASTContext& Ctx = PDB.getContext(); |
| 435 | SourceManager& SMgr = PDB.getSourceManager(); |
| 436 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 437 | ? NULL : *(N->pred_begin()); |
| 438 | while (NextNode) { |
| 439 | N = NextNode; |
| 440 | NextNode = GetPredecessorNode(N); |
| 441 | |
| 442 | ProgramPoint P = N->getLocation(); |
| 443 | |
| 444 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 445 | CFGBlock* Src = BE->getSrc(); |
| 446 | CFGBlock* Dst = BE->getDst(); |
| 447 | Stmt* T = Src->getTerminator(); |
| 448 | |
| 449 | if (!T) |
| 450 | continue; |
| 451 | |
| 452 | FullSourceLoc Start(T->getLocStart(), SMgr); |
| 453 | |
| 454 | switch (T->getStmtClass()) { |
| 455 | default: |
| 456 | break; |
| 457 | |
| 458 | case Stmt::GotoStmtClass: |
| 459 | case Stmt::IndirectGotoStmtClass: { |
| 460 | Stmt* S = GetNextStmt(N); |
| 461 | |
| 462 | if (!S) |
| 463 | continue; |
| 464 | |
| 465 | std::string sbuf; |
| 466 | llvm::raw_string_ostream os(sbuf); |
| 467 | const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); |
| 468 | |
| 469 | os << "Control jumps to line " |
| 470 | << End.asLocation().getInstantiationLineNumber(); |
| 471 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 472 | os.str())); |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | case Stmt::SwitchStmtClass: { |
| 477 | // Figure out what case arm we took. |
| 478 | std::string sbuf; |
| 479 | llvm::raw_string_ostream os(sbuf); |
| 480 | |
| 481 | if (Stmt* S = Dst->getLabel()) { |
| 482 | PathDiagnosticLocation End(S, SMgr); |
| 483 | |
| 484 | switch (S->getStmtClass()) { |
| 485 | default: |
| 486 | os << "No cases match in the switch statement. " |
| 487 | "Control jumps to line " |
| 488 | << End.asLocation().getInstantiationLineNumber(); |
| 489 | break; |
| 490 | case Stmt::DefaultStmtClass: |
| 491 | os << "Control jumps to the 'default' case at line " |
| 492 | << End.asLocation().getInstantiationLineNumber(); |
| 493 | break; |
| 494 | |
| 495 | case Stmt::CaseStmtClass: { |
| 496 | os << "Control jumps to 'case "; |
| 497 | CaseStmt* Case = cast<CaseStmt>(S); |
| 498 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
| 499 | |
| 500 | // Determine if it is an enum. |
| 501 | bool GetRawInt = true; |
| 502 | |
| 503 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 504 | // FIXME: Maybe this should be an assertion. Are there cases |
| 505 | // were it is not an EnumConstantDecl? |
| 506 | EnumConstantDecl* D = |
| 507 | dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 508 | |
| 509 | if (D) { |
| 510 | GetRawInt = false; |
| 511 | os << D->getNameAsString(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | if (GetRawInt) { |
| 516 | |
| 517 | // Not an enum. |
| 518 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 519 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 520 | llvm::APSInt V(bits, false); |
| 521 | |
| 522 | if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) { |
| 523 | assert (false && "Case condition must be constant."); |
| 524 | continue; |
| 525 | } |
| 526 | |
| 527 | os << V; |
| 528 | } |
| 529 | |
| 530 | os << ":' at line " |
| 531 | << End.asLocation().getInstantiationLineNumber(); |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 536 | os.str())); |
| 537 | } |
| 538 | else { |
| 539 | os << "'Default' branch taken. "; |
| 540 | const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N); |
| 541 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 542 | os.str())); |
| 543 | } |
| 544 | |
| 545 | break; |
| 546 | } |
| 547 | |
| 548 | case Stmt::BreakStmtClass: |
| 549 | case Stmt::ContinueStmtClass: { |
| 550 | std::string sbuf; |
| 551 | llvm::raw_string_ostream os(sbuf); |
| 552 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 553 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 554 | os.str())); |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | // Determine control-flow for ternary '?'. |
| 559 | case Stmt::ConditionalOperatorClass: { |
| 560 | std::string sbuf; |
| 561 | llvm::raw_string_ostream os(sbuf); |
| 562 | os << "'?' condition is "; |
| 563 | |
| 564 | if (*(Src->succ_begin()+1) == Dst) |
| 565 | os << "false"; |
| 566 | else |
| 567 | os << "true"; |
| 568 | |
| 569 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 570 | |
| 571 | if (const Stmt *S = End.asStmt()) |
| 572 | End = PDB.getEnclosingStmtLocation(S); |
| 573 | |
| 574 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 575 | os.str())); |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | // Determine control-flow for short-circuited '&&' and '||'. |
| 580 | case Stmt::BinaryOperatorClass: { |
| 581 | if (!PDB.supportsLogicalOpControlFlow()) |
| 582 | break; |
| 583 | |
| 584 | BinaryOperator *B = cast<BinaryOperator>(T); |
| 585 | std::string sbuf; |
| 586 | llvm::raw_string_ostream os(sbuf); |
| 587 | os << "Left side of '"; |
| 588 | |
| 589 | if (B->getOpcode() == BinaryOperator::LAnd) { |
| 590 | os << "&&" << "' is "; |
| 591 | |
| 592 | if (*(Src->succ_begin()+1) == Dst) { |
| 593 | os << "false"; |
| 594 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 595 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 596 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 597 | os.str())); |
| 598 | } |
| 599 | else { |
| 600 | os << "true"; |
| 601 | PathDiagnosticLocation Start(B->getLHS(), SMgr); |
| 602 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 603 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 604 | os.str())); |
| 605 | } |
| 606 | } |
| 607 | else { |
| 608 | assert(B->getOpcode() == BinaryOperator::LOr); |
| 609 | os << "||" << "' is "; |
| 610 | |
| 611 | if (*(Src->succ_begin()+1) == Dst) { |
| 612 | os << "false"; |
| 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 | else { |
| 619 | os << "true"; |
| 620 | PathDiagnosticLocation End(B->getLHS(), SMgr); |
| 621 | PathDiagnosticLocation Start(B->getOperatorLoc(), SMgr); |
| 622 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 623 | os.str())); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | break; |
| 628 | } |
| 629 | |
| 630 | case Stmt::DoStmtClass: { |
| 631 | if (*(Src->succ_begin()) == Dst) { |
| 632 | std::string sbuf; |
| 633 | llvm::raw_string_ostream os(sbuf); |
| 634 | |
| 635 | os << "Loop condition is true. "; |
| 636 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 637 | |
| 638 | if (const Stmt *S = End.asStmt()) |
| 639 | End = PDB.getEnclosingStmtLocation(S); |
| 640 | |
| 641 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 642 | os.str())); |
| 643 | } |
| 644 | else { |
| 645 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 646 | |
| 647 | if (const Stmt *S = End.asStmt()) |
| 648 | End = PDB.getEnclosingStmtLocation(S); |
| 649 | |
| 650 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 651 | "Loop condition is false. Exiting loop")); |
| 652 | } |
| 653 | |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | case Stmt::WhileStmtClass: |
| 658 | case Stmt::ForStmtClass: { |
| 659 | if (*(Src->succ_begin()+1) == Dst) { |
| 660 | std::string sbuf; |
| 661 | llvm::raw_string_ostream os(sbuf); |
| 662 | |
| 663 | os << "Loop condition is false. "; |
| 664 | PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); |
| 665 | if (const Stmt *S = End.asStmt()) |
| 666 | End = PDB.getEnclosingStmtLocation(S); |
| 667 | |
| 668 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 669 | os.str())); |
| 670 | } |
| 671 | else { |
| 672 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 673 | if (const Stmt *S = End.asStmt()) |
| 674 | End = PDB.getEnclosingStmtLocation(S); |
| 675 | |
| 676 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 677 | "Loop condition is true. Entering loop body")); |
| 678 | } |
| 679 | |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | case Stmt::IfStmtClass: { |
| 684 | PathDiagnosticLocation End = PDB.ExecutionContinues(N); |
| 685 | |
| 686 | if (const Stmt *S = End.asStmt()) |
| 687 | End = PDB.getEnclosingStmtLocation(S); |
| 688 | |
| 689 | if (*(Src->succ_begin()+1) == Dst) |
| 690 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 691 | "Taking false branch")); |
| 692 | else |
| 693 | PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, |
| 694 | "Taking true branch")); |
| 695 | |
| 696 | break; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if (PathDiagnosticPiece* p = |
| 702 | PDB.getReport().VisitNode(N, NextNode, PDB.getGraph(), |
| 703 | PDB.getBugReporter(), |
| 704 | PDB.getNodeMapClosure())) { |
| 705 | PD.push_front(p); |
| 706 | } |
| 707 | |
| 708 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 709 | // Scan the region bindings, and see if a "notable" symbol has a new |
| 710 | // lval binding. |
| 711 | ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD); |
| 712 | PDB.getStateManager().iterBindings(N->getState(), SNS); |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 718 | // Methods for BugType and subclasses. |
| 719 | //===----------------------------------------------------------------------===// |
| 720 | BugType::~BugType() {} |
| 721 | void BugType::FlushReports(BugReporter &BR) {} |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 722 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 723 | //===----------------------------------------------------------------------===// |
| 724 | // Methods for BugReport and subclasses. |
| 725 | //===----------------------------------------------------------------------===// |
| 726 | BugReport::~BugReport() {} |
| 727 | RangedBugReport::~RangedBugReport() {} |
| 728 | |
| 729 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 730 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 731 | Stmt *S = NULL; |
| 732 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 733 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 734 | if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 735 | } |
| 736 | if (!S) S = GetStmt(ProgP); |
| 737 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 738 | return S; |
| 739 | } |
| 740 | |
| 741 | PathDiagnosticPiece* |
| 742 | BugReport::getEndPath(BugReporter& BR, |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 743 | const ExplodedNode<GRState>* EndPathNode) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 744 | |
| 745 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 746 | |
| 747 | if (!S) |
| 748 | return NULL; |
| 749 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 750 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | 1fbfd5b | 2009-03-06 23:58:11 +0000 | [diff] [blame] | 751 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 752 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 753 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 754 | getRanges(BR, Beg, End); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 755 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 756 | for (; Beg != End; ++Beg) |
| 757 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 758 | |
| 759 | return P; |
| 760 | } |
| 761 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 762 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 763 | const SourceRange*& end) { |
| 764 | |
| 765 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 766 | R = E->getSourceRange(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 767 | assert(R.isValid()); |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 768 | beg = &R; |
| 769 | end = beg+1; |
| 770 | } |
| 771 | else |
| 772 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 775 | SourceLocation BugReport::getLocation() const { |
| 776 | if (EndNode) |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 777 | if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) { |
| 778 | // For member expressions, return the location of the '.' or '->'. |
| 779 | if (MemberExpr* ME = dyn_cast<MemberExpr>(S)) |
| 780 | return ME->getMemberLoc(); |
| 781 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 782 | return S->getLocStart(); |
Ted Kremenek | 9b5e505 | 2009-02-27 20:05:10 +0000 | [diff] [blame] | 783 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 784 | |
| 785 | return FullSourceLoc(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 788 | PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N, |
| 789 | const ExplodedNode<GRState>* PrevN, |
| 790 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 791 | BugReporter& BR, |
| 792 | NodeResolver &NR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
| 795 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 796 | //===----------------------------------------------------------------------===// |
| 797 | // Methods for BugReporter and subclasses. |
| 798 | //===----------------------------------------------------------------------===// |
| 799 | |
| 800 | BugReportEquivClass::~BugReportEquivClass() { |
| 801 | for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; |
| 802 | } |
| 803 | |
| 804 | GRBugReporter::~GRBugReporter() { FlushReports(); } |
| 805 | BugReporterData::~BugReporterData() {} |
| 806 | |
| 807 | ExplodedGraph<GRState>& |
| 808 | GRBugReporter::getGraph() { return Eng.getGraph(); } |
| 809 | |
| 810 | GRStateManager& |
| 811 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
| 812 | |
| 813 | BugReporter::~BugReporter() { FlushReports(); } |
| 814 | |
| 815 | void BugReporter::FlushReports() { |
| 816 | if (BugTypes.isEmpty()) |
| 817 | return; |
| 818 | |
| 819 | // First flush the warnings for each BugType. This may end up creating new |
| 820 | // warnings and new BugTypes. Because ImmutableSet is a functional data |
| 821 | // structure, we do not need to worry about the iterators being invalidated. |
| 822 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 823 | const_cast<BugType*>(*I)->FlushReports(*this); |
| 824 | |
| 825 | // Iterate through BugTypes a second time. BugTypes may have been updated |
| 826 | // with new BugType objects and new warnings. |
| 827 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) { |
| 828 | BugType *BT = const_cast<BugType*>(*I); |
| 829 | |
| 830 | typedef llvm::FoldingSet<BugReportEquivClass> SetTy; |
| 831 | SetTy& EQClasses = BT->EQClasses; |
| 832 | |
| 833 | for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ |
| 834 | BugReportEquivClass& EQ = *EI; |
| 835 | FlushReport(EQ); |
| 836 | } |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 837 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 838 | // Delete the BugType object. This will also delete the equivalence |
| 839 | // classes. |
| 840 | delete BT; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 841 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 842 | |
| 843 | // Remove all references to the BugType objects. |
| 844 | BugTypes = F.GetEmptySet(); |
| 845 | } |
| 846 | |
| 847 | //===----------------------------------------------------------------------===// |
| 848 | // PathDiagnostics generation. |
| 849 | //===----------------------------------------------------------------------===// |
| 850 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 851 | static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 852 | std::pair<ExplodedNode<GRState>*, unsigned> > |
| 853 | MakeReportGraph(const ExplodedGraph<GRState>* G, |
| 854 | const ExplodedNode<GRState>** NStart, |
| 855 | const ExplodedNode<GRState>** NEnd) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 856 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 857 | // Create the trimmed graph. It will contain the shortest paths from the |
| 858 | // error nodes to the root. In the new graph we should only have one |
| 859 | // error node unless there are two or more error nodes with the same minimum |
| 860 | // path length. |
| 861 | ExplodedGraph<GRState>* GTrim; |
| 862 | InterExplodedGraphMap<GRState>* NMap; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 863 | |
| 864 | llvm::DenseMap<const void*, const void*> InverseMap; |
| 865 | llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 866 | |
| 867 | // Create owning pointers for GTrim and NMap just to ensure that they are |
| 868 | // released when this function exists. |
| 869 | llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim); |
| 870 | llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap); |
| 871 | |
| 872 | // Find the (first) error node in the trimmed graph. We just need to consult |
| 873 | // the node map (NMap) which maps from nodes in the original graph to nodes |
| 874 | // in the new graph. |
| 875 | const ExplodedNode<GRState>* N = 0; |
| 876 | unsigned NodeIndex = 0; |
| 877 | |
| 878 | for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I) |
| 879 | if ((N = NMap->getMappedNode(*I))) { |
| 880 | NodeIndex = (I - NStart) / sizeof(*I); |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | assert(N && "No error node found in the trimmed graph."); |
| 885 | |
| 886 | // Create a new (third!) graph with a single path. This is the graph |
| 887 | // that will be returned to the caller. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 888 | ExplodedGraph<GRState> *GNew = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 889 | new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 890 | GTrim->getContext()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 891 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 892 | // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 893 | // to the root node, and then construct a new graph that contains only |
| 894 | // a single path. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 895 | llvm::DenseMap<const void*,unsigned> Visited; |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 896 | std::queue<const ExplodedNode<GRState>*> WS; |
| 897 | WS.push(N); |
| 898 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 899 | unsigned cnt = 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 900 | const ExplodedNode<GRState>* Root = 0; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 901 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 902 | while (!WS.empty()) { |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 903 | const ExplodedNode<GRState>* Node = WS.front(); |
| 904 | WS.pop(); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 905 | |
| 906 | if (Visited.find(Node) != Visited.end()) |
| 907 | continue; |
| 908 | |
| 909 | Visited[Node] = cnt++; |
| 910 | |
| 911 | if (Node->pred_empty()) { |
| 912 | Root = Node; |
| 913 | break; |
| 914 | } |
| 915 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 916 | for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(), |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 917 | E=Node->pred_end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 918 | WS.push(*I); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 919 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 920 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 921 | assert (Root); |
| 922 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 923 | // 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] | 924 | // with the lowest number. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 925 | ExplodedNode<GRState> *Last = 0, *First = 0; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 926 | NodeBackMap *BM = new NodeBackMap(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 927 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 928 | for ( N = Root ;;) { |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 929 | // Lookup the number associated with the current node. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 930 | llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 931 | assert (I != Visited.end()); |
| 932 | |
| 933 | // Create the equivalent node in the new graph with the same state |
| 934 | // and location. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 935 | ExplodedNode<GRState>* NewN = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 936 | GNew->getNode(N->getLocation(), N->getState()); |
| 937 | |
| 938 | // Store the mapping to the original node. |
| 939 | llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N); |
| 940 | assert(IMitr != InverseMap.end() && "No mapping to original node."); |
| 941 | (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 942 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 943 | // Link up the new node with the previous node. |
| 944 | if (Last) |
| 945 | NewN->addPredecessor(Last); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 946 | |
| 947 | Last = NewN; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 948 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 949 | // Are we at the final node? |
| 950 | if (I->second == 0) { |
| 951 | First = NewN; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 952 | break; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 953 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 954 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 955 | // Find the next successor node. We choose the node that is marked |
| 956 | // with the lowest DFS number. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 957 | ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin(); |
| 958 | ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end(); |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 959 | N = 0; |
| 960 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 961 | for (unsigned MinVal = 0; SI != SE; ++SI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 962 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 963 | I = Visited.find(*SI); |
| 964 | |
| 965 | if (I == Visited.end()) |
| 966 | continue; |
| 967 | |
| 968 | if (!N || I->second < MinVal) { |
| 969 | N = *SI; |
| 970 | MinVal = I->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 971 | } |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 972 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 973 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 974 | assert (N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 975 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 976 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 977 | assert (First); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 978 | return std::make_pair(std::make_pair(GNew, BM), |
| 979 | std::make_pair(First, NodeIndex)); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Ted Kremenek | 0e5c8d4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 982 | /// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object |
| 983 | /// and collapses PathDiagosticPieces that are expanded by macros. |
| 984 | static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) { |
| 985 | typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> > |
| 986 | MacroStackTy; |
| 987 | |
| 988 | typedef std::vector<PathDiagnosticPiece*> |
| 989 | PiecesTy; |
| 990 | |
| 991 | MacroStackTy MacroStack; |
| 992 | PiecesTy Pieces; |
| 993 | |
| 994 | for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) { |
| 995 | // Get the location of the PathDiagnosticPiece. |
| 996 | const FullSourceLoc Loc = I->getLocation(); |
| 997 | |
| 998 | // Determine the instantiation location, which is the location we group |
| 999 | // related PathDiagnosticPieces. |
| 1000 | SourceLocation InstantiationLoc = Loc.isMacroID() ? |
| 1001 | SM.getInstantiationLoc(Loc) : |
| 1002 | SourceLocation(); |
| 1003 | |
| 1004 | if (Loc.isFileID()) { |
| 1005 | MacroStack.clear(); |
| 1006 | Pieces.push_back(&*I); |
| 1007 | continue; |
| 1008 | } |
| 1009 | |
| 1010 | assert(Loc.isMacroID()); |
| 1011 | |
| 1012 | // Is the PathDiagnosticPiece within the same macro group? |
| 1013 | if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { |
| 1014 | MacroStack.back().first->push_back(&*I); |
| 1015 | continue; |
| 1016 | } |
| 1017 | |
| 1018 | // We aren't in the same group. Are we descending into a new macro |
| 1019 | // or are part of an old one? |
| 1020 | PathDiagnosticMacroPiece *MacroGroup = 0; |
| 1021 | |
| 1022 | SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? |
| 1023 | SM.getInstantiationLoc(Loc) : |
| 1024 | SourceLocation(); |
| 1025 | |
| 1026 | // Walk the entire macro stack. |
| 1027 | while (!MacroStack.empty()) { |
| 1028 | if (InstantiationLoc == MacroStack.back().second) { |
| 1029 | MacroGroup = MacroStack.back().first; |
| 1030 | break; |
| 1031 | } |
| 1032 | |
| 1033 | if (ParentInstantiationLoc == MacroStack.back().second) { |
| 1034 | MacroGroup = MacroStack.back().first; |
| 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | MacroStack.pop_back(); |
| 1039 | } |
| 1040 | |
| 1041 | if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { |
| 1042 | // Create a new macro group and add it to the stack. |
| 1043 | PathDiagnosticMacroPiece *NewGroup = new PathDiagnosticMacroPiece(Loc); |
| 1044 | |
| 1045 | if (MacroGroup) |
| 1046 | MacroGroup->push_back(NewGroup); |
| 1047 | else { |
| 1048 | assert(InstantiationLoc.isFileID()); |
| 1049 | Pieces.push_back(NewGroup); |
| 1050 | } |
| 1051 | |
| 1052 | MacroGroup = NewGroup; |
| 1053 | MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); |
| 1054 | } |
| 1055 | |
| 1056 | // Finally, add the PathDiagnosticPiece to the group. |
| 1057 | MacroGroup->push_back(&*I); |
| 1058 | } |
| 1059 | |
| 1060 | // Now take the pieces and construct a new PathDiagnostic. |
| 1061 | PD.resetPath(false); |
| 1062 | |
| 1063 | for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) { |
| 1064 | if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I)) |
| 1065 | if (!MP->containsEvent()) { |
| 1066 | delete MP; |
| 1067 | continue; |
| 1068 | } |
| 1069 | |
| 1070 | PD.push_back(*I); |
| 1071 | } |
| 1072 | } |
| 1073 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1074 | void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
| 1075 | BugReportEquivClass& EQ) { |
| 1076 | |
| 1077 | std::vector<const ExplodedNode<GRState>*> Nodes; |
| 1078 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1079 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { |
| 1080 | const ExplodedNode<GRState>* N = I->getEndNode(); |
| 1081 | if (N) Nodes.push_back(N); |
| 1082 | } |
| 1083 | |
| 1084 | if (Nodes.empty()) |
| 1085 | return; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1086 | |
| 1087 | // 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] | 1088 | // node to a root. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1089 | const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1090 | std::pair<ExplodedNode<GRState>*, unsigned> >& |
| 1091 | GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size()); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1092 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1093 | // Find the BugReport with the original location. |
| 1094 | BugReport *R = 0; |
| 1095 | unsigned i = 0; |
| 1096 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i) |
| 1097 | if (i == GPair.second.second) { R = *I; break; } |
| 1098 | |
| 1099 | assert(R && "No original report found for sliced graph."); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 1100 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1101 | llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first); |
| 1102 | llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1103 | const ExplodedNode<GRState> *N = GPair.second.first; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1104 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1105 | // Start building the path diagnostic... |
| 1106 | if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N)) |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1107 | PD.push_back(Piece); |
| 1108 | else |
| 1109 | return; |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1110 | |
| 1111 | PathDiagnosticBuilder PDB(*this, ReportGraph.get(), R, BackMap.get(), |
| 1112 | getStateManager().getCodeDecl(), |
Ted Kremenek | babdd7b | 2009-03-27 05:06:10 +0000 | [diff] [blame] | 1113 | getPathDiagnosticClient()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 1114 | |
Ted Kremenek | 7dc8664 | 2009-03-31 20:22:36 +0000 | [diff] [blame] | 1115 | switch (PDB.getGenerationScheme()) { |
| 1116 | case PathDiagnosticClient::Extensive: |
| 1117 | case PathDiagnosticClient::Minimal: |
| 1118 | GenerateMinimalPathDiagnostic(PD, PDB, N); |
| 1119 | break; |
| 1120 | } |
| 1121 | |
| 1122 | // After constructing the full PathDiagnostic, do a pass over it to compact |
| 1123 | // PathDiagnosticPieces that occur within a macro. |
| 1124 | CompactPathDiagnostic(PD, PDB.getSourceManager()); |
| 1125 | } |
| 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 | } |