Skip to content

[clang] Show warnings for Objective-C pointers/blocks with an assign attribute #136305

Closed
@d0iasm

Description

@d0iasm

We would like to propose showing a warning message when a developer uses an assign attribute to an Objective-C pointer/block when ARC is enabled with -fobjc-arc as clang treats the assign property as __unsafe_unretained which a developer may not expect.

Even though the fact that assign corresponds to __unsafe_unretained is clearly documented in property declarations, this can be surprising in a code base written in Objective-C++ (as Chromium) as this is the only qualifier that is available for pointer to non reference counted object, and thus their use for reference counted may be accidental, especially if the code is modified by a developer less familiar with Objective-C.

Finally, this warning would not prevent declaring properties that keep a non-zeroing weak pointer since it is possible to use unsafe_unretained to declare properties that use __unsafe_unretained which is clearer than the overloaded assign attribute.

Consider the following code:

@interface Bar : NSObject
  @property(nonatomic, assign) NSObject* foo1;
  @property(nonatomic, strong) NSObject* foo2;
@end

@implementation Bar
@end

It generates the following assembly with the command $ clang -Wall -Wextra -fobjc-arc -o foo.s -S foo.m:

"-[Bar setFoo1:]":                      ## @"\01-[Bar setFoo1:]"
    .cfi_startproc
## %bb.0:
    pushq    %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register %rbp
    movq    %rdi, -8(%rbp)
    movq    %rsi, -16(%rbp)
    movq    %rdx, -24(%rbp)
    movq    -24(%rbp), %rcx
    movq    -8(%rbp), %rax
    movq    %rcx, 8(%rax)
    popq    %rbp
    retq
    .cfi_endproc

"-[Bar setFoo2:]":                      ## @"\01-[Bar setFoo2:]"
    .cfi_startproc
## %bb.0:
    pushq    %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register %rbp
    subq    $32, %rsp
    movq    %rdi, -8(%rbp)
    movq    %rsi, -16(%rbp)
    movq    %rdx, -24(%rbp)
    movq    -24(%rbp), %rsi
    movq    -8(%rbp), %rdi
    addq    $16, %rdi
    callq    _objc_storeStrong
    addq    $32, %rsp
    popq    %rbp
    retq
    .cfi_endproc

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerobjective-cquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions