[clang] [clang][Driver] Use shared_ptr in the Compilation class (PR #116406)

Victor Campos via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 15 08:27:21 PST 2024


https://github.com/vhscampos created https://github.com/llvm/llvm-project/pull/116406

This patch replaces uses of raw pointers by shared_ptrs in the Driver's Compilation class.

The manual memory management which was done before this patch could be error prone. Plus, code is now simpler.

>From 6291206f06c524951ba0d9f444cc75293de41015 Mon Sep 17 00:00:00 2001
From: Victor Campos <victor.campos at arm.com>
Date: Fri, 15 Nov 2024 16:15:04 +0000
Subject: [PATCH] [clang][Driver] Use shared_ptr in the Compilation class

This patch replaces uses of raw pointers by shared_ptrs in the Driver's
Compilation class.

The manual memory management which was done before this patch could be
error prone. Plus, code is now simpler.
---
 clang/include/clang/Driver/Compilation.h |  6 ++--
 clang/lib/Driver/Compilation.cpp         | 41 ++++++++----------------
 2 files changed, 17 insertions(+), 30 deletions(-)

diff --git a/clang/include/clang/Driver/Compilation.h b/clang/include/clang/Driver/Compilation.h
index 36ae85c4245143..cceb0c8a4466ba 100644
--- a/clang/include/clang/Driver/Compilation.h
+++ b/clang/include/clang/Driver/Compilation.h
@@ -61,11 +61,11 @@ class Compilation {
       OrderedOffloadingToolchains;
 
   /// The original (untranslated) input argument list.
-  llvm::opt::InputArgList *Args;
+  std::shared_ptr<llvm::opt::InputArgList> Args;
 
   /// The driver translated arguments. Note that toolchains may perform their
   /// own argument translation.
-  llvm::opt::DerivedArgList *TranslatedArgs;
+  std::shared_ptr<llvm::opt::DerivedArgList> TranslatedArgs;
 
   /// The list of actions we've created via MakeAction.  This is not accessible
   /// to consumers; it's here just to manage ownership.
@@ -100,7 +100,7 @@ class Compilation {
       return false;
     }
   };
-  std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
+  std::map<TCArgsKey, std::shared_ptr<llvm::opt::DerivedArgList>> TCArgs;
 
   /// Temporary files which should be removed on exit.
   llvm::opt::ArgStringList TempFiles;
diff --git a/clang/lib/Driver/Compilation.cpp b/clang/lib/Driver/Compilation.cpp
index ad077d5bbfa69a..0fd60f9e80bd37 100644
--- a/clang/lib/Driver/Compilation.cpp
+++ b/clang/lib/Driver/Compilation.cpp
@@ -47,14 +47,6 @@ Compilation::~Compilation() {
   // the file names might be derived from the input arguments.
   if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
     CleanupFileList(TempFiles);
-
-  delete TranslatedArgs;
-  delete Args;
-
-  // Free any derived arg lists.
-  for (auto Arg : TCArgs)
-    if (Arg.second != TranslatedArgs)
-      delete Arg.second;
 }
 
 const DerivedArgList &
@@ -63,41 +55,39 @@ Compilation::getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
   if (!TC)
     TC = &DefaultToolChain;
 
-  DerivedArgList *&Entry = TCArgs[{TC, BoundArch, DeviceOffloadKind}];
+  std::shared_ptr<DerivedArgList> &Entry =
+      TCArgs[{TC, BoundArch, DeviceOffloadKind}];
   if (!Entry) {
     SmallVector<Arg *, 4> AllocatedArgs;
-    DerivedArgList *OpenMPArgs = nullptr;
+    std::shared_ptr<DerivedArgList> OpenMPArgs;
     // Translate OpenMP toolchain arguments provided via the -Xopenmp-target flags.
     if (DeviceOffloadKind == Action::OFK_OpenMP) {
       const ToolChain *HostTC = getSingleOffloadToolChain<Action::OFK_Host>();
       bool SameTripleAsHost = (TC->getTriple() == HostTC->getTriple());
-      OpenMPArgs = TC->TranslateOpenMPTargetArgs(
-          *TranslatedArgs, SameTripleAsHost, AllocatedArgs);
+      OpenMPArgs.reset(TC->TranslateOpenMPTargetArgs(
+          *TranslatedArgs, SameTripleAsHost, AllocatedArgs));
     }
 
-    DerivedArgList *NewDAL = nullptr;
+    std::shared_ptr<DerivedArgList> NewDAL;
     if (!OpenMPArgs) {
-      NewDAL = TC->TranslateXarchArgs(*TranslatedArgs, BoundArch,
-                                      DeviceOffloadKind, &AllocatedArgs);
+      NewDAL.reset(TC->TranslateXarchArgs(*TranslatedArgs, BoundArch,
+                                          DeviceOffloadKind, &AllocatedArgs));
     } else {
-      NewDAL = TC->TranslateXarchArgs(*OpenMPArgs, BoundArch, DeviceOffloadKind,
-                                      &AllocatedArgs);
+      NewDAL.reset(TC->TranslateXarchArgs(*OpenMPArgs, BoundArch,
+                                          DeviceOffloadKind, &AllocatedArgs));
       if (!NewDAL)
         NewDAL = OpenMPArgs;
-      else
-        delete OpenMPArgs;
     }
 
     if (!NewDAL) {
-      Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch, DeviceOffloadKind);
+      Entry.reset(
+          TC->TranslateArgs(*TranslatedArgs, BoundArch, DeviceOffloadKind));
       if (!Entry)
         Entry = TranslatedArgs;
     } else {
-      Entry = TC->TranslateArgs(*NewDAL, BoundArch, DeviceOffloadKind);
+      Entry.reset(TC->TranslateArgs(*NewDAL, BoundArch, DeviceOffloadKind));
       if (!Entry)
-        Entry = NewDAL;
-      else
-        delete NewDAL;
+        Entry = std::shared_ptr<DerivedArgList>(NewDAL);
     }
 
     // Add allocated arguments to the final DAL.
@@ -290,9 +280,6 @@ void Compilation::initCompilationForDiagnostics() {
 
   // Force re-creation of the toolchain Args, otherwise our modifications just
   // above will have no effect.
-  for (auto Arg : TCArgs)
-    if (Arg.second != TranslatedArgs)
-      delete Arg.second;
   TCArgs.clear();
 
   // Redirect stdout/stderr to /dev/null.



More information about the cfe-commits mailing list