[clang-repl] Enable basic multiline support.

This patch allows the users to use backslash to tell clang-repl that more input
is coming. This would help support OpenMP directives which generally require to
be in separate lines.
This commit is contained in:
Vassil Vassilev 2023-05-08 19:40:39 +00:00
parent b7fb2a3fec
commit c96c5edb58
2 changed files with 44 additions and 10 deletions

View File

@ -0,0 +1,24 @@
// REQUIRES: host-supports-jit
// UNSUPPORTED: system-aix
// RUN: cat %s | clang-repl | FileCheck %s
extern "C" int printf(const char*,...);
int i = \
12;
printf("i=%d\n", i);
// CHECK: i=12
void f(int x) \
{ \
printf("x=\
%d", x); \
}
f(i);
// CHECK: x=12
// FIXME: Support preprocessor directives.
// #if 0 \
// #error "Can't be!" \
// #endif

View File

@ -113,28 +113,38 @@ int main(int argc, const char **argv) {
if (OptInputs.empty()) {
llvm::LineEditor LE("clang-repl");
// FIXME: Add LE.setListCompleter
std::string Input;
while (std::optional<std::string> Line = LE.readLine()) {
if (*Line == R"(%quit)")
llvm::StringRef L = *Line;
L = L.trim();
if (L.endswith("\\")) {
// FIXME: Support #ifdef X \ ...
Input += L.drop_back(1);
LE.setPrompt("clang-repl... ");
continue;
}
Input += L;
if (Input == R"(%quit)") {
break;
if (*Line == R"(%undo)") {
} else if (Input == R"(%undo)") {
if (auto Err = Interp->Undo()) {
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
HasError = true;
}
continue;
}
if (Line->rfind("%lib ", 0) == 0) {
if (auto Err = Interp->LoadDynamicLibrary(Line->data() + 5)) {
} else if (Input.rfind("%lib ", 0) == 0) {
if (auto Err = Interp->LoadDynamicLibrary(Input.data() + 5)) {
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
HasError = true;
}
continue;
}
if (auto Err = Interp->ParseAndExecute(*Line)) {
} else if (auto Err = Interp->ParseAndExecute(Input)) {
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
HasError = true;
}
Input = "";
LE.setPrompt("clang-repl> ");
}
}