[clang] [Clang] Fix missing diagnostic for non-standard layout type in `offsetof` (PR #65246)

Louis Dionne via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 21 15:09:12 PDT 2023


ldionne wrote:

Ok so after scratching my head a bit, I'm actually not certain of how to proceed with this. I can't find a way to implement `__libcpp_datasizeof` in libc++ that doesn't involve `offsetof`, so our only option would be to silence the warning. We could do it like (if we end up doing that, just take that diff and incorporate it to your patch, we can land both at once and the libc++ CI will also trigger):

```
diff --git a/libcxx/include/__type_traits/datasizeof.h b/libcxx/include/__type_traits/datasizeof.h
index 019099a9cf18..88a702470c3c 100644
--- a/libcxx/include/__type_traits/datasizeof.h
+++ b/libcxx/include/__type_traits/datasizeof.h
@@ -47,7 +47,11 @@ struct __libcpp_datasizeof {
   };
 #endif
 
+  _LIBCPP_DIAGNOSTIC_PUSH
+  _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
+  _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
   static const size_t value = offsetof(_FirstPaddingByte<>, __first_padding_byte_);
+  _LIBCPP_DIAGNOSTIC_POP
 };
 
 _LIBCPP_END_NAMESPACE_STD
```

However, in C++03, using `offsetof` with a non-POD is undefined behavior (hence the warning, I guess). So we'd technically have UB in our code here, which isn't great. In C++17 doing that is conditionally supported, so I guess we're fine.

So... should we strive to eradicate this from libc++ in the first place, or should we just silence the warning. WDYT?

Also, given that it is conditionally supported in C++17, I assume this warning doesn't trigger at all in C++17 mode, right?

https://github.com/llvm/llvm-project/pull/65246


More information about the cfe-commits mailing list