forked from PlatONnetwork/pWASM-abigen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
348 lines (273 loc) · 10.4 KB
/
main.cpp
File metadata and controls
348 lines (273 loc) · 10.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <iostream>
#include<fstream>
#include <string>
#include <vector>
#include <regex>
#define RAPIDJSON_HAS_STDSTRING 1
#include "clang/Driver/Options.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/AST/AST.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Sema/Sema.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/MacroArgs.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Core/QualTypeNames.h"
#include "llvm/Support/raw_ostream.h"
#include "boost/filesystem.hpp"
#include "boost/random.hpp"
#include "StringUtil.h"
#include "AbiMacro.h"
#include "AbiGenerator.h"
#include "AbiDef.h"
#include "Template.h"
#include "AbiJson.h"
#include "Exception.h"
#include "Log.h"
using namespace std;
namespace cl = llvm::cl;
namespace fs = boost::filesystem;
namespace br = boost::random;
using namespace clang;
using namespace platon;
static cl::OptionCategory abiGeneratorOptions("ABI generator options");
static cl::opt<std::string> logPath(
"log-path",
cl::desc("log path, default = /tmp/[randomdir]/platonabi.log"),
cl::cat(abiGeneratorOptions)
);
static cl::opt<std::string> logLevel(
"log-level",
cl::desc("log level, default = debug"),
cl::cat(abiGeneratorOptions),
cl::init("debug")
);
static cl::opt<bool> verbose(
"verbose",
cl::desc("log on-off, default = true"),
cl::cat(abiGeneratorOptions),
cl::init(true)
);
static cl::opt<std::string> abiJsonName(
"abi-name",
cl::desc("abi json filename, defalut = destination filename"),
cl::cat(abiGeneratorOptions)
);
static cl::opt<std::string> outpath(
"outpath",
cl::desc("outpath, default = ./"),
cl::cat(abiGeneratorOptions),
cl::init("./")
);
std::unique_ptr<tooling::FrontendActionFactory> CreateFindMacroFactory(ContractDef &contractDef, vector<string>& actions) {
struct FrontendMacroActionFactory: public tooling::FrontendActionFactory {
ContractDef& contractDef;
vector<string>& actions;
FrontendMacroActionFactory(ContractDef &contractDef, vector<string>& actions)
:contractDef(contractDef), actions(actions) {}
clang::FrontendAction *create() override {
return new FindMacroAction(contractDef, actions);
}
};
LOGDEBUG << "start create_find_macro_factory";
return std::unique_ptr<tooling::FrontendActionFactory>(new FrontendMacroActionFactory(contractDef, actions));
}
std::unique_ptr<tooling::FrontendActionFactory> createFactory(const string& contract, const vector<string>& actions, ABIDef &abiDef) {
struct ABIFrontendActionFactory: public tooling::FrontendActionFactory {
const string& contract;
const vector<string>& actions;
ABIDef& abiDef;
ABIFrontendActionFactory(const string& contract, const vector<string>& actions, ABIDef &abiDef)
: contract(contract), actions(actions), abiDef(abiDef) {}
clang::FrontendAction *create() override {
return new ABIAction(contract, actions, abiDef);
}
};
return std::unique_ptr<tooling::FrontendActionFactory>(
new ABIFrontendActionFactory(contract, actions, abiDef)
);
}
void createJsonAbi(const ABIDef &abiDef, const ContractDef &contractDef, const string &srcFile, const string &destPath, fs::path &randomDir) {
fs::path current = fs::current_path();
fs::path outPath;
if (destPath == "." || destPath == "" || destPath == "./") {
outPath = current;
}
if (srcFile.empty()) {
throw Exception() << ErrStr("src name is empty");
}
string fileName = srcFile + ".abi.json";
fs::path tmpFile = randomDir / fileName;
LOGERROR << "random:[" << randomDir.string() << "] fileName:" << fileName;
LOGDEBUG << "tmpFile:[" << tmpFile.string() << "]";
std::ofstream fs(tmpFile.string().c_str());
if (!fs.is_open()) {
throw Exception() << ErrStr("fs is not open:") << ErrStr(strerror(errno));
}
outputJsonABI(abiDef, contractDef, fs);
fs.close();
fs::path oldFile(outPath);
if (fs::exists(oldFile / fileName)) {
oldFile = oldFile / fileName;
LOGDEBUG << "swap old::" << oldFile.string() << " tmp::" << tmpFile.string();
fs::remove(oldFile);
fs::copy_file(tmpFile, oldFile);
} else {
outPath = outPath /fileName;
LOGDEBUG << "copy ::" << tmpFile.string() << " to ::" << outPath.string();
fs::copy_file(tmpFile, outPath);
}
}
void createContractFile(fs::path &randomDir, const string &srcPath, const string &filename, const string &externC) {
fs::path tmpFile = randomDir / filename;
std::ofstream tmpStream(tmpFile.string().c_str());
if (!tmpStream.is_open()) {
throw Exception() << ErrStr("tmp src file is not open:") << ErrStr(strerror(errno));
}
std::ifstream srcStream(srcPath);
if (!srcStream.is_open()) {
throw Exception() << ErrStr("src is not open:") << ErrStr(strerror(errno));
}
string line;
bool skip = false;
while (getline(srcStream, line)) {
if (line.find("platon autogen end") != string::npos){
skip = false;
continue;
}
if (line.find("platon autogen begin") != string::npos){
skip = true;
continue;
}
if (!skip) {
tmpStream << line << "\n";
}
line.clear();
}
tmpStream << externC;
tmpStream.close();
srcStream.close();
LOGDEBUG << "swap src::" << srcPath << " tmp::" << tmpFile.string();
fs::remove(srcPath);
fs::copy_file(tmpFile, srcPath);
}
void prepareFile(const string &filename) {
fstream fs(filename);
string line;
auto cur = fs.tellg();
// auto begin = fstream::pos_type(-1);
// auto end = fstream::pos_type(-1);
vector<tuple<fstream::pos_type, string>> replaceText;
string tailText;
bool skip = false;
bool append = false;
while (getline(fs, line)) {
if (append) {
tailText.append(line);
}
if (line.find("platon autogen end") != string::npos){
//end = cur + fstream::pos_type(line.length());
replaceText.push_back(tuple<fstream::pos_type, string>(cur, line));
skip = false;
append = true;
}
if (line.find("platon autogen begin") != string::npos){
//begin = cur;
skip = true;
}
if (skip) {
replaceText.push_back(tuple<fstream::pos_type, string>(cur, line));
}
line.clear();
cur = fs.tellg();
}
if (!replaceText.empty()) {
cout << "have abi need replace" << endl;
} else {
return;
};
fs.close();
fstream writeAnno(filename);
if (!writeAnno.is_open()) {
throw Exception() << ErrStr("open file failed");
}
string rt = "//";
writeAnno.seekp(get<0>(replaceText[0]));
for (int i = 0; i < replaceText.size(); i++) {
writeAnno << rt << get<1>(replaceText[i]) << '\n';
}
writeAnno << tailText;
// writeAnno.seekp(end);
// writeAnno << "*/";
// writeAnno.seekp(begin);
// writeAnno << "/*//platon autogen begin";
writeAnno.close();
}
int main(int argc, const char **argv) {
try {
tooling::CommonOptionsParser op(argc, argv, abiGeneratorOptions);
if (op.getSourcePathList().size() > 1) {
throw Exception() << ErrStr("only one contract file");
}
prepareFile(op.getSourcePathList()[0]);
tooling::ClangTool Tool(op.getCompilations(), op.getSourcePathList());
fs::path randomDir("/tmp/");
randomDir = randomDir / randomString(20).c_str(); // "57298a3ci7d0g504ha00";// randomString(20);
if (!fs::create_directories(randomDir)) {
throw Exception() << ErrStr("create dir failed:") << ErrStr(strerror(errno));
}
cout << "tmp dir in :" << randomDir.string() << endl;
if (verbose) {
if (logPath.empty()){
logPath = randomDir.string();
}
initLog(logPath, logLevel);
}
vector<string> action;
ContractDef contractDef;
LOGDEBUG << "start run";
int result = Tool.run(CreateFindMacroFactory(contractDef, action).get());
if (result != 0 || contractDef.name.empty()) {
throw Exception() << ErrStr("find macro PLATON_ABI failed");
}
LOGDEBUG << "result:" << result << "contract fullname:[" << contractDef.fullName << "]";
for (const string &act : action) {
LOGDEBUG << "contract:" << contractDef.name << " action:" << act;
}
ABIDef abiDef;
result = Tool.run(createFactory(contractDef.name, action, abiDef).get());
if (result != 0) {
throw Exception() << ErrStr("find contract abi defined failed");
}
LOGINFO << "find method success" << "find abi size:" << abiDef.abis.size();
for (size_t i = 0; i < abiDef.abis.size(); ++i) {
LOGDEBUG << "methodName:" << abiDef.abis[i].methodName << " args:(";
for (size_t j = 0; j < abiDef.abis[i].args.size(); ++j) {
LOGDEBUG << "name:" << abiDef.abis[i].args[j] << ", typeName:" << abiDef.abis[i].types[j].typeName << " realTypeName:" << abiDef.abis[i].types[j].realTypeName;
}
LOGDEBUG << ")";
LOGDEBUG << "return typeName:" << abiDef.abis[i].returnType.typeName << " realTypeName:" << abiDef.abis[i].returnType.realTypeName;
}
LOGDEBUG << "start create abi json";
string srcFilename = fs::path(op.getSourcePathList()[0]).filename().string();
createJsonAbi(abiDef, contractDef, srcFilename, outpath, randomDir);
ABI initAbi;
initAbi.methodName = "init";
initAbi.returnType.realTypeName = "void";
abiDef.abis.push_back(initAbi);
string externC = generateAbiCPlusPlus(contractDef, abiDef);
createContractFile(randomDir, op.getSourcePathList()[0], srcFilename, externC);
} catch (Exception e) {
cerr << *boost::get_error_info<ErrStr>(e) << endl;
cerr << e.what() << endl;
}
return 0;
}