@@ -1573,7 +1573,21 @@ bool ShouldUnique(StringRef arg) {
15731573
15741574// static
15751575void SwiftASTContext::AddExtraClangArgs (const std::vector<std::string> &source,
1576- std::vector<std::string> &dest) {
1576+ std::vector<std::string> &dest,
1577+ bool cc1) {
1578+ // FIXME: Support for cc1 flags isn't complete. The uniquing
1579+ // algortihm below does not work for cc1 flags. Since cc1 flags are
1580+ // not stable it's not feasible to keep a list of all multi-arg
1581+ // flags, for example. It also makes it difficult to correctly
1582+ // identify where workng directories and path remappings should
1583+ // applied. For all these reasons, using cc1 flags for anything but
1584+ // a local build with explicit modules and precise compiler
1585+ // invocations isn't supported yet.
1586+ if (cc1) {
1587+ dest.insert (dest.end (), source.begin (), source.end ());
1588+ return ;
1589+ }
1590+
15771591 llvm::StringSet<> unique_flags;
15781592 for (auto &arg : dest)
15791593 unique_flags.insert (arg);
@@ -1751,11 +1765,27 @@ static void applyOverrideOptions(std::vector<std::string> &args,
17511765void SwiftASTContext::AddExtraClangArgs (
17521766 const std::vector<std::string> &ExtraArgs, StringRef overrideOpts) {
17531767 swift::ClangImporterOptions &importer_options = GetClangImporterOptions ();
1754- AddExtraClangArgs (ExtraArgs, importer_options.ExtraArgs );
1768+
1769+ // Detect cc1 flags. When DirectClangCC1ModuleBuild is on then the
1770+ // clang arguments in the serialized invocation are clang cc1 flags,
1771+ // which are very specific to one compiler version and cannot
1772+ // be merged with driver options.
1773+ bool fresh_invocation = importer_options.ExtraArgs .empty ();
1774+ if (fresh_invocation && !ExtraArgs.empty () && ExtraArgs.front () == " -cc1" )
1775+ importer_options.DirectClangCC1ModuleBuild = true ;
1776+ if (!importer_options.DirectClangCC1ModuleBuild && !ExtraArgs.empty () &&
1777+ ExtraArgs.front () == " -cc1" )
1778+ AddDiagnostic (
1779+ eSeverityWarning,
1780+ " Mixing and matching of driver and cc1 Clang options detected" );
1781+
1782+ AddExtraClangArgs (ExtraArgs, importer_options.ExtraArgs ,
1783+ importer_options.DirectClangCC1ModuleBuild );
17551784 applyOverrideOptions (importer_options.ExtraArgs , overrideOpts);
17561785 if (HasNonexistentExplicitModule (importer_options.ExtraArgs ))
17571786 RemoveExplicitModules (importer_options.ExtraArgs );
17581787
1788+ // Detect explicitly-built modules.
17591789 m_has_explicit_modules =
17601790 llvm::any_of (importer_options.ExtraArgs , [](const std::string &arg) {
17611791 return StringRef (arg).starts_with (" -fmodule-file=" );
@@ -1864,6 +1894,11 @@ void SwiftASTContext::FilterClangImporterOptions(
18641894 arg_sr == " -fno-implicit-module-maps" )
18651895 continue ;
18661896
1897+ // This is not a cc1 option.
1898+ if (arg_sr.starts_with (" --target=" ) && ctx &&
1899+ ctx->GetClangImporterOptions ().DirectClangCC1ModuleBuild )
1900+ continue ;
1901+
18671902 // The VFS options turn into fatal errors when the referenced file
18681903 // is not found. Since the Xcode build system tends to create a
18691904 // lot of VFS overlays by default, stat them and emit a warning if
@@ -1889,6 +1924,7 @@ void SwiftASTContext::FilterClangImporterOptions(
18891924 // Keep it.
18901925 extra_args.push_back (ivfs_arg);
18911926 }
1927+
18921928 extra_args.push_back (std::move (arg));
18931929 }
18941930}
0 commit comments