[cfe-commits] r112488 - in /cfe/trunk: include/clang/AST/ASTContext.h lib/AST/ASTContext.cpp

Douglas Gregor dgregor at apple.com
Mon Aug 30 09:49:28 PDT 2010


Author: dgregor
Date: Mon Aug 30 11:49:28 2010
New Revision: 112488

URL: http://llvm.org/viewvc/llvm-project?rev=112488&view=rev
Log:
Heap-allocate the attribute vectors in
ASTContext::DeclAttrs. Otherwise, iterators will go stale when the
DenseMap reallocates, which can cause crashes when, e.g., looping over
the attributes in a template to instantiate them and add the results
to the instantiation of that template.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/lib/AST/ASTContext.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=112488&r1=112487&r2=112488&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Mon Aug 30 11:49:28 2010
@@ -198,7 +198,7 @@
   ///
   /// Since so few decls have attrs, we keep them in a hash map instead of
   /// wasting space in the Decl class.
-  llvm::DenseMap<const Decl*, AttrVec> DeclAttrs;
+  llvm::DenseMap<const Decl*, AttrVec*> DeclAttrs;
 
   /// \brief Keeps track of the static data member templates from which
   /// static data members of class template specializations were instantiated.
@@ -321,10 +321,10 @@
   }
 
   /// \brief Retrieve the attributes for the given declaration.
-  AttrVec& getDeclAttrs(const Decl *D) { return DeclAttrs[D]; }
+  AttrVec& getDeclAttrs(const Decl *D);
 
   /// \brief Erase the attributes corresponding to the given declaration.
-  void eraseDeclAttrs(const Decl *D) { DeclAttrs.erase(D); }
+  void eraseDeclAttrs(const Decl *D);
 
   /// \brief If this variable is an instantiated static data member of a
   /// class template specialization, returns the templated static data member

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=112488&r1=112487&r2=112488&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Aug 30 11:49:28 2010
@@ -206,7 +206,12 @@
     if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
       R->Destroy(*this);
   }
-  }
+  
+  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
+                                                    AEnd = DeclAttrs.end();
+       A != AEnd; ++A)
+    A->second->~AttrVec();
+}
 
 void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
   Deallocations.push_back(std::make_pair(Callback, Data));
@@ -364,6 +369,26 @@
   InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
 }
 
+AttrVec& ASTContext::getDeclAttrs(const Decl *D) {
+  AttrVec *&Result = DeclAttrs[D];
+  if (!Result) {
+    void *Mem = Allocate(sizeof(AttrVec));
+    Result = new (Mem) AttrVec;
+  }
+    
+  return *Result;
+}
+
+/// \brief Erase the attributes corresponding to the given declaration.
+void ASTContext::eraseDeclAttrs(const Decl *D) { 
+  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
+  if (Pos != DeclAttrs.end()) {
+    Pos->second->~AttrVec();
+    DeclAttrs.erase(Pos);
+  }
+}
+
+
 MemberSpecializationInfo *
 ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
   assert(Var->isStaticDataMember() && "Not a static data member");





More information about the cfe-commits mailing list