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