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