AnalysisConsumer.cpp revision e9baa6b82625a6e55d21a32ca0227950d30ec57d
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#include "AnalysisConsumer.h"
15#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/ParentMap.h"
20#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/Analyses/UninitializedValues.h"
22#include "clang/Analysis/CFG.h"
23#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
24#include "clang/StaticAnalyzer/Core/CheckerManager.h"
25#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
26#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
27#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
28#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
29#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
30#include "clang/StaticAnalyzer/Core/PathSensitive/TransferFuncs.h"
31#include "clang/StaticAnalyzer/Core/PathDiagnosticClients.h"
32
33// FIXME: Restructure checker registration.
34#include "../Checkers/ClangSACheckers.h"
35#include "../Checkers/ExperimentalChecks.h"
36#include "../Checkers/InternalChecks.h"
37
38#include "clang/Basic/FileManager.h"
39#include "clang/Basic/SourceManager.h"
40#include "clang/Frontend/AnalyzerOptions.h"
41#include "clang/Lex/Preprocessor.h"
42#include "llvm/Support/raw_ostream.h"
43#include "llvm/Support/Path.h"
44#include "llvm/Support/Program.h"
45#include "llvm/ADT/OwningPtr.h"
46
47using namespace clang;
48using namespace ento;
49
50static ExplodedNode::Auditor* CreateUbiViz();
51
52//===----------------------------------------------------------------------===//
53// Special PathDiagnosticClients.
54//===----------------------------------------------------------------------===//
55
56static PathDiagnosticClient*
57createPlistHTMLDiagnosticClient(const std::string& prefix,
58                                const Preprocessor &PP) {
59  PathDiagnosticClient *PD =
60    createHTMLDiagnosticClient(llvm::sys::path::parent_path(prefix), PP);
61  return createPlistDiagnosticClient(prefix, PP, PD);
62}
63
64//===----------------------------------------------------------------------===//
65// AnalysisConsumer declaration.
66//===----------------------------------------------------------------------===//
67
68namespace {
69
70class AnalysisConsumer : public ASTConsumer {
71public:
72  typedef void (*CodeAction)(AnalysisConsumer &C, AnalysisManager &M, Decl *D);
73  typedef void (*TUAction)(AnalysisConsumer &C, AnalysisManager &M,
74                           TranslationUnitDecl &TU);
75
76private:
77  typedef std::vector<CodeAction> Actions;
78  typedef std::vector<TUAction> TUActions;
79
80  Actions FunctionActions;
81  Actions ObjCMethodActions;
82  Actions ObjCImplementationActions;
83  Actions CXXMethodActions;
84  TUActions TranslationUnitActions; // Remove this.
85
86public:
87  ASTContext* Ctx;
88  const Preprocessor &PP;
89  const std::string OutDir;
90  AnalyzerOptions Opts;
91
92  // PD is owned by AnalysisManager.
93  PathDiagnosticClient *PD;
94
95  StoreManagerCreator CreateStoreMgr;
96  ConstraintManagerCreator CreateConstraintMgr;
97
98  llvm::OwningPtr<CheckerManager> checkerMgr;
99  llvm::OwningPtr<AnalysisManager> Mgr;
100
101  AnalysisConsumer(const Preprocessor& pp,
102                   const std::string& outdir,
103                   const AnalyzerOptions& opts)
104    : Ctx(0), PP(pp), OutDir(outdir),
105      Opts(opts), PD(0) {
106    DigestAnalyzerOptions();
107  }
108
109  void DigestAnalyzerOptions() {
110    // Create the PathDiagnosticClient.
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 = createTextPathDiagnosticClient("", PP);
122    }
123
124    // Create the analyzer component creators.
125    switch (Opts.AnalysisStoreOpt) {
126    default:
127      assert(0 && "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      assert(0 && "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  void addCodeAction(CodeAction action) {
167    FunctionActions.push_back(action);
168    ObjCMethodActions.push_back(action);
169    CXXMethodActions.push_back(action);
170  }
171
172  void addTranslationUnitAction(TUAction action) {
173    TranslationUnitActions.push_back(action);
174  }
175
176  void addObjCImplementationAction(CodeAction action) {
177    ObjCImplementationActions.push_back(action);
178  }
179
180  virtual void Initialize(ASTContext &Context) {
181    Ctx = &Context;
182    checkerMgr.reset(registerCheckers(Opts, PP.getDiagnostics()));
183    Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(),
184                                  PP.getLangOptions(), PD,
185                                  CreateStoreMgr, CreateConstraintMgr,
186                                  checkerMgr.get(),
187                                  /* Indexer */ 0,
188                                  Opts.MaxNodes, Opts.MaxLoop,
189                                  Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
190                                  Opts.PurgeDead, Opts.EagerlyAssume,
191                                  Opts.TrimGraph, Opts.InlineCall,
192                                  Opts.UnoptimizedCFG, Opts.CFGAddImplicitDtors,
193                                  Opts.CFGAddInitializers,
194                                  Opts.EagerlyTrimEGraph));
195  }
196
197  virtual void HandleTranslationUnit(ASTContext &C);
198  void HandleDeclContext(ASTContext &C, DeclContext *dc);
199
200  void HandleCode(Decl *D, Actions& actions);
201};
202} // end anonymous namespace
203
204//===----------------------------------------------------------------------===//
205// AnalysisConsumer implementation.
206//===----------------------------------------------------------------------===//
207
208void AnalysisConsumer::HandleDeclContext(ASTContext &C, DeclContext *dc) {
209  for (DeclContext::decl_iterator I = dc->decls_begin(), E = dc->decls_end();
210       I != E; ++I) {
211    Decl *D = *I;
212
213    switch (D->getKind()) {
214      case Decl::Namespace: {
215        HandleDeclContext(C, cast<NamespaceDecl>(D));
216        break;
217      }
218      case Decl::CXXConstructor:
219      case Decl::CXXDestructor:
220      case Decl::CXXConversion:
221      case Decl::CXXMethod:
222      case Decl::Function: {
223        FunctionDecl* FD = cast<FunctionDecl>(D);
224        // We skip function template definitions, as their semantics is
225        // only determined when they are instantiated.
226        if (FD->isThisDeclarationADefinition() &&
227            !FD->isDependentContext()) {
228          if (!Opts.AnalyzeSpecificFunction.empty() &&
229              FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction)
230            break;
231          DisplayFunction(FD);
232          HandleCode(FD, FunctionActions);
233        }
234        break;
235      }
236
237      case Decl::ObjCImplementation: {
238        ObjCImplementationDecl* ID = cast<ObjCImplementationDecl>(*I);
239        HandleCode(ID, ObjCImplementationActions);
240
241        for (ObjCImplementationDecl::method_iterator MI = ID->meth_begin(),
242             ME = ID->meth_end(); MI != ME; ++MI) {
243          if ((*MI)->isThisDeclarationADefinition()) {
244            if (!Opts.AnalyzeSpecificFunction.empty() &&
245                Opts.AnalyzeSpecificFunction != (*MI)->getSelector().getAsString())
246              break;
247            DisplayFunction(*MI);
248            HandleCode(*MI, ObjCMethodActions);
249          }
250        }
251        break;
252      }
253
254      default:
255        break;
256    }
257  }
258}
259
260void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
261  TranslationUnitDecl *TU = C.getTranslationUnitDecl();
262  HandleDeclContext(C, TU);
263
264  for (TUActions::iterator I = TranslationUnitActions.begin(),
265                           E = TranslationUnitActions.end(); I != E; ++I) {
266    (*I)(*this, *Mgr, *TU);
267  }
268
269  // Explicitly destroy the PathDiagnosticClient.  This will flush its output.
270  // FIXME: This should be replaced with something that doesn't rely on
271  // side-effects in PathDiagnosticClient's destructor. This is required when
272  // used with option -disable-free.
273  Mgr.reset(NULL);
274}
275
276static void FindBlocks(DeclContext *D, llvm::SmallVectorImpl<Decl*> &WL) {
277  if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
278    WL.push_back(BD);
279
280  for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
281       I!=E; ++I)
282    if (DeclContext *DC = dyn_cast<DeclContext>(*I))
283      FindBlocks(DC, WL);
284}
285
286void AnalysisConsumer::HandleCode(Decl *D, Actions& actions) {
287
288  // Don't run the actions if an error has occured with parsing the file.
289  Diagnostic &Diags = PP.getDiagnostics();
290  if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
291    return;
292
293  // Don't run the actions on declarations in header files unless
294  // otherwise specified.
295  SourceManager &SM = Ctx->getSourceManager();
296  SourceLocation SL = SM.getInstantiationLoc(D->getLocation());
297  if (!Opts.AnalyzeAll && !SM.isFromMainFile(SL))
298    return;
299
300  // Clear the AnalysisManager of old AnalysisContexts.
301  Mgr->ClearContexts();
302
303  // Dispatch on the actions.
304  llvm::SmallVector<Decl*, 10> WL;
305  WL.push_back(D);
306
307  if (D->hasBody() && Opts.AnalyzeNestedBlocks)
308    FindBlocks(cast<DeclContext>(D), WL);
309
310  for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I)
311    for (llvm::SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end();
312         WI != WE; ++WI)
313      (*I)(*this, *Mgr, *WI);
314}
315
316//===----------------------------------------------------------------------===//
317// Analyses
318//===----------------------------------------------------------------------===//
319
320static void ActionWarnDeadStores(AnalysisConsumer &C, AnalysisManager& mgr,
321                                 Decl *D) {
322  if (LiveVariables *L = mgr.getLiveVariables(D)) {
323    BugReporter BR(mgr);
324    CheckDeadStores(*mgr.getCFG(D), *L, mgr.getParentMap(D), BR);
325  }
326}
327
328static void ActionWarnUninitVals(AnalysisConsumer &C, AnalysisManager& mgr,
329                                 Decl *D) {
330  if (CFG* c = mgr.getCFG(D)) {
331    CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic());
332  }
333}
334
335
336static void ActionExprEngine(AnalysisConsumer &C, AnalysisManager& mgr,
337                               Decl *D,
338                               TransferFuncs* tf) {
339
340  llvm::OwningPtr<TransferFuncs> TF(tf);
341
342  // Construct the analysis engine.  We first query for the LiveVariables
343  // information to see if the CFG is valid.
344  // FIXME: Inter-procedural analysis will need to handle invalid CFGs.
345  if (!mgr.getLiveVariables(D))
346    return;
347  ExprEngine Eng(mgr, TF.take());
348
349  if (C.Opts.EnableExperimentalInternalChecks)
350    RegisterExperimentalInternalChecks(Eng);
351
352  RegisterAppleChecks(Eng, *D);
353
354  if (C.Opts.EnableExperimentalChecks)
355    RegisterExperimentalChecks(Eng);
356
357  // Enable idempotent operation checking if it was explicitly turned on, or if
358  // we are running experimental checks (i.e. everything)
359  if (C.Opts.IdempotentOps || C.Opts.EnableExperimentalChecks
360      || C.Opts.EnableExperimentalInternalChecks)
361    RegisterIdempotentOperationChecker(Eng);
362
363  if (C.Opts.BufferOverflows)
364    RegisterArrayBoundCheckerV2(Eng);
365
366  // Enable AnalyzerStatsChecker if it was given as an argument
367  if (C.Opts.AnalyzerStats)
368    RegisterAnalyzerStatsChecker(Eng);
369
370  // Set the graph auditor.
371  llvm::OwningPtr<ExplodedNode::Auditor> Auditor;
372  if (mgr.shouldVisualizeUbigraph()) {
373    Auditor.reset(CreateUbiViz());
374    ExplodedNode::SetAuditor(Auditor.get());
375  }
376
377  // Execute the worklist algorithm.
378  Eng.ExecuteWorkList(mgr.getStackFrame(D, 0), mgr.getMaxNodes());
379
380  // Release the auditor (if any) so that it doesn't monitor the graph
381  // created BugReporter.
382  ExplodedNode::SetAuditor(0);
383
384  // Visualize the exploded graph.
385  if (mgr.shouldVisualizeGraphviz())
386    Eng.ViewGraph(mgr.shouldTrimGraph());
387
388  // Display warnings.
389  Eng.getBugReporter().FlushReports();
390}
391
392static void ActionObjCMemCheckerAux(AnalysisConsumer &C, AnalysisManager& mgr,
393                                  Decl *D, bool GCEnabled) {
394
395  TransferFuncs* TF = MakeCFRefCountTF(mgr.getASTContext(),
396                                         GCEnabled,
397                                         mgr.getLangOptions());
398
399  ActionExprEngine(C, mgr, D, TF);
400}
401
402static void ActionObjCMemChecker(AnalysisConsumer &C, AnalysisManager& mgr,
403                               Decl *D) {
404
405 switch (mgr.getLangOptions().getGCMode()) {
406 default:
407   assert (false && "Invalid GC mode.");
408 case LangOptions::NonGC:
409   ActionObjCMemCheckerAux(C, mgr, D, false);
410   break;
411
412 case LangOptions::GCOnly:
413   ActionObjCMemCheckerAux(C, mgr, D, true);
414   break;
415
416 case LangOptions::HybridGC:
417   ActionObjCMemCheckerAux(C, mgr, D, false);
418   ActionObjCMemCheckerAux(C, mgr, D, true);
419   break;
420 }
421}
422
423static void ActionDisplayLiveVariables(AnalysisConsumer &C,
424                                       AnalysisManager& mgr, Decl *D) {
425  if (LiveVariables* L = mgr.getLiveVariables(D)) {
426    L->dumpBlockLiveness(mgr.getSourceManager());
427  }
428}
429
430static void ActionCFGDump(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
431  if (CFG *cfg = mgr.getCFG(D)) {
432    cfg->dump(mgr.getLangOptions());
433  }
434}
435
436static void ActionCFGView(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) {
437  if (CFG *cfg = mgr.getCFG(D)) {
438    cfg->viewCFG(mgr.getLangOptions());
439  }
440}
441
442static void ActionSecuritySyntacticChecks(AnalysisConsumer &C,
443                                          AnalysisManager &mgr, Decl *D) {
444  BugReporter BR(mgr);
445  CheckSecuritySyntaxOnly(D, BR);
446}
447
448static void ActionLLVMConventionChecker(AnalysisConsumer &C,
449                                        AnalysisManager &mgr,
450                                        TranslationUnitDecl &TU) {
451  BugReporter BR(mgr);
452  CheckLLVMConventions(TU, BR);
453}
454
455static void ActionWarnObjCDealloc(AnalysisConsumer &C, AnalysisManager& mgr,
456                                  Decl *D) {
457  if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly)
458    return;
459  BugReporter BR(mgr);
460  CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR);
461}
462
463static void ActionWarnObjCUnusedIvars(AnalysisConsumer &C, AnalysisManager& mgr,
464                                      Decl *D) {
465  BugReporter BR(mgr);
466  CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR);
467}
468
469static void ActionWarnObjCMethSigs(AnalysisConsumer &C, AnalysisManager& mgr,
470                                   Decl *D) {
471  BugReporter BR(mgr);
472  CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR);
473}
474
475static void ActionWarnSizeofPointer(AnalysisConsumer &C, AnalysisManager &mgr,
476                                    Decl *D) {
477  BugReporter BR(mgr);
478  CheckSizeofPointer(D, BR);
479}
480
481//===----------------------------------------------------------------------===//
482// AnalysisConsumer creation.
483//===----------------------------------------------------------------------===//
484
485ASTConsumer* ento::CreateAnalysisConsumer(const Preprocessor& pp,
486                                           const std::string& OutDir,
487                                           const AnalyzerOptions& Opts) {
488  llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(pp, OutDir, Opts));
489
490  for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i)
491    switch (Opts.AnalysisList[i]) {
492#define ANALYSIS(NAME, CMD, DESC, SCOPE)\
493    case NAME:\
494      C->add ## SCOPE ## Action(&Action ## NAME);\
495      break;
496#include "clang/Frontend/Analyses.def"
497    default: break;
498    }
499
500  // Last, disable the effects of '-Werror' when using the AnalysisConsumer.
501  pp.getDiagnostics().setWarningsAsErrors(false);
502
503  return C.take();
504}
505
506//===----------------------------------------------------------------------===//
507// Ubigraph Visualization.  FIXME: Move to separate file.
508//===----------------------------------------------------------------------===//
509
510namespace {
511
512class UbigraphViz : public ExplodedNode::Auditor {
513  llvm::OwningPtr<llvm::raw_ostream> Out;
514  llvm::sys::Path Dir, Filename;
515  unsigned Cntr;
516
517  typedef llvm::DenseMap<void*,unsigned> VMap;
518  VMap M;
519
520public:
521  UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
522              llvm::sys::Path& filename);
523
524  ~UbigraphViz();
525
526  virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst);
527};
528
529} // end anonymous namespace
530
531static ExplodedNode::Auditor* CreateUbiViz() {
532  std::string ErrMsg;
533
534  llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
535  if (!ErrMsg.empty())
536    return 0;
537
538  llvm::sys::Path Filename = Dir;
539  Filename.appendComponent("llvm_ubi");
540  Filename.makeUnique(true,&ErrMsg);
541
542  if (!ErrMsg.empty())
543    return 0;
544
545  llvm::errs() << "Writing '" << Filename.str() << "'.\n";
546
547  llvm::OwningPtr<llvm::raw_fd_ostream> Stream;
548  Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg));
549
550  if (!ErrMsg.empty())
551    return 0;
552
553  return new UbigraphViz(Stream.take(), Dir, Filename);
554}
555
556void UbigraphViz::AddEdge(ExplodedNode* Src, ExplodedNode* Dst) {
557
558  assert (Src != Dst && "Self-edges are not allowed.");
559
560  // Lookup the Src.  If it is a new node, it's a root.
561  VMap::iterator SrcI= M.find(Src);
562  unsigned SrcID;
563
564  if (SrcI == M.end()) {
565    M[Src] = SrcID = Cntr++;
566    *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
567  }
568  else
569    SrcID = SrcI->second;
570
571  // Lookup the Dst.
572  VMap::iterator DstI= M.find(Dst);
573  unsigned DstID;
574
575  if (DstI == M.end()) {
576    M[Dst] = DstID = Cntr++;
577    *Out << "('vertex', " << DstID << ")\n";
578  }
579  else {
580    // We have hit DstID before.  Change its style to reflect a cache hit.
581    DstID = DstI->second;
582    *Out << "('change_vertex_style', " << DstID << ", 1)\n";
583  }
584
585  // Add the edge.
586  *Out << "('edge', " << SrcID << ", " << DstID
587       << ", ('arrow','true'), ('oriented', 'true'))\n";
588}
589
590UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir,
591                         llvm::sys::Path& filename)
592  : Out(out), Dir(dir), Filename(filename), Cntr(0) {
593
594  *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
595  *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
596          " ('size', '1.5'))\n";
597}
598
599UbigraphViz::~UbigraphViz() {
600  Out.reset(0);
601  llvm::errs() << "Running 'ubiviz' program... ";
602  std::string ErrMsg;
603  llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
604  std::vector<const char*> args;
605  args.push_back(Ubiviz.c_str());
606  args.push_back(Filename.c_str());
607  args.push_back(0);
608
609  if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
610    llvm::errs() << "Error viewing graph: " << ErrMsg << "\n";
611  }
612
613  // Delete the directory.
614  Dir.eraseFromDisk(true);
615}
616