Skip to content

Commit 888fe84

Browse files
authored
Merge pull request #62073 from xymus/module-alias-disambiguate-more
[ModuleInterface] AliasModuleNames workaround support for indirect dependencies and missing imports
2 parents d6cffbb + 7ae02c0 commit 888fe84

File tree

10 files changed

+70
-31
lines changed

10 files changed

+70
-31
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ class ASTPrinter {
166166
PrintNameContext NameContext = PrintNameContext::Normal);
167167

168168
/// Called when printing the referenced name of a module.
169-
virtual void printModuleRef(ModuleEntity Mod, Identifier Name,
170-
const PrintOptions &Options);
169+
virtual void printModuleRef(ModuleEntity Mod, Identifier Name);
171170

172171
/// Called before printing a synthesized extension.
173172
virtual void printSynthesizedExtensionPre(const ExtensionDecl *ED,

include/swift/AST/PrintOptions.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/TypeOrExtensionDecl.h"
2020
#include "llvm/ADT/Optional.h"
2121
#include "llvm/ADT/DenseMap.h"
22+
#include "llvm/ADT/SmallSet.h"
2223
#include <limits.h>
2324
#include <vector>
2425

@@ -470,6 +471,14 @@ struct PrintOptions {
470471
/// with types sharing a name with a module.
471472
bool AliasModuleNames = false;
472473

474+
/// Name of the modules that have been aliased in AliasModuleNames mode.
475+
/// Ideally we would use something other than a string to identify a module,
476+
/// but since one alias can apply to more than one module, strings happen
477+
/// to be pretty reliable. That is, unless there's an unexpected name
478+
/// collision between two modules, which isn't supported by this workaround
479+
/// yet.
480+
llvm::SmallSet<StringRef, 4> *AliasModuleNamesTargets = nullptr;
481+
473482
/// When printing an Optional<T>, rather than printing 'T?', print
474483
/// 'T!'. Used as a modifier only when we know we're printing
475484
/// something that was declared as an implicitly unwrapped optional
@@ -640,7 +649,10 @@ struct PrintOptions {
640649
bool preferTypeRepr,
641650
bool printFullConvention,
642651
bool printSPIs,
643-
bool aliasModuleNames);
652+
bool aliasModuleNames,
653+
llvm::SmallSet<StringRef, 4>
654+
*aliasModuleNamesTargets
655+
);
644656

645657
/// Retrieve the set of options suitable for "Generated Interfaces", which
646658
/// are a prettified representation of the public API of a module, to be

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
132132
bool preferTypeRepr,
133133
bool printFullConvention,
134134
bool printSPIs,
135-
bool aliasModuleNames) {
135+
bool aliasModuleNames,
136+
llvm::SmallSet<StringRef, 4>
137+
*aliasModuleNamesTargets
138+
) {
136139
PrintOptions result;
137140
result.IsForSwiftInterface = true;
138141
result.PrintLongAttrsOnSeparateLines = true;
@@ -154,6 +157,7 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint,
154157
OpaqueReturnTypePrintingMode::StableReference;
155158
result.PreferTypeRepr = preferTypeRepr;
156159
result.AliasModuleNames = aliasModuleNames;
160+
result.AliasModuleNamesTargets = aliasModuleNamesTargets;
157161
if (printFullConvention)
158162
result.PrintFunctionRepresentationAttrs =
159163
PrintOptions::FunctionRepresentationMode::Full;
@@ -367,11 +371,7 @@ void ASTPrinter::printTypeRef(Type T, const TypeDecl *RefTo, Identifier Name,
367371
printName(Name, Context);
368372
}
369373

370-
void ASTPrinter::printModuleRef(ModuleEntity Mod, Identifier Name,
371-
const PrintOptions &Options) {
372-
if (Options.AliasModuleNames)
373-
printTextImpl(MODULE_DISAMBIGUATING_PREFIX);
374-
374+
void ASTPrinter::printModuleRef(ModuleEntity Mod, Identifier Name) {
375375
printName(Name);
376376
}
377377

@@ -2507,7 +2507,7 @@ void PrintAST::visitImportDecl(ImportDecl *decl) {
25072507
Name = Declaring->getRealName();
25082508
}
25092509
}
2510-
Printer.printModuleRef(Mods.front(), Name, Options);
2510+
Printer.printModuleRef(Mods.front(), Name);
25112511
Mods = Mods.slice(1);
25122512
} else {
25132513
Printer << Elem.Item.str();
@@ -5403,7 +5403,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
54035403
}
54045404
}
54055405

5406-
Printer.printModuleRef(Mod, Name, Options);
5406+
if (Options.AliasModuleNames && Options.AliasModuleNamesTargets &&
5407+
Options.AliasModuleNamesTargets->contains(Name.str())) {
5408+
auto nameTwine = MODULE_DISAMBIGUATING_PREFIX + Name.str();
5409+
Name = Mod->getASTContext().getIdentifier(nameTwine.str());
5410+
}
5411+
5412+
Printer.printModuleRef(Mod, Name);
54075413
Printer << ".";
54085414
}
54095415

@@ -5750,8 +5756,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
57505756
Printer << "module<";
57515757
// Should print the module real name in case module aliasing is
57525758
// used (see -module-alias), since that's the actual binary name.
5753-
Printer.printModuleRef(T->getModule(), T->getModule()->getRealName(),
5754-
Options);
5759+
Printer.printModuleRef(T->getModule(), T->getModule()->getRealName());
57555760
Printer << ">";
57565761
}
57575762

lib/AST/TypeRepr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void ComponentIdentTypeRepr::printImpl(ASTPrinter &Printer,
269269
const PrintOptions &Opts) const {
270270
if (auto *TD = dyn_cast_or_null<TypeDecl>(getBoundDecl())) {
271271
if (auto MD = dyn_cast<ModuleDecl>(TD))
272-
Printer.printModuleRef(MD, getNameRef().getBaseIdentifier(), Opts);
272+
Printer.printModuleRef(MD, getNameRef().getBaseIdentifier());
273273
else
274274
Printer.printTypeRef(Type(), TD, getNameRef().getBaseIdentifier());
275275
} else {

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ version::Version swift::InterfaceFormatVersion({1, 0});
4949
/// construct \p M.
5050
static void printToolVersionAndFlagsComment(raw_ostream &out,
5151
ModuleInterfaceOptions const &Opts,
52-
ModuleDecl *M) {
52+
ModuleDecl *M,
53+
llvm::SmallSet<StringRef, 4>
54+
&AliasModuleNamesTargets) {
5355
auto &Ctx = M->getASTContext();
5456
auto ToolsVersion =
5557
getSwiftInterfaceCompilerVersionForCurrentCompiler(Ctx);
@@ -62,9 +64,8 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
6264

6365
// Insert additional -module-alias flags
6466
if (Opts.AliasModuleNames) {
65-
llvm::SmallSet<StringRef, 2> aliasTargets;
6667
StringRef moduleName = M->getNameStr();
67-
aliasTargets.insert(M->getNameStr());
68+
AliasModuleNamesTargets.insert(M->getNameStr());
6869
out << " -module-alias " << MODULE_DISAMBIGUATING_PREFIX <<
6970
moduleName << "=" << moduleName;
7071

@@ -74,9 +75,10 @@ static void printToolVersionAndFlagsComment(raw_ostream &out,
7475
ModuleDecl::ImportFilterKind::Exported,
7576
ModuleDecl::ImportFilterKind::SPIOnly,
7677
ModuleDecl::ImportFilterKind::SPIAccessControl});
78+
M->getMissingImportedModules(imports);
7779
for (ImportedModule import: imports) {
7880
StringRef importedName = import.importedModule->getNameStr();
79-
if (aliasTargets.insert(importedName).second) {
81+
if (AliasModuleNamesTargets.insert(importedName).second) {
8082
out << " -module-alias " << MODULE_DISAMBIGUATING_PREFIX <<
8183
importedName << "=" << importedName;
8284
}
@@ -781,12 +783,14 @@ bool swift::emitSwiftInterface(raw_ostream &out,
781783
ModuleDecl *M) {
782784
assert(M);
783785

784-
printToolVersionAndFlagsComment(out, Opts, M);
786+
llvm::SmallSet<StringRef, 4> aliasModuleNamesTargets;
787+
printToolVersionAndFlagsComment(out, Opts, M, aliasModuleNamesTargets);
788+
785789
printImports(out, Opts, M);
786790

787791
const PrintOptions printOptions = PrintOptions::printSwiftInterfaceFile(
788792
M, Opts.PreserveTypesAsWritten, Opts.PrintFullConvention, Opts.PrintSPIs,
789-
Opts.AliasModuleNames);
793+
Opts.AliasModuleNames, &aliasModuleNamesTargets);
790794
InheritedProtocolCollector::PerTypeMap inheritedProtocolMap;
791795

792796
SmallVector<Decl *, 16> topLevelDecls;

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ class ClangCommentPrinter : public ASTPrinter {
8080
PrintNameContext NameContext) override {
8181
return OtherPrinter.printTypeRef(T, TD, Name, NameContext);
8282
}
83-
void printModuleRef(ModuleEntity Mod, Identifier Name,
84-
const PrintOptions &Options) override {
85-
return OtherPrinter.printModuleRef(Mod, Name, Options);
83+
void printModuleRef(ModuleEntity Mod, Identifier Name) override {
84+
return OtherPrinter.printModuleRef(Mod, Name);
8685
}
8786
void printSynthesizedExtensionPre(const ExtensionDecl *ED,
8887
TypeOrExtensionDecl Target,

test/ModuleInterface/ambiguous-aliases-workaround.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
// RUN: %empty-directory(%t)
44
// RUN: split-file %s %t
55

6+
// RUN: %target-swift-frontend -emit-module -module-name ExportedLib \
7+
// RUN: -swift-version 5 -enable-library-evolution \
8+
// RUN: -o %t/ExportedLib.swiftmodule \
9+
// RUN: -emit-module-interface-path %t/ExportedLib.swiftinterface \
10+
// RUN: %t/ExportedLib.swift
11+
// RUN: %target-swift-typecheck-module-from-interface(%t/ExportedLib.swiftinterface)
12+
613
// RUN: %target-swift-frontend -emit-module -module-name AmbiguousLib \
714
// RUN: -swift-version 5 -enable-library-evolution \
815
// RUN: -o %t/AmbiguousLib.swiftmodule \
916
// RUN: -emit-module-interface-path %t/AmbiguousLib.swiftinterface \
10-
// RUN: %t/AmbiguousLib.swift
11-
// RUN: %target-swift-typecheck-module-from-interface(%t/AmbiguousLib.swiftinterface)
17+
// RUN: %t/AmbiguousLib.swift -I%t
18+
// RUN: %target-swift-typecheck-module-from-interface(%t/AmbiguousLib.swiftinterface) -I%t
1219

1320
// RUN: %target-swift-frontend -emit-module -module-name AmbiguousClientName \
1421
// RUN: -swift-version 5 -enable-library-evolution \
@@ -40,10 +47,17 @@ struct UnderlyingType {};
4047
void underlyingFunc() {}
4148

4249
//--- SomeClangModule.h
50+
4351
struct CType {};
4452

53+
//--- ExportedLib.swift
54+
55+
public struct ExportedStruct {}
56+
4557
//--- AmbiguousLib.swift
4658

59+
@_exported import ExportedLib
60+
4761
// 1. AmbiguousLib defined a type named AmbiguousLib
4862
public struct AmbiguousLib {
4963
public struct Nested {}
@@ -73,6 +87,8 @@ public func refToStdlib(_ a: Swift.Int) {}
7387
public func refToUnderlying(_ a: UnderlyingType) {}
7488
public func refToC(_ a: CType) {}
7589

90+
public func refToReexport(_ a: ExportedStruct) {}
91+
7692
//--- OverlayClient.swift
7793

7894
import AmbiguousClientName

test/Sema/missing-import-inlinable-code.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
// RUN: %target-swift-emit-module-interface(%t/ClientFixed.swiftinterface) %t/clientFileA-Swift5.swift %t/clientFileB.swift -I %t
2525
// RUN: %target-swift-typecheck-module-from-interface(%t/ClientFixed.swiftinterface) -I %t
2626

27+
/// The inserted missing imports should be aliased.
28+
// RUN: %target-swift-emit-module-interface(%t/ClientFixed.swiftinterface) %t/clientFileA-Swift5.swift %t/clientFileB.swift -I %t -alias-module-names-in-module-interface
29+
// RUN: %target-swift-typecheck-module-from-interface(%t/ClientFixed.swiftinterface) -I %t
30+
// RUN: cat %t/ClientFixed.swiftinterface | %FileCheck -check-prefix ALIASED %s
31+
// ALIASED: import Module___libB
32+
2733
// REQUIRES: asserts
2834

2935
// BEGIN empty.swift

tools/SourceKit/lib/SwiftLang/SwiftEditorInterfaceGen.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,10 @@ class AnnotatingPrinter : public StreamPrinter {
185185
StreamPrinter::printTypeRef(T, TD, Name, NameContext);
186186
}
187187

188-
void printModuleRef(ModuleEntity Mod, Identifier Name,
189-
const PrintOptions &Options) override {
188+
void printModuleRef(ModuleEntity Mod, Identifier Name) override {
190189
unsigned StartOffset = OS.tell();
191190
Info.References.emplace_back(Mod, StartOffset, Name.str().size());
192-
StreamPrinter::printModuleRef(Mod, Name, Options);
191+
StreamPrinter::printModuleRef(Mod, Name);
193192
}
194193
};
195194

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,10 +2824,9 @@ class AnnotatingPrinter : public StreamPrinter {
28242824
StreamPrinter::printTypeRef(T, TD, Name, NameContext);
28252825
OS << "</ref>";
28262826
}
2827-
void printModuleRef(ModuleEntity Mod, Identifier Name,
2828-
const PrintOptions &Options) override {
2827+
void printModuleRef(ModuleEntity Mod, Identifier Name) override {
28292828
OS << "<ref:module>";
2830-
StreamPrinter::printModuleRef(Mod, Name, Options);
2829+
StreamPrinter::printModuleRef(Mod, Name);
28312830
OS << "</ref>";
28322831
}
28332832
};

0 commit comments

Comments
 (0)