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