Skip to content

[LIB][TOOLS] Modernize unique_ptr new() C++11 to std::make_unique C++17 #124690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/include/llvm/BinaryFormat/MsgPackDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ class Document {
/// Copy a string into the Document's strings list, and return the copy that
/// is owned by the Document.
StringRef addString(StringRef S) {
Strings.push_back(std::unique_ptr<char[]>(new char[S.size()]));
Strings.push_back(std::make_unique<char[]>(S.size()));
memcpy(&Strings.back()[0], S.data(), S.size());
return StringRef(&Strings.back()[0], S.size());
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/RegAllocPBQP.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ class NodeMetadata {

void setup(const Vector& Costs) {
NumOpts = Costs.getLength() - 1;
OptUnsafeEdges = std::unique_ptr<unsigned[]>(new unsigned[NumOpts]());
OptUnsafeEdges = std::make_unique<unsigned[]>(NumOpts);
}

ReductionState getReductionState() const { return RS; }
Expand Down
8 changes: 2 additions & 6 deletions llvm/include/llvm/Transforms/Scalar/LoopPassManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,7 @@ createFunctionToLoopPassAdaptor(LoopNestPassT &&Pass, bool UseMemorySSA = false,
LoopStandardAnalysisResults &, LPMUpdater &>;
// Do not use make_unique, it causes too many template instantiations,
// causing terrible compile times.
return FunctionToLoopPassAdaptor(
std::unique_ptr<FunctionToLoopPassAdaptor::PassConceptT>(
new PassModelT(std::move(LPM))),
return FunctionToLoopPassAdaptor(std::make_unique<PassModelT>(std::move(LPM)),
UseMemorySSA, UseBlockFrequencyInfo, UseBranchProbabilityInfo, true);
}

Expand All @@ -495,9 +493,7 @@ createFunctionToLoopPassAdaptor<LoopPassManager>(
bool LoopNestMode = (LPM.getNumLoopPasses() == 0);
// Do not use make_unique, it causes too many template instantiations,
// causing terrible compile times.
return FunctionToLoopPassAdaptor(
std::unique_ptr<FunctionToLoopPassAdaptor::PassConceptT>(
new PassModelT(std::move(LPM))),
return FunctionToLoopPassAdaptor(std::make_unique<PassModelT>(std::move(LPM)),
UseMemorySSA, UseBlockFrequencyInfo, UseBranchProbabilityInfo,
LoopNestMode);
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/DetectDeadLanes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ DeadLaneDetector::DeadLaneDetector(const MachineRegisterInfo *MRI,
const TargetRegisterInfo *TRI)
: MRI(MRI), TRI(TRI) {
unsigned NumVirtRegs = MRI->getNumVirtRegs();
VRegInfos = std::unique_ptr<VRegInfo[]>(new VRegInfo[NumVirtRegs]);
VRegInfos = std::make_unique<VRegInfo[]>(NumVirtRegs);
WorklistMembers.resize(NumVirtRegs);
DefinedByCopy.resize(NumVirtRegs);
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/CodeGen/GlobalISel/RegBankSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ bool RegBankSelect::repairReg(
// TODO:
// Check if MI is legal. if not, we need to legalize all the
// instructions we are going to insert.
std::unique_ptr<MachineInstr *[]> NewInstrs(
new MachineInstr *[RepairPt.getNumInsertPoints()]);
auto NewInstrs = std::make_unique<MachineInstr *[]>(RepairPt.getNumInsertPoints());
bool IsFirst = true;
unsigned Idx = 0;
for (const std::unique_ptr<InsertPoint> &InsertPt : RepairPt) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/DebugInfo/PDB/Native/NativeExeSymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ std::unique_ptr<IPDBEnumSymbols>
NativeExeSymbol::findChildren(PDB_SymType Type) const {
switch (Type) {
case PDB_SymType::Compiland: {
return std::unique_ptr<IPDBEnumSymbols>(new NativeEnumModules(Session));
return std::make_unique<NativeEnumModules>(Session);
break;
}
case PDB_SymType::ArrayType:
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/DebugInfo/PDB/Native/NativeTypeFunctionSig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ NativeTypeFunctionSig::findChildren(PDB_SymType Type) const {

auto NET = std::make_unique<NativeEnumTypes>(Session,
/* copy */ ArgList.ArgIndices);
return std::unique_ptr<IPDBEnumSymbols>(
new NativeEnumFunctionArgs(Session, std::move(NET)));
return std::make_unique<NativeEnumFunctionArgs>(Session, std::move(NET));
}

SymIndexId NativeTypeFunctionSig::getClassParentId() const {
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/DebugInfo/PDB/Native/SymbolCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ SymbolCache::createTypeEnumerator(std::vector<TypeLeafKind> Kinds) {
return nullptr;
}
auto &Types = Tpi->typeCollection();
return std::unique_ptr<IPDBEnumSymbols>(
new NativeEnumTypes(Session, Types, std::move(Kinds)));
return std::make_unique<NativeEnumTypes>(Session, Types, std::move(Kinds));
}

std::unique_ptr<IPDBEnumSymbols>
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/InterfaceStub/IFSHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ bool usesTriple(StringRef Buf) {

Expected<std::unique_ptr<IFSStub>> ifs::readIFSFromBuffer(StringRef Buf) {
yaml::Input YamlIn(Buf);
std::unique_ptr<IFSStubTriple> Stub(new IFSStubTriple());
auto Stub = std::make_unique<IFSStubTriple>();
if (usesTriple(Buf)) {
YamlIn >> *Stub;
} else {
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/LTO/LTOBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,7 @@ static void runNewPMPasses(const Config &Conf, Module &Mod, TargetMachine *TM,

RegisterPassPlugins(Conf.PassPlugins, PB);

std::unique_ptr<TargetLibraryInfoImpl> TLII(
new TargetLibraryInfoImpl(Triple(TM->getTargetTriple())));
auto TLII = std::make_unique<TargetLibraryInfoImpl> (Triple(TM->getTargetTriple()));
if (Conf.Freestanding)
TLII->disableAllFunctions();
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/LTO/ThinLTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ static void optimizeModule(Module &TheModule, TargetMachine &TM,
PTO.SLPVectorization = true;
PassBuilder PB(&TM, PTO, PGOOpt, &PIC);

std::unique_ptr<TargetLibraryInfoImpl> TLII(
new TargetLibraryInfoImpl(Triple(TM.getTargetTriple())));
auto TLII = std::make_unique<TargetLibraryInfoImpl>(Triple(TM.getTargetTriple()));
if (Freestanding)
TLII->disableAllFunctions();
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/MC/MCDisassembler/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ LLVMCreateDisasmCPUFeatures(const char *TT, const char *CPU,
return nullptr;

// Set up the MCContext for creating symbols and MCExpr's.
std::unique_ptr<MCContext> Ctx(
new MCContext(Triple(TT), MAI.get(), MRI.get(), STI.get()));
auto Ctx = std::make_unique<MCContext>(
Triple(TT), MAI.get(), MRI.get(), STI.get());
if (!Ctx)
return nullptr;

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Object/GOFFObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using namespace llvm;
Expected<std::unique_ptr<ObjectFile>>
ObjectFile::createGOFFObjectFile(MemoryBufferRef Object) {
Error Err = Error::success();
std::unique_ptr<GOFFObjectFile> Ret(new GOFFObjectFile(Object, Err));
auto Ret = std::make_unique<GOFFObjectFile>(Object, Err);
if (Err)
return std::move(Err);
return std::move(Ret);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Object/MachOUniversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ void MachOUniversalBinary::anchor() { }
Expected<std::unique_ptr<MachOUniversalBinary>>
MachOUniversalBinary::create(MemoryBufferRef Source) {
Error Err = Error::success();
std::unique_ptr<MachOUniversalBinary> Ret(
new MachOUniversalBinary(Source, Err));
auto Ret = std::make_unique<MachOUniversalBinary>(Source, Err);
if (Err)
return std::move(Err);
return std::move(Ret);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Object/SymbolicFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ SymbolicFile::createSymbolicFile(MemoryBufferRef Object, file_magic Type,
case file_magic::wasm_object:
return ObjectFile::createObjectFile(Object, Type, InitContent);
case file_magic::coff_import_library:
return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
return std::make_unique<COFFImportFile>(Object);
case file_magic::elf_relocatable:
case file_magic::macho_object:
case file_magic::coff_object: {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Object/TapiUniversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TapiUniversal::ObjectForArch::getAsObjectFile() const {
Expected<std::unique_ptr<TapiUniversal>>
TapiUniversal::create(MemoryBufferRef Source) {
Error Err = Error::success();
std::unique_ptr<TapiUniversal> Ret(new TapiUniversal(Source, Err));
auto Ret = std::make_unique<TapiUniversal>(Source, Err);
if (Err)
return std::move(Err);
return std::move(Ret);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Support/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,7 @@ InMemoryFileSystem::openFileForRead(const Twine &Path) {
// When we have a file provide a heap-allocated wrapper for the memory buffer
// to match the ownership semantics for File.
if (auto *F = dyn_cast<detail::InMemoryFile>(*Node))
return std::unique_ptr<File>(
new detail::InMemoryFileAdaptor(*F, Path.str()));
return std::make_unique<detail::InMemoryFileAdaptor>(*F, Path.str());

// FIXME: errc::not_a_file?
return make_error_code(llvm::errc::invalid_argument);
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/Target/SPIRV/SPIRVAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ SPIRVTranslateModule(Module *M, std::string &SpirvObj, std::string &ErrMsg,
TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
legacy::PassManager PM;
PM.add(new TargetLibraryInfoWrapperPass(TLII));
std::unique_ptr<MachineModuleInfoWrapperPass> MMIWP(
new MachineModuleInfoWrapperPass(Target.get()));
auto MMIWP = std::make_unique<MachineModuleInfoWrapperPass>(Target.get());
const_cast<TargetLoweringObjectFile *>(Target->getObjFileLowering())
->Initialize(MMIWP.get()->getMMI().getContext(), *Target);

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/TextAPI/InterfaceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ InterfaceFile::extract(Architecture Arch) const {
inconvertibleErrorCode());
}

std::unique_ptr<InterfaceFile> IF(new InterfaceFile());
auto IF = std::make_unique<InterfaceFile>();
IF->setFileType(getFileType());
IF->setPath(getPath());
IF->addTargets(targets(Arch));
Expand Down
3 changes: 1 addition & 2 deletions llvm/tools/llvm-as/llvm-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) {
}

std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
auto Out = std::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::OF_None);
if (EC) {
errs() << EC.message() << '\n';
exit(1);
Expand Down
4 changes: 2 additions & 2 deletions llvm/tools/llvm-dis/llvm-dis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ int main(int argc, char **argv) {
}

std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(FinalFilename, EC, sys::fs::OF_TextWithCRLF));
auto Out = std::make_unique<ToolOutputFile>(
FinalFilename, EC, sys::fs::OF_TextWithCRLF);
if (EC) {
errs() << EC.message() << '\n';
return 1;
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-link/llvm-link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ static std::unique_ptr<Module> loadFile(const char *argv0,
static std::unique_ptr<Module> loadArFile(const char *Argv0,
std::unique_ptr<MemoryBuffer> Buffer,
LLVMContext &Context) {
std::unique_ptr<Module> Result(new Module("ArchiveModule", Context));
auto Result = std::make_unique<Module>("ArchiveModule", Context);
StringRef ArchiveName = Buffer->getBufferIdentifier();
if (Verbose)
errs() << "Reading library archive file '" << ArchiveName
Expand Down
3 changes: 1 addition & 2 deletions llvm/tools/llvm-modextract/llvm-modextract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ int main(int argc, char **argv) {
}

std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
auto Out = std::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::OF_None);
ExitOnErr(errorCodeToError(EC));

if (BinaryExtract) {
Expand Down
3 changes: 1 addition & 2 deletions llvm/tools/llvm-sim/llvm-sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ exportToFile(const StringRef FilePath,
const SimilarityGroupList &SimSections,
const DenseMap<Instruction *, unsigned> &LLVMInstNum) {
std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(FilePath, EC, sys::fs::OF_None));
auto Out = std::make_unique<ToolOutputFile>(FilePath, EC, sys::fs::OF_None);
if (EC)
return EC;

Expand Down
3 changes: 1 addition & 2 deletions llvm/tools/llvm-split/llvm-split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ int main(int argc, char **argv) {
unsigned I = 0;
const auto HandleModulePart = [&](std::unique_ptr<Module> MPart) {
std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(OutputFilename + utostr(I++), EC, sys::fs::OF_None));
auto Out = std::make_unique<ToolOutputFile>(OutputFilename + utostr(I++), EC, sys::fs::OF_None);
if (EC) {
errs() << EC.message() << '\n';
exit(1);
Expand Down
3 changes: 1 addition & 2 deletions llvm/tools/obj2yaml/obj2yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ int main(int argc, char *argv[]) {
nullptr, /*LongOptionsUseDoubleDash=*/true);

std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_Text));
auto Out = std::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::OF_Text);
if (EC) {
WithColor::error(errs(), "obj2yaml")
<< "failed to open '" + OutputFilename + "': " + EC.message() << '\n';
Expand Down
3 changes: 1 addition & 2 deletions llvm/tools/yaml2obj/yaml2obj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ int main(int argc, char **argv) {
};

std::error_code EC;
std::unique_ptr<ToolOutputFile> Out(
new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
auto Out = std::make_unique<ToolOutputFile>(OutputFilename, EC, sys::fs::OF_None);
if (EC) {
ErrHandler("failed to open '" + OutputFilename + "': " + EC.message());
return 1;
Expand Down
Loading