[llvm] r307762 - Have Module::createRNG return a unique_ptr

Serge Guelton via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 12 01:03:44 PDT 2017


Author: serge_sans_paille
Date: Wed Jul 12 01:03:44 2017
New Revision: 307762

URL: http://llvm.org/viewvc/llvm-project?rev=307762&view=rev
Log:
Have Module::createRNG return a unique_ptr

Instead of a raw pointer, this makes memory management safer.

Modified:
    llvm/trunk/include/llvm/IR/Module.h
    llvm/trunk/lib/IR/Module.cpp
    llvm/trunk/unittests/IR/ModuleTest.cpp

Modified: llvm/trunk/include/llvm/IR/Module.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Module.h?rev=307762&r1=307761&r2=307762&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Module.h (original)
+++ llvm/trunk/include/llvm/IR/Module.h Wed Jul 12 01:03:44 2017
@@ -249,7 +249,7 @@ public:
   /// when other randomness consuming passes are added or removed. In
   /// addition, the random stream will be reproducible across LLVM
   /// versions when the pass does not change.
-  RandomNumberGenerator *createRNG(const Pass* P) const;
+  std::unique_ptr<RandomNumberGenerator> createRNG(const Pass* P) const;
 
 /// @}
 /// @name Module Level Mutators

Modified: llvm/trunk/lib/IR/Module.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Module.cpp?rev=307762&r1=307761&r2=307762&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Module.cpp (original)
+++ llvm/trunk/lib/IR/Module.cpp Wed Jul 12 01:03:44 2017
@@ -88,7 +88,7 @@ Module::~Module() {
   delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
 }
 
-RandomNumberGenerator *Module::createRNG(const Pass* P) const {
+std::unique_ptr<RandomNumberGenerator> Module::createRNG(const Pass* P) const {
   SmallString<32> Salt(P->getPassName());
 
   // This RNG is guaranteed to produce the same random stream only
@@ -103,7 +103,7 @@ RandomNumberGenerator *Module::createRNG
   // store salt metadata from the Module constructor.
   Salt += sys::path::filename(getModuleIdentifier());
 
-  return new RandomNumberGenerator(Salt);
+  return std::unique_ptr<RandomNumberGenerator>{new RandomNumberGenerator(Salt)};
 }
 
 /// getNamedValue - Return the first global value in the module with

Modified: llvm/trunk/unittests/IR/ModuleTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/ModuleTest.cpp?rev=307762&r1=307761&r2=307762&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/ModuleTest.cpp (original)
+++ llvm/trunk/unittests/IR/ModuleTest.cpp Wed Jul 12 01:03:44 2017
@@ -63,7 +63,7 @@ TEST(ModuleTest, randomNumberGenerator)
 
   std::array<int, NBCheck> RandomStreams[2];
   for (auto &RandomStream : RandomStreams) {
-    std::unique_ptr<RandomNumberGenerator> RNG{M.createRNG(&DP)};
+    std::unique_ptr<RandomNumberGenerator> RNG = M.createRNG(&DP);
     std::generate(RandomStream.begin(), RandomStream.end(),
                   [&]() { return dist(*RNG); });
   }




More information about the llvm-commits mailing list