diff --git a/include/swift/AST/PrintOptions.h b/include/swift/AST/PrintOptions.h index 1eda0fb4b8d..7bca650afa8 100644 --- a/include/swift/AST/PrintOptions.h +++ b/include/swift/AST/PrintOptions.h @@ -279,11 +279,26 @@ public: /// Whether to protocol-qualify DependentMemberTypes. bool ProtocolQualifiedDependentMemberTypes = false; - /// If true, printed module names will use the "exported" name, which may be - /// different from the regular name. + enum class ExportedModuleNameUsage : uint8_t { + /// If there is an exported module name, always use it instead of the + /// regular name. + Always, + + /// If there is an exported module name and the named module is the main + /// module or has been loaded by the main module, use it instead of the + /// regular name. + IfLoaded, + + /// Always use the regular name. + Never, + }; + + /// Conditions under which printed module names will use the "exported" name, + /// which may be different from the regular name. /// /// \see FileUnit::getExportedModuleName - bool UseExportedModuleNames = false; + ExportedModuleNameUsage UseExportedModuleNames = + ExportedModuleNameUsage::Never; /// If true, printed module names will use the "public" (for documentation) /// name, which may be different from the regular name. @@ -781,7 +796,8 @@ public: bool preferTypeRepr, bool printFullConvention, InterfaceMode interfaceMode, - bool useExportedModuleNames, + ExportedModuleNameUsage + useExportedModuleNames, bool aliasModuleNames, llvm::SmallSet *aliasModuleNamesTargets diff --git a/lib/AST/ASTPrinter.cpp b/lib/AST/ASTPrinter.cpp index cc902dc7885..d4f53b82d72 100644 --- a/lib/AST/ASTPrinter.cpp +++ b/lib/AST/ASTPrinter.cpp @@ -273,7 +273,8 @@ PrintOptions PrintOptions::printSwiftInterfaceFile(ModuleDecl *ModuleToPrint, bool preferTypeRepr, bool printFullConvention, InterfaceMode interfaceMode, - bool useExportedModuleNames, + ExportedModuleNameUsage + useExportedModuleNames, bool aliasModuleNames, llvm::SmallSet *aliasModuleNamesTargets @@ -6085,6 +6086,23 @@ class TypePrinter : public TypeVisitorgetVisibleClangModules(Options.InterfaceContentKind); } + bool shouldUseExportedModuleName(ASTContext &ctx, + StringRef exportedModuleName) { + if (exportedModuleName.empty()) + return false; + + switch (Options.UseExportedModuleNames) { + case PrintOptions::ExportedModuleNameUsage::Always: + return true; + case PrintOptions::ExportedModuleNameUsage::Never: + return false; + case PrintOptions::ExportedModuleNameUsage::IfLoaded: + return ctx.getLoadedModule(ctx.getIdentifier(exportedModuleName)); + } + + llvm_unreachable("Unrecognized ExportedModuleNameUsage"); + } + void printModuleContext(GenericTypeDecl *TyDecl) { FileUnit *File = cast(TyDecl->getModuleScopeContext()); ModuleDecl *Mod = File->getParentModule(); @@ -6122,7 +6140,7 @@ class TypePrinter : public TypeVisitorgetRealName(); - if (Options.UseExportedModuleNames && !ExportedModuleName.empty()) { + if (shouldUseExportedModuleName(Mod->getASTContext(), ExportedModuleName)) { Name = Mod->getASTContext().getIdentifier(ExportedModuleName); } diff --git a/lib/Frontend/ModuleInterfaceSupport.cpp b/lib/Frontend/ModuleInterfaceSupport.cpp index 72b8d489738..f3f99d01ad0 100644 --- a/lib/Frontend/ModuleInterfaceSupport.cpp +++ b/lib/Frontend/ModuleInterfaceSupport.cpp @@ -892,7 +892,9 @@ bool swift::emitSwiftInterface(raw_ostream &out, DisableModuleSelectorsInModuleInterface)) useModuleSelectors = false; - bool useExportedModuleNames = Opts.printPublicInterface(); + auto useExportedModuleNames = Opts.printPublicInterface() + ? PrintOptions::ExportedModuleNameUsage::Always + : PrintOptions::ExportedModuleNameUsage::IfLoaded; const PrintOptions printOptions = PrintOptions::printSwiftInterfaceFile( M, useModuleSelectors, Opts.PreserveTypesAsWritten, Opts.PrintFullConvention, diff --git a/test/ModuleInterface/export-as-in-swiftinterfaces.swift b/test/ModuleInterface/export-as-in-swiftinterfaces.swift index 58a816301f8..f616da1bf68 100644 --- a/test/ModuleInterface/export-as-in-swiftinterfaces.swift +++ b/test/ModuleInterface/export-as-in-swiftinterfaces.swift @@ -12,8 +12,8 @@ // RUN: -emit-module-interface-path %t/Exported.swiftinterface \ // RUN: -emit-private-module-interface-path %t/Exported.private.swiftinterface // RUN: %target-swift-typecheck-module-from-interface(%t/Exported.private.swiftinterface) -module-name Exported -I %t -// RUN: cat %t/Exported.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s -// RUN: cat %t/Exported.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s +// RUN: %FileCheck -input-file=%t/Exported.swiftinterface -check-prefix=CHECK-USE-EXPORTER %s +// RUN: %FileCheck -input-file=%t/Exported.private.swiftinterface -check-prefix=CHECK-USE-EXPORTED %s /// The public swiftinterface only builds under the name of Exporter. // RUN: sed -e "s/module-name Exported/module-name Exporter/" -ibk %t/Exported.swiftinterface @@ -28,20 +28,8 @@ // RUN: -emit-private-module-interface-path %t/Exporter.private.swiftinterface // RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.swiftinterface) -I %t // RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.private.swiftinterface) -module-name Exporter -I %t -// RUN: cat %t/Exporter.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s -// RUN: cat %t/Exporter.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s - -/// Build lib with an @exported import of the exported one. -// RUN: %target-swift-frontend -emit-module %t/Exporter.swift \ -// RUN: -module-name Exporter -swift-version 5 -I %t \ -// RUN: -enable-library-evolution \ -// RUN: -emit-module-path %t/Exporter.swiftmodule \ -// RUN: -emit-module-interface-path %t/Exporter.swiftinterface \ -// RUN: -emit-private-module-interface-path %t/Exporter.private.swiftinterface -// RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.swiftinterface) -I %t -// RUN: %target-swift-typecheck-module-from-interface(%t/Exporter.private.swiftinterface) -module-name Exporter -I %t -// RUN: cat %t/Exporter.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s -// RUN: cat %t/Exporter.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s +// RUN: %FileCheck -input-file=%t/Exporter.swiftinterface -check-prefix=CHECK-USE-EXPORTER %s +// RUN: %FileCheck -input-file=%t/Exporter.private.swiftinterface -check-prefix=CHECK-USE-EXPORTER %s /// Build a client of the exporter lib. // RUN: %target-swift-frontend -emit-module %t/Client.swift \ @@ -52,8 +40,8 @@ // RUN: -emit-private-module-interface-path %t/Client.private.swiftinterface // RUN: %target-swift-typecheck-module-from-interface(%t/Client.swiftinterface) -I %t // RUN: %target-swift-typecheck-module-from-interface(%t/Client.private.swiftinterface) -module-name Client -I %t -// RUN: cat %t/Client.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s -// RUN: cat %t/Client.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s +// RUN: %FileCheck -input-file=%t/Client.swiftinterface -check-prefix=CHECK-USE-EXPORTER %s +// RUN: %FileCheck -input-file=%t/Client.private.swiftinterface -check-prefix=CHECK-USE-EXPORTER %s /// Build a client of the exporter lib against the public swiftinterface. // RUN: rm %t/Exporter.private.swiftinterface %t/Exporter.swiftmodule @@ -65,8 +53,8 @@ // RUN: -emit-private-module-interface-path %t/Client.private.swiftinterface // RUN: %target-swift-typecheck-module-from-interface(%t/Client.swiftinterface) -I %t // RUN: %target-swift-typecheck-module-from-interface(%t/Client.private.swiftinterface) -module-name Client -I %t -// RUN: cat %t/Client.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTER %s -// RUN: cat %t/Client.private.swiftinterface | %FileCheck -check-prefix=CHECK-USE-EXPORTED %s +// RUN: %FileCheck -input-file=%t/Client.swiftinterface -check-prefix=CHECK-USE-EXPORTER %s +// RUN: %FileCheck -input-file=%t/Client.private.swiftinterface -check-prefix=CHECK-USE-EXPORTER %s //--- module.modulemap module Exported {