[cfe-commits] r104191 - in /cfe/trunk: include/clang/AST/DeclTemplate.h lib/AST/DeclTemplate.cpp

Chris Lattner sabre at nondot.org
Wed May 19 17:11:47 PDT 2010


Author: lattner
Date: Wed May 19 19:11:47 2010
New Revision: 104191

URL: http://llvm.org/viewvc/llvm-project?rev=104191&view=rev
Log:
Clarify TemplateArgumentList ownership over its "flat" and 
"structure" arg lists, the first step to fixing some massive
memory leaks.

Modified:
    cfe/trunk/include/clang/AST/DeclTemplate.h
    cfe/trunk/lib/AST/DeclTemplate.cpp

Modified: cfe/trunk/include/clang/AST/DeclTemplate.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclTemplate.h?rev=104191&r1=104190&r2=104191&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclTemplate.h (original)
+++ cfe/trunk/include/clang/AST/DeclTemplate.h Wed May 19 19:11:47 2010
@@ -131,8 +131,6 @@
   void BeginPack();
   void EndPack();
 
-  void ReleaseArgs();
-
   unsigned flatSize() const {
     return NumFlatArgs;
   }
@@ -165,7 +163,7 @@
   /// \brief The template argument list.
   ///
   /// The integer value will be non-zero to indicate that this
-  /// template argument list does not own the pointer.
+  /// template argument list does own the pointer.
   llvm::PointerIntPair<const TemplateArgument *, 1> FlatArguments;
 
   /// \brief The number of template arguments in this template
@@ -176,6 +174,10 @@
   unsigned NumStructuredArguments;
 
 public:
+  /// TemplateArgumentList - If this constructor is passed "true" for 'TakeArgs'
+  /// it copies them into a locally new[]'d array.  If passed "false", then it
+  /// just references the array passed in.  This is only safe if the builder
+  /// outlives it, but saves a copy.
   TemplateArgumentList(ASTContext &Context,
                        TemplateArgumentListBuilder &Builder,
                        bool TakeArgs);

Modified: cfe/trunk/lib/AST/DeclTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclTemplate.cpp?rev=104191&r1=104190&r2=104191&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclTemplate.cpp (original)
+++ cfe/trunk/lib/AST/DeclTemplate.cpp Wed May 19 19:11:47 2010
@@ -353,15 +353,6 @@
                                                       /*CopyArgs=*/false);
 }
 
-void TemplateArgumentListBuilder::ReleaseArgs() {
-  FlatArgs = 0;
-  NumFlatArgs = 0;
-  MaxFlatArgs = 0;
-  StructuredArgs = 0;
-  NumStructuredArgs = 0;
-  MaxStructuredArgs = 0;
-}
-
 //===----------------------------------------------------------------------===//
 // TemplateArgumentList Implementation
 //===----------------------------------------------------------------------===//
@@ -376,9 +367,23 @@
   if (!TakeArgs)
     return;
 
-  if (Builder.getStructuredArguments() == Builder.getFlatArguments())
+  // If this does take ownership of the arguments, then we have to new them
+  // and copy over.
+  TemplateArgument *NewArgs = new TemplateArgument[Builder.flatSize()];
+  std::copy(Builder.getFlatArguments(),
+            Builder.getFlatArguments()+Builder.flatSize(), NewArgs);
+  FlatArguments.setPointer(NewArgs);
+      
+  // Just reuse the structured and flat arguments array if possible.
+  if (Builder.getStructuredArguments() == Builder.getFlatArguments()) {
+    StructuredArguments.setPointer(NewArgs);    
     StructuredArguments.setInt(0);
-  Builder.ReleaseArgs();
+  } else {
+    TemplateArgument *NewSArgs = new TemplateArgument[Builder.flatSize()];
+    std::copy(Builder.getFlatArguments(),
+              Builder.getFlatArguments()+Builder.flatSize(), NewSArgs);
+    StructuredArguments.setPointer(NewSArgs);
+  }
 }
 
 TemplateArgumentList::TemplateArgumentList(const TemplateArgumentList &Other)
@@ -388,7 +393,10 @@
     NumStructuredArguments(Other.NumStructuredArguments) { }
 
 TemplateArgumentList::~TemplateArgumentList() {
-  // FIXME: Deallocate template arguments
+  if (FlatArguments.getInt())
+    delete [] FlatArguments.getPointer();
+  if (StructuredArguments.getInt())
+    delete [] StructuredArguments.getPointer();
 }
 
 //===----------------------------------------------------------------------===//





More information about the cfe-commits mailing list