xref: /linux/tools/testing/selftests/bpf/progs/btf_dump_test_case_bitfields.c (revision 58f6259b7a08f8d47d4629609703d358b042f0fd)
1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
2 
3 /*
4  * BTF-to-C dumper tests for bitfield.
5  *
6  * Copyright (c) 2019 Facebook
7  */
8 #include <stdbool.h>
9 
10 /* ----- START-EXPECTED-OUTPUT ----- */
11 /*
12  *struct bitfields_only_mixed_types {
13  *	int a: 3;
14  *	long b: 2;
15  *	_Bool c: 1;
16  *	enum {
17  *		A = 0,
18  *		B = 1,
19  *	} d: 1;
20  *	short e: 5;
21  *	int: 20;
22  *	unsigned int f: 30;
23  *};
24  *
25  */
26 /* ------ END-EXPECTED-OUTPUT ------ */
27 
28 struct bitfields_only_mixed_types {
29 	int a: 3;
30 	long b: 2;
31 	bool c: 1; /* it's really a _Bool type */
32 	enum {
33 		A, /* A = 0, dumper is very explicit */
34 		B, /* B = 1, same */
35 	} d: 1;
36 	short e: 5;
37 	/* 20-bit padding here */
38 	unsigned f: 30; /* this gets aligned on 4-byte boundary */
39 };
40 
41 /* ----- START-EXPECTED-OUTPUT ----- */
42 /*
43  *struct bitfield_mixed_with_others {
44  *	char: 4;
45  *	int a: 4;
46  *	short b;
47  *	long c;
48  *	long d: 8;
49  *	int e;
50  *	int f;
51  *};
52  *
53  */
54 /* ------ END-EXPECTED-OUTPUT ------ */
55 struct bitfield_mixed_with_others {
56 	char: 4; /* char is enough as a backing field */
57 	int a: 4;
58 	/* 8-bit implicit padding */
59 	short b; /* combined with previous bitfield */
60 	/* 4 more bytes of implicit padding */
61 	long c;
62 	long d: 8;
63 	/* 24 bits implicit padding */
64 	int e; /* combined with previous bitfield */
65 	int f;
66 	/* 4 bytes of padding */
67 };
68 
69 /* ----- START-EXPECTED-OUTPUT ----- */
70 /*
71  *struct bitfield_flushed {
72  *	int a: 4;
73  *	long: 60;
74  *	long b: 16;
75  *};
76  *
77  */
78 /* ------ END-EXPECTED-OUTPUT ------ */
79 struct bitfield_flushed {
80 	int a: 4;
81 	long: 0; /* flush until next natural alignment boundary */
82 	long b: 16;
83 };
84 
85 int f(struct {
86 	struct bitfields_only_mixed_types _1;
87 	struct bitfield_mixed_with_others _2;
88 	struct bitfield_flushed _3;
89 } *_)
90 {
91 	return 0;
92 }
93