1 // RUN: %clang_cc1 -triple=x86_64-unknown-linux -frandomize-layout-seed=1234567890abcded \
2 // RUN: -verify -fsyntax-only -Werror %s
4 // NOTE: The current seed (1234567890abcded) is specifically chosen because it
5 // uncovered a bug in diagnostics. With it the randomization of "t9" places the
6 // "a" element at the end of the record. When that happens, the clang complains
7 // about excessive initializers, which is confusing, because there aren't
8 // excessive initializers. It should instead complain about using a
9 // non-designated initializer on a raqndomized struct.
11 // Initializing a randomized structure requires a designated initializer,
12 // otherwise the element ordering will be off. The only exceptions to this rule
15 // - A structure with only one element, and
16 // - A structure initialized with "{0}".
18 // These are well-defined situations where the field ordering doesn't affect
21 typedef void (*func_ptr
)();
36 } __attribute__((randomize_layout
));
38 struct test t1
= {}; // This should be fine per WG14 N2900 (in C23) + our extension handling of it in earlier modes
39 struct test t2
= { 0 }; // This should also be fine per C99 6.7.8p19
40 struct test t3
= { .f
= baz
, .b
= bar
, .g
= gaz
, .a
= foo
}; // Okay
41 struct test t4
= { .a
= foo
, bar
, baz
}; // expected-error {{a randomized struct can only be initialized with a designated initializer}}
47 } __attribute__((randomize_layout
));
49 struct other_test t5
= { .a
= foo
, .b
[0] = foo
}; // Okay
50 struct other_test t6
= { .a
= foo
, .b
[0] = foo
, bar
, baz
}; // Okay
51 struct other_test t7
= { .a
= foo
, .b
= { foo
, bar
, baz
} }; // Okay
52 struct other_test t8
= { baz
, bar
, gaz
, foo
}; // expected-error {{a randomized struct can only be initialized with a designated initializer}}
53 struct other_test t9
= { .a
= foo
, .b
[0] = foo
, bar
, baz
, gaz
}; // expected-error {{a randomized struct can only be initialized with a designated initializer}}
56 } __attribute__((randomize_layout
));
58 struct empty_test t10
= {}; // Okay
62 } __attribute__((randomize_layout
));
64 struct degen_test t11
= { foo
}; // Okay
66 struct static_assert_test
{
68 _Static_assert(sizeof(int) == 4, "oh no!");
69 } __attribute__((randomize_layout
));
71 struct static_assert_test t12
= { 42 }; // Okay
73 struct enum_decl_test
{
74 enum e
{ BORK
= 42, FORK
= 9 } f
;
75 } __attribute__((randomize_layout
));
77 struct enum_decl_test t13
= { BORK
}; // Okay