v.g.vassilev created this revision.
v.g.vassilev added reviewers: junaire, sunho, aaron.ballman.
Herald added a project: All.
v.g.vassilev requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added subscribers: jplehr, sstefan1.

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.


Repository:
  rC Clang

https://reviews.llvm.org/D150139

Files:
  clang/test/Interpreter/multiline.cpp
  clang/tools/clang-repl/ClangRepl.cpp


Index: clang/tools/clang-repl/ClangRepl.cpp
===================================================================
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -113,28 +113,38 @@
   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> ");
     }
   }
 
Index: clang/test/Interpreter/multiline.cpp
===================================================================
--- /dev/null
+++ 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
+


Index: clang/tools/clang-repl/ClangRepl.cpp
===================================================================
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -113,28 +113,38 @@
   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> ");
     }
   }
 
Index: clang/test/Interpreter/multiline.cpp
===================================================================
--- /dev/null
+++ 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
+
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to