/
main.cpp
273 lines (231 loc) · 6.47 KB
/
main.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
270
271
272
273
#include "cfg.hpp"
#include "cfg_builder.hpp"
#include "cfg_json_writer.hpp"
#include "ast_pretty_printer.hpp"
#include "unit_navigator.hpp"
#include <clang/Lex/Preprocessor.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Basic/FileManager.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Lex/HeaderSearch.h>
#include <clang/AST/ASTContext.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/Decl.h>
#include <clang/AST/Stmt.h>
#include <clang/AST/DeclTemplate.h>
#include <clang/AST/DeclCXX.h>
#include <clang/Analysis/CFG.h>
#include <clang/Frontend/Utils.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Frontend/TextDiagnosticBuffer.h>
#include <clang/Sema/Sema.h>
#include <iostream>
#include <set>
#include <boost/utility.hpp>
#include <boost/assert.hpp>
struct config
{
int printJsonCfg;
bool printReadableAST;
bool printUnitAST;
bool buildCfg;
bool debugCFG;
bool showFnNames;
bool dump_progress;
std::string filter;
std::string static_prefix;
};
struct cfg_build_visitor
: default_build_visitor
{
cfg_build_visitor(clang::CompilerInstance & ci, config const & c)
: m_ci(ci), m_c(c)
{
}
bool function_started(std::string const & name)
{
if (!m_c.filter.empty() && name.substr(0, m_c.filter.size()) != m_c.filter)
return false;
if (m_c.showFnNames)
std::cerr << name << std::endl;
return true;
}
void statement_visited(clang::Stmt const * stmt)
{
if (m_c.dump_progress)
{
stmt->getLocStart().dump(m_ci.getSourceManager());
std::cerr << '\n';
}
}
clang::CompilerInstance & m_ci;
config const & m_c;
};
class MyConsumer
: public clang::ASTConsumer
{
public:
MyConsumer(clang::CompilerInstance & ci, config const & c, std::string const & filename)
: m_ci(ci), m_c(c), m_filename(filename)
{
}
void HandleTranslationUnit(clang::ASTContext &ctx)
{
if (m_ci.getDiagnostics().hasErrorOccurred())
{
std::cerr << "Errors were found, serialization of AST and CFGs is disabled." << std::endl;
return;
}
#if 0
std::vector<clang::CXXRecordDecl *> structure_decls;
for (clang::ASTContext::type_iterator it = ctx.types_begin(); it != ctx.types_end(); ++it)
{
clang::Type * type = *it;
if (type->isDependentType() || !type->isStructureOrClassType())
continue;
structure_decls.push_back(type->getAsCXXRecordDecl());
}
// Instantiate all special member functions
for (std::size_t i = 0; i != structure_decls.size(); ++i)
{
clang::CXXRecordDecl * recdecl = structure_decls[i];
for (clang::CXXRecordDecl::method_iterator mit = recdecl->method_begin(); mit != recdecl->method_end(); ++mit)
{
clang::CXXMethodDecl * method = *mit;
m_ci.getSema().MarkDeclarationReferenced(method->getLocation(), method);
}
}
#endif
name_mangler nm(ctx);
unit_navigator un(nm, ctx.getTranslationUnitDecl());
if (!m_c.filter.empty())
un.filter(m_c.filter);
if (m_c.printReadableAST)
{
for (unit_navigator::fns_const_iterator ci = un.fns_begin(); ci != un.fns_end(); ++ci)
pretty_print_decl(*ci, std::cout);
}
if (m_c.buildCfg)
build_program(un, m_ci.getSourceManager(), cfg_build_visitor(m_ci, m_c), m_c.static_prefix);
if (m_c.printJsonCfg)
{
program prog = build_program(un, m_ci.getSourceManager(), cfg_build_visitor(m_ci, m_c), m_c.static_prefix);
cfg_json_write(std::cout, prog, nm, m_c.printJsonCfg == 1);
}
if (m_c.printUnitAST)
pretty_print_decl(ctx.getTranslationUnitDecl(), std::cout, 0);
if (m_c.debugCFG)
{
program prog = build_program(un, m_ci.getSourceManager(), cfg_build_visitor(m_ci, m_c), m_c.static_prefix);
prog.pretty_print(std::cerr);
}
}
private:
clang::CompilerInstance & m_ci;
config m_c;
std::string m_filename;
};
class MyASTDumpAction : public clang::ASTFrontendAction
{
public:
MyASTDumpAction(config const & c)
: m_c(c)
{
}
clang::ASTContext * ctx;
protected:
virtual clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &CI,
llvm::StringRef InFile)
{
return new MyConsumer(CI, m_c, InFile.str());
}
private:
config m_c;
};
class MyDiagClient : public clang::DiagnosticClient
{
public:
virtual void HandleDiagnostic(clang::Diagnostic::Level DiagLevel, const clang::DiagnosticInfo &Info)
{
}
};
int main(int argc, char * argv[])
{
try
{
static char const * additional_args[] = {
"-triple", "i686-pc-win32",
"-ferror-limit", "19",
"-fmessage-length", "300",
"-fexceptions",
"-fcxx-exceptions",
"-fms-extensions",
"-fgnu-runtime",
"-fdiagnostics-show-option",
"-fcolor-diagnostics",
"-x", "c++",
"-nobuiltininc",
};
std::vector<char const *> args(additional_args, additional_args + sizeof additional_args / sizeof additional_args[0]);
config c = {};
c.static_prefix = "__unique";
// Parse the arguments
bool print_help = false;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "-a")
c.printReadableAST = true;
if (arg == "-h" || arg == "--help")
print_help = true;
else if (arg == "-j")
c.printJsonCfg = 1;
else if (arg == "-J")
c.printJsonCfg = 2;
else if (arg == "-u")
c.printUnitAST = true;
else if (arg == "-c")
c.buildCfg = true;
else if (arg == "--debugcfg")
c.debugCFG = true;
else if (arg == "--showfnnames")
c.showFnNames = true;
else if (arg == "--dumpprogress")
c.dump_progress = true;
else if (arg == "--unitid" && i + 1 < argc)
c.static_prefix = argv[++i];
else if (arg == "--filter" && i + 1 < argc)
c.filter = argv[++i];
else
args.push_back(argv[i]);
}
if (print_help)
{
std::cout << "Usage: " << argv[0] << "[-Jjuca] [--unitid <id>] [<clang options>] <filename>" << std::endl;
return 0;
}
clang::CompilerInstance comp_inst;
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagIds;
clang::Diagnostic argDiag(diagIds);
clang::CompilerInvocation & ci = comp_inst.getInvocation();
clang::CompilerInvocation::CreateFromArgs(ci, &args.front(), &args.back() + 1, argDiag);
comp_inst.createDiagnostics(args.size(), (char **)&args[0]);
if (!c.printJsonCfg && !c.printReadableAST && !c.debugCFG && !c.printUnitAST && !c.buildCfg)
c.printJsonCfg = 2;
MyASTDumpAction act(c);
comp_inst.ExecuteAction(act);
return comp_inst.getDiagnostics().hasErrorOccurred();
}
catch (std::exception const & e)
{
std::cerr << "error: " << e.what() << std::endl;
return 1;
}
catch (...)
{
std::cerr << "error: unexpected error occured" << std::endl;
return 1;
}
}