[cfe-commits] r51862 - in /cfe/trunk: include/clang/AST/DeclBase.h lib/AST/Decl.cpp lib/Sema/SemaDecl.cpp test/CodeGen/merge-attrs.c

Nuno Lopes nunoplopes at sapo.pt
Sun Jun 1 15:53:53 PDT 2008


Author: nlopes
Date: Sun Jun  1 17:53:53 2008
New Revision: 51862

URL: http://llvm.org/viewvc/llvm-project?rev=51862&view=rev
Log:
fix decl attributes cleaning
this plugs the leak of attributes and also fixes a crash in the test

Added:
    cfe/trunk/test/CodeGen/merge-attrs.c
Modified:
    cfe/trunk/include/clang/AST/DeclBase.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Sun Jun  1 17:53:53 2008
@@ -136,6 +136,7 @@
   void addAttr(Attr *attr);
   const Attr *getAttrs() const;
   void swapAttrs(Decl *D);
+  void invalidateAttrs();
 
   template<typename T> const T *getAttr() const {
     for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())

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

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Sun Jun  1 17:53:53 2008
@@ -336,14 +336,9 @@
   DeclAttrMapTy::iterator it = DeclAttrs->find(this);
   assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
 
-  // FIXME: Properly release attributes.
-  // delete it->second;
-  DeclAttrs->erase(it);
-  
-  if (DeclAttrs->empty()) {
-    delete DeclAttrs;
-    DeclAttrs = 0;
-  }        
+  // release attributes.
+  delete it->second;
+  invalidateAttrs();
 }
 
 void Decl::addAttr(Attr *NewAttr) {
@@ -358,6 +353,19 @@
   HasAttrs = true;
 }
 
+void Decl::invalidateAttrs() {
+  if (!HasAttrs) return;
+
+  HasAttrs = false;
+  (*DeclAttrs)[this] = 0;
+  DeclAttrs->erase(this);
+
+  if (DeclAttrs->empty()) {
+    delete DeclAttrs;
+    DeclAttrs = 0;
+  }
+}
+
 const Attr *Decl::getAttrs() const {
   if (!HasAttrs)
     return 0;

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sun Jun  1 17:53:53 2008
@@ -276,7 +276,6 @@
 static void MergeAttributes(Decl *New, Decl *Old) {
   Attr *attr = const_cast<Attr*>(Old->getAttrs()), *tmp;
 
-// FIXME: fix this code to cleanup the Old attrs correctly
   while (attr) {
      tmp = attr;
      attr = attr->getNext();
@@ -288,6 +287,8 @@
        delete(tmp);
     }
   }
+
+  Old->invalidateAttrs();
 }
 
 /// MergeFunctionDecl - We just parsed a function 'New' from

Added: cfe/trunk/test/CodeGen/merge-attrs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/merge-attrs.c?rev=51862&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/merge-attrs.c (added)
+++ cfe/trunk/test/CodeGen/merge-attrs.c Sun Jun  1 17:53:53 2008
@@ -0,0 +1,13 @@
+// RUN: clang %s -emit-llvm
+
+void *malloc(int size) __attribute__ ((__nothrow__));
+
+inline static void __zend_malloc() {
+    malloc(1);
+}
+
+void *malloc(int size) __attribute__ ((__nothrow__));
+
+void fontFetch() {
+    __zend_malloc(1);
+}





More information about the cfe-commits mailing list