Introduce ASTUnresolvedSet, an UnresolvedSet-like class, whose contents are
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 28 Nov 2012 03:56:16 +0000 (03:56 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 28 Nov 2012 03:56:16 +0000 (03:56 +0000)
allocated using the allocator associated with an ASTContext.

Use this inside CXXRecordDecl::DefinitionData instead of an UnresolvedSet to
avoid a potential memory leak.

rdar://12761275

llvm-svn: 168771

clang/include/clang/AST/ASTUnresolvedSet.h [new file with mode: 0644]
clang/include/clang/AST/ASTVector.h
clang/include/clang/AST/DeclCXX.h
clang/include/clang/AST/Expr.h
clang/include/clang/AST/UnresolvedSet.h
clang/include/clang/Serialization/ASTReader.h
clang/include/clang/Serialization/ASTWriter.h
clang/lib/AST/DeclCXX.cpp
clang/lib/Serialization/ASTReader.cpp
clang/lib/Serialization/ASTReaderStmt.cpp
clang/lib/Serialization/ASTWriter.cpp

diff --git a/clang/include/clang/AST/ASTUnresolvedSet.h b/clang/include/clang/AST/ASTUnresolvedSet.h
new file mode 100644 (file)
index 0000000..26c5406
--- /dev/null
@@ -0,0 +1,86 @@
+//===-- ASTUnresolvedSet.h - Unresolved sets of declarations  ---*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file provides an UnresolvedSet-like class, whose contents are
+//  allocated using the allocator associated with an ASTContext.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
+#define LLVM_CLANG_AST_ASTUNRESOLVEDSET_H
+
+#include "clang/AST/UnresolvedSet.h"
+#include "clang/AST/ASTVector.h"
+
+namespace clang {
+
+/// \brief An UnresolvedSet-like class which uses the ASTContext's allocator.
+class ASTUnresolvedSet {
+  typedef ASTVector<DeclAccessPair> DeclsTy;
+  DeclsTy Decls;
+
+  ASTUnresolvedSet(const ASTUnresolvedSet &) LLVM_DELETED_FUNCTION;
+  void operator=(const ASTUnresolvedSet &) LLVM_DELETED_FUNCTION;
+
+public:
+  ASTUnresolvedSet() {}
+  ASTUnresolvedSet(ASTContext &C, unsigned N) : Decls(C, N) {}
+
+  typedef UnresolvedSetIterator iterator;
+  typedef UnresolvedSetIterator const_iterator;
+
+  iterator begin() { return iterator(Decls.begin()); }
+  iterator end() { return iterator(Decls.end()); }
+
+  const_iterator begin() const { return const_iterator(Decls.begin()); }
+  const_iterator end() const { return const_iterator(Decls.end()); }
+
+  void addDecl(ASTContext &C, NamedDecl *D) {
+    addDecl(C, D, AS_none);
+  }
+
+  void addDecl(ASTContext &C, NamedDecl *D, AccessSpecifier AS) {
+    Decls.push_back(DeclAccessPair::make(D, AS), C);
+  }
+
+  /// Replaces the given declaration with the new one, once.
+  ///
+  /// \return true if the set changed
+  bool replace(const NamedDecl* Old, NamedDecl *New) {
+    for (DeclsTy::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I)
+      if (I->getDecl() == Old)
+        return (I->setDecl(New), true);
+    return false;
+  }
+
+  void erase(unsigned I) {
+    Decls[I] = Decls.back();
+    Decls.pop_back();
+  }
+
+  void clear() { Decls.clear(); }
+
+  bool empty() const { return Decls.empty(); }
+  unsigned size() const { return Decls.size(); }
+
+  void reserve(ASTContext &C, unsigned N) {
+    Decls.reserve(C, N);
+  }
+
+  void append(ASTContext &C, iterator I, iterator E) {
+    Decls.append(C, I.ir, E.ir);
+  }
+
+  DeclAccessPair &operator[](unsigned I) { return Decls[I]; }
+  const DeclAccessPair &operator[](unsigned I) const { return Decls[I]; }
+};
+  
+} // namespace clang
+
+#endif
index 4ff5ea3..9196652 100644 (file)
@@ -59,7 +59,9 @@ class ASTVector {
 
 public:
   // Default ctor - Initialize to empty.
-  explicit ASTVector(ASTContext &C, unsigned N = 0)
+  ASTVector() : Begin(NULL), End(NULL), Capacity(NULL) { }
+
+  ASTVector(ASTContext &C, unsigned N)
   : Begin(NULL), End(NULL), Capacity(NULL) {
     reserve(C, N);
   }
index 6abc82d..7e89e1e 100644 (file)
@@ -19,7 +19,7 @@
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/TypeLoc.h"
-#include "clang/AST/UnresolvedSet.h"
+#include "clang/AST/ASTUnresolvedSet.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/SmallPtrSet.h"
@@ -522,14 +522,14 @@ class CXXRecordDecl : public RecordDecl {
     /// of this C++ class (but not its inherited conversion
     /// functions). Each of the entries in this overload set is a
     /// CXXConversionDecl.
-    UnresolvedSet<4> Conversions;
+    ASTUnresolvedSet Conversions;
 
     /// VisibleConversions - Overload set containing the conversion
     /// functions of this C++ class and all those inherited conversion
     /// functions that are visible in this class. Each of the entries
     /// in this overload set is a CXXConversionDecl or a
     /// FunctionTemplateDecl.
-    UnresolvedSet<4> VisibleConversions;
+    ASTUnresolvedSet VisibleConversions;
 
     /// Definition - The declaration which defines this record.
     CXXRecordDecl *Definition;
index dc83654..ac35bf8 100644 (file)
@@ -3580,8 +3580,8 @@ public:
                ArrayRef<Expr*> initExprs, SourceLocation rbraceloc);
 
   /// \brief Build an empty initializer list.
-  explicit InitListExpr(ASTContext &C, EmptyShell Empty)
-    : Expr(InitListExprClass, Empty), InitExprs(C) { }
+  explicit InitListExpr(EmptyShell Empty)
+    : Expr(InitListExprClass, Empty) { }
 
   unsigned getNumInits() const { return InitExprs.size(); }
 
index 9f11ee5..de96601 100644 (file)
@@ -25,12 +25,13 @@ namespace clang {
 /// non-const iterator.
 class UnresolvedSetIterator {
 private:
-  typedef SmallVectorImpl<DeclAccessPair> DeclsTy;
+  typedef llvm::MutableArrayRef<DeclAccessPair> DeclsTy;
   typedef DeclsTy::iterator IteratorTy;
 
   IteratorTy ir;
 
   friend class UnresolvedSetImpl;
+  friend class ASTUnresolvedSet;
   friend class OverloadExpr;
   explicit UnresolvedSetIterator(DeclsTy::iterator ir) : ir(ir) {}
   explicit UnresolvedSetIterator(DeclsTy::const_iterator ir) :
@@ -87,7 +88,7 @@ public:
 
 /// UnresolvedSet - A set of unresolved declarations.
 class UnresolvedSetImpl {
-  typedef UnresolvedSetIterator::DeclsTy DeclsTy;
+  typedef SmallVectorImpl<DeclAccessPair> DeclsTy;
 
   // Don't allow direct construction, and only permit subclassing by
   // UnresolvedSet.
index 14550ba..6b3c4f1 100644 (file)
@@ -85,6 +85,7 @@ class TypeLocReader;
 struct HeaderFileInfo;
 class VersionTuple;
 class TargetOptions;
+class ASTUnresolvedSet;
 
 /// \brief Abstract interface for callback invocations by the ASTReader.
 ///
@@ -1612,7 +1613,7 @@ public:
                            unsigned &Idx);
 
   /// \brief Read a UnresolvedSet structure.
-  void ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
+  void ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
                          const RecordData &Record, unsigned &Idx);
 
   /// \brief Read a C++ base specifier.
index ac81e21..19636bf 100644 (file)
@@ -63,6 +63,7 @@ class SourceManager;
 class SwitchCase;
 class TargetInfo;
 class VersionTuple;
+class ASTUnresolvedSet;
 
 namespace SrcMgr { class SLocEntry; }
 
@@ -603,7 +604,7 @@ public:
                                 RecordDataImpl &Record);
 
   /// \brief Emit a UnresolvedSet structure.
-  void AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record);
+  void AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record);
 
   /// \brief Emit a C++ base specifier.
   void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
index bf6107f..d661aa0 100644 (file)
@@ -709,13 +709,13 @@ NotASpecialMember:;
           data().Conversions.replace(FunTmpl->getPreviousDecl(),
                                      FunTmpl);
         else
-          data().Conversions.addDecl(FunTmpl);
+          data().Conversions.addDecl(getASTContext(), FunTmpl);
       } else {
         if (Conversion->getPreviousDecl())
           data().Conversions.replace(Conversion->getPreviousDecl(),
                                      Conversion);
         else
-          data().Conversions.addDecl(Conversion);        
+          data().Conversions.addDecl(getASTContext(), Conversion);
       }
     }
     
@@ -935,7 +935,7 @@ NotASpecialMember:;
   if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D))
     if (Shadow->getDeclName().getNameKind()
           == DeclarationName::CXXConversionFunctionName)
-      data().Conversions.addDecl(Shadow, Shadow->getAccess());
+      data().Conversions.addDecl(getASTContext(), Shadow, Shadow->getAccess());
 }
 
 bool CXXRecordDecl::isCLike() const {
@@ -996,7 +996,7 @@ static void CollectVisibleConversions(ASTContext &Context,
                                       bool InVirtual,
                                       AccessSpecifier Access,
                   const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes,
-                                      UnresolvedSetImpl &Output,
+                                      ASTUnresolvedSet &Output,
                                       UnresolvedSetImpl &VOutput,
                            llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) {
   // The set of types which have conversions in this class or its
@@ -1032,7 +1032,7 @@ static void CollectVisibleConversions(ASTContext &Context,
         if (InVirtual)
           VOutput.addDecl(I.getDecl(), IAccess);
         else
-          Output.addDecl(I.getDecl(), IAccess);
+          Output.addDecl(Context, I.getDecl(), IAccess);
       }
     }
   }
@@ -1059,7 +1059,7 @@ static void CollectVisibleConversions(ASTContext &Context,
 /// bases.  It might be worth special-casing that, really.
 static void CollectVisibleConversions(ASTContext &Context,
                                       CXXRecordDecl *Record,
-                                      UnresolvedSetImpl &Output) {
+                                      ASTUnresolvedSet &Output) {
   // The collection of all conversions in virtual bases that we've
   // found.  These will be added to the output as long as they don't
   // appear in the hidden-conversions set.
@@ -1076,7 +1076,7 @@ static void CollectVisibleConversions(ASTContext &Context,
   // hidden-types set.
   CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin();
   CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end();
-  Output.append(ConvI, ConvE);
+  Output.append(Context, ConvI, ConvE);
   for (; ConvI != ConvE; ++ConvI)
     HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl()));
 
@@ -1095,7 +1095,7 @@ static void CollectVisibleConversions(ASTContext &Context,
   for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end();
          I != E; ++I) {
     if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())))
-      Output.addDecl(I.getDecl(), I.getAccess());
+      Output.addDecl(Context, I.getDecl(), I.getAccess());
   }
 }
 
@@ -1127,7 +1127,7 @@ void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) {
   // with sufficiently large numbers of directly-declared conversions
   // that asymptotic behavior matters.
 
-  UnresolvedSetImpl &Convs = data().Conversions;
+  ASTUnresolvedSet &Convs = data().Conversions;
   for (unsigned I = 0, E = Convs.size(); I != E; ++I) {
     if (Convs[I].getDecl() == ConvDecl) {
       Convs.erase(I);
@@ -1266,7 +1266,7 @@ void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) {
   for (UnresolvedSetIterator I = data().Conversions.begin(), 
                              E = data().Conversions.end(); 
        I != E; ++I)
-    data().Conversions.setAccess(I, (*I)->getAccess());
+    I.setAccess((*I)->getAccess());
 }
 
 bool CXXRecordDecl::mayBeAbstract() const {
index 6e82cd9..ffed835 100644 (file)
@@ -6448,13 +6448,14 @@ ReadTemplateArgumentList(SmallVector<TemplateArgument, 8> &TemplArgs,
 }
 
 /// \brief Read a UnresolvedSet structure.
-void ASTReader::ReadUnresolvedSet(ModuleFile &F, UnresolvedSetImpl &Set,
+void ASTReader::ReadUnresolvedSet(ModuleFile &F, ASTUnresolvedSet &Set,
                                   const RecordData &Record, unsigned &Idx) {
   unsigned NumDecls = Record[Idx++];
+  Set.reserve(Context, NumDecls);
   while (NumDecls--) {
     NamedDecl *D = ReadDeclAs<NamedDecl>(F, Record, Idx);
     AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
-    Set.addDecl(D, AS);
+    Set.addDecl(Context, D, AS);
   }
 }
 
index 367f75f..529cfea 100644 (file)
@@ -1868,7 +1868,7 @@ Stmt *ASTReader::ReadStmtFromStream(ModuleFile &F) {
       break;
 
     case EXPR_INIT_LIST:
-      S = new (Context) InitListExpr(getContext(), Empty);
+      S = new (Context) InitListExpr(Empty);
       break;
 
     case EXPR_DESIGNATED_INIT:
index 744ae9f..41665d8 100644 (file)
@@ -4480,9 +4480,9 @@ ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
 
 
 void
-ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
+ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
   Record.push_back(Set.size());
-  for (UnresolvedSetImpl::const_iterator
+  for (ASTUnresolvedSet::const_iterator
          I = Set.begin(), E = Set.end(); I != E; ++I) {
     AddDeclRef(I.getDecl(), Record);
     Record.push_back(I.getAccess());