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