35
35
#include " swift/Parse/Lexer.h"
36
36
#include " swift/Parse/Parser.h"
37
37
#include " swift/Config.h"
38
+ #include " swift/Strings.h"
38
39
#include " clang/AST/ASTContext.h"
39
40
#include " clang/AST/Mangle.h"
40
41
#include " clang/Basic/CharInfo.h"
@@ -719,6 +720,15 @@ ClangImporter::create(ASTContext &ctx,
719
720
auto ppTracker = llvm::make_unique<BridgingPPTracker>(importer->Impl );
720
721
clangPP.addPPCallbacks (std::move (ppTracker));
721
722
723
+ // If we're going to load a bridging PCH, we need to know this now in order to
724
+ // clobber clangPP's predefined-macros buffer, otherwise it will provide macro
725
+ // definitions that collide with those in the PCH. This is actually simulating
726
+ // something clang does when it attaches to a PCH ASTReader as well, it's just
727
+ // interleaving the steps in an unusual order.
728
+ if (llvm::sys::path::extension (importerOpts.BridgingHeader ).endswith (
729
+ PCH_EXTENSION))
730
+ clangPP.setPredefines (" " );
731
+
722
732
instance.createModuleManager ();
723
733
instance.getModuleManager ()->addListener (
724
734
std::unique_ptr<clang::ASTReaderListener>(
@@ -926,6 +936,9 @@ bool ClangImporter::importHeader(StringRef header, Module *adapter,
926
936
bool ClangImporter::importBridgingHeader (StringRef header, Module *adapter,
927
937
SourceLoc diagLoc,
928
938
bool trackParsedSymbols) {
939
+ if (llvm::sys::path::extension (header).endswith (PCH_EXTENSION)) {
940
+ return importBridgingPCH (header, adapter);
941
+ }
929
942
clang::FileManager &fileManager = Impl.Instance ->getFileManager ();
930
943
const clang::FileEntry *headerFile = fileManager.getFile (header,
931
944
/* OpenFile=*/ true );
@@ -948,6 +961,29 @@ bool ClangImporter::importBridgingHeader(StringRef header, Module *adapter,
948
961
std::move (sourceBuffer));
949
962
}
950
963
964
+ bool ClangImporter::importBridgingPCH (StringRef pchFile, ModuleDecl *adapter,
965
+ SourceLoc diagLoc)
966
+ {
967
+ clang::ASTReader &R = *Impl.Instance ->getModuleManager ();
968
+ SmallVector<clang::ASTReader::ImportedSubmodule, 2 > Imports;
969
+ switch (R.ReadAST (pchFile, clang::serialization::MK_PCH,
970
+ clang::SourceLocation (), 0 , &Imports)) {
971
+ case clang::ASTReader::Success:
972
+ Impl.ImportedHeaderOwners .push_back (adapter);
973
+ for (auto I : Imports) {
974
+ clang::Module *CM = R.getSubmodule (I.ID );
975
+ assert (CM);
976
+ Impl.getClangPreprocessor ().makeModuleVisible (CM, I.ImportLoc );
977
+ R.makeModuleVisible (CM, clang::Module::AllVisible, I.ImportLoc );
978
+ Module *M = Impl.finishLoadingClangModule (*this , CM, /* adapter=*/ true );
979
+ Impl.ImportedHeaderExports .push_back ({ /* filter=*/ {}, M });
980
+ }
981
+ return false ;
982
+ default :
983
+ return true ;
984
+ }
985
+ }
986
+
951
987
std::string ClangImporter::getBridgingHeaderContents (StringRef headerPath,
952
988
off_t &fileSize,
953
989
time_t &fileModTime) {
0 commit comments