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