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