Allocate the 'Protocols' array in ObjCInterfaceType and
ObjCObjectPointerType using the allocator associated with ASTContext.
Not only does this fix a memory leak, but it also makes these arrays
BumpPtrAllocated (in the typical case).


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94090 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp
index e0055f1..edfb580 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -339,6 +339,25 @@
   return 0;
 }
 
+ObjCInterfaceType::ObjCInterfaceType(ASTContext &Ctx, QualType Canonical,
+                                     ObjCInterfaceDecl *D,
+                                     ObjCProtocolDecl **Protos, unsigned NumP) :
+  Type(ObjCInterface, Canonical, /*Dependent=*/false),
+  Decl(D), Protocols(0), NumProtocols(NumP)
+{
+  if (NumProtocols) {
+    Protocols = new (Ctx) ObjCProtocolDecl*[NumProtocols];
+    memcpy(Protocols, Protos, NumProtocols * sizeof(*Protocols));
+  }
+}
+
+void ObjCInterfaceType::Destroy(ASTContext& C) {
+  if (Protocols)
+    C.Deallocate(Protocols);
+  this->~ObjCInterfaceType();
+  C.Deallocate(this);
+}
+
 const ObjCInterfaceType *Type::getAsObjCQualifiedInterfaceType() const {
   // There is no sugar for ObjCInterfaceType's, just return the canonical
   // type pointer if it is the right class.  There is no typedef information to
@@ -353,6 +372,26 @@
   return getAsObjCQualifiedInterfaceType() != 0;
 }
 
+ObjCObjectPointerType::ObjCObjectPointerType(ASTContext &Ctx,
+                                             QualType Canonical, QualType T,
+                                             ObjCProtocolDecl **Protos,
+                                             unsigned NumP) :
+  Type(ObjCObjectPointer, Canonical, /*Dependent=*/false),
+  PointeeType(T), Protocols(NULL), NumProtocols(NumP)
+{
+  if (NumProtocols) {
+    Protocols = new (Ctx) ObjCProtocolDecl*[NumProtocols];
+    memcpy(Protocols, Protos, NumProtocols * sizeof(*Protocols));
+  }
+}
+
+void ObjCObjectPointerType::Destroy(ASTContext& C) {
+  if (Protocols)
+    C.Deallocate(Protocols);
+  this->~ObjCObjectPointerType();
+  C.Deallocate(this);
+}
+
 const ObjCObjectPointerType *Type::getAsObjCQualifiedIdType() const {
   // There is no sugar for ObjCQualifiedIdType's, just return the canonical
   // type pointer if it is the right class.