-
Notifications
You must be signed in to change notification settings - Fork 441
Improve diagnostic in testMissingArgumentToAttribute
#738
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
add_library(SwiftBasicFormat STATIC | ||
Trivia+Extension.swift | ||
generated/Format.swift | ||
) | ||
|
||
target_link_libraries(SwiftBasicFormat PUBLIC | ||
SwiftSyntax) | ||
|
||
set_property(GLOBAL APPEND PROPERTY SWIFTSYNTAX_EXPORTS SwiftBasicFormat) | ||
|
||
# NOTE: workaround for CMake not setting up include flags yet | ||
set_target_properties(SwiftBasicFormat PROPERTIES | ||
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) | ||
|
||
install(TARGETS SwiftBasicFormat | ||
EXPORT SwiftSyntaxTargets | ||
ARCHIVE DESTINATION lib | ||
LIBRARY DESTINATION lib | ||
RUNTIME DESTINATION bin) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//===--- PresenceUtils.swift ----------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2022 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@_spi(RawSyntax) import SwiftSyntax | ||
import SwiftBasicFormat | ||
|
||
/// Walks a tree and checks whether the tree contained any present tokens. | ||
class PresentNodeChecker: SyntaxAnyVisitor { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should really introduce a |
||
var hasPresentToken: Bool = false | ||
|
||
override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { | ||
if hasPresentToken { | ||
// If we already saw a present token, we don't need to continue. | ||
return .skipChildren | ||
} else { | ||
return .visitChildren | ||
} | ||
} | ||
|
||
override func visit(_ node: TokenSyntax) -> SyntaxVisitorContinueKind { | ||
if node.presence == .present { | ||
hasPresentToken = true | ||
} | ||
return .visitChildren | ||
} | ||
} | ||
|
||
extension SyntaxProtocol { | ||
/// Returns `true` if all tokens nodes in this tree are missing. | ||
var isMissingAllTokens: Bool { | ||
let checker = PresentNodeChecker(viewMode: .all) | ||
checker.walk(Syntax(self)) | ||
return !checker.hasPresentToken | ||
} | ||
} | ||
|
||
/// Transforms a syntax tree by making all missing tokens present. | ||
class PresentMaker: SyntaxRewriter { | ||
override func visit(_ token: TokenSyntax) -> Syntax { | ||
if token.presence == .missing { | ||
let presentToken: TokenSyntax | ||
let (rawKind, text) = token.tokenKind.decomposeToRaw() | ||
if let text = text, !text.isEmpty { | ||
presentToken = TokenSyntax(token.tokenKind, presence: .present) | ||
} else { | ||
let newKind = TokenKind.fromRaw(kind: rawKind, text: rawKind.defaultText.map(String.init) ?? "<#\(rawKind.nameForDiagnostics)#>") | ||
presentToken = TokenSyntax(newKind, presence: .present) | ||
} | ||
return Syntax(Format().format(syntax: presentToken)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment about |
||
} else { | ||
return Syntax(token) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,7 +149,8 @@ public enum RawTokenKind: Equatable, Hashable { | |
case ${token.swift_kind()} | ||
% end | ||
|
||
var defaultText: SyntaxText? { | ||
@_spi(RawSyntax) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to mention this in the other PR, but since this seems to extract this part... IMO we shouldn't be using |
||
public var defaultText: SyntaxText? { | ||
switch self { | ||
case .eof: return "" | ||
% for token in SYNTAX_TOKENS: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
argument(s)
IMO, same with the fix-it message.