-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.cpp
144 lines (138 loc) · 5.59 KB
/
analyzer.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
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
// Need to add support to recognize the unsigned data type
#include "llvm/Pass.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/InstIterator.h"
#include "set"
#include "fstream"
using namespace llvm;
using namespace std;
namespace
{
struct analyzer : public ModulePass
{
static char ID;
LLVMContext* Context;
set<unsigned int> line;
set<unsigned int>::iterator iter;
analyzer() : ModulePass(ID) {}
bool fooexpr(Instruction *vi)
{
if(!isa<PHINode>(*vi)) // && !isa<CallInst>(*vi))
{
// errs() << *vi << "\n";
if(isa<CastInst>(*vi) && (vi->getOperand(0)->getType()) != (vi->getType()))// && !isa<BitCastInst>(*vi) (vi->getOperand(0)->getType()) > (vi->getType())
{
const DebugLoc &location = vi->getDebugLoc();
if (location)
{
// errs() << "\n" << (vi->getOperand(0)->getType());
// errs() << "\n" << (vi->getType());
// errs() << "\n" << *vi;
line.insert(location.getLine());
// errs() << "\nLine: " << location.getLine();// << "\t" << location.getCol();
}
return 1;
}
for(User::op_iterator i = vi->op_begin(); i != vi->op_end(); ++i)
{
if(Instruction *ti = dyn_cast<Instruction>(*i))
{
// errs() << *vi << " \t " << *ti << "\n";
if(analyzer::fooexpr(ti))
{
break;
}
}
}
}
return 0;
}
virtual bool runOnModule(Module &M)
{
bool ret;
Context = &M.getContext();
for(Module::iterator F = M.begin(); F != M.end(); ++F)
{
for(Function::iterator BB = F->begin(); BB != F->end(); ++BB)
{
for(BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI)
{
if(isa<StoreInst>(&(*BI)))
{
StoreInst *instr = dyn_cast<StoreInst>(BI);
if(Instruction *vi = dyn_cast<Instruction>(instr->getOperand(0)))
{
if(!isa<AllocaInst>(*vi))
{
ret=analyzer::fooexpr(vi);
}
}
}
else
{
if(isa<ICmpInst>(&(*BI)))
{
ICmpInst *instr = dyn_cast<ICmpInst>(BI);
if(Instruction *vi = dyn_cast<Instruction>(instr->getOperand(0)))
{
if(!isa<AllocaInst>(*vi))
{
ret=analyzer::fooexpr(vi);
}
}
else
{
if (ConstantInt* CI = dyn_cast<ConstantInt>(instr->getOperand(0)))
{
if(CI->isNegative())
{
const DebugLoc &location = instr->getDebugLoc();
line.insert(location.getLine());
}
}
}
if(Instruction *vi = dyn_cast<Instruction>(instr->getOperand(1)))
{
if(!isa<AllocaInst>(*vi))
{
ret=analyzer::fooexpr(vi);
}
}
else
{
if (ConstantInt* CI = dyn_cast<ConstantInt>(instr->getOperand(1)))
{
if(CI->isNegative())
{
const DebugLoc &location = instr->getDebugLoc();
line.insert(location.getLine());
}
}
}
}
}
}
}
}
ofstream filewrite;
filewrite.open("~/irfiles/LoC.txt");
for(iter=line.begin(); iter!=line.end(); iter++)
filewrite << "Line:" << *iter << " \n";
filewrite.close();
return true;
}
};
}
char analyzer::ID = 0;
static RegisterPass<analyzer> X("analyzer", "test function exist", false, false);