Skip to content

Commit 4ccc95f

Browse files
author
Nathan Hawes
authored
Merge pull request swiftlang#30636 from nathawes/cross-import-indexing
[Index] Add index support for cross import overlays.
2 parents 1985cfd + 45dc14a commit 4ccc95f

File tree

21 files changed

+282
-44
lines changed

21 files changed

+282
-44
lines changed

include/swift/Frontend/FrontendOptions.h

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

85+
/// If indexing system modules, don't index the stdlib.
86+
bool IndexIgnoreStdlib = false;
87+
8588
/// The module for which we should verify all of the generic signatures.
8689
std::string VerifyGenericSignaturesInModule;
8790

include/swift/Index/IndexRecord.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ namespace index {
3636
/// \param indexSystemModules If true, emit index data for imported serialized
3737
/// swift system modules.
3838
///
39+
/// \param skipStdlib If indexing system modules, don't index the standard
40+
/// library.
41+
///
3942
/// \param isDebugCompilation true for non-optimized compiler invocation.
4043
///
4144
/// \param targetTriple The target for this compilation.
4245
///
4346
/// \param dependencyTracker The set of dependencies seen while building.
4447
bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
4548
StringRef indexStorePath, bool indexSystemModules,
46-
bool isDebugCompilation, StringRef targetTriple,
49+
bool skipStdlib, bool isDebugCompilation,
50+
StringRef targetTriple,
4751
const DependencyTracker &dependencyTracker);
4852

4953
/// Index the given module and store the results to \p indexStorePath.
@@ -64,15 +68,18 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
6468
/// \param indexSystemModules If true, emit index data for imported serialized
6569
/// swift system modules.
6670
///
71+
/// \param skipStdlib If indexing system modules, don't index the standard
72+
/// library.
73+
///
6774
/// \param isDebugCompilation true for non-optimized compiler invocation.
6875
///
6976
/// \param targetTriple The target for this compilation.
7077
///
7178
/// \param dependencyTracker The set of dependencies seen while building.
7279
bool indexAndRecord(ModuleDecl *module, ArrayRef<std::string> indexUnitTokens,
7380
StringRef moduleUnitToken, StringRef indexStorePath,
74-
bool indexSystemModules, bool isDebugCompilation,
75-
StringRef targetTriple,
81+
bool indexSystemModules, bool skipStdlib,
82+
bool isDebugCompilation, StringRef targetTriple,
7683
const DependencyTracker &dependencyTracker);
7784
// FIXME: indexUnitTokens could be StringRef, but that creates an impedance
7885
// mismatch in the caller.

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ def external_pass_pipeline_filename : Separate<["-"], "external-pass-pipeline-fi
604604
def index_system_modules : Flag<["-"], "index-system-modules">,
605605
HelpText<"Emit index data for imported serialized swift system modules">;
606606

607+
def index_ignore_stdlib :
608+
Flag<["-"], "index-ignore-stdlib">,
609+
HelpText<"Avoid emitting index data for the standard library.">;
610+
607611
def dump_interface_hash : Flag<["-"], "dump-interface-hash">,
608612
HelpText<"Parse input file(s) and dump interface token hash(es)">,
609613
ModeOpt;

lib/AST/Module.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,10 +2105,15 @@ SourceFile::getModuleShadowedBySeparatelyImportedOverlay(const ModuleDecl *overl
21052105
}
21062106
}
21072107
}
2108-
auto i = separatelyImportedOverlaysReversed.find(overlay);
2109-
return i != separatelyImportedOverlaysReversed.end()
2110-
? std::get<1>(*i)
2111-
: nullptr;
2108+
2109+
ModuleDecl *underlying = const_cast<ModuleDecl *>(overlay);
2110+
while (underlying->getNameStr().startswith("_")) {
2111+
auto next = separatelyImportedOverlaysReversed.find(underlying);
2112+
if (next == separatelyImportedOverlaysReversed.end())
2113+
return nullptr;
2114+
underlying = std::get<1>(*next);
2115+
}
2116+
return underlying;
21122117
};
21132118

21142119
void ModuleDecl::clearLookupCache() {

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ bool ArgsToFrontendOptionsConverter::convert(
6767
Opts.BridgingHeaderDirForPrint = A->getValue();
6868
}
6969
Opts.IndexSystemModules |= Args.hasArg(OPT_index_system_modules);
70+
Opts.IndexIgnoreStdlib |= Args.hasArg(OPT_index_ignore_stdlib);
7071

7172
Opts.EmitVerboseSIL |= Args.hasArg(OPT_emit_verbose_sil);
7273
Opts.EmitSortedSIL |= Args.hasArg(OPT_emit_sorted_sil);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,7 +1729,8 @@ static bool emitIndexDataIfNeeded(SourceFile *PrimarySourceFile,
17291729
PrimarySourceFile->getFilename());
17301730
if (index::indexAndRecord(PrimarySourceFile, PSPs.OutputFilename,
17311731
opts.IndexStorePath, opts.IndexSystemModules,
1732-
isDebugCompilation, Invocation.getTargetTriple(),
1732+
opts.IndexIgnoreStdlib, isDebugCompilation,
1733+
Invocation.getTargetTriple(),
17331734
*Instance.getDependencyTracker())) {
17341735
return true;
17351736
}
@@ -1741,7 +1742,7 @@ static bool emitIndexDataIfNeeded(SourceFile *PrimarySourceFile,
17411742

17421743
if (index::indexAndRecord(Instance.getMainModule(), opts.InputsAndOutputs.copyOutputFilenames(),
17431744
moduleToken, opts.IndexStorePath,
1744-
opts.IndexSystemModules,
1745+
opts.IndexSystemModules, opts.IndexIgnoreStdlib,
17451746
isDebugCompilation, Invocation.getTargetTriple(),
17461747
*Instance.getDependencyTracker())) {
17471748
return true;

lib/IDE/CodeCompletion.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,9 @@ void CodeCompletionResultBuilder::setAssociatedDecl(const Decl *D,
781781
// If this is an underscored cross-import overlay, map it to its underlying
782782
// module instead.
783783
if (SF) {
784-
while (MD->getNameStr().startswith("_")) {
785-
auto *Underlying = SF->getModuleShadowedBySeparatelyImportedOverlay(MD);
786-
if (!Underlying)
787-
break;
784+
auto *Underlying = SF->getModuleShadowedBySeparatelyImportedOverlay(MD);
785+
if (Underlying)
788786
MD = Underlying;
789-
}
790787
}
791788
CurrentModule = MD;
792789
}

lib/Index/Index.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ class SourceFileOrModule {
129129
ImportFilter |= ModuleDecl::ImportFilterKind::Public;
130130
ImportFilter |= ModuleDecl::ImportFilterKind::Private;
131131
ImportFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
132-
// FIXME: ImportFilterKind::ShadowedBySeparateOverlay?
133132

134133
if (auto *SF = SFOrMod.dyn_cast<SourceFile *>()) {
135134
SF->getImportedModules(Modules, ImportFilter);
@@ -147,6 +146,7 @@ struct IndexedWitness {
147146
class IndexSwiftASTWalker : public SourceEntityWalker {
148147
IndexDataConsumer &IdxConsumer;
149148
SourceManager &SrcMgr;
149+
SourceFile *InitialFile; ///< The SoureFile we started walking from, if any.
150150
unsigned BufferID;
151151
bool enableWarnings;
152152

@@ -282,8 +282,9 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
282282

283283
public:
284284
IndexSwiftASTWalker(IndexDataConsumer &IdxConsumer, ASTContext &Ctx,
285-
unsigned BufferID = -1)
286-
: IdxConsumer(IdxConsumer), SrcMgr(Ctx.SourceMgr), BufferID(BufferID),
285+
SourceFile *SF = nullptr)
286+
: IdxConsumer(IdxConsumer), SrcMgr(Ctx.SourceMgr), InitialFile(SF),
287+
BufferID(SF ? SF->getBufferID().getValueOr(-1) : -1),
287288
enableWarnings(IdxConsumer.enableWarnings()) {}
288289

289290
~IndexSwiftASTWalker() override {
@@ -755,7 +756,18 @@ bool IndexSwiftASTWalker::visitImports(
755756
continue;
756757
bool IsClangModule = *IsClangModuleOpt;
757758

758-
if (!IdxConsumer.startDependency(Mod->getName().str(), Path, IsClangModule,
759+
StringRef ModuleName = Mod->getNameStr();
760+
761+
// If this module is an underscored cross-import overlay, use the name
762+
// of the underlying module instead.
763+
if (InitialFile) {
764+
ModuleDecl *Underlying =
765+
InitialFile->getModuleShadowedBySeparatelyImportedOverlay(Mod);
766+
if (Underlying)
767+
ModuleName = Underlying->getNameStr();
768+
}
769+
770+
if (!IdxConsumer.startDependency(ModuleName, Path, IsClangModule,
759771
Mod->isSystemModule()))
760772
return false;
761773
if (!IsClangModule)
@@ -1584,16 +1596,15 @@ void IndexSwiftASTWalker::collectRecursiveModuleImports(
15841596

15851597
void index::indexDeclContext(DeclContext *DC, IndexDataConsumer &consumer) {
15861598
assert(DC);
1587-
unsigned bufferId = DC->getParentSourceFile()->getBufferID().getValue();
1588-
IndexSwiftASTWalker walker(consumer, DC->getASTContext(), bufferId);
1599+
SourceFile *SF = DC->getParentSourceFile();
1600+
IndexSwiftASTWalker walker(consumer, DC->getASTContext(), SF);
15891601
walker.visitDeclContext(DC);
15901602
consumer.finish();
15911603
}
15921604

15931605
void index::indexSourceFile(SourceFile *SF, IndexDataConsumer &consumer) {
15941606
assert(SF);
1595-
unsigned bufferID = SF->getBufferID().getValue();
1596-
IndexSwiftASTWalker walker(consumer, SF->getASTContext(), bufferID);
1607+
IndexSwiftASTWalker walker(consumer, SF->getASTContext(), SF);
15971608
walker.visitModule(*SF->getParentModule());
15981609
consumer.finish();
15991610
}

lib/Index/IndexRecord.cpp

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -372,23 +372,40 @@ getModuleInfoFromOpaqueModule(clang::index::writer::OpaqueModule mod,
372372
return info;
373373
}
374374

375+
/// Gets the module name of the given module, or its underlying module if it's a
376+
/// cross import overlay implicitly imported in \p initialFile.
377+
static StringRef getUnderlyingModuleName(ModuleDecl *module,
378+
SourceFile *initialFile) {
379+
if (initialFile) {
380+
ModuleDecl *underlying =
381+
initialFile->getModuleShadowedBySeparatelyImportedOverlay(module);
382+
if (underlying)
383+
module = underlying;
384+
}
385+
return module->getNameStr();
386+
}
387+
375388
static bool
376389
emitDataForSwiftSerializedModule(ModuleDecl *module,
377390
StringRef indexStorePath,
378391
bool indexSystemModules,
392+
bool skipStdlib,
379393
StringRef targetTriple,
380394
const clang::CompilerInstance &clangCI,
381395
DiagnosticEngine &diags,
382-
IndexUnitWriter &parentUnitWriter);
396+
IndexUnitWriter &parentUnitWriter,
397+
SourceFile *initialFile);
383398

384399
static void addModuleDependencies(ArrayRef<ModuleDecl::ImportedModule> imports,
385400
StringRef indexStorePath,
386401
bool indexSystemModules,
402+
bool skipStdlib,
387403
StringRef targetTriple,
388404
const clang::CompilerInstance &clangCI,
389405
DiagnosticEngine &diags,
390406
IndexUnitWriter &unitWriter,
391-
StringScratchSpace &moduleNameScratch) {
407+
StringScratchSpace &moduleNameScratch,
408+
SourceFile *initialFile = nullptr) {
392409
auto &fileMgr = clangCI.getFileManager();
393410

394411
for (auto &import : imports) {
@@ -426,13 +443,18 @@ static void addModuleDependencies(ArrayRef<ModuleDecl::ImportedModule> imports,
426443
// We don't officially support binary swift modules, so normally
427444
// the index data for user modules would get generated while
428445
// building them.
429-
if (mod->isSystemModule() && indexSystemModules) {
446+
if (mod->isSystemModule() && indexSystemModules &&
447+
(!skipStdlib || !mod->isStdlibModule())) {
430448
emitDataForSwiftSerializedModule(mod, indexStorePath,
431-
indexSystemModules,
449+
indexSystemModules, skipStdlib,
432450
targetTriple, clangCI, diags,
433-
unitWriter);
451+
unitWriter, initialFile);
434452
withoutUnitName = false;
435453
}
454+
455+
// If this is a cross-import overlay, make sure we use the name of
456+
// the underlying module instead.
457+
moduleName = getUnderlyingModuleName(mod, initialFile);
436458
}
437459
clang::index::writer::OpaqueModule opaqMod =
438460
moduleNameScratch.createString(moduleName);
@@ -451,12 +473,16 @@ static bool
451473
emitDataForSwiftSerializedModule(ModuleDecl *module,
452474
StringRef indexStorePath,
453475
bool indexSystemModules,
476+
bool skipStdlib,
454477
StringRef targetTriple,
455478
const clang::CompilerInstance &clangCI,
456479
DiagnosticEngine &diags,
457-
IndexUnitWriter &parentUnitWriter) {
480+
IndexUnitWriter &parentUnitWriter,
481+
SourceFile *initialFile) {
458482
StringRef filename = module->getModuleFilename();
459-
std::string moduleName = module->getNameStr().str();
483+
// If this is a cross-import overlay, make sure we use the name of the
484+
// underlying module instead.
485+
std::string moduleName = getUnderlyingModuleName(module, initialFile);
460486

461487
std::string error;
462488
auto isUptodateOpt = parentUnitWriter.isUnitUpToDateForOutputFile(/*FilePath=*/filename,
@@ -565,12 +591,12 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
565591
ModuleDecl::ImportFilter importFilter;
566592
importFilter |= ModuleDecl::ImportFilterKind::Public;
567593
importFilter |= ModuleDecl::ImportFilterKind::Private;
568-
// FIXME: ImportFilterKind::ShadowedBySeparateOverlay?
569594
SmallVector<ModuleDecl::ImportedModule, 8> imports;
570595
module->getImportedModules(imports, importFilter);
571596
StringScratchSpace moduleNameScratch;
572-
addModuleDependencies(imports, indexStorePath, indexSystemModules,
573-
targetTriple, clangCI, diags, unitWriter, moduleNameScratch);
597+
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
598+
targetTriple, clangCI, diags, unitWriter,
599+
moduleNameScratch, initialFile);
574600

575601
if (unitWriter.write(error)) {
576602
diags.diagnose(SourceLoc(), diag::error_write_index_unit, error);
@@ -583,7 +609,8 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
583609
static bool
584610
recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
585611
StringRef indexStorePath, bool indexSystemModules,
586-
bool isDebugCompilation, StringRef targetTriple,
612+
bool skipStdlib, bool isDebugCompilation,
613+
StringRef targetTriple,
587614
ArrayRef<const clang::FileEntry *> fileDependencies,
588615
const clang::CompilerInstance &clangCI,
589616
DiagnosticEngine &diags) {
@@ -606,12 +633,13 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
606633
importFilter |= ModuleDecl::ImportFilterKind::Public;
607634
importFilter |= ModuleDecl::ImportFilterKind::Private;
608635
importFilter |= ModuleDecl::ImportFilterKind::ImplementationOnly;
609-
// FIXME: ImportFilterKind::ShadowedBySeparateOverlay?
636+
610637
SmallVector<ModuleDecl::ImportedModule, 8> imports;
611638
primarySourceFile->getImportedModules(imports, importFilter);
612639
StringScratchSpace moduleNameScratch;
613-
addModuleDependencies(imports, indexStorePath, indexSystemModules,
614-
targetTriple, clangCI, diags, unitWriter, moduleNameScratch);
640+
addModuleDependencies(imports, indexStorePath, indexSystemModules, skipStdlib,
641+
targetTriple, clangCI, diags, unitWriter,
642+
moduleNameScratch, primarySourceFile);
615643

616644
// File dependencies.
617645
for (auto *F : fileDependencies)
@@ -662,6 +690,7 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
662690
StringRef indexUnitToken,
663691
StringRef indexStorePath,
664692
bool indexSystemModules,
693+
bool skipStdlib,
665694
bool isDebugCompilation,
666695
StringRef targetTriple,
667696
const DependencyTracker &dependencyTracker) {
@@ -689,7 +718,7 @@ bool index::indexAndRecord(SourceFile *primarySourceFile,
689718
#endif
690719

691720
return recordSourceFileUnit(primarySourceFile, indexUnitToken,
692-
indexStorePath, indexSystemModules,
721+
indexStorePath, indexSystemModules, skipStdlib,
693722
isDebugCompilation, targetTriple,
694723
fileDependencies.getArrayRef(),
695724
clangCI, diags);
@@ -700,6 +729,7 @@ bool index::indexAndRecord(ModuleDecl *module,
700729
StringRef moduleUnitToken,
701730
StringRef indexStorePath,
702731
bool indexSystemModules,
732+
bool skipStdlib,
703733
bool isDebugCompilation,
704734
StringRef targetTriple,
705735
const DependencyTracker &dependencyTracker) {
@@ -735,7 +765,7 @@ bool index::indexAndRecord(ModuleDecl *module,
735765
return true;
736766
}
737767
if (recordSourceFileUnit(SF, indexUnitTokens[unitIndex],
738-
indexStorePath, indexSystemModules,
768+
indexStorePath, indexSystemModules, skipStdlib,
739769
isDebugCompilation, targetTriple,
740770
fileDependencies.getArrayRef(),
741771
clangCI, diags))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%YAML 1.2
2+
---
3+
version: 1
4+
modules:
5+
- name: _ABAdditions

0 commit comments

Comments
 (0)