Skip to content

[ASTGen] NFC: Format with swift-format #69325

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 1 commit into from
Oct 23, 2023
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
17 changes: 17 additions & 0 deletions lib/ASTGen/.swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"version": 1,
"lineLength": 120,
Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI we settled on aiming for 160 here, which motivated swiftlang/swift-syntax#1714.

How about a symlink to the swift-syntax configuration file for everything inside lib instead? Consistent formatting across our Swift repos sounds really nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll let @ahoppen confirm, but AFAIK we want to standardize on 120 swiftlang/sourcekit-lsp#890 (comment)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but AFAIK we want to standardize on 120

Oh, that’s new. Totally fine by me! I still incline to a symlink until swift-syntax transitions though. I’m wondering what we can do to standardize the Swift formatting in these repositories without having to maintain multiple config files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we agreed to use 120 columns. We should update swift-syntax to 120 as well at some point but I haven’t gotten to it yet.

I still incline to a symlink until swift-syntax transitions though

If we were to symlink swift-syntax’s config file to swift, it means that you can’t work on swift-syntax as a standalone package anymore. Similar for sourcekit-lsp. I think the best thing to do, is to have a separate config file in each repo and add a CI check that all the config files are the same.

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were to symlink swift-syntax’s config file to swift, it means that you can’t work on swift-syntax as a standalone package anymore.

I meant the symlink to be in the swift repository and link to the swift-syntax or sourcekit-lsp config file.

I think the best thing to do, is to have a separate config file in each repo and add a CI check that all the config files are the same.

Alternatively, maybe we could create a special repo to host this kind of stuff and add it as a git submodule to other repos. Then each repo can replace their own config files with symlinks to the config files in that submodule.

"indentation": {
"spaces": 2
},
"lineBreakBeforeEachArgument": true,
"indentConditionalCompilationBlocks": false,
"rules": {
"AlwaysUseLowerCamelCase": false,
"AmbiguousTrailingClosureOverload": false,
"NoBlockComments": false,
"OrderedImports": true,
"UseLetInEveryBoundCaseVariable": false,
"UseSynthesizedInitializer": false
}
}
2 changes: 1 addition & 1 deletion lib/ASTGen/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let package = Package(
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"),
"swiftLLVMJSON"
"swiftLLVMJSON",
],
path: "Sources/ASTGen",
swiftSettings: swiftSetttings
Expand Down
17 changes: 11 additions & 6 deletions lib/ASTGen/Sources/ASTGen/ASTGen.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import CASTBridging
import CBasicBridging

// Needed to use BumpPtrAllocator
@_spi(BumpPtrAllocator)
import SwiftSyntax
@_spi(BumpPtrAllocator) import SwiftSyntax

import struct SwiftDiagnostics.Diagnostic

extension UnsafePointer {
Expand Down Expand Up @@ -107,13 +106,19 @@ struct ASTGenVisitor {
out.append(d.raw)
case .stmt(let s):
let topLevelDecl = BridgedTopLevelCodeDecl.createParsed(
self.ctx, declContext: self.declContext, startLoc: loc, stmt: s,
self.ctx,
declContext: self.declContext,
startLoc: loc,
stmt: s,
endLoc: loc
)
out.append(topLevelDecl.raw)
case .expr(let e):
let topLevelDecl = BridgedTopLevelCodeDecl.createParsed(
self.ctx, declContext: self.declContext, startLoc: loc, expr: e,
self.ctx,
declContext: self.declContext,
startLoc: loc,
expr: e,
endLoc: loc
)
out.append(topLevelDecl.raw)
Expand Down Expand Up @@ -174,7 +179,7 @@ extension ASTGenVisitor {
func generate(_ node: some SyntaxChildChoices) -> ASTNode {
return self.generate(Syntax(node))
}

func generate(_ node: Syntax) -> ASTNode {
switch node.as(SyntaxEnum.self) {
case .actorDecl(let node):
Expand Down
2 changes: 1 addition & 1 deletion lib/ASTGen/Sources/ASTGen/Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ extension Optional where Wrapped: SyntaxProtocol {
guard let self else {
return nil
}

return self.bridgedSourceLoc(in: astgen)
}
}
Expand Down
19 changes: 11 additions & 8 deletions lib/ASTGen/Sources/ASTGen/Decls.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import CASTBridging

@_spi(ExperimentalLanguageFeatures)
import SwiftSyntax
import SwiftDiagnostics
@_spi(ExperimentalLanguageFeatures) import SwiftSyntax

// MARK: - TypeDecl

Expand Down Expand Up @@ -37,7 +35,7 @@ extension ASTGenVisitor {
genericWhereClause: self.generate(node.genericWhereClause),
braceRange: BridgedSourceRange(
startToken: node.memberBlock.leftBrace,
endToken: node.memberBlock.rightBrace,
endToken: node.memberBlock.rightBrace,
in: self
)
)
Expand All @@ -63,7 +61,7 @@ extension ASTGenVisitor {
genericWhereClause: self.generate(node.genericWhereClause),
braceRange: BridgedSourceRange(
startToken: node.memberBlock.leftBrace,
endToken: node.memberBlock.rightBrace,
endToken: node.memberBlock.rightBrace,
in: self
)
)
Expand Down Expand Up @@ -339,14 +337,17 @@ extension BridgedOperatorFixity {
extension ASTGenVisitor {
func generate(_ node: OperatorDeclSyntax) -> BridgedOperatorDecl {
let (name, nameLoc) = node.name.bridgedIdentifierAndSourceLoc(in: self)
let (precedenceGroupName, precedenceGroupLoc) = (node.operatorPrecedenceAndTypes?.precedenceGroup).bridgedIdentifierAndSourceLoc(in: self)
let (precedenceGroupName, precedenceGroupLoc) = (node.operatorPrecedenceAndTypes?.precedenceGroup)
.bridgedIdentifierAndSourceLoc(in: self)

let fixity: BridgedOperatorFixity
if let value = BridgedOperatorFixity(from: node.fixitySpecifier.tokenKind) {
fixity = value
} else {
fixity = .infix
self.diagnose(Diagnostic(node: node.fixitySpecifier, message: UnexpectedTokenKindError(token: node.fixitySpecifier)))
self.diagnose(
Diagnostic(node: node.fixitySpecifier, message: UnexpectedTokenKindError(token: node.fixitySpecifier))
)
}

return .createParsed(
Expand Down Expand Up @@ -388,7 +389,9 @@ extension ASTGenVisitor {
}

func diagnoseDuplicateSyntax(_ duplicate: some SyntaxProtocol, original: some SyntaxProtocol) {
self.diagnose(Diagnostic(node: duplicate, message: DuplicateSyntaxError(duplicate: duplicate, original: original)))
self.diagnose(
Diagnostic(node: duplicate, message: DuplicateSyntaxError(duplicate: duplicate, original: original))
)
}

let body = node.groupAttributes.reduce(into: PrecedenceGroupBody()) { body, element in
Expand Down
4 changes: 3 additions & 1 deletion lib/ASTGen/Sources/ASTGen/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ struct DuplicateSyntaxError: ASTGenError {
init(duplicate: some SyntaxProtocol, original: some SyntaxProtocol) {
precondition(duplicate.kind == original.kind, "Expected duplicate and original to be of same kind")

guard let duplicateParent = duplicate.parent, let originalParent = original.parent, duplicateParent == originalParent, duplicateParent.kind.isSyntaxCollection else {
guard let duplicateParent = duplicate.parent, let originalParent = original.parent,
duplicateParent == originalParent, duplicateParent.kind.isSyntaxCollection
else {
preconditionFailure("Expected a shared syntax collection parent")
}

Expand Down
49 changes: 29 additions & 20 deletions lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ fileprivate func emitDiagnosticParts(
case .replaceLeadingTrivia(let oldToken, let newTrivia):
replaceStartLoc = bridgedSourceLoc(at: oldToken.position)
replaceEndLoc = bridgedSourceLoc(
at: oldToken.positionAfterSkippingLeadingTrivia)
at: oldToken.positionAfterSkippingLeadingTrivia
)
newText = newTrivia.description

case .replaceTrailingTrivia(let oldToken, let newTrivia):
Expand All @@ -63,7 +64,8 @@ fileprivate func emitDiagnosticParts(

newText.withBridgedString { bridgedMessage in
diag.fixItReplace(
start: replaceStartLoc, end: replaceEndLoc,
start: replaceStartLoc,
end: replaceEndLoc,
replacement: bridgedMessage
)
}
Expand Down Expand Up @@ -117,11 +119,11 @@ func emitDiagnostic(
extension DiagnosticSeverity {
var bridged: BridgedDiagnosticSeverity {
switch self {
case .error: return .error
case .note: return .note
case .warning: return .warning
case .remark: return .remark
@unknown default: return .error
case .error: return .error
case .note: return .note
case .warning: return .warning
case .remark: return .remark
@unknown default: return .error
}
}
}
Expand Down Expand Up @@ -186,7 +188,8 @@ extension SourceManager {
case .replaceTrailingTrivia(let oldToken, let newTrivia):
replaceStartLoc = bridgedSourceLoc(
for: oldToken,
at: oldToken.endPositionBeforeTrailingTrivia)
at: oldToken.endPositionBeforeTrailingTrivia
)
replaceEndLoc = bridgedSourceLoc(
for: oldToken,
at: oldToken.endPosition
Expand All @@ -196,7 +199,8 @@ extension SourceManager {

newText.withBridgedString { bridgedMessage in
diag.fixItReplace(
start: replaceStartLoc, end: replaceEndLoc,
start: replaceStartLoc,
end: replaceEndLoc,
replacement: bridgedMessage
)
}
Expand All @@ -222,11 +226,11 @@ extension SourceManager {
// Emit Fix-Its.
for fixIt in diagnostic.fixIts {
diagnoseSingle(
message: fixIt.message.message,
severity: .note,
node: diagnostic.node,
position: diagnostic.position,
fixItChanges: fixIt.changes
message: fixIt.message.message,
severity: .note,
node: diagnostic.node,
position: diagnostic.position,
fixItChanges: fixIt.changes
)
}

Expand Down Expand Up @@ -315,7 +319,8 @@ public func addQueuedSourceFile(
// Determine the parent link, for a child buffer.
let parent: (GroupedDiagnostics.SourceFileID, AbsolutePosition)?
if parentID >= 0,
let parentSourceFileID = queuedDiagnostics.pointee.sourceFileIDs[parentID] {
let parentSourceFileID = queuedDiagnostics.pointee.sourceFileIDs[parentID]
{
parent = (parentSourceFileID.pointee, AbsolutePosition(utf8Offset: positionInParent))
} else {
parent = nil
Expand Down Expand Up @@ -387,17 +392,20 @@ public func addQueuedDiagnostic(
// Map the highlights.
var highlights: [Syntax] = []
let highlightRanges = UnsafeBufferPointer<BridgedSourceLoc>(
start: highlightRangesPtr, count: numHighlightRanges * 2
start: highlightRangesPtr,
count: numHighlightRanges * 2
)
for index in 0..<numHighlightRanges {
// Make sure both the start and the end land within this source file.
guard let start = highlightRanges[index * 2].raw,
let end = highlightRanges[index * 2 + 1].raw else {
let end = highlightRanges[index * 2 + 1].raw
else {
continue
}

guard start >= sourceFileBaseAddress && start < sourceFileEndAddress,
end >= sourceFileBaseAddress && end <= sourceFileEndAddress else {
end >= sourceFileBaseAddress && end <= sourceFileEndAddress
else {
continue
}

Expand All @@ -414,8 +422,9 @@ public func addQueuedDiagnostic(
while true {
// If this syntax matches our starting/ending positions, add the
// highlight and we're done.
if highlightSyntax.positionAfterSkippingLeadingTrivia == startPos &&
highlightSyntax.endPositionBeforeTrailingTrivia == endPos {
if highlightSyntax.positionAfterSkippingLeadingTrivia == startPos
&& highlightSyntax.endPositionBeforeTrailingTrivia == endPos
{
highlights.append(highlightSyntax)
break
}
Expand Down
27 changes: 20 additions & 7 deletions lib/ASTGen/Sources/ASTGen/Exprs.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CASTBridging
import SwiftSyntax
import SwiftDiagnostics
import SwiftSyntax

extension ASTGenVisitor {
public func generate(_ node: ClosureExprSyntax) -> BridgedClosureExpr {
Expand All @@ -18,10 +18,14 @@ extension ASTGenVisitor {
public func generate(_ node: FunctionCallExprSyntax) -> BridgedCallExpr {
if !node.arguments.isEmpty || node.trailingClosure == nil {
if node.leftParen == nil {
self.diagnose(Diagnostic(node: node, message: MissingChildTokenError(parent: node, kindOfTokenMissing: .leftParen)))
self.diagnose(
Diagnostic(node: node, message: MissingChildTokenError(parent: node, kindOfTokenMissing: .leftParen))
)
}
if node.rightParen == nil {
self.diagnose(Diagnostic(node: node, message: MissingChildTokenError(parent: node, kindOfTokenMissing: .rightParen)))
self.diagnose(
Diagnostic(node: node, message: MissingChildTokenError(parent: node, kindOfTokenMissing: .rightParen))
)
}
}

Expand All @@ -30,14 +34,20 @@ extension ASTGenVisitor {
// Transform the trailing closure into an argument.
if let trailingClosure = node.trailingClosure {
let tupleElement = LabeledExprSyntax(
label: nil, colon: nil, expression: ExprSyntax(trailingClosure), trailingComma: nil)
label: nil,
colon: nil,
expression: ExprSyntax(trailingClosure),
trailingComma: nil
)

node.arguments.append(tupleElement)
node.trailingClosure = nil
}

let argumentTuple = self.generate(
node.arguments, leftParen: node.leftParen, rightParen: node.rightParen
node.arguments,
leftParen: node.leftParen,
rightParen: node.rightParen
)
let callee = generate(node.calledExpression)

Expand Down Expand Up @@ -69,7 +79,10 @@ extension ASTGenVisitor {

// Wrap in a SingleValueStmtExpr to embed as an expression.
return .createWithWrappedBranches(
ctx, stmt: stmt, declContext: declContext, mustBeExpr: true
ctx,
stmt: stmt,
declContext: declContext,
mustBeExpr: true
)
}

Expand All @@ -94,7 +107,7 @@ extension ASTGenVisitor {

return $0.bridgedSourceLoc(in: self)
}

return BridgedTupleExpr.createParsed(
self.ctx,
leftParenLoc: leftParen.bridgedSourceLoc(in: self),
Expand Down
Loading