-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathInvocationOptions.cpp
More file actions
203 lines (170 loc) · 6.88 KB
/
Copy pathInvocationOptions.cpp
File metadata and controls
203 lines (170 loc) · 6.88 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
//--------------------------------------------------------------------*- C++ -*-
// CLING - the C++ LLVM-based InterpreterG :)
// author: Axel Naumann <axel@cern.ch>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------
#include "cling/Interpreter/InvocationOptions.h"
#include "cling/Interpreter/ClingOptions.h"
#include "cling/Utils/Output.h"
#include "clang/Driver/Options.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Option/OptTable.h"
#include <memory>
using namespace clang;
using namespace clang::driver;
using namespace llvm;
using namespace llvm::opt;
using namespace cling;
using namespace cling::driver::clingoptions;
namespace {
// MSVC C++ backend currently does not support -nostdinc++. Translate it to
// -nostdinc so users scripts are insulated from mundane implementation details.
#if defined(LLVM_ON_WIN32) && !defined(_LIBCPP_VERSION)
#define CLING_TRANSLATE_NOSTDINCxx
// Likely to be string-pooled, but make sure it's valid after func exit.
static const char kNoStdInc[] = "-nostdinc";
#endif
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR)
#include "cling/Interpreter/ClingOptions.inc"
#undef OPTION
#undef PREFIX
static const OptTable::Info ClingInfoTable[] = {
#define PREFIX(NAME, VALUE)
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR) \
{ PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, PARAM, \
FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS },
#include "cling/Interpreter/ClingOptions.inc"
#undef OPTION
#undef PREFIX
};
class ClingOptTable : public OptTable {
public:
ClingOptTable()
: OptTable(ClingInfoTable) {}
};
static OptTable* CreateClingOptTable() {
return new ClingOptTable();
}
static void ParseStartupOpts(cling::InvocationOptions& Opts,
InputArgList& Args) {
Opts.ErrorOut = Args.hasArg(OPT__errorout);
Opts.NoLogo = Args.hasArg(OPT__nologo);
Opts.ShowVersion = Args.hasArg(OPT_version);
Opts.Help = Args.hasArg(OPT_help);
Opts.NoRuntime = Args.hasArg(OPT_noruntime);
if (Arg* MetaStringArg = Args.getLastArg(OPT__metastr, OPT__metastr_EQ)) {
Opts.MetaString = MetaStringArg->getValue();
if (Opts.MetaString.empty()) {
cling::errs() << "ERROR: meta string must be non-empty! Defaulting to '.'.\n";
Opts.MetaString = ".";
}
}
}
static void Extend(std::vector<std::string>& A, std::vector<std::string> B) {
A.reserve(A.size()+B.size());
for (std::string& Val: B)
A.push_back(std::move(Val));
}
static void ParseLinkerOpts(cling::InvocationOptions& Opts,
InputArgList& Args /* , Diags */) {
Extend(Opts.LibsToLoad, Args.getAllArgValues(OPT_l));
Extend(Opts.LibSearchPath, Args.getAllArgValues(OPT_L));
}
}
CompilerOptions::CompilerOptions(int argc, const char* const* argv) :
Language(false), ResourceDir(false), SysRoot(false), NoBuiltinInc(false),
NoCXXInc(false), StdVersion(false), StdLib(false), HasOutput(false),
Verbose(false) {
if (argc && argv) {
// Preserve what's already in Remaining, the user might want to push args
// to clang while still using main's argc, argv
// insert should/usually does call reserve, but its not part of the standard
Remaining.reserve(Remaining.size() + argc);
Remaining.insert(Remaining.end(), argv, argv+argc);
Parse(argc, argv);
}
}
void CompilerOptions::Parse(int argc, const char* const argv[],
std::vector<std::string>* Inputs) {
unsigned MissingArgIndex, MissingArgCount;
std::unique_ptr<OptTable> OptsC1(createDriverOptTable());
ArrayRef<const char *> ArgStrings(argv+1, argv + argc);
InputArgList Args(OptsC1->ParseArgs(ArgStrings, MissingArgIndex,
MissingArgCount, 0,
options::NoDriverOption | options::CLOption));
for (const Arg* arg : Args) {
switch (arg->getOption().getID()) {
// case options::OPT_d_Flag:
case options::OPT_E:
case options::OPT_o: HasOutput = true; break;
case options::OPT_x: Language = true; break;
case options::OPT_resource_dir: ResourceDir = true; break;
case options::OPT_isysroot: SysRoot = true; break;
case options::OPT_std_EQ: StdVersion = true; break;
case options::OPT_stdlib_EQ: StdLib = true; break;
// case options::OPT_nostdlib:
case options::OPT_nobuiltininc: NoBuiltinInc = true; break;
// case options::OPT_nostdinc:
case options::OPT_nostdincxx: NoCXXInc = true; break;
case options::OPT_v: Verbose = true; break;
default:
if (Inputs && arg->getOption().getKind() == Option::InputClass)
Inputs->push_back(arg->getValue());
break;
}
}
}
InvocationOptions::InvocationOptions(int argc, const char* const* argv) :
MetaString("."), ErrorOut(false), NoLogo(false), ShowVersion(false),
Help(false), NoRuntime(false) {
ArrayRef<const char *> ArgStrings(argv, argv + argc);
unsigned MissingArgIndex, MissingArgCount;
std::unique_ptr<OptTable> Opts(CreateClingOptTable());
InputArgList Args(Opts->ParseArgs(ArgStrings, MissingArgIndex,
MissingArgCount, 0,
options::NoDriverOption | options::CLOption));
// Forward unknown arguments.
for (const Arg* arg : Args) {
switch (arg->getOption().getKind()) {
case Option::FlagClass:
// pass -v to clang as well
if (arg->getOption().getID() != OPT_v)
break;
case Option::UnknownClass:
case Option::InputClass:
// prune "-" we need to control where it appears when invoking clang
if (!arg->getSpelling().equals("-")) {
if (const char* Arg = argv[arg->getIndex()]) {
#ifdef CLING_TRANSLATE_NOSTDINCxx
if (!::strcmp(Arg, "-nostdinc++"))
Arg = kNoStdInc;
#endif
CompilerOpts.Remaining.push_back(Arg);
}
}
default:
break;
}
}
// Get Input list and any compiler specific flags we're interested in
CompilerOpts.Parse(argc, argv, &Inputs);
ParseStartupOpts(*this, Args);
ParseLinkerOpts(*this, Args);
}
void InvocationOptions::PrintHelp() {
std::unique_ptr<OptTable> Opts(CreateClingOptTable());
Opts->PrintHelp(cling::outs(), "cling",
"cling: LLVM/clang C++ Interpreter: http://cern.ch/cling");
cling::outs() << "\n\n";
std::unique_ptr<OptTable> OptsC1(createDriverOptTable());
OptsC1->PrintHelp(cling::outs(), "clang -cc1",
"LLVM 'Clang' Compiler: http://clang.llvm.org");
}