<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/136305>136305</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] Show warnings for Objective-C pointers/blocks with an `assign` attribute
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
d0iasm
</td>
</tr>
</table>
<pre>
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](https://clang.llvm.org/docs/AutomaticReferenceCounting.html#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
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzcVkFv6jgQ_jXmMgIZJ1A4cKB0kfayK70e3hFN4gnxq2Pn2Q6U_fUrO0kLLX172Mtqq0pE8Xwz33zz2TF6r46GaMMWj2zxNMEu1NZtJFfom0lh5WXzneBsOy1BqxeCYKF1trWewNf2rMwREM7oTHxqyHs8EpxrMoAg6UTatuSg8-QBDbAl7yuyJQcMwamiCykpGviz-EFlUCea7qC1ygRyTOwLbcuXPuP22w6UBzJYaJJwVqGGaWWLH-UUXQnoodRojhAcYfAQarotGImTC5cYyZb8cOiMx4oOnXEUUBmSMepcq7K-Yd_gBYwNQK8tlWHG-Jbx7W8nMhBq2x3rVKnCMkCoMdzWLK1z5FtrpI9tfl1WRfaETl9A2rJryASSoAywxeMbcUmlRodBWePZ4omJVR1C61m2ZWLPxD71P9P61MysOzKxl7b0TOy3XbANBlV-o4ocmZJ2tjNBmeOsDo1mIhtLTG9KiDUTOwh1JIcGCgLfudYpH6et4oxLKwkK9ARnp0IgE19fTZKJRyYegYkVetjVzjaqa5hYxxmkvKoflDX6Aj871KpS5HohlQc8odJx3FBZN7oiCmmsATc2A2XshiTYVDhyRiMh1F1KrpIBU4bPkDjcggDLUkkyAXVEk2-pVKj1BVSV-KU-lYfGyshQQnG58Ygm76HCRmmFrrfmlQqDZ_bKxJxvmo77pt9f0WKtoxOZMAw6rg2DUeR7UV6IWsDY__QvcjbBCV_etPEqdqeSeq31XkXxgk0KsCW_671gh4L0qdwA--VeGa3bz8308zyR0xYlyS82_SAJ49udNV7JBI5D0ro_VqLi0dh92JIP_3zLcp5arbAkeEQHLN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