aboutsummaryrefslogtreecommitdiff
path: root/clang/test/Sema/offsetof.c
diff options
context:
space:
mode:
authorAaron Ballman <aaron@aaronballman.com>2023-01-18 08:49:45 -0500
committerAaron Ballman <aaron@aaronballman.com>2023-01-18 08:51:21 -0500
commite7300e75b51a7e7d4e81975b4be7a6c65f9a8286 (patch)
treede4e2e3f1ceaca35ae0e37c4417c66b97d9af7d6 /clang/test/Sema/offsetof.c
parent63ba34f3fb752e0a60a78c21ee694e1064a870ab (diff)
downloadllvm-e7300e75b51a7e7d4e81975b4be7a6c65f9a8286.zip
llvm-e7300e75b51a7e7d4e81975b4be7a6c65f9a8286.tar.gz
llvm-e7300e75b51a7e7d4e81975b4be7a6c65f9a8286.tar.bz2
Diagnose extensions in 'offsetof'
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
Diffstat (limited to 'clang/test/Sema/offsetof.c')
-rw-r--r--clang/test/Sema/offsetof.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/clang/test/Sema/offsetof.c b/clang/test/Sema/offsetof.c
index 3a5ddc4..8fd9ad6 100644
--- a/clang/test/Sema/offsetof.c
+++ b/clang/test/Sema/offsetof.c
@@ -5,35 +5,38 @@
typedef struct P { int i; float f; } PT;
struct external_sun3_core
{
- unsigned c_regs;
+ unsigned c_regs;
PT X[100];
-
+
};
+// Ensure the builtin works as a constant expression
+int i = offsetof(PT, f);
+
void swap(void)
{
int x;
x = offsetof(struct external_sun3_core, c_regs);
x = __builtin_offsetof(struct external_sun3_core, X[42].f);
-
+
x = __builtin_offsetof(struct external_sun3_core, X[42].f2); // expected-error {{no member named 'f2'}}
x = __builtin_offsetof(int, X[42].f2); // expected-error {{offsetof requires struct}}
-
+
int a[__builtin_offsetof(struct external_sun3_core, X) == 4 ? 1 : -1];
int b[__builtin_offsetof(struct external_sun3_core, X[42]) == 340 ? 1 : -1];
int c[__builtin_offsetof(struct external_sun3_core, X[42].f2) == 344 ? 1 : -1]; // expected-error {{no member named 'f2'}}
-}
+}
extern int f(void);
-struct s1 { int a; };
+struct s1 { int a; };
int v1 = offsetof (struct s1, a) == 0 ? 0 : f();
-struct s2 { int a; };
+struct s2 { int a; };
int v2 = (int)(&((struct s2 *) 0)->a) == 0 ? 0 : f();
-struct s3 { int a; };
+struct s3 { int a; };
int v3 = __builtin_offsetof(struct s3, a) == 0 ? 0 : f();
// PR3396