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