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" |
| 22 | #include "clang/Analysis/ProgramPoint.h" |
| 23 | #include "clang/Analysis/PathDiagnostic.h" |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // static functions. |
| 32 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 34 | static inline Stmt* GetStmt(ProgramPoint P) { |
| 35 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 36 | return PS->getStmt(); |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 37 | else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 38 | return BE->getSrc()->getTerminator(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 39 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 40 | return 0; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 43 | static inline const ExplodedNode<GRState>* |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 44 | GetPredecessorNode(const ExplodedNode<GRState>* N) { |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 45 | return N->pred_empty() ? NULL : *(N->pred_begin()); |
| 46 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 47 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 48 | static inline const ExplodedNode<GRState>* |
| 49 | GetSuccessorNode(const ExplodedNode<GRState>* N) { |
| 50 | return N->succ_empty() ? NULL : *(N->succ_begin()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 53 | static Stmt* GetPreviousStmt(const ExplodedNode<GRState>* N) { |
| 54 | for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N)) |
| 55 | if (Stmt *S = GetStmt(N->getLocation())) |
| 56 | return S; |
| 57 | |
| 58 | return 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 61 | static Stmt* GetNextStmt(const ExplodedNode<GRState>* N) { |
| 62 | for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N)) |
| 63 | if (Stmt *S = GetStmt(N->getLocation())) |
| 64 | return S; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static inline Stmt* GetCurrentOrPreviousStmt(const ExplodedNode<GRState>* N) { |
| 70 | if (Stmt *S = GetStmt(N->getLocation())) |
| 71 | return S; |
| 72 | |
| 73 | return GetPreviousStmt(N); |
| 74 | } |
| 75 | |
| 76 | static inline Stmt* GetCurrentOrNextStmt(const ExplodedNode<GRState>* N) { |
| 77 | if (Stmt *S = GetStmt(N->getLocation())) |
| 78 | return S; |
| 79 | |
| 80 | return GetNextStmt(N); |
| 81 | } |
| 82 | |
| 83 | //===----------------------------------------------------------------------===// |
| 84 | // Diagnostics for 'execution continues on line XXX'. |
| 85 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 86 | |
| 87 | static inline void ExecutionContinues(llvm::raw_string_ostream& os, |
| 88 | SourceManager& SMgr, |
| 89 | const ExplodedNode<GRState>* N, |
| 90 | const Decl& D) { |
| 91 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 92 | // Slow, but probably doesn't matter. |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 93 | if (os.str().empty()) |
| 94 | os << ' '; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 96 | if (Stmt *S = GetNextStmt(N)) |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 97 | os << "Execution continues on line " |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 98 | << SMgr.getInstantiationLineNumber(S->getLocStart()) << '.'; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 99 | else |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 100 | os << "Execution jumps to the end of the " |
| 101 | << (isa<ObjCMethodDecl>(D) ? "method" : "function") << '.'; |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 104 | //===----------------------------------------------------------------------===// |
| 105 | // Methods for BugType and subclasses. |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | BugType::~BugType() {} |
| 108 | void BugType::FlushReports(BugReporter &BR) {} |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 110 | //===----------------------------------------------------------------------===// |
| 111 | // Methods for BugReport and subclasses. |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | BugReport::~BugReport() {} |
| 114 | RangedBugReport::~RangedBugReport() {} |
| 115 | |
| 116 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 117 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 118 | Stmt *S = NULL; |
| 119 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 120 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) { |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 121 | if (BE->getBlock() == &BR.getCFG()->getExit()) S = GetPreviousStmt(EndNode); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 122 | } |
| 123 | if (!S) S = GetStmt(ProgP); |
| 124 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 125 | return S; |
| 126 | } |
| 127 | |
| 128 | PathDiagnosticPiece* |
| 129 | BugReport::getEndPath(BugReporter& BR, |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 130 | const ExplodedNode<GRState>* EndPathNode) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 131 | |
| 132 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 133 | |
| 134 | if (!S) |
| 135 | return NULL; |
| 136 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 137 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
| 138 | PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 140 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 141 | getRanges(BR, Beg, End); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 143 | for (; Beg != End; ++Beg) |
| 144 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 145 | |
| 146 | return P; |
| 147 | } |
| 148 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 149 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 150 | const SourceRange*& end) { |
| 151 | |
| 152 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 153 | R = E->getSourceRange(); |
| 154 | beg = &R; |
| 155 | end = beg+1; |
| 156 | } |
| 157 | else |
| 158 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 161 | SourceLocation BugReport::getLocation() const { |
| 162 | if (EndNode) |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 163 | if (Stmt* S = GetCurrentOrPreviousStmt(EndNode)) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 164 | return S->getLocStart(); |
| 165 | |
| 166 | return FullSourceLoc(); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 169 | PathDiagnosticPiece* BugReport::VisitNode(const ExplodedNode<GRState>* N, |
| 170 | const ExplodedNode<GRState>* PrevN, |
| 171 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 172 | BugReporter& BR, |
| 173 | NodeResolver &NR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 174 | return NULL; |
| 175 | } |
| 176 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 177 | //===----------------------------------------------------------------------===// |
| 178 | // Methods for BugReporter and subclasses. |
| 179 | //===----------------------------------------------------------------------===// |
| 180 | |
| 181 | BugReportEquivClass::~BugReportEquivClass() { |
| 182 | for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; |
| 183 | } |
| 184 | |
| 185 | GRBugReporter::~GRBugReporter() { FlushReports(); } |
| 186 | BugReporterData::~BugReporterData() {} |
| 187 | |
| 188 | ExplodedGraph<GRState>& |
| 189 | GRBugReporter::getGraph() { return Eng.getGraph(); } |
| 190 | |
| 191 | GRStateManager& |
| 192 | GRBugReporter::getStateManager() { return Eng.getStateManager(); } |
| 193 | |
| 194 | BugReporter::~BugReporter() { FlushReports(); } |
| 195 | |
| 196 | void BugReporter::FlushReports() { |
| 197 | if (BugTypes.isEmpty()) |
| 198 | return; |
| 199 | |
| 200 | // First flush the warnings for each BugType. This may end up creating new |
| 201 | // warnings and new BugTypes. Because ImmutableSet is a functional data |
| 202 | // structure, we do not need to worry about the iterators being invalidated. |
| 203 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) |
| 204 | const_cast<BugType*>(*I)->FlushReports(*this); |
| 205 | |
| 206 | // Iterate through BugTypes a second time. BugTypes may have been updated |
| 207 | // with new BugType objects and new warnings. |
| 208 | for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) { |
| 209 | BugType *BT = const_cast<BugType*>(*I); |
| 210 | |
| 211 | typedef llvm::FoldingSet<BugReportEquivClass> SetTy; |
| 212 | SetTy& EQClasses = BT->EQClasses; |
| 213 | |
| 214 | for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ |
| 215 | BugReportEquivClass& EQ = *EI; |
| 216 | FlushReport(EQ); |
| 217 | } |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 219 | // Delete the BugType object. This will also delete the equivalence |
| 220 | // classes. |
| 221 | delete BT; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 222 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 223 | |
| 224 | // Remove all references to the BugType objects. |
| 225 | BugTypes = F.GetEmptySet(); |
| 226 | } |
| 227 | |
| 228 | //===----------------------------------------------------------------------===// |
| 229 | // PathDiagnostics generation. |
| 230 | //===----------------------------------------------------------------------===// |
| 231 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 232 | typedef llvm::DenseMap<const ExplodedNode<GRState>*, |
| 233 | const ExplodedNode<GRState>*> NodeBackMap; |
| 234 | |
| 235 | static std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 236 | std::pair<ExplodedNode<GRState>*, unsigned> > |
| 237 | MakeReportGraph(const ExplodedGraph<GRState>* G, |
| 238 | const ExplodedNode<GRState>** NStart, |
| 239 | const ExplodedNode<GRState>** NEnd) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 240 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 241 | // Create the trimmed graph. It will contain the shortest paths from the |
| 242 | // error nodes to the root. In the new graph we should only have one |
| 243 | // error node unless there are two or more error nodes with the same minimum |
| 244 | // path length. |
| 245 | ExplodedGraph<GRState>* GTrim; |
| 246 | InterExplodedGraphMap<GRState>* NMap; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 247 | |
| 248 | llvm::DenseMap<const void*, const void*> InverseMap; |
| 249 | llvm::tie(GTrim, NMap) = G->Trim(NStart, NEnd, &InverseMap); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 250 | |
| 251 | // Create owning pointers for GTrim and NMap just to ensure that they are |
| 252 | // released when this function exists. |
| 253 | llvm::OwningPtr<ExplodedGraph<GRState> > AutoReleaseGTrim(GTrim); |
| 254 | llvm::OwningPtr<InterExplodedGraphMap<GRState> > AutoReleaseNMap(NMap); |
| 255 | |
| 256 | // Find the (first) error node in the trimmed graph. We just need to consult |
| 257 | // the node map (NMap) which maps from nodes in the original graph to nodes |
| 258 | // in the new graph. |
| 259 | const ExplodedNode<GRState>* N = 0; |
| 260 | unsigned NodeIndex = 0; |
| 261 | |
| 262 | for (const ExplodedNode<GRState>** I = NStart; I != NEnd; ++I) |
| 263 | if ((N = NMap->getMappedNode(*I))) { |
| 264 | NodeIndex = (I - NStart) / sizeof(*I); |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | assert(N && "No error node found in the trimmed graph."); |
| 269 | |
| 270 | // Create a new (third!) graph with a single path. This is the graph |
| 271 | // that will be returned to the caller. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 272 | ExplodedGraph<GRState> *GNew = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 273 | new ExplodedGraph<GRState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 274 | GTrim->getContext()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 275 | |
| 276 | // Sometimes the trimmed graph can contain a cycle. Perform a reverse DFS |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 277 | // to the root node, and then construct a new graph that contains only |
| 278 | // a single path. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 279 | llvm::DenseMap<const void*,unsigned> Visited; |
| 280 | llvm::SmallVector<const ExplodedNode<GRState>*, 10> WS; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 281 | WS.push_back(N); |
| 282 | unsigned cnt = 0; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 283 | const ExplodedNode<GRState>* Root = 0; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 285 | while (!WS.empty()) { |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 286 | const ExplodedNode<GRState>* Node = WS.back(); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 287 | WS.pop_back(); |
| 288 | |
| 289 | if (Visited.find(Node) != Visited.end()) |
| 290 | continue; |
| 291 | |
| 292 | Visited[Node] = cnt++; |
| 293 | |
| 294 | if (Node->pred_empty()) { |
| 295 | Root = Node; |
| 296 | break; |
| 297 | } |
| 298 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 299 | for (ExplodedNode<GRState>::const_pred_iterator I=Node->pred_begin(), |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 300 | E=Node->pred_end(); I!=E; ++I) |
| 301 | WS.push_back(*I); |
| 302 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 304 | assert (Root); |
| 305 | |
| 306 | // Now walk from the root down the DFS path, always taking the successor |
| 307 | // with the lowest number. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 308 | ExplodedNode<GRState> *Last = 0, *First = 0; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 309 | NodeBackMap *BM = new NodeBackMap(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 311 | for ( N = Root ;;) { |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 312 | // Lookup the number associated with the current node. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 313 | llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N); |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 314 | assert (I != Visited.end()); |
| 315 | |
| 316 | // Create the equivalent node in the new graph with the same state |
| 317 | // and location. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 318 | ExplodedNode<GRState>* NewN = |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 319 | GNew->getNode(N->getLocation(), N->getState()); |
| 320 | |
| 321 | // Store the mapping to the original node. |
| 322 | llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N); |
| 323 | assert(IMitr != InverseMap.end() && "No mapping to original node."); |
| 324 | (*BM)[NewN] = (const ExplodedNode<GRState>*) IMitr->second; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 326 | // Link up the new node with the previous node. |
| 327 | if (Last) |
| 328 | NewN->addPredecessor(Last); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 329 | |
| 330 | Last = NewN; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 331 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 332 | // Are we at the final node? |
| 333 | if (I->second == 0) { |
| 334 | First = NewN; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 335 | break; |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 336 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 337 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 338 | // Find the next successor node. We choose the node that is marked |
| 339 | // with the lowest DFS number. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 340 | ExplodedNode<GRState>::const_succ_iterator SI = N->succ_begin(); |
| 341 | ExplodedNode<GRState>::const_succ_iterator SE = N->succ_end(); |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 342 | N = 0; |
| 343 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 344 | for (unsigned MinVal = 0; SI != SE; ++SI) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 346 | I = Visited.find(*SI); |
| 347 | |
| 348 | if (I == Visited.end()) |
| 349 | continue; |
| 350 | |
| 351 | if (!N || I->second < MinVal) { |
| 352 | N = *SI; |
| 353 | MinVal = I->second; |
Ted Kremenek | c1da441 | 2008-06-17 19:14:06 +0000 | [diff] [blame] | 354 | } |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 355 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 356 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 357 | assert (N); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 358 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 331b0ac | 2008-06-18 05:34:07 +0000 | [diff] [blame] | 360 | assert (First); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 361 | return std::make_pair(std::make_pair(GNew, BM), |
| 362 | std::make_pair(First, NodeIndex)); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 365 | static const VarDecl* |
| 366 | GetMostRecentVarDeclBinding(const ExplodedNode<GRState>* N, |
| 367 | GRStateManager& VMgr, SVal X) { |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 368 | |
| 369 | for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) { |
| 370 | |
| 371 | ProgramPoint P = N->getLocation(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 373 | if (!isa<PostStmt>(P)) |
| 374 | continue; |
| 375 | |
| 376 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt()); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 377 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 378 | if (!DR) |
| 379 | continue; |
| 380 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 381 | SVal Y = VMgr.GetSVal(N->getState(), DR); |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 382 | |
| 383 | if (X != Y) |
| 384 | continue; |
| 385 | |
| 386 | VarDecl* VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 387 | |
| 388 | if (!VD) |
| 389 | continue; |
| 390 | |
| 391 | return VD; |
| 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 397 | namespace { |
| 398 | class VISIBILITY_HIDDEN NotableSymbolHandler |
| 399 | : public StoreManager::BindingsHandler { |
| 400 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 401 | SymbolRef Sym; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 402 | const GRState* PrevSt; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 403 | const Stmt* S; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 404 | GRStateManager& VMgr; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 405 | const ExplodedNode<GRState>* Pred; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 406 | PathDiagnostic& PD; |
| 407 | BugReporter& BR; |
| 408 | |
| 409 | public: |
| 410 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 411 | NotableSymbolHandler(SymbolRef sym, const GRState* prevst, const Stmt* s, |
| 412 | GRStateManager& vmgr, const ExplodedNode<GRState>* pred, |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 413 | PathDiagnostic& pd, BugReporter& br) |
| 414 | : Sym(sym), PrevSt(prevst), S(s), VMgr(vmgr), Pred(pred), PD(pd), BR(br) {} |
| 415 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 416 | bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 418 | SymbolRef ScanSym; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 419 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 420 | if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 421 | ScanSym = SV->getSymbol(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 422 | else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V)) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 423 | ScanSym = SV->getSymbol(); |
| 424 | else |
| 425 | return true; |
| 426 | |
| 427 | if (ScanSym != Sym) |
| 428 | return true; |
| 429 | |
| 430 | // Check if the previous state has this binding. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 431 | SVal X = VMgr.GetSVal(PrevSt, loc::MemRegionVal(R)); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 432 | |
| 433 | if (X == V) // Same binding? |
| 434 | return true; |
| 435 | |
| 436 | // Different binding. Only handle assignments for now. We don't pull |
| 437 | // this check out of the loop because we will eventually handle other |
| 438 | // cases. |
| 439 | |
| 440 | VarDecl *VD = 0; |
| 441 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 442 | if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 443 | if (!B->isAssignmentOp()) |
| 444 | return true; |
| 445 | |
| 446 | // What variable did we assign to? |
| 447 | DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts()); |
| 448 | |
| 449 | if (!DR) |
| 450 | return true; |
| 451 | |
| 452 | VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 453 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 454 | else if (const DeclStmt* DS = dyn_cast<DeclStmt>(S)) { |
Ted Kremenek | f21a4b4 | 2008-10-06 18:37:46 +0000 | [diff] [blame] | 455 | // FIXME: Eventually CFGs won't have DeclStmts. Right now we |
| 456 | // assume that each DeclStmt has a single Decl. This invariant |
| 457 | // holds by contruction in the CFG. |
| 458 | VD = dyn_cast<VarDecl>(*DS->decl_begin()); |
| 459 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 460 | |
| 461 | if (!VD) |
| 462 | return true; |
| 463 | |
| 464 | // What is the most recently referenced variable with this binding? |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 465 | const VarDecl* MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 466 | |
| 467 | if (!MostRecent) |
| 468 | return true; |
| 469 | |
| 470 | // Create the diagnostic. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 471 | FullSourceLoc L(S->getLocStart(), BR.getSourceManager()); |
| 472 | |
Ted Kremenek | 3daea0a | 2009-02-26 20:29:19 +0000 | [diff] [blame^] | 473 | if (Loc::IsLocType(VD->getType())) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 474 | std::string msg = "'" + std::string(VD->getNameAsString()) + |
| 475 | "' now aliases '" + MostRecent->getNameAsString() + "'"; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 476 | |
| 477 | PD.push_front(new PathDiagnosticPiece(L, msg)); |
| 478 | } |
| 479 | |
| 480 | return true; |
| 481 | } |
| 482 | }; |
| 483 | } |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 484 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 485 | static void HandleNotableSymbol(const ExplodedNode<GRState>* N, |
| 486 | const Stmt* S, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 487 | SymbolRef Sym, BugReporter& BR, |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 488 | PathDiagnostic& PD) { |
| 489 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 490 | const ExplodedNode<GRState>* Pred = N->pred_empty() ? 0 : *N->pred_begin(); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 491 | const GRState* PrevSt = Pred ? Pred->getState() : 0; |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 492 | |
| 493 | if (!PrevSt) |
| 494 | return; |
| 495 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 496 | // Look at the region bindings of the current state that map to the |
| 497 | // specified symbol. Are any of them not in the previous state? |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 498 | GRStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 499 | NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR); |
| 500 | cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H); |
| 501 | } |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 503 | namespace { |
| 504 | class VISIBILITY_HIDDEN ScanNotableSymbols |
| 505 | : public StoreManager::BindingsHandler { |
| 506 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 507 | llvm::SmallSet<SymbolRef, 10> AlreadyProcessed; |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 508 | const ExplodedNode<GRState>* N; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 509 | Stmt* S; |
| 510 | GRBugReporter& BR; |
| 511 | PathDiagnostic& PD; |
| 512 | |
| 513 | public: |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 514 | ScanNotableSymbols(const ExplodedNode<GRState>* n, Stmt* s, GRBugReporter& br, |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 515 | PathDiagnostic& pd) |
| 516 | : N(n), S(s), BR(br), PD(pd) {} |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 517 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 518 | bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal V) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 519 | SymbolRef ScanSym; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 520 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 521 | if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&V)) |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 522 | ScanSym = SV->getSymbol(); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 523 | else if (nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(&V)) |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 524 | ScanSym = SV->getSymbol(); |
| 525 | else |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 526 | return true; |
| 527 | |
| 528 | assert (ScanSym.isInitialized()); |
| 529 | |
| 530 | if (!BR.isNotable(ScanSym)) |
| 531 | return true; |
| 532 | |
| 533 | if (AlreadyProcessed.count(ScanSym)) |
| 534 | return true; |
| 535 | |
| 536 | AlreadyProcessed.insert(ScanSym); |
| 537 | |
| 538 | HandleNotableSymbol(N, S, ScanSym, BR, PD); |
| 539 | return true; |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 540 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 541 | }; |
| 542 | } // end anonymous namespace |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 544 | namespace { |
| 545 | class VISIBILITY_HIDDEN NodeMapClosure : public BugReport::NodeResolver { |
| 546 | NodeBackMap& M; |
| 547 | public: |
| 548 | NodeMapClosure(NodeBackMap *m) : M(*m) {} |
| 549 | ~NodeMapClosure() {} |
| 550 | |
| 551 | const ExplodedNode<GRState>* getOriginalNode(const ExplodedNode<GRState>* N) { |
| 552 | NodeBackMap::iterator I = M.find(N); |
| 553 | return I == M.end() ? 0 : I->second; |
| 554 | } |
| 555 | }; |
| 556 | } |
| 557 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 558 | void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 559 | BugReportEquivClass& EQ) { |
| 560 | |
| 561 | std::vector<const ExplodedNode<GRState>*> Nodes; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 562 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 563 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { |
| 564 | const ExplodedNode<GRState>* N = I->getEndNode(); |
| 565 | if (N) Nodes.push_back(N); |
| 566 | } |
| 567 | |
| 568 | if (Nodes.empty()) |
| 569 | return; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 570 | |
| 571 | // 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] | 572 | // node to a root. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 573 | const std::pair<std::pair<ExplodedGraph<GRState>*, NodeBackMap*>, |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 574 | std::pair<ExplodedNode<GRState>*, unsigned> >& |
| 575 | GPair = MakeReportGraph(&getGraph(), &Nodes[0], &Nodes[0] + Nodes.size()); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 576 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 577 | // Find the BugReport with the original location. |
| 578 | BugReport *R = 0; |
| 579 | unsigned i = 0; |
| 580 | for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I, ++i) |
| 581 | if (i == GPair.second.second) { R = *I; break; } |
| 582 | |
| 583 | assert(R && "No original report found for sliced graph."); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 584 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 585 | llvm::OwningPtr<ExplodedGraph<GRState> > ReportGraph(GPair.first.first); |
| 586 | llvm::OwningPtr<NodeBackMap> BackMap(GPair.first.second); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 587 | const ExplodedNode<GRState> *N = GPair.second.first; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 588 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 589 | // Start building the path diagnostic... |
| 590 | if (PathDiagnosticPiece* Piece = R->getEndPath(*this, N)) |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 591 | PD.push_back(Piece); |
| 592 | else |
| 593 | return; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 594 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 595 | const ExplodedNode<GRState>* NextNode = N->pred_empty() |
| 596 | ? NULL : *(N->pred_begin()); |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 597 | |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 598 | ASTContext& Ctx = getContext(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 599 | SourceManager& SMgr = Ctx.getSourceManager(); |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 600 | NodeMapClosure NMC(BackMap.get()); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 601 | |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 602 | while (NextNode) { |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 603 | N = NextNode; |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 604 | NextNode = GetPredecessorNode(N); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 605 | |
| 606 | ProgramPoint P = N->getLocation(); |
| 607 | |
| 608 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 609 | |
| 610 | CFGBlock* Src = BE->getSrc(); |
| 611 | CFGBlock* Dst = BE->getDst(); |
| 612 | |
| 613 | Stmt* T = Src->getTerminator(); |
| 614 | |
| 615 | if (!T) |
| 616 | continue; |
| 617 | |
| 618 | FullSourceLoc L(T->getLocStart(), SMgr); |
| 619 | |
| 620 | switch (T->getStmtClass()) { |
| 621 | default: |
| 622 | break; |
| 623 | |
| 624 | case Stmt::GotoStmtClass: |
| 625 | case Stmt::IndirectGotoStmtClass: { |
| 626 | |
Ted Kremenek | b697b10 | 2009-02-23 22:44:26 +0000 | [diff] [blame] | 627 | Stmt* S = GetNextStmt(N); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 628 | |
| 629 | if (!S) |
| 630 | continue; |
| 631 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 632 | std::string sbuf; |
| 633 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 634 | |
| 635 | os << "Control jumps to line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 636 | << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 637 | |
| 638 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 639 | break; |
| 640 | } |
| 641 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 642 | case Stmt::SwitchStmtClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 643 | // Figure out what case arm we took. |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 644 | std::string sbuf; |
| 645 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 646 | |
| 647 | if (Stmt* S = Dst->getLabel()) |
| 648 | switch (S->getStmtClass()) { |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 649 | default: |
Ted Kremenek | 3ddc4d5 | 2008-12-20 01:41:43 +0000 | [diff] [blame] | 650 | os << "No cases match in the switch statement. " |
| 651 | "Control jumps to line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 652 | << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 653 | break; |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 654 | case Stmt::DefaultStmtClass: |
| 655 | os << "Control jumps to the 'default' case at line " |
| 656 | << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n"; |
| 657 | break; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 658 | |
| 659 | case Stmt::CaseStmtClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 660 | os << "Control jumps to 'case "; |
| 661 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 662 | CaseStmt* Case = cast<CaseStmt>(S); |
| 663 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 664 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 665 | // Determine if it is an enum. |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 666 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 667 | bool GetRawInt = true; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 668 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 669 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 670 | |
| 671 | // FIXME: Maybe this should be an assertion. Are there cases |
| 672 | // were it is not an EnumConstantDecl? |
Chris Lattner | 470e5fc | 2008-11-18 06:07:40 +0000 | [diff] [blame] | 673 | EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl()); |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 674 | if (D) { |
| 675 | GetRawInt = false; |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 676 | os << D->getNameAsString(); |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 677 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 678 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 679 | |
| 680 | if (GetRawInt) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 681 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 682 | // Not an enum. |
| 683 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 684 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 685 | llvm::APSInt V(bits, false); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 686 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 687 | if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) { |
| 688 | assert (false && "Case condition must be constant."); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 689 | continue; |
| 690 | } |
| 691 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 692 | os << V; |
| 693 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 694 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 695 | os << ":' at line " |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 696 | << SMgr.getInstantiationLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 697 | |
| 698 | break; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 701 | else { |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 702 | os << "'Default' branch taken. "; |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 703 | ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl()); |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 704 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 705 | |
| 706 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 707 | break; |
| 708 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 709 | |
| 710 | case Stmt::BreakStmtClass: |
| 711 | case Stmt::ContinueStmtClass: { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 712 | std::string sbuf; |
| 713 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 714 | ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl()); |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 715 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 716 | break; |
| 717 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 718 | |
| 719 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 720 | std::string sbuf; |
| 721 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 722 | os << "'?' condition evaluates to "; |
| 723 | |
| 724 | if (*(Src->succ_begin()+1) == Dst) |
| 725 | os << "false."; |
| 726 | else |
| 727 | os << "true."; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 728 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 729 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 730 | break; |
| 731 | } |
| 732 | |
| 733 | case Stmt::DoStmtClass: { |
| 734 | |
| 735 | if (*(Src->succ_begin()) == Dst) { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 736 | std::string sbuf; |
| 737 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 738 | |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 739 | os << "Loop condition is true. "; |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 740 | ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl()); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 741 | |
| 742 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 743 | } |
| 744 | else |
| 745 | PD.push_front(new PathDiagnosticPiece(L, |
| 746 | "Loop condition is false. Exiting loop.")); |
| 747 | |
| 748 | break; |
| 749 | } |
| 750 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 751 | case Stmt::WhileStmtClass: |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 752 | case Stmt::ForStmtClass: { |
| 753 | |
| 754 | if (*(Src->succ_begin()+1) == Dst) { |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 755 | std::string sbuf; |
| 756 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 757 | |
Ted Kremenek | c3517eb | 2008-09-12 18:17:46 +0000 | [diff] [blame] | 758 | os << "Loop condition is false. "; |
Ted Kremenek | b479dad | 2009-02-23 23:13:51 +0000 | [diff] [blame] | 759 | ExecutionContinues(os, SMgr, N, getStateManager().getCodeDecl()); |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 760 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 761 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 762 | } |
| 763 | else |
| 764 | PD.push_front(new PathDiagnosticPiece(L, |
| 765 | "Loop condition is true. Entering loop body.")); |
| 766 | |
| 767 | break; |
| 768 | } |
| 769 | |
Ted Kremenek | 297308e | 2009-02-10 23:56:07 +0000 | [diff] [blame] | 770 | case Stmt::IfStmtClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 771 | if (*(Src->succ_begin()+1) == Dst) |
| 772 | PD.push_front(new PathDiagnosticPiece(L, "Taking false branch.")); |
| 773 | else |
| 774 | PD.push_front(new PathDiagnosticPiece(L, "Taking true branch.")); |
| 775 | |
| 776 | break; |
| 777 | } |
| 778 | } |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 779 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 780 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 781 | if (PathDiagnosticPiece* p = R->VisitNode(N, NextNode, *ReportGraph, *this, |
| 782 | NMC)) |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 783 | PD.push_front(p); |
| 784 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 785 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 786 | // 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] | 787 | // lval binding. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 788 | ScanNotableSymbols SNS(N, PS->getStmt(), *this, PD); |
| 789 | getStateManager().iterBindings(N->getState(), SNS); |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 790 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 794 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 795 | void BugReporter::Register(BugType *BT) { |
| 796 | BugTypes = F.Add(BugTypes, BT); |
Ted Kremenek | 76d90c8 | 2008-05-16 18:33:14 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 799 | void BugReporter::EmitReport(BugReport* R) { |
| 800 | // Compute the bug report's hash to determine its equivalence class. |
| 801 | llvm::FoldingSetNodeID ID; |
| 802 | R->Profile(ID); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 803 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 804 | // Lookup the equivance class. If there isn't one, create it. |
| 805 | BugType& BT = R->getBugType(); |
| 806 | Register(&BT); |
| 807 | void *InsertPos; |
| 808 | BugReportEquivClass* EQ = BT.EQClasses.FindNodeOrInsertPos(ID, InsertPos); |
| 809 | |
| 810 | if (!EQ) { |
| 811 | EQ = new BugReportEquivClass(R); |
| 812 | BT.EQClasses.InsertNode(EQ, InsertPos); |
| 813 | } |
| 814 | else |
| 815 | EQ->AddReport(R); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 818 | void BugReporter::FlushReport(BugReportEquivClass& EQ) { |
| 819 | assert(!EQ.Reports.empty()); |
| 820 | BugReport &R = **EQ.begin(); |
| 821 | |
| 822 | // FIXME: Make sure we use the 'R' for the path that was actually used. |
| 823 | // Probably doesn't make a difference in practice. |
| 824 | BugType& BT = R.getBugType(); |
| 825 | |
| 826 | llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getBugType().getName(), |
| 827 | R.getDescription(), |
| 828 | BT.getCategory())); |
| 829 | GeneratePathDiagnostic(*D.get(), EQ); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 830 | |
| 831 | // Get the meta data. |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 832 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 833 | 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] | 834 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 835 | // Emit a summary diagnostic to the regular Diagnostics engine. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 836 | PathDiagnosticClient* PD = getPathDiagnosticClient(); |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 837 | const SourceRange *Beg = 0, *End = 0; |
| 838 | R.getRanges(*this, Beg, End); |
| 839 | Diagnostic& Diag = getDiagnostic(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 840 | FullSourceLoc L(R.getLocation(), getSourceManager()); |
Ted Kremenek | d90e708 | 2009-02-07 22:36:41 +0000 | [diff] [blame] | 841 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 842 | R.getDescription().c_str()); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 843 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 844 | switch (End-Beg) { |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 845 | default: assert(0 && "Don't handle this many ranges yet!"); |
| 846 | case 0: Diag.Report(L, ErrorDiag); break; |
| 847 | case 1: Diag.Report(L, ErrorDiag) << Beg[0]; break; |
| 848 | case 2: Diag.Report(L, ErrorDiag) << Beg[0] << Beg[1]; break; |
| 849 | 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] | 850 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 851 | |
| 852 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
| 853 | if (!PD) |
| 854 | return; |
| 855 | |
| 856 | if (D->empty()) { |
| 857 | PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription()); |
| 858 | for ( ; Beg != End; ++Beg) piece->addRange(*Beg); |
| 859 | D->push_back(piece); |
| 860 | } |
| 861 | |
| 862 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 863 | } |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 864 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 865 | void BugReporter::EmitBasicReport(const char* name, const char* str, |
| 866 | SourceLocation Loc, |
| 867 | SourceRange* RBeg, unsigned NumRanges) { |
| 868 | EmitBasicReport(name, "", str, Loc, RBeg, NumRanges); |
| 869 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 870 | |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 871 | void BugReporter::EmitBasicReport(const char* name, const char* category, |
| 872 | const char* str, SourceLocation Loc, |
| 873 | SourceRange* RBeg, unsigned NumRanges) { |
| 874 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 875 | // 'BT' will be owned by BugReporter as soon as we call 'EmitReport'. |
| 876 | BugType *BT = new BugType(name, category); |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 877 | FullSourceLoc L = getContext().getFullLoc(Loc); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 878 | RangedBugReport *R = new DiagBugReport(*BT, str, L); |
| 879 | for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg); |
| 880 | EmitReport(R); |
Ted Kremenek | 5720207 | 2008-07-14 17:40:50 +0000 | [diff] [blame] | 881 | } |