Author: Vassil Vassilev
Date: 2026-01-12T16:57:43+05:30
New Revision: 4cec62299dadbcb44035c14228c834c0be990d73

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

LOG: [clang-repl] Move the produced temporary files in wasm in a temp folder. 
(#175508)

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.

Added: 
    

Modified: 
    clang/include/clang/Interpreter/IncrementalExecutor.h
    clang/lib/Interpreter/IncrementalExecutor.cpp
    clang/lib/Interpreter/Wasm.cpp
    clang/lib/Interpreter/Wasm.h

Removed: 
    


################################################################################
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


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to