When checking the namespace of a redeclaration or definition, look through linkage specs. Fixes PR5430.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86461 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 8e1a19c..cfea66b 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -1839,17 +1839,22 @@
     if (isa<TranslationUnitDecl>(DC)) {
       Diag(D.getIdentifierLoc(), diag::err_invalid_declarator_global_scope)
         << Name << D.getCXXScopeSpec().getRange();
-    } else if (!CurContext->Encloses(DC)) {
-      // The qualifying scope doesn't enclose the original declaration.
-      // Emit diagnostic based on current scope.
-      SourceLocation L = D.getIdentifierLoc();
-      SourceRange R = D.getCXXScopeSpec().getRange();
-      if (isa<FunctionDecl>(CurContext))
-        Diag(L, diag::err_invalid_declarator_in_function) << Name << R;
-      else
-        Diag(L, diag::err_invalid_declarator_scope)
-          << Name << cast<NamedDecl>(DC) << R;
-      D.setInvalidType();
+    } else {
+      DeclContext *Cur = CurContext;
+      while (isa<LinkageSpecDecl>(Cur))
+        Cur = Cur->getParent();
+      if (!Cur->Encloses(DC)) {
+        // The qualifying scope doesn't enclose the original declaration.
+        // Emit diagnostic based on current scope.
+        SourceLocation L = D.getIdentifierLoc();
+        SourceRange R = D.getCXXScopeSpec().getRange();
+        if (isa<FunctionDecl>(Cur))
+          Diag(L, diag::err_invalid_declarator_in_function) << Name << R;
+        else
+          Diag(L, diag::err_invalid_declarator_scope)
+            << Name << cast<NamedDecl>(DC) << R;
+        D.setInvalidType();
+      }
     }
   }
 
diff --git a/test/SemaCXX/linkage-spec.cpp b/test/SemaCXX/linkage-spec.cpp
index 53cd61c..fc9b3ab 100644
--- a/test/SemaCXX/linkage-spec.cpp
+++ b/test/SemaCXX/linkage-spec.cpp
@@ -33,3 +33,10 @@
     };
   }
 }
+
+// PR5430
+namespace pr5430 {
+  extern "C" void func(void);
+}
+using namespace pr5430;
+extern "C" void pr5430::func(void) { }