@@ -194,7 +194,8 @@ namespace {
194194 Importer.addSearchPath (path, /* isFramework*/ false , /* isSystem=*/ false );
195195 }
196196
197- auto PCH = Importer.getOrCreatePCH (ImporterOpts, SwiftPCHHash);
197+ auto PCH =
198+ Importer.getOrCreatePCH (ImporterOpts, SwiftPCHHash, /* Cached=*/ true );
198199 if (PCH.has_value ()) {
199200 Impl.getClangInstance ()->getPreprocessorOpts ().ImplicitPCHInclude =
200201 PCH.value ();
@@ -546,8 +547,10 @@ importer::getNormalInvocationArguments(
546547 });
547548 }
548549
549- if (auto path = getCxxShimModuleMapPath (searchPathOpts, triple)) {
550- invocationArgStrs.push_back ((Twine (" -fmodule-map-file=" ) + *path).str ());
550+ if (LangOpts.EnableCXXInterop ) {
551+ if (auto path = getCxxShimModuleMapPath (searchPathOpts, triple)) {
552+ invocationArgStrs.push_back ((Twine (" -fmodule-map-file=" ) + *path).str ());
553+ }
551554 }
552555
553556 // Set C language options.
@@ -953,8 +956,9 @@ ClangImporter::getPCHFilename(const ClangImporterOptions &ImporterOptions,
953956 return PCHFilename.str ().str ();
954957}
955958
956- Optional<std::string> ClangImporter::getOrCreatePCH (
957- const ClangImporterOptions &ImporterOptions, StringRef SwiftPCHHash) {
959+ Optional<std::string>
960+ ClangImporter::getOrCreatePCH (const ClangImporterOptions &ImporterOptions,
961+ StringRef SwiftPCHHash, bool Cached) {
958962 bool isExplicit;
959963 auto PCHFilename = getPCHFilename (ImporterOptions, SwiftPCHHash,
960964 isExplicit);
@@ -970,8 +974,8 @@ Optional<std::string> ClangImporter::getOrCreatePCH(
970974 << EC.message ();
971975 return None;
972976 }
973- auto FailedToEmit =
974- emitBridgingPCH (ImporterOptions. BridgingHeader , PCHFilename.value ());
977+ auto FailedToEmit = emitBridgingPCH (ImporterOptions. BridgingHeader ,
978+ PCHFilename.value (), Cached );
975979 if (FailedToEmit) {
976980 return None;
977981 }
@@ -983,9 +987,10 @@ Optional<std::string> ClangImporter::getOrCreatePCH(
983987std::vector<std::string>
984988ClangImporter::getClangArguments (ASTContext &ctx, bool ignoreClangTarget) {
985989 std::vector<std::string> invocationArgStrs;
986- // Clang expects this to be like an actual command line. So we need to pass in
987- // "clang" for argv[0]
988- invocationArgStrs.push_back (ctx.ClangImporterOpts .clangPath );
990+ // When creating from driver commands, clang expects this to be like an actual
991+ // command line. So we need to pass in "clang" for argv[0]
992+ if (!ctx.ClangImporterOpts .DirectClangCC1ModuleBuild )
993+ invocationArgStrs.push_back (ctx.ClangImporterOpts .clangPath );
989994 if (ctx.ClangImporterOpts .ExtraArgsOnly ) {
990995 invocationArgStrs.insert (invocationArgStrs.end (),
991996 ctx.ClangImporterOpts .ExtraArgs .begin (),
@@ -1734,13 +1739,13 @@ ClangImporter::cloneCompilerInstanceForPrecompiling() {
17341739}
17351740
17361741bool ClangImporter::emitBridgingPCH (
1737- StringRef headerPath, StringRef outputPCHPath) {
1742+ StringRef headerPath, StringRef outputPCHPath, bool cached ) {
17381743 auto emitInstance = cloneCompilerInstanceForPrecompiling ();
17391744 auto &invocation = emitInstance->getInvocation ();
17401745
17411746 auto LangOpts = invocation.getLangOpts ();
17421747 LangOpts->NeededByPCHOrCompilationUsesPCH = true ;
1743- LangOpts->CacheGeneratedPCH = true ;
1748+ LangOpts->CacheGeneratedPCH = cached ;
17441749
17451750 auto language = getLanguageFromOptions (LangOpts);
17461751 auto inputFile = clang::FrontendInputFile (headerPath, language);
@@ -1991,12 +1996,16 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
19911996 auto &clangHeaderSearch = getClangPreprocessor ().getHeaderSearchInfo ();
19921997 auto realModuleName = SwiftContext.getRealModuleName (path.front ().Item ).str ();
19931998
1994- // Look up the top-level module first, to see if it exists at all.
1995- clang::Module *clangModule = clangHeaderSearch.lookupModule (
1996- realModuleName, /* ImportLoc=*/ clang::SourceLocation (),
1997- /* AllowSearch=*/ true , /* AllowExtraModuleMapSearch=*/ true );
1998- if (!clangModule)
1999- return nullptr ;
1999+ // For explicit module build, module should always exist but module map might
2000+ // not be exist. Go straight to module loader.
2001+ if (Instance->getInvocation ().getLangOpts ()->ImplicitModules ) {
2002+ // Look up the top-level module first, to see if it exists at all.
2003+ clang::Module *clangModule = clangHeaderSearch.lookupModule (
2004+ realModuleName, /* ImportLoc=*/ clang::SourceLocation (),
2005+ /* AllowSearch=*/ true , /* AllowExtraModuleMapSearch=*/ true );
2006+ if (!clangModule)
2007+ return nullptr ;
2008+ }
20002009
20012010 // Convert the Swift import path over to a Clang import path.
20022011 SmallVector<std::pair<clang::IdentifierInfo *, clang::SourceLocation>, 4 >
@@ -2051,7 +2060,7 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
20512060
20522061 // Now load the top-level module, so that we can check if the submodule
20532062 // exists without triggering a fatal error.
2054- clangModule = loadModule (clangPath.front (), clang::Module::AllVisible);
2063+ auto clangModule = loadModule (clangPath.front (), clang::Module::AllVisible);
20552064 if (!clangModule)
20562065 return nullptr ;
20572066
0 commit comments