<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/113503>113503</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
-Waddress-of-packed-member misses some formations of const&s
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
gburgessiv
</td>
</tr>
</table>
<pre>
The following code sample produces no `-Waddress-of-packed-member` diagnostics on Clang 19:
```
struct Packed {
int foo;
} __attribute__((packed));
const Packed *get_packed_struct();
void use_const_int_ref(const int &);
void foo() {
use_const_int_ref(get_packed_struct()->foo);
const Packed *p = get_packed_struct();
use_const_int_ref(p->foo);
}
```
Clang warns about this for pointers: https://godbolt.org/z/nMdEGcc4s which have the same alignment requirements as references.
A reduced version of the 'real-world' code motivating this bug looked something like:
```
struct Packed {
int foo;
} __attribute__((packed));
const Packed *get_packed_struct();
std::optional<int> foo() {
const Packed *p = get_packed_struct();
// std::optional's ctor takes this as a `const int&`, which requires alignof(int).
// If this function didn't have an implicit conversion to optional, this would work
// fine, since Clang emits IR assuming 1-byte alignment for this load.
return p->foo;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8lUGP6jYQxz-NuYxAwQZCDjmwy1K9Q6WqqtQjcuJJ4q7jST0O6PXTV054y-s-VpX28KTIhMT-z388v3E0s209Yim2T2J7XOgxdhTKthpDi8z2sqjIfC3_6BAaco6u1rdQk0Fg3Q8OYQhkxhoZPIHYZcs_tTEBmZfULAddv6JZ9thXGMQuA2N164mjrRnIw7PTvoV1IdRBZEeRfRt32e2a_nIMYx3ht0kMRP40PwYAsD5CQyTU7ZnIj3A-6xiDrcaI57OQeyH3sw8hi3S9zZ3GmjzfteWhxXiep5_nuJPCfdWFrIGR8TwtPFsfzwEbIfezUDIk5O7HONO6ZHWS-28Wj-Q-8LEU6mVSuesnhfdZDCDUEf4vl4eBhwch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