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

Vassil Vassilev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 1 12:57:35 PDT 2021


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
v.g.vassilev marked 2 inline comments as done.
Closed by commit rGe386871e1d21: [clang-repl] Allow passing in code as positional arguments. (authored by v.g.vassilev).
Herald added a project: clang.

Changed prior to commit:
  https://reviews.llvm.org/D104898?vs=354437&id=355990#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104898/new/

https://reviews.llvm.org/D104898

Files:
  clang/test/Interpreter/execute.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
@@ -28,6 +28,9 @@
               llvm::cl::CommaSeparated);
 static llvm::cl::opt<bool> OptHostSupportsJit("host-supports-jit",
                                               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,
                              bool GenCrashDiag) {
@@ -78,15 +81,22 @@
                                     static_cast<void *>(&CI->getDiagnostics()));
 
   auto Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));
-  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))
+  for (const std::string &input : OptInputs) {
+    if (auto Err = Interp->ParseAndExecute(input))
       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
   // potentially about to delete. Uninstall the handler now so that any
   // later errors use the default handling behavior instead.
Index: clang/test/Interpreter/execute.cpp
===================================================================
--- clang/test/Interpreter/execute.cpp
+++ clang/test/Interpreter/execute.cpp
@@ -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
 // UNSUPPORTED: system-aix
 
+// CHECK-DRIVER: i = 10
+
+// RUN: cat %s | clang-repl | FileCheck %s
+
 extern "C" int printf(const char *, ...);
 int i = 42;
 auto r1 = printf("i = %d\n", i);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104898.355990.patch
Type: text/x-patch
Size: 2537 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210701/19de6b95/attachment-0001.bin>


More information about the cfe-commits mailing list