[clang] [clang-repl] Fix inconsistent flushing between in-process and out-of-process (PR #166368)
Anutosh Bhat via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 6 00:09:28 PST 2025
https://github.com/anutosh491 updated https://github.com/llvm/llvm-project/pull/166368
>From 560ccf47c2cb4b56c4dbf805e89c323ba31af41a Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Tue, 4 Nov 2025 18:30:09 +0530
Subject: [PATCH 1/3] Fix inconsistent flushing between in-process and
out-of-process
---
clang/lib/Interpreter/Interpreter.cpp | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index cde354c9cd8d1..f3927fc341341 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -351,6 +351,15 @@ const char *const Runtimes = R"(
EXTERN_C void __clang_Interpreter_SetValueNoAlloc(void *This, void *OutVal, void *OpaqueType, ...);
)";
+const char *const OOPRuntimes = R"(
+ #include <stdio.h>
+ __attribute__((constructor))
+ static void __clang_repl_ioinit(void) {
+ setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stderr, NULL, _IONBF, 0);
+ }
+)";
+
llvm::Expected<std::pair<std::unique_ptr<llvm::orc::LLJITBuilder>, uint32_t>>
Interpreter::outOfProcessJITBuilder(JITConfig Config) {
std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC;
@@ -463,6 +472,11 @@ Interpreter::create(std::unique_ptr<CompilerInstance> CI, JITConfig Config) {
if (auto E = Interp->ParseAndExecute(Runtimes))
return std::move(E);
+ if (Config.IsOutOfProcess) {
+ if (auto E = Interp->ParseAndExecute(OOPRuntimes))
+ return std::move(E);
+ }
+
Interp->markUserCodeStart();
return std::move(Interp);
>From 54f577c9131f54815fad71e9b5ef932adc1bb288 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Thu, 6 Nov 2025 13:11:13 +0530
Subject: [PATCH 2/3] Initialize streams to be unbuffered
---
clang/lib/Interpreter/Interpreter.cpp | 14 --------------
clang/tools/clang-repl/ClangRepl.cpp | 14 ++++++++++++++
2 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index f3927fc341341..cde354c9cd8d1 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -351,15 +351,6 @@ const char *const Runtimes = R"(
EXTERN_C void __clang_Interpreter_SetValueNoAlloc(void *This, void *OutVal, void *OpaqueType, ...);
)";
-const char *const OOPRuntimes = R"(
- #include <stdio.h>
- __attribute__((constructor))
- static void __clang_repl_ioinit(void) {
- setvbuf(stdout, NULL, _IONBF, 0);
- setvbuf(stderr, NULL, _IONBF, 0);
- }
-)";
-
llvm::Expected<std::pair<std::unique_ptr<llvm::orc::LLJITBuilder>, uint32_t>>
Interpreter::outOfProcessJITBuilder(JITConfig Config) {
std::unique_ptr<llvm::orc::ExecutorProcessControl> EPC;
@@ -472,11 +463,6 @@ Interpreter::create(std::unique_ptr<CompilerInstance> CI, JITConfig Config) {
if (auto E = Interp->ParseAndExecute(Runtimes))
return std::move(E);
- if (Config.IsOutOfProcess) {
- if (auto E = Interp->ParseAndExecute(OOPRuntimes))
- return std::move(E);
- }
-
Interp->markUserCodeStart();
return std::move(Interp);
diff --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index c7879422cd7df..530955b31814e 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -352,6 +352,20 @@ int main(int argc, const char **argv) {
Interp = ExitOnErr(clang::Interpreter::create(std::move(CI), Config));
}
+ if (Config.IsOutOfProcess) {
+ static const char *const InitUnbufferedStreams = R"(
+ #include <stdio.h>
+ __attribute__((constructor))
+ static void __clang_repl_ioinit(void) {
+ setvbuf(stdout, NULL, _IONBF, 0);
+ setvbuf(stderr, NULL, _IONBF, 0);
+ }
+ )";
+
+ if (auto Err = Interp->ParseAndExecute(InitUnbufferedStreams))
+ llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+ }
+
bool HasError = false;
for (const std::string &input : OptInputs) {
>From 181acda02109a1fca7f6e88654426788b14154c4 Mon Sep 17 00:00:00 2001
From: anutosh491 <andersonbhat491 at gmail.com>
Date: Thu, 6 Nov 2025 13:39:08 +0530
Subject: [PATCH 3/3] Add tests
---
clang/test/Interpreter/out-of-process.cpp | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/clang/test/Interpreter/out-of-process.cpp b/clang/test/Interpreter/out-of-process.cpp
index 6922ca6e82053..cfbca644cdc90 100644
--- a/clang/test/Interpreter/out-of-process.cpp
+++ b/clang/test/Interpreter/out-of-process.cpp
@@ -4,6 +4,11 @@
extern "C" int printf(const char *, ...);
+printf("FLUSH_START");
+// CHECK: FLUSH_START
+printf("\nFLUSH_OK\n");
+// CHECK: FLUSH_OK
+
int intVar = 0;
double doubleVar = 3.14;
%undo
@@ -85,4 +90,7 @@ int complexFunc(int x) \
auto r11 = printf("complexFunc(5) = %d\n", complexFunc(5));
// CHECK: complexFunc(5) = 15
+printf("FINAL_FLUSH");
+// CHECK: FINAL_FLUSH
+
%quit
\ No newline at end of file
More information about the cfe-commits
mailing list