Skip to content

[Indexing] If module aliasing is used, store module real names to index files #40450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,19 @@ class ModuleEntity {
ModuleEntity(const ModuleDecl *Mod) : Mod(Mod) {}
ModuleEntity(const clang::Module *Mod) : Mod(static_cast<const void *>(Mod)){}

StringRef getName() const;
std::string getFullName() const;
/// @param useRealNameIfAliased Whether to use the module's real name in case
/// module aliasing is used. For example, if a file
/// has `import Foo` and `-module-alias Foo=Bar` is
/// passed, treat Foo as an alias and Bar as the real
/// module name as its dependency. This only applies
/// to Swift modules.
/// @return The module name; for Swift modules, the real module name could be
/// different from the name if module aliasing is used.
StringRef getName(bool useRealNameIfAliased = false) const;

/// For Swift modules, it returns the same result as \c ModuleEntity::getName(bool).
/// For Clang modules, it returns the result of \c clang::Module::getFullModuleName.
std::string getFullName(bool useRealNameIfAliased = false) const;

bool isSystemModule() const;
bool isBuiltinModule() const;
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/USRGeneration.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ bool printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS);
bool printValueDeclUSR(const ValueDecl *D, raw_ostream &OS);

/// Prints out the USR for the given ModuleEntity.
/// In case module aliasing is used, it prints the real module name. For example,
/// if a file has `import Foo` and `-module-alias Foo=Bar` is passed, treat Foo as
/// an alias and Bar as the real module name as its dependency. Note that the
/// aliasing only applies to Swift modules.
/// \returns true if it failed, false on success.
bool printModuleUSR(ModuleEntity Mod, raw_ostream &OS);

Expand Down
8 changes: 4 additions & 4 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3073,17 +3073,17 @@ getClangModule(llvm::PointerUnion<const ModuleDecl *, const void *> Union) {
return static_cast<const clang::Module *>(Union.get<const void *>());
}

StringRef ModuleEntity::getName() const {
StringRef ModuleEntity::getName(bool useRealNameIfAliased) const {
assert(!Mod.isNull());
if (auto SwiftMod = Mod.dyn_cast<const ModuleDecl*>())
return SwiftMod->getName().str();
return useRealNameIfAliased ? SwiftMod->getRealName().str() : SwiftMod->getName().str();
return getClangModule(Mod)->Name;
}

std::string ModuleEntity::getFullName() const {
std::string ModuleEntity::getFullName(bool useRealNameIfAliased) const {
assert(!Mod.isNull());
if (auto SwiftMod = Mod.dyn_cast<const ModuleDecl*>())
return std::string(SwiftMod->getName());
return std::string(useRealNameIfAliased ? SwiftMod->getRealName() : SwiftMod->getName());
return getClangModule(Mod)->getFullModuleName();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/AST/USRGeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ swift::MangleLocalTypeDeclRequest::evaluate(Evaluator &evaluator,

bool ide::printModuleUSR(ModuleEntity Mod, raw_ostream &OS) {
if (auto *D = Mod.getAsSwiftModule()) {
StringRef moduleName = D->getName().str();
StringRef moduleName = D->getRealName().str();
return clang::index::generateFullUSRForTopLevelModuleName(moduleName, OS);
} else if (auto ClangM = Mod.getAsClangModule()) {
return clang::index::generateFullUSRForModule(ClangM, OS);
Expand Down
9 changes: 6 additions & 3 deletions lib/Index/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
storage.clear();
{
llvm::raw_svector_ostream OS(storage);
OS << Mod.getFullName();
OS << Mod.getFullName(/*useRealNameIfAliased=*/true);
result.name = stringStorage.copyString(OS.str());
}
}
Expand Down Expand Up @@ -1119,8 +1119,11 @@ bool IndexSwiftASTWalker::visitImports(
if (!IsClangModuleOpt.hasValue())
continue;
bool IsClangModule = *IsClangModuleOpt;

StringRef ModuleName = Mod->getNameStr();
// Use module real name in case module aliasing is used.
// For example, if a file being indexed has `import Foo`
// and `-module-alias Foo=Bar` is passed, treat Foo as an
// alias and Bar as the real module name as its dependency.
StringRef ModuleName = Mod->getRealName().str();

// If this module is an underscored cross-import overlay, use the name
// of the underlying module that declared it instead.
Expand Down
7 changes: 5 additions & 2 deletions lib/Index/IndexRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,11 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
case FileUnitKind::ClangModule: {
auto *LFU = cast<LoadedFile>(FU);
if (auto F = fileMgr.getFile(LFU->getFilename())) {
StringRef moduleName = mod->getNameStr();
// Use module real name for unit writer in case module aliasing
// is used. For example, if a file being indexed has `import Foo`
// and `-module-alias Foo=Bar` is passed, treat Foo as an alias
// and Bar as the real module name as its dependency.
StringRef moduleName = mod->getRealName().str();
bool withoutUnitName = true;
if (FU->getKind() == FileUnitKind::ClangModule) {
auto clangModUnit = cast<ClangModuleUnit>(LFU);
Expand Down Expand Up @@ -611,7 +615,6 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
// FIXME: Get real values for the following.
StringRef swiftVersion;
StringRef sysrootPath = clangCI.getHeaderSearchOpts().Sysroot;

IndexUnitWriter unitWriter(
fileMgr, indexStorePath, "swift", swiftVersion, indexUnitToken,
module->getNameStr(), mainFile ? *mainFile : nullptr, isSystem,
Expand Down
84 changes: 84 additions & 0 deletions test/Index/module-alias-indexing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s

// RUN: %target-swift-frontend %t/FileLogging.swift -module-name AppleLogging -module-alias XLogging=AppleLogging -emit-module -o %t/AppleLogging.swiftmodule -index-store-path %t/indexResult
// RUN: %target-swift-frontend -typecheck %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t -index-store-path %t/indexResult

// RUN: c-index-test core -print-unit %t/indexResult > %t/indexUnitDump.txt
// RUN: %FileCheck %s -input-file %t/indexUnitDump.txt -check-prefix CHECK-UNIT

// RUN: c-index-test core -print-record %t/indexResult > %t/indexRecordDump.txt
// RUN: %FileCheck %s -input-file %t/indexRecordDump.txt -check-prefix CHECK-RECORD

// BEGIN FileLogging.swift
public struct Logger {
public init() {}
}

public func setup() -> XLogging.Logger? {
return Logger()
}

// BEGIN FileLib.swift
import XLogging

public func start() {
_ = XLogging.setup()
}


// CHECK-UNIT: [[MODA:--[A-Z0-9]*]]
// CHECK-UNIT: --------
// CHECK-UNIT: module-name: FileLib
// CHECK-UNIT: has-main: 1
// CHECK-UNIT: main-path: {{.*}}{{/|\\}}FileLib.swift
// CHECK-UNIT: out-file: -
// CHECK-UNIT: DEPEND START
// CHECK-UNIT: Unit | system | Swift | {{.*}}{{/|\\}}Swift.swiftmodule
// CHECK-UNIT: Unit | user | AppleLogging | {{.*}}{{/|\\}}AppleLogging.swiftmodule
// CHECK-UNIT: Record | user | {{.*}}{{/|\\}}FileLib.swift | [[MODA:FileLib.swift-[A-Z0-9]*]]
// CHECK-UNIT: DEPEND END

// CHECK-UNIT: [[MODA:AppleLogging.swiftmodule-[A-Z0-9]*]]
// CHECK-UNIT: --------
// CHECK-UNIT: module-name: AppleLogging
// CHECK-UNIT: has-main: 1
// CHECK-UNIT: main-path: {{.*}}{{/|\\}}FileLogging.swift
// CHECK-UNIT: out-file: {{.*}}{{/|\\}}AppleLogging.swiftmodule
// CHECK-UNIT: DEPEND START
// CHECK-UNIT: Unit | system | Swift | {{.*}}{{/|\\}}Swift.swiftmodule
// CHECK-UNIT: Record | user | {{.*}}{{/|\\}}FileLogging.swift | [[MODA:FileLogging.swift-[A-Z0-9]*]]
// CHECK-UNIT: DEPEND END

// CHECK-RECORD: FileLib.swift
// CHECK-RECORD: ------------
// CHECK-RECORD: module/Swift | AppleLogging | c:@M@AppleLogging | <no-cgname> | Ref,RelCont -
// CHECK-RECORD: function/Swift | start() | s:7FileLib5startyyF | <no-cgname> | Def - RelCall,RelCont
// CHECK-RECORD: function/Swift | setup() | s:12AppleLogging5setupAA6LoggerVSgyF | <no-cgname> | Ref,Call,RelCall,RelCont -
// CHECK-RECORD: ------------
// CHECK-RECORD: 1:8 | module/Swift | c:@M@AppleLogging | Ref | rel: 0
// CHECK-RECORD: 3:13 | function/Swift | s:7FileLib5startyyF | Def | rel: 0
// CHECK-RECORD: 4:7 | module/Swift | c:@M@AppleLogging | Ref,RelCont | rel: 1
// CHECK-RECORD: RelCont | s:7FileLib5startyyF
// CHECK-RECORD: 4:16 | function/Swift | s:12AppleLogging5setupAA6LoggerVSgyF | Ref,Call,RelCall,RelCont | rel: 1
// CHECK-RECORD: RelCall,RelCont | s:7FileLib5startyyF

// CHECK-RECORD: FileLogging.swift
// CHECK-RECORD: ------------
// CHECK-RECORD: struct/Swift | Logger | s:12AppleLogging6LoggerV | <no-cgname> | Def,Ref,RelCont - RelChild
// CHECK-RECORD: constructor/Swift | init() | s:12AppleLogging6LoggerVACycfc | <no-cgname> | Def,Ref,Call,RelChild,RelCall,RelCont -
// CHECK-RECORD: function/Swift | setup() | s:12AppleLogging5setupAA6LoggerVSgyF | <no-cgname> | Def - RelCall,RelCont
// CHECK-RECORD: ------------
// CHECK-RECORD: 1:15 | struct/Swift | s:12AppleLogging6LoggerV | Def | rel: 0
// CHECK-RECORD: 2:10 | constructor/Swift | s:12AppleLogging6LoggerVACycfc | Def,RelChild | rel: 1
// CHECK-RECORD: RelChild | s:12AppleLogging6LoggerV
// CHECK-RECORD: 5:13 | function/Swift | s:12AppleLogging5setupAA6LoggerVSgyF | Def | rel: 0
// CHECK-RECORD: 5:24 | module/Swift | c:@M@AppleLogging | Ref,RelCont | rel: 1
// CHECK-RECORD: RelCont | s:12AppleLogging5setupAA6LoggerVSgyF
// CHECK-RECORD: 5:33 | struct/Swift | s:12AppleLogging6LoggerV | Ref,RelCont | rel: 1
// CHECK-RECORD: RelCont | s:12AppleLogging5setupAA6LoggerVSgyF
// CHECK-RECORD: 6:10 | struct/Swift | s:12AppleLogging6LoggerV | Ref,RelCont | rel: 1
// CHECK-RECORD: RelCont | s:12AppleLogging5setupAA6LoggerVSgyF
// CHECK-RECORD: 6:10 | constructor/Swift | s:12AppleLogging6LoggerVACycfc | Ref,Call,RelCall,RelCont | rel: 1
// CHECK-RECORD: RelCall,RelCont | s:12AppleLogging5setupAA6LoggerVSgyF