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