Skip to content

Commit a245ed2

Browse files
committed
Add -index-ignore-clang-modules flag
This flag avoids indexing import clang modules (pcms), behaving similar to `-index-ignore-system-modules` except for PCMs.
1 parent d34d88d commit a245ed2

File tree

8 files changed

+86
-40
lines changed

8 files changed

+86
-40
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class FrontendOptions {
9999
/// Emit index data for imported serialized swift system modules.
100100
bool IndexSystemModules = false;
101101

102+
/// Avoid emitting index data for imported clang modules (pcms).
103+
bool IndexIgnoreClangModules = false;
104+
102105
/// If indexing system modules, don't index the stdlib.
103106
bool IndexIgnoreStdlib = false;
104107

include/swift/Index/IndexRecord.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ namespace index {
3333
///
3434
/// \param indexStorePath The location to write the indexing data to.
3535
///
36+
/// \param indexClangModules If true, emit index data for imported clang modules
37+
/// (pcms).
38+
///
3639
/// \param indexSystemModules If true, emit index data for imported serialized
3740
/// swift system modules.
3841
///
@@ -45,9 +48,9 @@ namespace index {
4548
///
4649
/// \param dependencyTracker The set of dependencies seen while building.
4750
bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
48-
StringRef indexStorePath, bool indexSystemModules,
49-
bool skipStdlib, bool isDebugCompilation,
50-
StringRef targetTriple,
51+
StringRef indexStorePath, bool indexClangModules,
52+
bool indexSystemModules, bool skipStdlib,
53+
bool isDebugCompilation, StringRef targetTriple,
5154
const DependencyTracker &dependencyTracker);
5255

5356
/// Index the given module and store the results to \p indexStorePath.
@@ -65,6 +68,9 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
6568
///
6669
/// \param indexStorePath The location to write the indexing data to.
6770
///
71+
/// \param indexClangModules If true, emit index data for imported clang modules
72+
/// (pcms).
73+
///
6874
/// \param indexSystemModules If true, emit index data for imported serialized
6975
/// swift system modules.
7076
///
@@ -78,8 +84,9 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
7884
/// \param dependencyTracker The set of dependencies seen while building.
7985
bool indexAndRecord(ModuleDecl *module, ArrayRef<std::string> indexUnitTokens,
8086
StringRef moduleUnitToken, StringRef indexStorePath,
81-
bool indexSystemModules, bool skipStdlib,
82-
bool isDebugCompilation, StringRef targetTriple,
87+
bool indexClangModules, bool indexSystemModules,
88+
bool skipStdlib, bool isDebugCompilation,
89+
StringRef targetTriple,
8390
const DependencyTracker &dependencyTracker);
8491
// FIXME: indexUnitTokens could be StringRef, but that creates an impedance
8592
// mismatch in the caller.

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,10 @@ def index_unit_output_path : Separate<["-"], "index-unit-output-path">,
12501250
Flags<[FrontendOption, ArgumentIsPath]>, MetaVarName<"<path>">,
12511251
HelpText<"Use <path> as the output path in the produced index data.">;
12521252

1253+
def index_ignore_clang_modules : Flag<["-"], "index-ignore-clang-modules">,
1254+
Flags<[FrontendOption]>,
1255+
HelpText<"Avoid indexing clang modules (pcms)">;
1256+
12531257
def index_ignore_system_modules : Flag<["-"], "index-ignore-system-modules">,
12541258
Flags<[NoInteractiveOption]>,
12551259
HelpText<"Avoid indexing system modules">;

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ ToolChain::constructInvocation(const CompileJobAction &job,
572572
context.Args.AddLastArg(Arguments, options::OPT_index_store_path);
573573
if (!context.Args.hasArg(options::OPT_index_ignore_system_modules))
574574
Arguments.push_back("-index-system-modules");
575+
context.Args.AddLastArg(Arguments, options::OPT_index_ignore_clang_modules);
575576
}
576577

577578
if (context.Args.hasArg(options::OPT_debug_info_store_invocation) ||

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ bool ArgsToFrontendOptionsConverter::convert(
6969
if (const Arg *A = Args.getLastArg(OPT_bridging_header_directory_for_print)) {
7070
Opts.BridgingHeaderDirForPrint = A->getValue();
7171
}
72+
Opts.IndexIgnoreClangModules |= Args.hasArg(OPT_index_ignore_clang_modules);
7273
Opts.IndexSystemModules |= Args.hasArg(OPT_index_system_modules);
7374
Opts.IndexIgnoreStdlib |= Args.hasArg(OPT_index_ignore_stdlib);
7475

lib/FrontendTool/FrontendTool.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,9 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17601760
if (OutputFile.empty())
17611761
OutputFile = PSPs.OutputFilename;
17621762
(void) index::indexAndRecord(PrimarySourceFile, OutputFile,
1763-
opts.IndexStorePath, opts.IndexSystemModules,
1763+
opts.IndexStorePath,
1764+
!opts.IndexIgnoreClangModules,
1765+
opts.IndexSystemModules,
17641766
opts.IndexIgnoreStdlib, isDebugCompilation,
17651767
Invocation.getTargetTriple(),
17661768
*Instance.getDependencyTracker());
@@ -1774,6 +1776,7 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17741776
opts.InputsAndOutputs
17751777
.copyIndexUnitOutputFilenames(),
17761778
moduleToken, opts.IndexStorePath,
1779+
!opts.IndexIgnoreClangModules,
17771780
opts.IndexSystemModules,
17781781
opts.IndexIgnoreStdlib,
17791782
isDebugCompilation,

lib/Index/IndexRecord.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ getModuleInfoFromOpaqueModule(clang::index::writer::OpaqueModule mod,
375375
static bool
376376
emitDataForSwiftSerializedModule(ModuleDecl *module,
377377
StringRef indexStorePath,
378+
bool indexClangModules,
378379
bool indexSystemModules,
379380
bool skipStdlib,
380381
StringRef targetTriple,
@@ -385,6 +386,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
385386

386387
static void addModuleDependencies(ArrayRef<ImportedModule> imports,
387388
StringRef indexStorePath,
389+
bool indexClangModules,
388390
bool indexSystemModules,
389391
bool skipStdlib,
390392
StringRef targetTriple,
@@ -421,7 +423,8 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
421423
bool withoutUnitName = true;
422424
if (FU->getKind() == FileUnitKind::ClangModule) {
423425
auto clangModUnit = cast<ClangModuleUnit>(LFU);
424-
if (!clangModUnit->isSystemModule() || indexSystemModules) {
426+
if ((!clangModUnit->isSystemModule() || indexSystemModules)
427+
&& indexClangModules) {
425428
withoutUnitName = false;
426429
if (auto clangMod = clangModUnit->getUnderlyingClangModule()) {
427430
moduleName = clangMod->getTopLevelModuleName();
@@ -440,6 +443,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
440443
if (mod->isSystemModule() && indexSystemModules &&
441444
(!skipStdlib || !mod->isStdlibModule())) {
442445
emitDataForSwiftSerializedModule(mod, indexStorePath,
446+
indexClangModules,
443447
indexSystemModules, skipStdlib,
444448
targetTriple, clangCI, diags,
445449
unitWriter, initialFile);
@@ -467,6 +471,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
467471
static bool
468472
emitDataForSwiftSerializedModule(ModuleDecl *module,
469473
StringRef indexStorePath,
474+
bool indexClangModules,
470475
bool indexSystemModules,
471476
bool skipStdlib,
472477
StringRef targetTriple,
@@ -588,9 +593,9 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
588593
module->getImportedModules(imports, {ModuleDecl::ImportFilterKind::Exported,
589594
ModuleDecl::ImportFilterKind::Default});
590595
StringScratchSpace moduleNameScratch;
591-
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
592-
targetTriple, clangCI, diags, unitWriter,
593-
moduleNameScratch, initialFile);
596+
addModuleDependencies(imports, indexStorePath, indexClangModules,
597+
indexSystemModules, skipStdlib, targetTriple, clangCI,
598+
diags, unitWriter, moduleNameScratch, initialFile);
594599

595600
if (unitWriter.write(error)) {
596601
diags.diagnose(SourceLoc(), diag::error_write_index_unit, error);
@@ -602,9 +607,9 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
602607

603608
static bool
604609
recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
605-
StringRef indexStorePath, bool indexSystemModules,
606-
bool skipStdlib, bool isDebugCompilation,
607-
StringRef targetTriple,
610+
StringRef indexStorePath, bool indexClangModules,
611+
bool indexSystemModules, bool skipStdlib,
612+
bool isDebugCompilation, StringRef targetTriple,
608613
ArrayRef<const clang::FileEntry *> fileDependencies,
609614
const clang::CompilerInstance &clangCI,
610615
DiagnosticEngine &diags) {
@@ -628,9 +633,10 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
628633
ModuleDecl::ImportFilterKind::Default,
629634
ModuleDecl::ImportFilterKind::ImplementationOnly});
630635
StringScratchSpace moduleNameScratch;
631-
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
632-
targetTriple, clangCI, diags, unitWriter,
633-
moduleNameScratch, primarySourceFile);
636+
addModuleDependencies(imports, indexStorePath, indexClangModules,
637+
indexSystemModules, skipStdlib, targetTriple, clangCI,
638+
diags, unitWriter, moduleNameScratch,
639+
primarySourceFile);
634640

635641
// File dependencies.
636642
for (auto *F : fileDependencies)
@@ -680,6 +686,7 @@ collectFileDependencies(llvm::SetVector<const clang::FileEntry *> &result,
680686
bool index::indexAndRecord(SourceFile *primarySourceFile,
681687
StringRef indexUnitToken,
682688
StringRef indexStorePath,
689+
bool indexClangModules,
683690
bool indexSystemModules,
684691
bool skipStdlib,
685692
bool isDebugCompilation,
@@ -709,7 +716,8 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
709716
#endif
710717

711718
return recordSourceFileUnit(primarySourceFile, indexUnitToken,
712-
indexStorePath, indexSystemModules, skipStdlib,
719+
indexStorePath, indexClangModules,
720+
indexSystemModules, skipStdlib,
713721
isDebugCompilation, targetTriple,
714722
fileDependencies.getArrayRef(),
715723
clangCI, diags);
@@ -719,6 +727,7 @@ bool index::indexAndRecord(ModuleDecl *module,
719727
ArrayRef<std::string> indexUnitTokens,
720728
StringRef moduleUnitToken,
721729
StringRef indexStorePath,
730+
bool indexClangModules,
722731
bool indexSystemModules,
723732
bool skipStdlib,
724733
bool isDebugCompilation,
@@ -756,7 +765,8 @@ bool index::indexAndRecord(ModuleDecl *module,
756765
return true;
757766
}
758767
if (recordSourceFileUnit(SF, indexUnitTokens[unitIndex],
759-
indexStorePath, indexSystemModules, skipStdlib,
768+
indexStorePath, indexClangModules,
769+
indexSystemModules, skipStdlib,
760770
isDebugCompilation, targetTriple,
761771
fileDependencies.getArrayRef(),
762772
clangCI, diags))

test/Index/Store/unit-pcm-dependency.swift

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
// RUN: rm -rf %t
22
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -index-store-path %t/idx -primary-file %s -o %t/s1.o -I %S/Inputs -typecheck -module-cache-path %t/mcp -enable-objc-interop
3-
// RUN: c-index-test core -print-unit %t/idx | %FileCheck %s -check-prefix=FILE1
3+
// RUN: c-index-test core -print-unit %t/idx | %FileCheck %s -check-prefixes=FILE1,FILE1-PCM
44

55
// If the module cache already exists, the pcm gets indexed.
66
// RUN: rm -rf %t/idx
77
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -index-store-path %t/idx -primary-file %s -o %t/s1.o -I %S/Inputs -typecheck -module-cache-path %t/mcp -enable-objc-interop
8-
// RUN: c-index-test core -print-unit %t/idx | %FileCheck %s -check-prefix=FILE1
8+
// RUN: c-index-test core -print-unit %t/idx | %FileCheck %s -check-prefixes=FILE1,FILE1-PCM
99

1010
// FIXME: index the bridging header!
1111

1212
// RUN: %empty-directory(%t)
1313
// RUN: echo 'import ClangModuleA' > %t/s2.swift
1414
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -index-store-path %t/idx %s %t/s2.swift -o %t/s1.o -o %t/s2.o -I %S/Inputs -c -emit-module -module-name main -emit-module-path %t/main.swiftmodule -module-cache-path %t/mcp -enable-objc-interop
1515
// RUN: c-index-test core -print-unit %t/idx > %t/both.txt
16-
// RUN: %FileCheck %s -check-prefix=FILE1 < %t/both.txt
17-
// RUN: %FileCheck %s -check-prefix=FILE2 < %t/both.txt
16+
// RUN: %FileCheck %s -check-prefixes=FILE1,FILE1-PCM < %t/both.txt
17+
// RUN: %FileCheck %s -check-prefixes=FILE2,FILE2-PCM < %t/both.txt
18+
19+
20+
// Test -index-ignore-clang-modules.
21+
22+
// RUN: %empty-directory(%t)
23+
// RUN: echo 'import ClangModuleA' > %t/s2.swift
24+
// RUN: %target-swift-frontend -disable-implicit-concurrency-module-import -index-store-path %t/idx -index-ignore-clang-modules %s %t/s2.swift -o %t/s1.o -o %t/s2.o -I %S/Inputs -c -emit-module -module-name main -emit-module-path %t/main.swiftmodule -module-cache-path %t/mcp -enable-objc-interop
25+
// RUN: c-index-test core -print-unit %t/idx > %t/both.txt
26+
// RUN: %FileCheck %s -check-prefixes=FILE1,FILE1-IGNORE < %t/both.txt --dump-input-filter all
27+
// RUN: %FileCheck %s -check-prefixes=FILE2,FILE2-IGNORE < %t/both.txt
1828

1929

2030
import ClangModuleB
@@ -26,22 +36,26 @@ func test() {
2636
funcB()
2737
}
2838

29-
// FILE1: ClangModuleA-
30-
// FILE1: --------
31-
// FILE1: is-system: 0
32-
// FILE1: has-main: 0
33-
// FILE1: DEPEND START
34-
// FILE1: Record | user | {{.*}}ClangModuleA.h | ClangModuleA.h-
35-
// FILE1: DEPEND END
39+
// FILE1-IGNORE-NOT: ClangModuleA-
3640

37-
// FILE1: ClangModuleB-
38-
// FILE1: --------
39-
// FILE1: is-system: 0
40-
// FILE1: has-main: 0
41-
// FILE1: DEPEND START
42-
// FILE1: Unit | user | ClangModuleA | {{.*}}ClangModuleA-{{.*}}.pcm | ClangModuleA-{{.*}}.pcm-
43-
// FILE1: Record | user | {{.*}}ClangModuleB.h | ClangModuleB.h-
44-
// FILE1: DEPEND END
41+
// FILE1-PCM: ClangModuleA-
42+
// FILE1-PCM: --------
43+
// FILE1-PCM: is-system: 0
44+
// FILE1-PCM: has-main: 0
45+
// FILE1-PCM: DEPEND START
46+
// FILE1-PCM: Record | user | {{.*}}ClangModuleA.h | ClangModuleA.h-
47+
// FILE1-PCM: DEPEND END
48+
49+
// FILE1-IGNORE-NOT: ClangModuleB-
50+
51+
// FILE1-PCM: ClangModuleB-
52+
// FILE1-PCM: --------
53+
// FILE1-PCM: is-system: 0
54+
// FILE1-PCM: has-main: 0
55+
// FILE1-PCM: DEPEND START
56+
// FILE1-PCM: Unit | user | ClangModuleA | {{.*}}ClangModuleA-{{.*}}.pcm | ClangModuleA-{{.*}}.pcm-
57+
// FILE1-PCM: Record | user | {{.*}}ClangModuleB.h | ClangModuleB.h-
58+
// FILE1-PCM: DEPEND END
4559

4660
// FILE1: s1.o-
4761
// FILE1: --------
@@ -51,8 +65,10 @@ func test() {
5165
// FILE1-NOT: Unit |{{.*}}ClangModuleA
5266
// FILE1: Unit | system | Swift | {{.*}}Swift.swiftmodule
5367
// FILE1-NOT: Unit |{{.*}}ClangModuleA
54-
// FILE1: Unit | user | ClangModuleB | {{.*}}ClangModuleB-{{[A-Z0-9]*}}.pcm | ClangModuleB-{{[A-Z0-9]*}}.pcm-
55-
// FILE1: Unit | user | ClangModuleC | {{.*}}ClangModuleC-{{[A-Z0-9]*}}.pcm | ClangModuleC-{{[A-Z0-9]*}}.pcm-
68+
// FILE1-PCM: Unit | user | ClangModuleB | {{.*}}ClangModuleB-{{[A-Z0-9]*}}.pcm | ClangModuleB-{{[A-Z0-9]*}}.pcm-
69+
// FILE1-PCM: Unit | user | ClangModuleC | {{.*}}ClangModuleC-{{[A-Z0-9]*}}.pcm | ClangModuleC-{{[A-Z0-9]*}}.pcm-
70+
// FILE1-IGNORE: Unit | user | ClangModuleB | {{.*}}ClangModuleB-{{[A-Z0-9]*}}.pcm
71+
// FILE1-IGNORE: Unit | user | Sub1 | {{.*}}ClangModuleC-{{[A-Z0-9]*}}.pcm
5672
// FILE1-NOT: Unit |{{.*}}ClangModuleA
5773
// FILE1: Record | user | {{.*}}unit-pcm-dependency.swift | unit-pcm-dependency.swift-
5874
// FILE1-NOT: Unit |{{.*}}ClangModuleA
@@ -71,7 +87,8 @@ func test() {
7187
// FILE2: Unit | system | Swift | {{.*}}Swift.swiftmodule
7288
// FILE2-NOT: Unit |{{.*}}ClangModuleB
7389
// FILE2-NOT: Record
74-
// FILE2: Unit | user | ClangModuleA | {{.*}}ClangModuleA-{{[A-Z0-9]*}}.pcm | ClangModuleA-{{[A-Z0-9]*}}.pcm-
90+
// FILE2-PCM: Unit | user | ClangModuleA | {{.*}}ClangModuleA-{{[A-Z0-9]*}}.pcm | ClangModuleA-{{[A-Z0-9]*}}.pcm-
91+
// FILE2-IGNORE: Unit | user | ClangModuleA | {{.*}}ClangModuleA-{{[A-Z0-9]*}}.pcm
7592
// FILE2: Record | user | {{.*}}s2.swift | s2.swift-
7693
// FILE2-NOT: Unit |{{.*}}ClangModuleB
7794
// FILE2-NOT: Record

0 commit comments

Comments
 (0)