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