]> granicus.if.org Git - clang/commitdiff
fix decl attributes cleaning
authorNuno Lopes <nunoplopes@sapo.pt>
Sun, 1 Jun 2008 22:53:53 +0000 (22:53 +0000)
committerNuno Lopes <nunoplopes@sapo.pt>
Sun, 1 Jun 2008 22:53:53 +0000 (22:53 +0000)
this plugs the leak of attributes and also fixes a crash in the test

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51862 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclBase.h
lib/AST/Decl.cpp
lib/Sema/SemaDecl.cpp
test/CodeGen/merge-attrs.c [new file with mode: 0644]

index 210f1691502ca9694cba9844ab587c4b2cfcfb33..b4f4c3b04651aec8f7b3d19327601ba07e068468 100644 (file)
@@ -136,6 +136,7 @@ public:
   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())
index 8d87d1cb4a25a6062c2ae7635efa696e3fcfe827..8d9913b1aae9dc82b94f8aa75d8bca963d59df7d 100644 (file)
@@ -336,14 +336,9 @@ Decl::~Decl() {
   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 @@ void Decl::addAttr(Attr *NewAttr) {
   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;
index eb0abafcaaa29cdf2a06ef987355c48c7cb7eea2..230ea3cb9da72d6fec8d0faebcf7973b4ba0eb4a 100644 (file)
@@ -276,7 +276,6 @@ static bool DeclHasAttr(const Decl *decl, const Attr *target) {
 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 @@ static void MergeAttributes(Decl *New, Decl *Old) {
        delete(tmp);
     }
   }
+
+  Old->invalidateAttrs();
 }
 
 /// MergeFunctionDecl - We just parsed a function 'New' from
diff --git a/test/CodeGen/merge-attrs.c b/test/CodeGen/merge-attrs.c
new file mode 100644 (file)
index 0000000..5ac0217
--- /dev/null
@@ -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);
+}