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