Skip to content

Commit 9fc8bba

Browse files
committed
[ASTGen] Fix 'RawDocCommentAttr' generation
* Generate only when 'AttachCommentsToDecls' lang option is enabled. * Fix the range.
1 parent c90768f commit 9fc8bba

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedEndianness : size_t {
317317
EndianBig,
318318
};
319319

320+
SWIFT_NAME("getter:BridgedASTContext.langOptsAttachCommentsToDecls(self:)")
321+
bool BridgedASTContext_langOptsAttachCommentsToDecls(
322+
BridgedASTContext cContext);
323+
320324
SWIFT_NAME("getter:BridgedASTContext.langOptsTargetEndianness(self:)")
321325
BridgedEndianness BridgedASTContext_langOptsTargetEndianness(BridgedASTContext cContext);
322326

lib/AST/Bridging/ASTContextBridging.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ BridgedASTContext_langOptsTargetPointerBitWidth(BridgedASTContext cContext) {
9898
: 0;
9999
}
100100

101+
bool BridgedASTContext_langOptsAttachCommentsToDecls(
102+
BridgedASTContext cContext) {
103+
return cContext.unbridged().LangOpts.AttachCommentsToDecls;
104+
}
105+
101106
BridgedEndianness
102107
BridgedASTContext_langOptsTargetEndianness(BridgedASTContext cContext) {
103108
return cContext.unbridged().LangOpts.Target.isLittleEndian() ? EndianLittle

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@ extension ASTGenVisitor {
3030
var staticLoc: BridgedSourceLoc = nil
3131

3232
// Comments.
33-
if let firstTok = node.firstToken(viewMode: .sourceAccurate) {
33+
COMMENT: if
34+
self.ctx.langOptsAttachCommentsToDecls,
35+
let firstTok = node.firstToken(viewMode: .sourceAccurate)
36+
{
3437
var pos = firstTok.position
3538
for p in firstTok.leadingTrivia {
3639
switch p {
37-
case .docLineComment, .docBlockComment:
38-
let range = self.generateCharSourceRange(start: pos, length: p.sourceLength)
40+
// 'RawDocCommentAttr' takes the range '[start of any comments, start of the token text)'.
41+
case .docLineComment, .docBlockComment, .lineComment, .blockComment:
42+
let commentLength = firstTok.positionAfterSkippingLeadingTrivia.utf8Offset - pos.utf8Offset
43+
let range = self.generateCharSourceRange(start: pos, length: SourceLength(utf8Length: commentLength))
3944
let attr = BridgedRawDocCommentAttr.createParsed(self.ctx, range: range)
4045
attrs.add(attr.asDeclAttribute)
46+
break COMMENT
4147
default:
4248
break
4349
}

0 commit comments

Comments
 (0)