@@ -1752,24 +1752,32 @@ static void applyOverrideOptions(std::vector<std::string> &args,
17521752
17531753void SwiftASTContext::AddExtraClangArgs (
17541754 const std::vector<std::string> &ExtraArgs, StringRef overrideOpts) {
1755+ if (ExtraArgs.empty ())
1756+ return ;
1757+
17551758 swift::ClangImporterOptions &importer_options = GetClangImporterOptions ();
17561759
17571760 // Detect cc1 flags. When DirectClangCC1ModuleBuild is on then the
17581761 // clang arguments in the serialized invocation are clang cc1 flags,
17591762 // which are very specific to one compiler version and cannot
17601763 // be merged with driver options.
17611764 bool fresh_invocation = importer_options.ExtraArgs .empty ();
1762- if (fresh_invocation && !ExtraArgs.empty () && ExtraArgs.front () == " -cc1" )
1763- importer_options.DirectClangCC1ModuleBuild = true ;
1764- if (!importer_options.DirectClangCC1ModuleBuild && !ExtraArgs.empty () &&
1765- ExtraArgs.front () == " -cc1" )
1765+ bool invocation_direct_cc1 = ExtraArgs.front () == " -cc1" ;
1766+
1767+ // If it is not a fresh invocation, make sure the cc1 option matches.
1768+ if (!fresh_invocation &&
1769+ (importer_options.DirectClangCC1ModuleBuild != invocation_direct_cc1))
17661770 AddDiagnostic (
17671771 eSeverityWarning,
17681772 " Mixing and matching of driver and cc1 Clang options detected" );
17691773
1774+ importer_options.DirectClangCC1ModuleBuild = invocation_direct_cc1;
1775+
17701776 // If using direct cc1 flags, compute the arguments and return.
1771- // Since this is cc1 flags, no driver overwrite can be applied .
1777+ // Since this is cc1 flags, override options are driver flags and don't apply .
17721778 if (importer_options.DirectClangCC1ModuleBuild ) {
1779+ if (!fresh_invocation)
1780+ importer_options.ExtraArgs .clear ();
17731781 AddExtraClangCC1Args (ExtraArgs, importer_options.ExtraArgs );
17741782 return ;
17751783 }
@@ -1821,12 +1829,17 @@ void SwiftASTContext::AddExtraClangCC1Args(
18211829 invocation.getFrontendOpts ().ModuleCacheKeys .clear ();
18221830 invocation.getCASOpts () = clang::CASOptions ();
18231831
1832+ // Ignore CAS info inside modules when loading.
1833+ invocation.getFrontendOpts ().ModuleLoadIgnoreCAS = true ;
1834+
18241835 // Remove non-existing modules in a systematic way.
18251836 bool module_missing = false ;
18261837 auto CheckFileExists = [&](const char *file) {
18271838 if (!llvm::sys::fs::exists (file)) {
1828- std::string m_description;
1829- HEALTH_LOG_PRINTF (" Nonexistent explicit module file %s" , file);
1839+ std::string warn;
1840+ llvm::raw_string_ostream (warn)
1841+ << " Nonexistent explicit module file " << file;
1842+ AddDiagnostic (eSeverityWarning, warn);
18301843 module_missing = true ;
18311844 }
18321845 };
@@ -1835,8 +1848,8 @@ void SwiftASTContext::AddExtraClangCC1Args(
18351848 llvm::for_each (invocation.getFrontendOpts ().ModuleFiles ,
18361849 [&](const auto &mod) { CheckFileExists (mod.c_str ()); });
18371850
1838- // If missing, clear all the prebuilt module options and use implicit module
1839- // build.
1851+ // If missing, clear all the prebuilt module options and switch to implicit
1852+ // modules build.
18401853 if (module_missing) {
18411854 invocation.getHeaderSearchOpts ().PrebuiltModuleFiles .clear ();
18421855 invocation.getFrontendOpts ().ModuleFiles .clear ();
@@ -1944,6 +1957,10 @@ void SwiftASTContext::RemapClangImporterOptions(
19441957
19451958void SwiftASTContext::FilterClangImporterOptions (
19461959 std::vector<std::string> &extra_args, SwiftASTContext *ctx) {
1960+ // The direct cc1 mode do not need any extra audit.
1961+ if (ctx && ctx->GetClangImporterOptions ().DirectClangCC1ModuleBuild )
1962+ return ;
1963+
19471964 std::string ivfs_arg;
19481965 // Copy back a filtered version of ExtraArgs.
19491966 std::vector<std::string> orig_args (std::move (extra_args));
@@ -1954,11 +1971,6 @@ void SwiftASTContext::FilterClangImporterOptions(
19541971 arg_sr == " -fno-implicit-module-maps" )
19551972 continue ;
19561973
1957- // This is not a cc1 option.
1958- if (arg_sr.starts_with (" --target=" ) && ctx &&
1959- ctx->GetClangImporterOptions ().DirectClangCC1ModuleBuild )
1960- continue ;
1961-
19621974 // The VFS options turn into fatal errors when the referenced file
19631975 // is not found. Since the Xcode build system tends to create a
19641976 // lot of VFS overlays by default, stat them and emit a warning if
@@ -2245,6 +2257,11 @@ ProcessModule(Module &module, std::string m_description,
22452257 for (auto path : opts.getFrameworkSearchPaths ())
22462258 framework_search_paths.push_back ({path.Path , path.IsSystem });
22472259 auto &clang_opts = invocation.getClangImporterOptions ().ExtraArgs ;
2260+ // If the args embedded are cc1 args, they are not compatible with existing
2261+ // setting. Clear the previous args.
2262+ if (!clang_opts.empty () && clang_opts.front () == " -cc1" )
2263+ extra_clang_args.clear ();
2264+
22482265 for (const std::string &arg : clang_opts) {
22492266 extra_clang_args.push_back (arg);
22502267 LOG_VERBOSE_PRINTF (GetLog (LLDBLog::Types), " adding Clang argument \" %s\" ." ,
0 commit comments