Skip to content

Commit a331583

Browse files
[ScanDependency] Add -experimental-clang-importer-direct-cc1-scan
Add an experimental option to tell dependency scanner to report clang cc1 args should be used to construct clang importer in all constructed swift-frontend tasks.
1 parent 0b3a1fc commit a331583

File tree

7 files changed

+51
-3
lines changed

7 files changed

+51
-3
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,10 @@ namespace swift {
961961
/// Using ClangIncludeTreeRoot for compilation.
962962
bool HasClangIncludeTreeRoot = false;
963963

964+
/// Whether the dependency scanner should construct all swift-frontend
965+
/// invocations directly from clang cc1 args.
966+
bool ClangImporterDirectCC1Scan = false;
967+
964968
/// Return a hash code of any components from these options that should
965969
/// contribute to a Swift Bridging PCH hash.
966970
llvm::hash_code getPCHHashComponents() const {

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,11 @@ def gcc_toolchain: Separate<["-"], "gcc-toolchain">,
18901890
MetaVarName<"<path>">,
18911891
HelpText<"Specify a directory where the clang importer and clang linker can find headers and libraries">;
18921892

1893+
def experimental_clang_importer_direct_cc1_scan:
1894+
Flag<["-"], "experimental-clang-importer-direct-cc1-scan">,
1895+
Flags<[FrontendOption, NewDriverOnlyOption, HelpHidden]>,
1896+
HelpText<"Enables swift driver to construct swift-frontend invocations using -direct-clang-cc1-module-build">;
1897+
18931898
def cache_compile_job: Flag<["-"], "cache-compile-job">,
18941899
Flags<[FrontendOption, NewDriverOnlyOption]>,
18951900
HelpText<"Enable compiler caching">;

lib/DependencyScan/ModuleDependencyScanner.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ ModuleDependencyScanner::getMainModuleDependencyInfo(
347347

348348
std::string rootID;
349349
std::vector<std::string> buildArgs;
350+
auto clangImporter =
351+
static_cast<ClangImporter *>(ScanASTContext.getClangModuleLoader());
350352
if (tracker) {
351353
tracker->startTracking();
352354
for (auto fileUnit : mainModule->getFiles()) {
@@ -359,8 +361,6 @@ ModuleDependencyScanner::getMainModuleDependencyInfo(
359361
ScanCompilerInvocation.getSearchPathOptions());
360362
// Fetch some dependency files from clang importer.
361363
std::vector<std::string> clangDependencyFiles;
362-
auto clangImporter =
363-
static_cast<ClangImporter *>(ScanASTContext.getClangModuleLoader());
364364
clangImporter->addClangInvovcationDependencies(clangDependencyFiles);
365365
llvm::for_each(clangDependencyFiles,
366366
[&](std::string &file) { tracker->trackFile(file); });
@@ -372,7 +372,9 @@ ModuleDependencyScanner::getMainModuleDependencyInfo(
372372
return std::make_error_code(std::errc::io_error);
373373
}
374374
rootID = root->getID().toString();
375+
}
375376

377+
if (ScanASTContext.ClangImporterOpts.ClangImporterDirectCC1Scan) {
376378
buildArgs.push_back("-direct-clang-cc1-module-build");
377379
for (auto &arg : clangImporter->getSwiftExplicitModuleDirectCC1Args()) {
378380
buildArgs.push_back("-Xcc");

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,13 +1736,17 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts, ArgList &Args,
17361736
Opts.DisableSourceImport |=
17371737
Args.hasArg(OPT_disable_clangimporter_source_import);
17381738

1739+
Opts.ClangImporterDirectCC1Scan |=
1740+
Args.hasArg(OPT_experimental_clang_importer_direct_cc1_scan);
17391741
// Forward the FrontendOptions to clang importer option so it can be
17401742
// accessed when creating clang module compilation invocation.
17411743
if (CASOpts.EnableCaching) {
17421744
// Only set UseClangIncludeTree when caching is enabled since it is not
17431745
// useful in non-caching context.
17441746
Opts.UseClangIncludeTree |= !Args.hasArg(OPT_no_clang_include_tree);
17451747
Opts.HasClangIncludeTreeRoot |= Args.hasArg(OPT_clang_include_tree_root);
1748+
// Caching requires direct clang import cc1 scanning.
1749+
Opts.ClangImporterDirectCC1Scan = true;
17461750
}
17471751

17481752
return false;

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,9 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
16731673
Feature::LayoutPrespecialization);
16741674
}
16751675

1676+
genericSubInvocation.getClangImporterOptions().ClangImporterDirectCC1Scan =
1677+
clangImporterOpts.ClangImporterDirectCC1Scan;
1678+
16761679
// Validate Clang modules once per-build session flags must be consistent
16771680
// across all module sub-invocations
16781681
if (clangImporterOpts.ValidateModulesOnce) {

lib/Serialization/ScanningLoaders.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ SwiftModuleScanner::scanInterfaceFile(Twine moduleInterfacePath,
174174

175175
// Handle clang arguments. For caching build, all arguments are passed
176176
// with `-direct-clang-cc1-module-build`.
177-
if (Ctx.CASOpts.EnableCaching) {
177+
if (Ctx.ClangImporterOpts.ClangImporterDirectCC1Scan) {
178178
Args.push_back("-direct-clang-cc1-module-build");
179179
auto *importer =
180180
static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -emit-module %t/A.swift -module-name A \
5+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
6+
// RUN: -enable-library-evolution -swift-version 5 \
7+
// RUN: -emit-module-interface-path %t/A.swiftinterface \
8+
// RUN: -o %t/A.swiftmodule
9+
10+
// RUN: %target-swift-frontend -scan-dependencies -o %t/deps.json -I %t \
11+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
12+
// RUN: %t/test.swift -module-name Test -swift-version 5
13+
// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json A | %FileCheck %s --check-prefix CHECK-NO-DIRECT-CC1
14+
// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps.json Test | %FileCheck %s --allow-empty --check-prefix CHECK-NO-DIRECT-CC1
15+
16+
// RUN: %target-swift-frontend -scan-dependencies -o %t/deps2.json -I %t \
17+
// RUN: -disable-implicit-string-processing-module-import -disable-implicit-concurrency-module-import -parse-stdlib \
18+
// RUN: %t/test.swift -module-name Test -swift-version 5 -experimental-clang-importer-direct-cc1-scan
19+
// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps2.json A | %FileCheck %s --check-prefix CHECK-DIRECT-CC1-SCAN
20+
// RUN: %{python} %S/../CAS/Inputs/BuildCommandExtractor.py %t/deps2.json Test | %FileCheck %s --check-prefix CHECK-DIRECT-CC1-SCAN
21+
22+
// CHECK-NO-DIRECT-CC1-NOT: -direct-clang-cc1-module-build
23+
// CHECK-DIRECT-CC1-SCAN: -direct-clang-cc1-module-build
24+
25+
//--- A.swift
26+
func a() {}
27+
28+
//--- test.swift
29+
import A
30+
func test() {}

0 commit comments

Comments
 (0)