Skip to content
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
40 changes: 40 additions & 0 deletions test/SourceKit/SemanticTokens/issue-85705.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %host-build-swift -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %t/macro.swift -g -no-toolchain-stdlib-rpath

// RUN: %sourcekitd-test -req=semantic-tokens %t/main.swift -- %t/main.swift -load-plugin-library %t/%target-library-name(MacroDefinition) > %t/result.response
// RUN: diff -u %t/expected.response %t/result.response

// REQUIRES: swift_swift_parser

//--- macro.swift
import SwiftSyntax
import SwiftSyntaxMacros

public struct LogMacro: BodyMacro {
public static func expansion(
of node: AttributeSyntax,
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
in context: some MacroExpansionContext,
) throws -> [CodeBlockItemSyntax] {
return ["print()"]
}
}
//--- main.swift
@attached(body)
public macro log() = #externalMacro(module: "MacroDefinition", type: "LogMacro")

// Make sure we don't walk into the macro expanded body here.
@log func foo() {}

//--- expected.response
{
key.semantic_tokens: [
{
key.kind: source.lang.swift.ref.macro,
key.offset: 161,
key.length: 3
}
]
}
9 changes: 4 additions & 5 deletions tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1027,12 +1027,11 @@ class SemanticAnnotator : public SourceEntityWalker {
if (!Range.isValid())
return;

// If we are walking into macro expansions, make sure we only report ranges
// from the requested buffer, not any buffers of child macro expansions.
if (IsWalkingMacroExpansionBuffer &&
SM.findBufferContainingLoc(Range.getStart()) != BufferID) {
// Make sure we only report from the requested buffer, not any buffers of
// child macro expansions.
if (SM.findBufferContainingLoc(Range.getStart()) != BufferID)
return;
}

unsigned ByteOffset = SM.getLocOffsetInBuffer(Range.getStart(), BufferID);
unsigned Length = Range.getByteLength();
auto Kind = ContextFreeCodeCompletionResult::getCodeCompletionDeclKind(D);
Expand Down