Skip to content

Commit 51b71c7

Browse files
Merge pull request #36829 from apple/QuietMisdreavus/serialize-aware-cache
[AST] make ASTContext RawComment cache aware of serialization
2 parents f85633a + 2745247 commit 51b71c7

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,8 +1170,8 @@ class ASTContext final {
11701170

11711171
private:
11721172
friend Decl;
1173-
Optional<RawComment> getRawComment(const Decl *D);
1174-
void setRawComment(const Decl *D, RawComment RC);
1173+
Optional<std::pair<RawComment, bool>> getRawComment(const Decl *D);
1174+
void setRawComment(const Decl *D, RawComment RC, bool FromSerialized);
11751175

11761176
Optional<StringRef> getBriefComment(const Decl *D);
11771177
void setBriefComment(const Decl *D, StringRef Comment);

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ struct ASTContext::Implementation {
285285
ClangModuleLoader *TheDWARFModuleLoader = nullptr;
286286

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

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

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

20282028
return Known->second;
20292029
}
20302030

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

20352035
Optional<StringRef> ASTContext::getBriefComment(const Decl *D) {

lib/AST/RawComment.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,16 @@ RawComment Decl::getRawComment(bool SerializedOK) const {
137137

138138
// Check the cache in ASTContext.
139139
auto &Context = getASTContext();
140-
if (Optional<RawComment> RC = Context.getRawComment(this))
141-
return RC.getValue();
140+
if (Optional<std::pair<RawComment, bool>> RC = Context.getRawComment(this)) {
141+
auto P = RC.getValue();
142+
if (!SerializedOK || P.second)
143+
return P.first;
144+
}
142145

143146
// Check the declaration itself.
144147
if (auto *Attr = getAttrs().getAttribute<RawDocCommentAttr>()) {
145148
RawComment Result = toRawComment(Context, Attr->getCommentRange());
146-
Context.setRawComment(this, Result);
149+
Context.setRawComment(this, Result, true);
147150
return Result;
148151
}
149152

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

174177
if (!RC.isEmpty()) {
175-
Context.setRawComment(this, RC);
178+
Context.setRawComment(this, RC, true);
176179
return RC;
177180
}
178181
}
179182
}
180183
}
181184

182185
if (Optional<CommentInfo> C = Unit->getCommentForDecl(this)) {
183-
Context.setRawComment(this, C->Raw);
186+
Context.setRawComment(this, C->Raw, false);
184187
return C->Raw;
185188
}
186189

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %empty-directory(%t)
2+
// 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
3+
// RUN: %FileCheck %s --input-file %t/BatchMode.symbols.json
4+
5+
// changes to the doc comment cache created situations where building in batch mode caused symbol
6+
// graphs to lose source range information for individual doc comment lines.
7+
8+
// CHECK: "range":{"start":{"line":[[# @LINE]],"character":4},"end":{"line":[[# @LINE]],"character":22}}
9+
/// This is some func.
10+
public func someFunc() {}

test/SymbolGraph/verbose.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66
// RUN: %empty-directory(%t)
77
// 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
88

9-
// rdar://76461340
10-
// REQUIRES: rdar76461340
11-
129
// QUIET-NOT: Emitting symbol graph for module file
13-
// QUIET-NOT: 2 top-level declarations in this module.
14-
// QUIET-NOT: Found 1 symbols and 0 relationships.
10+
// QUIET-NOT: {{[[:digit:]]}} top-level declarations in this module.
11+
// QUIET-NOT: Found {{[[:digit:]]}} symbols and {{[[:digit:]]}} relationships.
1512

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

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

2421
public func someFunc() {}

0 commit comments

Comments
 (0)