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" |
| 24 | #include <sstream> |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | BugReporter::~BugReporter() {} |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 29 | BugType::~BugType() {} |
| 30 | BugReport::~BugReport() {} |
Ted Kremenek | 5e55cda | 2008-04-11 18:40:29 +0000 | [diff] [blame] | 31 | RangedBugReport::~RangedBugReport() {} |
| 32 | |
Ted Kremenek | 1aa44c7 | 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 | 61f3e05 | 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 | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 56 | static inline Stmt* GetStmt(const CFGBlock* B) { |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 57 | if (B->empty()) |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 58 | return const_cast<Stmt*>(B->getTerminator()); |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 59 | else |
| 60 | return (*B)[0]; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Ted Kremenek | bd7efa8 | 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 | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | bd7efa8 | 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 | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 82 | static void ExecutionContinues(std::ostringstream& os, SourceManager& SMgr, |
| 83 | Stmt* S) { |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 84 | |
| 85 | if (!S) |
| 86 | return; |
Ted Kremenek | 143ca22 | 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 | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 93 | << SMgr.getLogicalLineNumber(S->getLocStart()) << '.'; |
| 94 | } |
Ted Kremenek | 143ca22 | 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 | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 108 | ExecutionContinues(os, SMgr, GetStmt(B)); |
| 109 | } |
| 110 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 111 | |
| 112 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
| 113 | |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 114 | ProgramPoint ProgP = EndNode->getLocation(); |
Ted Kremenek | bd7efa8 | 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 | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 119 | S = GetLastStmt(EndNode); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 120 | if (!S) |
Ted Kremenek | bb77e9b | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 131 | |
| 132 | if (!S) |
| 133 | return NULL; |
| 134 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 135 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
| 136 | PathDiagnosticPiece* P = new PathDiagnosticPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 138 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 139 | getRanges(BR, Beg, End); |
| 140 | |
| 141 | for (; Beg != End; ++Beg) |
| 142 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 143 | |
| 144 | return P; |
| 145 | } |
| 146 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 147 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 148 | const SourceRange*& end) { |
| 149 | |
| 150 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 151 | R = E->getSourceRange(); |
| 152 | beg = &R; |
| 153 | end = beg+1; |
| 154 | } |
| 155 | else |
| 156 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 159 | FullSourceLoc BugReport::getLocation(SourceManager& Mgr) { |
| 160 | |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 161 | if (!EndNode) |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 162 | return FullSourceLoc(); |
| 163 | |
Ted Kremenek | 200ed92 | 2008-05-02 23:21:21 +0000 | [diff] [blame] | 164 | Stmt* S = GetStmt(EndNode->getLocation()); |
Ted Kremenek | d2f642b | 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 | 50a6d0c | 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 | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 175 | BugReporter& BR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 176 | return NULL; |
| 177 | } |
| 178 | |
Ted Kremenek | a43a1eb | 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 | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 182 | llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(G->Trim(&N, &N+1)); |
| 183 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 184 | // Find the error node in the trimmed graph. |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 186 | ExplodedNode<ValueState>* NOld = N; |
| 187 | N = 0; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 188 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 189 | for (ExplodedGraph<ValueState>::node_iterator |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 190 | I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 192 | if (I->getState() == NOld->getState() && |
| 193 | I->getLocation() == NOld->getLocation()) { |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 194 | N = &*I; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
| 197 | } |
| 198 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 199 | assert(N); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | a43a1eb | 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 | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 207 | ExplodedNode<ValueState> *Last = 0, *First = 0; |
Ted Kremenek | a43a1eb | 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 | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 213 | if (!First) First = NewN; |
Ted Kremenek | a43a1eb | 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 | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 220 | return std::make_pair(G, First); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Ted Kremenek | 1aa44c7 | 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 | a43a1eb | 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 | bd7efa8 | 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 | 6837faa | 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 | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 370 | SourceManager& SMgr = Ctx.getSourceManager(); |
| 371 | |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 372 | while (NextNode) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 373 | |
| 374 | ExplodedNode<ValueState>* LastNode = N; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 375 | N = NextNode; |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 376 | NextNode = GetNextNode(N); |
Ted Kremenek | 61f3e05 | 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 | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 407 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 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 | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 416 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 417 | std::ostringstream os; |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 418 | |
| 419 | if (Stmt* S = Dst->getLabel()) |
| 420 | switch (S->getStmtClass()) { |
| 421 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 422 | default: |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 423 | assert(false && "Not a valid switch label."); |
| 424 | continue; |
| 425 | |
Ted Kremenek | 8b90455 | 2008-04-22 22:29:46 +0000 | [diff] [blame] | 426 | case Stmt::DefaultStmtClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 427 | |
| 428 | os << "Control jumps to the 'default' case at line " |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 429 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 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 | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 438 | CaseStmt* Case = cast<CaseStmt>(S); |
| 439 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 441 | // Determine if it is an enum. |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 442 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 443 | bool GetRawInt = true; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | 5a42995 | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 456 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 457 | |
| 458 | if (GetRawInt) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 459 | |
Ted Kremenek | 5a42995 | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | 5a42995 | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 467 | continue; |
| 468 | } |
| 469 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 470 | os << V.toString(); |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 471 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 473 | os << ":' at line " |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 474 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 475 | |
| 476 | break; |
| 477 | |
| 478 | } |
| 479 | } |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 480 | else { |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 481 | os << "'Default' branch taken."; |
| 482 | ExecutionContinues(os, SMgr, LastNode); |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 483 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 484 | |
| 485 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 486 | break; |
| 487 | } |
Ted Kremenek | 2673c9f | 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 | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 496 | |
| 497 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | 706e3cf | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 506 | |
Ted Kremenek | 706e3cf | 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 | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 518 | os << "Loop condition is true."; |
| 519 | ExecutionContinues(os, SMgr, Dst); |
Ted Kremenek | 706e3cf | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 530 | case Stmt::WhileStmtClass: |
Ted Kremenek | 706e3cf | 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 | 143ca22 | 2008-05-06 18:11:09 +0000 | [diff] [blame] | 537 | os << "Loop condition is false."; |
| 538 | ExecutionContinues(os, SMgr, Dst); |
Ted Kremenek | 706e3cf | 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 | 61f3e05 | 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 | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 559 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 560 | |
| 561 | if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this)) |
Ted Kremenek | 1aa44c7 | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame^] | 604 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 605 | bool BugTypeCacheLocation::isCached(BugReport& R) { |
| 606 | |
| 607 | ExplodedNode<ValueState>* N = R.getEndNode(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 608 | |
Ted Kremenek | 329d2cc | 2008-04-18 02:24:50 +0000 | [diff] [blame] | 609 | if (!N) |
| 610 | return false; |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 611 | |
| 612 | // Cache the location of the error. Don't emit the same |
Ted Kremenek | 61f3e05 | 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 | 76d90c8 | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 622 | |
| 623 | if (CachedErrors.count(p)) |
| 624 | return true; |
| 625 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 626 | CachedErrors.insert(p); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 630 | void BugReporter::EmitWarning(BugReport& R) { |
| 631 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 632 | if (R.getBugType().isCached(R)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 633 | return; |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | 5585114 | 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 | 072192b | 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 | 75840e1 | 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 | 70d1722 | 2008-04-03 07:33:55 +0000 | [diff] [blame] | 646 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 647 | if (PD && !D->empty()) { |
| 648 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 649 | return; |
| 650 | } |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 651 | |
Ted Kremenek | 75840e1 | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 654 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 655 | FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager()) |
| 656 | : D->back()->getLocation(); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 657 | |
| 658 | |
| 659 | // Determine the range. |
Ted Kremenek | 5fcca68 | 2008-04-14 18:06:42 +0000 | [diff] [blame] | 660 | |
Ted Kremenek | 4bb6ac2 | 2008-04-10 16:12:38 +0000 | [diff] [blame] | 661 | const SourceRange *Beg, *End; |
Ted Kremenek | 5fcca68 | 2008-04-14 18:06:42 +0000 | [diff] [blame] | 662 | |
Ted Kremenek | 5585114 | 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 | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 666 | } |
| 667 | else |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 668 | R.getRanges(*this, Beg, End); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 669 | |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 670 | if (PD) { |
Ted Kremenek | 2f0e89e | 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 | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 676 | D->push_back(piece); |
| 677 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 678 | } |
| 679 | else { |
| 680 | std::ostringstream os; |
| 681 | os << "[CHECKER] "; |
| 682 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 683 | if (D->empty()) |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 684 | os << R.getDescription(); |
| 685 | else |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 686 | os << D->back()->getString(); |
Ted Kremenek | 2f0e89e | 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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 694 | } |