@@ -1644,16 +1644,6 @@ void ModuleDependencyScanner::resolveCrossImportOverlayDependencies(
1644
1644
allModules.end (), action);
1645
1645
}
1646
1646
1647
- static void appendHeaderContent (llvm::raw_ostream &OS,
1648
- llvm::MemoryBufferRef buffer,
1649
- ModuleDependencyID fromModule) {
1650
- // Use preprocessor directives to add some clues for where the content is
1651
- // coming from.
1652
- OS << " # 1 \" <module-" << fromModule.ModuleName << " >/"
1653
- << llvm::sys::path::filename (buffer.getBufferIdentifier ()) << " \" 1\n " ;
1654
- OS << buffer.getBuffer ();
1655
- }
1656
-
1657
1647
llvm::Error ModuleDependencyScanner::performBridgingHeaderChaining (
1658
1648
const ModuleDependencyID &rootModuleID, ModuleDependenciesCache &cache,
1659
1649
ModuleDependencyIDSetVector &allModules) {
@@ -1663,35 +1653,60 @@ llvm::Error ModuleDependencyScanner::performBridgingHeaderChaining(
1663
1653
llvm::SmallString<256 > chainedHeaderBuffer;
1664
1654
llvm::raw_svector_ostream outOS (chainedHeaderBuffer);
1665
1655
1656
+ // If prefix mapping is used, don't try to import header since that will add
1657
+ // a path-relative component into dependency scanning and defeat the purpose
1658
+ // of prefix mapping. Additionally, if everything is prefix mapped, the
1659
+ // embedded header path is also prefix mapped, thus it can't be found anyway.
1660
+ bool useImportHeader = !hasPathMapping ();
1661
+ auto FS = ScanASTContext.SourceMgr .getFileSystem ();
1662
+
1663
+ auto chainBridgingHeader = [&](StringRef moduleName, StringRef headerPath,
1664
+ StringRef binaryModulePath) -> llvm::Error {
1665
+ if (useImportHeader) {
1666
+ if (auto buffer = FS->getBufferForFile (headerPath)) {
1667
+ outOS << " #include \" " << headerPath << " \"\n " ;
1668
+ return llvm::Error::success ();
1669
+ }
1670
+ }
1671
+
1672
+ if (binaryModulePath.empty ())
1673
+ return llvm::createStringError (" failed to load bridging header " +
1674
+ headerPath);
1675
+
1676
+ // Extract the embedded bridging header
1677
+ auto moduleBuf = FS->getBufferForFile (binaryModulePath);
1678
+ if (!moduleBuf)
1679
+ return llvm::errorCodeToError (moduleBuf.getError ());
1680
+
1681
+ auto content = extractEmbeddedBridgingHeaderContent (
1682
+ std::move (*moduleBuf), /* headerPath=*/ " " , ScanASTContext);
1683
+ if (!content)
1684
+ return llvm::createStringError (" can't load embedded header from " +
1685
+ binaryModulePath);
1686
+
1687
+ outOS << " # 1 \" <module-" << moduleName
1688
+ << " -embedded-bridging-header>\" 1\n " ;
1689
+ outOS << content->getBuffer () << " \n " ;
1690
+ return llvm::Error::success ();
1691
+ };
1692
+
1666
1693
// Iterate through all the modules and collect all the bridging header
1667
1694
// and chain them into a single file. The allModules list is in the order of
1668
1695
// discover, thus providing stable ordering for a deterministic generated
1669
1696
// buffer.
1670
- auto FS = ScanASTContext.SourceMgr .getFileSystem ();
1671
1697
for (const auto &moduleID : allModules) {
1672
1698
if (moduleID.Kind != ModuleDependencyKind::SwiftBinary)
1673
1699
continue ;
1674
1700
1675
1701
auto moduleDependencyInfo = cache.findKnownDependency (moduleID);
1676
1702
if (auto *binaryMod = moduleDependencyInfo.getAsSwiftBinaryModule ()) {
1677
- if (!binaryMod->headerImport .empty ()) {
1678
- if (auto buffer= FS->getBufferForFile (binaryMod->headerImport )) {
1679
- appendHeaderContent (outOS, (*buffer)->getMemBufferRef (), moduleID);
1680
- } else {
1681
- // Extract the embedded bridging header
1682
- auto moduleBuf = FS->getBufferForFile (binaryMod->compiledModulePath );
1683
- if (!moduleBuf)
1684
- return llvm::errorCodeToError (moduleBuf.getError ());
1685
-
1686
- auto content = extractEmbeddedBridgingHeaderContent (
1687
- std::move (*moduleBuf), /* headerPath=*/ " " , ScanASTContext);
1688
- if (!content)
1689
- return llvm::createStringError (" can't load embedded header from " +
1690
- binaryMod->compiledModulePath );
1691
-
1692
- outOS << content->getBuffer () << " \n " ;
1693
- }
1694
- }
1703
+ if (binaryMod->headerImport .empty ())
1704
+ continue ;
1705
+
1706
+ if (auto E =
1707
+ chainBridgingHeader (moduleID.ModuleName , binaryMod->headerImport ,
1708
+ binaryMod->compiledModulePath ))
1709
+ return E;
1695
1710
}
1696
1711
}
1697
1712
@@ -1718,11 +1733,10 @@ llvm::Error ModuleDependencyScanner::performBridgingHeaderChaining(
1718
1733
// There are bridging header needed to be chained. Append the bridging
1719
1734
// header from main module if needed and create use a new source buffer.
1720
1735
if (mainModule->textualModuleDetails .bridgingHeaderFile ) {
1721
- auto srcBuf = FS->getBufferForFile (
1722
- *mainModule->textualModuleDetails .bridgingHeaderFile );
1723
- if (!srcBuf)
1724
- return llvm::errorCodeToError (srcBuf.getError ());
1725
- appendHeaderContent (outOS, (*srcBuf)->getMemBufferRef (), rootModuleID);
1736
+ if (auto E = chainBridgingHeader (
1737
+ rootModuleID.ModuleName ,
1738
+ *mainModule->textualModuleDetails .bridgingHeaderFile , " " ))
1739
+ return E;
1726
1740
}
1727
1741
1728
1742
SmallString<256 > outputPath (
0 commit comments