Clang warning: A attribute for getter must not have any parameters besides 'self:' [-Wswift-name-attribute] (warn_attr_swift_name_getter_parameters)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: A attribute for getter must not have any parameters besides 'self:' (since 12.0)
Type Warning
Category Semantic Issue (since 12.0)
Internal Id warn_attr_swift_name_getter_parameters (since 12.0)
Active by Default Yes
Flags -Wno-swift-name-attribute (13 elements)
Internal Message %0 attribute for getter must not have any parameters besides 'self:' (since 12.0)
Regular Expression (?:warning|error|fatal error)\: (.*?) attribute for getter must not have any parameters besides 'self\:' \[(?:\-Werror,)?\-Wswift\-name\-attribute[^\]]*\]
First Commit 2020-02-28 14f6bfcb52e7 [clang] Implement objc_non_runtime_protocol to remove protocol metadata

Description

The warning is issued by the Clang compiler when an attribute intended to specify the Swift name of a getter method in Objective-C code violates certain conditions. Specifically, for a method to be correctly exposed to Swift as a getter, its Swift name attribute must not include any parameters other than 'self:'. The warning indicates a discrepancy between the intended Swift name, as specified by the __attribute__((swift_name("..."))), and the allowable syntax for a getter in Swift, which expects no parameters aside from an implicit 'self'. This restriction ensures that the Objective-C method can be seamlessly accessed in Swift using property-like syntax, aligning with Swift's naming conventions and access patterns for properties. The Clang compiler checks for this condition to facilitate better interoperability between Objective-C and Swift, highlighting declarations that may not behave as expected when bridged into Swift.  
AI Generated

Example

In the following example, an Objective-C class named C is defined. This class includes a method f: with an integer parameter v. The method is attributed with __attribute__((swift_name("..."))) to customize its name when exposed to Swift. However, the provided Swift name includes a parameter, violating the requirement that getter methods in Swift should not have parameters other than an implicit 'self'. As a result, Clang emits a warning indicating the misuse of the swift_name attribute, as the specified Swift name does not adhere to the criteria for a getter method. Additionally, a warning about the class C being defined without a base class is generated, with an accompanying note suggesting the addition of a super class to resolve this issue.  
AI Generated


Flags -xobjective-c

[Try out in Compiler Explorer]

Source
@interface C
- (int)f:(int)v __attribute__((swift_name("getter:withParam(param:)")));
@end
@implementation C
- (int)f:(int)v { return v; }
@end
Compiler Output
<source>:2:43: warning: 'swift_name' attribute for getter must not have any parameters besides 'self:' [-Wswift-name-attribute]
<source>:1:12: warning: class 'C' defined without specifying a base class [-Wobjc-root-class]
<source>:1:13: note: add a super class to fix this problem


Clang Internals (17.0.6)

Git Commit Message

[clang] Implement objc_non_runtime_protocol to remove protocol metadata

Summary:
Motivated by the new objc_direct attribute, this change adds a new
attribute that remotes metadata from Protocols that the programmer knows
isn't going to be used at runtime. We simply have the frontend skip
generating any protocol metadata entries (e.g. OBJC_CLASS_NAME,
_OBJC_$_PROTOCOL_INSTANCE_METHDOS, _OBJC_PROTOCOL, etc) for a protocol
marked with `__attribute__((objc_non_runtime_protocol))`.

There are a few APIs used to retrieve a protocol at runtime.
`@protocol(SomeProtocol)` will now error out of the requested protocol
is marked with attribute. `objc_getProtocol` will return `NULL` which
is consistent with the behavior of a non-existing protocol.

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D75574

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDeclAttr.cpp (line 6719)

// For a function, this will validate a compound Swift name, e.g.
// <code>init(foo:bar:baz:)</code> or <code>controllerForName(_:)</code>, and
// the function will output the number of parameter names, and whether this is a
// single-arg initializer.
//
// For a type, enum constant, property, or variable declaration, this will
// validate either a simple identifier, or a qualified
// <code>context.identifier</code> name.
static bool validateSwiftFunctionName(Sema &S, const ParsedAttr &AL, SourceLocation Loc, StringRef Name, unsigned &SwiftParamCount, bool &IsSingleParamInit) {
  // ...
  // Check the number of parameters for a getter/setter.
  if (IsGetter || IsSetter) {
    // ...
    unsigned ParamDiag = IsGetter ? diag::warn_attr_swift_name_getter_parameters : diag::warn_attr_swift_name_setter_parameters;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/SemaObjC/attr-swift_name.m

  • clang/test/SemaObjC/attr-swift_name.m:104:78: warning: '__swift_name__' attribute for getter must not have any parameters besides 'self:' [-Wswift-name-attribute]
  • clang/test/SemaObjC/attr-swift_name.m:158:38: warning: '__swift_name__' attribute for getter must not have any parameters besides 'self:' [-Wswift-name-attribute]