llvm-project/clang/test/Sema/offsetof-64.c
Richard Smith 552c6c2328 PR44406: Follow behavior of array bound constant folding in more recent versions of GCC.
Old GCC used to aggressively fold VLAs to constant-bound arrays at block
scope in GNU mode. That's non-conforming, and more modern versions of
GCC only do this at file scope. Update Clang to do the same.

Also promote the warning for this from off-by-default to on-by-default
in all cases; more recent versions of GCC likewise warn on this by
default.

This is still slightly more permissive than GCC, as pointed out in
PR44406, as we still fold VLAs to constant arrays in structs, but that
seems justifiable given that we don't support VLA-in-struct (and don't
intend to ever support it), but GCC does.

Differential Revision: https://reviews.llvm.org/D89523
2020-10-16 14:34:35 -07:00

23 lines
747 B
C

// RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-linux-gnu
// PR15216
// Don't crash when taking computing the offset of structs with large arrays.
const unsigned long Size = (1l << 60);
struct Chunk1 {
char padding[Size]; // expected-warning {{folded to constant}}
char more_padding[1][Size]; // expected-warning {{folded to constant}}
char data;
};
int test1 = __builtin_offsetof(struct Chunk1, data);
struct Chunk2 {
char padding[Size][Size][Size]; // expected-error {{array is too large}}
char data;
};
// FIXME: Remove this error when the constant evaluator learns to
// ignore bad types.
int test2 = __builtin_offsetof(struct Chunk2, data); // expected-error{{initializer element is not a compile-time constant}}