Skip to content

Commit 939fe1c

Browse files
add @_nodoc to @_exported import statements
1 parent 383552c commit 939fe1c

File tree

8 files changed

+45
-3
lines changed

8 files changed

+45
-3
lines changed

docs/ReferenceGuides/UnderscoredAttributes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,9 @@ marked with this attribute are treated as if they were given an underscored
489489
name, meaning they are still included in a symbol graph with a "minimum access
490490
level" of `internal` or lower.
491491

492+
This attribute can also be attached to `@_exported import` statements to prevent
493+
symbols imported with this statement from appearing in symbol graphs.
494+
492495
## `@_nonEphemeral`
493496

494497
Marks a function parameter that cannot accept a temporary pointer produced from

include/swift/AST/Import.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ enum class ImportFlags {
8484
/// concurrency.
8585
Preconcurrency = 0x20,
8686

87+
/// The module is being exported, but its contents should not appear in a
88+
/// symbol graph.
89+
NoDoc = 0x40,
90+
8791
/// Used for DenseMap.
8892
Reserved = 0x80
8993
};

include/swift/AST/Module.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,8 @@ inline SourceLoc extractNearestSourceLoc(const ModuleDecl *mod) {
951951
/// Collects modules that this module imports via `@_exported import`.
952952
void collectParsedExportedImports(const ModuleDecl *M,
953953
SmallPtrSetImpl<ModuleDecl *> &Imports,
954-
llvm::SmallDenseMap<ModuleDecl *, SmallPtrSet<Decl *, 4>, 4> &QualifiedImports);
954+
llvm::SmallDenseMap<ModuleDecl *, SmallPtrSet<Decl *, 4>, 4> &QualifiedImports,
955+
bool DropNoDoc = false);
955956

956957
} // end namespace swift
957958

lib/AST/Module.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,14 @@ bool ModuleDecl::shouldCollectDisplayDecls() const {
794794

795795
void swift::collectParsedExportedImports(const ModuleDecl *M,
796796
SmallPtrSetImpl<ModuleDecl *> &Imports,
797-
llvm::SmallDenseMap<ModuleDecl *, SmallPtrSet<Decl *, 4>, 4> &QualifiedImports) {
797+
llvm::SmallDenseMap<ModuleDecl *, SmallPtrSet<Decl *, 4>, 4> &QualifiedImports,
798+
bool DropNoDoc) {
798799
for (const FileUnit *file : M->getFiles()) {
799800
if (const SourceFile *source = dyn_cast<SourceFile>(file)) {
800801
if (source->hasImports()) {
801802
for (auto import : source->getImports()) {
802803
if (import.options.contains(ImportFlags::Exported) &&
804+
!(DropNoDoc && import.options.contains(ImportFlags::NoDoc)) &&
803805
import.module.importedModule->shouldCollectDisplayDecls()) {
804806
auto *TheModule = import.module.importedModule;
805807

lib/AST/TypeCheckRequests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,8 @@ void swift::simple_display(llvm::raw_ostream &out,
14591459

14601460
if (import.options.contains(ImportFlags::Exported))
14611461
out << " exported";
1462+
if (import.options.contains(ImportFlags::NoDoc))
1463+
out << " nodoc";
14621464
if (import.options.contains(ImportFlags::Testable))
14631465
out << " testable";
14641466
if (import.options.contains(ImportFlags::ImplementationOnly))

lib/Sema/ImportResolution.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,9 @@ UnboundImport::UnboundImport(ImportDecl *ID)
530530
if (ID->isExported())
531531
import.options |= ImportFlags::Exported;
532532

533+
if (ID->getAttrs().hasAttribute<NoDocAttr>())
534+
import.options |= ImportFlags::NoDoc;
535+
533536
if (ID->getAttrs().hasAttribute<TestableAttr>())
534537
import.options |= ImportFlags::Testable;
535538

@@ -982,6 +985,11 @@ UnboundImport::UnboundImport(
982985
bystandingOptions.contains(ImportFlags::Exported))
983986
import.options |= ImportFlags::Exported;
984987

988+
// If both are @_nodoc, the cross-import is @_nodoc.
989+
if (declaringOptions.contains(ImportFlags::NoDoc) &&
990+
bystandingOptions.contains(ImportFlags::NoDoc))
991+
import.options |= ImportFlags::NoDoc;
992+
985993
// If either are implementation-only, the cross-import is
986994
// implementation-only.
987995
if (declaringOptions.contains(ImportFlags::ImplementationOnly) ||

lib/SymbolGraphGen/SymbolGraphGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ symbolgraphgen::emitSymbolGraphForModule(ModuleDecl *M,
6060

6161
SmallPtrSet<ModuleDecl *, 4> ExportedImportedModules;
6262
llvm::SmallDenseMap<ModuleDecl *, SmallPtrSet<Decl *, 4>, 4> QualifiedImports;
63-
swift::collectParsedExportedImports(M, ExportedImportedModules, QualifiedImports);
63+
bool DropNoDoc = Options.MinimumAccessLevel > AccessLevel::Internal;
64+
swift::collectParsedExportedImports(M, ExportedImportedModules, QualifiedImports, DropNoDoc);
6465

6566
if (Options.PrintMessages)
6667
llvm::errs() << ModuleDecls.size()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %S/Inputs/ExportedImport/A.swift -module-name A -emit-module -emit-module-path %t/A.swiftmodule
3+
// RUN: %target-swift-frontend %S/Inputs/ExportedImport/B.swift -module-name B -emit-module -emit-module-path %t/B.swiftmodule
4+
5+
// RUN: %target-swift-frontend %s -module-name NoDocExportedImport -emit-module -emit-module-path /dev/null -I %t -emit-symbol-graph -emit-symbol-graph-dir %t/
6+
// RUN: %FileCheck %s --input-file %t/NoDocExportedImport.symbols.json --check-prefix PUBLIC
7+
// RUN: ls %t | %FileCheck %s --check-prefix FILES
8+
9+
@_nodoc @_exported import A
10+
@_nodoc @_exported import struct B.StructOne
11+
12+
// PUBLIC-NOT: InternalSymbolFromA
13+
// PUBLIC-NOT: StructTwo
14+
// PUBLIC-NOT: "precise":"s:1A11SymbolFromAV"
15+
// PUBLIC-NOT: "precise":"s:1B9StructOneV"
16+
17+
// FIXME: Symbols from `@_exported import` do not get emitted when using swift-symbolgraph-extract
18+
// This is tracked by https://bugs.swift.org/browse/SR-15921.
19+
20+
// FILES-NOT: [email protected]
21+

0 commit comments

Comments
 (0)