Merge remote-tracking branch 'origin/swift-4.0-branch' into stable
diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h
index defa08d..bcc2bc0 100644
--- a/include/clang/AST/DeclBase.h
+++ b/include/clang/AST/DeclBase.h
@@ -34,6 +34,7 @@
 class DependentDiagnostic;
 class EnumDecl;
 class ExportDecl;
+class ExternalSourceSymbolAttr;
 class FunctionDecl;
 class FunctionType;
 enum Linkage : unsigned char;
@@ -562,6 +563,10 @@
     NextInContextAndBits.setInt(Bits);
   }
 
+  /// \brief Looks on this and related declarations for an applicable
+  /// external source symbol attribute.
+  ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const;
+
   /// \brief Whether this declaration was marked as being private to the
   /// module in which it was defined.
   bool isModulePrivate() const {
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index 0e37238..4ab06b4 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -403,6 +403,27 @@
   return false;
 }
 
+ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const {
+  const Decl *Definition = nullptr;
+  if (auto ID = dyn_cast<ObjCInterfaceDecl>(this)) {
+    Definition = ID->getDefinition();
+  } else if (auto PD = dyn_cast<ObjCProtocolDecl>(this)) {
+    Definition = PD->getDefinition();
+  } else if (auto TD = dyn_cast<TagDecl>(this)) {
+    Definition = TD->getDefinition();
+  }
+  if (!Definition)
+    Definition = this;
+
+  if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>())
+    return attr;
+  if (auto *dcd = dyn_cast<Decl>(getDeclContext())) {
+    return dcd->getAttr<ExternalSourceSymbolAttr>();
+  }
+
+  return nullptr;
+}
+
 bool Decl::hasDefiningAttr() const {
   return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>();
 }
diff --git a/lib/Index/IndexSymbol.cpp b/lib/Index/IndexSymbol.cpp
index 524cb43..fc5c3df 100644
--- a/lib/Index/IndexSymbol.cpp
+++ b/lib/Index/IndexSymbol.cpp
@@ -318,16 +318,7 @@
   if (Info.Properties & (unsigned)SymbolProperty::Generic)
     Info.Lang = SymbolLanguage::CXX;
 
-  auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* {
-    if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>())
-      return attr;
-    if (auto *dcd = dyn_cast<Decl>(D->getDeclContext())) {
-      if (auto *attr = dcd->getAttr<ExternalSourceSymbolAttr>())
-        return attr;
-    }
-    return nullptr;
-  };
-  if (auto *attr = getExternalSymAttr(D)) {
+  if (auto *attr = D->getExternalSourceSymbolAttr()) {
     if (attr->getLanguage() == "Swift")
       Info.Lang = SymbolLanguage::Swift;
   }
diff --git a/lib/Index/USRGeneration.cpp b/lib/Index/USRGeneration.cpp
index 4fb437b..808150b 100644
--- a/lib/Index/USRGeneration.cpp
+++ b/lib/Index/USRGeneration.cpp
@@ -49,7 +49,7 @@
 static StringRef GetExternalSourceContainer(const NamedDecl *D) {
   if (!D)
     return StringRef();
-  if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) {
+  if (auto *attr = D->getExternalSourceSymbolAttr()) {
     return attr->getDefinedIn();
   }
   return StringRef();
diff --git a/test/Index/Core/external-source-symbol-attr.m b/test/Index/Core/external-source-symbol-attr.m
index 49a075f..cdc5296 100644
--- a/test/Index/Core/external-source-symbol-attr.m
+++ b/test/Index/Core/external-source-symbol-attr.m
@@ -4,6 +4,10 @@
 #define GEN_DECL(mod_name) __attribute__((external_source_symbol(language="Swift", defined_in=mod_name, generated_declaration)))
 #define PUSH_GEN_DECL(mod_name) push(GEN_DECL(mod_name), apply_to=any(enum, objc_interface, objc_category, objc_protocol))
 
+// Forward declarations should not affect module namespacing below
+@class I1;
+@class I2;
+
 // This should not be indexed.
 GEN_DECL("some_module")
 @interface I1
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index 0b041a8..4fd26a9 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -7428,16 +7428,7 @@
 
   const Decl *D = getCursorDecl(C);
 
-  auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* {
-    if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>())
-      return attr;
-    if (auto *dcd = dyn_cast<Decl>(D->getDeclContext())) {
-      if (auto *attr = dcd->getAttr<ExternalSourceSymbolAttr>())
-        return attr;
-    }
-    return nullptr;
-  };
-  if (auto *attr = getExternalSymAttr(D)) {
+  if (auto *attr = D->getExternalSourceSymbolAttr()) {
     if (language)
       *language = cxstring::createDup(attr->getLanguage());
     if (definedIn)