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 | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 33 | ExplodedGraph<ValueState>& BugReporter::getGraph() { return Eng.getGraph(); } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 34 | |
| 35 | static inline Stmt* GetStmt(const ProgramPoint& P) { |
| 36 | if (const PostStmt* PS = dyn_cast<PostStmt>(&P)) { |
| 37 | return PS->getStmt(); |
| 38 | } |
| 39 | else if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 40 | return BE->getSrc()->getTerminator(); |
| 41 | } |
| 42 | else if (const BlockEntrance* BE = dyn_cast<BlockEntrance>(&P)) { |
| 43 | return BE->getFirstStmt(); |
| 44 | } |
| 45 | |
| 46 | assert (false && "Unsupported ProgramPoint."); |
| 47 | return NULL; |
| 48 | } |
| 49 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 50 | static inline Stmt* GetStmt(const CFGBlock* B) { |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 51 | if (B->empty()) { |
| 52 | assert (B->getTerminator() && "Empty block should have a terminator."); |
| 53 | return const_cast<Stmt*>(B->getTerminator()); |
| 54 | } |
| 55 | else |
| 56 | return (*B)[0]; |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 59 | static inline ExplodedNode<ValueState>* |
| 60 | GetNextNode(ExplodedNode<ValueState>* N) { |
| 61 | return N->pred_empty() ? NULL : *(N->pred_begin()); |
| 62 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 64 | static Stmt* GetLastStmt(ExplodedNode<ValueState>* N) { |
| 65 | assert (isa<BlockEntrance>(N->getLocation())); |
| 66 | |
| 67 | for (N = GetNextNode(N); N; N = GetNextNode(N)) { |
| 68 | |
| 69 | ProgramPoint P = N->getLocation(); |
| 70 | |
| 71 | if (PostStmt* PS = dyn_cast<PostStmt>(&P)) |
| 72 | return PS->getStmt(); |
| 73 | } |
| 74 | |
| 75 | return NULL; |
| 76 | } |
| 77 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame^] | 78 | |
| 79 | static void ExecutionContinues(std::ostream& os, SourceManager& SMgr, |
| 80 | ExplodedNode<ValueState>* N) { |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame^] | 82 | Stmt* S = GetStmt(N->getLocation()); |
| 83 | |
| 84 | if (!S) |
| 85 | return; |
| 86 | |
| 87 | os << "Execution continue on line " |
| 88 | << SMgr.getLogicalLineNumber(S->getLocStart()) << '.'; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | Stmt* BugReport::getStmt(BugReporter& BR) const { |
| 93 | |
| 94 | ProgramPoint ProgP = N->getLocation(); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 95 | Stmt *S = NULL; |
| 96 | |
| 97 | if (BlockEntrance* BE = dyn_cast<BlockEntrance>(&ProgP)) |
| 98 | if (BE->getBlock() == &BR.getCFG().getExit()) |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame^] | 99 | S = GetLastStmt(N); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 100 | if (!S) |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame^] | 101 | S = GetStmt(ProgP); |
| 102 | |
| 103 | return S; |
| 104 | } |
| 105 | |
| 106 | PathDiagnosticPiece* |
| 107 | BugReport::getEndPath(BugReporter& BR, |
| 108 | ExplodedNode<ValueState>* EndPathNode) { |
| 109 | |
| 110 | Stmt* S = getStmt(BR); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 111 | |
| 112 | if (!S) |
| 113 | return NULL; |
| 114 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 115 | FullSourceLoc L(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 116 | |
| 117 | PathDiagnosticPiece* P = |
| 118 | new PathDiagnosticPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 120 | const SourceRange *Beg, *End; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame^] | 121 | getRanges(BR, Beg, End); |
| 122 | |
| 123 | for (; Beg != End; ++Beg) |
| 124 | P->addRange(*Beg); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 125 | |
| 126 | return P; |
| 127 | } |
| 128 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame^] | 129 | void BugReport::getRanges(BugReporter& BR, const SourceRange*& beg, |
| 130 | const SourceRange*& end) { |
| 131 | |
| 132 | if (Expr* E = dyn_cast_or_null<Expr>(getStmt(BR))) { |
| 133 | R = E->getSourceRange(); |
| 134 | beg = &R; |
| 135 | end = beg+1; |
| 136 | } |
| 137 | else |
| 138 | beg = end = 0; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 141 | FullSourceLoc BugReport::getLocation(SourceManager& Mgr) { |
| 142 | |
| 143 | if (!N) |
| 144 | return FullSourceLoc(); |
| 145 | |
| 146 | Stmt* S = GetStmt(N->getLocation()); |
| 147 | |
| 148 | if (!S) |
| 149 | return FullSourceLoc(); |
| 150 | |
| 151 | return FullSourceLoc(S->getLocStart(), Mgr); |
| 152 | } |
| 153 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 154 | PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N, |
| 155 | ExplodedNode<ValueState>* PrevN, |
| 156 | ExplodedGraph<ValueState>& G, |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 157 | BugReporter& BR) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
| 160 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 161 | static std::pair<ExplodedGraph<ValueState>*, ExplodedNode<ValueState>*> |
| 162 | MakeReportGraph(ExplodedGraph<ValueState>* G, ExplodedNode<ValueState>* N) { |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 163 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 164 | llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(G->Trim(&N, &N+1)); |
| 165 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 166 | // Find the error node in the trimmed graph. |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 168 | ExplodedNode<ValueState>* NOld = N; |
| 169 | N = 0; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 171 | for (ExplodedGraph<ValueState>::node_iterator |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 172 | I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) { |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 173 | |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 174 | if (I->getState() == NOld->getState() && |
| 175 | I->getLocation() == NOld->getLocation()) { |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 176 | N = &*I; |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 181 | assert(N); |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 183 | // Create a new graph with a single path. |
| 184 | |
| 185 | G = new ExplodedGraph<ValueState>(GTrim->getCFG(), GTrim->getCodeDecl(), |
| 186 | GTrim->getContext()); |
| 187 | |
| 188 | |
Ted Kremenek | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 189 | ExplodedNode<ValueState> *Last = 0, *First = 0; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 190 | |
| 191 | while (N) { |
| 192 | ExplodedNode<ValueState>* NewN = |
| 193 | G->getNode(N->getLocation(), N->getState()); |
| 194 | |
Ted Kremenek | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 195 | if (!First) First = NewN; |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 196 | if (Last) Last->addPredecessor(NewN); |
| 197 | |
| 198 | Last = NewN; |
| 199 | N = N->pred_empty() ? 0 : *(N->pred_begin()); |
| 200 | } |
| 201 | |
Ted Kremenek | 6aaca9c | 2008-04-23 23:04:32 +0000 | [diff] [blame] | 202 | return std::make_pair(G, First); |
Ted Kremenek | a43a1eb | 2008-04-23 23:02:12 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
| 206 | BugReport& R) { |
| 207 | |
| 208 | ExplodedNode<ValueState>* N = R.getEndNode(); |
| 209 | |
| 210 | if (!N) return; |
| 211 | |
| 212 | // Construct a new graph that contains only a single path from the error |
| 213 | // node to a root. |
| 214 | |
| 215 | const std::pair<ExplodedGraph<ValueState>*,ExplodedNode<ValueState>*> |
| 216 | GPair = MakeReportGraph(&getGraph(), N); |
| 217 | |
| 218 | llvm::OwningPtr<ExplodedGraph<ValueState> > ReportGraph(GPair.first); |
| 219 | assert(GPair.second->getLocation() == N->getLocation()); |
| 220 | N = GPair.second; |
| 221 | |
| 222 | // Start building the path diagnostic... |
| 223 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 224 | if (PathDiagnosticPiece* Piece = R.getEndPath(*this, N)) |
| 225 | PD.push_back(Piece); |
| 226 | else |
| 227 | return; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 228 | |
| 229 | ExplodedNode<ValueState>* NextNode = N->pred_empty() |
| 230 | ? NULL : *(N->pred_begin()); |
| 231 | |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 232 | SourceManager& SMgr = Ctx.getSourceManager(); |
| 233 | |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 234 | while (NextNode) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 235 | |
| 236 | ExplodedNode<ValueState>* LastNode = N; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 237 | N = NextNode; |
Ted Kremenek | bd7efa8 | 2008-04-17 23:44:37 +0000 | [diff] [blame] | 238 | NextNode = GetNextNode(N); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 239 | |
| 240 | ProgramPoint P = N->getLocation(); |
| 241 | |
| 242 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 243 | |
| 244 | CFGBlock* Src = BE->getSrc(); |
| 245 | CFGBlock* Dst = BE->getDst(); |
| 246 | |
| 247 | Stmt* T = Src->getTerminator(); |
| 248 | |
| 249 | if (!T) |
| 250 | continue; |
| 251 | |
| 252 | FullSourceLoc L(T->getLocStart(), SMgr); |
| 253 | |
| 254 | switch (T->getStmtClass()) { |
| 255 | default: |
| 256 | break; |
| 257 | |
| 258 | case Stmt::GotoStmtClass: |
| 259 | case Stmt::IndirectGotoStmtClass: { |
| 260 | |
| 261 | Stmt* S = GetStmt(LastNode->getLocation()); |
| 262 | |
| 263 | if (!S) |
| 264 | continue; |
| 265 | |
| 266 | std::ostringstream os; |
| 267 | |
| 268 | os << "Control jumps to line " |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 269 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 270 | |
| 271 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | case Stmt::SwitchStmtClass: { |
| 276 | |
| 277 | // Figure out what case arm we took. |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 279 | std::ostringstream os; |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 280 | |
| 281 | if (Stmt* S = Dst->getLabel()) |
| 282 | switch (S->getStmtClass()) { |
| 283 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 284 | default: |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 285 | assert(false && "Not a valid switch label."); |
| 286 | continue; |
| 287 | |
Ted Kremenek | 8b90455 | 2008-04-22 22:29:46 +0000 | [diff] [blame] | 288 | case Stmt::DefaultStmtClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 289 | |
| 290 | os << "Control jumps to the 'default' case at line " |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 291 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 292 | |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | case Stmt::CaseStmtClass: { |
| 297 | |
| 298 | os << "Control jumps to 'case "; |
| 299 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 300 | CaseStmt* Case = cast<CaseStmt>(S); |
| 301 | Expr* LHS = Case->getLHS()->IgnoreParenCasts(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 303 | // Determine if it is an enum. |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 305 | bool GetRawInt = true; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 306 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 307 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) { |
| 308 | |
| 309 | // FIXME: Maybe this should be an assertion. Are there cases |
| 310 | // were it is not an EnumConstantDecl? |
| 311 | |
| 312 | EnumConstantDecl* D = dyn_cast<EnumConstantDecl>(DR->getDecl()); |
| 313 | |
| 314 | if (D) { |
| 315 | GetRawInt = false; |
| 316 | os << D->getName(); |
| 317 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 318 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 319 | |
| 320 | if (GetRawInt) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 321 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 322 | // Not an enum. |
| 323 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 324 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 325 | llvm::APSInt V(bits, false); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 327 | if (!LHS->isIntegerConstantExpr(V, Ctx, 0, true)) { |
| 328 | assert (false && "Case condition must be constant."); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 329 | continue; |
| 330 | } |
| 331 | |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 332 | os << V.toString(); |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 333 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 334 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 335 | os << ":' at line " |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 336 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 337 | |
| 338 | break; |
| 339 | |
| 340 | } |
| 341 | } |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 342 | else { |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 343 | os << "'Default' branch taken."; |
| 344 | ExecutionContinues(os, SMgr, LastNode); |
Ted Kremenek | 5678392 | 2008-04-25 01:29:56 +0000 | [diff] [blame] | 345 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 346 | |
| 347 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 348 | break; |
| 349 | } |
Ted Kremenek | 2673c9f | 2008-04-25 19:01:27 +0000 | [diff] [blame] | 350 | |
| 351 | case Stmt::BreakStmtClass: |
| 352 | case Stmt::ContinueStmtClass: { |
| 353 | std::ostringstream os; |
| 354 | ExecutionContinues(os, SMgr, LastNode); |
| 355 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 356 | break; |
| 357 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 358 | |
| 359 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 361 | std::ostringstream os; |
| 362 | os << "'?' condition evaluates to "; |
| 363 | |
| 364 | if (*(Src->succ_begin()+1) == Dst) |
| 365 | os << "false."; |
| 366 | else |
| 367 | os << "true."; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 369 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 370 | |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | case Stmt::DoStmtClass: { |
| 375 | |
| 376 | if (*(Src->succ_begin()) == Dst) { |
| 377 | |
| 378 | std::ostringstream os; |
| 379 | |
| 380 | os << "Loop condition is true. Execution continues on line " |
| 381 | << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.'; |
| 382 | |
| 383 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 384 | } |
| 385 | else |
| 386 | PD.push_front(new PathDiagnosticPiece(L, |
| 387 | "Loop condition is false. Exiting loop.")); |
| 388 | |
| 389 | break; |
| 390 | } |
| 391 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 392 | case Stmt::WhileStmtClass: |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 393 | case Stmt::ForStmtClass: { |
| 394 | |
| 395 | if (*(Src->succ_begin()+1) == Dst) { |
| 396 | |
| 397 | std::ostringstream os; |
| 398 | |
| 399 | os << "Loop condition is false. Execution continues on line " |
| 400 | << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.'; |
| 401 | |
| 402 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 403 | } |
| 404 | else |
| 405 | PD.push_front(new PathDiagnosticPiece(L, |
| 406 | "Loop condition is true. Entering loop body.")); |
| 407 | |
| 408 | break; |
| 409 | } |
| 410 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 411 | case Stmt::IfStmtClass: { |
| 412 | |
| 413 | if (*(Src->succ_begin()+1) == Dst) |
| 414 | PD.push_front(new PathDiagnosticPiece(L, "Taking false branch.")); |
| 415 | else |
| 416 | PD.push_front(new PathDiagnosticPiece(L, "Taking true branch.")); |
| 417 | |
| 418 | break; |
| 419 | } |
| 420 | } |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 421 | } |
Ted Kremenek | 5a42995 | 2008-04-23 23:35:07 +0000 | [diff] [blame] | 422 | |
| 423 | if (PathDiagnosticPiece* p = R.VisitNode(N, NextNode, *ReportGraph, *this)) |
| 424 | PD.push_front(p); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 428 | bool BugTypeCacheLocation::isCached(BugReport& R) { |
| 429 | |
| 430 | ExplodedNode<ValueState>* N = R.getEndNode(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 431 | |
Ted Kremenek | 329d2cc | 2008-04-18 02:24:50 +0000 | [diff] [blame] | 432 | if (!N) |
| 433 | return false; |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 434 | |
| 435 | // Cache the location of the error. Don't emit the same |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 436 | // warning for the same error type that occurs at the same program |
| 437 | // location but along a different path. |
| 438 | |
| 439 | void* p = N->getLocation().getRawData(); |
| 440 | |
| 441 | if (CachedErrors.count(p)) |
| 442 | return true; |
| 443 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 444 | CachedErrors.insert(p); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 448 | void BugReporter::EmitWarning(BugReport& R) { |
| 449 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 450 | if (R.getBugType().isCached(R)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 451 | return; |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 452 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 453 | llvm::OwningPtr<PathDiagnostic> D(new PathDiagnostic(R.getName())); |
| 454 | GeneratePathDiagnostic(*D.get(), R); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 455 | |
| 456 | // Get the meta data. |
| 457 | |
| 458 | std::pair<const char**, const char**> Meta = R.getExtraDescriptiveText(); |
| 459 | |
| 460 | for (const char** s = Meta.first; s != Meta.second; ++s) |
| 461 | D->addMeta(*s); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 462 | |
| 463 | // Emit a full diagnostic for the path if we have a PathDiagnosticClient. |
Ted Kremenek | 70d1722 | 2008-04-03 07:33:55 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 465 | if (PD && !D->empty()) { |
| 466 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 467 | return; |
| 468 | } |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 469 | |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 470 | // We don't have a PathDiagnosticClient, but we can still emit a single |
| 471 | // line diagnostic. Determine the location. |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 472 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 473 | FullSourceLoc L = D->empty() ? R.getLocation(Ctx.getSourceManager()) |
| 474 | : D->back()->getLocation(); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 475 | |
| 476 | |
| 477 | // Determine the range. |
Ted Kremenek | 5fcca68 | 2008-04-14 18:06:42 +0000 | [diff] [blame] | 478 | |
Ted Kremenek | 4bb6ac2 | 2008-04-10 16:12:38 +0000 | [diff] [blame] | 479 | const SourceRange *Beg, *End; |
Ted Kremenek | 5fcca68 | 2008-04-14 18:06:42 +0000 | [diff] [blame] | 480 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 481 | if (!D->empty()) { |
| 482 | Beg = D->back()->ranges_begin(); |
| 483 | End = D->back()->ranges_end(); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 484 | } |
| 485 | else |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame^] | 486 | R.getRanges(*this, Beg, End); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 487 | |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 488 | if (PD) { |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 489 | PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription()); |
| 490 | |
| 491 | for ( ; Beg != End; ++Beg) |
| 492 | piece->addRange(*Beg); |
| 493 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 494 | D->push_back(piece); |
| 495 | PD->HandlePathDiagnostic(D.take()); |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 496 | } |
| 497 | else { |
| 498 | std::ostringstream os; |
| 499 | os << "[CHECKER] "; |
| 500 | |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 501 | if (D->empty()) |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 502 | os << R.getDescription(); |
| 503 | else |
Ted Kremenek | 5585114 | 2008-04-22 16:15:03 +0000 | [diff] [blame] | 504 | os << D->back()->getString(); |
Ted Kremenek | 2f0e89e | 2008-04-18 22:56:53 +0000 | [diff] [blame] | 505 | |
| 506 | |
| 507 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 508 | os.str().c_str()); |
| 509 | |
| 510 | Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg); |
| 511 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 512 | } |