Skip to content

[AST] make ASTContext RawComment cache aware of serialization #36829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,8 @@ class ASTContext final {

private:
friend Decl;
Optional<RawComment> getRawComment(const Decl *D);
void setRawComment(const Decl *D, RawComment RC);
Optional<std::pair<RawComment, bool>> getRawComment(const Decl *D);
void setRawComment(const Decl *D, RawComment RC, bool FromSerialized);

Optional<StringRef> getBriefComment(const Decl *D);
void setBriefComment(const Decl *D, StringRef Comment);
Expand Down
8 changes: 4 additions & 4 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ struct ASTContext::Implementation {
ClangModuleLoader *TheDWARFModuleLoader = nullptr;

/// Map from Swift declarations to raw comments.
llvm::DenseMap<const Decl *, RawComment> RawComments;
llvm::DenseMap<const Decl *, std::pair<RawComment, bool>> RawComments;

/// Map from Swift declarations to brief comments.
llvm::DenseMap<const Decl *, StringRef> BriefComments;
Expand Down Expand Up @@ -2020,16 +2020,16 @@ ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
return TheStdlibModule;
}

Optional<RawComment> ASTContext::getRawComment(const Decl *D) {
Optional<std::pair<RawComment, bool>> ASTContext::getRawComment(const Decl *D) {
auto Known = getImpl().RawComments.find(D);
if (Known == getImpl().RawComments.end())
return None;

return Known->second;
}

void ASTContext::setRawComment(const Decl *D, RawComment RC) {
getImpl().RawComments[D] = RC;
void ASTContext::setRawComment(const Decl *D, RawComment RC, bool FromSerialized) {
getImpl().RawComments[D] = std::make_pair(RC, FromSerialized);
}

Optional<StringRef> ASTContext::getBriefComment(const Decl *D) {
Expand Down
13 changes: 8 additions & 5 deletions lib/AST/RawComment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,16 @@ RawComment Decl::getRawComment(bool SerializedOK) const {

// Check the cache in ASTContext.
auto &Context = getASTContext();
if (Optional<RawComment> RC = Context.getRawComment(this))
return RC.getValue();
if (Optional<std::pair<RawComment, bool>> RC = Context.getRawComment(this)) {
auto P = RC.getValue();
if (!SerializedOK || P.second)
return P.first;
}

// Check the declaration itself.
if (auto *Attr = getAttrs().getAttribute<RawDocCommentAttr>()) {
RawComment Result = toRawComment(Context, Attr->getCommentRange());
Context.setRawComment(this, Result);
Context.setRawComment(this, Result, true);
return Result;
}

Expand Down Expand Up @@ -172,15 +175,15 @@ RawComment Decl::getRawComment(bool SerializedOK) const {
auto RC = RawComment(Context.AllocateCopy(llvm::makeArrayRef(SRCs)));

if (!RC.isEmpty()) {
Context.setRawComment(this, RC);
Context.setRawComment(this, RC, true);
return RC;
}
}
}
}

if (Optional<CommentInfo> C = Unit->getCommentForDecl(this)) {
Context.setRawComment(this, C->Raw);
Context.setRawComment(this, C->Raw, false);
return C->Raw;
}

Expand Down
10 changes: 10 additions & 0 deletions test/SymbolGraph/Symbols/DocComment/BatchMode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name BatchMode -emit-module-path %t/BatchMode.swiftmodule -emit-symbol-graph -emit-symbol-graph-dir %t -O -enable-batch-mode
// RUN: %FileCheck %s --input-file %t/BatchMode.symbols.json

// changes to the doc comment cache created situations where building in batch mode caused symbol
// graphs to lose source range information for individual doc comment lines.

// CHECK: "range":{"start":{"line":[[# @LINE]],"character":4},"end":{"line":[[# @LINE]],"character":22}}
/// This is some func.
public func someFunc() {}
15 changes: 6 additions & 9 deletions test/SymbolGraph/verbose.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -module-name verbose -emit-module -emit-module-path %t/verbose.swiftmodule -emit-symbol-graph -emit-symbol-graph-dir %t | %FileCheck %s -check-prefix=DRIVER --allow-empty

// rdar://76461340
// REQUIRES: rdar76461340

// QUIET-NOT: Emitting symbol graph for module file
// QUIET-NOT: 2 top-level declarations in this module.
// QUIET-NOT: Found 1 symbols and 0 relationships.
// QUIET-NOT: {{[[:digit:]]}} top-level declarations in this module.
// QUIET-NOT: Found {{[[:digit:]]}} symbols and {{[[:digit:]]}} relationships.

// VERBOSE: Emitting symbol graph for module file
// VERBOSE: 2 top-level declarations in this module.
// VERBOSE: Found 1 symbols and 0 relationships.
// VERBOSE: {{[[:digit:]]}} top-level declarations in this module.
// VERBOSE: Found {{[[:digit:]]}} symbols and {{[[:digit:]]}} relationships.

// DRIVER-NOT: Emitting symbol graph for module file
// DRIVER-NOT: 2 top-level declarations in this module.
// DRIVER-NOT: Found 1 symbols and 0 relationships.
// DRIVER-NOT: {{[[:digit:]]}} top-level declarations in this module.
// DRIVER-NOT: Found {{[[:digit:]]}} symbols and {{[[:digit:]]}} relationships.

public func someFunc() {}