Merge pull request #16147 from slavapestov/autolinking-fixes

IRGen: Remove collectLinkLibrariesFromExternals()
diff --git a/include/swift/IRGen/IRGenPublic.h b/include/swift/IRGen/IRGenPublic.h
index c379919..a5857a6 100644
--- a/include/swift/IRGen/IRGenPublic.h
+++ b/include/swift/IRGen/IRGenPublic.h
@@ -36,20 +36,6 @@
 /// Delete the IRGenModule and IRGenerator obtained by the above call.
 void deleteIRGenModule(std::pair<IRGenerator *, IRGenModule *> &Module);
 
-/// Collect the set of libraries to autolink against by mining the
-/// external definitions stored in an AST context.
-///
-/// This entire thing is a hack that we shouldn't need, but it reflects the
-/// fact that we can end up referencing something via an external definition
-/// (e.g., an imported Clang declaration) indirectly via the Clang importer
-/// that would not be visible directly. In such cases, we would fail to
-/// link against the shared library that defines the entity or an overlay that
-/// is needed as part of its import from Clang (e.g., the Foundation overlay
-/// is needed when bridging NSString, even if there is no other mention of
-/// an entity from the Foundation overlay).
-llvm::SmallVector<LinkLibrary, 4> collectLinkLibrariesFromExternals(
-                                                           ASTContext &ctx);
-
 } // end namespace irgen
 } // end namespace swift
 
diff --git a/lib/IRGen/IRGen.cpp b/lib/IRGen/IRGen.cpp
index 92fea19..f877d6b 100644
--- a/lib/IRGen/IRGen.cpp
+++ b/lib/IRGen/IRGen.cpp
@@ -793,10 +793,6 @@
       IGM.addLinkLibrary(linkLib);
     });
 
-    // Hack to handle thunks eagerly synthesized by the Clang importer.
-    for (const auto &linkLib : collectLinkLibrariesFromExternals(Ctx))
-      IGM.addLinkLibrary(linkLib);
-
     if (!IGM.finalize())
       return nullptr;
 
@@ -966,10 +962,6 @@
                   PrimaryGM->addLinkLibrary(linkLib);
                 });
   
-  // Hack to handle thunks eagerly synthesized by the Clang importer.
-  for (const auto &linkLib : collectLinkLibrariesFromExternals(Ctx))
-    PrimaryGM->addLinkLibrary(linkLib);
-
   llvm::StringSet<> referencedGlobals;
 
   for (auto it = irgen.begin(); it != irgen.end(); ++it) {
@@ -1150,20 +1142,3 @@
     return true;
   return false;
 }
-
-SmallVector<LinkLibrary, 4> irgen::collectLinkLibrariesFromExternals(
-                                                           ASTContext &ctx) {
-  SmallVector<LinkLibrary, 4> result;
-  auto addLinkLibrary = [&](LinkLibrary linkLib) {
-    result.push_back(linkLib);
-  };
-
-  llvm::SmallPtrSet<ModuleDecl *, 8> known;
-  for (auto external : ctx.ExternalDefinitions) {
-    swift::ModuleDecl *module = external->getModuleContext();
-    if (known.insert(module).second)
-      module->collectLinkLibraries(addLinkLibrary);
-  }
-
-  return result;
-}
diff --git a/lib/Immediate/Immediate.cpp b/lib/Immediate/Immediate.cpp
index 1396858..fa19415 100644
--- a/lib/Immediate/Immediate.cpp
+++ b/lib/Immediate/Immediate.cpp
@@ -224,12 +224,6 @@
     import.second->collectLinkLibraries(addLinkLibrary);
   });
 
-  // Hack to handle thunks eagerly synthesized by the Clang importer.
-  for (const auto &linkLib :
-          irgen::collectLinkLibrariesFromExternals(CI.getASTContext())) {
-    addLinkLibrary(linkLib);
-  }
-
   tryLoadLibraries(AllLinkLibraries, CI.getASTContext().SearchPathOpts,
                    CI.getDiags());