[clang-repl] Allow passing in code as positional arguments.

Now we can do things like: clang-repl "int i = 1;" "int j = 2;".

Differential revision: https://reviews.llvm.org/D104898
This commit is contained in:
Vassil Vassilev 2021-07-01 17:03:23 +00:00
parent 45e8a0befb
commit e386871e1d
2 changed files with 22 additions and 7 deletions

View File

@ -1,7 +1,12 @@
// RUN: cat %s | clang-repl | FileCheck %s // RUN: clang-repl "int i = 10;" 'extern "C" int printf(const char*,...);' \
// RUN: 'auto r1 = printf("i = %d\n", i);' | FileCheck --check-prefix=CHECK-DRIVER %s
// REQUIRES: host-supports-jit // REQUIRES: host-supports-jit
// UNSUPPORTED: system-aix // UNSUPPORTED: system-aix
// CHECK-DRIVER: i = 10
// RUN: cat %s | clang-repl | FileCheck %s
extern "C" int printf(const char *, ...); extern "C" int printf(const char *, ...);
int i = 42; int i = 42;
auto r1 = printf("i = %d\n", i); auto r1 = printf("i = %d\n", i);

View File

@ -28,6 +28,9 @@ static llvm::cl::list<std::string>
llvm::cl::CommaSeparated); llvm::cl::CommaSeparated);
static llvm::cl::opt<bool> OptHostSupportsJit("host-supports-jit", static llvm::cl::opt<bool> OptHostSupportsJit("host-supports-jit",
llvm::cl::Hidden); llvm::cl::Hidden);
static llvm::cl::list<std::string> OptInputs(llvm::cl::Positional,
llvm::cl::ZeroOrMore,
llvm::cl::desc("[code to run]"));
static void LLVMErrorHandler(void *UserData, const std::string &Message, static void LLVMErrorHandler(void *UserData, const std::string &Message,
bool GenCrashDiag) { bool GenCrashDiag) {
@ -78,15 +81,22 @@ int main(int argc, const char **argv) {
static_cast<void *>(&CI->getDiagnostics())); static_cast<void *>(&CI->getDiagnostics()));
auto Interp = ExitOnErr(clang::Interpreter::create(std::move(CI))); auto Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));
llvm::LineEditor LE("clang-repl"); for (const std::string &input : OptInputs) {
// FIXME: Add LE.setListCompleter if (auto Err = Interp->ParseAndExecute(input))
while (llvm::Optional<std::string> Line = LE.readLine()) {
if (*Line == "quit")
break;
if (auto Err = Interp->ParseAndExecute(*Line))
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: "); llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
} }
if (OptInputs.empty()) {
llvm::LineEditor LE("clang-repl");
// FIXME: Add LE.setListCompleter
while (llvm::Optional<std::string> Line = LE.readLine()) {
if (*Line == "quit")
break;
if (auto Err = Interp->ParseAndExecute(*Line))
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
}
}
// Our error handler depends on the Diagnostics object, which we're // Our error handler depends on the Diagnostics object, which we're
// potentially about to delete. Uninstall the handler now so that any // potentially about to delete. Uninstall the handler now so that any
// later errors use the default handling behavior instead. // later errors use the default handling behavior instead.