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