[cfe-commits] r64474 - in /cfe/trunk: include/clang/AST/DeclGroup.h include/clang/AST/Stmt.h lib/AST/CFG.cpp lib/AST/DeclGroup.cpp lib/AST/Stmt.cpp lib/AST/StmtSerialization.cpp lib/Sema/SemaStmt.cpp

Douglas Gregor dgregor at apple.com
Fri Feb 13 11:06:18 PST 2009


Author: dgregor
Date: Fri Feb 13 13:06:18 2009
New Revision: 64474

URL: http://llvm.org/viewvc/llvm-project?rev=64474&view=rev
Log:
Remove DeclGroupOwningRef, since we intend for declarations to be owned
by DeclContexts (always) rather than by statements. 

DeclContext currently goes out of its way to avoid destroying any
Decls that might be owned by a DeclGroupOwningRef. However, in an
error-recovery situation, a failure in a declaration statement can
cause all of the decls in a DeclGroupOwningRef to be destroyed after
they've already be added into the DeclContext. Hence, DeclContext is
left with already-destroyed declarations, and bad things happen. This
problem was causing failures that showed up as assertions on x86 Linux
in test/Parser/objc-forcollection-neg-2.m.


Modified:
    cfe/trunk/include/clang/AST/DeclGroup.h
    cfe/trunk/include/clang/AST/Stmt.h
    cfe/trunk/lib/AST/CFG.cpp
    cfe/trunk/lib/AST/DeclGroup.cpp
    cfe/trunk/lib/AST/Stmt.cpp
    cfe/trunk/lib/AST/StmtSerialization.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp

Modified: cfe/trunk/include/clang/AST/DeclGroup.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclGroup.h?rev=64474&r1=64473&r2=64474&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclGroup.h (original)
+++ cfe/trunk/include/clang/AST/DeclGroup.h Fri Feb 13 13:06:18 2009
@@ -53,7 +53,7 @@
   void Emit(llvm::Serializer& S) const;
   
   /// Read - Deserialize a DeclGroup from Bitcode.
-  static DeclGroup* Create(llvm::Deserializer& D, ASTContext& C);
+  static DeclGroup* Read(llvm::Deserializer& D, ASTContext& C);
 };
     
 class DeclGroupRef {
@@ -110,30 +110,5 @@
   static DeclGroupRef ReadVal(llvm::Deserializer& D);
 };
   
-class DeclGroupOwningRef : public DeclGroupRef {
-public:
-  explicit DeclGroupOwningRef() : DeclGroupRef((Decl*)0) {}
-  explicit DeclGroupOwningRef(Decl* d) : DeclGroupRef(d) {}
-  explicit DeclGroupOwningRef(DeclGroup* dg) : DeclGroupRef(dg) {}
-
-  ~DeclGroupOwningRef();  
-  void Destroy(ASTContext& C);
-  
-  DeclGroupOwningRef(DeclGroupOwningRef& R)
-    : DeclGroupRef(R) { R.D = 0; }
-  
-  DeclGroupOwningRef& operator=(DeclGroupOwningRef& R) {
-    D = R.D;
-    R.D = 0;
-    return *this;
-  }
-  
-  /// Emit - Serialize a DeclGroupOwningRef to Bitcode.
-  void Emit(llvm::Serializer& S) const;
-  
-  /// Read - Deserialize a DeclGroupOwningRef from Bitcode.
-  DeclGroupOwningRef& Read(llvm::Deserializer& D, ASTContext& C);
-};
-
 } // end clang namespace
 #endif

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=64474&r1=64473&r2=64474&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Fri Feb 13 13:06:18 2009
@@ -226,10 +226,10 @@
 ///
 class DeclStmt : public Stmt {
 protected:
-  DeclGroupOwningRef DG;
+  DeclGroupRef DG;
   SourceLocation StartLoc, EndLoc;
 public:
-  DeclStmt(DeclGroupOwningRef& dg, SourceLocation startLoc, 
+  DeclStmt(DeclGroupRef dg, SourceLocation startLoc, 
            SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg),
                                     StartLoc(startLoc), EndLoc(endLoc) {}
   

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

==============================================================================
--- cfe/trunk/lib/AST/CFG.cpp (original)
+++ cfe/trunk/lib/AST/CFG.cpp Fri Feb 13 13:06:18 2009
@@ -379,11 +379,8 @@
                        ? 8 : llvm::AlignOf<DeclStmt>::Alignment;
           
           // Allocate the DeclStmt using the BumpPtrAllocator.  It will
-          // get automatically freed with the CFG.  Note that even though
-          // we are using a DeclGroupOwningRef that wraps a singe Decl*,
-          // that Decl* will not get deallocated because the destroy method
-          // of DG is never called.
-          DeclGroupOwningRef DG(*I);
+          // get automatically freed with the CFG. 
+          DeclGroupRef DG(*I);
           Decl* D = *I;
           void* Mem = cfg->getAllocator().Allocate(sizeof(DeclStmt), A);
           

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

==============================================================================
--- cfe/trunk/lib/AST/DeclGroup.cpp (original)
+++ cfe/trunk/lib/AST/DeclGroup.cpp Fri Feb 13 13:06:18 2009
@@ -7,7 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 //
-//  This file defines the DeclGroup, DeclGroupRef, and OwningDeclGroup classes.
+//  This file defines the DeclGroup and DeclGroupRef classes.
 //
 //===----------------------------------------------------------------------===//
 
@@ -36,7 +36,7 @@
 }
 
 /// Read - Deserialize a DeclGroup from Bitcode.
-DeclGroup* DeclGroup::Create(llvm::Deserializer& D, ASTContext& C) {
+DeclGroup* DeclGroup::Read(llvm::Deserializer& D, ASTContext& C) {
   unsigned NumDecls = (unsigned) D.ReadInt();
   unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls;
   unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;  
@@ -54,32 +54,10 @@
 }
 
 void DeclGroup::Destroy(ASTContext& C) {
-  Decl** Decls = (Decl**) (this + 1);
-  
-  for (unsigned i = 0; i < NumDecls; ++i)
-    Decls[i]->Destroy(C);
-  
   this->~DeclGroup();
   C.Deallocate((void*) this);
 }
 
-DeclGroupOwningRef::~DeclGroupOwningRef() {
-  assert (D == 0 && "Destroy method not called.");
-}
-
-void DeclGroupOwningRef::Destroy(ASTContext& C) {
-  if (!D)
-    return;
-  
-  if (getKind() == DeclKind)
-    D->Destroy(C);
-  else    
-    reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) &
-                                 ~Mask)->Destroy(C);
-  
-  D = 0;
-}
-
 void DeclGroupRef::Emit(llvm::Serializer& S) const {
   if (getKind() == DeclKind) {
     S.EmitBool(false);
@@ -98,28 +76,3 @@
   
   return DeclGroupRef(D.ReadPtr<DeclGroup>());
 }
-
-void DeclGroupOwningRef::Emit(llvm::Serializer& S) const {
-  if (getKind() == DeclKind) {
-    S.EmitBool(false);
-    S.EmitOwnedPtr(D);
-  }
-  else {
-    S.EmitBool(true);
-    S.EmitOwnedPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D)
-                                                & ~Mask));        
-  }
-}
-
-DeclGroupOwningRef& DeclGroupOwningRef::Read(llvm::Deserializer& Dezr, 
-                                             ASTContext& C) {
-  
-  if (!Dezr.ReadBool())
-    D = Dezr.ReadOwnedPtr<Decl>(C);
-  else {
-    uintptr_t x = reinterpret_cast<uintptr_t>(Dezr.ReadOwnedPtr<DeclGroup>(C));
-    D = reinterpret_cast<Decl*>(x | DeclGroupKind);
-  }
-  
-  return *this;
-}

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

==============================================================================
--- cfe/trunk/lib/AST/Stmt.cpp (original)
+++ cfe/trunk/lib/AST/Stmt.cpp Fri Feb 13 13:06:18 2009
@@ -58,7 +58,6 @@
 }
 
 void DeclStmt::Destroy(ASTContext& C) {
-  DG.Destroy(C);
   this->~DeclStmt();
   C.Deallocate((void *)this);
 }

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

==============================================================================
--- cfe/trunk/lib/AST/StmtSerialization.cpp (original)
+++ cfe/trunk/lib/AST/StmtSerialization.cpp Fri Feb 13 13:06:18 2009
@@ -550,8 +550,7 @@
 DeclStmt* DeclStmt::CreateImpl(Deserializer& D, ASTContext& C) {
   SourceLocation StartLoc = SourceLocation::ReadVal(D);
   SourceLocation EndLoc = SourceLocation::ReadVal(D); 
-  DeclGroupOwningRef DG;
-  return new DeclStmt(DG.Read(D, C), StartLoc, EndLoc);
+  return new DeclStmt(DeclGroupRef::ReadVal(D), StartLoc, EndLoc);
 }
 
 void DeclRefExpr::EmitImpl(Serializer& S) const {

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=64474&r1=64473&r2=64474&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Fri Feb 13 13:06:18 2009
@@ -58,11 +58,11 @@
   assert (!decls.empty());
 
   if (decls.size() == 1) {
-    DeclGroupOwningRef DG(*decls.begin());                      
+    DeclGroupRef DG(*decls.begin());                      
     return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
   }
   else {
-    DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
+    DeclGroupRef DG(DeclGroup::Create(Context, decls.size(), &decls[0]));
     return Owned(new (Context) DeclStmt(DG, StartLoc, EndLoc));
   }
 }





More information about the cfe-commits mailing list