Skip to content

[6.0][Macros] Fix column calculation in MacroExpansionContext for plugins #2657

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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftSyntaxMacros

public struct NativeFileIDMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
return context.location(
of: node,
at: .afterLeadingTrivia,
filePathMode: .fileID
)!.file
}
}

public struct NativeFilePathMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
return context.location(
of: node,
at: .afterLeadingTrivia,
filePathMode: .filePath
)!.file
}
}

public struct NativeLineMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
return context.location(of: node)!.line
}
}

public struct NativeColumnMacro: ExpressionMacro {
public static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) -> ExprSyntax {
return context.location(of: node)!.column
}
}
4 changes: 4 additions & 0 deletions Examples/Sources/MacroExamples/Implementation/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ struct MyPlugin: CompilerPlugin {
FuncUniqueMacro.self,
MemberDeprecatedMacro.self,
MetaEnumMacro.self,
NativeColumnMacro.self,
NativeFileIDMacro.self,
NativeFilePathMacro.self,
NativeLineMacro.self,
NewTypeMacro.self,
ObservableMacro.self,
ObservablePropertyMacro.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@freestanding(expression)
public macro FileID<T: ExpressibleByStringLiteral>() -> T =
#externalMacro(
module: "MacroExamplesImplementation",
type: "NativeFileIDMacro"
)

@freestanding(expression)
public macro FilePath<T: ExpressibleByStringLiteral>() -> T =
#externalMacro(
module: "MacroExamplesImplementation",
type: "NativeFilePathMacro"
)

@freestanding(expression)
public macro Line<T: ExpressibleByIntegerLiteral>() -> T =
#externalMacro(
module: "MacroExamplesImplementation",
type: "NativeLineMacro"
)

@freestanding(expression)
public macro Column<T: ExpressibleByIntegerLiteral>() -> T =
#externalMacro(
module: "MacroExamplesImplementation",
type: "NativeColumnMacro"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import MacroExamplesInterface

func runSourceLoctionMacrosPlayground() {
print("FileID: \(#FileID as String)")
print("FilePath: \(#FilePath as String)")
print("Line: \(#Line as Int)")
print("Column: \(#Column as Int)")
}
4 changes: 4 additions & 0 deletions Examples/Sources/MacroExamples/Playground/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ runMemberMacrosPlayground()
// MARK: - Peer Macros

runPeerMacrosPlayground()

// MARK: - SourceLocationMacros

runSourceLoctionMacrosPlayground()
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ class SourceManager {
let localLocation = base.locationConverter.location(for: localPosition)

let positionOffset = base.location.offset
// NOTE '- 1' because base.location.{line|column} are 1-based.
let lineOffset = base.location.line - 1
let columnOffset = localLocation.line == 1 ? base.location.column : 0
let columnOffset = localLocation.line == 1 ? (base.location.column - 1) : 0

return SourceLocation(
// NOTE: IUO because 'localLocation' is created by a location converter
Expand Down