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