-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap_maker.cpp
More file actions
314 lines (264 loc) · 12.4 KB
/
map_maker.cpp
File metadata and controls
314 lines (264 loc) · 12.4 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <clang/ASTMatchers/ASTMatchFinder.h>
#include <clang/ASTMatchers/ASTMatchers.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Tooling/Tooling.h>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
#include <llvm/Support/CommandLine.h>
#include <set>
#include <string>
#include <utility>
#include <vector>
// see AST matcher reference for available macros at
// http://clang.llvm.org/docs/LibASTMatchersReference.html
using namespace clang::ast_matchers;
auto DOFieldMatcher = fieldDecl(isPublic(), hasType(recordDecl(hasName("DataObject")))).bind("DO");
auto LinkFieldMatcher = fieldDecl(isPublic(), hasType(recordDecl(hasName("LinkObject")))).bind("link");
auto ModuleClassMatcher = cxxRecordDecl(anyOf(has(DOFieldMatcher), has(LinkFieldMatcher))).bind("moduleClass");
auto ModuleInstanceMatcher = varDecl(hasType(ModuleClassMatcher)).bind("moduleInstance");
// map should be sorted for print_modules output
std::multimap<std::string, std::pair<std::string, std::string>> DOs; // key: module class name, value: DO as pair of instance name and
// datatype
std::multimap<std::string, std::pair<std::string, std::string>> Links; // key: module class name, value: link as pair of instance name and
// datatype
std::map<std::string, std::string> ModuleInstances; // key: variable, value: module class name
std::set<std::string> DOSet;
std::set<std::string> LinkSet;
void replaceAll(std::string &s, const std::string &search, const std::string &replace) {
for (size_t pos = 0;; pos += replace.length()) {
// Locate the substring to replace
pos = s.find(search, pos);
if (pos == std::string::npos)
break;
// Replace by erasing and inserting
s.erase(pos, search.length());
s.insert(pos, replace);
}
};
class MapMaker : public clang::ast_matchers::MatchFinder::MatchCallback {
public:
MapMaker() : printingPolicy(getLangOptions()) {}
void run(const clang::ast_matchers::MatchFinder::MatchResult &Result) override {
if (const clang::RecordDecl *rd = Result.Nodes.getNodeAs<clang::RecordDecl>("moduleClass")) {
std::string moduleClass = rd->getNameAsString();
if (const clang::VarDecl *vd = Result.Nodes.getNodeAs<clang::VarDecl>("moduleInstance")) {
ModuleInstances.emplace(vd->getNameAsString(), moduleClass);
}
} else if (const clang::FieldDecl *fd = Result.Nodes.getNodeAs<clang::FieldDecl>("DO")) {
DOs.emplace(fd->getParent()->getNameAsString(), make_pair(fd->getNameAsString(), fd->getType().getAsString(printingPolicy)));
} else if (const clang::FieldDecl *fd = Result.Nodes.getNodeAs<clang::FieldDecl>("link")) {
Links.emplace(fd->getParent()->getNameAsString(), make_pair(fd->getNameAsString(), fd->getType().getAsString(printingPolicy)));
}
}
private:
static clang::LangOptions getLangOptions() {
clang::LangOptions langOptions;
langOptions.Bool = true;
return langOptions;
}
clang::PrintingPolicy printingPolicy;
};
static llvm::cl::OptionCategory MapMakerCategory("MapMaker options");
static llvm::cl::opt<std::string> PathToASM("asm", llvm::cl::desc("Path to ASM"), llvm::cl::Optional, llvm::cl::cat(MapMakerCategory));
std::string makerPath{"./maker"};
int main(int argc, const char **argv) {
llvm::Expected<clang::tooling::CommonOptionsParser> op = clang::tooling::CommonOptionsParser::create(argc, argv, MapMakerCategory);
clang::tooling::ClangTool Tool(op->getCompilations(), op->getSourcePathList());
if(PathToASM.getValue() != "") {
std::cout << "Path to ASM: " << PathToASM.getValue() << std::endl;
makerPath = PathToASM.getValue();
}
// see compilation database reference for JSON commands
// https://clang.llvm.org/doxygen/JSONCompilationDatabase_8h_source.html
clang::tooling::CompilationDatabase &cdb = op->getCompilations();
for (auto path : op->getSourcePathList())
std::cout << "Compilations source path: " << path << std::endl;
for (auto f : cdb.getAllFiles())
std::cout << "Compilations file: " << f << std::endl;
for (auto cc : cdb.getAllCompileCommands()) {
std::cout << "Compilations command directory: " << cc.Directory << std::endl;
std::cout << "Compilations command file: " << cc.Filename << std::endl;
for (auto cl : cc.CommandLine) {
std::cout << "Compilations command line: " << cl << std::endl;
}
}
MapMaker Maker;
clang::ast_matchers::MatchFinder Finder;
std::experimental::filesystem::create_directories(makerPath);
std::fstream fs;
// create needed files for compiling process
// maker_do.hpp
fs.open(makerPath + std::string{"/maker_do.hpp"}, std::fstream::out);
fs << "#pragma once" << std::endl
<< std::endl
<< "#include <boost/variant.hpp>" << std::endl
<< std::endl
<< "#include \"../asm/dataobject.hpp\"" << std::endl
<< "#include \"../datatypes/global_datatypes.hpp\"" << std::endl
<< std::endl
<< "namespace Asm {" << std::endl
<< "using data_variant = boost::variant<Asm::DataObject<bool>&>;" << std::endl
<< "}" << std::endl;
fs.close();
// maker_lo.hpp
fs.open(makerPath + std::string{"/maker_lo.hpp"}, std::fstream::out);
fs << "#pragma once" << std::endl
<< std::endl
<< "#include <boost/variant.hpp>" << std::endl
<< std::endl
<< "#include \"../asm/dataobject.hpp\"" << std::endl
<< "#include \"../asm/linkobject.hpp\"" << std::endl
<< "#include \"../datatypes/global_datatypes.hpp\"" << std::endl
<< std::endl
<< "namespace Asm {" << std::endl
<< "using link_variant = "
"boost::variant<Asm::LinkObject<Asm::DataObject<bool>, "
"Asm::DataObject<bool>>& >;"
<< std::endl
<< "}" << std::endl;
fs.close();
// maker_reflection.hpp
fs.open(makerPath + std::string{"/maker_reflection.hpp"}, std::fstream::out);
fs << "#pragma once" << std::endl
<< std::endl
<< "#include \"../asm/asm.hpp\"" << std::endl
<< std::endl
<< "using id_string_map = std::unordered_map<void*, std::string>;" << std::endl
<< "using name_dataobject_map = std::unordered_map<std::string, "
"Asm::data_variant>;"
<< std::endl
<< "using name_link_map = std::unordered_map<std::string, "
"Asm::link_variant>;"
<< std::endl
<< "using print_module_map = std::map<std::string, std::string>;" << std::endl
<< std::endl
<< "extern const id_string_map module_name;" << std::endl
<< "extern const id_string_map do_names;" << std::endl
<< "extern const name_dataobject_map name_dataobjects;" << std::endl
<< "extern const name_link_map name_links;" << std::endl
<< "extern const print_module_map print_modules;" << std::endl;
fs.close();
Finder.addMatcher(DOFieldMatcher, &Maker);
Finder.addMatcher(LinkFieldMatcher, &Maker);
Finder.addMatcher(ModuleInstanceMatcher, &Maker);
auto faf = clang::tooling::newFrontendActionFactory(&Finder);
int Result = Tool.run(faf.get());
std::cout << "Return value of Tool.run: " << Result << std::endl;
// maker_reflection.cpp
fs.open(makerPath + std::string{"/maker_reflection.cpp"}, std::fstream::out);
fs << "#include \"maker_reflection.hpp\"" << std::endl
<< "#include \"../modules/global_modules.hpp\"" << std::endl
<< std::endl
<< "// Get out the module name for humans" << std::endl
<< "const id_string_map module_name {" << std::endl;
for (auto &ModuleInstance : ModuleInstances) {
fs << "\t{&" + ModuleInstance.first + ", \"" + ModuleInstance.second + "." + ModuleInstance.first + "\"}";
fs << ",";
fs << std::endl;
}
fs << "};" << std::endl
<< std::endl
<< "// Get out the DO name for humans" << std::endl
<< "const id_string_map do_names" << std::endl
<< "{" << std::endl;
for (auto &ModuleInstance : ModuleInstances) {
auto range = DOs.equal_range(ModuleInstance.second);
for (auto it = range.first; it != range.second; ++it) {
fs << "\t{&" + ModuleInstance.first + "." + it->second.first + ", \"" + ModuleInstance.second + "." + ModuleInstance.first + "." +
it->second.first + "\"},"
<< std::endl;
}
}
fs << "};" << std::endl << std::endl;
// map for sorted order
fs << "// Get out by name all modules, all their dataobjects with type and "
"their links for humans"
<< std::endl
<< "const print_module_map print_modules {" << std::endl;
std::string type;
for (auto &ModuleInstance : ModuleInstances) {
fs << "\t{\"" + ModuleInstance.second + "." + ModuleInstance.first + "\",\"";
auto range = DOs.equal_range(ModuleInstance.second);
for (auto it = range.first; it != range.second; ++it) {
type = it->second.second;
replaceAll(type, "Asm::", "");
fs << " |> " << type << " " << it->second.first + "\\n";
}
auto range2 = Links.equal_range(ModuleInstance.second);
for (auto it = range2.first; it != range2.second; ++it) {
type = it->second.second;
replaceAll(type, "Asm::", "");
fs << " |> " << type << " " << it->second.first + "\\n";
}
fs << "\"}," << std::endl;
}
fs << "};" << std::endl << std::endl;
fs << "const name_dataobject_map name_dataobjects {" << std::endl;
for (auto &ModuleInstance : ModuleInstances) {
auto range = DOs.equal_range(ModuleInstance.second);
for (auto it = range.first; it != range.second; ++it) {
fs << "\t{\"" + ModuleInstance.second + "." + ModuleInstance.first + "." + it->second.first + "\", " + ModuleInstance.first + "." +
it->second.first + "},"
<< std::endl;
DOSet.insert(it->second.second);
}
}
fs << "};" << std::endl << std::endl;
fs << "const name_link_map name_links {" << std::endl;
for (auto &ModuleInstance : ModuleInstances) {
auto range = Links.equal_range(ModuleInstance.second);
for (auto it = range.first; it != range.second; ++it) {
fs << "\t{\"" + ModuleInstance.second + "." + ModuleInstance.first + "." + it->second.first + "\", " + ModuleInstance.first + "." +
it->second.first + "},"
<< std::endl;
LinkSet.insert(it->second.second);
}
}
fs << "};" << std::endl << std::endl;
fs.close();
// maker_do.hpp
fs.open(makerPath + std::string{"/maker_do.hpp"}, std::fstream::out);
fs << "#pragma once" << std::endl
<< std::endl
<< "#include <boost/variant.hpp>" << std::endl
<< std::endl
<< "#include \"../asm/dataobject.hpp\"" << std::endl
<< "#include \"../datatypes/global_datatypes.hpp\"" << std::endl
<< std::endl
<< "namespace Asm {" << std::endl
<< std::endl
<< "using data_variant = boost::variant<" << std::endl;
unsigned int counter = 0;
for (const std::string type : DOSet) {
fs << " " << type << "&";
if (++counter != DOSet.size())
fs << ", ";
fs << std::endl;
}
fs << " >;" << std::endl << "}" << std::endl;
fs.close();
// maker_lo.hpp
fs.open(makerPath + std::string{"/maker_lo.hpp"}, std::fstream::out);
fs << "#pragma once" << std::endl
<< std::endl
<< "#include <boost/variant.hpp>" << std::endl
<< std::endl
<< "#include \"../asm/dataobject.hpp\"" << std::endl
<< "#include \"../asm/linkobject.hpp\"" << std::endl
<< "#include \"../datatypes/global_datatypes.hpp\"" << std::endl
<< std::endl
<< "namespace Asm {" << std::endl
<< std::endl
<< "using link_variant = boost::variant<" << std::endl;
counter = 0;
for (const std::string type : LinkSet) {
fs << " " << type << "&";
if (++counter != LinkSet.size())
fs << ", ";
fs << std::endl;
}
fs << " >;" << std::endl << "}" << std::endl;
fs.close();
return Result;
}