|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftSyntax |
| 14 | + |
| 15 | +/// Protocol representing a declaration scope which can potentially carry warning |
| 16 | +/// diagnostic group behavior controls (`@warn` attributes). |
| 17 | +protocol WarningControlDeclSyntax { |
| 18 | + var attributes: AttributeListSyntax { get } |
| 19 | + var position: AbsolutePosition { get } |
| 20 | + var endPosition: AbsolutePosition { get } |
| 21 | +} |
| 22 | + |
| 23 | +// Compute a dictionary of all `@warn` diagnostic group behaviors |
| 24 | +// specified on this warning control declaration scope. |
| 25 | +extension WarningControlDeclSyntax { |
| 26 | + func allWarningGroupControls() -> [String: WarningGroupBehavior] { |
| 27 | + return attributes.reduce(into: [String: WarningGroupBehavior]()) { result, attr in |
| 28 | + if case .attribute(let attributeSyntax) = attr, |
| 29 | + attributeSyntax.attributeName.as(IdentifierTypeSyntax.self)?.name.text == "warn" |
| 30 | + { |
| 31 | + if let diagnosticGroupID = attributeSyntax.arguments?.as(LabeledExprListSyntax.self)?.first?.expression.as( |
| 32 | + DeclReferenceExprSyntax.self |
| 33 | + )?.baseName.text { |
| 34 | + if let behaviorText = attr.as(AttributeSyntax.self)? |
| 35 | + .arguments?.as(LabeledExprListSyntax.self)? |
| 36 | + .dropFirst().first? |
| 37 | + .expression.as(DeclReferenceExprSyntax.self)? |
| 38 | + .baseName.text |
| 39 | + { |
| 40 | + result[diagnosticGroupID] = WarningGroupBehavior(rawValue: behaviorText) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// All syntax nodes which carry attributes and may represent a warning control scope |
| 49 | + |
| 50 | +// @warn(Deprecate, as: ignored) |
| 51 | +// func foo() {...} |
| 52 | +extension FunctionDeclSyntax: WarningControlDeclSyntax {} |
| 53 | + |
| 54 | +// @warn(Deprecate, as: ignored) |
| 55 | +// class Foo {...} |
| 56 | +extension ClassDeclSyntax: WarningControlDeclSyntax {} |
| 57 | + |
| 58 | +// @warn(Deprecate, as: ignored) |
| 59 | +// actor Foo {...} |
| 60 | +extension ActorDeclSyntax: WarningControlDeclSyntax {} |
| 61 | + |
| 62 | +// struct Foo { |
| 63 | +// @warn(Deprecate, as: ignored) |
| 64 | +// deinit() {...} |
| 65 | +extension DeinitializerDeclSyntax: WarningControlDeclSyntax {} |
| 66 | + |
| 67 | +// struct Foo { |
| 68 | +// @warn(Deprecate, as: ignored) |
| 69 | +// init() {...} |
| 70 | +extension InitializerDeclSyntax: WarningControlDeclSyntax {} |
| 71 | + |
| 72 | +// @warn(Deprecate, as: ignored) |
| 73 | +// enum Foo {...} |
| 74 | +extension EnumDeclSyntax: WarningControlDeclSyntax {} |
| 75 | + |
| 76 | +// @warn(Deprecate, as: ignored) |
| 77 | +// extension Foo {...} |
| 78 | +extension ExtensionDeclSyntax: WarningControlDeclSyntax {} |
| 79 | + |
| 80 | +// @warn(Deprecate, as: ignored) |
| 81 | +// import bar |
| 82 | +extension ImportDeclSyntax: WarningControlDeclSyntax {} |
| 83 | + |
| 84 | +// @warn(Deprecate, as: ignored) |
| 85 | +// protocol Foo {...} |
| 86 | +extension ProtocolDeclSyntax: WarningControlDeclSyntax {} |
| 87 | + |
| 88 | +// @warn(Deprecate, as: ignored) |
| 89 | +// struct Foo {...} |
| 90 | +extension StructDeclSyntax: WarningControlDeclSyntax {} |
| 91 | + |
| 92 | +// @warn(Deprecate, as: ignored) |
| 93 | +// subscript(index: Int) -> T {...} |
| 94 | +extension SubscriptDeclSyntax: WarningControlDeclSyntax {} |
| 95 | + |
| 96 | +// @warn(Deprecate, as: ignored) |
| 97 | +// var x: {...} |
| 98 | +extension VariableDeclSyntax: WarningControlDeclSyntax {} |
| 99 | + |
| 100 | +// struct Foo { |
| 101 | +// var property: Int { |
| 102 | +// @warn(Deprecate, as: ignored) |
| 103 | +// get {...} |
| 104 | +extension AccessorDeclSyntax: WarningControlDeclSyntax {} |
0 commit comments