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