mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-25 19:16:05 +00:00

https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm made very clear that it is an UB having type definitions with in offsetof. Clang supports defining a type as the first argument as a conforming extension due to how many projects use the construct in C99 and earlier to calculate the alignment of a type. GCC also supports defining a type as the first argument. This adds extension warnings and documentation for the functionality Clang explicitly supports. Fixes #57065 Reverts the revert of 39da55e8f548a11f7dadefa73ea73d809a5f1729 Co-authored-by: Yingchi Long <i@lyc.dev> Co-authored-by: Aaron Ballman <aaron@aaronballman.com> Differential Revision: https://reviews.llvm.org/D133574
17 lines
447 B
C
17 lines
447 B
C
// RUN: %clang_cc1 %s -emit-llvm -o %t
|
|
|
|
// PR2910
|
|
struct sockaddr_un {
|
|
unsigned char sun_len;
|
|
char sun_path[104];
|
|
};
|
|
|
|
int test(int len) {
|
|
return __builtin_offsetof(struct sockaddr_un, sun_path[len+1]);
|
|
}
|
|
|
|
// Ensure we can form the offset to a structure defined in the first argument
|
|
// without crashing or asserting on an invalid declaration (because the
|
|
// declaration is actually valid).
|
|
void c() { __builtin_offsetof(struct {int b;}, b); }
|