BugReporter.cpp revision 0cf3d471546251b12bdceff360f66c079c40526c
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. 12// 13//===----------------------------------------------------------------------===// 14 15#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 16#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 17#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 18#include "clang/AST/ASTContext.h" 19#include "clang/Analysis/CFG.h" 20#include "clang/AST/DeclObjC.h" 21#include "clang/AST/Expr.h" 22#include "clang/AST/ParentMap.h" 23#include "clang/AST/StmtObjC.h" 24#include "clang/Basic/Diagnostic.h" 25#include "clang/Basic/SourceManager.h" 26#include "clang/Analysis/ProgramPoint.h" 27#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" 28#include "llvm/Support/raw_ostream.h" 29#include "llvm/ADT/DenseMap.h" 30#include "llvm/ADT/SmallString.h" 31#include "llvm/ADT/STLExtras.h" 32#include "llvm/ADT/OwningPtr.h" 33#include <queue> 34 35using namespace clang; 36using namespace ento; 37 38BugReporterVisitor::~BugReporterVisitor() {} 39 40void BugReporterContext::anchor() {} 41 42//===----------------------------------------------------------------------===// 43// Helper routines for walking the ExplodedGraph and fetching statements. 44//===----------------------------------------------------------------------===// 45 46static inline const Stmt *GetStmt(const ProgramPoint &P) { 47 if (const StmtPoint* SP = dyn_cast<StmtPoint>(&P)) 48 return SP->getStmt(); 49 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) 50 return BE->getSrc()->getTerminator(); 51 52 return 0; 53} 54 55static inline const ExplodedNode* 56GetPredecessorNode(const ExplodedNode *N) { 57 return N->pred_empty() ? NULL : *(N->pred_begin()); 58} 59 60static inline const ExplodedNode* 61GetSuccessorNode(const ExplodedNode *N) { 62 return N->succ_empty() ? NULL : *(N->succ_begin()); 63} 64 65static const Stmt *GetPreviousStmt(const ExplodedNode *N) { 66 for (N = GetPredecessorNode(N); N; N = GetPredecessorNode(N)) 67 if (const Stmt *S = GetStmt(N->getLocation())) 68 return S; 69 70 return 0; 71} 72 73static const Stmt *GetNextStmt(const ExplodedNode *N) { 74 for (N = GetSuccessorNode(N); N; N = GetSuccessorNode(N)) 75 if (const Stmt *S = GetStmt(N->getLocation())) { 76 // Check if the statement is '?' or '&&'/'||'. These are "merges", 77 // not actual statement points. 78 switch (S->getStmtClass()) { 79 case Stmt::ChooseExprClass: 80 case Stmt::BinaryConditionalOperatorClass: continue; 81 case Stmt::ConditionalOperatorClass: continue; 82 case Stmt::BinaryOperatorClass: { 83 BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode(); 84 if (Op == BO_LAnd || Op == BO_LOr) 85 continue; 86 break; 87 } 88 default: 89 break; 90 } 91 return S; 92 } 93 94 return 0; 95} 96 97static inline const Stmt* 98GetCurrentOrPreviousStmt(const ExplodedNode *N) { 99 if (const Stmt *S = GetStmt(N->getLocation())) 100 return S; 101 102 return GetPreviousStmt(N); 103} 104 105static inline const Stmt* 106GetCurrentOrNextStmt(const ExplodedNode *N) { 107 if (const Stmt *S = GetStmt(N->getLocation())) 108 return S; 109 110 return GetNextStmt(N); 111} 112 113//===----------------------------------------------------------------------===// 114// PathDiagnosticBuilder and its associated routines and helper objects. 115//===----------------------------------------------------------------------===// 116 117typedef llvm::DenseMap<const ExplodedNode*, 118const ExplodedNode*> NodeBackMap; 119 120namespace { 121class NodeMapClosure : public BugReport::NodeResolver { 122 NodeBackMap& M; 123public: 124 NodeMapClosure(NodeBackMap *m) : M(*m) {} 125 ~NodeMapClosure() {} 126 127 const ExplodedNode *getOriginalNode(const ExplodedNode *N) { 128 NodeBackMap::iterator I = M.find(N); 129 return I == M.end() ? 0 : I->second; 130 } 131}; 132 133class PathDiagnosticBuilder : public BugReporterContext { 134 BugReport *R; 135 PathDiagnosticConsumer *PDC; 136 OwningPtr<ParentMap> PM; 137 NodeMapClosure NMC; 138public: 139 PathDiagnosticBuilder(GRBugReporter &br, 140 BugReport *r, NodeBackMap *Backmap, 141 PathDiagnosticConsumer *pdc) 142 : BugReporterContext(br), 143 R(r), PDC(pdc), NMC(Backmap) {} 144 145 PathDiagnosticLocation ExecutionContinues(const ExplodedNode *N); 146 147 PathDiagnosticLocation ExecutionContinues(llvm::raw_string_ostream &os, 148 const ExplodedNode *N); 149 150 BugReport *getBugReport() { return R; } 151 152 Decl const &getCodeDecl() { return R->getErrorNode()->getCodeDecl(); } 153 154 const LocationContext* getLocationContext() { 155 return R->getErrorNode()->getLocationContext(); 156 } 157 158 ParentMap& getParentMap() { return R->getErrorNode()->getParentMap(); } 159 160 const Stmt *getParent(const Stmt *S) { 161 return getParentMap().getParent(S); 162 } 163 164 virtual NodeMapClosure& getNodeResolver() { return NMC; } 165 166 PathDiagnosticLocation getEnclosingStmtLocation(const Stmt *S); 167 168 PathDiagnosticConsumer::PathGenerationScheme getGenerationScheme() const { 169 return PDC ? PDC->getGenerationScheme() : PathDiagnosticConsumer::Extensive; 170 } 171 172 bool supportsLogicalOpControlFlow() const { 173 return PDC ? PDC->supportsLogicalOpControlFlow() : true; 174 } 175}; 176} // end anonymous namespace 177 178PathDiagnosticLocation 179PathDiagnosticBuilder::ExecutionContinues(const ExplodedNode *N) { 180 if (const Stmt *S = GetNextStmt(N)) 181 return PathDiagnosticLocation(S, getSourceManager(), getLocationContext()); 182 183 return PathDiagnosticLocation::createDeclEnd(N->getLocationContext(), 184 getSourceManager()); 185} 186 187PathDiagnosticLocation 188PathDiagnosticBuilder::ExecutionContinues(llvm::raw_string_ostream &os, 189 const ExplodedNode *N) { 190 191 // Slow, but probably doesn't matter. 192 if (os.str().empty()) 193 os << ' '; 194 195 const PathDiagnosticLocation &Loc = ExecutionContinues(N); 196 197 if (Loc.asStmt()) 198 os << "Execution continues on line " 199 << getSourceManager().getExpansionLineNumber(Loc.asLocation()) 200 << '.'; 201 else { 202 os << "Execution jumps to the end of the "; 203 const Decl *D = N->getLocationContext()->getDecl(); 204 if (isa<ObjCMethodDecl>(D)) 205 os << "method"; 206 else if (isa<FunctionDecl>(D)) 207 os << "function"; 208 else { 209 assert(isa<BlockDecl>(D)); 210 os << "anonymous block"; 211 } 212 os << '.'; 213 } 214 215 return Loc; 216} 217 218static bool IsNested(const Stmt *S, ParentMap &PM) { 219 if (isa<Expr>(S) && PM.isConsumedExpr(cast<Expr>(S))) 220 return true; 221 222 const Stmt *Parent = PM.getParentIgnoreParens(S); 223 224 if (Parent) 225 switch (Parent->getStmtClass()) { 226 case Stmt::ForStmtClass: 227 case Stmt::DoStmtClass: 228 case Stmt::WhileStmtClass: 229 return true; 230 default: 231 break; 232 } 233 234 return false; 235} 236 237PathDiagnosticLocation 238PathDiagnosticBuilder::getEnclosingStmtLocation(const Stmt *S) { 239 assert(S && "Null Stmt *passed to getEnclosingStmtLocation"); 240 ParentMap &P = getParentMap(); 241 SourceManager &SMgr = getSourceManager(); 242 const LocationContext *LC = getLocationContext(); 243 244 while (IsNested(S, P)) { 245 const Stmt *Parent = P.getParentIgnoreParens(S); 246 247 if (!Parent) 248 break; 249 250 switch (Parent->getStmtClass()) { 251 case Stmt::BinaryOperatorClass: { 252 const BinaryOperator *B = cast<BinaryOperator>(Parent); 253 if (B->isLogicalOp()) 254 return PathDiagnosticLocation(S, SMgr, LC); 255 break; 256 } 257 case Stmt::CompoundStmtClass: 258 case Stmt::StmtExprClass: 259 return PathDiagnosticLocation(S, SMgr, LC); 260 case Stmt::ChooseExprClass: 261 // Similar to '?' if we are referring to condition, just have the edge 262 // point to the entire choose expression. 263 if (cast<ChooseExpr>(Parent)->getCond() == S) 264 return PathDiagnosticLocation(Parent, SMgr, LC); 265 else 266 return PathDiagnosticLocation(S, SMgr, LC); 267 case Stmt::BinaryConditionalOperatorClass: 268 case Stmt::ConditionalOperatorClass: 269 // For '?', if we are referring to condition, just have the edge point 270 // to the entire '?' expression. 271 if (cast<AbstractConditionalOperator>(Parent)->getCond() == S) 272 return PathDiagnosticLocation(Parent, SMgr, LC); 273 else 274 return PathDiagnosticLocation(S, SMgr, LC); 275 case Stmt::DoStmtClass: 276 return PathDiagnosticLocation(S, SMgr, LC); 277 case Stmt::ForStmtClass: 278 if (cast<ForStmt>(Parent)->getBody() == S) 279 return PathDiagnosticLocation(S, SMgr, LC); 280 break; 281 case Stmt::IfStmtClass: 282 if (cast<IfStmt>(Parent)->getCond() != S) 283 return PathDiagnosticLocation(S, SMgr, LC); 284 break; 285 case Stmt::ObjCForCollectionStmtClass: 286 if (cast<ObjCForCollectionStmt>(Parent)->getBody() == S) 287 return PathDiagnosticLocation(S, SMgr, LC); 288 break; 289 case Stmt::WhileStmtClass: 290 if (cast<WhileStmt>(Parent)->getCond() != S) 291 return PathDiagnosticLocation(S, SMgr, LC); 292 break; 293 default: 294 break; 295 } 296 297 S = Parent; 298 } 299 300 assert(S && "Cannot have null Stmt for PathDiagnosticLocation"); 301 302 // Special case: DeclStmts can appear in for statement declarations, in which 303 // case the ForStmt is the context. 304 if (isa<DeclStmt>(S)) { 305 if (const Stmt *Parent = P.getParent(S)) { 306 switch (Parent->getStmtClass()) { 307 case Stmt::ForStmtClass: 308 case Stmt::ObjCForCollectionStmtClass: 309 return PathDiagnosticLocation(Parent, SMgr, LC); 310 default: 311 break; 312 } 313 } 314 } 315 else if (isa<BinaryOperator>(S)) { 316 // Special case: the binary operator represents the initialization 317 // code in a for statement (this can happen when the variable being 318 // initialized is an old variable. 319 if (const ForStmt *FS = 320 dyn_cast_or_null<ForStmt>(P.getParentIgnoreParens(S))) { 321 if (FS->getInit() == S) 322 return PathDiagnosticLocation(FS, SMgr, LC); 323 } 324 } 325 326 return PathDiagnosticLocation(S, SMgr, LC); 327} 328 329//===----------------------------------------------------------------------===// 330// ScanNotableSymbols: closure-like callback for scanning Store bindings. 331//===----------------------------------------------------------------------===// 332 333static const VarDecl* GetMostRecentVarDeclBinding(const ExplodedNode *N, 334 ProgramStateManager& VMgr, 335 SVal X) { 336 337 for ( ; N ; N = N->pred_empty() ? 0 : *N->pred_begin()) { 338 339 ProgramPoint P = N->getLocation(); 340 341 if (!isa<PostStmt>(P)) 342 continue; 343 344 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(cast<PostStmt>(P).getStmt()); 345 346 if (!DR) 347 continue; 348 349 SVal Y = N->getState()->getSVal(DR, N->getLocationContext()); 350 351 if (X != Y) 352 continue; 353 354 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); 355 356 if (!VD) 357 continue; 358 359 return VD; 360 } 361 362 return 0; 363} 364 365namespace { 366class NotableSymbolHandler 367: public StoreManager::BindingsHandler { 368 369 SymbolRef Sym; 370 ProgramStateRef PrevSt; 371 const Stmt *S; 372 ProgramStateManager& VMgr; 373 const ExplodedNode *Pred; 374 PathDiagnostic& PD; 375 BugReporter& BR; 376 377public: 378 379 NotableSymbolHandler(SymbolRef sym, 380 ProgramStateRef prevst, 381 const Stmt *s, 382 ProgramStateManager& vmgr, 383 const ExplodedNode *pred, 384 PathDiagnostic& pd, 385 BugReporter& br) 386 : Sym(sym), 387 PrevSt(prevst), 388 S(s), 389 VMgr(vmgr), 390 Pred(pred), 391 PD(pd), 392 BR(br) {} 393 394 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, 395 SVal V) { 396 397 SymbolRef ScanSym = V.getAsSymbol(); 398 399 if (ScanSym != Sym) 400 return true; 401 402 // Check if the previous state has this binding. 403 SVal X = PrevSt->getSVal(loc::MemRegionVal(R)); 404 405 if (X == V) // Same binding? 406 return true; 407 408 // Different binding. Only handle assignments for now. We don't pull 409 // this check out of the loop because we will eventually handle other 410 // cases. 411 412 VarDecl *VD = 0; 413 414 if (const BinaryOperator* B = dyn_cast<BinaryOperator>(S)) { 415 if (!B->isAssignmentOp()) 416 return true; 417 418 // What variable did we assign to? 419 DeclRefExpr *DR = dyn_cast<DeclRefExpr>(B->getLHS()->IgnoreParenCasts()); 420 421 if (!DR) 422 return true; 423 424 VD = dyn_cast<VarDecl>(DR->getDecl()); 425 } 426 else if (const DeclStmt *DS = dyn_cast<DeclStmt>(S)) { 427 // FIXME: Eventually CFGs won't have DeclStmts. Right now we 428 // assume that each DeclStmt has a single Decl. This invariant 429 // holds by construction in the CFG. 430 VD = dyn_cast<VarDecl>(*DS->decl_begin()); 431 } 432 433 if (!VD) 434 return true; 435 436 // What is the most recently referenced variable with this binding? 437 const VarDecl *MostRecent = GetMostRecentVarDeclBinding(Pred, VMgr, V); 438 439 if (!MostRecent) 440 return true; 441 442 // Create the diagnostic. 443 if (Loc::isLocType(VD->getType())) { 444 SmallString<64> buf; 445 llvm::raw_svector_ostream os(buf); 446 os << '\'' << *VD << "' now aliases '" << *MostRecent << '\''; 447 PathDiagnosticLocation L = 448 PathDiagnosticLocation::createBegin(S, BR.getSourceManager(), 449 Pred->getLocationContext()); 450 PD.push_front(new PathDiagnosticEventPiece(L, os.str())); 451 } 452 453 return true; 454 } 455}; 456} 457 458static void HandleNotableSymbol(const ExplodedNode *N, 459 const Stmt *S, 460 SymbolRef Sym, BugReporter& BR, 461 PathDiagnostic& PD) { 462 463 const ExplodedNode *Pred = N->pred_empty() ? 0 : *N->pred_begin(); 464 ProgramStateRef PrevSt = Pred ? Pred->getState() : 0; 465 466 if (!PrevSt) 467 return; 468 469 // Look at the region bindings of the current state that map to the 470 // specified symbol. Are any of them not in the previous state? 471 ProgramStateManager& VMgr = cast<GRBugReporter>(BR).getStateManager(); 472 NotableSymbolHandler H(Sym, PrevSt, S, VMgr, Pred, PD, BR); 473 cast<GRBugReporter>(BR).getStateManager().iterBindings(N->getState(), H); 474} 475 476namespace { 477class ScanNotableSymbols 478: public StoreManager::BindingsHandler { 479 480 llvm::SmallSet<SymbolRef, 10> AlreadyProcessed; 481 const ExplodedNode *N; 482 const Stmt *S; 483 GRBugReporter& BR; 484 PathDiagnostic& PD; 485 486public: 487 ScanNotableSymbols(const ExplodedNode *n, const Stmt *s, 488 GRBugReporter& br, PathDiagnostic& pd) 489 : N(n), S(s), BR(br), PD(pd) {} 490 491 bool HandleBinding(StoreManager& SMgr, Store store, 492 const MemRegion* R, SVal V) { 493 494 SymbolRef ScanSym = V.getAsSymbol(); 495 496 if (!ScanSym) 497 return true; 498 499 if (!BR.isNotable(ScanSym)) 500 return true; 501 502 if (AlreadyProcessed.count(ScanSym)) 503 return true; 504 505 AlreadyProcessed.insert(ScanSym); 506 507 HandleNotableSymbol(N, S, ScanSym, BR, PD); 508 return true; 509 } 510}; 511} // end anonymous namespace 512 513//===----------------------------------------------------------------------===// 514// "Minimal" path diagnostic generation algorithm. 515//===----------------------------------------------------------------------===// 516 517static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM); 518 519static void GenerateMinimalPathDiagnostic(PathDiagnostic& PD, 520 PathDiagnosticBuilder &PDB, 521 const ExplodedNode *N) { 522 523 SourceManager& SMgr = PDB.getSourceManager(); 524 const LocationContext *LC = PDB.getLocationContext(); 525 const ExplodedNode *NextNode = N->pred_empty() 526 ? NULL : *(N->pred_begin()); 527 while (NextNode) { 528 N = NextNode; 529 NextNode = GetPredecessorNode(N); 530 531 ProgramPoint P = N->getLocation(); 532 533 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { 534 const CFGBlock *Src = BE->getSrc(); 535 const CFGBlock *Dst = BE->getDst(); 536 const Stmt *T = Src->getTerminator(); 537 538 if (!T) 539 continue; 540 541 PathDiagnosticLocation Start = 542 PathDiagnosticLocation::createBegin(T, SMgr, 543 N->getLocationContext()); 544 545 switch (T->getStmtClass()) { 546 default: 547 break; 548 549 case Stmt::GotoStmtClass: 550 case Stmt::IndirectGotoStmtClass: { 551 const Stmt *S = GetNextStmt(N); 552 553 if (!S) 554 continue; 555 556 std::string sbuf; 557 llvm::raw_string_ostream os(sbuf); 558 const PathDiagnosticLocation &End = PDB.getEnclosingStmtLocation(S); 559 560 os << "Control jumps to line " 561 << End.asLocation().getExpansionLineNumber(); 562 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 563 os.str())); 564 break; 565 } 566 567 case Stmt::SwitchStmtClass: { 568 // Figure out what case arm we took. 569 std::string sbuf; 570 llvm::raw_string_ostream os(sbuf); 571 572 if (const Stmt *S = Dst->getLabel()) { 573 PathDiagnosticLocation End(S, SMgr, LC); 574 575 switch (S->getStmtClass()) { 576 default: 577 os << "No cases match in the switch statement. " 578 "Control jumps to line " 579 << End.asLocation().getExpansionLineNumber(); 580 break; 581 case Stmt::DefaultStmtClass: 582 os << "Control jumps to the 'default' case at line " 583 << End.asLocation().getExpansionLineNumber(); 584 break; 585 586 case Stmt::CaseStmtClass: { 587 os << "Control jumps to 'case "; 588 const CaseStmt *Case = cast<CaseStmt>(S); 589 const Expr *LHS = Case->getLHS()->IgnoreParenCasts(); 590 591 // Determine if it is an enum. 592 bool GetRawInt = true; 593 594 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(LHS)) { 595 // FIXME: Maybe this should be an assertion. Are there cases 596 // were it is not an EnumConstantDecl? 597 const EnumConstantDecl *D = 598 dyn_cast<EnumConstantDecl>(DR->getDecl()); 599 600 if (D) { 601 GetRawInt = false; 602 os << *D; 603 } 604 } 605 606 if (GetRawInt) 607 os << LHS->EvaluateKnownConstInt(PDB.getASTContext()); 608 609 os << ":' at line " 610 << End.asLocation().getExpansionLineNumber(); 611 break; 612 } 613 } 614 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 615 os.str())); 616 } 617 else { 618 os << "'Default' branch taken. "; 619 const PathDiagnosticLocation &End = PDB.ExecutionContinues(os, N); 620 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 621 os.str())); 622 } 623 624 break; 625 } 626 627 case Stmt::BreakStmtClass: 628 case Stmt::ContinueStmtClass: { 629 std::string sbuf; 630 llvm::raw_string_ostream os(sbuf); 631 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); 632 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 633 os.str())); 634 break; 635 } 636 637 // Determine control-flow for ternary '?'. 638 case Stmt::BinaryConditionalOperatorClass: 639 case Stmt::ConditionalOperatorClass: { 640 std::string sbuf; 641 llvm::raw_string_ostream os(sbuf); 642 os << "'?' condition is "; 643 644 if (*(Src->succ_begin()+1) == Dst) 645 os << "false"; 646 else 647 os << "true"; 648 649 PathDiagnosticLocation End = PDB.ExecutionContinues(N); 650 651 if (const Stmt *S = End.asStmt()) 652 End = PDB.getEnclosingStmtLocation(S); 653 654 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 655 os.str())); 656 break; 657 } 658 659 // Determine control-flow for short-circuited '&&' and '||'. 660 case Stmt::BinaryOperatorClass: { 661 if (!PDB.supportsLogicalOpControlFlow()) 662 break; 663 664 const BinaryOperator *B = cast<BinaryOperator>(T); 665 std::string sbuf; 666 llvm::raw_string_ostream os(sbuf); 667 os << "Left side of '"; 668 669 if (B->getOpcode() == BO_LAnd) { 670 os << "&&" << "' is "; 671 672 if (*(Src->succ_begin()+1) == Dst) { 673 os << "false"; 674 PathDiagnosticLocation End(B->getLHS(), SMgr, LC); 675 PathDiagnosticLocation Start = 676 PathDiagnosticLocation::createOperatorLoc(B, SMgr); 677 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 678 os.str())); 679 } 680 else { 681 os << "true"; 682 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC); 683 PathDiagnosticLocation End = PDB.ExecutionContinues(N); 684 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 685 os.str())); 686 } 687 } 688 else { 689 assert(B->getOpcode() == BO_LOr); 690 os << "||" << "' is "; 691 692 if (*(Src->succ_begin()+1) == Dst) { 693 os << "false"; 694 PathDiagnosticLocation Start(B->getLHS(), SMgr, LC); 695 PathDiagnosticLocation End = PDB.ExecutionContinues(N); 696 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 697 os.str())); 698 } 699 else { 700 os << "true"; 701 PathDiagnosticLocation End(B->getLHS(), SMgr, LC); 702 PathDiagnosticLocation Start = 703 PathDiagnosticLocation::createOperatorLoc(B, SMgr); 704 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 705 os.str())); 706 } 707 } 708 709 break; 710 } 711 712 case Stmt::DoStmtClass: { 713 if (*(Src->succ_begin()) == Dst) { 714 std::string sbuf; 715 llvm::raw_string_ostream os(sbuf); 716 717 os << "Loop condition is true. "; 718 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); 719 720 if (const Stmt *S = End.asStmt()) 721 End = PDB.getEnclosingStmtLocation(S); 722 723 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 724 os.str())); 725 } 726 else { 727 PathDiagnosticLocation End = PDB.ExecutionContinues(N); 728 729 if (const Stmt *S = End.asStmt()) 730 End = PDB.getEnclosingStmtLocation(S); 731 732 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 733 "Loop condition is false. Exiting loop")); 734 } 735 736 break; 737 } 738 739 case Stmt::WhileStmtClass: 740 case Stmt::ForStmtClass: { 741 if (*(Src->succ_begin()+1) == Dst) { 742 std::string sbuf; 743 llvm::raw_string_ostream os(sbuf); 744 745 os << "Loop condition is false. "; 746 PathDiagnosticLocation End = PDB.ExecutionContinues(os, N); 747 if (const Stmt *S = End.asStmt()) 748 End = PDB.getEnclosingStmtLocation(S); 749 750 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 751 os.str())); 752 } 753 else { 754 PathDiagnosticLocation End = PDB.ExecutionContinues(N); 755 if (const Stmt *S = End.asStmt()) 756 End = PDB.getEnclosingStmtLocation(S); 757 758 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 759 "Loop condition is true. Entering loop body")); 760 } 761 762 break; 763 } 764 765 case Stmt::IfStmtClass: { 766 PathDiagnosticLocation End = PDB.ExecutionContinues(N); 767 768 if (const Stmt *S = End.asStmt()) 769 End = PDB.getEnclosingStmtLocation(S); 770 771 if (*(Src->succ_begin()+1) == Dst) 772 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 773 "Taking false branch")); 774 else 775 PD.push_front(new PathDiagnosticControlFlowPiece(Start, End, 776 "Taking true branch")); 777 778 break; 779 } 780 } 781 } 782 783 if (NextNode) { 784 // Add diagnostic pieces from custom visitors. 785 BugReport *R = PDB.getBugReport(); 786 for (BugReport::visitor_iterator I = R->visitor_begin(), 787 E = R->visitor_end(); I!=E; ++I) { 788 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) 789 PD.push_front(p); 790 } 791 } 792 793 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) { 794 // Scan the region bindings, and see if a "notable" symbol has a new 795 // lval binding. 796 ScanNotableSymbols SNS(N, PS->getStmt(), PDB.getBugReporter(), PD); 797 PDB.getStateManager().iterBindings(N->getState(), SNS); 798 } 799 } 800 801 // After constructing the full PathDiagnostic, do a pass over it to compact 802 // PathDiagnosticPieces that occur within a macro. 803 CompactPathDiagnostic(PD, PDB.getSourceManager()); 804} 805 806//===----------------------------------------------------------------------===// 807// "Extensive" PathDiagnostic generation. 808//===----------------------------------------------------------------------===// 809 810static bool IsControlFlowExpr(const Stmt *S) { 811 const Expr *E = dyn_cast<Expr>(S); 812 813 if (!E) 814 return false; 815 816 E = E->IgnoreParenCasts(); 817 818 if (isa<AbstractConditionalOperator>(E)) 819 return true; 820 821 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(E)) 822 if (B->isLogicalOp()) 823 return true; 824 825 return false; 826} 827 828namespace { 829class ContextLocation : public PathDiagnosticLocation { 830 bool IsDead; 831public: 832 ContextLocation(const PathDiagnosticLocation &L, bool isdead = false) 833 : PathDiagnosticLocation(L), IsDead(isdead) {} 834 835 void markDead() { IsDead = true; } 836 bool isDead() const { return IsDead; } 837}; 838 839class EdgeBuilder { 840 std::vector<ContextLocation> CLocs; 841 typedef std::vector<ContextLocation>::iterator iterator; 842 PathDiagnostic &PD; 843 PathDiagnosticBuilder &PDB; 844 PathDiagnosticLocation PrevLoc; 845 846 bool IsConsumedExpr(const PathDiagnosticLocation &L); 847 848 bool containsLocation(const PathDiagnosticLocation &Container, 849 const PathDiagnosticLocation &Containee); 850 851 PathDiagnosticLocation getContextLocation(const PathDiagnosticLocation &L); 852 853 PathDiagnosticLocation cleanUpLocation(PathDiagnosticLocation L, 854 bool firstCharOnly = false) { 855 if (const Stmt *S = L.asStmt()) { 856 const Stmt *Original = S; 857 while (1) { 858 // Adjust the location for some expressions that are best referenced 859 // by one of their subexpressions. 860 switch (S->getStmtClass()) { 861 default: 862 break; 863 case Stmt::ParenExprClass: 864 case Stmt::GenericSelectionExprClass: 865 S = cast<Expr>(S)->IgnoreParens(); 866 firstCharOnly = true; 867 continue; 868 case Stmt::BinaryConditionalOperatorClass: 869 case Stmt::ConditionalOperatorClass: 870 S = cast<AbstractConditionalOperator>(S)->getCond(); 871 firstCharOnly = true; 872 continue; 873 case Stmt::ChooseExprClass: 874 S = cast<ChooseExpr>(S)->getCond(); 875 firstCharOnly = true; 876 continue; 877 case Stmt::BinaryOperatorClass: 878 S = cast<BinaryOperator>(S)->getLHS(); 879 firstCharOnly = true; 880 continue; 881 } 882 883 break; 884 } 885 886 if (S != Original) 887 L = PathDiagnosticLocation(S, L.getManager(), PDB.getLocationContext()); 888 } 889 890 if (firstCharOnly) 891 L = PathDiagnosticLocation::createSingleLocation(L); 892 893 return L; 894 } 895 896 void popLocation() { 897 if (!CLocs.back().isDead() && CLocs.back().asLocation().isFileID()) { 898 // For contexts, we only one the first character as the range. 899 rawAddEdge(cleanUpLocation(CLocs.back(), true)); 900 } 901 CLocs.pop_back(); 902 } 903 904public: 905 EdgeBuilder(PathDiagnostic &pd, PathDiagnosticBuilder &pdb) 906 : PD(pd), PDB(pdb) { 907 908 // If the PathDiagnostic already has pieces, add the enclosing statement 909 // of the first piece as a context as well. 910 if (!PD.empty()) { 911 PrevLoc = PD.begin()->getLocation(); 912 913 if (const Stmt *S = PrevLoc.asStmt()) 914 addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt()); 915 } 916 } 917 918 ~EdgeBuilder() { 919 while (!CLocs.empty()) popLocation(); 920 921 // Finally, add an initial edge from the start location of the first 922 // statement (if it doesn't already exist). 923 PathDiagnosticLocation L = PathDiagnosticLocation::createDeclBegin( 924 PDB.getLocationContext(), 925 PDB.getSourceManager()); 926 if (L.isValid()) 927 rawAddEdge(L); 928 } 929 930 void addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd = false); 931 932 void rawAddEdge(PathDiagnosticLocation NewLoc); 933 934 void addContext(const Stmt *S); 935 void addExtendedContext(const Stmt *S); 936}; 937} // end anonymous namespace 938 939 940PathDiagnosticLocation 941EdgeBuilder::getContextLocation(const PathDiagnosticLocation &L) { 942 if (const Stmt *S = L.asStmt()) { 943 if (IsControlFlowExpr(S)) 944 return L; 945 946 return PDB.getEnclosingStmtLocation(S); 947 } 948 949 return L; 950} 951 952bool EdgeBuilder::containsLocation(const PathDiagnosticLocation &Container, 953 const PathDiagnosticLocation &Containee) { 954 955 if (Container == Containee) 956 return true; 957 958 if (Container.asDecl()) 959 return true; 960 961 if (const Stmt *S = Containee.asStmt()) 962 if (const Stmt *ContainerS = Container.asStmt()) { 963 while (S) { 964 if (S == ContainerS) 965 return true; 966 S = PDB.getParent(S); 967 } 968 return false; 969 } 970 971 // Less accurate: compare using source ranges. 972 SourceRange ContainerR = Container.asRange(); 973 SourceRange ContaineeR = Containee.asRange(); 974 975 SourceManager &SM = PDB.getSourceManager(); 976 SourceLocation ContainerRBeg = SM.getExpansionLoc(ContainerR.getBegin()); 977 SourceLocation ContainerREnd = SM.getExpansionLoc(ContainerR.getEnd()); 978 SourceLocation ContaineeRBeg = SM.getExpansionLoc(ContaineeR.getBegin()); 979 SourceLocation ContaineeREnd = SM.getExpansionLoc(ContaineeR.getEnd()); 980 981 unsigned ContainerBegLine = SM.getExpansionLineNumber(ContainerRBeg); 982 unsigned ContainerEndLine = SM.getExpansionLineNumber(ContainerREnd); 983 unsigned ContaineeBegLine = SM.getExpansionLineNumber(ContaineeRBeg); 984 unsigned ContaineeEndLine = SM.getExpansionLineNumber(ContaineeREnd); 985 986 assert(ContainerBegLine <= ContainerEndLine); 987 assert(ContaineeBegLine <= ContaineeEndLine); 988 989 return (ContainerBegLine <= ContaineeBegLine && 990 ContainerEndLine >= ContaineeEndLine && 991 (ContainerBegLine != ContaineeBegLine || 992 SM.getExpansionColumnNumber(ContainerRBeg) <= 993 SM.getExpansionColumnNumber(ContaineeRBeg)) && 994 (ContainerEndLine != ContaineeEndLine || 995 SM.getExpansionColumnNumber(ContainerREnd) >= 996 SM.getExpansionColumnNumber(ContainerREnd))); 997} 998 999void EdgeBuilder::rawAddEdge(PathDiagnosticLocation NewLoc) { 1000 if (!PrevLoc.isValid()) { 1001 PrevLoc = NewLoc; 1002 return; 1003 } 1004 1005 const PathDiagnosticLocation &NewLocClean = cleanUpLocation(NewLoc); 1006 const PathDiagnosticLocation &PrevLocClean = cleanUpLocation(PrevLoc); 1007 1008 if (NewLocClean.asLocation() == PrevLocClean.asLocation()) 1009 return; 1010 1011 // FIXME: Ignore intra-macro edges for now. 1012 if (NewLocClean.asLocation().getExpansionLoc() == 1013 PrevLocClean.asLocation().getExpansionLoc()) 1014 return; 1015 1016 PD.push_front(new PathDiagnosticControlFlowPiece(NewLocClean, PrevLocClean)); 1017 PrevLoc = NewLoc; 1018} 1019 1020void EdgeBuilder::addEdge(PathDiagnosticLocation NewLoc, bool alwaysAdd) { 1021 1022 if (!alwaysAdd && NewLoc.asLocation().isMacroID()) 1023 return; 1024 1025 const PathDiagnosticLocation &CLoc = getContextLocation(NewLoc); 1026 1027 while (!CLocs.empty()) { 1028 ContextLocation &TopContextLoc = CLocs.back(); 1029 1030 // Is the top location context the same as the one for the new location? 1031 if (TopContextLoc == CLoc) { 1032 if (alwaysAdd) { 1033 if (IsConsumedExpr(TopContextLoc) && 1034 !IsControlFlowExpr(TopContextLoc.asStmt())) 1035 TopContextLoc.markDead(); 1036 1037 rawAddEdge(NewLoc); 1038 } 1039 1040 return; 1041 } 1042 1043 if (containsLocation(TopContextLoc, CLoc)) { 1044 if (alwaysAdd) { 1045 rawAddEdge(NewLoc); 1046 1047 if (IsConsumedExpr(CLoc) && !IsControlFlowExpr(CLoc.asStmt())) { 1048 CLocs.push_back(ContextLocation(CLoc, true)); 1049 return; 1050 } 1051 } 1052 1053 CLocs.push_back(CLoc); 1054 return; 1055 } 1056 1057 // Context does not contain the location. Flush it. 1058 popLocation(); 1059 } 1060 1061 // If we reach here, there is no enclosing context. Just add the edge. 1062 rawAddEdge(NewLoc); 1063} 1064 1065bool EdgeBuilder::IsConsumedExpr(const PathDiagnosticLocation &L) { 1066 if (const Expr *X = dyn_cast_or_null<Expr>(L.asStmt())) 1067 return PDB.getParentMap().isConsumedExpr(X) && !IsControlFlowExpr(X); 1068 1069 return false; 1070} 1071 1072void EdgeBuilder::addExtendedContext(const Stmt *S) { 1073 if (!S) 1074 return; 1075 1076 const Stmt *Parent = PDB.getParent(S); 1077 while (Parent) { 1078 if (isa<CompoundStmt>(Parent)) 1079 Parent = PDB.getParent(Parent); 1080 else 1081 break; 1082 } 1083 1084 if (Parent) { 1085 switch (Parent->getStmtClass()) { 1086 case Stmt::DoStmtClass: 1087 case Stmt::ObjCAtSynchronizedStmtClass: 1088 addContext(Parent); 1089 default: 1090 break; 1091 } 1092 } 1093 1094 addContext(S); 1095} 1096 1097void EdgeBuilder::addContext(const Stmt *S) { 1098 if (!S) 1099 return; 1100 1101 PathDiagnosticLocation L(S, PDB.getSourceManager(), PDB.getLocationContext()); 1102 1103 while (!CLocs.empty()) { 1104 const PathDiagnosticLocation &TopContextLoc = CLocs.back(); 1105 1106 // Is the top location context the same as the one for the new location? 1107 if (TopContextLoc == L) 1108 return; 1109 1110 if (containsLocation(TopContextLoc, L)) { 1111 CLocs.push_back(L); 1112 return; 1113 } 1114 1115 // Context does not contain the location. Flush it. 1116 popLocation(); 1117 } 1118 1119 CLocs.push_back(L); 1120} 1121 1122static void GenerateExtensivePathDiagnostic(PathDiagnostic& PD, 1123 PathDiagnosticBuilder &PDB, 1124 const ExplodedNode *N) { 1125 EdgeBuilder EB(PD, PDB); 1126 const SourceManager& SM = PDB.getSourceManager(); 1127 1128 const ExplodedNode *NextNode = N->pred_empty() ? NULL : *(N->pred_begin()); 1129 while (NextNode) { 1130 N = NextNode; 1131 NextNode = GetPredecessorNode(N); 1132 ProgramPoint P = N->getLocation(); 1133 1134 do { 1135 // Block edges. 1136 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { 1137 const CFGBlock &Blk = *BE->getSrc(); 1138 const Stmt *Term = Blk.getTerminator(); 1139 1140 // Are we jumping to the head of a loop? Add a special diagnostic. 1141 if (const Stmt *Loop = BE->getDst()->getLoopTarget()) { 1142 PathDiagnosticLocation L(Loop, SM, PDB.getLocationContext()); 1143 const CompoundStmt *CS = NULL; 1144 1145 if (!Term) { 1146 if (const ForStmt *FS = dyn_cast<ForStmt>(Loop)) 1147 CS = dyn_cast<CompoundStmt>(FS->getBody()); 1148 else if (const WhileStmt *WS = dyn_cast<WhileStmt>(Loop)) 1149 CS = dyn_cast<CompoundStmt>(WS->getBody()); 1150 } 1151 1152 PathDiagnosticEventPiece *p = 1153 new PathDiagnosticEventPiece(L, 1154 "Looping back to the head of the loop"); 1155 1156 EB.addEdge(p->getLocation(), true); 1157 PD.push_front(p); 1158 1159 if (CS) { 1160 PathDiagnosticLocation BL = 1161 PathDiagnosticLocation::createEndBrace(CS, SM); 1162 EB.addEdge(BL); 1163 } 1164 } 1165 1166 if (Term) 1167 EB.addContext(Term); 1168 1169 break; 1170 } 1171 1172 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&P)) { 1173 if (const CFGStmt *S = BE->getFirstElement().getAs<CFGStmt>()) { 1174 const Stmt *stmt = S->getStmt(); 1175 if (IsControlFlowExpr(stmt)) { 1176 // Add the proper context for '&&', '||', and '?'. 1177 EB.addContext(stmt); 1178 } 1179 else 1180 EB.addExtendedContext(PDB.getEnclosingStmtLocation(stmt).asStmt()); 1181 } 1182 1183 break; 1184 } 1185 } while (0); 1186 1187 if (!NextNode) 1188 continue; 1189 1190 // Add pieces from custom visitors. 1191 BugReport *R = PDB.getBugReport(); 1192 for (BugReport::visitor_iterator I = R->visitor_begin(), 1193 E = R->visitor_end(); I!=E; ++I) { 1194 if (PathDiagnosticPiece *p = (*I)->VisitNode(N, NextNode, PDB, *R)) { 1195 const PathDiagnosticLocation &Loc = p->getLocation(); 1196 EB.addEdge(Loc, true); 1197 PD.push_front(p); 1198 if (const Stmt *S = Loc.asStmt()) 1199 EB.addExtendedContext(PDB.getEnclosingStmtLocation(S).asStmt()); 1200 } 1201 } 1202 } 1203} 1204 1205//===----------------------------------------------------------------------===// 1206// Methods for BugType and subclasses. 1207//===----------------------------------------------------------------------===// 1208BugType::~BugType() { } 1209 1210void BugType::FlushReports(BugReporter &BR) {} 1211 1212void BuiltinBug::anchor() {} 1213 1214//===----------------------------------------------------------------------===// 1215// Methods for BugReport and subclasses. 1216//===----------------------------------------------------------------------===// 1217 1218void BugReport::NodeResolver::anchor() {} 1219 1220void BugReport::addVisitor(BugReporterVisitor* visitor) { 1221 if (!visitor) 1222 return; 1223 1224 llvm::FoldingSetNodeID ID; 1225 visitor->Profile(ID); 1226 void *InsertPos; 1227 1228 if (CallbacksSet.FindNodeOrInsertPos(ID, InsertPos)) { 1229 delete visitor; 1230 return; 1231 } 1232 1233 CallbacksSet.InsertNode(visitor, InsertPos); 1234 Callbacks = F.add(visitor, Callbacks); 1235} 1236 1237BugReport::~BugReport() { 1238 for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) { 1239 delete *I; 1240 } 1241} 1242 1243void BugReport::Profile(llvm::FoldingSetNodeID& hash) const { 1244 hash.AddPointer(&BT); 1245 hash.AddString(Description); 1246 if (Location.isValid()) { 1247 Location.Profile(hash); 1248 } else { 1249 assert(ErrorNode); 1250 hash.AddPointer(GetCurrentOrPreviousStmt(ErrorNode)); 1251 } 1252 1253 for (SmallVectorImpl<SourceRange>::const_iterator I = 1254 Ranges.begin(), E = Ranges.end(); I != E; ++I) { 1255 const SourceRange range = *I; 1256 if (!range.isValid()) 1257 continue; 1258 hash.AddInteger(range.getBegin().getRawEncoding()); 1259 hash.AddInteger(range.getEnd().getRawEncoding()); 1260 } 1261} 1262 1263const Stmt *BugReport::getStmt() const { 1264 if (!ErrorNode) 1265 return 0; 1266 1267 ProgramPoint ProgP = ErrorNode->getLocation(); 1268 const Stmt *S = NULL; 1269 1270 if (BlockEntrance *BE = dyn_cast<BlockEntrance>(&ProgP)) { 1271 CFGBlock &Exit = ProgP.getLocationContext()->getCFG()->getExit(); 1272 if (BE->getBlock() == &Exit) 1273 S = GetPreviousStmt(ErrorNode); 1274 } 1275 if (!S) 1276 S = GetStmt(ProgP); 1277 1278 return S; 1279} 1280 1281std::pair<BugReport::ranges_iterator, BugReport::ranges_iterator> 1282BugReport::getRanges() { 1283 // If no custom ranges, add the range of the statement corresponding to 1284 // the error node. 1285 if (Ranges.empty()) { 1286 if (const Expr *E = dyn_cast_or_null<Expr>(getStmt())) 1287 addRange(E->getSourceRange()); 1288 else 1289 return std::make_pair(ranges_iterator(), ranges_iterator()); 1290 } 1291 1292 // User-specified absence of range info. 1293 if (Ranges.size() == 1 && !Ranges.begin()->isValid()) 1294 return std::make_pair(ranges_iterator(), ranges_iterator()); 1295 1296 return std::make_pair(Ranges.begin(), Ranges.end()); 1297} 1298 1299PathDiagnosticLocation BugReport::getLocation(const SourceManager &SM) const { 1300 if (ErrorNode) { 1301 assert(!Location.isValid() && 1302 "Either Location or ErrorNode should be specified but not both."); 1303 1304 if (const Stmt *S = GetCurrentOrPreviousStmt(ErrorNode)) { 1305 const LocationContext *LC = ErrorNode->getLocationContext(); 1306 1307 // For member expressions, return the location of the '.' or '->'. 1308 if (const MemberExpr *ME = dyn_cast<MemberExpr>(S)) 1309 return PathDiagnosticLocation::createMemberLoc(ME, SM); 1310 // For binary operators, return the location of the operator. 1311 if (const BinaryOperator *B = dyn_cast<BinaryOperator>(S)) 1312 return PathDiagnosticLocation::createOperatorLoc(B, SM); 1313 1314 return PathDiagnosticLocation::createBegin(S, SM, LC); 1315 } 1316 } else { 1317 assert(Location.isValid()); 1318 return Location; 1319 } 1320 1321 return PathDiagnosticLocation(); 1322} 1323 1324//===----------------------------------------------------------------------===// 1325// Methods for BugReporter and subclasses. 1326//===----------------------------------------------------------------------===// 1327 1328BugReportEquivClass::~BugReportEquivClass() { 1329 for (iterator I=begin(), E=end(); I!=E; ++I) delete *I; 1330} 1331 1332GRBugReporter::~GRBugReporter() { } 1333BugReporterData::~BugReporterData() {} 1334 1335ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); } 1336 1337ProgramStateManager& 1338GRBugReporter::getStateManager() { return Eng.getStateManager(); } 1339 1340BugReporter::~BugReporter() { 1341 FlushReports(); 1342 1343 // Free the bug reports we are tracking. 1344 typedef std::vector<BugReportEquivClass *> ContTy; 1345 for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end(); 1346 I != E; ++I) { 1347 delete *I; 1348 } 1349} 1350 1351void BugReporter::FlushReports() { 1352 if (BugTypes.isEmpty()) 1353 return; 1354 1355 // First flush the warnings for each BugType. This may end up creating new 1356 // warnings and new BugTypes. 1357 // FIXME: Only NSErrorChecker needs BugType's FlushReports. 1358 // Turn NSErrorChecker into a proper checker and remove this. 1359 SmallVector<const BugType*, 16> bugTypes; 1360 for (BugTypesTy::iterator I=BugTypes.begin(), E=BugTypes.end(); I!=E; ++I) 1361 bugTypes.push_back(*I); 1362 for (SmallVector<const BugType*, 16>::iterator 1363 I = bugTypes.begin(), E = bugTypes.end(); I != E; ++I) 1364 const_cast<BugType*>(*I)->FlushReports(*this); 1365 1366 typedef llvm::FoldingSet<BugReportEquivClass> SetTy; 1367 for (SetTy::iterator EI=EQClasses.begin(), EE=EQClasses.end(); EI!=EE;++EI){ 1368 BugReportEquivClass& EQ = *EI; 1369 FlushReport(EQ); 1370 } 1371 1372 // BugReporter owns and deletes only BugTypes created implicitly through 1373 // EmitBasicReport. 1374 // FIXME: There are leaks from checkers that assume that the BugTypes they 1375 // create will be destroyed by the BugReporter. 1376 for (llvm::StringMap<BugType*>::iterator 1377 I = StrBugTypes.begin(), E = StrBugTypes.end(); I != E; ++I) 1378 delete I->second; 1379 1380 // Remove all references to the BugType objects. 1381 BugTypes = F.getEmptySet(); 1382} 1383 1384//===----------------------------------------------------------------------===// 1385// PathDiagnostics generation. 1386//===----------------------------------------------------------------------===// 1387 1388static std::pair<std::pair<ExplodedGraph*, NodeBackMap*>, 1389 std::pair<ExplodedNode*, unsigned> > 1390MakeReportGraph(const ExplodedGraph* G, 1391 SmallVectorImpl<const ExplodedNode*> &nodes) { 1392 1393 // Create the trimmed graph. It will contain the shortest paths from the 1394 // error nodes to the root. In the new graph we should only have one 1395 // error node unless there are two or more error nodes with the same minimum 1396 // path length. 1397 ExplodedGraph* GTrim; 1398 InterExplodedGraphMap* NMap; 1399 1400 llvm::DenseMap<const void*, const void*> InverseMap; 1401 llvm::tie(GTrim, NMap) = G->Trim(nodes.data(), nodes.data() + nodes.size(), 1402 &InverseMap); 1403 1404 // Create owning pointers for GTrim and NMap just to ensure that they are 1405 // released when this function exists. 1406 OwningPtr<ExplodedGraph> AutoReleaseGTrim(GTrim); 1407 OwningPtr<InterExplodedGraphMap> AutoReleaseNMap(NMap); 1408 1409 // Find the (first) error node in the trimmed graph. We just need to consult 1410 // the node map (NMap) which maps from nodes in the original graph to nodes 1411 // in the new graph. 1412 1413 std::queue<const ExplodedNode*> WS; 1414 typedef llvm::DenseMap<const ExplodedNode*, unsigned> IndexMapTy; 1415 IndexMapTy IndexMap; 1416 1417 for (unsigned nodeIndex = 0 ; nodeIndex < nodes.size(); ++nodeIndex) { 1418 const ExplodedNode *originalNode = nodes[nodeIndex]; 1419 if (const ExplodedNode *N = NMap->getMappedNode(originalNode)) { 1420 WS.push(N); 1421 IndexMap[originalNode] = nodeIndex; 1422 } 1423 } 1424 1425 assert(!WS.empty() && "No error node found in the trimmed graph."); 1426 1427 // Create a new (third!) graph with a single path. This is the graph 1428 // that will be returned to the caller. 1429 ExplodedGraph *GNew = new ExplodedGraph(); 1430 1431 // Sometimes the trimmed graph can contain a cycle. Perform a reverse BFS 1432 // to the root node, and then construct a new graph that contains only 1433 // a single path. 1434 llvm::DenseMap<const void*,unsigned> Visited; 1435 1436 unsigned cnt = 0; 1437 const ExplodedNode *Root = 0; 1438 1439 while (!WS.empty()) { 1440 const ExplodedNode *Node = WS.front(); 1441 WS.pop(); 1442 1443 if (Visited.find(Node) != Visited.end()) 1444 continue; 1445 1446 Visited[Node] = cnt++; 1447 1448 if (Node->pred_empty()) { 1449 Root = Node; 1450 break; 1451 } 1452 1453 for (ExplodedNode::const_pred_iterator I=Node->pred_begin(), 1454 E=Node->pred_end(); I!=E; ++I) 1455 WS.push(*I); 1456 } 1457 1458 assert(Root); 1459 1460 // Now walk from the root down the BFS path, always taking the successor 1461 // with the lowest number. 1462 ExplodedNode *Last = 0, *First = 0; 1463 NodeBackMap *BM = new NodeBackMap(); 1464 unsigned NodeIndex = 0; 1465 1466 for ( const ExplodedNode *N = Root ;;) { 1467 // Lookup the number associated with the current node. 1468 llvm::DenseMap<const void*,unsigned>::iterator I = Visited.find(N); 1469 assert(I != Visited.end()); 1470 1471 // Create the equivalent node in the new graph with the same state 1472 // and location. 1473 ExplodedNode *NewN = GNew->getNode(N->getLocation(), N->getState()); 1474 1475 // Store the mapping to the original node. 1476 llvm::DenseMap<const void*, const void*>::iterator IMitr=InverseMap.find(N); 1477 assert(IMitr != InverseMap.end() && "No mapping to original node."); 1478 (*BM)[NewN] = (const ExplodedNode*) IMitr->second; 1479 1480 // Link up the new node with the previous node. 1481 if (Last) 1482 NewN->addPredecessor(Last, *GNew); 1483 1484 Last = NewN; 1485 1486 // Are we at the final node? 1487 IndexMapTy::iterator IMI = 1488 IndexMap.find((const ExplodedNode*)(IMitr->second)); 1489 if (IMI != IndexMap.end()) { 1490 First = NewN; 1491 NodeIndex = IMI->second; 1492 break; 1493 } 1494 1495 // Find the next successor node. We choose the node that is marked 1496 // with the lowest DFS number. 1497 ExplodedNode::const_succ_iterator SI = N->succ_begin(); 1498 ExplodedNode::const_succ_iterator SE = N->succ_end(); 1499 N = 0; 1500 1501 for (unsigned MinVal = 0; SI != SE; ++SI) { 1502 1503 I = Visited.find(*SI); 1504 1505 if (I == Visited.end()) 1506 continue; 1507 1508 if (!N || I->second < MinVal) { 1509 N = *SI; 1510 MinVal = I->second; 1511 } 1512 } 1513 1514 assert(N); 1515 } 1516 1517 assert(First); 1518 1519 return std::make_pair(std::make_pair(GNew, BM), 1520 std::make_pair(First, NodeIndex)); 1521} 1522 1523/// CompactPathDiagnostic - This function postprocesses a PathDiagnostic object 1524/// and collapses PathDiagosticPieces that are expanded by macros. 1525static void CompactPathDiagnostic(PathDiagnostic &PD, const SourceManager& SM) { 1526 typedef std::vector<std::pair<PathDiagnosticMacroPiece*, SourceLocation> > 1527 MacroStackTy; 1528 1529 typedef std::vector<PathDiagnosticPiece*> 1530 PiecesTy; 1531 1532 MacroStackTy MacroStack; 1533 PiecesTy Pieces; 1534 1535 for (PathDiagnostic::iterator I = PD.begin(), E = PD.end(); I!=E; ++I) { 1536 // Get the location of the PathDiagnosticPiece. 1537 const FullSourceLoc Loc = I->getLocation().asLocation(); 1538 1539 // Determine the instantiation location, which is the location we group 1540 // related PathDiagnosticPieces. 1541 SourceLocation InstantiationLoc = Loc.isMacroID() ? 1542 SM.getExpansionLoc(Loc) : 1543 SourceLocation(); 1544 1545 if (Loc.isFileID()) { 1546 MacroStack.clear(); 1547 Pieces.push_back(&*I); 1548 continue; 1549 } 1550 1551 assert(Loc.isMacroID()); 1552 1553 // Is the PathDiagnosticPiece within the same macro group? 1554 if (!MacroStack.empty() && InstantiationLoc == MacroStack.back().second) { 1555 MacroStack.back().first->push_back(&*I); 1556 continue; 1557 } 1558 1559 // We aren't in the same group. Are we descending into a new macro 1560 // or are part of an old one? 1561 PathDiagnosticMacroPiece *MacroGroup = 0; 1562 1563 SourceLocation ParentInstantiationLoc = InstantiationLoc.isMacroID() ? 1564 SM.getExpansionLoc(Loc) : 1565 SourceLocation(); 1566 1567 // Walk the entire macro stack. 1568 while (!MacroStack.empty()) { 1569 if (InstantiationLoc == MacroStack.back().second) { 1570 MacroGroup = MacroStack.back().first; 1571 break; 1572 } 1573 1574 if (ParentInstantiationLoc == MacroStack.back().second) { 1575 MacroGroup = MacroStack.back().first; 1576 break; 1577 } 1578 1579 MacroStack.pop_back(); 1580 } 1581 1582 if (!MacroGroup || ParentInstantiationLoc == MacroStack.back().second) { 1583 // Create a new macro group and add it to the stack. 1584 PathDiagnosticMacroPiece *NewGroup = 1585 new PathDiagnosticMacroPiece( 1586 PathDiagnosticLocation::createSingleLocation(I->getLocation())); 1587 1588 if (MacroGroup) 1589 MacroGroup->push_back(NewGroup); 1590 else { 1591 assert(InstantiationLoc.isFileID()); 1592 Pieces.push_back(NewGroup); 1593 } 1594 1595 MacroGroup = NewGroup; 1596 MacroStack.push_back(std::make_pair(MacroGroup, InstantiationLoc)); 1597 } 1598 1599 // Finally, add the PathDiagnosticPiece to the group. 1600 MacroGroup->push_back(&*I); 1601 } 1602 1603 // Now take the pieces and construct a new PathDiagnostic. 1604 PD.resetPath(false); 1605 1606 for (PiecesTy::iterator I=Pieces.begin(), E=Pieces.end(); I!=E; ++I) { 1607 if (PathDiagnosticMacroPiece *MP=dyn_cast<PathDiagnosticMacroPiece>(*I)) 1608 if (!MP->containsEvent()) { 1609 delete MP; 1610 continue; 1611 } 1612 1613 PD.push_back(*I); 1614 } 1615} 1616 1617void GRBugReporter::GeneratePathDiagnostic(PathDiagnostic& PD, 1618 SmallVectorImpl<BugReport *> &bugReports) { 1619 1620 assert(!bugReports.empty()); 1621 SmallVector<const ExplodedNode *, 10> errorNodes; 1622 for (SmallVectorImpl<BugReport*>::iterator I = bugReports.begin(), 1623 E = bugReports.end(); I != E; ++I) { 1624 errorNodes.push_back((*I)->getErrorNode()); 1625 } 1626 1627 // Construct a new graph that contains only a single path from the error 1628 // node to a root. 1629 const std::pair<std::pair<ExplodedGraph*, NodeBackMap*>, 1630 std::pair<ExplodedNode*, unsigned> >& 1631 GPair = MakeReportGraph(&getGraph(), errorNodes); 1632 1633 // Find the BugReport with the original location. 1634 assert(GPair.second.second < bugReports.size()); 1635 BugReport *R = bugReports[GPair.second.second]; 1636 assert(R && "No original report found for sliced graph."); 1637 1638 OwningPtr<ExplodedGraph> ReportGraph(GPair.first.first); 1639 OwningPtr<NodeBackMap> BackMap(GPair.first.second); 1640 const ExplodedNode *N = GPair.second.first; 1641 1642 // Start building the path diagnostic... 1643 PathDiagnosticBuilder PDB(*this, R, BackMap.get(), 1644 getPathDiagnosticConsumer()); 1645 1646 // Register additional node visitors. 1647 R->addVisitor(new NilReceiverBRVisitor()); 1648 R->addVisitor(new ConditionBRVisitor()); 1649 1650 // If inlining is turning out, emit diagnostics for CallEnter and 1651 // CallExit at the top level. 1652 bool showTopLevel = Eng.getAnalysisManager().shouldInlineCall(); 1653 R->addVisitor(new CallEnterExitBRVisitor(showTopLevel)); 1654 1655 // Generate the very last diagnostic piece - the piece is visible before 1656 // the trace is expanded. 1657 PathDiagnosticPiece *LastPiece = 0; 1658 for (BugReport::visitor_iterator I = R->visitor_begin(), 1659 E = R->visitor_end(); I!=E; ++I) { 1660 if (PathDiagnosticPiece *Piece = (*I)->getEndPath(PDB, N, *R)) { 1661 assert (!LastPiece && 1662 "There can only be one final piece in a diagnostic."); 1663 LastPiece = Piece; 1664 } 1665 } 1666 if (!LastPiece) 1667 LastPiece = BugReporterVisitor::getDefaultEndPath(PDB, N, *R); 1668 if (LastPiece) 1669 PD.push_back(LastPiece); 1670 else 1671 return; 1672 1673 switch (PDB.getGenerationScheme()) { 1674 case PathDiagnosticConsumer::Extensive: 1675 GenerateExtensivePathDiagnostic(PD, PDB, N); 1676 break; 1677 case PathDiagnosticConsumer::Minimal: 1678 GenerateMinimalPathDiagnostic(PD, PDB, N); 1679 break; 1680 } 1681} 1682 1683void BugReporter::Register(BugType *BT) { 1684 BugTypes = F.add(BugTypes, BT); 1685} 1686 1687void BugReporter::EmitReport(BugReport* R) { 1688 // Compute the bug report's hash to determine its equivalence class. 1689 llvm::FoldingSetNodeID ID; 1690 R->Profile(ID); 1691 1692 // Lookup the equivance class. If there isn't one, create it. 1693 BugType& BT = R->getBugType(); 1694 Register(&BT); 1695 void *InsertPos; 1696 BugReportEquivClass* EQ = EQClasses.FindNodeOrInsertPos(ID, InsertPos); 1697 1698 if (!EQ) { 1699 EQ = new BugReportEquivClass(R); 1700 EQClasses.InsertNode(EQ, InsertPos); 1701 EQClassesVector.push_back(EQ); 1702 } 1703 else 1704 EQ->AddReport(R); 1705} 1706 1707 1708//===----------------------------------------------------------------------===// 1709// Emitting reports in equivalence classes. 1710//===----------------------------------------------------------------------===// 1711 1712namespace { 1713struct FRIEC_WLItem { 1714 const ExplodedNode *N; 1715 ExplodedNode::const_succ_iterator I, E; 1716 1717 FRIEC_WLItem(const ExplodedNode *n) 1718 : N(n), I(N->succ_begin()), E(N->succ_end()) {} 1719}; 1720} 1721 1722static BugReport * 1723FindReportInEquivalenceClass(BugReportEquivClass& EQ, 1724 SmallVectorImpl<BugReport*> &bugReports) { 1725 1726 BugReportEquivClass::iterator I = EQ.begin(), E = EQ.end(); 1727 assert(I != E); 1728 BugReport *R = *I; 1729 BugType& BT = R->getBugType(); 1730 1731 // If we don't need to suppress any of the nodes because they are 1732 // post-dominated by a sink, simply add all the nodes in the equivalence class 1733 // to 'Nodes'. Any of the reports will serve as a "representative" report. 1734 if (!BT.isSuppressOnSink()) { 1735 for (BugReportEquivClass::iterator I=EQ.begin(), E=EQ.end(); I!=E; ++I) { 1736 const ExplodedNode *N = I->getErrorNode(); 1737 if (N) { 1738 R = *I; 1739 bugReports.push_back(R); 1740 } 1741 } 1742 return R; 1743 } 1744 1745 // For bug reports that should be suppressed when all paths are post-dominated 1746 // by a sink node, iterate through the reports in the equivalence class 1747 // until we find one that isn't post-dominated (if one exists). We use a 1748 // DFS traversal of the ExplodedGraph to find a non-sink node. We could write 1749 // this as a recursive function, but we don't want to risk blowing out the 1750 // stack for very long paths. 1751 BugReport *exampleReport = 0; 1752 1753 for (; I != E; ++I) { 1754 R = *I; 1755 const ExplodedNode *errorNode = R->getErrorNode(); 1756 1757 if (!errorNode) 1758 continue; 1759 if (errorNode->isSink()) { 1760 llvm_unreachable( 1761 "BugType::isSuppressSink() should not be 'true' for sink end nodes"); 1762 } 1763 // No successors? By definition this nodes isn't post-dominated by a sink. 1764 if (errorNode->succ_empty()) { 1765 bugReports.push_back(R); 1766 if (!exampleReport) 1767 exampleReport = R; 1768 continue; 1769 } 1770 1771 // At this point we know that 'N' is not a sink and it has at least one 1772 // successor. Use a DFS worklist to find a non-sink end-of-path node. 1773 typedef FRIEC_WLItem WLItem; 1774 typedef SmallVector<WLItem, 10> DFSWorkList; 1775 llvm::DenseMap<const ExplodedNode *, unsigned> Visited; 1776 1777 DFSWorkList WL; 1778 WL.push_back(errorNode); 1779 Visited[errorNode] = 1; 1780 1781 while (!WL.empty()) { 1782 WLItem &WI = WL.back(); 1783 assert(!WI.N->succ_empty()); 1784 1785 for (; WI.I != WI.E; ++WI.I) { 1786 const ExplodedNode *Succ = *WI.I; 1787 // End-of-path node? 1788 if (Succ->succ_empty()) { 1789 // If we found an end-of-path node that is not a sink. 1790 if (!Succ->isSink()) { 1791 bugReports.push_back(R); 1792 if (!exampleReport) 1793 exampleReport = R; 1794 WL.clear(); 1795 break; 1796 } 1797 // Found a sink? Continue on to the next successor. 1798 continue; 1799 } 1800 // Mark the successor as visited. If it hasn't been explored, 1801 // enqueue it to the DFS worklist. 1802 unsigned &mark = Visited[Succ]; 1803 if (!mark) { 1804 mark = 1; 1805 WL.push_back(Succ); 1806 break; 1807 } 1808 } 1809 1810 // The worklist may have been cleared at this point. First 1811 // check if it is empty before checking the last item. 1812 if (!WL.empty() && &WL.back() == &WI) 1813 WL.pop_back(); 1814 } 1815 } 1816 1817 // ExampleReport will be NULL if all the nodes in the equivalence class 1818 // were post-dominated by sinks. 1819 return exampleReport; 1820} 1821 1822//===----------------------------------------------------------------------===// 1823// DiagnosticCache. This is a hack to cache analyzer diagnostics. It 1824// uses global state, which eventually should go elsewhere. 1825//===----------------------------------------------------------------------===// 1826namespace { 1827class DiagCacheItem : public llvm::FoldingSetNode { 1828 llvm::FoldingSetNodeID ID; 1829public: 1830 DiagCacheItem(BugReport *R, PathDiagnostic *PD) { 1831 R->Profile(ID); 1832 PD->Profile(ID); 1833 } 1834 1835 void Profile(llvm::FoldingSetNodeID &id) { 1836 id = ID; 1837 } 1838 1839 llvm::FoldingSetNodeID &getID() { return ID; } 1840}; 1841} 1842 1843static bool IsCachedDiagnostic(BugReport *R, PathDiagnostic *PD) { 1844 // FIXME: Eventually this diagnostic cache should reside in something 1845 // like AnalysisManager instead of being a static variable. This is 1846 // really unsafe in the long term. 1847 typedef llvm::FoldingSet<DiagCacheItem> DiagnosticCache; 1848 static DiagnosticCache DC; 1849 1850 void *InsertPos; 1851 DiagCacheItem *Item = new DiagCacheItem(R, PD); 1852 1853 if (DC.FindNodeOrInsertPos(Item->getID(), InsertPos)) { 1854 delete Item; 1855 return true; 1856 } 1857 1858 DC.InsertNode(Item, InsertPos); 1859 return false; 1860} 1861 1862void BugReporter::FlushReport(BugReportEquivClass& EQ) { 1863 SmallVector<BugReport*, 10> bugReports; 1864 BugReport *exampleReport = FindReportInEquivalenceClass(EQ, bugReports); 1865 if (!exampleReport) 1866 return; 1867 1868 PathDiagnosticConsumer* PD = getPathDiagnosticConsumer(); 1869 1870 // FIXME: Make sure we use the 'R' for the path that was actually used. 1871 // Probably doesn't make a difference in practice. 1872 BugType& BT = exampleReport->getBugType(); 1873 1874 OwningPtr<PathDiagnostic> 1875 D(new PathDiagnostic(exampleReport->getBugType().getName(), 1876 !PD || PD->useVerboseDescription() 1877 ? exampleReport->getDescription() 1878 : exampleReport->getShortDescription(), 1879 BT.getCategory())); 1880 1881 if (!bugReports.empty()) 1882 GeneratePathDiagnostic(*D.get(), bugReports); 1883 1884 if (IsCachedDiagnostic(exampleReport, D.get())) 1885 return; 1886 1887 // Get the meta data. 1888 const BugReport::ExtraTextList &Meta = 1889 exampleReport->getExtraText(); 1890 for (BugReport::ExtraTextList::const_iterator i = Meta.begin(), 1891 e = Meta.end(); i != e; ++i) { 1892 D->addMeta(*i); 1893 } 1894 1895 // Emit a summary diagnostic to the regular Diagnostics engine. 1896 BugReport::ranges_iterator Beg, End; 1897 llvm::tie(Beg, End) = exampleReport->getRanges(); 1898 DiagnosticsEngine &Diag = getDiagnostic(); 1899 1900 // Search the description for '%', as that will be interpretted as a 1901 // format character by FormatDiagnostics. 1902 StringRef desc = exampleReport->getShortDescription(); 1903 unsigned ErrorDiag; 1904 { 1905 SmallString<512> TmpStr; 1906 llvm::raw_svector_ostream Out(TmpStr); 1907 for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I) 1908 if (*I == '%') 1909 Out << "%%"; 1910 else 1911 Out << *I; 1912 1913 Out.flush(); 1914 ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning, TmpStr); 1915 } 1916 1917 { 1918 DiagnosticBuilder diagBuilder = Diag.Report( 1919 exampleReport->getLocation(getSourceManager()).asLocation(), ErrorDiag); 1920 for (BugReport::ranges_iterator I = Beg; I != End; ++I) 1921 diagBuilder << *I; 1922 } 1923 1924 // Emit a full diagnostic for the path if we have a PathDiagnosticConsumer. 1925 if (!PD) 1926 return; 1927 1928 if (D->empty()) { 1929 PathDiagnosticPiece *piece = new PathDiagnosticEventPiece( 1930 exampleReport->getLocation(getSourceManager()), 1931 exampleReport->getDescription()); 1932 1933 for ( ; Beg != End; ++Beg) piece->addRange(*Beg); 1934 D->push_back(piece); 1935 } 1936 1937 PD->HandlePathDiagnostic(D.take()); 1938} 1939 1940void BugReporter::EmitBasicReport(StringRef name, StringRef str, 1941 PathDiagnosticLocation Loc, 1942 SourceRange* RBeg, unsigned NumRanges) { 1943 EmitBasicReport(name, "", str, Loc, RBeg, NumRanges); 1944} 1945 1946void BugReporter::EmitBasicReport(StringRef name, 1947 StringRef category, 1948 StringRef str, PathDiagnosticLocation Loc, 1949 SourceRange* RBeg, unsigned NumRanges) { 1950 1951 // 'BT' is owned by BugReporter. 1952 BugType *BT = getBugTypeForName(name, category); 1953 BugReport *R = new BugReport(*BT, str, Loc); 1954 for ( ; NumRanges > 0 ; --NumRanges, ++RBeg) R->addRange(*RBeg); 1955 EmitReport(R); 1956} 1957 1958BugType *BugReporter::getBugTypeForName(StringRef name, 1959 StringRef category) { 1960 SmallString<136> fullDesc; 1961 llvm::raw_svector_ostream(fullDesc) << name << ":" << category; 1962 llvm::StringMapEntry<BugType *> & 1963 entry = StrBugTypes.GetOrCreateValue(fullDesc); 1964 BugType *BT = entry.getValue(); 1965 if (!BT) { 1966 BT = new BugType(name, category); 1967 entry.setValue(BT); 1968 } 1969 return BT; 1970} 1971