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