Skip to content

Commit 8b03b05

Browse files
author
Nathan Hawes
committed
[Index] Add index support for cross import overlays.
Resolves rdar://problem/59445445
1 parent 1beb37e commit 8b03b05

File tree

14 files changed

+195
-30
lines changed

14 files changed

+195
-30
lines changed

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/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: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,28 @@ 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,
379392
StringRef targetTriple,
380393
const clang::CompilerInstance &clangCI,
381394
DiagnosticEngine &diags,
382-
IndexUnitWriter &parentUnitWriter);
395+
IndexUnitWriter &parentUnitWriter,
396+
SourceFile *initialFile);
383397

384398
static void addModuleDependencies(ArrayRef<ModuleDecl::ImportedModule> imports,
385399
StringRef indexStorePath,
@@ -388,7 +402,8 @@ static void addModuleDependencies(ArrayRef<ModuleDecl::ImportedModule> imports,
388402
const clang::CompilerInstance &clangCI,
389403
DiagnosticEngine &diags,
390404
IndexUnitWriter &unitWriter,
391-
StringScratchSpace &moduleNameScratch) {
405+
StringScratchSpace &moduleNameScratch,
406+
SourceFile *initialFile = nullptr) {
392407
auto &fileMgr = clangCI.getFileManager();
393408

394409
for (auto &import : imports) {
@@ -430,9 +445,13 @@ static void addModuleDependencies(ArrayRef<ModuleDecl::ImportedModule> imports,
430445
emitDataForSwiftSerializedModule(mod, indexStorePath,
431446
indexSystemModules,
432447
targetTriple, clangCI, diags,
433-
unitWriter);
448+
unitWriter, initialFile);
434449
withoutUnitName = false;
435450
}
451+
452+
// If this is a cross-import overlay, make sure we use the name of
453+
// the underlying module instead.
454+
moduleName = getUnderlyingModuleName(mod, initialFile);
436455
}
437456
clang::index::writer::OpaqueModule opaqMod =
438457
moduleNameScratch.createString(moduleName);
@@ -454,9 +473,12 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
454473
StringRef targetTriple,
455474
const clang::CompilerInstance &clangCI,
456475
DiagnosticEngine &diags,
457-
IndexUnitWriter &parentUnitWriter) {
476+
IndexUnitWriter &parentUnitWriter,
477+
SourceFile *initialFile) {
458478
StringRef filename = module->getModuleFilename();
459-
std::string moduleName = module->getNameStr().str();
479+
// If this is a cross-import overlay, make sure we use the name of the
480+
// underlying module instead.
481+
std::string moduleName = getUnderlyingModuleName(module, initialFile);
460482

461483
std::string error;
462484
auto isUptodateOpt = parentUnitWriter.isUnitUpToDateForOutputFile(/*FilePath=*/filename,
@@ -565,12 +587,12 @@ emitDataForSwiftSerializedModule(ModuleDecl *module,
565587
ModuleDecl::ImportFilter importFilter;
566588
importFilter |= ModuleDecl::ImportFilterKind::Public;
567589
importFilter |= ModuleDecl::ImportFilterKind::Private;
568-
// FIXME: ImportFilterKind::ShadowedBySeparateOverlay?
569590
SmallVector<ModuleDecl::ImportedModule, 8> imports;
570591
module->getImportedModules(imports, importFilter);
571592
StringScratchSpace moduleNameScratch;
572593
addModuleDependencies(imports, indexStorePath, indexSystemModules,
573-
targetTriple, clangCI, diags, unitWriter, moduleNameScratch);
594+
targetTriple, clangCI, diags, unitWriter,
595+
moduleNameScratch, initialFile);
574596

575597
if (unitWriter.write(error)) {
576598
diags.diagnose(SourceLoc(), diag::error_write_index_unit, error);
@@ -611,7 +633,8 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
611633
primarySourceFile->getImportedModules(imports, importFilter);
612634
StringScratchSpace moduleNameScratch;
613635
addModuleDependencies(imports, indexStorePath, indexSystemModules,
614-
targetTriple, clangCI, diags, unitWriter, moduleNameScratch);
636+
targetTriple, clangCI, diags, unitWriter,
637+
moduleNameScratch, primarySourceFile);
615638

616639
// File dependencies.
617640
for (auto *F : fileDependencies)
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
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name A
3+
4+
public func fromA()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name B
3+
4+
public func fromB()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name C
3+
4+
public func fromC()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
%YAML 1.2
2+
---
3+
version: 1
4+
modules:
5+
- name: __ABAdditionsCAdditions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -swift-version 5 -enable-library-evolution -module-name _ABAdditions
3+
4+
@_exported import A
5+
import B
6+
7+
public func from_ABAdditions()
8+
public struct From_ABAdditionsType {}

0 commit comments

Comments
 (0)