Skip to content

Commit 0cf830a

Browse files
committed
Add CPPINTEROP_EXTRA_INTERPRETER_ARGS env variable to pass extra arguments.
This patch also teaches CppInterOp to pass down arguments directly to llvm. For example -mllvm -debug-only=jitlink to subscribe for debug output for a specific debug type.
1 parent 36ac254 commit 0cf830a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ namespace Cpp {
113113
};
114114

115115
/// Enables or disables the debugging printouts on stderr.
116+
/// Debugging output can be enabled also by the environment variable
117+
/// CPPINTEROP_EXTRA_INTERPRETER_ARGS. For example,
118+
/// CPPINTEROP_EXTRA_INTERPRETER_ARGS="-mllvm -debug-only=jitcall" to produce
119+
/// only debug output for jitcall events.
116120
void EnableDebugOutput(bool value = true);
117121

118122
///\returns true if the debugging printouts on stderr are enabled.
@@ -296,6 +300,11 @@ namespace Cpp {
296300
/// Returns the argument name of function as string.
297301
std::string GetFunctionArgName(TCppFunction_t func, TCppIndex_t param_index);
298302

303+
/// Creates an instance of the interpreter we need for the various interop
304+
/// services.
305+
///\param[in] Args - the list of arguments for interpreter constructor.
306+
///\param[in] CPPINTEROP_EXTRA_INTERPRETER_ARGS - an env variable, if defined,
307+
/// adds additional arguments to the interpreter.
299308
TInterp_t CreateInterpreter(const std::vector<const char *> &Args = {});
300309

301310
TCppSema_t GetSema(TInterp_t interp);

lib/Interpreter/CppInterOp.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2429,7 +2429,40 @@ namespace Cpp {
24292429
"-std=c++14"};
24302430
ClingArgv.insert(ClingArgv.begin(), MainExecutableName.c_str());
24312431
ClingArgv.insert(ClingArgv.end(), Args.begin(), Args.end());
2432-
return new compat::Interpreter(ClingArgv.size(), &ClingArgv[0]);
2432+
2433+
// Process externally passed arguments if present.
2434+
std::vector<std::string> ExtraArgs;
2435+
llvm::Optional<std::string> EnvOpt
2436+
= llvm::sys::Process::GetEnv("CPPINTEROP_EXTRA_INTERPRETER_ARGS");
2437+
if (EnvOpt) {
2438+
StringRef Env(*EnvOpt);
2439+
while (!Env.empty()) {
2440+
StringRef Arg;
2441+
std::tie(Arg, Env) = Env.split(' ');
2442+
ExtraArgs.push_back(Arg.str());
2443+
}
2444+
}
2445+
std::transform(ExtraArgs.begin(), ExtraArgs.end(),
2446+
std::back_inserter(ClingArgv),
2447+
[&](const std::string& str) { return str.c_str(); });
2448+
2449+
auto I = new compat::Interpreter(ClingArgv.size(), &ClingArgv[0]);
2450+
2451+
// Honor -mllvm.
2452+
//
2453+
// FIXME: Remove this, one day.
2454+
// This should happen AFTER plugins have been loaded!
2455+
const CompilerInstance* Clang = I->getCI();
2456+
if (!Clang->getFrontendOpts().LLVMArgs.empty()) {
2457+
unsigned NumArgs = Clang->getFrontendOpts().LLVMArgs.size();
2458+
auto Args = std::make_unique<const char*[]>(NumArgs + 2);
2459+
Args[0] = "clang (LLVM option parsing)";
2460+
for (unsigned i = 0; i != NumArgs; ++i)
2461+
Args[i + 1] = Clang->getFrontendOpts().LLVMArgs[i].c_str();
2462+
Args[NumArgs + 1] = nullptr;
2463+
llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
2464+
}
2465+
return I;
24332466
}
24342467

24352468
TCppSema_t GetSema(TInterp_t interp) {

0 commit comments

Comments
 (0)