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