[llvm] [llvm] Use std::make_unique (NFC) (PR #97165)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 29 06:20:43 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/97165

This patch is based on clang-tidy's modernize-make-unique but limited
to those cases where type names are mentioned twice like
std::unique_ptr<Type>(new Type()), which is a bit mouthful.


>From 0128f968145b8ba844cbac54777b7ceac5d63356 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 26 Jun 2024 17:50:49 -0700
Subject: [PATCH] [llvm] Use std::make_unique (NFC)

This patch is based on clang-tidy's modernize-make-unique but limited
to those cases where type names are mentioned twice like
std::unique_ptr<Type>(new Type()), which is a bit mouthful.
---
 llvm/include/llvm/BinaryFormat/MsgPackDocument.h | 4 ++--
 llvm/lib/DebugInfo/PDB/Native/SymbolCache.cpp    | 3 +--
 llvm/lib/ExecutionEngine/Orc/Layer.cpp           | 5 ++---
 llvm/lib/Object/TapiUniversal.cpp                | 6 +++---
 llvm/lib/Target/ARM/ARMBlockPlacement.cpp        | 2 +-
 llvm/lib/Target/ARM/ARMConstantIslandPass.cpp    | 2 +-
 llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp      | 2 +-
 llvm/lib/XRay/Profile.cpp                        | 2 +-
 llvm/tools/lli/ForwardingMemoryManager.h         | 4 ++--
 llvm/tools/sancov/sancov.cpp                     | 5 ++---
 llvm/unittests/Support/ErrorOrTest.cpp           | 4 +---
 11 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/llvm/include/llvm/BinaryFormat/MsgPackDocument.h b/llvm/include/llvm/BinaryFormat/MsgPackDocument.h
index 7a181bd9bf841..09091264cac2d 100644
--- a/llvm/include/llvm/BinaryFormat/MsgPackDocument.h
+++ b/llvm/include/llvm/BinaryFormat/MsgPackDocument.h
@@ -388,7 +388,7 @@ class Document {
   /// Create an empty Map node associated with this Document.
   MapDocNode getMapNode() {
     auto N = DocNode(&KindAndDocs[size_t(Type::Map)]);
-    Maps.push_back(std::unique_ptr<DocNode::MapTy>(new DocNode::MapTy));
+    Maps.push_back(std::make_unique<DocNode::MapTy>());
     N.Map = Maps.back().get();
     return N.getMap();
   }
@@ -396,7 +396,7 @@ class Document {
   /// Create an empty Array node associated with this Document.
   ArrayDocNode getArrayNode() {
     auto N = DocNode(&KindAndDocs[size_t(Type::Array)]);
-    Arrays.push_back(std::unique_ptr<DocNode::ArrayTy>(new DocNode::ArrayTy));
+    Arrays.push_back(std::make_unique<DocNode::ArrayTy>());
     N.Array = Arrays.back().get();
     return N.getArray();
   }
diff --git a/llvm/lib/DebugInfo/PDB/Native/SymbolCache.cpp b/llvm/lib/DebugInfo/PDB/Native/SymbolCache.cpp
index f89f09aa3399a..1a31aa206dfcc 100644
--- a/llvm/lib/DebugInfo/PDB/Native/SymbolCache.cpp
+++ b/llvm/lib/DebugInfo/PDB/Native/SymbolCache.cpp
@@ -618,8 +618,7 @@ SymbolCache::getSourceFileById(SymIndexId FileId) const {
   if (FileId == 0)
     return nullptr;
 
-  return std::unique_ptr<NativeSourceFile>(
-      new NativeSourceFile(*SourceFiles[FileId].get()));
+  return std::make_unique<NativeSourceFile>(*SourceFiles[FileId].get());
 }
 
 SymIndexId
diff --git a/llvm/lib/ExecutionEngine/Orc/Layer.cpp b/llvm/lib/ExecutionEngine/Orc/Layer.cpp
index 3368d3276cb37..d97336c914ea6 100644
--- a/llvm/lib/ExecutionEngine/Orc/Layer.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/Layer.cpp
@@ -200,9 +200,8 @@ BasicObjectLayerMaterializationUnit::Create(ObjectLayer &L,
   if (!ObjInterface)
     return ObjInterface.takeError();
 
-  return std::unique_ptr<BasicObjectLayerMaterializationUnit>(
-      new BasicObjectLayerMaterializationUnit(L, std::move(O),
-                                              std::move(*ObjInterface)));
+  return std::make_unique<BasicObjectLayerMaterializationUnit>(
+      L, std::move(O), std::move(*ObjInterface));
 }
 
 BasicObjectLayerMaterializationUnit::BasicObjectLayerMaterializationUnit(
diff --git a/llvm/lib/Object/TapiUniversal.cpp b/llvm/lib/Object/TapiUniversal.cpp
index bf96b57f03210..4db5841a8f34c 100644
--- a/llvm/lib/Object/TapiUniversal.cpp
+++ b/llvm/lib/Object/TapiUniversal.cpp
@@ -47,9 +47,9 @@ TapiUniversal::~TapiUniversal() = default;
 
 Expected<std::unique_ptr<TapiFile>>
 TapiUniversal::ObjectForArch::getAsObjectFile() const {
-  return std::unique_ptr<TapiFile>(new TapiFile(Parent->getMemoryBufferRef(),
-                                                *Parent->ParsedFile,
-                                                Parent->Libraries[Index].Arch));
+  return std::make_unique<TapiFile>(Parent->getMemoryBufferRef(),
+                                    *Parent->ParsedFile,
+                                    Parent->Libraries[Index].Arch);
 }
 
 Expected<std::unique_ptr<TapiUniversal>>
diff --git a/llvm/lib/Target/ARM/ARMBlockPlacement.cpp b/llvm/lib/Target/ARM/ARMBlockPlacement.cpp
index b2d291bbe7ffd..65538009b510d 100644
--- a/llvm/lib/Target/ARM/ARMBlockPlacement.cpp
+++ b/llvm/lib/Target/ARM/ARMBlockPlacement.cpp
@@ -219,7 +219,7 @@ bool ARMBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
   LLVM_DEBUG(dbgs() << DEBUG_PREFIX << "Running on " << MF.getName() << "\n");
   MLI = &getAnalysis<MachineLoopInfo>();
   TII = static_cast<const ARMBaseInstrInfo *>(ST.getInstrInfo());
-  BBUtils = std::unique_ptr<ARMBasicBlockUtils>(new ARMBasicBlockUtils(MF));
+  BBUtils = std::make_unique<ARMBasicBlockUtils>(MF);
   MF.RenumberBlocks();
   BBUtils->computeAllBlockSizes();
   BBUtils->adjustBBOffsetsAfter(&MF.front());
diff --git a/llvm/lib/Target/ARM/ARMConstantIslandPass.cpp b/llvm/lib/Target/ARM/ARMConstantIslandPass.cpp
index 90f5c6c40b49c..99ad53232f8a5 100644
--- a/llvm/lib/Target/ARM/ARMConstantIslandPass.cpp
+++ b/llvm/lib/Target/ARM/ARMConstantIslandPass.cpp
@@ -388,7 +388,7 @@ static bool AlignBlocks(MachineFunction *MF, const ARMSubtarget *STI) {
 bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) {
   MF = &mf;
   MCP = mf.getConstantPool();
-  BBUtils = std::unique_ptr<ARMBasicBlockUtils>(new ARMBasicBlockUtils(mf));
+  BBUtils = std::make_unique<ARMBasicBlockUtils>(mf);
 
   LLVM_DEBUG(dbgs() << "***** ARMConstantIslands: "
                     << MCP->getConstants().size() << " CP entries, aligned to "
diff --git a/llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp b/llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp
index 919828753f45a..48df7d43a280d 100644
--- a/llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp
+++ b/llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp
@@ -1301,7 +1301,7 @@ bool ARMLowOverheadLoops::runOnMachineFunction(MachineFunction &mf) {
   MRI = &MF->getRegInfo();
   TII = static_cast<const ARMBaseInstrInfo*>(ST.getInstrInfo());
   TRI = ST.getRegisterInfo();
-  BBUtils = std::unique_ptr<ARMBasicBlockUtils>(new ARMBasicBlockUtils(*MF));
+  BBUtils = std::make_unique<ARMBasicBlockUtils>(*MF);
   BBUtils->computeAllBlockSizes();
   BBUtils->adjustBBOffsetsAfter(&MF->front());
 
diff --git a/llvm/lib/XRay/Profile.cpp b/llvm/lib/XRay/Profile.cpp
index c1a43632b600f..4d1ab10e1d1b8 100644
--- a/llvm/lib/XRay/Profile.cpp
+++ b/llvm/lib/XRay/Profile.cpp
@@ -202,7 +202,7 @@ Profile mergeProfilesByThread(const Profile &L, const Profile &R) {
     for (const auto &Block : P.get()) {
       ThreadProfileIndexMap::iterator It;
       std::tie(It, std::ignore) = ThreadProfileIndex.insert(
-          {Block.Thread, PathDataMapPtr{new PathDataMap()}});
+          {Block.Thread, std::make_unique<PathDataMap>()});
       for (const auto &PathAndData : Block.PathData) {
         auto &PathID = PathAndData.first;
         auto &Data = PathAndData.second;
diff --git a/llvm/tools/lli/ForwardingMemoryManager.h b/llvm/tools/lli/ForwardingMemoryManager.h
index 2cc669953ee69..e5c10d672ccfb 100644
--- a/llvm/tools/lli/ForwardingMemoryManager.h
+++ b/llvm/tools/lli/ForwardingMemoryManager.h
@@ -98,8 +98,8 @@ class RemoteResolver : public LegacyJITSymbolResolver {
     auto H = DylibMgr->open("", 0);
     if (!H)
       return H.takeError();
-    return std::unique_ptr<RemoteResolver>(
-        new RemoteResolver(std::move(*DylibMgr), std::move(*H)));
+    return std::make_unique<RemoteResolver>(std::move(*DylibMgr),
+                                            std::move(*H));
   }
 
   JITSymbol findSymbol(const std::string &Name) override {
diff --git a/llvm/tools/sancov/sancov.cpp b/llvm/tools/sancov/sancov.cpp
index e5b0875d46a11..dd51226e21311 100644
--- a/llvm/tools/sancov/sancov.cpp
+++ b/llvm/tools/sancov/sancov.cpp
@@ -262,7 +262,7 @@ RawCoverage::read(const std::string &FileName) {
   // to compactify the data.
   Addrs->erase(0);
 
-  return std::unique_ptr<RawCoverage>(new RawCoverage(std::move(Addrs)));
+  return std::make_unique<RawCoverage>(std::move(Addrs));
 }
 
 // Print coverage addresses.
@@ -460,8 +460,7 @@ static std::unique_ptr<symbolize::LLVMSymbolizer> createSymbolizer() {
   symbolize::LLVMSymbolizer::Options SymbolizerOptions;
   SymbolizerOptions.Demangle = ClDemangle;
   SymbolizerOptions.UseSymbolTable = true;
-  return std::unique_ptr<symbolize::LLVMSymbolizer>(
-      new symbolize::LLVMSymbolizer(SymbolizerOptions));
+  return std::make_unique<symbolize::LLVMSymbolizer>(SymbolizerOptions);
 }
 
 static std::string normalizeFilename(const std::string &FileName) {
diff --git a/llvm/unittests/Support/ErrorOrTest.cpp b/llvm/unittests/Support/ErrorOrTest.cpp
index cb0e63660452b..a271c02d341af 100644
--- a/llvm/unittests/Support/ErrorOrTest.cpp
+++ b/llvm/unittests/Support/ErrorOrTest.cpp
@@ -36,9 +36,7 @@ TEST(ErrorOr, SimpleValue) {
 #endif
 }
 
-ErrorOr<std::unique_ptr<int> > t3() {
-  return std::unique_ptr<int>(new int(3));
-}
+ErrorOr<std::unique_ptr<int>> t3() { return std::make_unique<int>(3); }
 
 TEST(ErrorOr, Types) {
   int x;



More information about the llvm-commits mailing list