]> granicus.if.org Git - clang/commitdiff
PR3575 - warn on declared variable or function attributes after a definition, which...
authorRyan Flynn <pizza@parseerror.com>
Sat, 25 Jul 2009 22:29:44 +0000 (22:29 +0000)
committerRyan Flynn <pizza@parseerror.com>
Sat, 25 Jul 2009 22:29:44 +0000 (22:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@77095 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Parse/DeclSpec.h
lib/Sema/SemaDecl.cpp
test/Sema/attr-decl-after-definition.c [new file with mode: 0644]

index d9e6af455fed19400b8a4adf92b36f999100ec5e..0ce5db6b4ba12f32fef2d973fc96bcd2d5df3dbc 100644 (file)
@@ -523,6 +523,8 @@ def err_attribute_aligned_not_power_of_two : Error<
 def warn_redeclaration_without_attribute_prev_attribute_ignored : Warning<
   "'%0' redeclared without %1 attribute: previous %1 ignored">;
 def warn_attribute_ignored : Warning<"%0 attribute ignored">;
+def warn_attribute_precede_definition : Warning<
+  "attribute declaration must precede definition">;
 def warn_attribute_weak_on_field : Warning<
   "__weak attribute cannot be specified on a field declaration">;
 def warn_attribute_weak_on_local : Warning<
index b6044ff4c84714c60d0a675399a07dc8365a792b..c202b075bf9a2d6c3b8e112564284a87a2f7ab30 100644 (file)
@@ -1062,6 +1062,15 @@ public:
   const AttributeList *getAttributes() const { return AttrList; }
   AttributeList *getAttributes() { return AttrList; }
 
+  /// hasAttributes - do we contain any attributes?
+  bool hasAttributes() const {
+    if (getAttributes() || getDeclSpec().getAttributes()) return true;
+    for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i)
+      if (getTypeObject(i).getAttrs())
+        return true;
+    return false;
+  }
+
   void setAsmLabel(ActionBase::ExprTy *E) { AsmLabel = E; }
   ActionBase::ExprTy *getAsmLabel() const { return AsmLabel; }
 
index 07e1ce77de8f50aa9f596d9d1ecf88cc61359dcb..0f4e9a42d6ed3a1bdaa7dd08be5447e75f2d6411 100644 (file)
@@ -2085,6 +2085,15 @@ Sema::ActOnVariableDeclarator(Scope* S, Declarator& D, DeclContext* DC,
 
   CheckVariableDeclaration(NewVD, PrevDecl, Redeclaration);
 
+  // attributes declared post-definition are currently ignored
+  if (PrevDecl) {
+    const VarDecl *Def = 0, *PrevVD = dyn_cast<VarDecl>(PrevDecl);
+    if (PrevVD->getDefinition(Def) && 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.
   if (CurContext->isFunctionOrMethod() && NewVD->isExternC(Context) &&
@@ -2582,6 +2591,16 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
   // FIXME: This needs to happen before we merge declarations. Then,
   // let attribute merging cope with attribute conflicts.
   ProcessDeclAttributes(S, NewFD, D);
+
+  // attributes declared post-definition are currently ignored
+  if (PrevDecl) {
+    const FunctionDecl *Def, *PrevFD = dyn_cast<FunctionDecl>(PrevDecl);
+    if (PrevFD && PrevFD->getBody(Def) && D.hasAttributes()) {
+      Diag(NewFD->getLocation(), diag::warn_attribute_precede_definition);
+      Diag(Def->getLocation(), diag::note_previous_definition);
+    }
+  }
+
   AddKnownFunctionAttributes(NewFD);
 
   if (OverloadableAttrRequired && !NewFD->getAttr<OverloadableAttr>()) {
diff --git a/test/Sema/attr-decl-after-definition.c b/test/Sema/attr-decl-after-definition.c
new file mode 100644 (file)
index 0000000..c1d1b53
--- /dev/null
@@ -0,0 +1,19 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+void foo();
+void foo() __attribute__((unused));
+void foo() __attribute__((unused));
+void foo(){} // expected-note {{previous definition is here}}
+void foo() __attribute__((constructor)); // expected-warning {{must precede definition}}
+void foo();
+
+int bar;
+extern int bar;
+int bar;
+int bar __attribute__((weak));
+int bar __attribute__((used));
+extern int bar __attribute__((weak));
+int bar = 0; // expected-note {{previous definition is here}}
+int bar __attribute__((weak)); // expected-warning {{must precede definition}}
+int bar;
+