[cfe-commits] r157039 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/Sema/attr-decl-after-definition.c

Rafael Espindola rafael.espindola at gmail.com
Thu May 17 18:47:00 PDT 2012


Author: rafael
Date: Thu May 17 20:47:00 2012
New Revision: 157039

URL: http://llvm.org/viewvc/llvm-project?rev=157039&view=rev
Log:
Centralize the handling of the "attribute declaration must precede definition"
warning. This also makes us warn on tags, which, ironically, is the only case
gcc warns on.

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/attr-decl-after-definition.c

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=157039&r1=157038&r2=157039&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu May 17 20:47:00 2012
@@ -1698,9 +1698,30 @@
   return false;
 }
 
+static const Decl *getDefinition(Decl *D) {
+  if (TagDecl *TD = dyn_cast<TagDecl>(D))
+    return TD->getDefinition();
+  if (VarDecl *VD = dyn_cast<VarDecl>(D))
+    return VD->getDefinition();
+  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+    const FunctionDecl* Def;
+    if (FD->hasBody(Def))
+      return Def;
+  }
+  return NULL;
+}
+
 /// mergeDeclAttributes - Copy attributes from the Old decl to the New one.
 void Sema::mergeDeclAttributes(Decl *New, Decl *Old,
                                bool MergeDeprecation) {
+  // attributes declared post-definition are currently ignored
+  const Decl *Def = getDefinition(Old);
+  if (Def && Def != New && New->hasAttrs()) {
+    Diag(New->getLocation(), diag::warn_attribute_precede_definition);
+    Diag(Def->getLocation(), diag::note_previous_definition);
+    New->dropAttrs();
+  }
+
   if (!Old->hasAttrs())
     return;
 
@@ -4251,18 +4272,6 @@
         CheckMemberSpecialization(NewVD, Previous))
       NewVD->setInvalidDecl();
   }
-  
-  // attributes declared post-definition are currently ignored
-  // FIXME: This should be handled in attribute merging, not
-  // here.
-  if (Previous.isSingleResult()) {
-    VarDecl *Def = dyn_cast<VarDecl>(Previous.getFoundDecl());
-    if (Def && (Def = Def->getDefinition()) &&
-        Def != NewVD && D.hasAttributes()) {
-      Diag(NewVD->getLocation(), diag::warn_attribute_precede_definition);
-      Diag(Def->getLocation(), diag::note_previous_definition);
-    }
-  }
 
   // If this is a locally-scoped extern C variable, update the map of
   // such variables.
@@ -5665,17 +5674,6 @@
     }
   }
 
-  // attributes declared post-definition are currently ignored
-  // FIXME: This should happen during attribute merging
-  if (D.isRedeclaration() && Previous.isSingleResult()) {
-    const FunctionDecl *Def;
-    FunctionDecl *PrevFD = dyn_cast<FunctionDecl>(Previous.getFoundDecl());
-    if (PrevFD && PrevFD->isDefined(Def) && D.hasAttributes()) {
-      Diag(NewFD->getLocation(), diag::warn_attribute_precede_definition);
-      Diag(Def->getLocation(), diag::note_previous_definition);
-    }
-  }
-
   AddKnownFunctionAttributes(NewFD);
 
   if (NewFD->hasAttr<OverloadableAttr>() && 

Modified: cfe/trunk/test/Sema/attr-decl-after-definition.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-decl-after-definition.c?rev=157039&r1=157038&r2=157039&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-decl-after-definition.c (original)
+++ cfe/trunk/test/Sema/attr-decl-after-definition.c Thu May 17 20:47:00 2012
@@ -17,3 +17,6 @@
 int bar __attribute__((weak)); // expected-warning {{must precede definition}}
 int bar;
 
+struct zed {  // expected-note {{previous definition is here}}
+};
+struct __attribute__((visibility("hidden"))) zed; // expected-warning {{must precede definition}}





More information about the cfe-commits mailing list