Skip to content

Commit d95d0a0

Browse files
authored
Merge pull request #40450 from apple/es-index
[Indexing] If module aliasing is used, store module real names to index unit/record files Resolves rdar://82896373
2 parents 6a90b6a + e358302 commit d95d0a0

File tree

7 files changed

+117
-12
lines changed

7 files changed

+117
-12
lines changed

include/swift/AST/Module.h

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

885-
StringRef getName() const;
886-
std::string getFullName() const;
885+
/// @param useRealNameIfAliased Whether to use the module's real name in case
886+
/// module aliasing is used. For example, if a file
887+
/// has `import Foo` and `-module-alias Foo=Bar` is
888+
/// passed, treat Foo as an alias and Bar as the real
889+
/// module name as its dependency. This only applies
890+
/// to Swift modules.
891+
/// @return The module name; for Swift modules, the real module name could be
892+
/// different from the name if module aliasing is used.
893+
StringRef getName(bool useRealNameIfAliased = false) const;
894+
895+
/// For Swift modules, it returns the same result as \c ModuleEntity::getName(bool).
896+
/// For Clang modules, it returns the result of \c clang::Module::getFullModuleName.
897+
std::string getFullName(bool useRealNameIfAliased = false) const;
887898

888899
bool isSystemModule() const;
889900
bool isBuiltinModule() const;

include/swift/AST/USRGeneration.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ 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+
/// In case module aliasing is used, it prints the real module name. For example,
51+
/// if a file has `import Foo` and `-module-alias Foo=Bar` is passed, treat Foo as
52+
/// an alias and Bar as the real module name as its dependency. Note that the
53+
/// aliasing only applies to Swift modules.
5054
/// \returns true if it failed, false on success.
5155
bool printModuleUSR(ModuleEntity Mod, raw_ostream &OS);
5256

lib/AST/Module.cpp

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

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

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

lib/AST/USRGeneration.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ swift::MangleLocalTypeDeclRequest::evaluate(Evaluator &evaluator,
280280

281281
bool ide::printModuleUSR(ModuleEntity Mod, raw_ostream &OS) {
282282
if (auto *D = Mod.getAsSwiftModule()) {
283-
StringRef moduleName = D->getName().str();
283+
StringRef moduleName = D->getRealName().str();
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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
491491
storage.clear();
492492
{
493493
llvm::raw_svector_ostream OS(storage);
494-
OS << Mod.getFullName();
494+
OS << Mod.getFullName(/*useRealNameIfAliased=*/true);
495495
result.name = stringStorage.copyString(OS.str());
496496
}
497497
}
@@ -1121,8 +1121,11 @@ bool IndexSwiftASTWalker::visitImports(
11211121
if (!IsClangModuleOpt.hasValue())
11221122
continue;
11231123
bool IsClangModule = *IsClangModuleOpt;
1124-
1125-
StringRef ModuleName = Mod->getNameStr();
1124+
// Use module real name in case module aliasing is used.
1125+
// For example, if a file being indexed has `import Foo`
1126+
// and `-module-alias Foo=Bar` is passed, treat Foo as an
1127+
// alias and Bar as the real module name as its dependency.
1128+
StringRef ModuleName = Mod->getRealName().str();
11261129

11271130
// If this module is an underscored cross-import overlay, use the name
11281131
// 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)