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