forked from v-yatsenko/binder-aio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinder.cpp
More file actions
273 lines (202 loc) · 9.09 KB
/
binder.cpp
File metadata and controls
273 lines (202 loc) · 9.09 KB
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
// -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
// vi: set ts=2 noet:
//
// Copyright (c) 2016 Sergey Lyskov <[email protected]>
//
// All rights reserved. Use of this source code is governed by a
// MIT license that can be found in the LICENSE file.
/// @file binder/binder.cpp
/// @brief Main
/// @author Sergey Lyskov
#include <binder.hpp>
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Basic/SourceLocation.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/AST/Comment.h>
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include <context.hpp>
#include <enum.hpp>
#include <function.hpp>
#include <class.hpp>
#include <util.hpp>
#include <reporter.hpp>
using namespace clang::tooling;
using namespace llvm;
using binder::Reporter;
// Apply a custom category to all command-line options so that they are the
// only ones displayed.
static llvm::cl::OptionCategory BinderToolCategory("Binder options");
// CommonOptionsParser declares HelpMessage with a description of the common
// command-line options related to the compilation database and input files.
// It's nice to have this help message in all tools.
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
// A help message for this specific tool can be added afterwards.
static cl::extrahelp MoreHelp("\nMore help text...\n");
using binder::Config;
using namespace clang;
using std::string;
cl::opt<std::string> O_root_module("root-module", cl::desc("Name of root module"), /*cl::init("example"),*/ cl::cat(BinderToolCategory));
cl::opt<int> O_max_file_size("max-file-size", cl::desc("Specify maximum length of generated source files"), cl::init(1024*16), cl::cat(BinderToolCategory));
cl::opt<std::string> O_prefix("prefix", cl::desc("Output prefix for all generated files. Might contain directories."), cl::init(""), cl::cat(BinderToolCategory));
cl::list<std::string> O_bind("bind", cl::desc("Namespace to bind, could be specified more then once. Specify \"\" to bind all namespaces."), cl::cat(BinderToolCategory)); // , cl::OneOrMore
cl::list<std::string> O_skip("skip", cl::desc("Namespace to skip, could be specified more then once"), cl::cat(BinderToolCategory)); // , cl::OneOrMore
cl::opt<std::string> O_config("config", cl::desc("Specify config file from which bindings setting will be read"), cl::init(""), cl::cat(BinderToolCategory));
cl::opt<bool> O_annotate_includes("annotate-includes", cl::desc("Annotate each includes in generated code with type name that trigger it inclusion"), cl::init(false), cl::cat(BinderToolCategory));
cl::opt<bool> O_single_file("single-file", cl::desc("Concatenate all binder output into single file with name: root-module-name + '.cpp'. Use this for a small projects and for testing."), cl::init(false), cl::cat(BinderToolCategory));
cl::opt<bool> O_trace("trace", cl::desc("Add tracer output for each binded object (i.e. for debugging)"), cl::init(false), cl::cat(BinderToolCategory));
cl::opt<std::string> O_coverage("coverage", cl::desc("Output binding coverage statistic to specified file"), cl::init(""), cl::cat(BinderToolCategory));
cl::opt<bool> O_verbose("v", cl::desc("Increase verbosity of output"), cl::init(false), cl::cat(BinderToolCategory));
class ClassVisitor : public RecursiveASTVisitor<ClassVisitor>
{
public:
explicit ClassVisitor(DeclContext *dc) /*: decl_context(dc)*/ {}
virtual ~ClassVisitor() {}
virtual bool VisitEnumDecl(EnumDecl *record) {
errs() << "ClassVisitor EnumDecl: " << record->getQualifiedNameAsString() << "\n";
record->dump();
return true;
}
private:
//DeclContext *decl_context;
};
string wrap_CXXRecordDecl(CXXRecordDecl *R)
{
ClassVisitor v{R};
v.TraverseDecl( R );
//R->dump();
return "";
}
class BinderVisitor : public RecursiveASTVisitor<BinderVisitor>
{
public:
explicit BinderVisitor(CompilerInstance *ci) : ast_context( &( ci->getASTContext() ) )
{
Config & config = Config::get();
config.root_module = O_root_module;
config.prefix = O_prefix;
config.maximum_file_length = O_max_file_size;
config.namespaces_to_bind = O_bind;
config.namespaces_to_skip = O_skip;
if( O_config.size() ) config.read(O_config);
}
virtual ~BinderVisitor() {}
bool shouldVisitTemplateInstantiations () const { return true; }
virtual bool VisitFunctionDecl(FunctionDecl *F)
{
auto loc = FullSourceLoc(F->getLocation(), ast_context->getSourceManager() );
if( !loc.isInSystemHeader() and isa<CXXMethodDecl>(F) and
( F->isCXXInstanceMember() ? F->getAccess() == AS_public : true ) ) {
Reporter::get().add_method_decl(F, ast_context);
}
if( F->isCXXInstanceMember() or isa<CXXMethodDecl>(F) ) return true;
if( binder::is_bindable(F) ) {
binder::BinderOP b = std::make_shared<binder::FunctionBinder>(F);
context.add(b);
} else if( F->isOverloadedOperator() and F->getNameAsString() == "operator<<" ) {
//outs() << "Adding insertion operator: " << binder::function_pointer_type(F) << "\n";
context.add_insertion_operator(F);
}
return true;
}
virtual bool VisitCXXRecordDecl(CXXRecordDecl *C) {
if( C->isCXXInstanceMember() )
return true;
if( C->isCXXClassMember() ) {
if (TagDecl::TagKind::TTK_Class == C->getTagKind()) {
for(auto t = C->ctor_begin(); t != C->ctor_end(); ++t) {
if (t->getAccess() == AS_public and !t->isMoveConstructor()) {
Reporter::get().add_method_decl(*t, ast_context);
}
}
}
return true;
}
if( binder::is_bindable(C) ) {
binder::BinderOP b = std::make_shared<binder::ClassBinder>(C);
context.add(b);
}
return true;
}
// virtual bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *C) {
// if( FullSourceLoc(C->getLocation(), ast_context->getSourceManager() ).isInSystemHeader() ) return true;
// errs() << "Visit ClassTemplateSpecializationDecl:" << C->getQualifiedNameAsString() << binder::template_specialization(C) << "\n";
// C->dump();
// return true;
// }
// virtual bool VisitTemplateDecl(TemplateDecl *record) {
// //if( FullSourceLoc(record->getLocation(), ast_context->getSourceManager() ).isInSystemHeader() ) return true;
// errs() << "Visit TemplateDecl: " << record->getQualifiedNameAsString() << "\n";
// //record->dump();
// return true;
// }
// virtual bool VisitClassTemplateDecl(ClassTemplateDecl *record) {
// //if( FullSourceLoc(record->getLocation(), ast_context->getSourceManager() ).isInSystemHeader() ) return true;
// errs() << "Visit ClassTemplateDecl: " << record->getQualifiedNameAsString() << binder::template_specialization( record->getTemplatedDecl() ) << "\n";
// //record->dump();
// return true;
// }
// virtual bool VisitTypedefDecl(TypedefDecl *T) {
// if( FullSourceLoc(T->getLocation(), ast_context->getSourceManager() ).isInSystemHeader() ) return true;
// //errs() << "Visit TypedefDecl: " << T->getQualifiedNameAsString() << " Type: " << T->getUnderlyingType()->getCanonicalTypeInternal()/*getCanonicalType()*/.getAsString() << "\n";
// // record->dump();
// return true;
// }
// virtual bool VisitNamedDecl(NamedDecl *record) {
// errs() << "Visit NamedRecord: " << record->getQualifiedNameAsString() << "\n";
// return true;
// }
// virtual bool VisitFieldDecl(FieldDecl *record) {
// errs() << "Visit FieldDecl: " << record->getQualifiedNameAsString() << "\n";
// record->dump();
// return true;
// }
virtual bool VisitEnumDecl(EnumDecl *E) {
if( E->isCXXInstanceMember() or E->isCXXClassMember() ) return true;
binder::BinderOP b = std::make_shared<binder::EnumBinder>( E/*->getCanonicalDecl()*/ );
context.add(b);
return true;
}
void generate(void) {
context.generate( Config::get() );
}
private:
ASTContext *ast_context;
binder::Context context;
};
class BinderASTConsumer : public ASTConsumer
{
private:
std::unique_ptr<BinderVisitor> visitor;
public:
// override the constructor in order to pass CI
explicit BinderASTConsumer(CompilerInstance *ci) : visitor(new BinderVisitor(ci)) {}
// override this to call our ExampleVisitor on the entire source file
virtual void HandleTranslationUnit(ASTContext &context)
{
visitor->TraverseDecl( context.getTranslationUnitDecl() );
visitor->generate();
}
};
class BinderFrontendAction : public ASTFrontendAction {
public:
virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(CompilerInstance &ci, StringRef file) {
return std::unique_ptr<ASTConsumer>( new BinderASTConsumer(&ci) );
}
};
int main(int argc, const char **argv)
{
CommonOptionsParser op(argc, argv, BinderToolCategory);
ClangTool tool(op.getCompilations(), op.getSourcePathList());
//outs() << "Root module: " << O_root_module << "\n";
//for(auto &s : O_bind) outs() << "Binding: '" << s << "'\n";
int result = tool.run(newFrontendActionFactory<BinderFrontendAction>().get());
if( result == 0 and !O_coverage.empty() ) {
Reporter::get().write_report(O_coverage);
}
return result;
}