Skip to content

Commit cb17ea8

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 fb3c268 commit cb17ea8

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

include/swift/Basic/LangOptions.h

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

972+
/// Whether the dependency scanner should construct all swift-frontend
973+
/// invocations directly from clang cc1 args.
974+
bool ClangImporterDirectCC1Scan = false;
975+
972976
/// Return a hash code of any components from these options that should
973977
/// contribute to a Swift Bridging PCH hash.
974978
llvm::hash_code getPCHHashComponents() const {

include/swift/Option/Options.td

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

1897+
def experimental_clang_importer_direct_cc1_scan:
1898+
Flag<["-"], "experimental-clang-importer-direct-cc1-scan">,
1899+
Flags<[FrontendOption, NewDriverOnlyOption, HelpHidden]>,
1900+
HelpText<"Enables swift driver to construct swift-frontend invocations using -direct-clang-cc1-module-build">;
1901+
18971902
def cache_compile_job: Flag<["-"], "cache-compile-job">,
18981903
Flags<[FrontendOption, NewDriverOnlyOption]>,
18991904
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
@@ -1709,13 +1709,17 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts, ArgList &Args,
17091709
Opts.DisableSourceImport |=
17101710
Args.hasArg(OPT_disable_clangimporter_source_import);
17111711

1712+
Opts.ClangImporterDirectCC1Scan |=
1713+
Args.hasArg(OPT_experimental_clang_importer_direct_cc1_scan);
17121714
// Forward the FrontendOptions to clang importer option so it can be
17131715
// accessed when creating clang module compilation invocation.
17141716
if (CASOpts.EnableCaching) {
17151717
// Only set UseClangIncludeTree when caching is enabled since it is not
17161718
// useful in non-caching context.
17171719
Opts.UseClangIncludeTree |= !Args.hasArg(OPT_no_clang_include_tree);
17181720
Opts.HasClangIncludeTreeRoot |= Args.hasArg(OPT_clang_include_tree_root);
1721+
// Caching requires direct clang import cc1 scanning.
1722+
Opts.ClangImporterDirectCC1Scan = true;
17191723
}
17201724

17211725
// If in direct clang cc1 module build mode, return early.

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,8 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
16771677

16781678
genericSubInvocation.getClangImporterOptions().DirectClangCC1ModuleBuild =
16791679
clangImporterOpts.DirectClangCC1ModuleBuild;
1680+
genericSubInvocation.getClangImporterOptions().ClangImporterDirectCC1Scan =
1681+
clangImporterOpts.ClangImporterDirectCC1Scan;
16801682

16811683
// Validate Clang modules once per-build session flags must be consistent
16821684
// across all module sub-invocations

lib/Serialization/ScanningLoaders.cpp

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

161161
// Handle clang arguments. For caching build, all arguments are passed
162162
// with `-direct-clang-cc1-module-build`.
163-
if (Ctx.CASOpts.EnableCaching) {
163+
if (Ctx.ClangImporterOpts.ClangImporterDirectCC1Scan) {
164164
Args.push_back("-direct-clang-cc1-module-build");
165165
auto *importer =
166166
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)