AnalysisConsumer.cpp revision 98520835eb1aa091429afa06e9f4f7ebe3864d34
1//===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===// 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// "Meta" ASTConsumer for running different source analyses. 11// 12//===----------------------------------------------------------------------===// 13 14#define DEBUG_TYPE "AnalysisConsumer" 15 16#include "AnalysisConsumer.h" 17#include "clang/AST/ASTConsumer.h" 18#include "clang/AST/Decl.h" 19#include "clang/AST/DeclCXX.h" 20#include "clang/AST/DeclObjC.h" 21#include "clang/AST/ParentMap.h" 22#include "clang/Analysis/CFG.h" 23#include "clang/Analysis/CallGraph.h" 24#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h" 25#include "clang/StaticAnalyzer/Core/CheckerManager.h" 26#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h" 27#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" 28#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 29#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" 30#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 31#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" 32 33#include "clang/Basic/FileManager.h" 34#include "clang/Basic/SourceManager.h" 35#include "clang/Frontend/AnalyzerOptions.h" 36#include "clang/Lex/Preprocessor.h" 37#include "llvm/Support/raw_ostream.h" 38#include "llvm/Support/Path.h" 39#include "llvm/Support/Program.h" 40#include "llvm/Support/Timer.h" 41#include "llvm/ADT/DepthFirstIterator.h" 42#include "llvm/ADT/OwningPtr.h" 43#include "llvm/ADT/Statistic.h" 44 45using namespace clang; 46using namespace ento; 47using llvm::SmallPtrSet; 48 49static ExplodedNode::Auditor* CreateUbiViz(); 50 51STATISTIC(NumFunctionTopLevel, "The # of functions at top level."); 52STATISTIC(NumFunctionsAnalyzed, "The # of functions analysed (as top level)."); 53 54//===----------------------------------------------------------------------===// 55// Special PathDiagnosticConsumers. 56//===----------------------------------------------------------------------===// 57 58static PathDiagnosticConsumer* 59createPlistHTMLDiagnosticConsumer(const std::string& prefix, 60 const Preprocessor &PP) { 61 PathDiagnosticConsumer *PD = 62 createHTMLDiagnosticConsumer(llvm::sys::path::parent_path(prefix), PP); 63 return createPlistDiagnosticConsumer(prefix, PP, PD); 64} 65 66//===----------------------------------------------------------------------===// 67// AnalysisConsumer declaration. 68//===----------------------------------------------------------------------===// 69 70namespace { 71 72class AnalysisConsumer : public ASTConsumer { 73public: 74 ASTContext *Ctx; 75 const Preprocessor &PP; 76 const std::string OutDir; 77 AnalyzerOptions Opts; 78 ArrayRef<std::string> Plugins; 79 80 // PD is owned by AnalysisManager. 81 PathDiagnosticConsumer *PD; 82 83 StoreManagerCreator CreateStoreMgr; 84 ConstraintManagerCreator CreateConstraintMgr; 85 86 OwningPtr<CheckerManager> checkerMgr; 87 OwningPtr<AnalysisManager> Mgr; 88 89 /// Time the analyzes time of each translation unit. 90 static llvm::Timer* TUTotalTimer; 91 92 AnalysisConsumer(const Preprocessor& pp, 93 const std::string& outdir, 94 const AnalyzerOptions& opts, 95 ArrayRef<std::string> plugins) 96 : Ctx(0), PP(pp), OutDir(outdir), Opts(opts), Plugins(plugins), PD(0) { 97 DigestAnalyzerOptions(); 98 if (Opts.PrintStats) { 99 llvm::EnableStatistics(); 100 TUTotalTimer = new llvm::Timer("Analyzer Total Time"); 101 } 102 } 103 104 ~AnalysisConsumer() { 105 if (Opts.PrintStats) 106 delete TUTotalTimer; 107 } 108 109 void DigestAnalyzerOptions() { 110 // Create the PathDiagnosticConsumer. 111 if (!OutDir.empty()) { 112 switch (Opts.AnalysisDiagOpt) { 113 default: 114#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \ 115 case PD_##NAME: PD = CREATEFN(OutDir, PP); break; 116#include "clang/Frontend/Analyses.def" 117 } 118 } else if (Opts.AnalysisDiagOpt == PD_TEXT) { 119 // Create the text client even without a specified output file since 120 // it just uses diagnostic notes. 121 PD = createTextPathDiagnosticConsumer("", PP); 122 } 123 124 // Create the analyzer component creators. 125 switch (Opts.AnalysisStoreOpt) { 126 default: 127 llvm_unreachable("Unknown store manager."); 128#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ 129 case NAME##Model: CreateStoreMgr = CREATEFN; break; 130#include "clang/Frontend/Analyses.def" 131 } 132 133 switch (Opts.AnalysisConstraintsOpt) { 134 default: 135 llvm_unreachable("Unknown store manager."); 136#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ 137 case NAME##Model: CreateConstraintMgr = CREATEFN; break; 138#include "clang/Frontend/Analyses.def" 139 } 140 } 141 142 void DisplayFunction(const Decl *D) { 143 if (!Opts.AnalyzerDisplayProgress) 144 return; 145 146 SourceManager &SM = Mgr->getASTContext().getSourceManager(); 147 PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); 148 if (Loc.isValid()) { 149 llvm::errs() << "ANALYZE: " << Loc.getFilename(); 150 151 if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { 152 const NamedDecl *ND = cast<NamedDecl>(D); 153 llvm::errs() << ' ' << *ND << '\n'; 154 } 155 else if (isa<BlockDecl>(D)) { 156 llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:" 157 << Loc.getColumn() << '\n'; 158 } 159 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { 160 Selector S = MD->getSelector(); 161 llvm::errs() << ' ' << S.getAsString(); 162 } 163 } 164 } 165 166 virtual void Initialize(ASTContext &Context) { 167 Ctx = &Context; 168 checkerMgr.reset(createCheckerManager(Opts, PP.getLangOpts(), Plugins, 169 PP.getDiagnostics())); 170 Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(), 171 PP.getLangOpts(), PD, 172 CreateStoreMgr, CreateConstraintMgr, 173 checkerMgr.get(), 174 /* Indexer */ 0, 175 Opts.MaxNodes, Opts.MaxLoop, 176 Opts.VisualizeEGDot, Opts.VisualizeEGUbi, 177 Opts.AnalysisPurgeOpt, Opts.EagerlyAssume, 178 Opts.TrimGraph, 179 Opts.UnoptimizedCFG, Opts.CFGAddImplicitDtors, 180 Opts.CFGAddInitializers, 181 Opts.EagerlyTrimEGraph, 182 Opts.IPAMode, 183 Opts.InlineMaxStackDepth, 184 Opts.InlineMaxFunctionSize, 185 Opts.InliningMode)); 186 } 187 188 virtual void HandleTranslationUnit(ASTContext &C); 189 void HandleDeclContext(ASTContext &C, DeclContext *dc); 190 void HandleDeclContextDecl(ASTContext &C, Decl *D); 191 void HandleDeclContextDeclFunction(ASTContext &C, Decl *D); 192 193 void HandleCode(Decl *D, SetOfDecls *VisitedCallees = 0); 194 bool skipFunction(Decl *D); 195 void RunPathSensitiveChecks(Decl *D, SetOfDecls *VisitedCallees); 196 void ActionExprEngine(Decl *D, bool ObjCGCEnabled, SetOfDecls *VisitedCallees); 197}; 198} // end anonymous namespace 199 200//===----------------------------------------------------------------------===// 201// AnalysisConsumer implementation. 202//===----------------------------------------------------------------------===// 203llvm::Timer* AnalysisConsumer::TUTotalTimer = 0; 204 205void AnalysisConsumer::HandleDeclContext(ASTContext &C, DeclContext *dc) { 206 // Don't run the actions if an error has occurred with parsing the file. 207 DiagnosticsEngine &Diags = PP.getDiagnostics(); 208 if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) 209 return; 210 211 for (DeclContext::decl_iterator I = dc->decls_begin(), E = dc->decls_end(); 212 I != E; ++I) { 213 HandleDeclContextDecl(C, *I); 214 } 215 216 // If inlining is not turned on, use the simplest function order. 217 if (!Mgr->shouldInlineCall()) { 218 for (DeclContext::decl_iterator I = dc->decls_begin(), E = dc->decls_end(); 219 I != E; ++I) 220 HandleDeclContextDeclFunction(C, *I); 221 return; 222 } 223 224 // Otherwise, use the Callgraph to derive the order. 225 // Build the Call Graph. 226 CallGraph CG; 227 CG.addToCallGraph(dc); 228 229 // Find the top level nodes - children of root + the unreachable (parentless) 230 // nodes. 231 llvm::SmallVector<CallGraphNode*, 24> TopLevelFunctions; 232 CallGraphNode *Entry = CG.getRoot(); 233 for (CallGraphNode::iterator I = Entry->begin(), 234 E = Entry->end(); I != E; ++I) { 235 TopLevelFunctions.push_back(*I); 236 NumFunctionTopLevel++; 237 } 238 for (CallGraph::nodes_iterator TI = CG.parentless_begin(), 239 TE = CG.parentless_end(); TI != TE; ++TI) { 240 TopLevelFunctions.push_back(*TI); 241 NumFunctionTopLevel++; 242 } 243 244 // TODO: Sort TopLevelFunctions. 245 246 // DFS over all of the top level nodes. Use external Visited set, which is 247 // also modified when we inline a function. 248 SmallPtrSet<CallGraphNode*,24> Visited; 249 for (llvm::SmallVector<CallGraphNode*, 24>::iterator 250 TI = TopLevelFunctions.begin(), TE = TopLevelFunctions.end(); 251 TI != TE; ++TI) { 252 for (llvm::df_ext_iterator<CallGraphNode*, SmallPtrSet<CallGraphNode*,24> > 253 DFI = llvm::df_ext_begin(*TI, Visited), 254 E = llvm::df_ext_end(*TI, Visited); 255 DFI != E; ++DFI) { 256 SetOfDecls VisitedCallees; 257 Decl *D = (*DFI)->getDecl(); 258 assert(D); 259 HandleCode(D, (Mgr->InliningMode == All ? 0 : &VisitedCallees)); 260 261 // Add the visited callees to the global visited set. 262 for (SetOfDecls::const_iterator I = VisitedCallees.begin(), 263 E = VisitedCallees.end(); I != E; ++I) { 264 CallGraphNode *VN = CG.getNode(*I); 265 if (VN) 266 Visited.insert(VN); 267 } 268 } 269 } 270} 271 272void AnalysisConsumer::HandleDeclContextDecl(ASTContext &C, Decl *D) { 273 { // Handle callbacks for arbitrary decls. 274 BugReporter BR(*Mgr); 275 checkerMgr->runCheckersOnASTDecl(D, *Mgr, BR); 276 } 277 278 switch (D->getKind()) { 279 case Decl::Namespace: { 280 HandleDeclContext(C, cast<NamespaceDecl>(D)); 281 break; 282 } 283 case Decl::ObjCCategoryImpl: 284 case Decl::ObjCImplementation: { 285 ObjCImplDecl *ID = cast<ObjCImplDecl>(D); 286 for (ObjCContainerDecl::method_iterator MI = ID->meth_begin(), 287 ME = ID->meth_end(); MI != ME; ++MI) { 288 BugReporter BR(*Mgr); 289 checkerMgr->runCheckersOnASTDecl(*MI, *Mgr, BR); 290 } 291 break; 292 } 293 294 default: 295 break; 296 } 297} 298 299void AnalysisConsumer::HandleDeclContextDeclFunction(ASTContext &C, Decl *D) { 300 switch (D->getKind()) { 301 case Decl::CXXConstructor: 302 case Decl::CXXDestructor: 303 case Decl::CXXConversion: 304 case Decl::CXXMethod: 305 case Decl::Function: { 306 FunctionDecl *FD = cast<FunctionDecl>(D); 307 IdentifierInfo *II = FD->getIdentifier(); 308 if (II && II->getName().startswith("__inline")) 309 break; 310 // We skip function template definitions, as their semantics is 311 // only determined when they are instantiated. 312 if (FD->isThisDeclarationADefinition() && 313 !FD->isDependentContext()) { 314 if (!Opts.AnalyzeSpecificFunction.empty() && 315 FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction) 316 break; 317 HandleCode(FD); 318 } 319 break; 320 } 321 322 case Decl::ObjCCategoryImpl: 323 case Decl::ObjCImplementation: { 324 ObjCImplDecl *ID = cast<ObjCImplDecl>(D); 325 for (ObjCContainerDecl::method_iterator MI = ID->meth_begin(), 326 ME = ID->meth_end(); MI != ME; ++MI) { 327 if ((*MI)->isThisDeclarationADefinition()) { 328 if (!Opts.AnalyzeSpecificFunction.empty() && 329 Opts.AnalyzeSpecificFunction != 330 (*MI)->getSelector().getAsString()) 331 continue; 332 HandleCode(*MI); 333 } 334 } 335 break; 336 } 337 338 default: 339 break; 340 } 341} 342 343void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { 344 { 345 if (TUTotalTimer) TUTotalTimer->startTimer(); 346 347 // Introduce a scope to destroy BR before Mgr. 348 BugReporter BR(*Mgr); 349 TranslationUnitDecl *TU = C.getTranslationUnitDecl(); 350 checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR); 351 HandleDeclContext(C, TU); 352 353 // After all decls handled, run checkers on the entire TranslationUnit. 354 checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR); 355 } 356 357 // Explicitly destroy the PathDiagnosticConsumer. This will flush its output. 358 // FIXME: This should be replaced with something that doesn't rely on 359 // side-effects in PathDiagnosticConsumer's destructor. This is required when 360 // used with option -disable-free. 361 Mgr.reset(NULL); 362 363 if (TUTotalTimer) TUTotalTimer->stopTimer(); 364} 365 366static void FindBlocks(DeclContext *D, SmallVectorImpl<Decl*> &WL) { 367 if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) 368 WL.push_back(BD); 369 370 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); 371 I!=E; ++I) 372 if (DeclContext *DC = dyn_cast<DeclContext>(*I)) 373 FindBlocks(DC, WL); 374} 375 376static std::string getFunctionName(const Decl *D) { 377 if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) { 378 return ID->getSelector().getAsString(); 379 } 380 if (const FunctionDecl *ND = dyn_cast<FunctionDecl>(D)) { 381 IdentifierInfo *II = ND->getIdentifier(); 382 if (II) 383 return II->getName(); 384 } 385 return ""; 386} 387 388bool AnalysisConsumer::skipFunction(Decl *D) { 389 if (!Opts.AnalyzeSpecificFunction.empty() && 390 getFunctionName(D) != Opts.AnalyzeSpecificFunction) 391 return true; 392 393 // Don't run the actions on declarations in header files unless 394 // otherwise specified. 395 SourceManager &SM = Ctx->getSourceManager(); 396 SourceLocation SL = SM.getExpansionLoc(D->getLocation()); 397 if (!Opts.AnalyzeAll && !SM.isFromMainFile(SL)) 398 return true; 399 400 return false; 401} 402 403void AnalysisConsumer::HandleCode(Decl *D, SetOfDecls *VisitedCallees) { 404 405 if (skipFunction(D)) 406 return; 407 408 DisplayFunction(D); 409 410 // Clear the AnalysisManager of old AnalysisDeclContexts. 411 Mgr->ClearContexts(); 412 413 // Dispatch on the actions. 414 SmallVector<Decl*, 10> WL; 415 WL.push_back(D); 416 417 if (D->hasBody() && Opts.AnalyzeNestedBlocks) 418 FindBlocks(cast<DeclContext>(D), WL); 419 420 BugReporter BR(*Mgr); 421 for (SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end(); 422 WI != WE; ++WI) 423 if ((*WI)->hasBody()) { 424 checkerMgr->runCheckersOnASTBody(*WI, *Mgr, BR); 425 if (checkerMgr->hasPathSensitiveCheckers()) 426 RunPathSensitiveChecks(*WI, VisitedCallees); 427 } 428 NumFunctionsAnalyzed++; 429} 430 431//===----------------------------------------------------------------------===// 432// Path-sensitive checking. 433//===----------------------------------------------------------------------===// 434 435void AnalysisConsumer::ActionExprEngine(Decl *D, bool ObjCGCEnabled, 436 SetOfDecls *VisitedCallees) { 437 // Construct the analysis engine. First check if the CFG is valid. 438 // FIXME: Inter-procedural analysis will need to handle invalid CFGs. 439 if (!Mgr->getCFG(D)) 440 return; 441 442 ExprEngine Eng(*Mgr, ObjCGCEnabled, VisitedCallees); 443 444 // Set the graph auditor. 445 OwningPtr<ExplodedNode::Auditor> Auditor; 446 if (Mgr->shouldVisualizeUbigraph()) { 447 Auditor.reset(CreateUbiViz()); 448 ExplodedNode::SetAuditor(Auditor.get()); 449 } 450 451 // Execute the worklist algorithm. 452 Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D, 0), 453 Mgr->getMaxNodes()); 454 455 // Release the auditor (if any) so that it doesn't monitor the graph 456 // created BugReporter. 457 ExplodedNode::SetAuditor(0); 458 459 // Visualize the exploded graph. 460 if (Mgr->shouldVisualizeGraphviz()) 461 Eng.ViewGraph(Mgr->shouldTrimGraph()); 462 463 // Display warnings. 464 Eng.getBugReporter().FlushReports(); 465} 466 467void AnalysisConsumer::RunPathSensitiveChecks(Decl *D, SetOfDecls *Visited) { 468 469 switch (Mgr->getLangOpts().getGC()) { 470 case LangOptions::NonGC: 471 ActionExprEngine(D, false, Visited); 472 break; 473 474 case LangOptions::GCOnly: 475 ActionExprEngine(D, true, Visited); 476 break; 477 478 case LangOptions::HybridGC: 479 ActionExprEngine(D, false, Visited); 480 ActionExprEngine(D, true, Visited); 481 break; 482 } 483} 484 485//===----------------------------------------------------------------------===// 486// AnalysisConsumer creation. 487//===----------------------------------------------------------------------===// 488 489ASTConsumer* ento::CreateAnalysisConsumer(const Preprocessor& pp, 490 const std::string& outDir, 491 const AnalyzerOptions& opts, 492 ArrayRef<std::string> plugins) { 493 // Disable the effects of '-Werror' when using the AnalysisConsumer. 494 pp.getDiagnostics().setWarningsAsErrors(false); 495 496 return new AnalysisConsumer(pp, outDir, opts, plugins); 497} 498 499//===----------------------------------------------------------------------===// 500// Ubigraph Visualization. FIXME: Move to separate file. 501//===----------------------------------------------------------------------===// 502 503namespace { 504 505class UbigraphViz : public ExplodedNode::Auditor { 506 OwningPtr<raw_ostream> Out; 507 llvm::sys::Path Dir, Filename; 508 unsigned Cntr; 509 510 typedef llvm::DenseMap<void*,unsigned> VMap; 511 VMap M; 512 513public: 514 UbigraphViz(raw_ostream *out, llvm::sys::Path& dir, 515 llvm::sys::Path& filename); 516 517 ~UbigraphViz(); 518 519 virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst); 520}; 521 522} // end anonymous namespace 523 524static ExplodedNode::Auditor* CreateUbiViz() { 525 std::string ErrMsg; 526 527 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); 528 if (!ErrMsg.empty()) 529 return 0; 530 531 llvm::sys::Path Filename = Dir; 532 Filename.appendComponent("llvm_ubi"); 533 Filename.makeUnique(true,&ErrMsg); 534 535 if (!ErrMsg.empty()) 536 return 0; 537 538 llvm::errs() << "Writing '" << Filename.str() << "'.\n"; 539 540 OwningPtr<llvm::raw_fd_ostream> Stream; 541 Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg)); 542 543 if (!ErrMsg.empty()) 544 return 0; 545 546 return new UbigraphViz(Stream.take(), Dir, Filename); 547} 548 549void UbigraphViz::AddEdge(ExplodedNode *Src, ExplodedNode *Dst) { 550 551 assert (Src != Dst && "Self-edges are not allowed."); 552 553 // Lookup the Src. If it is a new node, it's a root. 554 VMap::iterator SrcI= M.find(Src); 555 unsigned SrcID; 556 557 if (SrcI == M.end()) { 558 M[Src] = SrcID = Cntr++; 559 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; 560 } 561 else 562 SrcID = SrcI->second; 563 564 // Lookup the Dst. 565 VMap::iterator DstI= M.find(Dst); 566 unsigned DstID; 567 568 if (DstI == M.end()) { 569 M[Dst] = DstID = Cntr++; 570 *Out << "('vertex', " << DstID << ")\n"; 571 } 572 else { 573 // We have hit DstID before. Change its style to reflect a cache hit. 574 DstID = DstI->second; 575 *Out << "('change_vertex_style', " << DstID << ", 1)\n"; 576 } 577 578 // Add the edge. 579 *Out << "('edge', " << SrcID << ", " << DstID 580 << ", ('arrow','true'), ('oriented', 'true'))\n"; 581} 582 583UbigraphViz::UbigraphViz(raw_ostream *out, llvm::sys::Path& dir, 584 llvm::sys::Path& filename) 585 : Out(out), Dir(dir), Filename(filename), Cntr(0) { 586 587 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; 588 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," 589 " ('size', '1.5'))\n"; 590} 591 592UbigraphViz::~UbigraphViz() { 593 Out.reset(0); 594 llvm::errs() << "Running 'ubiviz' program... "; 595 std::string ErrMsg; 596 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); 597 std::vector<const char*> args; 598 args.push_back(Ubiviz.c_str()); 599 args.push_back(Filename.c_str()); 600 args.push_back(0); 601 602 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { 603 llvm::errs() << "Error viewing graph: " << ErrMsg << "\n"; 604 } 605 606 // Delete the directory. 607 Dir.eraseFromDisk(true); 608} 609