forked from chrisburel/smokegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
293 lines (261 loc) · 10.5 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Copyright (C) 2009 Arno Rehn <arno@arnorehn.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <QCoreApplication>
#include <QList>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QLibrary>
#include <QtXml>
#include <QtDebug>
#include <iostream>
#include <clang/Tooling/Tooling.h>
#include "options.h"
#include "config.h"
#include "frontendaction.h"
#include "embedded_includes.h"
typedef int (*GenerateFn)();
static void showUsage()
{
std::cout <<
"Usage: smokegen [options] [-clangOptions [options]] -- <header files>" << std::endl <<
"Possible command line options are:" << std::endl <<
" -I <include dir>" << std::endl <<
" -d <path to file containing #defines>" << std::endl <<
" -dm <list of macros that should be ignored>" << std::endl <<
" -g <generator to use>" << std::endl <<
" -qt enables Qt-mode (special treatment of QFlags)" << std::endl <<
" -t resolve typedefs" << std::endl <<
" -o <output dir>" << std::endl <<
" -config <config file>" << std::endl <<
" -clangOptions <flags to pass to the clang tool>" << std::endl <<
" -h shows this message" << std::endl;
}
int main(int argc, char **argv)
{
try
{
if (argc == 1) {
showUsage();
return EXIT_SUCCESS;
}
QCoreApplication app(argc, argv);
const QStringList& args = app.arguments();
QFileInfo configFile;
QString generator;
bool addHeaders = false;
bool addClangOptions = false;
bool hasCommandLineGenerator = false;
QStringList classes;
ParserOptions::notToBeResolved << "FILE";
std::vector<std::string> Argv{
argv[0],
"-x", "c++",
};
for (int i = 1; i < args.count(); i++) {
if ((args[i] == "-I" || args[i] == "-d" || args[i] == "-dm" ||
args[i] == "-g" || args[i] == "-config") && i + 1 >= args.count())
{
qCritical() << "not enough parameters for option" << args[i];
return EXIT_FAILURE;
}
if (args[i] == "-I") {
ParserOptions::includeDirs << QDir(args[++i]);
}
else if (args[i] == "-config") {
configFile = QFileInfo(args[++i]);
}
else if (args[i] == "-d") {
ParserOptions::definesList = QFileInfo(args[++i]);
}
else if (args[i] == "-dm") {
ParserOptions::dropMacros += args[++i].split(',');
}
else if (args[i] == "-g") {
generator = args[++i];
hasCommandLineGenerator = true;
}
else if ((args[i] == "-h" || args[i] == "--help") && argc == 2) {
showUsage();
return EXIT_SUCCESS;
}
else if (args[i] == "-t") {
ParserOptions::resolveTypedefs = true;
}
else if (args[i] == "-qt") {
ParserOptions::qtMode = true;
}
else if (args[i] == "-clangOptions") {
addClangOptions = true;
}
else if (args[i] == "--") {
addClangOptions = false;
addHeaders = true;
}
else if (addClangOptions) {
Argv.push_back(args[i].toStdString());
}
else if (addHeaders) {
ParserOptions::headerList << QFileInfo(args[i]);
}
}
if (configFile.exists()) {
QFile file(configFile.filePath());
file.open(QIODevice::ReadOnly);
QDomDocument doc;
doc.setContent(file.readAll());
file.close();
QDomElement root = doc.documentElement();
QDomNode node = root.firstChild();
while (!node.isNull()) {
QDomElement elem = node.toElement();
if (elem.isNull()) {
node = node.nextSibling();
continue;
}
if (elem.tagName() == "resolveTypedefs") {
ParserOptions::resolveTypedefs = (elem.text() == "true");
}
else if (elem.tagName() == "qtMode") {
ParserOptions::qtMode = (elem.text() == "true");
}
else if (!hasCommandLineGenerator && elem.tagName() == "generator") {
generator = elem.text();
}
else if (elem.tagName() == "includeDirs") {
QDomNode dir = elem.firstChild();
while (!dir.isNull()) {
QDomElement elem = dir.toElement();
if (elem.isNull()) {
dir = dir.nextSibling();
continue;
}
if (elem.tagName() == "dir") {
ParserOptions::includeDirs << QDir(elem.text());
}
else if (elem.tagName() == "framework") {
ParserOptions::frameworkDirs << QDir(elem.text());
}
dir = dir.nextSibling();
}
}
else if (elem.tagName() == "definesList") {
// reference to an external file, so it can be auto-generated
ParserOptions::definesList = QFileInfo(elem.text());
}
else if (elem.tagName() == "dropMacros") {
QDomNode macro = elem.firstChild();
while (!macro.isNull()) {
QDomElement elem = macro.toElement();
if (elem.isNull()) {
macro = macro.nextSibling();
continue;
}
if (elem.tagName() == "name") {
ParserOptions::dropMacros << elem.text();
}
macro = macro.nextSibling();
}
}
node = node.nextSibling();
}
}
else {
qWarning() << "Couldn't find config file" << configFile.filePath();
}
// first try to load plugins from the executable's directory
QLibrary lib(app.applicationDirPath() + "/generator_" + generator);
lib.load();
if (!lib.isLoaded()) {
lib.unload();
lib.setFileName(app.applicationDirPath() + "/../lib" + LIB_SUFFIX + "/smokegen/generator_" + generator);
lib.load();
}
if (!lib.isLoaded()) {
lib.unload();
lib.setFileName("generator_" + generator);
lib.load();
}
if (!lib.isLoaded()) {
qCritical() << lib.errorString();
return EXIT_FAILURE;
}
qDebug() << "using generator" << lib.fileName();
GenerateFn generate = (GenerateFn)lib.resolve("generate");
if (!generate) {
qCritical() << "couldn't resolve symbol 'generate', aborting";
return EXIT_FAILURE;
}
foreach(QDir dir, ParserOptions::includeDirs) {
if (!dir.exists()) {
qWarning() << "include directory" << dir.path() << "doesn't exist";
ParserOptions::includeDirs.removeAll(dir);
}
}
QStringList defines;
if (ParserOptions::definesList.exists()) {
QFile file(ParserOptions::definesList.filePath());
file.open(QIODevice::ReadOnly);
while (!file.atEnd()) {
QByteArray array = file.readLine();
if (!array.isEmpty())
defines << array.trimmed();
}
file.close();
}
else if (!ParserOptions::definesList.filePath().isEmpty()) {
qWarning() << "didn't find file" << ParserOptions::definesList.filePath();
}
QFile log("generator.log");
bool logErrors = log.open(QFile::WriteOnly | QFile::Truncate);
QTextStream logOut(&log);
foreach(QFileInfo file, ParserOptions::headerList) {
qDebug() << "parsing" << file.absoluteFilePath();
foreach(QDir dir, ParserOptions::includeDirs) {
Argv.push_back("-I" + dir.path().toStdString());
}
foreach(QDir dir, ParserOptions::frameworkDirs) {
Argv.push_back("-iframework");
Argv.push_back(dir.path().toStdString());
}
foreach(QString define, defines) {
Argv.push_back("-D" + define.toStdString());
}
Argv.push_back(file.absoluteFilePath().toStdString());
Argv.push_back("-I/builtins");
Argv.push_back("-fsyntax-only");
clang::FileManager FM({ "." });
FM.Retain();
clang::tooling::ToolInvocation inv(Argv, std::make_unique<SmokegenFrontendAction>(), &FM);
const EmbeddedFile* f = EmbeddedFiles;
while (f->filename) {
inv.mapVirtualFile(f->filename, { f->content, f->size });
++f;
}
if (!inv.run()) {
return 1;
}
// this has already been parsed because it was included by some header
if (!logErrors)
continue;
}
log.close();
return generate();
}
catch (const std::exception& e)
{
std::cout << "An error occured: " << e.what();
}
}