Skip to content

Commit 0ff713d

Browse files
committed
[Indexing] Use module real name in case module aliasing is used
Resolves rdar://82896373
1 parent 32c8bbe commit 0ff713d

File tree

7 files changed

+122
-15
lines changed

7 files changed

+122
-15
lines changed

include/swift/AST/Module.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,19 @@ class ModuleEntity {
869869
ModuleEntity(const ModuleDecl *Mod) : Mod(Mod) {}
870870
ModuleEntity(const clang::Module *Mod) : Mod(static_cast<const void *>(Mod)){}
871871

872-
StringRef getName() const;
873-
std::string getFullName() const;
872+
// @param useRealNameIfAliased Whether to use the module's real name in case
873+
// module aliasing is used. For example, if a file
874+
// has `import Foo` and `-module-alias Foo=Bar` is
875+
// passed, treat Foo as an alias and Bar as the real
876+
// module name as its dependency. This only applies
877+
// to Swift modules.
878+
// @return The module name; for Swift modules, the real module name could be
879+
// different from the name if module aliasing is used.
880+
StringRef getName(bool useRealNameIfAliased = false) const;
881+
882+
// For Swift modules, it returns the same result as \c ModuleEntity::getName(bool).
883+
// For Clang modules, it returns the result of \c clang::Module::getFullModuleName.
884+
std::string getFullName(bool useRealNameIfAliased = false) const;
874885

875886
bool isSystemModule() const;
876887
bool isBuiltinModule() const;

include/swift/AST/USRGeneration.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ bool printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS);
4747
bool printValueDeclUSR(const ValueDecl *D, raw_ostream &OS);
4848

4949
/// Prints out the USR for the given ModuleEntity.
50+
/// \param useRealNameIfAliased Whether to use the module's real name in case
51+
/// module aliasing is used. For example, if a file
52+
/// has `import Foo` and `-module-alias Foo=Bar` is
53+
/// passed, treat Foo as an alias and Bar as the real
54+
/// module name as its dependency. This only applies
55+
/// to Swift modules.
5056
/// \returns true if it failed, false on success.
51-
bool printModuleUSR(ModuleEntity Mod, raw_ostream &OS);
57+
bool printModuleUSR(ModuleEntity Mod, raw_ostream &OS, bool useRealNameIfAliased = false);
5258

5359
/// Prints out the accessor USR for the given storage Decl.
5460
/// \returns true if it failed, false on success.

lib/AST/Module.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3073,17 +3073,17 @@ getClangModule(llvm::PointerUnion<const ModuleDecl *, const void *> Union) {
30733073
return static_cast<const clang::Module *>(Union.get<const void *>());
30743074
}
30753075

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

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

lib/AST/USRGeneration.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ swift::MangleLocalTypeDeclRequest::evaluate(Evaluator &evaluator,
278278
return NewMangler.mangleLocalTypeDecl(D);
279279
}
280280

281-
bool ide::printModuleUSR(ModuleEntity Mod, raw_ostream &OS) {
281+
bool ide::printModuleUSR(ModuleEntity Mod, raw_ostream &OS, bool useRealNameIfAliased) {
282282
if (auto *D = Mod.getAsSwiftModule()) {
283-
StringRef moduleName = D->getName().str();
283+
StringRef moduleName = useRealNameIfAliased ? D->getRealName().str() : D->getNameStr();
284284
return clang::index::generateFullUSRForTopLevelModuleName(moduleName, OS);
285285
} else if (auto ClangM = Mod.getAsClangModule()) {
286286
return clang::index::generateFullUSRForModule(ClangM, OS);

lib/Index/Index.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,14 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
482482
SmallString<128> storage;
483483
{
484484
llvm::raw_svector_ostream OS(storage);
485-
if (ide::printModuleUSR(Mod, OS))
485+
if (ide::printModuleUSR(Mod, OS, /*useRealNameIfAliased=*/true))
486486
return true;
487487
result.USR = stringStorage.copyString(OS.str());
488488
}
489489
storage.clear();
490490
{
491491
llvm::raw_svector_ostream OS(storage);
492-
OS << Mod.getFullName();
492+
OS << Mod.getFullName(/*useRealNameIfAliased=*/true);
493493
result.name = stringStorage.copyString(OS.str());
494494
}
495495
}
@@ -1119,8 +1119,11 @@ bool IndexSwiftASTWalker::visitImports(
11191119
if (!IsClangModuleOpt.hasValue())
11201120
continue;
11211121
bool IsClangModule = *IsClangModuleOpt;
1122-
1123-
StringRef ModuleName = Mod->getNameStr();
1122+
// Use module real name in case module aliasing is used.
1123+
// For example, if a file being indexed has `import Foo`
1124+
// and `-module-alias Foo=Bar` is passed, treat Foo as an
1125+
// alias and Bar as the real module name as its dependency.
1126+
StringRef ModuleName = Mod->getRealName().str();
11241127

11251128
// If this module is an underscored cross-import overlay, use the name
11261129
// of the underlying module that declared it instead.

lib/Index/IndexRecord.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,11 @@ static void addModuleDependencies(ArrayRef<ImportedModule> imports,
413413
case FileUnitKind::ClangModule: {
414414
auto *LFU = cast<LoadedFile>(FU);
415415
if (auto F = fileMgr.getFile(LFU->getFilename())) {
416-
StringRef moduleName = mod->getNameStr();
416+
// Use module real name for unit writer in case module aliasing
417+
// is used. For example, if a file being indexed has `import Foo`
418+
// and `-module-alias Foo=Bar` is passed, treat Foo as an alias
419+
// and Bar as the real module name as its dependency.
420+
StringRef moduleName = mod->getRealName().str();
417421
bool withoutUnitName = true;
418422
if (FU->getKind() == FileUnitKind::ClangModule) {
419423
auto clangModUnit = cast<ClangModuleUnit>(LFU);
@@ -611,7 +615,6 @@ recordSourceFileUnit(SourceFile *primarySourceFile, StringRef indexUnitToken,
611615
// FIXME: Get real values for the following.
612616
StringRef swiftVersion;
613617
StringRef sysrootPath = clangCI.getHeaderSearchOpts().Sysroot;
614-
615618
IndexUnitWriter unitWriter(
616619
fileMgr, indexStorePath, "swift", swiftVersion, indexUnitToken,
617620
module->getNameStr(), mainFile ? *mainFile : nullptr, isSystem,
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// 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
5+
// RUN: %target-swift-frontend -typecheck %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t -index-store-path %t/indexResult
6+
7+
// RUN: c-index-test core -print-unit %t/indexResult > %t/indexUnitDump.txt
8+
// RUN: %FileCheck %s -input-file %t/indexUnitDump.txt -check-prefix CHECK-UNIT
9+
10+
// RUN: c-index-test core -print-record %t/indexResult > %t/indexRecordDump.txt
11+
// RUN: %FileCheck %s -input-file %t/indexRecordDump.txt -check-prefix CHECK-RECORD
12+
13+
// BEGIN FileLogging.swift
14+
public struct Logger {
15+
public init() {}
16+
}
17+
18+
public func setup() -> XLogging.Logger? {
19+
return Logger()
20+
}
21+
22+
// BEGIN FileLib.swift
23+
import XLogging
24+
25+
public func start() {
26+
_ = XLogging.setup()
27+
}
28+
29+
30+
// CHECK-UNIT: [[MODA:--[A-Z0-9]*]]
31+
// CHECK-UNIT: --------
32+
// CHECK-UNIT: module-name: FileLib
33+
// CHECK-UNIT: has-main: 1
34+
// CHECK-UNIT: main-path: {{.*}}{{/|\\}}FileLib.swift
35+
// CHECK-UNIT: out-file: -
36+
// CHECK-UNIT: DEPEND START
37+
// CHECK-UNIT: Unit | system | Swift | {{.*}}{{/|\\}}Swift.swiftmodule
38+
// CHECK-UNIT: Unit | user | AppleLogging | {{.*}}{{/|\\}}AppleLogging.swiftmodule
39+
// CHECK-UNIT: Record | user | {{.*}}{{/|\\}}FileLib.swift | [[MODA:FileLib.swift-[A-Z0-9]*]]
40+
// CHECK-UNIT: DEPEND END
41+
42+
// CHECK-UNIT: [[MODA:AppleLogging.swiftmodule-[A-Z0-9]*]]
43+
// CHECK-UNIT: --------
44+
// CHECK-UNIT: module-name: AppleLogging
45+
// CHECK-UNIT: has-main: 1
46+
// CHECK-UNIT: main-path: {{.*}}{{/|\\}}FileLogging.swift
47+
// CHECK-UNIT: out-file: {{.*}}{{/|\\}}AppleLogging.swiftmodule
48+
// CHECK-UNIT: DEPEND START
49+
// CHECK-UNIT: Unit | system | Swift | {{.*}}{{/|\\}}Swift.swiftmodule
50+
// CHECK-UNIT: Record | user | {{.*}}{{/|\\}}FileLogging.swift | [[MODA:FileLogging.swift-[A-Z0-9]*]]
51+
// CHECK-UNIT: DEPEND END
52+
53+
// CHECK-RECORD: FileLib.swift
54+
// CHECK-RECORD: ------------
55+
// CHECK-RECORD: module/Swift | AppleLogging | c:@M@AppleLogging | <no-cgname> | Ref,RelCont -
56+
// CHECK-RECORD: function/Swift | start() | s:7FileLib5startyyF | <no-cgname> | Def - RelCall,RelCont
57+
// CHECK-RECORD: function/Swift | setup() | s:12AppleLogging5setupAA6LoggerVSgyF | <no-cgname> | Ref,Call,RelCall,RelCont -
58+
// CHECK-RECORD: ------------
59+
// CHECK-RECORD: 1:8 | module/Swift | c:@M@AppleLogging | Ref | rel: 0
60+
// CHECK-RECORD: 3:13 | function/Swift | s:7FileLib5startyyF | Def | rel: 0
61+
// CHECK-RECORD: 4:7 | module/Swift | c:@M@AppleLogging | Ref,RelCont | rel: 1
62+
// CHECK-RECORD: RelCont | s:7FileLib5startyyF
63+
// CHECK-RECORD: 4:16 | function/Swift | s:12AppleLogging5setupAA6LoggerVSgyF | Ref,Call,RelCall,RelCont | rel: 1
64+
// CHECK-RECORD: RelCall,RelCont | s:7FileLib5startyyF
65+
66+
// CHECK-RECORD: FileLogging.swift
67+
// CHECK-RECORD: ------------
68+
// CHECK-RECORD: struct/Swift | Logger | s:12AppleLogging6LoggerV | <no-cgname> | Def,Ref,RelCont - RelChild
69+
// CHECK-RECORD: constructor/Swift | init() | s:12AppleLogging6LoggerVACycfc | <no-cgname> | Def,Ref,Call,RelChild,RelCall,RelCont -
70+
// CHECK-RECORD: function/Swift | setup() | s:12AppleLogging5setupAA6LoggerVSgyF | <no-cgname> | Def - RelCall,RelCont
71+
// CHECK-RECORD: ------------
72+
// CHECK-RECORD: 1:15 | struct/Swift | s:12AppleLogging6LoggerV | Def | rel: 0
73+
// CHECK-RECORD: 2:10 | constructor/Swift | s:12AppleLogging6LoggerVACycfc | Def,RelChild | rel: 1
74+
// CHECK-RECORD: RelChild | s:12AppleLogging6LoggerV
75+
// CHECK-RECORD: 5:13 | function/Swift | s:12AppleLogging5setupAA6LoggerVSgyF | Def | rel: 0
76+
// CHECK-RECORD: 5:24 | module/Swift | c:@M@AppleLogging | Ref,RelCont | rel: 1
77+
// CHECK-RECORD: RelCont | s:12AppleLogging5setupAA6LoggerVSgyF
78+
// CHECK-RECORD: 5:33 | struct/Swift | s:12AppleLogging6LoggerV | Ref,RelCont | rel: 1
79+
// CHECK-RECORD: RelCont | s:12AppleLogging5setupAA6LoggerVSgyF
80+
// CHECK-RECORD: 6:10 | struct/Swift | s:12AppleLogging6LoggerV | Ref,RelCont | rel: 1
81+
// CHECK-RECORD: RelCont | s:12AppleLogging5setupAA6LoggerVSgyF
82+
// CHECK-RECORD: 6:10 | constructor/Swift | s:12AppleLogging6LoggerVACycfc | Ref,Call,RelCall,RelCont | rel: 1
83+
// CHECK-RECORD: RelCall,RelCont | s:12AppleLogging5setupAA6LoggerVSgyF
84+

0 commit comments

Comments
 (0)