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 b0e5deb..8826d03 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1815,6 +1815,8 @@
 def warn_attribute_protected_visibility :
   Warning<"target does not support 'protected' visibility; using 'default'">,
   InGroup<DiagGroup<"unsupported-visibility">>;
+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 861b914..0e1c43c 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -1752,6 +1752,15 @@
     return;
   }
 
+  VisibilityAttr *PrevAttr = D->getCanonicalDecl()->getAttr<VisibilityAttr>();
+  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 5cf2695..afeb4f1 100644
--- a/test/Sema/attr-visibility.c
+++ b/test/Sema/attr-visibility.c
@@ -7,3 +7,5 @@
 // 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}}