When we inject a declaration into a namespace, add the primary DeclContext to
the update set rather than the current DeclContext. Add test for the local
extern case too.

llvm-svn: 204568
diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp
index 3aeb895..4474328 100644
--- a/clang/lib/Serialization/ASTWriterDecl.cpp
+++ b/clang/lib/Serialization/ASTWriterDecl.cpp
@@ -185,8 +185,9 @@
   // function with a local extern declaration, for instance.
   if (D->isOutOfLine()) {
     auto *NS = dyn_cast<NamespaceDecl>(D->getDeclContext()->getRedeclContext());
+    // FIXME: Also update surrounding inline namespaces.
     if (NS && NS->isFromASTFile())
-      Writer.AddUpdatedDeclContext(NS);
+      Writer.AddUpdatedDeclContext(NS->getPrimaryContext());
   }
 }
 
diff --git a/clang/test/Modules/Inputs/cxx-templates-b.h b/clang/test/Modules/Inputs/cxx-templates-b.h
index 6cd83fa..a682855 100644
--- a/clang/test/Modules/Inputs/cxx-templates-b.h
+++ b/clang/test/Modules/Inputs/cxx-templates-b.h
@@ -66,4 +66,5 @@
 
 void TriggerInstantiation() {
   UseDefinedInBImpl<void>();
+  Std::f<int>();
 }
diff --git a/clang/test/Modules/Inputs/cxx-templates-common.h b/clang/test/Modules/Inputs/cxx-templates-common.h
index 77e3ddd..9b46539 100644
--- a/clang/test/Modules/Inputs/cxx-templates-common.h
+++ b/clang/test/Modules/Inputs/cxx-templates-common.h
@@ -15,3 +15,9 @@
     friend bool operator!=(const WithFriend &A, const WithFriend &B) { return false; }
   };
 }
+
+namespace Std {
+  template<typename T> void f() {
+    extern T g();
+  }
+}
diff --git a/clang/test/Modules/cxx-templates.cpp b/clang/test/Modules/cxx-templates.cpp
index d933193..7b4e57c 100644
--- a/clang/test/Modules/cxx-templates.cpp
+++ b/clang/test/Modules/cxx-templates.cpp
@@ -121,6 +121,11 @@
   return wfi != wfi;
 }
 
+namespace Std {
+  void g(); // expected-error {{functions that differ only in their return type cannot be overloaded}}
+  // expected-note@cxx-templates-common.h:21 {{previous}}
+}
+
 // CHECK-GLOBAL:      DeclarationName 'f'
 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
 // CHECK-GLOBAL-NEXT: `-FunctionTemplate {{.*}} 'f'