Don't kill the declarations if the translation unit doesn't own them 
(specifically, for TranslationUnits created from SerializationTest.cpp).  
Fixes a double-free bug in the serialization tests.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51362 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/TranslationUnit.cpp b/lib/AST/TranslationUnit.cpp
index 9d77586..bfe6ee2 100644
--- a/lib/AST/TranslationUnit.cpp
+++ b/lib/AST/TranslationUnit.cpp
@@ -31,21 +31,18 @@
        DeclsBlock = 3 };
 
 TranslationUnit::~TranslationUnit() {
-  
-  llvm::DenseSet<Decl*> Killed;
-  
-  for (iterator I=begin(), E=end(); I!=E; ++I) {
-    if (Killed.count(*I)) continue;
-
-    Killed.insert(*I);
-    (*I)->Destroy(*Context);
-  }
-  
   if (OwnsMetaData && Context) {
+    llvm::DenseSet<Decl*> Killed;
+    for (iterator I=begin(), E=end(); I!=E; ++I) {
+      if (Killed.count(*I)) continue;
+
+      Killed.insert(*I);
+      (*I)->Destroy(*Context);
+    }
+
     // The ASTContext object has the sole references to the IdentifierTable
     // Selectors, and the Target information.  Go and delete them, since
     // the TranslationUnit effectively owns them.
-    
     delete &(Context->Idents);
     delete &(Context->Selectors);
     delete &(Context->Target);