blob: 45e889e170b800a04d7ac9b19b30941b14c0115e [file] [log] [blame]
Zhongxing Xu579855f2010-08-25 01:15:20 +00001//===--- clang-wpa.cpp - clang whole program analyzer ---------------------===//
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// This tool reads a sequence of precompiled AST files, and do various
11// cross translation unit analyses.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000017#include "clang/GR/PathSensitive/AnalysisManager.h"
18#include "clang/GR/PathSensitive/GRExprEngine.h"
19#include "clang/GR/PathSensitive/GRTransferFuncs.h"
20#include "clang/GR/Checkers/LocalCheckers.h"
Zhongxing Xu579855f2010-08-25 01:15:20 +000021#include "clang/Frontend/ASTUnit.h"
22#include "clang/Frontend/CompilerInstance.h"
23#include "clang/Index/CallGraph.h"
24#include "clang/Index/Indexer.h"
25#include "clang/Index/TranslationUnit.h"
26#include "clang/Index/DeclReferenceMap.h"
27#include "clang/Index/SelectorMap.h"
28#include "clang/Lex/Preprocessor.h"
29#include "llvm/ADT/IntrusiveRefCntPtr.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/raw_ostream.h"
32using namespace clang;
33using namespace idx;
34
35static llvm::cl::list<std::string>
36InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>"));
37
38static llvm::cl::opt<bool>
39ViewCallGraph("view-call-graph", llvm::cl::desc("Display the call graph."));
40
41static llvm::cl::opt<std::string>
42AnalyzeFunction("analyze-function",
43 llvm::cl::desc("Specify the entry function."));
44
45namespace {
46// A thin wrapper over ASTUnit implementing the TranslationUnit interface.
47class ASTUnitTU : public TranslationUnit {
48 ASTUnit *AST;
49 DeclReferenceMap DeclRefMap;
50 SelectorMap SelMap;
51
52public:
53 ASTUnitTU(ASTUnit *ast)
54 : AST(ast), DeclRefMap(AST->getASTContext()), SelMap(AST->getASTContext()) {
55 }
56
57 virtual ASTContext &getASTContext() {
58 return AST->getASTContext();
59 }
60
61 virtual Preprocessor &getPreprocessor() {
62 return AST->getPreprocessor();
63 }
64
65 virtual Diagnostic &getDiagnostic() {
66 return AST->getDiagnostics();
67 }
68
69 virtual DeclReferenceMap &getDeclReferenceMap() {
70 return DeclRefMap;
71 }
72
73 virtual SelectorMap &getSelectorMap() {
74 return SelMap;
75 }
76};
77}
78
79int main(int argc, char **argv) {
80 llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa");
81 std::vector<ASTUnit*> ASTUnits;
82
83 Program Prog;
84 Indexer Idxer(Prog);
85
86 if (InputFilenames.empty())
87 return 0;
88
89 DiagnosticOptions DiagOpts;
90 llvm::IntrusiveRefCntPtr<Diagnostic> Diags
91 = CompilerInstance::createDiagnostics(DiagOpts, argc, argv);
92 for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
93 const std::string &InFile = InputFilenames[i];
94 llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromASTFile(InFile, Diags));
95 if (!AST)
96 return 1;
97
98 ASTUnits.push_back(AST.take());
99 }
100
101 if (ViewCallGraph) {
102 llvm::OwningPtr<CallGraph> CG;
103 CG.reset(new CallGraph(Prog));
104
105 for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i)
106 CG->addTU(ASTUnits[i]->getASTContext());
107
108 CG->ViewCallGraph();
109 return 0;
110 }
111
112 if (AnalyzeFunction.empty())
113 return 0;
114
115 // Feed all ASTUnits to the Indexer.
116 for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) {
117 ASTUnitTU *TU = new ASTUnitTU(ASTUnits[i]);
118 Idxer.IndexAST(TU);
119 }
120
121 Entity Ent = Entity::get(AnalyzeFunction, Prog);
122 FunctionDecl *FD;
123 TranslationUnit *TU;
124 llvm::tie(FD, TU) = Idxer.getDefinitionFor(Ent);
125
126 if (!FD)
127 return 0;
128
129 // Create an analysis engine.
130 Preprocessor &PP = TU->getPreprocessor();
131
132 // Hard code options for now.
133 AnalysisManager AMgr(TU->getASTContext(), PP.getDiagnostics(),
134 PP.getLangOptions(), /* PathDiagnostic */ 0,
135 CreateRegionStoreManager,
136 CreateRangeConstraintManager, &Idxer,
Tom Care68625cf2010-09-14 21:35:27 +0000137 /* MaxNodes */ 300000, /* MaxVisit */ 3,
Zhongxing Xu579855f2010-08-25 01:15:20 +0000138 /* VisualizeEG */ false, /* VisualizeEGUbi */ false,
139 /* PurgeDead */ true, /* EagerlyAssume */ false,
140 /* TrimGraph */ false, /* InlineCall */ true,
141 /* UseUnoptimizedCFG */ false);
142
143 GRTransferFuncs* TF = MakeCFRefCountTF(AMgr.getASTContext(), /*GC*/false,
144 AMgr.getLangOptions());
145 GRExprEngine Eng(AMgr, TF);
146
147 Eng.ExecuteWorkList(AMgr.getStackFrame(FD, TU), AMgr.getMaxNodes());
148
149 return 0;
150}