From: Rafael Espindola Date: Thu, 26 Apr 2012 01:26:03 +0000 (+0000) Subject: Reject cases like X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=45a0b264512ee0e9ba874bb3bfeb7f5b96789117;p=clang Reject cases like struct __attribute__((visibility("hidden"))) a; struct __attribute__((visibility("default"))) b; which gcc already rejects. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155603 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index b0e5deba51..8826d0397b 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1815,6 +1815,8 @@ def warn_attribute_unknown_visibility : Warning<"unknown visibility '%0'">; def warn_attribute_protected_visibility : Warning<"target does not support 'protected' visibility; using 'default'">, InGroup>; +def err_mismatched_visibilit: Error<"visibility does not match previous declaration">; +def note_previous_attribute : Note<"previous attribute is here">; def err_unknown_machine_mode : Error<"unknown machine mode %0">; def err_unsupported_machine_mode : Error<"unsupported machine mode %0">; def err_mode_not_primitive : Error< diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 861b91420d..0e1c43c7a4 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -1752,6 +1752,15 @@ static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { return; } + VisibilityAttr *PrevAttr = D->getCanonicalDecl()->getAttr(); + if (PrevAttr) { + VisibilityAttr::VisibilityType PrevVisibility = PrevAttr->getVisibility(); + if (PrevVisibility != type) { + S.Diag(Attr.getLoc(), diag::err_mismatched_visibilit); + S.Diag(PrevAttr->getLocation(), diag::note_previous_attribute); + return; + } + } D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type)); } diff --git a/test/Sema/attr-visibility.c b/test/Sema/attr-visibility.c index 5cf269582a..afeb4f1546 100644 --- a/test/Sema/attr-visibility.c +++ b/test/Sema/attr-visibility.c @@ -7,3 +7,5 @@ void test2() __attribute__((visibility("internal"))); // rdar://problem/10753392 void test3() __attribute__((visibility("protected"))); // expected-warning {{target does not support 'protected' visibility; using 'default'}} +struct __attribute__((visibility("hidden"))) test4; // expected-note {{previous attribute is here}} +struct __attribute__((visibility("default"))) test4; // expected-error {{visibility does not match previous declaration}}