[clang] [clang-repl] Move the produced temporary files in wasm in a temp folder. (PR #175508)
Vassil Vassilev via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 12 02:50:25 PST 2026
https://github.com/vgvassilev updated https://github.com/llvm/llvm-project/pull/175508
>From 6ae13f21a18a293900e411d989de850d7e1e7d67 Mon Sep 17 00:00:00 2001
From: Vassil Vassilev <v.g.vassilev at gmail.com>
Date: Mon, 12 Jan 2026 10:47:29 +0000
Subject: [PATCH] [clang-repl] Move the produced temporary files in wasm in a
temp folder.
This patch avoids bloating the current folder with temporary files created by
wasm and moves them in a separate tmp directory. This patch is a version of a
downstream one resolving this issue.
Co-authored with Anutosh Bhat.
---
.../clang/Interpreter/IncrementalExecutor.h | 2 +-
clang/lib/Interpreter/IncrementalExecutor.cpp | 2 +-
clang/lib/Interpreter/Wasm.cpp | 33 ++++++++++++++++---
clang/lib/Interpreter/Wasm.h | 6 +++-
4 files changed, 35 insertions(+), 8 deletions(-)
diff --git a/clang/include/clang/Interpreter/IncrementalExecutor.h b/clang/include/clang/Interpreter/IncrementalExecutor.h
index 913da9230a947..0e879efa0b55f 100644
--- a/clang/include/clang/Interpreter/IncrementalExecutor.h
+++ b/clang/include/clang/Interpreter/IncrementalExecutor.h
@@ -90,4 +90,4 @@ class IncrementalExecutor {
} // namespace clang
-#endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALEXECUTOR_H
+#endif // LLVM_CLANG_LIB_INTERPRETER_INCREMENTALEXECUTOR_H
\ No newline at end of file
diff --git a/clang/lib/Interpreter/IncrementalExecutor.cpp b/clang/lib/Interpreter/IncrementalExecutor.cpp
index 001651522c329..74b751d0e2dae 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.cpp
+++ b/clang/lib/Interpreter/IncrementalExecutor.cpp
@@ -404,7 +404,7 @@ IncrementalExecutorBuilder::create(llvm::orc::ThreadSafeContext &TSC,
llvm::Error Err = llvm::Error::success();
std::unique_ptr<IncrementalExecutor> Executor;
#ifdef __EMSCRIPTEN__
- Executor = std::make_unique<WasmIncrementalExecutor>();
+ Executor = std::make_unique<WasmIncrementalExecutor>(Err);
#else
Executor = std::make_unique<OrcIncrementalExecutor>(TSC, *JITBuilder, Err);
#endif
diff --git a/clang/lib/Interpreter/Wasm.cpp b/clang/lib/Interpreter/Wasm.cpp
index 56f51e5d5311e..007227c73dc5f 100644
--- a/clang/lib/Interpreter/Wasm.cpp
+++ b/clang/lib/Interpreter/Wasm.cpp
@@ -12,6 +12,8 @@
#include "Wasm.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Module.h>
#include <llvm/MC/TargetRegistry.h>
@@ -57,7 +59,20 @@ bool link(llvm::ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
namespace clang {
-WasmIncrementalExecutor::WasmIncrementalExecutor() = default;
+WasmIncrementalExecutor::WasmIncrementalExecutor(llvm::Error &Err) {
+ llvm::ErrorAsOutParameter EAO(&Err);
+
+ if (Err)
+ return;
+
+ if (auto EC =
+ llvm::sys::fs::createUniqueDirectory("clang-wasm-exec-", TempDir))
+ Err = llvm::make_error<llvm::StringError>(
+ "Failed to create temporary directory for Wasm executor: " +
+ EC.message(),
+ llvm::inconvertibleErrorCode());
+}
+
WasmIncrementalExecutor::~WasmIncrementalExecutor() = default;
llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
@@ -74,11 +89,18 @@ llvm::Error WasmIncrementalExecutor::addModule(PartialTranslationUnit &PTU) {
llvm::TargetMachine *TargetMachine = Target->createTargetMachine(
PTU.TheModule->getTargetTriple(), "", "", TO, llvm::Reloc::Model::PIC_);
PTU.TheModule->setDataLayout(TargetMachine->createDataLayout());
- std::string ObjectFileName = PTU.TheModule->getName().str() + ".o";
- std::string BinaryFileName = PTU.TheModule->getName().str() + ".wasm";
- std::error_code Error;
- llvm::raw_fd_ostream ObjectFileOutput(llvm::StringRef(ObjectFileName), Error);
+ llvm::SmallString<256> ObjectFileName(TempDir);
+ llvm::sys::path::append(ObjectFileName, PTU.TheModule->getName() + ".o");
+
+ llvm::SmallString<256> BinaryFileName(TempDir);
+ llvm::sys::path::append(BinaryFileName, PTU.TheModule->getName() + ".wasm");
+
+ std::error_code EC;
+ llvm::raw_fd_ostream ObjectFileOutput(ObjectFileName, EC);
+
+ if (EC)
+ return llvm::errorCodeToError(EC);
llvm::legacy::PassManager PM;
if (TargetMachine->addPassesToEmitFile(PM, ObjectFileOutput, nullptr,
@@ -162,5 +184,6 @@ llvm::Error WasmIncrementalExecutor::LoadDynamicLibrary(const char *name) {
return llvm::make_error<llvm::StringError>("Failed to load dynamic library",
llvm::inconvertibleErrorCode());
}
+ return llvm::Error::success();
}
} // namespace clang
diff --git a/clang/lib/Interpreter/Wasm.h b/clang/lib/Interpreter/Wasm.h
index 55f375ac2b5c1..bf8777a41eac2 100644
--- a/clang/lib/Interpreter/Wasm.h
+++ b/clang/lib/Interpreter/Wasm.h
@@ -18,12 +18,13 @@
#endif // __EMSCRIPTEN__
#include "clang/Interpreter/IncrementalExecutor.h"
+#include "llvm/ADT/SmallString.h"
namespace clang {
class WasmIncrementalExecutor : public IncrementalExecutor {
public:
- WasmIncrementalExecutor();
+ WasmIncrementalExecutor(llvm::Error &Err);
~WasmIncrementalExecutor() override;
llvm::Error addModule(PartialTranslationUnit &PTU) override;
@@ -34,6 +35,9 @@ class WasmIncrementalExecutor : public IncrementalExecutor {
getSymbolAddress(llvm::StringRef Name,
SymbolNameKind NameKind) const override;
llvm::Error LoadDynamicLibrary(const char *name) override;
+
+private:
+ llvm::SmallString<256> TempDir;
};
} // namespace clang
More information about the cfe-commits
mailing list