[cfe-commits] r77095 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td include/clang/Parse/DeclSpec.h lib/Sema/SemaDecl.cpp test/Sema/attr-decl-after-definition.c
Ryan Flynn
pizza at parseerror.com
Sat Jul 25 15:29:44 PDT 2009
Author: pizza
Date: Sat Jul 25 17:29:44 2009
New Revision: 77095
URL: http://llvm.org/viewvc/llvm-project?rev=77095&view=rev
Log:
PR3575 - warn on declared variable or function attributes after a definition, which are currently ignored.
Added:
cfe/trunk/test/Sema/attr-decl-after-definition.c
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/include/clang/Parse/DeclSpec.h
cfe/trunk/lib/Sema/SemaDecl.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=77095&r1=77094&r2=77095&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sat Jul 25 17:29:44 2009
@@ -523,6 +523,8 @@
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<
Modified: cfe/trunk/include/clang/Parse/DeclSpec.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/DeclSpec.h?rev=77095&r1=77094&r2=77095&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/DeclSpec.h (original)
+++ cfe/trunk/include/clang/Parse/DeclSpec.h Sat Jul 25 17:29:44 2009
@@ -1062,6 +1062,15 @@
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; }
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=77095&r1=77094&r2=77095&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Jul 25 17:29:44 2009
@@ -2085,6 +2085,15 @@
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 @@
// 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>()) {
Added: 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=77095&view=auto
==============================================================================
--- cfe/trunk/test/Sema/attr-decl-after-definition.c (added)
+++ cfe/trunk/test/Sema/attr-decl-after-definition.c Sat Jul 25 17:29:44 2009
@@ -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;
+
More information about the cfe-commits
mailing list