Skip to content

Commit 337f0ed

Browse files
committed
[RISCV] Fix memory leak in RISCVInstrInfoTest.cpp unittest
We need to make sure the RISCVTargetMachine is also deleted.
1 parent 4e6a9fc commit 337f0ed

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

llvm/unittests/Target/RISCV/RISCVInstrInfoTest.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "llvm/CodeGen/MachineModuleInfo.h"
1313
#include "llvm/MC/TargetRegistry.h"
1414
#include "llvm/Support/TargetSelect.h"
15+
#include "llvm/Target/TargetLoweringObjectFile.h"
1516
#include "llvm/Target/TargetMachine.h"
1617
#include "llvm/Target/TargetOptions.h"
1718

@@ -25,6 +26,7 @@ namespace {
2526

2627
class RISCVInstrInfoTest : public testing::TestWithParam<const char *> {
2728
protected:
29+
std::unique_ptr<RISCVTargetMachine> TM;
2830
std::unique_ptr<LLVMContext> Ctx;
2931
std::unique_ptr<RISCVSubtarget> ST;
3032
std::unique_ptr<MachineModuleInfo> MMI;
@@ -42,16 +44,16 @@ class RISCVInstrInfoTest : public testing::TestWithParam<const char *> {
4244
const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
4345
TargetOptions Options;
4446

45-
RISCVTargetMachine *TM = static_cast<RISCVTargetMachine *>(
46-
TheTarget->createTargetMachine(TT, "generic", "", Options, std::nullopt,
47-
std::nullopt, CodeGenOptLevel::Default));
47+
TM.reset(static_cast<RISCVTargetMachine *>(TheTarget->createTargetMachine(
48+
TT, "generic", "", Options, std::nullopt, std::nullopt,
49+
CodeGenOptLevel::Default)));
4850

4951
Ctx = std::make_unique<LLVMContext>();
5052
Module M("Module", *Ctx);
5153
M.setDataLayout(TM->createDataLayout());
5254
auto *FType = FunctionType::get(Type::getVoidTy(*Ctx), false);
5355
auto *F = Function::Create(FType, GlobalValue::ExternalLinkage, "Test", &M);
54-
MMI = std::make_unique<MachineModuleInfo>(TM);
56+
MMI = std::make_unique<MachineModuleInfo>(TM.get());
5557

5658
ST = std::make_unique<RISCVSubtarget>(
5759
TM->getTargetTriple(), TM->getTargetCPU(), TM->getTargetCPU(),

0 commit comments

Comments
 (0)