[clang] [NFC][clang-repl] Fix leak of remapped buffer in builder unit tests (PR #243463)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 17 04:50:11 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Aditya Sinha (AdityaSinha149)

<details>
<summary>Changes</summary>

This PR is a fix to a buildbot eror in #<!-- -->242296. It adds cleanup to the tests: `HIPDeviceDefaultsToO3` and `UserOptLevelOverridesHIPDeviceDefault`. 

The cleanup function is also moved to `InterpreterTestFixture.h` so that it could be used in both IncrementalCompilerBuilderTest.cpp` and `DeviceOffloadTest.cpp`.

Assisted by Claude Sonnet 5.5

---
Full diff: https://github.com/llvm/llvm-project/pull/243463.diff


3 Files Affected:

- (modified) clang/unittests/Interpreter/DeviceOffloadTest.cpp (+3) 
- (modified) clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp (+2-10) 
- (modified) clang/unittests/Interpreter/InterpreterTestFixture.h (+12) 


``````````diff
diff --git a/clang/unittests/Interpreter/DeviceOffloadTest.cpp b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
index bfa2c31577d2a..5fca1df5711a9 100644
--- a/clang/unittests/Interpreter/DeviceOffloadTest.cpp
+++ b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
@@ -152,6 +152,8 @@ TEST(DeviceOffloadBuilderTest, HIPDeviceDefaultsToO3) {
   auto HostCI = CB.CreateHost(OffloadType::HIP);
   ASSERT_THAT_EXPECTED(HostCI, llvm::Succeeded());
   EXPECT_EQ((*HostCI)->getCodeGenOpts().OptimizationLevel, 0u);
+  cleanupRemappedFileBuffers(**DeviceCI);
+  cleanupRemappedFileBuffers(**HostCI);
 }
 
 TEST(DeviceOffloadBuilderTest, UserOptLevelOverridesHIPDeviceDefault) {
@@ -161,6 +163,7 @@ TEST(DeviceOffloadBuilderTest, UserOptLevelOverridesHIPDeviceDefault) {
   auto DeviceCI = CB.CreateDevice(OffloadType::HIP);
   ASSERT_THAT_EXPECTED(DeviceCI, llvm::Succeeded());
   EXPECT_EQ((*DeviceCI)->getCodeGenOpts().OptimizationLevel, 1u);
+  cleanupRemappedFileBuffers(**DeviceCI);
 }
 
 TEST_F(DeviceOffloadHIPTest, DeviceObjectIsGenerated) {
diff --git a/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp b/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp
index 7b4633bfc9e7a..708608b306113 100644
--- a/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp
+++ b/clang/unittests/Interpreter/IncrementalCompilerBuilderTest.cpp
@@ -6,10 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "InterpreterTestFixture.h"
+
 #include "clang/Basic/TargetOptions.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Interpreter/Interpreter.h"
-#include "clang/Lex/PreprocessorOptions.h"
 #include "llvm/Support/Error.h"
 #include "gtest/gtest.h"
 
@@ -18,15 +19,6 @@ using namespace clang;
 
 namespace {
 
-// Usually FrontendAction takes the raw pointers and wraps them back into
-// unique_ptrs in InitializeFileRemapping()
-static void cleanupRemappedFileBuffers(CompilerInstance &CI) {
-  for (const auto &RB : CI.getPreprocessorOpts().RemappedFileBuffers) {
-    delete RB.second;
-  }
-  CI.getPreprocessorOpts().clearRemappedFiles();
-}
-
 TEST(IncrementalCompilerBuilder, SetCompilerArgs) {
   std::vector<const char *> ClangArgv = {"-Xclang", "-ast-dump-all"};
   auto CB = clang::IncrementalCompilerBuilder();
diff --git a/clang/unittests/Interpreter/InterpreterTestFixture.h b/clang/unittests/Interpreter/InterpreterTestFixture.h
index 627b728b2f0da..cf0f1f2565914 100644
--- a/clang/unittests/Interpreter/InterpreterTestFixture.h
+++ b/clang/unittests/Interpreter/InterpreterTestFixture.h
@@ -9,6 +9,9 @@
 #ifndef LLVM_CLANG_UNITTESTS_INTERPRETER_INTERPRETERTESTBASE_H
 #define LLVM_CLANG_UNITTESTS_INTERPRETER_INTERPRETERTESTBASE_H
 
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Lex/PreprocessorOptions.h"
+
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/TargetSelect.h"
@@ -21,6 +24,15 @@
 
 namespace clang {
 
+// The builder stores the main-file buffer as a raw pointer, which a
+// FrontendAction takes ownership of in InitializeFileRemapping(). Free it here
+// only if no action ran, otherwise it is freed twice.
+inline void cleanupRemappedFileBuffers(CompilerInstance &CI) {
+  for (const auto &RB : CI.getPreprocessorOpts().RemappedFileBuffers)
+    delete RB.second;
+  CI.getPreprocessorOpts().clearRemappedFiles();
+}
+
 class InterpreterTestBase : public ::testing::Test {
 protected:
   static bool HostSupportsJIT() {

``````````

</details>


https://github.com/llvm/llvm-project/pull/243463


More information about the cfe-commits mailing list