-
Notifications
You must be signed in to change notification settings - Fork 0
/
kcov.cpp
269 lines (228 loc) · 9.12 KB
/
kcov.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <cstdio>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <map>
#include <utility>
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Rewrite/Frontend/Rewriters.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Frontend/Utils.h"
using namespace clang;
using namespace std;
SourceManager *m_srcmgr;
int ID = 0;
//int countBranch = 0;
class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor>
{
public:
MyASTVisitor() {
count = 0;
countBranch = 0;
}
bool VisitStmt(Stmt *s) {
// Fill out this function for your homework
SourceManager &srcmgr = *m_srcmgr;
SourceLocation startLoc = s->getLocStart();
unsigned int lineNum = srcmgr.getExpansionLineNumber(startLoc);
unsigned int colNum = srcmgr.getExpansionColumnNumber(startLoc);
/*string filename = srcmgr.getFilename(startLoc);
if ((!filename.compare("")) || filename.com) {
filename = prevFilename;
} else {
prevFilename = filename;
}*/
if (isa<IfStmt>(s)) {
printBranchLineColFilename("If", lineNum, colNum, filename);
IfStmt *ifstmt = cast<IfStmt>(s);
if(!ifstmt->getElse()) cout<<"this \"if\" has no else"<<endl;
else ifstmt->getElse()->dumpColor();
} else if (isa<ForStmt>(s)) {
printBranchLineColFilename("For", lineNum, colNum, filename);
} else if (isa<WhileStmt>(s)) {
printBranchLineColFilename("While", lineNum, colNum, filename);
} else if (isa<CaseStmt>(s)) {
printBranchLineColFilename("Case", lineNum, colNum, filename);
} else if (isa<DoStmt>(s)) {
//s->dump();
printBranchLineColFilename("Do", lineNum, colNum, filename);
} else if (isa<DefaultStmt>(s)) {
printBranchLineColFilename("Default", lineNum, colNum, filename);
} else if (isa<ConditionalOperator>(s)) {
printBranchLineColFilename("?:", lineNum, colNum, filename);
} else if (isa<SwitchStmt>(s)) {
SwitchStmt *switchstmt = cast<SwitchStmt>(s);
SwitchCase *branch = switchstmt->getSwitchCaseList();
bool hasDefault = false;
for(; branch != NULL ; branch = branch->getNextSwitchCase()) {
if (isa<DefaultStmt>(branch)) {
hasDefault = true;
break;
}
}
if(!hasDefault)
printBranchLineColFilename("ImpDef", lineNum, colNum, filename);
}
return true;
}
bool VisitFunctionDecl(FunctionDecl *f) {
// Fill out this function for your homework
SourceManager &srcmgr = *m_srcmgr;
string funcname = f->getName();
if (f->hasBody()) {
cout<<"function: "<<funcname<<endl;
SourceLocation startLoc = f->getLocStart();
filename = srcmgr.getFilename(startLoc);
//cout<<"AAAAA"<<filename<<endl;
}
if(!funcname.compare("main")) {
f->dumpColor();
//open measure.txt, read, save branch info
//
}
return true;
}
void printBranchNum() {
cout<<"Total number of branches: "<<countBranch<<endl;
}
private:
int count; //to print out ID
int countBranch; //to print out branchNUM
string filename;
void printBranchLineColFilename(string branchName, unsigned int lineNum,
unsigned int colNum, string filename) {
cout<<" "<<setw(10)<<left<<branchName;
cout<<"ID: ", cout.width(7);
cout<<count++;
cout<<" Line: ", cout.width(7);
cout<<lineNum;
cout<<" Col: ", cout.width(7);
cout<<colNum;
cout<<" Filename: "<<filename<<endl;
if ((!branchName.compare("ImpDef")) || (!branchName.compare("Default"))
|| (!branchName.compare("Case"))) countBranch ++;
else
countBranch += 2;
}
};
class MyASTConsumer : public ASTConsumer
{
public:
MyASTConsumer()
: Visitor() //initialize MyASTVisitor
{}
virtual bool HandleTopLevelDecl(DeclGroupRef DR) {
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b) {
// Travel each function declaration using MyASTVisitor
Visitor.TraverseDecl(*b);
}
return true;
}
void printBranchNum() {
Visitor.printBranchNum();
}
private:
MyASTVisitor Visitor;
};
//int MyASTVisitor::count = 0; // ID
//int countBranch = 0;
int main(int argc, char *argv[])
{
if (argc != 2) {
llvm::errs() << "Usage: kcov-branch-identify <filename>\n";
return 1;
}
// CompilerInstance will hold the instance of the Clang compiler for us,
// managing the various objects needed to run the compiler.
CompilerInstance TheCompInst;
// Diagnostics manage problems and issues in compile
TheCompInst.createDiagnostics(NULL, false);
// Set target platform options
// Initialize target info with the default triple for our platform.
TargetOptions *TO = new TargetOptions();
TO->Triple = llvm::sys::getDefaultTargetTriple();
TargetInfo *TI = TargetInfo::CreateTargetInfo(TheCompInst.getDiagnostics(), TO);
TheCompInst.setTarget(TI);
// FileManager supports for file system lookup, file system caching, and directory search management.
TheCompInst.createFileManager();
FileManager &FileMgr = TheCompInst.getFileManager();
// SourceManager handles loading and caching of source files into memory.
TheCompInst.createSourceManager(FileMgr);
SourceManager &SourceMgr = TheCompInst.getSourceManager();
//global var m_srcmgr
m_srcmgr = &SourceMgr;
// Prreprocessor runs within a single source file
TheCompInst.createPreprocessor();
// ASTContext holds long-lived AST nodes (such as types and decls) .
TheCompInst.createASTContext();
// Enable HeaderSearch option
llvm::IntrusiveRefCntPtr<clang::HeaderSearchOptions> hso( new HeaderSearchOptions());
HeaderSearch headerSearch(hso,
TheCompInst.getFileManager(),
TheCompInst.getDiagnostics(),
TheCompInst.getLangOpts(),
TI);
// <Warning!!> -- Platform Specific Code lives here
// This depends on A) that you're running linux and
// B) that you have the same GCC LIBs installed that I do.
/*
$ gcc -xc -E -v -
..
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/include
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/include-fixed
/usr/include
End of search list.
*/
const char *include_paths[] = {"/usr/local/include",
"/usr/lib/gcc/x86_64-linux-gnu/4.4.5/include",
"/usr/lib/gcc/x86_64-linux-gnu/4.4.5/include-fixed",
"/usr/include"};
for (int i=0; i<4; i++)
hso->AddPath(include_paths[i],
clang::frontend::Angled,
false,
false);
// </Warning!!> -- End of Platform Specific Code
InitializePreprocessor(TheCompInst.getPreprocessor(),
TheCompInst.getPreprocessorOpts(),
*hso,
TheCompInst.getFrontendOpts());
//Preprocessor &preprocessor TheCompInst.getPreprocessor(); my edit
// A Rewriter helps us manage the code rewriting task.
Rewriter TheRewriter;
TheRewriter.setSourceMgr(SourceMgr, TheCompInst.getLangOpts());
// Set the main file handled by the source manager to the input file.
const FileEntry *FileIn = FileMgr.getFile(argv[1]);
string filename(argv[1]);
string outputName = filename.substr(0, filename.length()-2) + "-cov.c";
SourceMgr.createMainFileID(FileIn);
// Inform Diagnostics that processing of a source file is beginning.
TheCompInst.getDiagnosticClient().BeginSourceFile(TheCompInst.getLangOpts(),&TheCompInst.getPreprocessor());
// Create an AST consumer instance which is going to get called by ParseAST.
MyASTConsumer TheConsumer;
// Parse the file to AST, registering our consumer as the AST consumer.
ParseAST(TheCompInst.getPreprocessor(), &TheConsumer, TheCompInst.getASTContext());
const RewriteBuffer *RewriteBuf = TheRewriter.getRewriteBufferFor(SourceMgr.getMainFileID());
ofstream output(outputName.c_str());
output << string(RewriteBuf->begin(), RewriteBuf->end());
output.close();
//cout<<"Total number of branches: "<<countBranch<<endl;
//TheConsumer.printBranchNum();
return 0;
}