[clang] [NFC][clang-repl] Fix leak of remapped buffer in builder unit tests (PR #243463)
Aditya Sinha via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 17 04:48:01 PST 2026
https://github.com/AdityaSinha149 created https://github.com/llvm/llvm-project/pull/243463
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
>From eddd952d6adb8d091d9053c33e03430907d97127 Mon Sep 17 00:00:00 2001
From: AdityaSinha149 <adsinha at amd.com>
Date: Tue, 17 Nov 2026 18:10:39 +0530
Subject: [PATCH] [NFC][clang-repl] Fix leak of remapped buffer in builder unit
tests
---
clang/unittests/Interpreter/DeviceOffloadTest.cpp | 3 +++
.../Interpreter/IncrementalCompilerBuilderTest.cpp | 12 ++----------
clang/unittests/Interpreter/InterpreterTestFixture.h | 12 ++++++++++++
3 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/clang/unittests/Interpreter/DeviceOffloadTest.cpp b/clang/unittests/Interpreter/DeviceOffloadTest.cpp
index bfa2c31577d2a5..5fca1df5711a90 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 7b4633bfc9e7a3..708608b3061133 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 627b728b2f0daa..cf0f1f25659142 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() {
More information about the cfe-commits
mailing list