[cfe-commits] r92047 - /cfe/trunk/lib/AST/ASTContext.cpp

Ted Kremenek kremenek at apple.com
Wed Dec 23 13:13:52 PST 2009


Author: kremenek
Date: Wed Dec 23 15:13:52 2009
New Revision: 92047

URL: http://llvm.org/viewvc/llvm-project?rev=92047&view=rev
Log:
Tidy up ~ASTContext a bit by turning orphan compound statements into
for loops.  Also do not manually free the Type objects when the
'FreeMemory' flag is set, as they will be deallocated when the
BumpPtrAllocator is destroyed.

Modified:
    cfe/trunk/lib/AST/ASTContext.cpp

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=92047&r1=92046&r2=92047&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Wed Dec 23 15:13:52 2009
@@ -56,44 +56,43 @@
 }
 
 ASTContext::~ASTContext() {
-  // Deallocate all the types.
-  while (!Types.empty()) {
-    Types.back()->Destroy(*this);
-    Types.pop_back();
-  }
+  if (FreeMemory) {
+    // Deallocate all the types.
+    while (!Types.empty()) {
+      Types.back()->Destroy(*this);
+      Types.pop_back();
+    }
 
-  {
-    llvm::FoldingSet<ExtQuals>::iterator
-      I = ExtQualNodes.begin(), E = ExtQualNodes.end();
-    while (I != E)
+    for (llvm::FoldingSet<ExtQuals>::iterator
+         I = ExtQualNodes.begin(), E = ExtQualNodes.end(); I != E; ) {
+      // Increment in loop to prevent using deallocated memory.
       Deallocate(&*I++);
+    }
   }
 
-  {
-    llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
-      I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
-    while (I != E) {
-      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
-      delete R;
-    }
+  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
+       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
+    // Increment in loop to prevent using deallocated memory.
+    ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
+    delete R;
   }
 
-  {
-    llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
-      I = ObjCLayouts.begin(), E = ObjCLayouts.end();
-    while (I != E) {
-      ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
-      delete R;
-    }
+  for (llvm::DenseMap<const ObjCContainerDecl*,
+                      const ASTRecordLayout*>::iterator
+       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
+    // Increment in loop to prevent using deallocated memory.
+    ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
+    delete R;
   }
 
   // Destroy nested-name-specifiers.
   for (llvm::FoldingSet<NestedNameSpecifier>::iterator
          NNS = NestedNameSpecifiers.begin(),
          NNSEnd = NestedNameSpecifiers.end();
-       NNS != NNSEnd;
-       /* Increment in loop */)
+       NNS != NNSEnd; ) {
+    // Increment in loop to prevent using deallocated memory.
     (*NNS++).Destroy(*this);
+  }
 
   if (GlobalNestedNameSpecifier)
     GlobalNestedNameSpecifier->Destroy(*this);





More information about the cfe-commits mailing list