xref: /llvm-project/clang/test/CodeGen/init-randomized-struct-fwd-decl.c (revision f85a9a6452e8f49f9768d66a86434a88a5891614)
1 // RUN: %clang_cc1 -triple=x86_64-unknown-linux -emit-llvm -frandomize-layout-seed=1234567890abcdef < %s | FileCheck %s
2 // PR60349
3 
4 // Clang will add a forward declaration of "struct bar" and "enum qux" to the
5 // structures. This shouldn't prevent these structures from being randomized.
6 // So the 'f' element shouldn't be at the start of the structure anymore.
7 
8 struct foo {
9   struct bar *(*f)(void);
10   struct bar *(*g)(void);
11   struct bar *(*h)(void);
12   struct bar *(*i)(void);
13   struct bar *(*j)(void);
14   struct bar *(*k)(void);
15 };
16 
17 // CHECK-LABEL: define {{.*}}@t1(
18 // CHECK-NOT: getelementptr inbounds %struct.foo, ptr %3, i32 0, i32 0
t1(struct foo * z)19 struct bar *t1(struct foo *z) {
20   return z->f();
21 }
22 
23 struct baz {
24   enum qux *(*f)(void);
25   enum qux *(*g)(void);
26   enum qux *(*h)(void);
27   enum qux *(*i)(void);
28   enum qux *(*j)(void);
29   enum qux *(*k)(void);
30 };
31 
32 // CHECK-LABEL: define {{.*}}@t2(
33 // CHECK-NOT: getelementptr inbounds %struct.baz, ptr %3, i32 0, i32 0
t2(struct baz * z)34 enum qux *t2(struct baz *z) {
35   return z->f();
36 }
37