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 | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 58 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 59 | PathDiagnosticPiece* BugReport::getEndPath(ASTContext& Ctx) const { |
| 60 | |
| 61 | Stmt* S = getStmt(); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 62 | |
| 63 | if (!S) |
| 64 | return NULL; |
| 65 | |
| 66 | FullSourceLoc L(S->getLocStart(), Ctx.getSourceManager()); |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 67 | |
| 68 | PathDiagnosticPiece* P = |
| 69 | new PathDiagnosticPiece(L, getDescription()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | de7161f | 2008-04-03 18:00:37 +0000 | [diff] [blame] | 71 | const SourceRange *Beg, *End; |
| 72 | getRanges(Beg, End); |
| 73 | |
| 74 | if (Beg == End) { |
| 75 | if (Expr* E = dyn_cast<Expr>(S)) |
| 76 | P->addRange(E->getSourceRange()); |
| 77 | } |
| 78 | else { |
| 79 | assert (Beg < End); |
| 80 | for (; Beg != End; ++Beg) |
| 81 | P->addRange(*Beg); |
| 82 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 83 | |
| 84 | return P; |
| 85 | } |
| 86 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 87 | void BugReport::getRanges(const SourceRange*& beg, |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 88 | const SourceRange*& end) const { |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 89 | beg = NULL; |
| 90 | end = NULL; |
| 91 | } |
| 92 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 93 | FullSourceLoc BugReport::getLocation(SourceManager& Mgr) { |
| 94 | |
| 95 | if (!N) |
| 96 | return FullSourceLoc(); |
| 97 | |
| 98 | Stmt* S = GetStmt(N->getLocation()); |
| 99 | |
| 100 | if (!S) |
| 101 | return FullSourceLoc(); |
| 102 | |
| 103 | return FullSourceLoc(S->getLocStart(), Mgr); |
| 104 | } |
| 105 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 106 | PathDiagnosticPiece* BugReport::VisitNode(ExplodedNode<ValueState>* N, |
| 107 | ExplodedNode<ValueState>* PrevN, |
| 108 | ExplodedGraph<ValueState>& G, |
| 109 | ASTContext& Ctx) { |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | void BugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 114 | BugReport& R) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 116 | ExplodedNode<ValueState>* N = R.getEndNode(); |
| 117 | |
| 118 | assert (N && "Path diagnostic requires a ExplodedNode."); |
| 119 | |
| 120 | if (PathDiagnosticPiece* Piece = R.getEndPath(Ctx)) |
Ted Kremenek | 70d1722 | 2008-04-03 07:33:55 +0000 | [diff] [blame] | 121 | PD.push_back(Piece); |
| 122 | else |
| 123 | return; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 124 | |
| 125 | SourceManager& SMgr = Ctx.getSourceManager(); |
| 126 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 127 | llvm::OwningPtr<ExplodedGraph<ValueState> > GTrim(getGraph().Trim(&N, &N+1)); |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 128 | |
| 129 | // Find the sink in the trimmed graph. |
| 130 | // FIXME: Should we eventually have a sink iterator? |
| 131 | |
| 132 | ExplodedNode<ValueState>* NewN = 0; |
| 133 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 134 | for (ExplodedGraph<ValueState>::node_iterator |
Ted Kremenek | 94826a7 | 2008-04-03 04:59:14 +0000 | [diff] [blame] | 135 | I = GTrim->nodes_begin(), E = GTrim->nodes_end(); I != E; ++I) { |
| 136 | |
| 137 | if (I->isSink()) { |
| 138 | NewN = &*I; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | assert (NewN); |
| 144 | assert (NewN->getLocation() == N->getLocation()); |
| 145 | |
| 146 | N = NewN; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 147 | |
| 148 | ExplodedNode<ValueState>* NextNode = N->pred_empty() |
| 149 | ? NULL : *(N->pred_begin()); |
| 150 | |
| 151 | while (NextNode) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 152 | |
| 153 | ExplodedNode<ValueState>* LastNode = N; |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 154 | N = NextNode; |
| 155 | NextNode = N->pred_empty() ? NULL : *(N->pred_begin()); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 156 | |
| 157 | ProgramPoint P = N->getLocation(); |
| 158 | |
| 159 | if (const BlockEdge* BE = dyn_cast<BlockEdge>(&P)) { |
| 160 | |
| 161 | CFGBlock* Src = BE->getSrc(); |
| 162 | CFGBlock* Dst = BE->getDst(); |
| 163 | |
| 164 | Stmt* T = Src->getTerminator(); |
| 165 | |
| 166 | if (!T) |
| 167 | continue; |
| 168 | |
| 169 | FullSourceLoc L(T->getLocStart(), SMgr); |
| 170 | |
| 171 | switch (T->getStmtClass()) { |
| 172 | default: |
| 173 | break; |
| 174 | |
| 175 | case Stmt::GotoStmtClass: |
| 176 | case Stmt::IndirectGotoStmtClass: { |
| 177 | |
| 178 | Stmt* S = GetStmt(LastNode->getLocation()); |
| 179 | |
| 180 | if (!S) |
| 181 | continue; |
| 182 | |
| 183 | std::ostringstream os; |
| 184 | |
| 185 | os << "Control jumps to line " |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 186 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 187 | |
| 188 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | case Stmt::SwitchStmtClass: { |
| 193 | |
| 194 | // Figure out what case arm we took. |
| 195 | |
| 196 | Stmt* S = Dst->getLabel(); |
| 197 | |
| 198 | if (!S) |
| 199 | continue; |
| 200 | |
| 201 | std::ostringstream os; |
| 202 | |
| 203 | switch (S->getStmtClass()) { |
| 204 | default: |
| 205 | continue; |
| 206 | |
| 207 | case Stmt::DefaultStmtClass: { |
| 208 | |
| 209 | os << "Control jumps to the 'default' case at line " |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 210 | << SMgr.getLogicalLineNumber(S->getLocStart()) << ".\n"; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 211 | |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | case Stmt::CaseStmtClass: { |
| 216 | |
| 217 | os << "Control jumps to 'case "; |
| 218 | |
| 219 | Expr* CondE = cast<SwitchStmt>(T)->getCond(); |
| 220 | unsigned bits = Ctx.getTypeSize(CondE->getType()); |
| 221 | |
| 222 | llvm::APSInt V1(bits, false); |
| 223 | |
| 224 | CaseStmt* Case = cast<CaseStmt>(S); |
| 225 | |
| 226 | if (!Case->getLHS()->isIntegerConstantExpr(V1, Ctx, 0, true)) { |
| 227 | assert (false && |
| 228 | "Case condition must evaluate to an integer constant."); |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | os << V1.toString(); |
| 233 | |
| 234 | // Get the RHS of the case, if it exists. |
| 235 | |
| 236 | if (Expr* E = Case->getRHS()) { |
| 237 | |
| 238 | llvm::APSInt V2(bits, false); |
| 239 | |
| 240 | if (!E->isIntegerConstantExpr(V2, Ctx, 0, true)) { |
| 241 | assert (false && |
| 242 | "Case condition (RHS) must evaluate to an integer constant."); |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | os << " .. " << V2.toString(); |
| 247 | } |
| 248 | |
| 249 | os << ":' at 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 | break; |
| 253 | |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 258 | break; |
| 259 | } |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 260 | |
| 261 | case Stmt::ConditionalOperatorClass: { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 262 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 263 | std::ostringstream os; |
| 264 | os << "'?' condition evaluates to "; |
| 265 | |
| 266 | if (*(Src->succ_begin()+1) == Dst) |
| 267 | os << "false."; |
| 268 | else |
| 269 | os << "true."; |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 271 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 272 | |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | case Stmt::DoStmtClass: { |
| 277 | |
| 278 | if (*(Src->succ_begin()) == Dst) { |
| 279 | |
| 280 | std::ostringstream os; |
| 281 | |
| 282 | os << "Loop condition is true. Execution continues on line " |
| 283 | << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.'; |
| 284 | |
| 285 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 286 | } |
| 287 | else |
| 288 | PD.push_front(new PathDiagnosticPiece(L, |
| 289 | "Loop condition is false. Exiting loop.")); |
| 290 | |
| 291 | break; |
| 292 | } |
| 293 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 294 | case Stmt::WhileStmtClass: |
Ted Kremenek | 706e3cf | 2008-04-07 23:35:17 +0000 | [diff] [blame] | 295 | case Stmt::ForStmtClass: { |
| 296 | |
| 297 | if (*(Src->succ_begin()+1) == Dst) { |
| 298 | |
| 299 | std::ostringstream os; |
| 300 | |
| 301 | os << "Loop condition is false. Execution continues on line " |
| 302 | << SMgr.getLogicalLineNumber(GetStmt(Dst)->getLocStart()) << '.'; |
| 303 | |
| 304 | PD.push_front(new PathDiagnosticPiece(L, os.str())); |
| 305 | } |
| 306 | else |
| 307 | PD.push_front(new PathDiagnosticPiece(L, |
| 308 | "Loop condition is true. Entering loop body.")); |
| 309 | |
| 310 | break; |
| 311 | } |
| 312 | |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 313 | case Stmt::IfStmtClass: { |
| 314 | |
| 315 | if (*(Src->succ_begin()+1) == Dst) |
| 316 | PD.push_front(new PathDiagnosticPiece(L, "Taking false branch.")); |
| 317 | else |
| 318 | PD.push_front(new PathDiagnosticPiece(L, "Taking true branch.")); |
| 319 | |
| 320 | break; |
| 321 | } |
| 322 | } |
Ted Kremenek | 6837faa | 2008-04-09 00:20:43 +0000 | [diff] [blame] | 323 | } |
| 324 | else |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 325 | if (PathDiagnosticPiece* piece = R.VisitNode(N, NextNode, *GTrim, Ctx)) |
| 326 | PD.push_front(piece); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
| 330 | bool BugReporter::IsCached(ExplodedNode<ValueState>* N) { |
| 331 | |
| 332 | // HACK: Cache the location of the error. Don't emit the same |
| 333 | // warning for the same error type that occurs at the same program |
| 334 | // location but along a different path. |
| 335 | |
| 336 | void* p = N->getLocation().getRawData(); |
| 337 | |
| 338 | if (CachedErrors.count(p)) |
| 339 | return true; |
| 340 | |
| 341 | CachedErrors.insert(p); |
| 342 | |
| 343 | return false; |
| 344 | } |
| 345 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 346 | void BugReporter::EmitPathWarning(BugReport& R) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 348 | ExplodedNode<ValueState>* N = R.getEndNode(); |
| 349 | |
| 350 | if (!PD || !N) { |
| 351 | EmitWarning(R); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | |
| 355 | if (IsCached(N)) |
| 356 | return; |
| 357 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 358 | PathDiagnostic D(R.getName()); |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 359 | GeneratePathDiagnostic(D, R); |
Ted Kremenek | 70d1722 | 2008-04-03 07:33:55 +0000 | [diff] [blame] | 360 | |
| 361 | if (!D.empty()) |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 362 | PD->HandlePathDiagnostic(D); |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 365 | void BugReporter::EmitWarning(BugReport& R) { |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 367 | ExplodedNode<ValueState>* N = R.getEndNode(); |
| 368 | |
| 369 | if (N && IsCached(N)) |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 370 | return; |
| 371 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 372 | FullSourceLoc L = R.getLocation(Ctx.getSourceManager()); |
Ted Kremenek | 5fcca68 | 2008-04-14 18:06:42 +0000 | [diff] [blame^] | 373 | |
Ted Kremenek | 4bb6ac2 | 2008-04-10 16:12:38 +0000 | [diff] [blame] | 374 | const SourceRange *Beg, *End; |
| 375 | R.getRanges(Beg, End); |
Ted Kremenek | 5fcca68 | 2008-04-14 18:06:42 +0000 | [diff] [blame^] | 376 | |
| 377 | if (!PD) { |
| 378 | |
| 379 | std::ostringstream os; |
| 380 | os << "[CHECKER] " << R.getDescription(); |
| 381 | |
| 382 | unsigned ErrorDiag = Diag.getCustomDiagID(Diagnostic::Warning, |
| 383 | os.str().c_str()); |
| 384 | |
| 385 | Diag.Report(L, ErrorDiag, NULL, 0, Beg, End - Beg); |
| 386 | } |
| 387 | else { |
| 388 | PathDiagnostic D(R.getName()); |
| 389 | PathDiagnosticPiece* piece = new PathDiagnosticPiece(L, R.getDescription()); |
| 390 | |
| 391 | for ( ; Beg != End; ++Beg) |
| 392 | piece->addRange(*Beg); |
| 393 | |
| 394 | D.push_back(piece); |
| 395 | PD->HandlePathDiagnostic(D); |
| 396 | } |
Ted Kremenek | 61f3e05 | 2008-04-03 04:42:52 +0000 | [diff] [blame] | 397 | } |