Skip to content

Commit 5ddeac2

Browse files
authored
Merge pull request #85961 from hamishknight/skip-body-macro-6.3
[6.3] [SourceKit] Always check buffer in `SemanticAnnotator`
2 parents c220020 + 7c11824 commit 5ddeac2

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %host-build-swift -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %t/macro.swift -g -no-toolchain-stdlib-rpath
5+
6+
// RUN: %sourcekitd-test -req=semantic-tokens %t/main.swift -- %t/main.swift -load-plugin-library %t/%target-library-name(MacroDefinition) > %t/result.response
7+
// RUN: diff -u %t/expected.response %t/result.response
8+
9+
// REQUIRES: swift_swift_parser
10+
11+
//--- macro.swift
12+
import SwiftSyntax
13+
import SwiftSyntaxMacros
14+
15+
public struct LogMacro: BodyMacro {
16+
public static func expansion(
17+
of node: AttributeSyntax,
18+
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
19+
in context: some MacroExpansionContext,
20+
) throws -> [CodeBlockItemSyntax] {
21+
return ["print()"]
22+
}
23+
}
24+
//--- main.swift
25+
@attached(body)
26+
public macro log() = #externalMacro(module: "MacroDefinition", type: "LogMacro")
27+
28+
// Make sure we don't walk into the macro expanded body here.
29+
@log func foo() {}
30+
31+
//--- expected.response
32+
{
33+
key.semantic_tokens: [
34+
{
35+
key.kind: source.lang.swift.ref.macro,
36+
key.offset: 161,
37+
key.length: 3
38+
}
39+
]
40+
}

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,12 +1027,11 @@ class SemanticAnnotator : public SourceEntityWalker {
10271027
if (!Range.isValid())
10281028
return;
10291029

1030-
// If we are walking into macro expansions, make sure we only report ranges
1031-
// from the requested buffer, not any buffers of child macro expansions.
1032-
if (IsWalkingMacroExpansionBuffer &&
1033-
SM.findBufferContainingLoc(Range.getStart()) != BufferID) {
1030+
// Make sure we only report from the requested buffer, not any buffers of
1031+
// child macro expansions.
1032+
if (SM.findBufferContainingLoc(Range.getStart()) != BufferID)
10341033
return;
1035-
}
1034+
10361035
unsigned ByteOffset = SM.getLocOffsetInBuffer(Range.getStart(), BufferID);
10371036
unsigned Length = Range.getByteLength();
10381037
auto Kind = ContextFreeCodeCompletionResult::getCodeCompletionDeclKind(D);

0 commit comments

Comments
 (0)