Skip to content

Commit 383552c

Browse files
add @_nodoc attribute to drop a symbol from the symbol graph
rdar://79049241
1 parent 88304c3 commit 383552c

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

docs/ReferenceGuides/UnderscoredAttributes.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,13 @@ Fun fact: Rust has a very similar concept called
482482
including one called `Send`,
483483
which inspired the design of `Sendable`.
484484

485+
## `@_nodoc`
486+
487+
Indicates that a declaration should not be included in symbol graphs. Symbols
488+
marked with this attribute are treated as if they were given an underscored
489+
name, meaning they are still included in a symbol graph with a "minimum access
490+
level" of `internal` or lower.
491+
485492
## `@_nonEphemeral`
486493

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

include/swift/AST/Attr.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,11 @@ SIMPLE_DECL_ATTR(_alwaysEmitConformanceMetadata, AlwaysEmitConformanceMetadata,
745745
OnProtocol | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
746746
132)
747747

748+
SIMPLE_DECL_ATTR(_nodoc, NoDoc,
749+
OnAnyDecl |
750+
APIBreakingToAdd | APIStableToRemove | ABIStableToAdd | ABIStableToRemove,
751+
132)
752+
748753
// If you're adding a new underscored attribute here, please document it in
749754
// docs/ReferenceGuides/UnderscoredAttributes.md.
750755

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
157157
IGNORED_ATTR(Isolated)
158158
IGNORED_ATTR(Preconcurrency)
159159
IGNORED_ATTR(BackDeploy)
160+
IGNORED_ATTR(NoDoc)
160161
#undef IGNORED_ATTR
161162

162163
void visitAlignmentAttr(AlignmentAttr *attr) {

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,7 @@ namespace {
15191519
UNINTERESTING_ATTR(Lazy)
15201520
UNINTERESTING_ATTR(LLDBDebuggerFunction)
15211521
UNINTERESTING_ATTR(Mutating)
1522+
UNINTERESTING_ATTR(NoDoc)
15221523
UNINTERESTING_ATTR(NonMutating)
15231524
UNINTERESTING_ATTR(NonEphemeral)
15241525
UNINTERESTING_ATTR(NonObjC)

lib/SymbolGraphGen/SymbolGraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ bool SymbolGraph::isImplicitlyPrivate(const Decl *D,
582582
}
583583

584584
// Don't record effectively internal declarations if specified
585-
if (D->hasUnderscoredNaming()) {
585+
if (D->hasUnderscoredNaming() || D->getAttrs().hasAttribute<NoDocAttr>()) {
586586
// Some implicit decls from Clang with underscored names sneak in, so throw those out
587587
if (const auto *clangD = D->getClangDecl()) {
588588
if (clangD->isImplicit())
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -module-name NoDocAttribute -emit-module -emit-module-path %t/
3+
// RUN: %target-swift-symbolgraph-extract -module-name NoDocAttribute -I %t -pretty-print -output-dir %t
4+
// RUN: %FileCheck %s --input-file %t/NoDocAttribute.symbols.json --check-prefix PUBLIC
5+
6+
// RUN: %target-swift-symbolgraph-extract -module-name NoDocAttribute -I %t -pretty-print -output-dir %t -minimum-access-level internal
7+
// RUN: %FileCheck %s --input-file %t/NoDocAttribute.symbols.json --check-prefix INTERNAL
8+
9+
// RUN: %target-swift-symbolgraph-extract -module-name NoDocAttribute -I %t -pretty-print -output-dir %t -minimum-access-level private
10+
// RUN: %FileCheck %s --input-file %t/NoDocAttribute.symbols.json --check-prefix PRIVATE
11+
12+
// This test is a mirror of SkipsPublicUnderscore.swift, but using `@_nodoc`
13+
// instead of underscored names.
14+
15+
public protocol PublicProtocol {}
16+
17+
// PUBLIC-NOT: ShouldntAppear
18+
// INTERNAL-DAG: ShouldntAppear
19+
// PRIVATE-DAG: ShouldntAppear
20+
21+
@_nodoc public struct ShouldntAppear: PublicProtocol {
22+
public struct InnerShouldntAppear {}
23+
}
24+
25+
public class SomeClass {
26+
// PUBLIC-NOT: internalVar
27+
// INTERNAL-NOT: internalVar
28+
// PRIVATE-DAG: internalVar
29+
@_nodoc internal var internalVar: String = ""
30+
}
31+
32+
@_nodoc public protocol ProtocolShouldntAppear {}
33+
34+
public struct PublicStruct: ProtocolShouldntAppear {
35+
@_nodoc public struct InnerShouldntAppear {}
36+
}

0 commit comments

Comments
 (0)