Skip to content

Commit 8d28ed4

Browse files
authored
Merge pull request #76269 from xymus/public-module-name
Diagnostics: Intro the public module name concept to hide support modules from clients
2 parents c6f81cb + c70162c commit 8d28ed4

18 files changed

+210
-3
lines changed

include/swift/AST/FileUnit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
335335
return getParentModule()->getRealName().str();
336336
}
337337

338+
/// Returns the public facing name of this module, only if it is set
339+
/// explicitly.
340+
virtual StringRef getPublicModuleName() const {
341+
return {};
342+
}
343+
338344
SWIFT_DEBUG_DUMPER(dumpDisplayDecls());
339345
SWIFT_DEBUG_DUMPER(dumpTopLevelDecls());
340346

include/swift/AST/Module.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ class ModuleDecl
243243
/// Module name to use when referenced in clients module interfaces.
244244
mutable Identifier ExportAsName;
245245

246+
mutable Identifier PublicModuleName;
247+
246248
public:
247249
/// Produces the components of a given module's full name in reverse order.
248250
///
@@ -502,6 +504,21 @@ class ModuleDecl
502504
ExportAsName = name;
503505
}
504506

507+
/// Public facing name for this module in diagnostics and documentation.
508+
///
509+
/// This always returns a valid name as it defaults to the module name if
510+
/// no public module name is set.
511+
///
512+
/// If `onlyIfImported`, return the normal module name when the module
513+
/// corresponding to the public module name isn't imported. Users working
514+
/// in between both modules will then see the normal module name,
515+
/// this may be more useful for diagnostics at that level.
516+
Identifier getPublicModuleName(bool onlyIfImported) const;
517+
518+
void setPublicModuleName(Identifier name) {
519+
PublicModuleName = name;
520+
}
521+
505522
/// Retrieve the actual module name of an alias used for this module (if any).
506523
///
507524
/// For example, if '-module-alias Foo=Bar' is passed in when building the main module,

include/swift/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class FrontendOptions {
7070
/// Module name to use when referenced in clients module interfaces.
7171
std::string ExportAsName;
7272

73+
/// The public facing name of the module to build.
74+
std::string PublicModuleName;
75+
7376
/// Arguments which should be passed in immediate mode.
7477
std::vector<std::string> ImmediateArgv;
7578

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,9 @@ def package_name : Separate<["-"], "package-name">,
586586
def export_as : Separate<["-"], "export-as">,
587587
Flags<[FrontendOption, ModuleInterfaceOption]>,
588588
HelpText<"Module name to use when referenced in clients module interfaces">;
589+
def public_module_name : Separate<["-"], "public-module-name">,
590+
Flags<[FrontendOption, ModuleInterfaceOptionIgnorable]>,
591+
HelpText<"Public facing module name to use in diagnostics and documentation">;
589592

590593
def emit_module : Flag<["-"], "emit-module">,
591594
Flags<[FrontendOption, NoInteractiveOption, SupplementaryOutput]>,

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ class SerializedASTFile final : public LoadedFile {
531531

532532
virtual StringRef getExportedModuleName() const override;
533533

534+
virtual StringRef getPublicModuleName() const override;
535+
534536
ValueDecl *getMainDecl() const override;
535537

536538
bool hasEntryPoint() const override;

include/swift/Serialization/Validation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class ExtendedValidationInfo {
129129
StringRef ModuleABIName;
130130
StringRef ModulePackageName;
131131
StringRef ExportAsName;
132+
StringRef PublicModuleName;
132133
CXXStdlibKind CXXStdlib;
133134
struct {
134135
unsigned ArePrivateImportsEnabled : 1;
@@ -230,6 +231,9 @@ class ExtendedValidationInfo {
230231
StringRef getModulePackageName() const { return ModulePackageName; }
231232
void setModulePackageName(StringRef name) { ModulePackageName = name; }
232233

234+
StringRef getPublicModuleName() const { return PublicModuleName; }
235+
void setPublicModuleName(StringRef name) { PublicModuleName = name; }
236+
233237
StringRef getExportAsName() const { return ExportAsName; }
234238
void setExportAsName(StringRef name) { ExportAsName = name; }
235239

lib/AST/DiagnosticEngine.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,9 @@ static void formatDiagnosticArgument(StringRef Modifier,
847847
// Figure out the name we want to print.
848848
DeclName name;
849849
if (includeName) {
850-
if (auto VD = dyn_cast<ValueDecl>(D))
850+
if (auto MD = dyn_cast<ModuleDecl>(D))
851+
name = MD->getPublicModuleName(/*onlyIfImported=*/true);
852+
else if (auto VD = dyn_cast<ValueDecl>(D))
851853
name = VD->getName();
852854
else if (auto PGD = dyn_cast<PrecedenceGroupDecl>(D))
853855
name = PGD->getName();
@@ -1419,7 +1421,9 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
14191421
// build the name of the buffer.
14201422
SmallVector<StringRef, 4> nameComponents;
14211423
while (dc) {
1422-
nameComponents.push_back(cast<ModuleDecl>(dc)->getName().str());
1424+
auto publicName = cast<ModuleDecl>(dc)->
1425+
getPublicModuleName(/*onlyIfImported*/true);
1426+
nameComponents.push_back(publicName.str());
14231427
dc = dc->getParent();
14241428
}
14251429

lib/AST/Module.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,19 @@ ImportedModule::removeDuplicates(SmallVectorImpl<ImportedModule> &imports) {
19221922
imports.erase(last, imports.end());
19231923
}
19241924

1925+
Identifier ModuleDecl::getPublicModuleName(bool onlyIfImported) const {
1926+
if (!PublicModuleName.empty()) {
1927+
if (!onlyIfImported)
1928+
return PublicModuleName;
1929+
1930+
bool publicModuleIsImported =
1931+
getASTContext().getModuleByIdentifier(PublicModuleName);
1932+
if (publicModuleIsImported)
1933+
return PublicModuleName;
1934+
}
1935+
return getName();
1936+
}
1937+
19251938
Identifier ModuleDecl::getRealName() const {
19261939
// This will return the real name for an alias (if used) or getName()
19271940
return getASTContext().getRealModuleName(getName());

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ bool ArgsToFrontendOptionsConverter::convert(
288288
Opts.ExportAsName = exportAs;
289289
}
290290

291+
if (const Arg *A = Args.getLastArg(OPT_public_module_name))
292+
Opts.PublicModuleName = A->getValue();
293+
291294
// This must be called after computing module name, module abi name,
292295
// and module link name. If computing module aliases is unsuccessful,
293296
// return early.

lib/Frontend/Frontend.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,10 @@ ModuleDecl *CompilerInstance::getMainModule() const {
14651465
MainModule->setExportAsName(getASTContext().getIdentifier(
14661466
Invocation.getFrontendOptions().ExportAsName));
14671467
}
1468+
if (!Invocation.getFrontendOptions().PublicModuleName.empty()) {
1469+
MainModule->setPublicModuleName(getASTContext().getIdentifier(
1470+
Invocation.getFrontendOptions().PublicModuleName));
1471+
}
14681472
if (Invocation.getFrontendOptions().EnableLibraryEvolution)
14691473
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
14701474
if (Invocation.getLangOptions().isSwiftVersionAtLeast(6))

0 commit comments

Comments
 (0)