Skip to content

Commit 46242b1

Browse files
committed
doc-serialization: exclude non-public decls when serializing module group info.
This reduces the size of x86_64.swiftdoc about 10%. rdar://48689311
1 parent 9740a31 commit 46242b1

File tree

3 files changed

+44
-29
lines changed

3 files changed

+44
-29
lines changed

lib/Serialization/SerializeDoc.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,34 @@ static void writeDeclCommentTable(
367367
return StringRef(Mem, String.size());
368368
}
369369

370+
bool shouldSerializeDoc(Decl *D) {
371+
if (auto *VD = dyn_cast<ValueDecl>(D)) {
372+
// Skip the decl if it's not visible to clients. The use of
373+
// getEffectiveAccess is unusual here; we want to take the testability
374+
// state into account and emit documentation if and only if they are
375+
// visible to clients (which means public ordinarily, but
376+
// public+internal when testing enabled).
377+
if (VD->getEffectiveAccess() < swift::AccessLevel::Public)
378+
return false;
379+
}
380+
381+
// When building the stdlib we intend to serialize unusual comments.
382+
// This situation is represented by GroupContext.isEnable(). In that
383+
// case, we perform more serialization to keep track of source order.
384+
if (GroupContext.isEnable())
385+
return true;
386+
387+
// Skip the decl if it cannot have a comment.
388+
if (!D->canHaveComment())
389+
return false;
390+
391+
// Skip the decl if it does not have a comment.
392+
if (D->getRawComment().Comments.empty())
393+
return false;
394+
return true;
395+
}
396+
370397
void writeDocForExtensionDecl(ExtensionDecl *ED) {
371-
RawComment Raw = ED->getRawComment();
372-
if (Raw.Comments.empty() && !GroupContext.isEnable())
373-
return;
374398
// Compute USR.
375399
{
376400
USRBuffer.clear();
@@ -379,12 +403,14 @@ static void writeDeclCommentTable(
379403
return;
380404
}
381405
generator.insert(copyString(USRBuffer.str()),
382-
{ ED->getBriefComment(), Raw,
406+
{ ED->getBriefComment(), ED->getRawComment(),
383407
GroupContext.getGroupSequence(ED),
384408
SourceOrder++ });
385409
}
386410

387411
bool walkToDeclPre(Decl *D) override {
412+
if (!shouldSerializeDoc(D))
413+
return true;
388414
if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
389415
writeDocForExtensionDecl(ED);
390416
return true;
@@ -394,29 +420,6 @@ static void writeDeclCommentTable(
394420
if (!VD)
395421
return true;
396422

397-
RawComment Raw = VD->getRawComment();
398-
// When building the stdlib we intend to serialize unusual comments.
399-
// This situation is represented by GroupContext.isEnable(). In that
400-
// case, we perform fewer serialization checks.
401-
if (!GroupContext.isEnable()) {
402-
// Skip the decl if it cannot have a comment.
403-
if (!VD->canHaveComment()) {
404-
return true;
405-
}
406-
407-
// Skip the decl if it does not have a comment.
408-
if (Raw.Comments.empty())
409-
return true;
410-
411-
// Skip the decl if it's not visible to clients. The use of
412-
// getEffectiveAccess is unusual here; we want to take the testability
413-
// state into account and emit documentation if and only if they are
414-
// visible to clients (which means public ordinarily, but
415-
// public+internal when testing enabled).
416-
if (VD->getEffectiveAccess() < swift::AccessLevel::Public)
417-
return true;
418-
}
419-
420423
// Compute USR.
421424
{
422425
USRBuffer.clear();
@@ -426,7 +429,7 @@ static void writeDeclCommentTable(
426429
}
427430

428431
generator.insert(copyString(USRBuffer.str()),
429-
{ VD->getBriefComment(), Raw,
432+
{ VD->getBriefComment(), D->getRawComment(),
430433
GroupContext.getGroupSequence(VD),
431434
SourceOrder++ });
432435
return true;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"group1": ["group_info_diags.swift"]
3+
}

test/Serialization/group_info_diags.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
// RUN: not %target-swift-frontend -emit-module %s -module-name HasArray -o %t -module-cache-path %t/mcp -group-info-path %S/Inputs/corrupted_group_info.json -emit-module-doc &> %t/result.txt
55
// RUN: %FileCheck %s -check-prefix=CORRUPTED < %t/result.txt
66

7-
public protocol P {}
7+
// RUN: %target-swift-frontend -emit-module %s -module-name HasArray -o %t -module-cache-path %t/mcp -group-info-path %S/Inputs/group_info.json -emit-module-doc &> %t/result.txt
8+
// RUN: strings %t/HasArray.swiftdoc > %t/doc_strings.txt
9+
// RUN: %FileCheck %s -check-prefix=INCLUDED < %t/doc_strings.txt
10+
// RUN: %FileCheck %s -check-prefix=EXCLUDED < %t/doc_strings.txt
11+
12+
public protocol PublicProtocol {}
13+
14+
internal class InternalClass {}
815

916
// MISSING: error: cannot find group info file at path
1017
// CORRUPTED: error: cannot parse group info file at path
18+
// EXCLUDED-NOT: InternalClass
19+
// INCLUDED: PublicProtocol

0 commit comments

Comments
 (0)