1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
//===--- clang-wpa.cpp - clang whole program analyzer ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This tool reads a sequence of precompiled AST files, and do various
// cross translation unit analyses.
//
//===----------------------------------------------------------------------===//
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/TransferFuncs.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Index/GlobalCallGraph.h"
#include "clang/Index/Indexer.h"
#include "clang/Index/TranslationUnit.h"
#include "clang/Index/DeclReferenceMap.h"
#include "clang/Index/SelectorMap.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace idx;
static llvm::cl::list<std::string>
InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input AST files>"));
static llvm::cl::opt<bool>
ViewCallGraph("view-call-graph", llvm::cl::desc("Display the call graph."));
static llvm::cl::opt<std::string>
AnalyzeFunction("analyze-function",
llvm::cl::desc("Specify the entry function."));
namespace {
// A thin wrapper over ASTUnit implementing the TranslationUnit interface.
class ASTUnitTU : public TranslationUnit {
ASTUnit *AST;
DeclReferenceMap DeclRefMap;
SelectorMap SelMap;
public:
ASTUnitTU(ASTUnit *ast)
: AST(ast), DeclRefMap(AST->getASTContext()), SelMap(AST->getASTContext()) {
}
virtual ASTContext &getASTContext() {
return AST->getASTContext();
}
virtual Preprocessor &getPreprocessor() {
return AST->getPreprocessor();
}
virtual Diagnostic &getDiagnostic() {
return AST->getDiagnostics();
}
virtual DeclReferenceMap &getDeclReferenceMap() {
return DeclRefMap;
}
virtual SelectorMap &getSelectorMap() {
return SelMap;
}
};
}
int main(int argc, char **argv) {
llvm::cl::ParseCommandLineOptions(argc, argv, "clang-wpa");
std::vector<ASTUnit*> ASTUnits;
Program Prog;
Indexer Idxer(Prog);
if (InputFilenames.empty())
return 0;
DiagnosticOptions DiagOpts;
IntrusiveRefCntPtr<Diagnostic> Diags
= CompilerInstance::createDiagnostics(DiagOpts, argc, argv);
for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
const std::string &InFile = InputFilenames[i];
OwningPtr<ASTUnit> AST(ASTUnit::LoadFromASTFile(InFile, Diags,
FileSystemOptions(),
false, 0, 0, true));
if (!AST)
return 1;
ASTUnits.push_back(AST.take());
}
if (ViewCallGraph) {
OwningPtr<clang::idx::CallGraph> CG;
CG.reset(new clang::idx::CallGraph(Prog));
for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i)
CG->addTU(ASTUnits[i]->getASTContext());
CG->ViewCallGraph();
return 0;
}
if (AnalyzeFunction.empty())
return 0;
// Feed all ASTUnits to the Indexer.
for (unsigned i = 0, e = ASTUnits.size(); i != e; ++i) {
ASTUnitTU *TU = new ASTUnitTU(ASTUnits[i]);
Idxer.IndexAST(TU);
}
Entity Ent = Entity::get(AnalyzeFunction, Prog);
FunctionDecl *FD;
TranslationUnit *TU;
llvm::tie(FD, TU) = Idxer.getDefinitionFor(Ent);
if (!FD)
return 0;
// Create an analysis engine.
Preprocessor &PP = TU->getPreprocessor();
AnalyzerOptions Opts;
// Hard code options and checkers for now.
Opts.MaxNodes = 300000;
Opts.MaxLoop = 3;
Opts.InlineCall = true;
Opts.CFGAddImplicitDtors = true;
Opts.EagerlyTrimEGraph = true;
Opts.CheckersControlList.push_back(std::make_pair("core", true));
if (PP.getTargetInfo().getTriple().getOS() != llvm::Triple::Win32)
Opts.CheckersControlList.push_back(std::make_pair("unix", true));
if (PP.getTargetInfo().getTriple().getVendor() == llvm::Triple::Apple)
Opts.CheckersControlList.push_back(std::make_pair("macosx", true));
// Checks to perform for Objective-C/Objective-C++.
if (PP.getLangOptions().ObjC1)
Opts.CheckersControlList.push_back(std::make_pair("cocoa", true));
OwningPtr<ento::CheckerManager> checkerMgr;
checkerMgr.reset(ento::registerCheckers(Opts, PP.getLangOptions(),
PP.getDiagnostics()));
using namespace clang::ento;
AnalysisManager AMgr(TU->getASTContext(), PP.getDiagnostics(),
PP.getLangOptions(), /* PathDiagnostic */ 0,
CreateRegionStoreManager,
CreateRangeConstraintManager, checkerMgr.get(), &Idxer,
Opts.MaxNodes, Opts.MaxLoop,
Opts.VisualizeEGDot, Opts.VisualizeEGUbi,
Opts.PurgeDead, Opts.EagerlyAssume,
Opts.TrimGraph, Opts.InlineCall,
Opts.UnoptimizedCFG, Opts.CFGAddImplicitDtors,
Opts.CFGAddInitializers,
Opts.EagerlyTrimEGraph);
TransferFuncs* TF = MakeCFRefCountTF(AMgr.getASTContext(), /*GC*/false,
AMgr.getLangOptions());
ExprEngine Eng(AMgr, TF);
Eng.ExecuteWorkList(AMgr.getStackFrame(FD, TU), AMgr.getMaxNodes());
return 0;
}
|