-
Notifications
You must be signed in to change notification settings - Fork 15
/
struct_member.cpp
65 lines (55 loc) · 2.4 KB
/
struct_member.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
/**
* @file struct_member.cpp
* @brief find struct member usages
*
*/
#include "near_core.h"
#include <fstream>
#include <set>
#include <utility>
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Pass.h"
#include "llvm/Support/Regex.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
namespace {
struct StructMember : public llvm::FunctionPass {
static char ID;
private:
std::ifstream is;
std::set<std::string> vars;
public:
StructMember() : FunctionPass(ID) { Rustle::readStructLog(is, vars); }
~StructMember() { is.close(); }
bool runOnFunction(llvm::Function &F) override {
using namespace llvm;
if (!Rustle::debug_check_all_func && Rustle::regexForLibFunc.match(F.getName()))
return false;
if (Rustle::debug_print_function)
Rustle::Logger().Debug("Checking function ", F.getName());
bool found = false;
for (BasicBlock &BB : F)
for (Instruction &I : BB) {
if (!I.getDebugLoc().get() || Rustle::regexForLibLoc.match(I.getDebugLoc().get()->getFilename()))
continue;
auto result = Rustle::usingStruct(&I, vars);
std::string const name = result.first;
Rustle::Mode const mode = result.second;
if (name != "") { // Found
Rustle::Logger().Info("Found struct <", name, "> used(",
mode == Rustle::Mode::RW ? "rw" : (mode == Rustle::Mode::Read ? "read" : (mode == Rustle::Mode::Write ? "write" : "unknown")), ") at ", I.getDebugLoc());
found = true;
}
}
if (!found && Rustle::debug_print_notfound)
Rustle::Logger().Info("No struct member used found");
return false;
}
};
} // namespace
char StructMember::ID = 0;
static llvm::RegisterPass<StructMember> X("struct-member", "", false /* Only looks at CFG */, false /* Analysis Pass */);
static llvm::RegisterStandardPasses Y(llvm::PassManagerBuilder::EP_EarlyAsPossible, [](const llvm::PassManagerBuilder &builder, llvm::legacy::PassManagerBase &PM) { PM.add(new StructMember()); });