Skip to content

Commit 15c0ce3

Browse files
authored
Merge pull request #58932 from DavidGoldman/ignoreclangmoduleindexing
Add `-index-ignore-clang-modules` flag to prevent indexing Clang modules
2 parents 9c72d09 + b939128 commit 15c0ce3

File tree

8 files changed

+93
-46
lines changed

8 files changed

+93
-46
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
@@ -34,6 +34,9 @@ namespace index {
3434
///
3535
/// \param indexStorePath The location to write the indexing data to.
3636
///
37+
/// \param indexClangModules If true, emit index data for imported clang modules
38+
/// (pcms).
39+
///
3740
/// \param indexSystemModules If true, emit index data for imported serialized
3841
/// swift system modules.
3942
///
@@ -48,9 +51,9 @@ namespace index {
4851
///
4952
/// \param pathRemapper Remapper to use for paths in index data.
5053
bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
51-
StringRef indexStorePath, bool indexSystemModules,
52-
bool skipStdlib, bool isDebugCompilation,
53-
StringRef targetTriple,
54+
StringRef indexStorePath, bool indexClangModules,
55+
bool indexSystemModules, bool skipStdlib,
56+
bool isDebugCompilation, StringRef targetTriple,
5457
const DependencyTracker &dependencyTracker,
5558
const PathRemapper &pathRemapper);
5659

@@ -69,6 +72,9 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
6972
///
7073
/// \param indexStorePath The location to write the indexing data to.
7174
///
75+
/// \param indexClangModules If true, emit index data for imported clang modules
76+
/// (pcms).
77+
///
7278
/// \param indexSystemModules If true, emit index data for imported serialized
7379
/// swift system modules.
7480
///
@@ -84,8 +90,9 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
8490
/// \param pathRemapper Remapper to use for paths in index data.
8591
bool indexAndRecord(ModuleDecl *module, ArrayRef<std::string> indexUnitTokens,
8692
StringRef moduleUnitToken, StringRef indexStorePath,
87-
bool indexSystemModules, bool skipStdlib,
88-
bool isDebugCompilation, StringRef targetTriple,
93+
bool indexClangModules, bool indexSystemModules,
94+
bool skipStdlib, bool isDebugCompilation,
95+
StringRef targetTriple,
8996
const DependencyTracker &dependencyTracker,
9097
const PathRemapper &pathRemapper);
9198
// FIXME: indexUnitTokens could be StringRef, but that creates an impedance

include/swift/Option/Options.td

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

1261+
def index_ignore_clang_modules : Flag<["-"], "index-ignore-clang-modules">,
1262+
Flags<[FrontendOption]>,
1263+
HelpText<"Avoid indexing clang modules (pcms)">;
1264+
12611265
def index_ignore_system_modules : Flag<["-"], "index-ignore-system-modules">,
12621266
Flags<[NoInteractiveOption]>,
12631267
HelpText<"Avoid indexing system modules">;

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ ToolChain::constructInvocation(const CompileJobAction &job,
574574
context.Args.AddLastArg(Arguments, options::OPT_index_store_path);
575575
if (!context.Args.hasArg(options::OPT_index_ignore_system_modules))
576576
Arguments.push_back("-index-system-modules");
577+
context.Args.AddLastArg(Arguments, options::OPT_index_ignore_clang_modules);
577578
}
578579

579580
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
@@ -1761,7 +1761,9 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17611761
if (OutputFile.empty())
17621762
OutputFile = PSPs.OutputFilename;
17631763
(void) index::indexAndRecord(PrimarySourceFile, OutputFile,
1764-
opts.IndexStorePath, opts.IndexSystemModules,
1764+
opts.IndexStorePath,
1765+
!opts.IndexIgnoreClangModules,
1766+
opts.IndexSystemModules,
17651767
opts.IndexIgnoreStdlib, isDebugCompilation,
17661768
Invocation.getTargetTriple(),
17671769
*Instance.getDependencyTracker(),
@@ -1776,6 +1778,7 @@ static void emitIndexDataForSourceFile(SourceFile *PrimarySourceFile,
17761778
opts.InputsAndOutputs
17771779
.copyIndexUnitOutputFilenames(),
17781780
moduleToken, opts.IndexStorePath,
1781+
!opts.IndexIgnoreClangModules,
17791782
opts.IndexSystemModules,
17801783
opts.IndexIgnoreStdlib,
17811784
isDebugCompilation,

lib/Index/IndexRecord.cpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ getModuleInfoFromOpaqueModule(clang::index::writer::OpaqueModule mod,
376376
static bool
377377
emitDataForSwiftSerializedModule(ModuleDecl *module,
378378
StringRef indexStorePath,
379+
bool indexClangModules,
379380
bool indexSystemModules,
380381
bool skipStdlib,
381382
StringRef targetTriple,
@@ -387,6 +388,7 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
387388

388389
static void addModuleDependencies(ArrayRef<ImportedModule> imports,
389390
StringRef indexStorePath,
391+
bool indexClangModules,
390392
bool indexSystemModules,
391393
bool skipStdlib,
392394
StringRef targetTriple,
@@ -424,15 +426,16 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
424426
bool withoutUnitName = true;
425427
if (FU->getKind() == FileUnitKind::ClangModule) {
426428
auto clangModUnit = cast<ClangModuleUnit>(LFU);
427-
if (!clangModUnit->isSystemModule() || indexSystemModules) {
428-
withoutUnitName = false;
429-
if (auto clangMod = clangModUnit->getUnderlyingClangModule()) {
430-
moduleName = clangMod->getTopLevelModuleName();
431-
// FIXME: clang's -Rremarks do not seem to go through Swift's
432-
// diagnostic emitter.
429+
bool shouldIndexModule = indexClangModules &&
430+
(!clangModUnit->isSystemModule() || indexSystemModules);
431+
withoutUnitName = !shouldIndexModule;
432+
if (auto clangMod = clangModUnit->getUnderlyingClangModule()) {
433+
moduleName = clangMod->getTopLevelModuleName();
434+
// FIXME: clang's -Rremarks do not seem to go through Swift's
435+
// diagnostic emitter.
436+
if (shouldIndexModule)
433437
clang::index::emitIndexDataForModuleFile(clangMod,
434438
clangCI, unitWriter);
435-
}
436439
}
437440
} else {
438441
// Serialized AST file.
@@ -443,6 +446,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
443446
if (mod->isSystemModule() && indexSystemModules &&
444447
(!skipStdlib || !mod->isStdlibModule())) {
445448
emitDataForSwiftSerializedModule(mod, indexStorePath,
449+
indexClangModules,
446450
indexSystemModules, skipStdlib,
447451
targetTriple, clangCI, diags,
448452
unitWriter,
@@ -472,6 +476,7 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
472476
static bool
473477
emitDataForSwiftSerializedModule(ModuleDecl *module,
474478
StringRef indexStorePath,
479+
bool indexClangModules,
475480
bool indexSystemModules,
476481
bool skipStdlib,
477482
StringRef targetTriple,
@@ -596,9 +601,10 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
596601
module->getImportedModules(imports, {ModuleDecl::ImportFilterKind::Exported,
597602
ModuleDecl::ImportFilterKind::Default});
598603
StringScratchSpace moduleNameScratch;
599-
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
600-
targetTriple, clangCI, diags, unitWriter,
601-
moduleNameScratch, pathRemapper, initialFile);
604+
addModuleDependencies(imports, indexStorePath, indexClangModules,
605+
indexSystemModules, skipStdlib, targetTriple, clangCI,
606+
diags, unitWriter, moduleNameScratch, pathRemapper,
607+
initialFile);
602608

603609
if (unitWriter.write(error)) {
604610
diags.diagnose(SourceLoc(), diag::error_write_index_unit, error);
@@ -610,9 +616,9 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
610616

611617
static bool
612618
recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
613-
StringRef indexStorePath, bool indexSystemModules,
614-
bool skipStdlib, bool isDebugCompilation,
615-
StringRef targetTriple,
619+
StringRef indexStorePath, bool indexClangModules,
620+
bool indexSystemModules, bool skipStdlib,
621+
bool isDebugCompilation, StringRef targetTriple,
616622
ArrayRef<const clang::FileEntry *> fileDependencies,
617623
const clang::CompilerInstance &clangCI,
618624
const PathRemapper &pathRemapper,
@@ -638,9 +644,10 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
638644
ModuleDecl::ImportFilterKind::Default,
639645
ModuleDecl::ImportFilterKind::ImplementationOnly});
640646
StringScratchSpace moduleNameScratch;
641-
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
642-
targetTriple, clangCI, diags, unitWriter,
643-
moduleNameScratch, pathRemapper, primarySourceFile);
647+
addModuleDependencies(imports, indexStorePath, indexClangModules,
648+
indexSystemModules, skipStdlib, targetTriple, clangCI,
649+
diags, unitWriter, moduleNameScratch, pathRemapper,
650+
primarySourceFile);
644651

645652
// File dependencies.
646653
for (auto *F : fileDependencies)
@@ -690,6 +697,7 @@ collectFileDependencies(llvm::SetVector<const clang::FileEntry *> &result,
690697
bool index::indexAndRecord(SourceFile *primarySourceFile,
691698
StringRef indexUnitToken,
692699
StringRef indexStorePath,
700+
bool indexClangModules,
693701
bool indexSystemModules,
694702
bool skipStdlib,
695703
bool isDebugCompilation,
@@ -720,7 +728,8 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
720728
#endif
721729

722730
return recordSourceFileUnit(primarySourceFile, indexUnitToken,
723-
indexStorePath, indexSystemModules, skipStdlib,
731+
indexStorePath, indexClangModules,
732+
indexSystemModules, skipStdlib,
724733
isDebugCompilation, targetTriple,
725734
fileDependencies.getArrayRef(),
726735
clangCI, pathRemapper, diags);
@@ -730,6 +739,7 @@ bool index::indexAndRecord(ModuleDecl *module,
730739
ArrayRef<std::string> indexUnitTokens,
731740
StringRef moduleUnitToken,
732741
StringRef indexStorePath,
742+
bool indexClangModules,
733743
bool indexSystemModules,
734744
bool skipStdlib,
735745
bool isDebugCompilation,
@@ -768,7 +778,8 @@ bool index::indexAndRecord(ModuleDecl *module,
768778
return true;
769779
}
770780
if (recordSourceFileUnit(SF, indexUnitTokens[unitIndex],
771-
indexStorePath, indexSystemModules, skipStdlib,
781+
indexStorePath, indexClangModules,
782+
indexSystemModules, skipStdlib,
772783
isDebugCompilation, targetTriple,
773784
fileDependencies.getArrayRef(),
774785
clangCI, pathRemapper, 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 | ClangModuleC | {{.*}}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)