AnalysisConsumer.cpp revision 9fcc2ab2ec5e00802880e205568ff3afbd70a773
1f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===//
2f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
3f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//                     The LLVM Compiler Infrastructure
4f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
5f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// This file is distributed under the University of Illinois Open Source
6f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// License. See LICENSE.TXT for details.
7f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
8f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
9f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
10f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// "Meta" ASTConsumer for running different source analyses.
11f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
12f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
13f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
14d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks#define DEBUG_TYPE "AnalysisConsumer"
15d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
16e6348c336fecc8da9288ea367375a1b1cd2358d2Argyrios Kyrtzidis#include "AnalysisConsumer.h"
17f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek#include "clang/AST/ASTConsumer.h"
18f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek#include "clang/AST/Decl.h"
19802be99a6817aba6edb166b93c133da4358aa783Zhongxing Xu#include "clang/AST/DeclCXX.h"
20f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek#include "clang/AST/DeclObjC.h"
21efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar#include "clang/AST/ParentMap.h"
22aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks#include "clang/AST/RecursiveASTVisitor.h"
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Analysis/Analyses/LiveVariables.h"
24efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar#include "clang/Analysis/CFG.h"
25d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks#include "clang/Analysis/CallGraph.h"
2655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/FileManager.h"
2755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/SourceManager.h"
2855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/Preprocessor.h"
2921142581d55918beed544a757e4af3bb865b1812Ted Kremenek#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
3055fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
329b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
3355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/CheckerManager.h"
3455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
359b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
369b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
3755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
38d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks#include "llvm/ADT/DepthFirstIterator.h"
396cb7c1a43b0c8f739d1f54b7fdae5ede86033496Benjamin Kramer#include "llvm/ADT/OwningPtr.h"
406a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks#include "llvm/ADT/SmallPtrSet.h"
4181fb169f42769e02c7425b23885a261c025fd5e6Anna Zaks#include "llvm/ADT/Statistic.h"
4255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/Path.h"
4355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/Program.h"
4455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/Timer.h"
4555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h"
467fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks#include <queue>
477fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
48f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenekusing namespace clang;
499ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenekusing namespace ento;
50d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaksusing llvm::SmallPtrSet;
51f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
52c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xustatic ExplodedNode::Auditor* CreateUbiViz();
53ff944a8c481d6c0f1ad2633e4be9bf8b1dd2a09fZhongxing Xu
543fd5f370a28552976c52e76c3035d79012d78ddaAnna ZaksSTATISTIC(NumFunctionTopLevel, "The # of functions at top level.");
5575f31c4862643ab09479c979fabf754e7ffe1460Anna ZaksSTATISTIC(NumFunctionsAnalyzed,
5639cf781d62daaec09e443c6e66a01cd7543ffb41Anna Zaks                      "The # of functions and blocks analyzed (as top level "
5739cf781d62daaec09e443c6e66a01cd7543ffb41Anna Zaks                      "with inlining turned on).");
58e62f048960645b79363408fdead53fec2a063c52Anna ZaksSTATISTIC(NumBlocksInAnalyzedFunctions,
5939cf781d62daaec09e443c6e66a01cd7543ffb41Anna Zaks                      "The # of basic blocks in the analyzed functions.");
60e62f048960645b79363408fdead53fec2a063c52Anna ZaksSTATISTIC(PercentReachableBlocks, "The % of reachable basic blocks.");
6184c1f4b1edddf74f8080cfc470796c2c498b5d43Anna ZaksSTATISTIC(MaxCFGSize, "The maximum number of basic blocks in a function.");
62d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
63be1fe1eb12a1cb91c8e3a9fcc2db4dfe989def6cTed Kremenek//===----------------------------------------------------------------------===//
64f39d962cf84f46d2c0512157259ae1d41a1a5173David Blaikie// Special PathDiagnosticConsumers.
65f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek//===----------------------------------------------------------------------===//
66f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek
679fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenekstatic void createPlistHTMLDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
689fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek                                              PathDiagnosticConsumers &C,
69c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                              const std::string &prefix,
70c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                              const Preprocessor &PP) {
719fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek  createHTMLDiagnosticConsumer(AnalyzerOpts, C,
729fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek                               llvm::sys::path::parent_path(prefix), PP);
739fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek  createPlistDiagnosticConsumer(AnalyzerOpts, C, prefix, PP);
74f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek}
75f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek
76c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremeneknamespace {
77c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekclass ClangDiagPathDiagConsumer : public PathDiagnosticConsumer {
78c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  DiagnosticsEngine &Diag;
79c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekpublic:
80c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  ClangDiagPathDiagConsumer(DiagnosticsEngine &Diag) : Diag(Diag) {}
81c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  virtual ~ClangDiagPathDiagConsumer() {}
82c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  virtual StringRef getName() const { return "ClangDiags"; }
83c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  virtual PathGenerationScheme getGenerationScheme() const { return None; }
84c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
85c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
86c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                            FilesMade *filesMade) {
87c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    for (std::vector<const PathDiagnostic*>::iterator I = Diags.begin(),
88c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek         E = Diags.end(); I != E; ++I) {
89c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      const PathDiagnostic *PD = *I;
903a46f5fd1709f6df03bbb8b0abf84052dc0f39ffJordan Rose      StringRef desc = PD->getShortDescription();
91c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      SmallString<512> TmpStr;
92c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      llvm::raw_svector_ostream Out(TmpStr);
93c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      for (StringRef::iterator I=desc.begin(), E=desc.end(); I!=E; ++I) {
94c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek        if (*I == '%')
95c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek          Out << "%%";
96c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek        else
97c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek          Out << *I;
98c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      }
99c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      Out.flush();
100c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      unsigned ErrorDiag = Diag.getCustomDiagID(DiagnosticsEngine::Warning,
101c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                                TmpStr);
102c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      SourceLocation L = PD->getLocation().asLocation();
103c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      DiagnosticBuilder diagBuilder = Diag.Report(L, ErrorDiag);
104c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
105c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      // Get the ranges from the last point in the path.
106c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      ArrayRef<SourceRange> Ranges = PD->path.back()->getRanges();
107c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
108c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      for (ArrayRef<SourceRange>::iterator I = Ranges.begin(),
109c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                           E = Ranges.end(); I != E; ++I) {
110c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek        diagBuilder << *I;
111c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      }
112c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    }
113c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  }
114c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek};
115c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek} // end anonymous namespace
116c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
117f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek//===----------------------------------------------------------------------===//
118f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// AnalysisConsumer declaration.
119f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
120f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
121f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremeneknamespace {
122f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
123aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass AnalysisConsumer : public ASTConsumer,
124aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks                         public RecursiveASTVisitor<AnalysisConsumer> {
1254eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  enum {
1264eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    AM_None = 0,
1274eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    AM_Syntax = 0x1,
1284eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    AM_Path = 0x2
129aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  };
1304eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  typedef unsigned AnalysisMode;
131aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
132aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// Mode of the analyzes while recursively visiting Decls.
133aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  AnalysisMode RecVisitorMode;
134aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// Bug Reporter to use while recursively visiting Decls.
135aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  BugReporter *RecVisitorBR;
136aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
137ed8afacb8118b71bcfa8017059e51da325e7691bZhongxing Xupublic:
1389c378f705405d37f49795d5e915989de774fe11fTed Kremenek  ASTContext *Ctx;
1391d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  const Preprocessor &PP;
1401d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  const std::string OutDir;
14145796b10d11869e86c6b85e24df165410536b313Ted Kremenek  AnalyzerOptionsRef Opts;
14208b86531ade68727c56918f162816075b87c864aJordy Rose  ArrayRef<std::string> Plugins;
143d07a0d0279c09d1017f8450fce575a94dc9703c0Zhongxing Xu
1446a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// \brief Stores the declarations from the local translation unit.
1456a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// Note, we pre-compute the local declarations at parse time as an
1466a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// optimization to make sure we do not deserialize everything from disk.
1476a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// The local declaration to all declarations ratio might be very small when
1486a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// working with a PCH file.
1496a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  SetOfDecls LocalTUDecls;
150cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek
151c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  // Set of PathDiagnosticConsumers.  Owned by AnalysisManager.
152c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  PathDiagnosticConsumers PathConsumers;
153d07a0d0279c09d1017f8450fce575a94dc9703c0Zhongxing Xu
1541d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  StoreManagerCreator CreateStoreMgr;
1551d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  ConstraintManagerCreator CreateConstraintMgr;
156f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
1576f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<CheckerManager> checkerMgr;
1586f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<AnalysisManager> Mgr;
159c471e7b44e63ff1b46b480e723c4130aeaef5a8aZhongxing Xu
160d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  /// Time the analyzes time of each translation unit.
161d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  static llvm::Timer* TUTotalTimer;
162d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
1633bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  /// The information about analyzed functions shared throughout the
1643bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  /// translation unit.
1653bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  FunctionSummariesTy FunctionSummaries;
1663bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks
1671d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  AnalysisConsumer(const Preprocessor& pp,
1681d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek                   const std::string& outdir,
16945796b10d11869e86c6b85e24df165410536b313Ted Kremenek                   AnalyzerOptionsRef opts,
17008b86531ade68727c56918f162816075b87c864aJordy Rose                   ArrayRef<std::string> plugins)
1714eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    : RecVisitorMode(0), RecVisitorBR(0),
172c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      Ctx(0), PP(pp), OutDir(outdir), Opts(opts), Plugins(plugins) {
1731d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    DigestAnalyzerOptions();
17445796b10d11869e86c6b85e24df165410536b313Ted Kremenek    if (Opts->PrintStats) {
175d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks      llvm::EnableStatistics();
176d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks      TUTotalTimer = new llvm::Timer("Analyzer Total Time");
177d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks    }
178d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  }
179d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
180d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  ~AnalysisConsumer() {
18145796b10d11869e86c6b85e24df165410536b313Ted Kremenek    if (Opts->PrintStats)
182d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks      delete TUTotalTimer;
1831d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  }
184fda7832b000ff8927386f093b52c067641679469Zhongxing Xu
1851d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  void DigestAnalyzerOptions() {
186ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie    // Create the PathDiagnosticConsumer.
187c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    PathConsumers.push_back(new ClangDiagPathDiagConsumer(PP.getDiagnostics()));
188c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
1891d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    if (!OutDir.empty()) {
19045796b10d11869e86c6b85e24df165410536b313Ted Kremenek      switch (Opts->AnalysisDiagOpt) {
1911d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek      default:
192fda7832b000ff8927386f093b52c067641679469Zhongxing Xu#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \
1939fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek        case PD_##NAME: CREATEFN(*Opts.getPtr(), PathConsumers, OutDir, PP);\
1949fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek        break;
195987695a5ddd78beca784d4e503ffbc2dc879181aTed Kremenek#include "clang/StaticAnalyzer/Core/Analyses.def"
196fda7832b000ff8927386f093b52c067641679469Zhongxing Xu      }
19745796b10d11869e86c6b85e24df165410536b313Ted Kremenek    } else if (Opts->AnalysisDiagOpt == PD_TEXT) {
198a599ae8826b01c0160a519b0fc5a4871f599bf04Argyrios Kyrtzidis      // Create the text client even without a specified output file since
199a599ae8826b01c0160a519b0fc5a4871f599bf04Argyrios Kyrtzidis      // it just uses diagnostic notes.
2009fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek      createTextPathDiagnosticConsumer(*Opts.getPtr(), PathConsumers, "", PP);
2011d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    }
202fda7832b000ff8927386f093b52c067641679469Zhongxing Xu
2031d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    // Create the analyzer component creators.
20445796b10d11869e86c6b85e24df165410536b313Ted Kremenek    switch (Opts->AnalysisStoreOpt) {
2055f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis    default:
206b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unknown store manager.");
207fda7832b000ff8927386f093b52c067641679469Zhongxing Xu#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN)           \
2085f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis      case NAME##Model: CreateStoreMgr = CREATEFN; break;
209987695a5ddd78beca784d4e503ffbc2dc879181aTed Kremenek#include "clang/StaticAnalyzer/Core/Analyses.def"
2101d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    }
2111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21245796b10d11869e86c6b85e24df165410536b313Ted Kremenek    switch (Opts->AnalysisConstraintsOpt) {
2135f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis    default:
214b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unknown store manager.");
215fda7832b000ff8927386f093b52c067641679469Zhongxing Xu#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN)     \
2165f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis      case NAME##Model: CreateConstraintMgr = CREATEFN; break;
217987695a5ddd78beca784d4e503ffbc2dc879181aTed Kremenek#include "clang/StaticAnalyzer/Core/Analyses.def"
218fda7832b000ff8927386f093b52c067641679469Zhongxing Xu    }
2191d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  }
220f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
221aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  void DisplayFunction(const Decl *D, AnalysisMode Mode) {
22245796b10d11869e86c6b85e24df165410536b313Ted Kremenek    if (!Opts->AnalyzerDisplayProgress)
2231d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek      return;
224f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
225fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek    SourceManager &SM = Mgr->getASTContext().getSourceManager();
226fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek    PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
227cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor    if (Loc.isValid()) {
228aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      llvm::errs() << "ANALYZE";
2294eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose
2304eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      if (Mode == AM_Syntax)
2314eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose        llvm::errs() << " (Syntax)";
2324eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      else if (Mode == AM_Path)
2334eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose        llvm::errs() << " (Path)";
2344eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      else
2354eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose        assert(Mode == (AM_Syntax | AM_Path) && "Unexpected mode!");
2364eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose
237aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      llvm::errs() << ": " << Loc.getFilename();
238cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
239cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor        const NamedDecl *ND = cast<NamedDecl>(D);
240b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer        llvm::errs() << ' ' << *ND << '\n';
241cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      }
242cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      else if (isa<BlockDecl>(D)) {
243cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor        llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:"
244cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor                     << Loc.getColumn() << '\n';
245cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      }
246cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
247cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor        Selector S = MD->getSelector();
248cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor        llvm::errs() << ' ' << S.getAsString();
249cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      }
25035fa76d0bb6fb8c86159a7506efd094a4fe376d2Ted Kremenek    }
2511d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  }
2521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2531d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  virtual void Initialize(ASTContext &Context) {
2541d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    Ctx = &Context;
25545796b10d11869e86c6b85e24df165410536b313Ted Kremenek    checkerMgr.reset(createCheckerManager(*Opts, PP.getLangOpts(), Plugins,
25608b86531ade68727c56918f162816075b87c864aJordy Rose                                          PP.getDiagnostics()));
257c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    Mgr.reset(new AnalysisManager(*Ctx,
258c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  PP.getDiagnostics(),
259c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  PP.getLangOpts(),
260c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  PathConsumers,
261c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  CreateStoreMgr,
262c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  CreateConstraintMgr,
26343dee220252ef0b42c5f8a3bb1eca97f84f2565fArgyrios Kyrtzidis                                  checkerMgr.get(),
26445796b10d11869e86c6b85e24df165410536b313Ted Kremenek                                  *Opts));
2651d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  }
2661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2676a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// \brief Store the top level decls in the set to be processed later on.
2686a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// (Doing this pre-processing avoids deserialization of data from PCH.)
2696a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  virtual bool HandleTopLevelDecl(DeclGroupRef D);
2706a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  virtual void HandleTopLevelDeclInObjCContainer(DeclGroupRef D);
2716a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
2721d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  virtual void HandleTranslationUnit(ASTContext &C);
27314cc9451de4a9539bf79e4e5d63248c2377426dbTed Kremenek
27475f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  /// \brief Determine which inlining mode should be used when this function is
27575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  /// analyzed. For example, determines if the callees should be inlined.
27675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  ExprEngine::InliningModes
277b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  getInliningModeForFunction(const Decl *D, SetOfConstDecls Visited);
27875f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
2796a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// \brief Build the call graph for all the top level decls of this TU and
2806a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// use it to define the order in which the functions should be visited.
281469ba550f54577f28e23ea42c293308fbcad6609Jordan Rose  void HandleDeclsCallGraph(const unsigned LocalTUDeclsSize);
282aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
283aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// \brief Run analyzes(syntax or path sensitive) on the given function.
284aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// \param Mode - determines if we are requesting syntax only or path
285aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// sensitive only analysis.
286aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// \param VisitedCallees - The output parameter, which is populated with the
287aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// set of functions which should be considered analyzed after analyzing the
288aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// given root function.
2896a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  void HandleCode(Decl *D, AnalysisMode Mode,
29075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                  ExprEngine::InliningModes IMode = ExprEngine::Inline_None,
2916a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                  SetOfConstDecls *VisitedCallees = 0);
292aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
29375f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  void RunPathSensitiveChecks(Decl *D,
29475f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                              ExprEngine::InliningModes IMode,
29575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                              SetOfConstDecls *VisitedCallees);
2966a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  void ActionExprEngine(Decl *D, bool ObjCGCEnabled,
29775f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                        ExprEngine::InliningModes IMode,
2986a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                        SetOfConstDecls *VisitedCallees);
299aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
300aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// Visitors for the RecursiveASTVisitor.
301c8848f34bbde083b8d89f445eb605eaabf30d6a8Anna Zaks  bool shouldWalkTypesOfTypeLocs() const { return false; }
302aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
303aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// Handle callbacks for arbitrary Decls.
304aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool VisitDecl(Decl *D) {
3054eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    AnalysisMode Mode = getModeForDecl(D, RecVisitorMode);
3064eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    if (Mode & AM_Syntax)
3074eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      checkerMgr->runCheckersOnASTDecl(D, *Mgr, *RecVisitorBR);
308aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    return true;
309aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  }
310aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
311aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool VisitFunctionDecl(FunctionDecl *FD) {
312aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    IdentifierInfo *II = FD->getIdentifier();
313aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    if (II && II->getName().startswith("__inline"))
314aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      return true;
315aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
316aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // We skip function template definitions, as their semantics is
317aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // only determined when they are instantiated.
318aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    if (FD->isThisDeclarationADefinition() &&
319aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks        !FD->isDependentContext()) {
32075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks      assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
321aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      HandleCode(FD, RecVisitorMode);
322aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    }
323aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    return true;
324aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  }
325aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
326aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool VisitObjCMethodDecl(ObjCMethodDecl *MD) {
32775f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    if (MD->isThisDeclarationADefinition()) {
32875f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks      assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
329aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      HandleCode(MD, RecVisitorMode);
33075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    }
331aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    return true;
332aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  }
3336a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
3346a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksprivate:
3356a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  void storeTopLevelDecls(DeclGroupRef DG);
3366a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
3376a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// \brief Check if we should skip (not analyze) the given function.
3384eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  AnalysisMode getModeForDecl(Decl *D, AnalysisMode Mode);
3396a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
3401d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek};
341f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek} // end anonymous namespace
342f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
343aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
344f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
345f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// AnalysisConsumer implementation.
346f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
347d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaksllvm::Timer* AnalysisConsumer::TUTotalTimer = 0;
348f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
3496a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksbool AnalysisConsumer::HandleTopLevelDecl(DeclGroupRef DG) {
3506a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  storeTopLevelDecls(DG);
3516a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  return true;
3526a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks}
3536a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
3546a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksvoid AnalysisConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) {
3556a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  storeTopLevelDecls(DG);
3566a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks}
3576a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
3586a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksvoid AnalysisConsumer::storeTopLevelDecls(DeclGroupRef DG) {
3596a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {
3606a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
3616a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    // Skip ObjCMethodDecl, wait for the objc container to avoid
3626a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    // analyzing twice.
3636a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    if (isa<ObjCMethodDecl>(*I))
3646a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks      continue;
3656a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
366577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    LocalTUDecls.push_back(*I);
3676a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  }
3686a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks}
3696a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
370b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaksstatic bool shouldSkipFunction(const Decl *D,
371b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks                               SetOfConstDecls Visited,
372b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks                               SetOfConstDecls VisitedAsTopLevel) {
373b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  if (VisitedAsTopLevel.count(D))
37475f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    return true;
37575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
37675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  // We want to re-analyse the functions as top level in the following cases:
377fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  // - The 'init' methods should be reanalyzed because
378fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  //   ObjCNonNilReturnValueChecker assumes that '[super init]' never returns
37975f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  //   'nil' and unless we analyze the 'init' functions as top level, we will
38075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  //   not catch errors within defensive code.
381fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  // - We want to reanalyze all ObjC methods as top level to report Retain
382fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  //   Count naming convention errors more aggressively.
383b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  if (isa<ObjCMethodDecl>(D))
384fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks    return false;
385fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks
386fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  // Otherwise, if we visited the function before, do not reanalyze it.
387b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  return Visited.count(D);
388fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks}
389fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks
39075f31c4862643ab09479c979fabf754e7ffe1460Anna ZaksExprEngine::InliningModes
391b467051a9f212c57335f88f16e839e1dc8afc1c1Anna ZaksAnalysisConsumer::getInliningModeForFunction(const Decl *D,
392b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks                                             SetOfConstDecls Visited) {
39375f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  ExprEngine::InliningModes HowToInline =
39475f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks      (Mgr->shouldInlineCall()) ? ExprEngine::Inline_All :
39575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                                  ExprEngine::Inline_None;
39675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
39775f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  // We want to reanalyze all ObjC methods as top level to report Retain
39875f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  // Count naming convention errors more aggressively. But we can turn off
39975f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  // inlining when reanalyzing an already inlined function.
400b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  if (Visited.count(D)) {
401b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    assert(isa<ObjCMethodDecl>(D) &&
40275f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks           "We are only reanalyzing ObjCMethods.");
403b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    const ObjCMethodDecl *ObjCM = cast<ObjCMethodDecl>(D);
40475f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    if (ObjCM->getMethodFamily() != OMF_init)
40575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks      HowToInline = ExprEngine::Inline_None;
40675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  }
40775f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
40875f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  return HowToInline;
40975f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks}
41075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
411469ba550f54577f28e23ea42c293308fbcad6609Jordan Rosevoid AnalysisConsumer::HandleDeclsCallGraph(const unsigned LocalTUDeclsSize) {
412d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Otherwise, use the Callgraph to derive the order.
413d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Build the Call Graph.
414d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  CallGraph CG;
415eaa069075f060f58840af03e0bd5bd75bb27e809Anna Zaks
4166a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  // Add all the top level declarations to the graph.
417694a942b0a14e02757a695142c45437eec6e3684Anna Zaks  // Note: CallGraph can trigger deserialization of more items from a pch
418694a942b0a14e02757a695142c45437eec6e3684Anna Zaks  // (though HandleInterestingDecl); triggering additions to LocalTUDecls.
419694a942b0a14e02757a695142c45437eec6e3684Anna Zaks  // We rely on random access to add the initially processed Decls to CG.
420694a942b0a14e02757a695142c45437eec6e3684Anna Zaks  for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) {
421eaa069075f060f58840af03e0bd5bd75bb27e809Anna Zaks    CG.addToCallGraph(LocalTUDecls[i]);
422eaa069075f060f58840af03e0bd5bd75bb27e809Anna Zaks  }
423d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
424d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Find the top level nodes - children of root + the unreachable (parentless)
425d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // nodes.
4263fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  llvm::SmallVector<CallGraphNode*, 24> TopLevelFunctions;
427b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks  for (CallGraph::nodes_iterator TI = CG.parentless_begin(),
428b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks                                 TE = CG.parentless_end(); TI != TE; ++TI) {
429b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks    TopLevelFunctions.push_back(*TI);
430b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks    NumFunctionTopLevel++;
431b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks  }
432d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  CallGraphNode *Entry = CG.getRoot();
433d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  for (CallGraphNode::iterator I = Entry->begin(),
4343fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks                               E = Entry->end(); I != E; ++I) {
435d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    TopLevelFunctions.push_back(*I);
4363fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks    NumFunctionTopLevel++;
4373fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  }
438d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
439b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks  // Make sure the nodes are sorted in order reverse of their definition in the
440b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks  // translation unit. This step is very important for performance. It ensures
441b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks  // that we analyze the root functions before the externally available
442b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks  // subroutines.
443cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek  std::deque<CallGraphNode*> BFSQueue;
444b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks  for (llvm::SmallVector<CallGraphNode*, 24>::reverse_iterator
445b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks         TI = TopLevelFunctions.rbegin(), TE = TopLevelFunctions.rend();
4467fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks         TI != TE; ++TI)
447371b477836f289f2e9caaab58530f187b51bc86dTed Kremenek    BFSQueue.push_back(*TI);
448d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
4497fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  // BFS over all of the functions, while skipping the ones inlined into
4507fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  // the previously processed functions. Use external Visited set, which is
451d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // also modified when we inline a function.
452b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  SetOfConstDecls Visited;
453b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  SetOfConstDecls VisitedAsTopLevel;
4547fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  while(!BFSQueue.empty()) {
4557fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CallGraphNode *N = BFSQueue.front();
456cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek    BFSQueue.pop_front();
4577fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
458371b477836f289f2e9caaab58530f187b51bc86dTed Kremenek    // Push the children into the queue.
459371b477836f289f2e9caaab58530f187b51bc86dTed Kremenek    for (CallGraphNode::const_iterator CI = N->begin(),
460371b477836f289f2e9caaab58530f187b51bc86dTed Kremenek         CE = N->end(); CI != CE; ++CI) {
461b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks      if (!shouldSkipFunction((*CI)->getDecl(), Visited, VisitedAsTopLevel))
462371b477836f289f2e9caaab58530f187b51bc86dTed Kremenek        BFSQueue.push_back(*CI);
463371b477836f289f2e9caaab58530f187b51bc86dTed Kremenek    }
464371b477836f289f2e9caaab58530f187b51bc86dTed Kremenek
465b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    Decl *D = N->getDecl();
466b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    assert(D);
467b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks
4687fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    // Skip the functions which have been processed already or previously
4697fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    // inlined.
470b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    if (shouldSkipFunction(D, Visited, VisitedAsTopLevel))
4717fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks      continue;
4727fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
4737fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    // Analyze the function.
4746a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    SetOfConstDecls VisitedCallees;
47575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
476b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    HandleCode(D, AM_Path, getInliningModeForFunction(D, Visited),
477255d4d4226b24036ceb11228fbb74286e58620f7Ted Kremenek               (Mgr->options.InliningMode == All ? 0 : &VisitedCallees));
4787fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
4797fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    // Add the visited callees to the global visited set.
480cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek    for (SetOfConstDecls::iterator I = VisitedCallees.begin(),
481cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek                                   E = VisitedCallees.end(); I != E; ++I) {
482b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks        Visited.insert(*I);
4837fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    }
484b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    VisitedAsTopLevel.insert(D);
485d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  }
486fee618af5dd7dee2caaa7347b372eb3dc5fdeffcTed Kremenek}
487fee618af5dd7dee2caaa7347b372eb3dc5fdeffcTed Kremenek
48814cc9451de4a9539bf79e4e5d63248c2377426dbTed Kremenekvoid AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
489aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Don't run the actions if an error has occurred with parsing the file.
490aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  DiagnosticsEngine &Diags = PP.getDiagnostics();
491aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
492aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    return;
493aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
494c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  {
495d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks    if (TUTotalTimer) TUTotalTimer->startTimer();
496d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
497c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    // Introduce a scope to destroy BR before Mgr.
498c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    BugReporter BR(*Mgr);
499c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    TranslationUnitDecl *TU = C.getTranslationUnitDecl();
500c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR);
501aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
502aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // Run the AST-only checks using the order in which functions are defined.
503aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // If inlining is not turned on, use the simplest function order for path
504aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // sensitive analyzes as well.
5054eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    RecVisitorMode = AM_Syntax;
5064eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    if (!Mgr->shouldInlineCall())
5074eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      RecVisitorMode |= AM_Path;
508aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    RecVisitorBR = &BR;
5096a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
5106a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    // Process all the top level declarations.
511cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek    //
512577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    // Note: TraverseDecl may modify LocalTUDecls, but only by appending more
513577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    // entries.  Thus we don't use an iterator, but rely on LocalTUDecls
514577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    // random access.  By doing so, we automatically compensate for iterators
515577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    // possibly being invalidated, although this is a bit slower.
516694a942b0a14e02757a695142c45437eec6e3684Anna Zaks    const unsigned LocalTUDeclsSize = LocalTUDecls.size();
517694a942b0a14e02757a695142c45437eec6e3684Anna Zaks    for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) {
518577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek      TraverseDecl(LocalTUDecls[i]);
519cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek    }
520aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
521aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    if (Mgr->shouldInlineCall())
522469ba550f54577f28e23ea42c293308fbcad6609Jordan Rose      HandleDeclsCallGraph(LocalTUDeclsSize);
523b317f8f5ca8737a5bbad97a3f7566a2dbd2ed61bZhongxing Xu
524c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    // After all decls handled, run checkers on the entire TranslationUnit.
525c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR);
526aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
527aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    RecVisitorBR = 0;
528c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  }
5299be6e7ce5788e50c62d40c59b0bbc2ea423683f7Ted Kremenek
530ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  // Explicitly destroy the PathDiagnosticConsumer.  This will flush its output.
531690a7f431d6863a101711e67636d51ddd13f35c5Ted Kremenek  // FIXME: This should be replaced with something that doesn't rely on
532ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  // side-effects in PathDiagnosticConsumer's destructor. This is required when
533da17fd50ad485fd2a1fc5c2f055caacf532992daZhongxing Xu  // used with option -disable-free.
534d07a0d0279c09d1017f8450fce575a94dc9703c0Zhongxing Xu  Mgr.reset(NULL);
535d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
536d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  if (TUTotalTimer) TUTotalTimer->stopTimer();
5376b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks
5386b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks  // Count how many basic blocks we have not covered.
5396b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks  NumBlocksInAnalyzedFunctions = FunctionSummaries.getTotalNumBasicBlocks();
5406b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks  if (NumBlocksInAnalyzedFunctions > 0)
5416b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks    PercentReachableBlocks =
5426b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks      (FunctionSummaries.getTotalNumVisitedBasicBlocks() * 100) /
5436b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks        NumBlocksInAnalyzedFunctions;
5446b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks
545db09a4dee28a4515438af60f2d2b4a83e4965c31Ted Kremenek}
546db09a4dee28a4515438af60f2d2b4a83e4965c31Ted Kremenek
5475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerstatic void FindBlocks(DeclContext *D, SmallVectorImpl<Decl*> &WL) {
548fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
549fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek    WL.push_back(BD);
550f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
551fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
552fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek       I!=E; ++I)
553fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek    if (DeclContext *DC = dyn_cast<DeclContext>(*I))
554fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek      FindBlocks(DC, WL);
555fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek}
556fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek
557d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaksstatic std::string getFunctionName(const Decl *D) {
558d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) {
559d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    return ID->getSelector().getAsString();
560d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  }
561d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  if (const FunctionDecl *ND = dyn_cast<FunctionDecl>(D)) {
562d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    IdentifierInfo *II = ND->getIdentifier();
563d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    if (II)
564d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks      return II->getName();
565d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  }
566d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  return "";
567d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks}
568d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
5694eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan RoseAnalysisConsumer::AnalysisMode
5704eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan RoseAnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
57145796b10d11869e86c6b85e24df165410536b313Ted Kremenek  if (!Opts->AnalyzeSpecificFunction.empty() &&
57245796b10d11869e86c6b85e24df165410536b313Ted Kremenek      getFunctionName(D) != Opts->AnalyzeSpecificFunction)
5734eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    return AM_None;
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5754eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // Unless -analyze-all is specified, treat decls differently depending on
5764eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // where they came from:
5774eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // - Main source file: run both path-sensitive and non-path-sensitive checks.
5784eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // - Header files: run non-path-sensitive checks only.
5794eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // - System headers: don't run any checks.
580fcd783d583d270b7ec1ec3e0fcf83cd93d30e381Ted Kremenek  SourceManager &SM = Ctx->getSourceManager();
581402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation SL = SM.getExpansionLoc(D->getLocation());
5824eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  if (!Opts->AnalyzeAll && !SM.isFromMainFile(SL)) {
5834eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    if (SL.isInvalid() || SM.isInSystemHeader(SL))
5844eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      return AM_None;
5854eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    return Mode & ~AM_Path;
5864eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  }
58798520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks
5884eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  return Mode;
58998520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks}
59098520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks
591aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksvoid AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
59275f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                                  ExprEngine::InliningModes IMode,
5936a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                                  SetOfConstDecls *VisitedCallees) {
5944eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  Mode = getModeForDecl(D, Mode);
5954eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  if (Mode == AM_None)
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return;
597f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
598aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  DisplayFunction(D, Mode);
59984c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks  CFG *DeclCFG = Mgr->getCFG(D);
60084c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks  if (DeclCFG) {
60184c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks    unsigned CFGSize = DeclCFG->size();
60284c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks    MaxCFGSize = MaxCFGSize < CFGSize ? CFGSize : MaxCFGSize;
60384c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks  }
60484c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks
60598520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks
6061d26f48dc2eea1c07431ca1519d7034a21b9bcffTed Kremenek  // Clear the AnalysisManager of old AnalysisDeclContexts.
60758f5ec7d56b1ebf5f90ee11226ebe7663f2821eaTed Kremenek  Mgr->ClearContexts();
608f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Dispatch on the actions.
6105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl*, 10> WL;
611fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  WL.push_back(D);
612f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
61345796b10d11869e86c6b85e24df165410536b313Ted Kremenek  if (D->hasBody() && Opts->AnalyzeNestedBlocks)
614fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek    FindBlocks(cast<DeclContext>(D), WL);
615f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
6169fb9474c5b267400d4abfbff63c8b39f378235d4Argyrios Kyrtzidis  BugReporter BR(*Mgr);
6175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end();
6189fb9474c5b267400d4abfbff63c8b39f378235d4Argyrios Kyrtzidis       WI != WE; ++WI)
619d655ab28fdf7c940d3f79f8f287954d7f76e0977Argyrios Kyrtzidis    if ((*WI)->hasBody()) {
6204eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      if (Mode & AM_Syntax)
621aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks        checkerMgr->runCheckersOnASTBody(*WI, *Mgr, BR);
6224eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      if ((Mode & AM_Path) && checkerMgr->hasPathSensitiveCheckers()) {
62375f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks        RunPathSensitiveChecks(*WI, IMode, VisitedCallees);
62439cf781d62daaec09e443c6e66a01cd7543ffb41Anna Zaks        if (IMode != ExprEngine::Inline_None)
62539cf781d62daaec09e443c6e66a01cd7543ffb41Anna Zaks          NumFunctionsAnalyzed++;
6261f5171edc9fb9b0698a75fa981c29a750b0a6082Anna Zaks      }
627d655ab28fdf7c940d3f79f8f287954d7f76e0977Argyrios Kyrtzidis    }
628f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek}
629f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
630f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
631d655ab28fdf7c940d3f79f8f287954d7f76e0977Argyrios Kyrtzidis// Path-sensitive checking.
632f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
633f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
6343fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaksvoid AnalysisConsumer::ActionExprEngine(Decl *D, bool ObjCGCEnabled,
63575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                                        ExprEngine::InliningModes IMode,
6366a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                                        SetOfConstDecls *VisitedCallees) {
637a5937bbfd19e61d651a58b0f0ffeef68457902a5Ted Kremenek  // Construct the analysis engine.  First check if the CFG is valid.
63875d03cffe20b5c945ef04eba208efb0437339997Ted Kremenek  // FIXME: Inter-procedural analysis will need to handle invalid CFGs.
6393fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  if (!Mgr->getCFG(D))
640f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek    return;
6413fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks
642d4aeb8050a1d0fe47c53a73361c8b0b8ac310f46Ted Kremenek  // See if the LiveVariables analysis scales.
643d4aeb8050a1d0fe47c53a73361c8b0b8ac310f46Ted Kremenek  if (!Mgr->getAnalysisDeclContext(D)->getAnalysis<RelaxedLiveVariables>())
644d4aeb8050a1d0fe47c53a73361c8b0b8ac310f46Ted Kremenek    return;
645d4aeb8050a1d0fe47c53a73361c8b0b8ac310f46Ted Kremenek
64675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  ExprEngine Eng(*Mgr, ObjCGCEnabled, VisitedCallees, &FunctionSummaries,IMode);
647f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
648f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Set the graph auditor.
6496f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<ExplodedNode::Auditor> Auditor;
6503b8a04004afa46057a9af4afbd086be71d619793Ted Kremenek  if (Mgr->options.visualizeExplodedGraphWithUbiGraph) {
651f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    Auditor.reset(CreateUbiViz());
652c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xu    ExplodedNode::SetAuditor(Auditor.get());
653f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  }
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
655b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek  // Execute the worklist algorithm.
656d200187bd27f9ad68699693a6e57f9ee3ff260faJordy Rose  Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D),
657255d4d4226b24036ceb11228fbb74286e58620f7Ted Kremenek                      Mgr->options.MaxNodes);
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
659f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Release the auditor (if any) so that it doesn't monitor the graph
660f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // created BugReporter.
661c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xu  ExplodedNode::SetAuditor(0);
6623df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek
66334d7734b6ed1d9c0f647405e065251eb67f42badTed Kremenek  // Visualize the exploded graph.
6643b8a04004afa46057a9af4afbd086be71d619793Ted Kremenek  if (Mgr->options.visualizeExplodedGraphWithGraphViz)
665255d4d4226b24036ceb11228fbb74286e58620f7Ted Kremenek    Eng.ViewGraph(Mgr->options.TrimGraph);
6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6673df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek  // Display warnings.
6683df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek  Eng.getBugReporter().FlushReports();
669bc46f345838b1c0d420dbd3655c94f5f360fb5b8Ted Kremenek}
670bc46f345838b1c0d420dbd3655c94f5f360fb5b8Ted Kremenek
6716a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksvoid AnalysisConsumer::RunPathSensitiveChecks(Decl *D,
67275f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                                              ExprEngine::InliningModes IMode,
6736a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                                              SetOfConstDecls *Visited) {
67417a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose
6754e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  switch (Mgr->getLangOpts().getGC()) {
67617a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  case LangOptions::NonGC:
67775f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    ActionExprEngine(D, false, IMode, Visited);
67817a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose    break;
67917a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose
68017a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  case LangOptions::GCOnly:
68175f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    ActionExprEngine(D, true, IMode, Visited);
68217a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose    break;
68317a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose
68417a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  case LangOptions::HybridGC:
68575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    ActionExprEngine(D, false, IMode, Visited);
68675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    ActionExprEngine(D, true, IMode, Visited);
68717a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose    break;
68817a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  }
689b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek}
690b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek
691f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
692f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// AnalysisConsumer creation.
693f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
694f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
6959ef6537a894c33003359b1f9b9676e9178e028b7Ted KremenekASTConsumer* ento::CreateAnalysisConsumer(const Preprocessor& pp,
69608b86531ade68727c56918f162816075b87c864aJordy Rose                                          const std::string& outDir,
69745796b10d11869e86c6b85e24df165410536b313Ted Kremenek                                          AnalyzerOptionsRef opts,
69808b86531ade68727c56918f162816075b87c864aJordy Rose                                          ArrayRef<std::string> plugins) {
69908b86531ade68727c56918f162816075b87c864aJordy Rose  // Disable the effects of '-Werror' when using the AnalysisConsumer.
700efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar  pp.getDiagnostics().setWarningsAsErrors(false);
701be1fe1eb12a1cb91c8e3a9fcc2db4dfe989def6cTed Kremenek
70208b86531ade68727c56918f162816075b87c864aJordy Rose  return new AnalysisConsumer(pp, outDir, opts, plugins);
703f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek}
704f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
705f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek//===----------------------------------------------------------------------===//
706f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek// Ubigraph Visualization.  FIXME: Move to separate file.
707f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek//===----------------------------------------------------------------------===//
708f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
709f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremeneknamespace {
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
711c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xuclass UbigraphViz : public ExplodedNode::Auditor {
7126f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<raw_ostream> Out;
713710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  llvm::sys::Path Dir, Filename;
714f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  unsigned Cntr;
715f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
716f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  typedef llvm::DenseMap<void*,unsigned> VMap;
717f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  VMap M;
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
719f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenekpublic:
7209c378f705405d37f49795d5e915989de774fe11fTed Kremenek  UbigraphViz(raw_ostream *out, llvm::sys::Path& dir,
72156b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek              llvm::sys::Path& filename);
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
723710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  ~UbigraphViz();
7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7259c378f705405d37f49795d5e915989de774fe11fTed Kremenek  virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst);
726f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek};
7271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
728f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek} // end anonymous namespace
729f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
730c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xustatic ExplodedNode::Auditor* CreateUbiViz() {
731f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  std::string ErrMsg;
7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
733710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg);
734f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  if (!ErrMsg.empty())
735f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    return 0;
736f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
737710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  llvm::sys::Path Filename = Dir;
738f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  Filename.appendComponent("llvm_ubi");
739f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  Filename.makeUnique(true,&ErrMsg);
740f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
741f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  if (!ErrMsg.empty())
742f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    return 0;
743f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
744d57a7ef9252964bc6c8471451d7bd395b0520cb8Chris Lattner  llvm::errs() << "Writing '" << Filename.str() << "'.\n";
7451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7466f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  OwningPtr<llvm::raw_fd_ostream> Stream;
747b044c473a155b7e827680635c1699cb2b5b6c0eeDan Gohman  Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg));
748f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
749f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  if (!ErrMsg.empty())
750f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    return 0;
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
752710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  return new UbigraphViz(Stream.take(), Dir, Filename);
753f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek}
754f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
7559c378f705405d37f49795d5e915989de774fe11fTed Kremenekvoid UbigraphViz::AddEdge(ExplodedNode *Src, ExplodedNode *Dst) {
7561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
75745479c88c8a44c55d27c3bd855273bb318082f37Ted Kremenek  assert (Src != Dst && "Self-edges are not allowed.");
7581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
759f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Lookup the Src.  If it is a new node, it's a root.
760f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  VMap::iterator SrcI= M.find(Src);
761f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  unsigned SrcID;
7621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
763f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  if (SrcI == M.end()) {
764f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    M[Src] = SrcID = Cntr++;
765f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n";
766f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  }
767f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  else
768f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    SrcID = SrcI->second;
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
770f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Lookup the Dst.
771f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  VMap::iterator DstI= M.find(Dst);
772f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  unsigned DstID;
773f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
774f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  if (DstI == M.end()) {
775f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    M[Dst] = DstID = Cntr++;
776f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    *Out << "('vertex', " << DstID << ")\n";
777f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  }
77856b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  else {
77956b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek    // We have hit DstID before.  Change its style to reflect a cache hit.
780f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    DstID = DstI->second;
78156b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek    *Out << "('change_vertex_style', " << DstID << ", 1)\n";
78256b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  }
783f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
784f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Add the edge.
7851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  *Out << "('edge', " << SrcID << ", " << DstID
786d1289327f7d2126d732e23cc189ecd0aa5d07cbbTed Kremenek       << ", ('arrow','true'), ('oriented', 'true'))\n";
787f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek}
788f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
7899c378f705405d37f49795d5e915989de774fe11fTed KremenekUbigraphViz::UbigraphViz(raw_ostream *out, llvm::sys::Path& dir,
79056b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek                         llvm::sys::Path& filename)
79156b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  : Out(out), Dir(dir), Filename(filename), Cntr(0) {
79256b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek
79356b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n";
79456b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66'),"
79556b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek          " ('size', '1.5'))\n";
79656b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek}
79756b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek
798710ad9343f32b33b336369b20edad1a21a0b3299Ted KremenekUbigraphViz::~UbigraphViz() {
799710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  Out.reset(0);
8006cb7c1a43b0c8f739d1f54b7fdae5ede86033496Benjamin Kramer  llvm::errs() << "Running 'ubiviz' program... ";
801710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  std::string ErrMsg;
802710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz");
803710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  std::vector<const char*> args;
804710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  args.push_back(Ubiviz.c_str());
805710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  args.push_back(Filename.c_str());
806710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  args.push_back(0);
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
808710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) {
8096cb7c1a43b0c8f739d1f54b7fdae5ede86033496Benjamin Kramer    llvm::errs() << "Error viewing graph: " << ErrMsg << "\n";
810710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  }
8111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
812710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  // Delete the directory.
8131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Dir.eraseFromDisk(true);
814932680ecdeab4c216ce7c0736093d3dcd2f60f6dDaniel Dunbar}
815