From 7c976c508249308f5aac97dc03e743ffdc7f67e9 Mon Sep 17 00:00:00 2001
From: Frederich Munch <marsupial@users.noreply.github.com>
Date: Wed, 12 Jul 2017 14:40:51 -0400
Subject: [PATCH] Hide members of CXAAtExitElement and make it a callable
 object. Update iteration to use llvm::reverse and C++11 range based for loop.

---
 .../lib/Interpreter/IncrementalExecutor.cpp   | 15 ++++------
 .../lib/Interpreter/IncrementalExecutor.h     | 29 ++++++++++---------
 2 files changed, 21 insertions(+), 23 deletions(-)

diff --git a/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp b/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp
index bca74595474..f195f2f028b 100644
--- a/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp
+++ b/interpreter/cling/lib/Interpreter/IncrementalExecutor.cpp
@@ -110,10 +110,8 @@ IncrementalExecutor::~IncrementalExecutor() {}
 void IncrementalExecutor::shuttingDown() {
   // No need to protect this access, since hopefully there is no concurrent
   // shutdown request.
-  for (size_t I = 0, N = m_AtExitFuncs.size(); I < N; ++I) {
-    const CXAAtExitElement& AEE = m_AtExitFuncs[N - I - 1];
-    (*AEE.m_Func)(AEE.m_Arg);
-  }
+  for (auto& AtExit : llvm::reverse(m_AtExitFuncs))
+    AtExit();
 }
 
 void IncrementalExecutor::AddAtExitFunc(void (*func) (void*), void* arg,
@@ -294,7 +292,7 @@ void IncrementalExecutor::runAndRemoveStaticDestructors(Transaction* T) {
     cling::internal::SpinLockGuard slg(m_AtExitFuncsSpinLock);
     for (AtExitFunctions::iterator I = m_AtExitFuncs.begin();
          I != m_AtExitFuncs.end();)
-      if (I->m_FromM == T->getModule()) {
+      if (I->getModule() == T->getModule()) {
         boundToT.push_back(*I);
         I = m_AtExitFuncs.erase(I);
       }
@@ -303,11 +301,8 @@ void IncrementalExecutor::runAndRemoveStaticDestructors(Transaction* T) {
   } // end of spin lock lifetime block.
 
   // 'Unload' the cxa_atexit entities.
-  for (AtExitFunctions::reverse_iterator I = boundToT.rbegin(),
-         E = boundToT.rend(); I != E; ++I) {
-    const CXAAtExitElement& AEE = *I;
-    (*AEE.m_Func)(AEE.m_Arg);
-  }
+  for (auto&& AtExit : llvm::reverse(boundToT))
+    AtExit();
 }
 
 void
diff --git a/interpreter/cling/lib/Interpreter/IncrementalExecutor.h b/interpreter/cling/lib/Interpreter/IncrementalExecutor.h
index b30a7ef47ef..8742e9c44b1 100644
--- a/interpreter/cling/lib/Interpreter/IncrementalExecutor.h
+++ b/interpreter/cling/lib/Interpreter/IncrementalExecutor.h
@@ -64,7 +64,20 @@ namespace cling {
     /// The object is registered first as an CXAAtExitElement and then cling
     /// takes the control of it's destruction.
     ///
-    struct CXAAtExitElement {
+    class CXAAtExitElement {
+      ///\brief The function to be called.
+      ///
+      void (*m_Func)(void*);
+
+      ///\brief The single argument passed to the function.
+      ///
+      void* m_Arg;
+
+      ///\brief The module whose unloading will trigger the call to this atexit
+      /// function.
+      ///
+      const llvm::Module* m_FromM;
+    public:
       ///\brief Constructs an element, whose destruction time will be managed by
       /// the interpreter. (By registering a function to be called by exit
       /// or when a shared library is unloaded.)
@@ -87,18 +100,8 @@ namespace cling {
                        const llvm::Module* fromM):
         m_Func(func), m_Arg(arg), m_FromM(fromM) {}
 
-      ///\brief The function to be called.
-      ///
-      void (*m_Func)(void*);
-
-      ///\brief The single argument passed to the function.
-      ///
-      void* m_Arg;
-
-      ///\brief The module whose unloading will trigger the call to this atexit
-      /// function.
-      ///
-      const llvm::Module* m_FromM;
+      void operator () () const { (*m_Func)(m_Arg); }
+      const llvm::Module* getModule() const { return m_FromM; }
     };
 
     ///\brief Atomic used as a spin lock to protect the access to m_AtExitFuncs
-- 
GitLab