-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathStaticMain.cpp
94 lines (79 loc) · 3.45 KB
/
StaticMain.cpp
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
//========================================================================
// FILE:
// StaticMain.cpp
//
// DESCRIPTION:
// A command-line tool that counts all static calls (i.e. calls as seen
// in the source code) in the input LLVM file. Internally it uses the
// StaticCallCounter pass.
//
// USAGE:
// # First, generate an LLVM file:
// clang -emit-llvm <input-file> -c -o <output-llvm-file>
// # Now you can run this tool as follows:
// <BUILD/DIR>/bin/static <output-llvm-file>
//
// License: MIT
//========================================================================
#include "StaticCallCounter.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
//===----------------------------------------------------------------------===//
// Command line options
//===----------------------------------------------------------------------===//
static cl::OptionCategory CallCounterCategory{"call counter options"};
static cl::opt<std::string> InputModule{cl::Positional,
cl::desc{"<Module to analyze>"},
cl::value_desc{"bitcode filename"},
cl::init(""),
cl::Required,
cl::cat{CallCounterCategory}};
//===----------------------------------------------------------------------===//
// static - implementation
//===----------------------------------------------------------------------===//
static void countStaticCalls(Module &M) {
// Create a module pass manager and add StaticCallCounterPrinter to it.
ModulePassManager MPM;
MPM.addPass(StaticCallCounterPrinter(llvm::errs()));
// Create an analysis manager and register StaticCallCounter with it.
ModuleAnalysisManager MAM;
MAM.registerPass([&] { return StaticCallCounter(); });
// Register all available module analysis passes defined in PassRegistry.def.
// We only really need PassInstrumentationAnalysis (which is pulled by
// default by PassBuilder), but to keep this concise, let PassBuilder do all
// the _heavy-lifting_.
PassBuilder PB;
PB.registerModuleAnalyses(MAM);
// Finally, run the passes registered with MPM
MPM.run(M, MAM);
}
//===----------------------------------------------------------------------===//
// Main driver code.
//===----------------------------------------------------------------------===//
int main(int Argc, char **Argv) {
// Hide all options apart from the ones specific to this tool
cl::HideUnrelatedOptions(CallCounterCategory);
cl::ParseCommandLineOptions(Argc, Argv,
"Counts the number of static function "
"calls in the input IR file\n");
// Makes sure llvm_shutdown() is called (which cleans up LLVM objects)
// http://llvm.org/docs/ProgrammersManual.html#ending-execution-with-llvm-shutdown
llvm_shutdown_obj SDO;
// Parse the IR file passed on the command line.
SMDiagnostic Err;
LLVMContext Ctx;
std::unique_ptr<Module> M = parseIRFile(InputModule.getValue(), Err, Ctx);
if (!M) {
errs() << "Error reading bitcode file: " << InputModule << "\n";
Err.print(Argv[0], errs());
return -1;
}
// Run the analysis and print the results
countStaticCalls(*M);
return 0;
}