Selaa lähdekoodia

cmMakefile: modernize memory management

Marc Chevrier 5 vuotta sitten
vanhempi
sitoutus
f466cea3c9

+ 4 - 2
Source/cmAddTestCommand.cxx

@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmAddTestCommand.h"
 
+#include <cm/memory>
+
 #include "cmExecutionStatus.h"
 #include "cmMakefile.h"
 #include "cmStringAlgorithms.h"
@@ -44,7 +46,7 @@ bool cmAddTestCommand(std::vector<std::string> const& args,
   } else {
     test = mf.CreateTest(args[0]);
     test->SetOldStyle(true);
-    mf.AddTestGenerator(new cmTestGenerator(test));
+    mf.AddTestGenerator(cm::make_unique<cmTestGenerator>(test));
   }
   test->SetCommand(command);
 
@@ -141,7 +143,7 @@ bool cmAddTestCommandHandleNameMode(std::vector<std::string> const& args,
     test->SetProperty("WORKING_DIRECTORY", working_directory.c_str());
   }
   test->SetCommandExpandLists(command_expand_lists);
-  mf.AddTestGenerator(new cmTestGenerator(test, configurations));
+  mf.AddTestGenerator(cm::make_unique<cmTestGenerator>(test, configurations));
 
   return true;
 }

+ 1 - 2
Source/cmExtraKateGenerator.cxx

@@ -246,8 +246,7 @@ std::string cmExtraKateGenerator::GenerateFilesString(
       }
     }
 
-    const std::vector<cmSourceFile*>& sources = makefile->GetSourceFiles();
-    for (cmSourceFile* sf : sources) {
+    for (const auto& sf : makefile->GetSourceFiles()) {
       if (sf->GetIsGenerated()) {
         continue;
       }

+ 3 - 2
Source/cmFileAPICodemodel.cxx

@@ -493,8 +493,9 @@ void CodemodelConfig::ProcessDirectories()
     Directory& d = *di;
 
     // Accumulate the presence of install rules on the way up.
-    for (auto gen : d.LocalGenerator->GetMakefile()->GetInstallGenerators()) {
-      if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(gen)) {
+    for (const auto& gen :
+         d.LocalGenerator->GetMakefile()->GetInstallGenerators()) {
+      if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(gen.get())) {
         d.HasInstallRule = true;
         break;
       }

+ 4 - 4
Source/cmGlobalGenerator.cxx

@@ -1412,7 +1412,7 @@ bool cmGlobalGenerator::Compute()
 
   for (const auto& localGen : this->LocalGenerators) {
     cmMakefile* mf = localGen->GetMakefile();
-    for (cmInstallGenerator* g : mf->GetInstallGenerators()) {
+    for (const auto& g : mf->GetInstallGenerators()) {
       if (!g->Compute(localGen.get())) {
         return false;
       }
@@ -1687,10 +1687,10 @@ void cmGlobalGenerator::CreateGeneratorTargets(TargetTypes targetTypes)
   std::map<cmTarget*, cmGeneratorTarget*> importedMap;
   for (unsigned int i = 0; i < this->Makefiles.size(); ++i) {
     auto& mf = this->Makefiles[i];
-    for (cmTarget* ownedImpTgt : mf->GetOwnedImportedTargets()) {
+    for (const auto& ownedImpTgt : mf->GetOwnedImportedTargets()) {
       cmLocalGenerator* lg = this->LocalGenerators[i].get();
-      auto gt = cm::make_unique<cmGeneratorTarget>(ownedImpTgt, lg);
-      importedMap[ownedImpTgt] = gt.get();
+      auto gt = cm::make_unique<cmGeneratorTarget>(ownedImpTgt.get(), lg);
+      importedMap[ownedImpTgt.get()] = gt.get();
       lg->AddOwnedImportedGeneratorTarget(std::move(gt));
     }
   }

+ 1 - 2
Source/cmGlobalNinjaGenerator.cxx

@@ -1330,8 +1330,7 @@ void cmGlobalNinjaGenerator::WriteUnknownExplicitDependencies(std::ostream& os)
         knownDependencies.insert(this->ConvertToNinjaPath(j));
       }
     }
-    for (cmGeneratorExpressionEvaluationFile* li :
-         lg->GetMakefile()->GetEvaluationFiles()) {
+    for (const auto& li : lg->GetMakefile()->GetEvaluationFiles()) {
       // get all the files created by generator expressions and convert them
       // to ninja paths
       for (std::string const& evaluationFile : li->GetFiles()) {

+ 71 - 70
Source/cmInstallCommand.cxx

@@ -85,7 +85,7 @@ public:
   std::string DefaultComponentName;
 };
 
-cmInstallTargetGenerator* CreateInstallTargetGenerator(
+std::unique_ptr<cmInstallTargetGenerator> CreateInstallTargetGenerator(
   cmTarget& target, const cmInstallCommandArguments& args, bool impLib,
   cmListFileBacktrace const& backtrace, const std::string& destination,
   bool forceOpt = false, bool namelink = false)
@@ -95,16 +95,16 @@ cmInstallTargetGenerator* CreateInstallTargetGenerator(
   target.SetHaveInstallRule(true);
   const char* component = namelink ? args.GetNamelinkComponent().c_str()
                                    : args.GetComponent().c_str();
-  auto g = new cmInstallTargetGenerator(
+  auto g = cm::make_unique<cmInstallTargetGenerator>(
     target.GetName(), destination.c_str(), impLib,
     args.GetPermissions().c_str(), args.GetConfigurations(), component,
     message, args.GetExcludeFromAll(), args.GetOptional() || forceOpt,
     backtrace);
-  target.AddInstallGenerator(g);
+  target.AddInstallGenerator(g.get());
   return g;
 }
 
-cmInstallTargetGenerator* CreateInstallTargetGenerator(
+std::unique_ptr<cmInstallTargetGenerator> CreateInstallTargetGenerator(
   cmTarget& target, const cmInstallCommandArguments& args, bool impLib,
   cmListFileBacktrace const& backtrace, bool forceOpt = false,
   bool namelink = false)
@@ -114,20 +114,20 @@ cmInstallTargetGenerator* CreateInstallTargetGenerator(
                                       namelink);
 }
 
-cmInstallFilesGenerator* CreateInstallFilesGenerator(
+std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator(
   cmMakefile* mf, const std::vector<std::string>& absFiles,
   const cmInstallCommandArguments& args, bool programs,
   const std::string& destination)
 {
   cmInstallGenerator::MessageLevel message =
     cmInstallGenerator::SelectMessageLevel(mf);
-  return new cmInstallFilesGenerator(
+  return cm::make_unique<cmInstallFilesGenerator>(
     absFiles, destination.c_str(), programs, args.GetPermissions().c_str(),
     args.GetConfigurations(), args.GetComponent().c_str(), message,
     args.GetExcludeFromAll(), args.GetRename().c_str(), args.GetOptional());
 }
 
-cmInstallFilesGenerator* CreateInstallFilesGenerator(
+std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator(
   cmMakefile* mf, const std::vector<std::string>& absFiles,
   const cmInstallCommandArguments& args, bool programs)
 {
@@ -196,13 +196,15 @@ bool HandleScriptMode(std::vector<std::string> const& args,
         status.SetError("given a directory as value of SCRIPT argument.");
         return false;
       }
-      helper.Makefile->AddInstallGenerator(new cmInstallScriptGenerator(
-        script.c_str(), false, component.c_str(), exclude_from_all));
+      helper.Makefile->AddInstallGenerator(
+        cm::make_unique<cmInstallScriptGenerator>(
+          script.c_str(), false, component.c_str(), exclude_from_all));
     } else if (doing_code) {
       doing_code = false;
       std::string const& code = arg;
-      helper.Makefile->AddInstallGenerator(new cmInstallScriptGenerator(
-        code.c_str(), true, component.c_str(), exclude_from_all));
+      helper.Makefile->AddInstallGenerator(
+        cm::make_unique<cmInstallScriptGenerator>(
+          code.c_str(), true, component.c_str(), exclude_from_all));
     }
   }
 
@@ -449,16 +451,16 @@ bool HandleTargetsMode(std::vector<std::string> const& args,
   for (cmTarget* ti : targets) {
     // Handle each target type.
     cmTarget& target = *ti;
-    cmInstallTargetGenerator* archiveGenerator = nullptr;
-    cmInstallTargetGenerator* libraryGenerator = nullptr;
-    cmInstallTargetGenerator* namelinkGenerator = nullptr;
-    cmInstallTargetGenerator* runtimeGenerator = nullptr;
-    cmInstallTargetGenerator* objectGenerator = nullptr;
-    cmInstallTargetGenerator* frameworkGenerator = nullptr;
-    cmInstallTargetGenerator* bundleGenerator = nullptr;
-    cmInstallFilesGenerator* privateHeaderGenerator = nullptr;
-    cmInstallFilesGenerator* publicHeaderGenerator = nullptr;
-    cmInstallFilesGenerator* resourceGenerator = nullptr;
+    std::unique_ptr<cmInstallTargetGenerator> archiveGenerator;
+    std::unique_ptr<cmInstallTargetGenerator> libraryGenerator;
+    std::unique_ptr<cmInstallTargetGenerator> namelinkGenerator;
+    std::unique_ptr<cmInstallTargetGenerator> runtimeGenerator;
+    std::unique_ptr<cmInstallTargetGenerator> objectGenerator;
+    std::unique_ptr<cmInstallTargetGenerator> frameworkGenerator;
+    std::unique_ptr<cmInstallTargetGenerator> bundleGenerator;
+    std::unique_ptr<cmInstallFilesGenerator> privateHeaderGenerator;
+    std::unique_ptr<cmInstallFilesGenerator> publicHeaderGenerator;
+    std::unique_ptr<cmInstallFilesGenerator> resourceGenerator;
 
     // Track whether this is a namelink-only rule.
     bool namelinkOnly = false;
@@ -485,7 +487,7 @@ bool HandleTargetsMode(std::vector<std::string> const& args,
             runtimeGenerator = CreateInstallTargetGenerator(
               target, runtimeArgs, false, helper.Makefile->GetBacktrace());
           }
-          if ((archiveGenerator == nullptr) && (runtimeGenerator == nullptr)) {
+          if (!archiveGenerator && !runtimeGenerator) {
             archiveGenerator = CreateInstallTargetGenerator(
               target, archiveArgs, true, helper.Makefile->GetBacktrace(),
               helper.GetArchiveDestination(nullptr));
@@ -712,43 +714,18 @@ bool HandleTargetsMode(std::vector<std::string> const& args,
       }
     }
 
-    // Keep track of whether we're installing anything in each category
-    installsArchive = installsArchive || archiveGenerator != nullptr;
-    installsLibrary = installsLibrary || libraryGenerator != nullptr;
-    installsNamelink = installsNamelink || namelinkGenerator != nullptr;
-    installsRuntime = installsRuntime || runtimeGenerator != nullptr;
-    installsObject = installsObject || objectGenerator != nullptr;
-    installsFramework = installsFramework || frameworkGenerator != nullptr;
-    installsBundle = installsBundle || bundleGenerator != nullptr;
-    installsPrivateHeader =
-      installsPrivateHeader || privateHeaderGenerator != nullptr;
-    installsPublicHeader =
-      installsPublicHeader || publicHeaderGenerator != nullptr;
-    installsResource = installsResource || resourceGenerator;
-
-    helper.Makefile->AddInstallGenerator(archiveGenerator);
-    helper.Makefile->AddInstallGenerator(libraryGenerator);
-    helper.Makefile->AddInstallGenerator(namelinkGenerator);
-    helper.Makefile->AddInstallGenerator(runtimeGenerator);
-    helper.Makefile->AddInstallGenerator(objectGenerator);
-    helper.Makefile->AddInstallGenerator(frameworkGenerator);
-    helper.Makefile->AddInstallGenerator(bundleGenerator);
-    helper.Makefile->AddInstallGenerator(privateHeaderGenerator);
-    helper.Makefile->AddInstallGenerator(publicHeaderGenerator);
-    helper.Makefile->AddInstallGenerator(resourceGenerator);
-
     // Add this install rule to an export if one was specified and
     // this is not a namelink-only rule.
     if (!exports.empty() && !namelinkOnly) {
       auto te = cm::make_unique<cmTargetExport>();
       te->TargetName = target.GetName();
-      te->ArchiveGenerator = archiveGenerator;
-      te->BundleGenerator = bundleGenerator;
-      te->FrameworkGenerator = frameworkGenerator;
-      te->HeaderGenerator = publicHeaderGenerator;
-      te->LibraryGenerator = libraryGenerator;
-      te->RuntimeGenerator = runtimeGenerator;
-      te->ObjectsGenerator = objectGenerator;
+      te->ArchiveGenerator = archiveGenerator.get();
+      te->BundleGenerator = bundleGenerator.get();
+      te->FrameworkGenerator = frameworkGenerator.get();
+      te->HeaderGenerator = publicHeaderGenerator.get();
+      te->LibraryGenerator = libraryGenerator.get();
+      te->RuntimeGenerator = runtimeGenerator.get();
+      te->ObjectsGenerator = objectGenerator.get();
       te->InterfaceIncludeDirectories =
         cmJoin(includesArgs.GetIncludeDirs(), ";");
 
@@ -756,6 +733,29 @@ bool HandleTargetsMode(std::vector<std::string> const& args,
         ->GetExportSets()[exports]
         .AddTargetExport(std::move(te));
     }
+
+    // Keep track of whether we're installing anything in each category
+    installsArchive = installsArchive || archiveGenerator;
+    installsLibrary = installsLibrary || libraryGenerator;
+    installsNamelink = installsNamelink || namelinkGenerator;
+    installsRuntime = installsRuntime || runtimeGenerator;
+    installsObject = installsObject || objectGenerator;
+    installsFramework = installsFramework || frameworkGenerator;
+    installsBundle = installsBundle || bundleGenerator;
+    installsPrivateHeader = installsPrivateHeader || privateHeaderGenerator;
+    installsPublicHeader = installsPublicHeader || publicHeaderGenerator;
+    installsResource = installsResource || resourceGenerator;
+
+    helper.Makefile->AddInstallGenerator(std::move(archiveGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(libraryGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(namelinkGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(runtimeGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(objectGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(frameworkGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(bundleGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(privateHeaderGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(publicHeaderGenerator));
+    helper.Makefile->AddInstallGenerator(std::move(resourceGenerator));
   }
 
   // Tell the global generator about any installation component names
@@ -1200,10 +1200,11 @@ bool HandleDirectoryMode(std::vector<std::string> const& args,
     cmInstallGenerator::SelectMessageLevel(helper.Makefile, message_never);
 
   // Create the directory install generator.
-  helper.Makefile->AddInstallGenerator(new cmInstallDirectoryGenerator(
-    dirs, destination, permissions_file.c_str(), permissions_dir.c_str(),
-    configurations, component.c_str(), message, exclude_from_all,
-    literal_args.c_str(), optional));
+  helper.Makefile->AddInstallGenerator(
+    cm::make_unique<cmInstallDirectoryGenerator>(
+      dirs, destination, permissions_file.c_str(), permissions_dir.c_str(),
+      configurations, component.c_str(), message, exclude_from_all,
+      literal_args.c_str(), optional));
 
   // Tell the global generator about any installation component names
   // specified.
@@ -1291,12 +1292,12 @@ bool HandleExportAndroidMKMode(std::vector<std::string> const& args,
     cmInstallGenerator::SelectMessageLevel(helper.Makefile);
 
   // Create the export install generator.
-  cmInstallExportGenerator* exportGenerator = new cmInstallExportGenerator(
-    &exportSet, ica.GetDestination().c_str(), ica.GetPermissions().c_str(),
-    ica.GetConfigurations(), ica.GetComponent().c_str(), message,
-    ica.GetExcludeFromAll(), fname.c_str(), name_space.c_str(), exportOld,
-    true);
-  helper.Makefile->AddInstallGenerator(exportGenerator);
+  helper.Makefile->AddInstallGenerator(
+    cm::make_unique<cmInstallExportGenerator>(
+      &exportSet, ica.GetDestination().c_str(), ica.GetPermissions().c_str(),
+      ica.GetConfigurations(), ica.GetComponent().c_str(), message,
+      ica.GetExcludeFromAll(), fname.c_str(), name_space.c_str(), exportOld,
+      true));
 
   return true;
 #else
@@ -1405,12 +1406,12 @@ bool HandleExportMode(std::vector<std::string> const& args,
     cmInstallGenerator::SelectMessageLevel(helper.Makefile);
 
   // Create the export install generator.
-  cmInstallExportGenerator* exportGenerator = new cmInstallExportGenerator(
-    &exportSet, ica.GetDestination().c_str(), ica.GetPermissions().c_str(),
-    ica.GetConfigurations(), ica.GetComponent().c_str(), message,
-    ica.GetExcludeFromAll(), fname.c_str(), name_space.c_str(), exportOld,
-    false);
-  helper.Makefile->AddInstallGenerator(exportGenerator);
+  helper.Makefile->AddInstallGenerator(
+    cm::make_unique<cmInstallExportGenerator>(
+      &exportSet, ica.GetDestination().c_str(), ica.GetPermissions().c_str(),
+      ica.GetConfigurations(), ica.GetComponent().c_str(), message,
+      ica.GetExcludeFromAll(), fname.c_str(), name_space.c_str(), exportOld,
+      false));
 
   return true;
 }

+ 3 - 1
Source/cmInstallFilesCommand.cxx

@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmInstallFilesCommand.h"
 
+#include <cm/memory>
+
 #include "cmExecutionStatus.h"
 #include "cmGeneratorExpression.h"
 #include "cmGlobalGenerator.h"
@@ -121,7 +123,7 @@ static void CreateInstallGenerator(cmMakefile& makefile,
   std::vector<std::string> no_configurations;
   cmInstallGenerator::MessageLevel message =
     cmInstallGenerator::SelectMessageLevel(&makefile);
-  makefile.AddInstallGenerator(new cmInstallFilesGenerator(
+  makefile.AddInstallGenerator(cm::make_unique<cmInstallFilesGenerator>(
     files, destination.c_str(), false, no_permissions, no_configurations,
     no_component.c_str(), message, no_exclude_from_all, no_rename));
 }

+ 3 - 1
Source/cmInstallProgramsCommand.cxx

@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmInstallProgramsCommand.h"
 
+#include <cm/memory>
+
 #include "cmExecutionStatus.h"
 #include "cmGeneratorExpression.h"
 #include "cmGlobalGenerator.h"
@@ -95,7 +97,7 @@ static void FinalAction(cmMakefile& makefile, std::string const& dest,
   std::vector<std::string> no_configurations;
   cmInstallGenerator::MessageLevel message =
     cmInstallGenerator::SelectMessageLevel(&makefile);
-  makefile.AddInstallGenerator(new cmInstallFilesGenerator(
+  makefile.AddInstallGenerator(cm::make_unique<cmInstallFilesGenerator>(
     files, destination.c_str(), true, no_permissions, no_configurations,
     no_component.c_str(), message, no_exclude_from_all, no_rename));
 }

+ 2 - 1
Source/cmInstallSubdirectoryGenerator.cxx

@@ -2,6 +2,7 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmInstallSubdirectoryGenerator.h"
 
+#include <memory>
 #include <sstream>
 #include <vector>
 
@@ -24,7 +25,7 @@ cmInstallSubdirectoryGenerator::~cmInstallSubdirectoryGenerator() = default;
 
 bool cmInstallSubdirectoryGenerator::HaveInstall()
 {
-  for (auto generator : this->Makefile->GetInstallGenerators()) {
+  for (const auto& generator : this->Makefile->GetInstallGenerators()) {
     if (generator->HaveInstall()) {
       return true;
     }

+ 5 - 5
Source/cmJsonObjects.cxx

@@ -487,10 +487,10 @@ static Json::Value DumpTarget(cmGeneratorTarget* target,
     result[kHAS_INSTALL_RULE] = true;
 
     Json::Value installPaths = Json::arrayValue;
-    auto targetGenerators = target->Makefile->GetInstallGenerators();
-    for (auto installGenerator : targetGenerators) {
+    for (const auto& installGenerator :
+         target->Makefile->GetInstallGenerators()) {
       auto installTargetGenerator =
-        dynamic_cast<cmInstallTargetGenerator*>(installGenerator);
+        dynamic_cast<cmInstallTargetGenerator*>(installGenerator.get());
       if (installTargetGenerator != nullptr &&
           installTargetGenerator->GetTarget()->Target == target->Target) {
         auto dest = installTargetGenerator->GetDestination(config);
@@ -643,9 +643,9 @@ static Json::Value DumpProjectList(const cmake* cm, std::string const& config)
     // associated generators.
     bool hasInstallRule = false;
     for (const auto generator : projectIt.second) {
-      for (const auto installGen :
+      for (const auto& installGen :
            generator->GetMakefile()->GetInstallGenerators()) {
-        if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(installGen)) {
+        if (!dynamic_cast<cmInstallSubdirectoryGenerator*>(installGen.get())) {
           hasInstallRule = true;
           break;
         }

+ 6 - 12
Source/cmLocalGenerator.cxx

@@ -315,9 +315,7 @@ void cmLocalGenerator::GenerateTestFiles()
   }
 
   // Ask each test generator to write its code.
-  std::vector<cmTestGenerator*> const& testers =
-    this->Makefile->GetTestGenerators();
-  for (cmTestGenerator* tester : testers) {
+  for (const auto& tester : this->Makefile->GetTestGenerators()) {
     tester->Compute(this);
     tester->Generate(fout, config, configurationTypes);
   }
@@ -363,9 +361,7 @@ void cmLocalGenerator::CreateEvaluationFileOutputs()
 
 void cmLocalGenerator::CreateEvaluationFileOutputs(std::string const& config)
 {
-  std::vector<cmGeneratorExpressionEvaluationFile*> ef =
-    this->Makefile->GetEvaluationFiles();
-  for (cmGeneratorExpressionEvaluationFile* geef : ef) {
+  for (const auto& geef : this->Makefile->GetEvaluationFiles()) {
     geef->CreateOutputFile(this, config);
   }
 }
@@ -373,8 +369,7 @@ void cmLocalGenerator::CreateEvaluationFileOutputs(std::string const& config)
 void cmLocalGenerator::ProcessEvaluationFiles(
   std::vector<std::string>& generatedFiles)
 {
-  for (cmGeneratorExpressionEvaluationFile* geef :
-       this->Makefile->GetEvaluationFiles()) {
+  for (const auto& geef : this->Makefile->GetEvaluationFiles()) {
     geef->Generate(this);
     if (cmSystemTools::GetFatalErrorOccured()) {
       return;
@@ -557,18 +552,17 @@ void cmLocalGenerator::GenerateInstallRules()
 
   // Ask each install generator to write its code.
   cmPolicies::PolicyStatus status = this->GetPolicyStatus(cmPolicies::CMP0082);
-  std::vector<cmInstallGenerator*> const& installers =
-    this->Makefile->GetInstallGenerators();
+  auto const& installers = this->Makefile->GetInstallGenerators();
   bool haveSubdirectoryInstall = false;
   bool haveInstallAfterSubdirectory = false;
   if (status == cmPolicies::WARN) {
-    for (cmInstallGenerator* installer : installers) {
+    for (const auto& installer : installers) {
       installer->CheckCMP0082(haveSubdirectoryInstall,
                               haveInstallAfterSubdirectory);
       installer->Generate(fout, config, configurationTypes);
     }
   } else {
-    for (cmInstallGenerator* installer : installers) {
+    for (const auto& installer : installers) {
       installer->Generate(fout, config, configurationTypes);
     }
   }

+ 38 - 29
Source/cmMakefile.cxx

@@ -124,15 +124,7 @@ cmMakefile::cmMakefile(cmGlobalGenerator* globalGenerator,
 #endif
 }
 
-cmMakefile::~cmMakefile()
-{
-  cmDeleteAll(this->InstallGenerators);
-  cmDeleteAll(this->TestGenerators);
-  cmDeleteAll(this->SourceFiles);
-  cmDeleteAll(this->Tests);
-  cmDeleteAll(this->ImportedTargetsOwned);
-  cmDeleteAll(this->EvaluationFiles);
-}
+cmMakefile::~cmMakefile() = default;
 
 cmDirectoryId cmMakefile::GetDirectoryId() const
 {
@@ -774,12 +766,13 @@ void cmMakefile::AddEvaluationFile(
   std::unique_ptr<cmCompiledGeneratorExpression> condition,
   bool inputIsContent)
 {
-  this->EvaluationFiles.push_back(new cmGeneratorExpressionEvaluationFile(
-    inputFile, std::move(outputName), std::move(condition), inputIsContent,
-    this->GetPolicyStatus(cmPolicies::CMP0070)));
+  this->EvaluationFiles.push_back(
+    cm::make_unique<cmGeneratorExpressionEvaluationFile>(
+      inputFile, std::move(outputName), std::move(condition), inputIsContent,
+      this->GetPolicyStatus(cmPolicies::CMP0070)));
 }
 
-std::vector<cmGeneratorExpressionEvaluationFile*>
+const std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>&
 cmMakefile::GetEvaluationFiles() const
 {
   return this->EvaluationFiles;
@@ -1425,6 +1418,20 @@ void cmMakefile::InitializeFromParent(cmMakefile* parent)
   this->RecursionDepth = parent->RecursionDepth;
 }
 
+void cmMakefile::AddInstallGenerator(std::unique_ptr<cmInstallGenerator> g)
+{
+  if (g) {
+    this->InstallGenerators.push_back(std::move(g));
+  }
+}
+
+void cmMakefile::AddTestGenerator(std::unique_ptr<cmTestGenerator> g)
+{
+  if (g) {
+    this->TestGenerators.push_back(std::move(g));
+  }
+}
+
 void cmMakefile::PushFunctionScope(std::string const& fileName,
                                    const cmPolicies::PolicyMap& pm)
 {
@@ -1740,7 +1747,7 @@ void cmMakefile::AddSubDirectory(const std::string& srcPath,
     this->UnConfiguredDirectories.push_back(subMf);
   }
 
-  this->AddInstallGenerator(new cmInstallSubdirectoryGenerator(
+  this->AddInstallGenerator(cm::make_unique<cmInstallSubdirectoryGenerator>(
     subMf, binPath.c_str(), excludeFromAll));
 }
 
@@ -2104,18 +2111,18 @@ cmSourceFile* cmMakefile::LinearGetSourceFileWithOutput(
 
   // Look through all the source files that have custom commands and see if the
   // custom command has the passed source file as an output.
-  for (cmSourceFile* src : this->SourceFiles) {
+  for (const auto& src : this->SourceFiles) {
     // Does this source file have a custom command?
     if (src->GetCustomCommand()) {
       // Does the output of the custom command match the source file name?
       if (AnyOutputMatches(name, src->GetCustomCommand()->GetOutputs())) {
         // Return the first matching output.
-        return src;
+        return src.get();
       }
       if (kind == cmSourceOutputKind::OutputOrByproduct) {
         if (AnyOutputMatches(name, src->GetCustomCommand()->GetByproducts())) {
           // Do not return the source yet as there might be a matching output.
-          fallback = src;
+          fallback = src.get();
         }
       }
     }
@@ -3473,24 +3480,25 @@ cmSourceFile* cmMakefile::CreateSource(const std::string& sourceName,
                                        bool generated,
                                        cmSourceFileLocationKind kind)
 {
-  cmSourceFile* sf = new cmSourceFile(this, sourceName, kind);
+  auto sf = cm::make_unique<cmSourceFile>(this, sourceName, kind);
   if (generated) {
     sf->SetProperty("GENERATED", "1");
   }
-  this->SourceFiles.push_back(sf);
 
   auto name =
     this->GetCMakeInstance()->StripExtension(sf->GetLocation().GetName());
 #if defined(_WIN32) || defined(__APPLE__)
   name = cmSystemTools::LowerCase(name);
 #endif
-  this->SourceFileSearchIndex[name].push_back(sf);
+  this->SourceFileSearchIndex[name].push_back(sf.get());
   // for "Known" paths add direct lookup (used for faster lookup in GetSource)
   if (kind == cmSourceFileLocationKind::Known) {
-    this->KnownFileSearchIndex[sourceName] = sf;
+    this->KnownFileSearchIndex[sourceName] = sf.get();
   }
 
-  return sf;
+  this->SourceFiles.push_back(std::move(sf));
+
+  return this->SourceFiles.back().get();
 }
 
 cmSourceFile* cmMakefile::GetOrCreateSource(const std::string& sourceName,
@@ -4084,9 +4092,10 @@ cmTest* cmMakefile::CreateTest(const std::string& testName)
   if (test) {
     return test;
   }
-  test = new cmTest(this);
-  test->SetName(testName);
-  this->Tests[testName] = test;
+  auto newTest = cm::make_unique<cmTest>(this);
+  test = newTest.get();
+  newTest->SetName(testName);
+  this->Tests[testName] = std::move(newTest);
   return test;
 }
 
@@ -4094,7 +4103,7 @@ cmTest* cmMakefile::GetTest(const std::string& testName) const
 {
   auto mi = this->Tests.find(testName);
   if (mi != this->Tests.end()) {
-    return mi->second;
+    return mi->second.get();
   }
   return nullptr;
 }
@@ -4102,7 +4111,7 @@ cmTest* cmMakefile::GetTest(const std::string& testName) const
 void cmMakefile::GetTests(const std::string& config,
                           std::vector<cmTest*>& tests)
 {
-  for (auto generator : this->GetTestGenerators()) {
+  for (const auto& generator : this->GetTestGenerators()) {
     if (generator->TestsForConfig(config)) {
       tests.push_back(generator->GetTest());
     }
@@ -4215,8 +4224,8 @@ cmTarget* cmMakefile::AddImportedTarget(const std::string& name,
   this->GetGlobalGenerator()->IndexTarget(target.get());
 
   // Transfer ownership to this cmMakefile object.
-  this->ImportedTargetsOwned.push_back(target.get());
-  return target.release();
+  this->ImportedTargetsOwned.push_back(std::move(target));
+  return this->ImportedTargetsOwned.back().get();
 }
 
 cmTarget* cmMakefile::FindTargetToUse(const std::string& name,

+ 22 - 26
Source/cmMakefile.h

@@ -431,7 +431,7 @@ public:
   /** Get the target map - const version */
   cmTargetMap const& GetTargets() const { return this->Targets; }
 
-  const std::vector<cmTarget*>& GetOwnedImportedTargets() const
+  const std::vector<std::unique_ptr<cmTarget>>& GetOwnedImportedTargets() const
   {
     return this->ImportedTargetsOwned;
   }
@@ -727,7 +727,7 @@ public:
   /**
    * Get all the source files this makefile knows about
    */
-  const std::vector<cmSourceFile*>& GetSourceFiles() const
+  const std::vector<std::unique_ptr<cmSourceFile>>& GetSourceFiles() const
   {
     return this->SourceFiles;
   }
@@ -794,28 +794,22 @@ public:
   //! Initialize a makefile from its parent
   void InitializeFromParent(cmMakefile* parent);
 
-  void AddInstallGenerator(cmInstallGenerator* g)
-  {
-    if (g) {
-      this->InstallGenerators.push_back(g);
-    }
-  }
-  std::vector<cmInstallGenerator*>& GetInstallGenerators()
+  void AddInstallGenerator(std::unique_ptr<cmInstallGenerator> g);
+
+  std::vector<std::unique_ptr<cmInstallGenerator>>& GetInstallGenerators()
   {
     return this->InstallGenerators;
   }
-  const std::vector<cmInstallGenerator*>& GetInstallGenerators() const
+  const std::vector<std::unique_ptr<cmInstallGenerator>>&
+  GetInstallGenerators() const
   {
     return this->InstallGenerators;
   }
 
-  void AddTestGenerator(cmTestGenerator* g)
-  {
-    if (g) {
-      this->TestGenerators.push_back(g);
-    }
-  }
-  const std::vector<cmTestGenerator*>& GetTestGenerators() const
+  void AddTestGenerator(std::unique_ptr<cmTestGenerator> g);
+
+  const std::vector<std::unique_ptr<cmTestGenerator>>& GetTestGenerators()
+    const
   {
     return this->TestGenerators;
   }
@@ -948,7 +942,8 @@ public:
     std::unique_ptr<cmCompiledGeneratorExpression> outputName,
     std::unique_ptr<cmCompiledGeneratorExpression> condition,
     bool inputIsContent);
-  std::vector<cmGeneratorExpressionEvaluationFile*> GetEvaluationFiles() const;
+  const std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>&
+  GetEvaluationFiles() const;
 
   std::vector<cmExportBuildFileGenerator*> GetExportBuildFileGenerators()
     const;
@@ -983,8 +978,7 @@ protected:
   using TargetsVec = std::vector<cmTarget*>;
   TargetsVec OrderedTargets;
 
-  using SourceFileVec = std::vector<cmSourceFile*>;
-  SourceFileVec SourceFiles;
+  std::vector<std::unique_ptr<cmSourceFile>> SourceFiles;
 
   // Because cmSourceFile names are compared in a fuzzy way (see
   // cmSourceFileLocation::Match()) we can't have a straight mapping from
@@ -992,14 +986,15 @@ protected:
   // Name portion of the cmSourceFileLocation and then compare on the list of
   // cmSourceFiles that might match that name.  Note that on platforms which
   // have a case-insensitive filesystem we store the key in all lowercase.
-  using SourceFileMap = std::unordered_map<std::string, SourceFileVec>;
+  using SourceFileMap =
+    std::unordered_map<std::string, std::vector<cmSourceFile*>>;
   SourceFileMap SourceFileSearchIndex;
 
   // For "Known" paths we can store a direct filename to cmSourceFile map
   std::unordered_map<std::string, cmSourceFile*> KnownFileSearchIndex;
 
   // Tests
-  std::map<std::string, cmTest*> Tests;
+  std::map<std::string, std::unique_ptr<cmTest>> Tests;
 
   // The set of include directories that are marked as system include
   // directories.
@@ -1008,8 +1003,8 @@ protected:
   std::vector<std::string> ListFiles;
   std::vector<std::string> OutputFiles;
 
-  std::vector<cmInstallGenerator*> InstallGenerators;
-  std::vector<cmTestGenerator*> TestGenerators;
+  std::vector<std::unique_ptr<cmInstallGenerator>> InstallGenerators;
+  std::vector<std::unique_ptr<cmTestGenerator>> TestGenerators;
 
   std::string ComplainFileRegularExpression;
   std::string DefineFlags;
@@ -1060,13 +1055,14 @@ private:
   std::vector<cmMakefile*> UnConfiguredDirectories;
   std::vector<cmExportBuildFileGenerator*> ExportBuildFileGenerators;
 
-  std::vector<cmGeneratorExpressionEvaluationFile*> EvaluationFiles;
+  std::vector<std::unique_ptr<cmGeneratorExpressionEvaluationFile>>
+    EvaluationFiles;
 
   std::vector<cmExecutionStatus*> ExecutionStatusStack;
   friend class cmMakefileCall;
   friend class cmParseFileScope;
 
-  std::vector<cmTarget*> ImportedTargetsOwned;
+  std::vector<std::unique_ptr<cmTarget>> ImportedTargetsOwned;
   using TargetMap = std::unordered_map<std::string, cmTarget*>;
   TargetMap ImportedTargets;
 

+ 5 - 5
Source/cmQtAutoGenInitializer.cxx

@@ -883,7 +883,7 @@ bool cmQtAutoGenInitializer::InitScanFiles()
   // The reason is that their file names might be discovered from source files
   // at generation time.
   if (this->MocOrUicEnabled()) {
-    for (cmSourceFile* sf : this->Makefile->GetSourceFiles()) {
+    for (const auto& sf : this->Makefile->GetSourceFiles()) {
       // sf->GetExtension() is only valid after sf->ResolveFullPath() ...
       // Since we're iterating over source files that might be not in the
       // target we need to check for path errors (not existing files).
@@ -896,15 +896,15 @@ bool cmQtAutoGenInitializer::InitScanFiles()
         cmSystemTools::LowerCase(sf->GetExtension());
 
       if (cm->IsHeaderExtension(extLower)) {
-        if (!cmContains(this->AutogenTarget.Headers, sf)) {
-          auto muf = makeMUFile(sf, fullPath, false);
+        if (!cmContains(this->AutogenTarget.Headers, sf.get())) {
+          auto muf = makeMUFile(sf.get(), fullPath, false);
           if (muf->SkipMoc || muf->SkipUic) {
             addMUHeader(std::move(muf), extLower);
           }
         }
       } else if (cm->IsSourceExtension(extLower)) {
-        if (!cmContains(this->AutogenTarget.Sources, sf)) {
-          auto muf = makeMUFile(sf, fullPath, false);
+        if (!cmContains(this->AutogenTarget.Sources, sf.get())) {
+          auto muf = makeMUFile(sf.get(), fullPath, false);
           if (muf->SkipMoc || muf->SkipUic) {
             addMUSource(std::move(muf));
           }

+ 6 - 2
Source/cmTarget.cxx

@@ -1562,8 +1562,12 @@ static void cmTargetCheckINTERFACE_LINK_LIBRARIES(const char* value,
 static void cmTargetCheckIMPORTED_GLOBAL(const cmTarget* target,
                                          cmMakefile* context)
 {
-  std::vector<cmTarget*> targets = context->GetOwnedImportedTargets();
-  auto it = std::find(targets.begin(), targets.end(), target);
+  const auto& targets = context->GetOwnedImportedTargets();
+  auto it =
+    std::find_if(targets.begin(), targets.end(),
+                 [&](const std::unique_ptr<cmTarget>& importTarget) -> bool {
+                   return target == importTarget.get();
+                 });
   if (it == targets.end()) {
     std::ostringstream e;
     e << "Attempt to promote imported target \"" << target->GetName()

+ 3 - 4
Source/cmTargetLinkLibrariesCommand.cxx

@@ -3,6 +3,7 @@
 #include "cmTargetLinkLibrariesCommand.h"
 
 #include <cstring>
+#include <memory>
 #include <sstream>
 
 #include "cmExecutionStatus.h"
@@ -64,11 +65,9 @@ bool cmTargetLinkLibrariesCommand(std::vector<std::string> const& args,
   cmTarget* target =
     mf.GetCMakeInstance()->GetGlobalGenerator()->FindTarget(args[0]);
   if (!target) {
-    const std::vector<cmTarget*>& importedTargets =
-      mf.GetOwnedImportedTargets();
-    for (cmTarget* importedTarget : importedTargets) {
+    for (const auto& importedTarget : mf.GetOwnedImportedTargets()) {
       if (importedTarget->GetName() == args[0]) {
-        target = importedTarget;
+        target = importedTarget.get();
         break;
       }
     }