AnalysisConsumer.cpp revision b6ee44dd2923fb7dac699c4bbfeded34a8604d3a
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 void RunPathSensitiveChecks(Decl *D, SetOfDecls *VisitedCallees); 195 void ActionExprEngine(Decl *D, bool ObjCGCEnabled, SetOfDecls *VisitedCallees); 196}; 197} // end anonymous namespace 198 199//===----------------------------------------------------------------------===// 200// AnalysisConsumer implementation. 201//===----------------------------------------------------------------------===// 202llvm::Timer* AnalysisConsumer::TUTotalTimer = 0; 203 204void AnalysisConsumer::HandleDeclContext(ASTContext &C, DeclContext *dc) { 205 // Don't run the actions if an error has occurred with parsing the file. 206 DiagnosticsEngine &Diags = PP.getDiagnostics(); 207 if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) 208 return; 209 210 for (DeclContext::decl_iterator I = dc->decls_begin(), E = dc->decls_end(); 211 I != E; ++I) { 212 HandleDeclContextDecl(C, *I); 213 } 214 215 // If inlining is not turned on, use the simplest function order. 216 if (!Mgr->shouldInlineCall()) { 217 for (DeclContext::decl_iterator I = dc->decls_begin(), E = dc->decls_end(); 218 I != E; ++I) 219 HandleDeclContextDeclFunction(C, *I); 220 return; 221 } 222 223 // Otherwise, use the Callgraph to derive the order. 224 // Build the Call Graph. 225 CallGraph CG; 226 CG.addToCallGraph(dc); 227 228 // Find the top level nodes - children of root + the unreachable (parentless) 229 // nodes. 230 llvm::SmallVector<CallGraphNode*, 24> TopLevelFunctions; 231 CallGraphNode *Entry = CG.getRoot(); 232 for (CallGraphNode::iterator I = Entry->begin(), 233 E = Entry->end(); I != E; ++I) { 234 TopLevelFunctions.push_back(*I); 235 NumFunctionTopLevel++; 236 } 237 for (CallGraph::nodes_iterator TI = CG.parentless_begin(), 238 TE = CG.parentless_end(); TI != TE; ++TI) { 239 TopLevelFunctions.push_back(*TI); 240 NumFunctionTopLevel++; 241 } 242 243 // TODO: Sort TopLevelFunctions. 244 245 // DFS over all of the top level nodes. Use external Visited set, which is 246 // also modified when we inline a function. 247 SmallPtrSet<CallGraphNode*,24> Visited; 248 for (llvm::SmallVector<CallGraphNode*, 24>::iterator 249 TI = TopLevelFunctions.begin(), TE = TopLevelFunctions.end(); 250 TI != TE; ++TI) { 251 for (llvm::df_ext_iterator<CallGraphNode*, SmallPtrSet<CallGraphNode*,24> > 252 DFI = llvm::df_ext_begin(*TI, Visited), 253 E = llvm::df_ext_end(*TI, Visited); 254 DFI != E; ++DFI) { 255 SetOfDecls VisitedCallees; 256 Decl *D = (*DFI)->getDecl(); 257 assert(D); 258 HandleCode(D, (Mgr->InliningMode == All ? 0 : &VisitedCallees)); 259 260 // Add the visited callees to the global visited set. 261 for (SetOfDecls::const_iterator I = VisitedCallees.begin(), 262 E = VisitedCallees.end(); I != E; ++I) { 263 CallGraphNode *VN = CG.getNode(*I); 264 if (VN) 265 Visited.insert(VN); 266 } 267 } 268 } 269} 270 271void AnalysisConsumer::HandleDeclContextDecl(ASTContext &C, Decl *D) { 272 { // Handle callbacks for arbitrary decls. 273 BugReporter BR(*Mgr); 274 checkerMgr->runCheckersOnASTDecl(D, *Mgr, BR); 275 } 276 277 switch (D->getKind()) { 278 case Decl::Namespace: { 279 HandleDeclContext(C, cast<NamespaceDecl>(D)); 280 break; 281 } 282 case Decl::ObjCCategoryImpl: 283 case Decl::ObjCImplementation: { 284 ObjCImplDecl *ID = cast<ObjCImplDecl>(D); 285 for (ObjCContainerDecl::method_iterator MI = ID->meth_begin(), 286 ME = ID->meth_end(); MI != ME; ++MI) { 287 BugReporter BR(*Mgr); 288 checkerMgr->runCheckersOnASTDecl(*MI, *Mgr, BR); 289 } 290 break; 291 } 292 293 default: 294 break; 295 } 296} 297 298void AnalysisConsumer::HandleDeclContextDeclFunction(ASTContext &C, Decl *D) { 299 switch (D->getKind()) { 300 case Decl::CXXConstructor: 301 case Decl::CXXDestructor: 302 case Decl::CXXConversion: 303 case Decl::CXXMethod: 304 case Decl::Function: { 305 FunctionDecl *FD = cast<FunctionDecl>(D); 306 IdentifierInfo *II = FD->getIdentifier(); 307 if (II && II->getName().startswith("__inline")) 308 break; 309 // We skip function template definitions, as their semantics is 310 // only determined when they are instantiated. 311 if (FD->isThisDeclarationADefinition() && 312 !FD->isDependentContext()) { 313 if (!Opts.AnalyzeSpecificFunction.empty() && 314 FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction) 315 break; 316 HandleCode(FD); 317 } 318 break; 319 } 320 321 case Decl::ObjCCategoryImpl: 322 case Decl::ObjCImplementation: { 323 ObjCImplDecl *ID = cast<ObjCImplDecl>(D); 324 for (ObjCContainerDecl::method_iterator MI = ID->meth_begin(), 325 ME = ID->meth_end(); MI != ME; ++MI) { 326 if ((*MI)->isThisDeclarationADefinition()) { 327 if (!Opts.AnalyzeSpecificFunction.empty() && 328 Opts.AnalyzeSpecificFunction != 329 (*MI)->getSelector().getAsString()) 330 continue; 331 HandleCode(*MI); 332 } 333 } 334 break; 335 } 336 337 default: 338 break; 339 } 340} 341 342void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { 343 { 344 if (TUTotalTimer) TUTotalTimer->startTimer(); 345 346 // Introduce a scope to destroy BR before Mgr. 347 BugReporter BR(*Mgr); 348 TranslationUnitDecl *TU = C.getTranslationUnitDecl(); 349 checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR); 350 HandleDeclContext(C, TU); 351 352 // After all decls handled, run checkers on the entire TranslationUnit. 353 checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR); 354 } 355 356 // Explicitly destroy the PathDiagnosticConsumer. This will flush its output. 357 // FIXME: This should be replaced with something that doesn't rely on 358 // side-effects in PathDiagnosticConsumer's destructor. This is required when 359 // used with option -disable-free. 360 Mgr.reset(NULL); 361 362 if (TUTotalTimer) TUTotalTimer->stopTimer(); 363} 364 365static void FindBlocks(DeclContext *D, SmallVectorImpl<Decl*> &WL) { 366 if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) 367 WL.push_back(BD); 368 369 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); 370 I!=E; ++I) 371 if (DeclContext *DC = dyn_cast<DeclContext>(*I)) 372 FindBlocks(DC, WL); 373} 374 375static std::string getFunctionName(const Decl *D) { 376 if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) { 377 return ID->getSelector().getAsString(); 378 } 379 if (const FunctionDecl *ND = dyn_cast<FunctionDecl>(D)) { 380 IdentifierInfo *II = ND->getIdentifier(); 381 if (II) 382 return II->getName(); 383 } 384 return ""; 385} 386 387void AnalysisConsumer::HandleCode(Decl *D, SetOfDecls *VisitedCallees) { 388 if (!Opts.AnalyzeSpecificFunction.empty() && 389 getFunctionName(D) != Opts.AnalyzeSpecificFunction) 390 return; 391 392 DisplayFunction(D); 393 394 // Don't run the actions on declarations in header files unless 395 // otherwise specified. 396 SourceManager &SM = Ctx->getSourceManager(); 397 SourceLocation SL = SM.getExpansionLoc(D->getLocation()); 398 if (!Opts.AnalyzeAll && !SM.isFromMainFile(SL)) 399 return; 400 401 // Clear the AnalysisManager of old AnalysisDeclContexts. 402 Mgr->ClearContexts(); 403 404 // Dispatch on the actions. 405 SmallVector<Decl*, 10> WL; 406 WL.push_back(D); 407 408 if (D->hasBody() && Opts.AnalyzeNestedBlocks) 409 FindBlocks(cast<DeclContext>(D), WL); 410 411 BugReporter BR(*Mgr); 412 for (SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end(); 413 WI != WE; ++WI) 414 if ((*WI)->hasBody()) { 415 checkerMgr->runCheckersOnASTBody(*WI, *Mgr, BR); 416 if (checkerMgr->hasPathSensitiveCheckers()) 417 RunPathSensitiveChecks(*WI, VisitedCallees); 418 } 419 NumFunctionsAnalyzed++; 420} 421 422//===----------------------------------------------------------------------===// 423// Path-sensitive checking. 424//===----------------------------------------------------------------------===// 425 426void AnalysisConsumer::ActionExprEngine(Decl *D, bool ObjCGCEnabled, 427 SetOfDecls *VisitedCallees) { 428 // Construct the analysis engine. First check if the CFG is valid. 429 // FIXME: Inter-procedural analysis will need to handle invalid CFGs. 430 if (!Mgr->getCFG(D)) 431 return; 432 433 ExprEngine Eng(*Mgr, ObjCGCEnabled, VisitedCallees); 434 435 // Set the graph auditor. 436 OwningPtr<ExplodedNode::Auditor> Auditor; 437 if (Mgr->shouldVisualizeUbigraph()) { 438 Auditor.reset(CreateUbiViz()); 439 ExplodedNode::SetAuditor(Auditor.get()); 440 } 441 442 // Execute the worklist algorithm. 443 Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D, 0), 444 Mgr->getMaxNodes()); 445 446 // Release the auditor (if any) so that it doesn't monitor the graph 447 // created BugReporter. 448 ExplodedNode::SetAuditor(0); 449 450 // Visualize the exploded graph. 451 if (Mgr->shouldVisualizeGraphviz()) 452 Eng.ViewGraph(Mgr->shouldTrimGraph()); 453 454 // Display warnings. 455 Eng.getBugReporter().FlushReports(); 456} 457 458void AnalysisConsumer::RunPathSensitiveChecks(Decl *D, SetOfDecls *Visited) { 459 460 switch (Mgr->getLangOpts().getGC()) { 461 case LangOptions::NonGC: 462 ActionExprEngine(D, false, Visited); 463 break; 464 465 case LangOptions::GCOnly: 466 ActionExprEngine(D, true, Visited); 467 break; 468 469 case LangOptions::HybridGC: 470 ActionExprEngine(D, false, Visited); 471 ActionExprEngine(D, true, Visited); 472 break; 473 } 474} 475 476//===----------------------------------------------------------------------===// 477// AnalysisConsumer creation. 478//===----------------------------------------------------------------------===// 479 480ASTConsumer* ento::CreateAnalysisConsumer(const Preprocessor& pp, 481 const std::string& outDir, 482 const AnalyzerOptions& opts, 483 ArrayRef<std::string> plugins) { 484 // Disable the effects of '-Werror' when using the AnalysisConsumer. 485 pp.getDiagnostics().setWarningsAsErrors(false); 486 487 return new AnalysisConsumer(pp, outDir, opts, plugins); 488} 489 490//===----------------------------------------------------------------------===// 491// Ubigraph Visualization. FIXME: Move to separate file. 492//===----------------------------------------------------------------------===// 493 494namespace { 495 496class UbigraphViz : public ExplodedNode::Auditor { 497 OwningPtr<raw_ostream> Out; 498 llvm::sys::Path Dir, Filename; 499 unsigned Cntr; 500 501 typedef llvm::DenseMap<void*,unsigned> VMap; 502 VMap M; 503 504public: 505 UbigraphViz(raw_ostream *out, llvm::sys::Path& dir, 506 llvm::sys::Path& filename); 507 508 ~UbigraphViz(); 509 510 virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst); 511}; 512 513} // end anonymous namespace 514 515static ExplodedNode::Auditor* CreateUbiViz() { 516 std::string ErrMsg; 517 518 llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); 519 if (!ErrMsg.empty()) 520 return 0; 521 522 llvm::sys::Path Filename = Dir; 523 Filename.appendComponent("llvm_ubi"); 524 Filename.makeUnique(true,&ErrMsg); 525 526 if (!ErrMsg.empty()) 527 return 0; 528 529 llvm::errs() << "Writing '" << Filename.str() << "'.\n"; 530 531 OwningPtr<llvm::raw_fd_ostream> Stream; 532 Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg)); 533 534 if (!ErrMsg.empty()) 535 return 0; 536 537 return new UbigraphViz(Stream.take(), Dir, Filename); 538} 539 540void UbigraphViz::AddEdge(ExplodedNode *Src, ExplodedNode *Dst) { 541 542 assert (Src != Dst && "Self-edges are not allowed."); 543 544 // Lookup the Src. If it is a new node, it's a root. 545 VMap::iterator SrcI= M.find(Src); 546 unsigned SrcID; 547 548 if (SrcI == M.end()) { 549 M[Src] = SrcID = Cntr++; 550 *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; 551 } 552 else 553 SrcID = SrcI->second; 554 555 // Lookup the Dst. 556 VMap::iterator DstI= M.find(Dst); 557 unsigned DstID; 558 559 if (DstI == M.end()) { 560 M[Dst] = DstID = Cntr++; 561 *Out << "('vertex', " << DstID << ")\n"; 562 } 563 else { 564 // We have hit DstID before. Change its style to reflect a cache hit. 565 DstID = DstI->second; 566 *Out << "('change_vertex_style', " << DstID << ", 1)\n"; 567 } 568 569 // Add the edge. 570 *Out << "('edge', " << SrcID << ", " << DstID 571 << ", ('arrow','true'), ('oriented', 'true'))\n"; 572} 573 574UbigraphViz::UbigraphViz(raw_ostream *out, llvm::sys::Path& dir, 575 llvm::sys::Path& filename) 576 : Out(out), Dir(dir), Filename(filename), Cntr(0) { 577 578 *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; 579 *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," 580 " ('size', '1.5'))\n"; 581} 582 583UbigraphViz::~UbigraphViz() { 584 Out.reset(0); 585 llvm::errs() << "Running 'ubiviz' program... "; 586 std::string ErrMsg; 587 llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); 588 std::vector<const char*> args; 589 args.push_back(Ubiviz.c_str()); 590 args.push_back(Filename.c_str()); 591 args.push_back(0); 592 593 if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { 594 llvm::errs() << "Error viewing graph: " << ErrMsg << "\n"; 595 } 596 597 // Delete the directory. 598 Dir.eraseFromDisk(true); 599} 600