[clang] c96c5ed - [clang-repl] Enable basic multiline support.

Vassil Vassilev via cfe-commits cfe-commits at lists.llvm.org
Sat May 20 07:53:49 PDT 2023


Author: Vassil Vassilev
Date: 2023-05-20T14:40:58Z
New Revision: c96c5edb58ed29e0fd936bc082ec6cae7082854e

URL: https://github.com/llvm/llvm-project/commit/c96c5edb58ed29e0fd936bc082ec6cae7082854e
DIFF: https://github.com/llvm/llvm-project/commit/c96c5edb58ed29e0fd936bc082ec6cae7082854e.diff

LOG: [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.

Added: 
    clang/test/Interpreter/multiline.cpp

Modified: 
    clang/tools/clang-repl/ClangRepl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/Interpreter/multiline.cpp b/clang/test/Interpreter/multiline.cpp
new file mode 100644
index 0000000000000..054e61a7e3d62
--- /dev/null
+++ b/clang/test/Interpreter/multiline.cpp
@@ -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
+

diff  --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index 33faf3fab58f0..5ac071fdbce3f 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -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> ");
     }
   }
 


        


More information about the cfe-commits mailing list