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
|
/*
Copyright Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "Detect.h"
#include "Options.h"
#include "RunClang.h"
#include "Utils.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include <iostream>
#include <set>
#include <system_error>
#include <vector>
#include <string.h>
class StringSaver: public llvm::cl::StringSaver {
std::set<std::string> Strings;
public:
const char* SaveString(const char* s) {
return this->Strings.insert(s).first->c_str();
}
};
//----------------------------------------------------------------------------
int main(int argc_in, const char** argv_in)
{
suppressInteractiveErrors();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
llvm::InitializeAllAsmParsers();
llvm::SmallVector<const char*, 64> argv;
llvm::SpecificBumpPtrAllocator<char> argAlloc;
if(std::error_code e =
llvm::sys::Process::GetArgumentVector(
argv, llvm::ArrayRef<const char*>(argv_in, argc_in), argAlloc)) {
llvm::errs() << "error: could not get arguments: " << e.message() << "\n";
return 1;
} else if(argv.empty()) {
llvm::errs() << "error: no argv[0]?!\n";
return 1;
}
StringSaver argSaver;
llvm::cl::ExpandResponseFiles(
argSaver, llvm::cl::TokenizeGNUCommandLine, argv);
size_t const argc = argv.size();
if(!findResourceDir(argv[0], std::cerr)) {
return 1;
}
const char* usage =
"Usage: castxml ( <castxml-opt> | <clang-opt> | <src> )...\n"
"\n"
" Options interpreted by castxml are listed below.\n"
" Remaining options are given to the internal Clang compiler.\n"
"\n"
"Options:\n"
"\n"
" --castxml-cc-<id> <cc>\n"
" --castxml-cc-<id> \"(\" <cc> <cc-opt>... \")\"\n"
" Configure the internal Clang preprocessor and target\n"
" platform to match that of the given compiler command.\n"
" The <id> must be \"gnu\" or \"msvc\". <cc> names a\n"
" compiler (e.g. \"gcc\") and <cc-opt>... specifies\n"
" options that may affect its target (e.g. \"-m32\").\n"
"\n"
" --castxml-gccxml\n"
" Write gccxml-format output to <src>.xml or file named by '-o'\n"
"\n"
" --castxml-start <name>\n"
" Start AST traversal at declaration with given (qualified) name\n"
"\n"
" -help, --help\n"
" Print castxml and internal Clang compiler usage information\n"
"\n"
" -o <file>\n"
" Write output to <file>\n"
"\n"
" --version\n"
" Print castxml and internal Clang compiler version information\n"
"\n"
;
Options opts;
llvm::SmallVector<const char *, 16> clang_args;
llvm::SmallVector<const char *, 16> cc_args;
const char* cc_id = 0;
for(size_t i=1; i < argc; ++i) {
if(strcmp(argv[i], "--castxml-gccxml") == 0) {
if(!opts.GccXml) {
opts.GccXml = true;
} else {
std::cerr <<
"error: '--castxml-gccxml' may be given at most once!\n"
"\n" <<
usage
;
return 1;
}
} else if(strcmp(argv[i], "--castxml-start") == 0) {
if((i+1) < argc) {
opts.StartNames.push_back(argv[++i]);
} else {
std::cerr <<
"error: argument to '--castxml-start' is missing "
"(expected 1 value)\n"
"\n" <<
usage
;
return 1;
}
} else if(strncmp(argv[i], "--castxml-cc-", 13) == 0) {
if(!cc_id) {
cc_id = argv[i] + 13;
if((i+1) >= argc) {
continue;
}
++i;
if(strncmp(argv[i], "-", 1) == 0) {
std::cerr <<
"error: argument to '--castxml-cc-" << cc_id <<
"' may not start with '-'\n"
"\n" <<
usage
;
return 1;
}
if(strcmp(argv[i], "(") == 0) {
unsigned int depth = 1;
for(++i; i < argc && depth > 0; ++i) {
if(strncmp(argv[i], "--castxml-", 10) == 0) {
std::cerr <<
"error: arguments to '--castxml-cc-" << cc_id <<
"' may not start with '--castxml-'\n"
"\n" <<
usage
;
return 1;
} else if(strcmp(argv[i], "(") == 0) {
++depth;
cc_args.push_back(argv[i]);
} else if(strcmp(argv[i], ")") == 0) {
if(--depth) {
cc_args.push_back(argv[i]);
}
} else {
cc_args.push_back(argv[i]);
}
}
if(depth) {
std::cerr <<
"error: unbalanced parentheses after '--castxml-cc-" <<
cc_id << "'\n"
"\n" <<
usage
;
return 1;
}
--i;
} else {
cc_args.push_back(argv[i]);
}
} else {
std::cerr <<
"error: '--castxml-cc-<id>' may be given at most once!\n"
"\n" <<
usage
;
return 1;
}
} else if(strcmp(argv[i], "-E") == 0) {
opts.PPOnly = true;
} else if(strcmp(argv[i], "-o") == 0) {
if((i+1) < argc) {
opts.OutputFile = argv[++i];
} else {
std::cerr <<
"error: argument to '-o' is missing (expected 1 value)\n"
"\n" <<
usage
;
return 1;
}
} else if(strcmp(argv[i], "-help") == 0 ||
strcmp(argv[i], "--help") == 0) {
std::cout <<
usage <<
"\n"
"Help for the internal Clang compiler appears below.\n"
"\n"
"---------------------------------------------------------------"
"\n" <<
std::endl;
// Also print Clang help.
clang_args.push_back(argv[i]);
} else if(strcmp(argv[i], "--version") == 0) {
std::cout <<
"castxml version " << getVersionString() << "\n"
"\n"
"CastXML project maintained and supported by Kitware "
"(kitware.com).\n" <<
std::endl;
// Also print Clang version.
clang_args.push_back(argv[i]);
} else {
clang_args.push_back(argv[i]);
}
}
if(cc_id) {
opts.HaveCC = true;
if(cc_args.empty()) {
std::cerr <<
"error: '--castxml-cc-" << cc_id <<
"' must be followed by a compiler command!\n"
"\n" <<
usage
;
return 1;
}
if(!detectCC(cc_id, cc_args.data(), cc_args.data() + cc_args.size(),
opts)) {
return 1;
}
}
if(clang_args.empty()) {
return 0;
}
return runClang(clang_args.data(), clang_args.data() + clang_args.size(),
opts);
}
|