Skip to content

Commit e386871

Browse files
committed
[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
1 parent 45e8a0b commit e386871

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

clang/test/Interpreter/execute.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
// RUN: cat %s | clang-repl | FileCheck %s
1+
// RUN: clang-repl "int i = 10;" 'extern "C" int printf(const char*,...);' \
2+
// RUN: 'auto r1 = printf("i = %d\n", i);' | FileCheck --check-prefix=CHECK-DRIVER %s
23
// REQUIRES: host-supports-jit
34
// UNSUPPORTED: system-aix
45

6+
// CHECK-DRIVER: i = 10
7+
8+
// RUN: cat %s | clang-repl | FileCheck %s
9+
510
extern "C" int printf(const char *, ...);
611
int i = 42;
712
auto r1 = printf("i = %d\n", i);

clang/tools/clang-repl/ClangRepl.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ static llvm::cl::list<std::string>
2828
llvm::cl::CommaSeparated);
2929
static llvm::cl::opt<bool> OptHostSupportsJit("host-supports-jit",
3030
llvm::cl::Hidden);
31+
static llvm::cl::list<std::string> OptInputs(llvm::cl::Positional,
32+
llvm::cl::ZeroOrMore,
33+
llvm::cl::desc("[code to run]"));
3134

3235
static void LLVMErrorHandler(void *UserData, const std::string &Message,
3336
bool GenCrashDiag) {
@@ -78,15 +81,22 @@ int main(int argc, const char **argv) {
7881
static_cast<void *>(&CI->getDiagnostics()));
7982

8083
auto Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));
81-
llvm::LineEditor LE("clang-repl");
82-
// FIXME: Add LE.setListCompleter
83-
while (llvm::Optional<std::string> Line = LE.readLine()) {
84-
if (*Line == "quit")
85-
break;
86-
if (auto Err = Interp->ParseAndExecute(*Line))
84+
for (const std::string &input : OptInputs) {
85+
if (auto Err = Interp->ParseAndExecute(input))
8786
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
8887
}
8988

89+
if (OptInputs.empty()) {
90+
llvm::LineEditor LE("clang-repl");
91+
// FIXME: Add LE.setListCompleter
92+
while (llvm::Optional<std::string> Line = LE.readLine()) {
93+
if (*Line == "quit")
94+
break;
95+
if (auto Err = Interp->ParseAndExecute(*Line))
96+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
97+
}
98+
}
99+
90100
// Our error handler depends on the Diagnostics object, which we're
91101
// potentially about to delete. Uninstall the handler now so that any
92102
// later errors use the default handling behavior instead.

0 commit comments

Comments
 (0)