-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicserializer.cpp
79 lines (64 loc) · 2.19 KB
/
basicserializer.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
#include <map>
#include <string>
#include <utility>
using namespace clang;
void basicserializer() {}
class PluginASTVisitor : public RecursiveASTVisitor<PluginASTVisitor> {
public:
typedef RecursiveASTVisitor<PluginASTVisitor> Base;
PluginASTVisitor(Rewriter &R, const std::string& f) : TheRewriter(R), filename(f) {}
bool VisitCXXRecordDecl(CXXRecordDecl* r)
{
std::cout << "record : " << r->getNameAsString() << ":" << r->field_empty() << std::endl;
std::map<std::string, std::string> fieldMap;
auto begin = r->field_begin();
auto end = r->field_end();
for(; begin != end; ++begin)
{
if(isa<FieldDecl>(*begin)) {
FieldDecl* field = dyn_cast<FieldDecl>(*begin);
std::cout << "field : " << field->getType().getAsString() << " " << field->getNameAsString() << std::endl;
fieldMap[field->getNameAsString()] = field->getType().getAsString();
}
}
auto locBegin = r->getLocStart();
auto locEnd = r->getLocEnd();
std::string output = "void serialize()\n";
output.append("{\n");
for(auto field : fieldMap) {
output.append("std::cout << \"type: ");
output.append(field.second);
output.append(" \"");
output.append(" << ");
output.append(" \"name: ");
output.append(field.first);
output.append(" \"");
output.append(" << ");
output.append(field.first);
output.append(" << ");
output.append("std::endl;");
output.append("\n");
}
output.append("}\n");
TheRewriter.InsertTextBefore(locEnd, output);
return true;
}
private:
Rewriter &TheRewriter;
std::string filename;
};
class PluginASTConsumer : public ASTConsumer {
public:
PluginASTConsumer(Rewriter &R, const std::string& filename) : Visitor(R, filename) {}
virtual bool HandleTopLevelDecl(DeclGroupRef DR) {
for (DeclGroupRef::iterator b = DR.begin(), e = DR.end(); b != e; ++b)
Visitor.TraverseDecl(*b);
return true;
}
private:
PluginASTVisitor Visitor;
};
ASTConsumer* createASTConsumer(Rewriter& TheRewriter, const std::string& filename)
{
return new PluginASTConsumer(TheRewriter, filename);
}