Skip to content

Commit 89a9faa

Browse files
youknowoneemilio
authored andcommitted
newlines for constexpr
1 parent 54a1098 commit 89a9faa

10 files changed

+166
-45
lines changed

src/bindgen/language_backend/clike.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -902,20 +902,29 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
902902
write!(out, "{}", export_name);
903903
}
904904

905-
write!(out, "{{ ");
905+
write!(out, "{{");
906+
if is_constexpr {
907+
out.push_tab();
908+
} else {
909+
write!(out, " ");
910+
}
906911
// In C++, same order as defined is required.
907912
let ordered_fields = out.bindings().struct_field_names(path);
908913
for (i, ordered_key) in ordered_fields.iter().enumerate() {
909914
if let Some(lit) = fields.get(ordered_key) {
910915
if is_constexpr {
911-
if i > 0 {
912-
write!(out, ", ");
913-
}
916+
out.new_line();
914917

915918
// TODO: Some C++ versions (c++20?) now support designated
916919
// initializers, consider generating them.
917920
write!(out, "/* .{} = */ ", ordered_key);
918921
self.write_literal(out, lit);
922+
if i + 1 != ordered_fields.len() {
923+
write!(out, ",");
924+
if !is_constexpr {
925+
write!(out, " ");
926+
}
927+
}
919928
} else {
920929
if i > 0 {
921930
write!(out, ", ");
@@ -932,7 +941,13 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
932941
}
933942
}
934943
}
935-
write!(out, " }}");
944+
if is_constexpr {
945+
out.pop_tab();
946+
out.new_line();
947+
} else {
948+
write!(out, " ");
949+
}
950+
write!(out, "}}");
936951
}
937952
}
938953
}

tests/expectations/associated_in_body.cpp

+32-10
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,44 @@ struct StyleAlignFlags {
4747
static const StyleAlignFlags MIXED_SELF;
4848
};
4949
/// 'auto'
50-
constexpr inline const StyleAlignFlags StyleAlignFlags::AUTO = StyleAlignFlags{ /* .bits = */ (uint8_t)0 };
50+
constexpr inline const StyleAlignFlags StyleAlignFlags::AUTO = StyleAlignFlags{
51+
/* .bits = */ (uint8_t)0
52+
};
5153
/// 'normal'
52-
constexpr inline const StyleAlignFlags StyleAlignFlags::NORMAL = StyleAlignFlags{ /* .bits = */ (uint8_t)1 };
54+
constexpr inline const StyleAlignFlags StyleAlignFlags::NORMAL = StyleAlignFlags{
55+
/* .bits = */ (uint8_t)1
56+
};
5357
/// 'start'
54-
constexpr inline const StyleAlignFlags StyleAlignFlags::START = StyleAlignFlags{ /* .bits = */ (uint8_t)(1 << 1) };
58+
constexpr inline const StyleAlignFlags StyleAlignFlags::START = StyleAlignFlags{
59+
/* .bits = */ (uint8_t)(1 << 1)
60+
};
5561
/// 'end'
56-
constexpr inline const StyleAlignFlags StyleAlignFlags::END = StyleAlignFlags{ /* .bits = */ (uint8_t)(1 << 2) };
57-
constexpr inline const StyleAlignFlags StyleAlignFlags::ALIAS = StyleAlignFlags{ /* .bits = */ (uint8_t)(StyleAlignFlags::END).bits };
62+
constexpr inline const StyleAlignFlags StyleAlignFlags::END = StyleAlignFlags{
63+
/* .bits = */ (uint8_t)(1 << 2)
64+
};
65+
constexpr inline const StyleAlignFlags StyleAlignFlags::ALIAS = StyleAlignFlags{
66+
/* .bits = */ (uint8_t)(StyleAlignFlags::END).bits
67+
};
5868
/// 'flex-start'
59-
constexpr inline const StyleAlignFlags StyleAlignFlags::FLEX_START = StyleAlignFlags{ /* .bits = */ (uint8_t)(1 << 3) };
60-
constexpr inline const StyleAlignFlags StyleAlignFlags::MIXED = StyleAlignFlags{ /* .bits = */ (uint8_t)(((1 << 4) | (StyleAlignFlags::FLEX_START).bits) | (StyleAlignFlags::END).bits) };
61-
constexpr inline const StyleAlignFlags StyleAlignFlags::MIXED_SELF = StyleAlignFlags{ /* .bits = */ (uint8_t)(((1 << 5) | (StyleAlignFlags::FLEX_START).bits) | (StyleAlignFlags::END).bits) };
69+
constexpr inline const StyleAlignFlags StyleAlignFlags::FLEX_START = StyleAlignFlags{
70+
/* .bits = */ (uint8_t)(1 << 3)
71+
};
72+
constexpr inline const StyleAlignFlags StyleAlignFlags::MIXED = StyleAlignFlags{
73+
/* .bits = */ (uint8_t)(((1 << 4) | (StyleAlignFlags::FLEX_START).bits) | (StyleAlignFlags::END).bits)
74+
};
75+
constexpr inline const StyleAlignFlags StyleAlignFlags::MIXED_SELF = StyleAlignFlags{
76+
/* .bits = */ (uint8_t)(((1 << 5) | (StyleAlignFlags::FLEX_START).bits) | (StyleAlignFlags::END).bits)
77+
};
6278

6379
/// An arbitrary identifier for a native (OS compositor) surface
6480
struct StyleNativeSurfaceId {
6581
uint64_t _0;
6682
static const StyleNativeSurfaceId DEBUG_OVERLAY;
6783
};
6884
/// A special id for the native surface that is used for debug / profiler overlays.
69-
constexpr inline const StyleNativeSurfaceId StyleNativeSurfaceId::DEBUG_OVERLAY = StyleNativeSurfaceId{ /* ._0 = */ UINT64_MAX };
85+
constexpr inline const StyleNativeSurfaceId StyleNativeSurfaceId::DEBUG_OVERLAY = StyleNativeSurfaceId{
86+
/* ._0 = */ UINT64_MAX
87+
};
7088

7189
struct StyleNativeTileId {
7290
StyleNativeSurfaceId surface_id;
@@ -75,7 +93,11 @@ struct StyleNativeTileId {
7593
static const StyleNativeTileId DEBUG_OVERLAY;
7694
};
7795
/// A special id for the native surface that is used for debug / profiler overlays.
78-
constexpr inline const StyleNativeTileId StyleNativeTileId::DEBUG_OVERLAY = StyleNativeTileId{ /* .surface_id = */ StyleNativeSurfaceId::DEBUG_OVERLAY, /* .x = */ 0, /* .y = */ 0 };
96+
constexpr inline const StyleNativeTileId StyleNativeTileId::DEBUG_OVERLAY = StyleNativeTileId{
97+
/* .surface_id = */ StyleNativeSurfaceId::DEBUG_OVERLAY,
98+
/* .x = */ 0,
99+
/* .y = */ 0
100+
};
79101

80102
extern "C" {
81103

tests/expectations/bitflags.cpp

+42-14
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,34 @@ struct AlignFlags {
3939
}
4040
};
4141
/// 'auto'
42-
constexpr static const AlignFlags AlignFlags_AUTO = AlignFlags{ /* .bits = */ (uint8_t)0 };
42+
constexpr static const AlignFlags AlignFlags_AUTO = AlignFlags{
43+
/* .bits = */ (uint8_t)0
44+
};
4345
/// 'normal'
44-
constexpr static const AlignFlags AlignFlags_NORMAL = AlignFlags{ /* .bits = */ (uint8_t)1 };
46+
constexpr static const AlignFlags AlignFlags_NORMAL = AlignFlags{
47+
/* .bits = */ (uint8_t)1
48+
};
4549
/// 'start'
46-
constexpr static const AlignFlags AlignFlags_START = AlignFlags{ /* .bits = */ (uint8_t)(1 << 1) };
50+
constexpr static const AlignFlags AlignFlags_START = AlignFlags{
51+
/* .bits = */ (uint8_t)(1 << 1)
52+
};
4753
/// 'end'
48-
constexpr static const AlignFlags AlignFlags_END = AlignFlags{ /* .bits = */ (uint8_t)(1 << 2) };
49-
constexpr static const AlignFlags AlignFlags_ALIAS = AlignFlags{ /* .bits = */ (uint8_t)(AlignFlags_END).bits };
54+
constexpr static const AlignFlags AlignFlags_END = AlignFlags{
55+
/* .bits = */ (uint8_t)(1 << 2)
56+
};
57+
constexpr static const AlignFlags AlignFlags_ALIAS = AlignFlags{
58+
/* .bits = */ (uint8_t)(AlignFlags_END).bits
59+
};
5060
/// 'flex-start'
51-
constexpr static const AlignFlags AlignFlags_FLEX_START = AlignFlags{ /* .bits = */ (uint8_t)(1 << 3) };
52-
constexpr static const AlignFlags AlignFlags_MIXED = AlignFlags{ /* .bits = */ (uint8_t)(((1 << 4) | (AlignFlags_FLEX_START).bits) | (AlignFlags_END).bits) };
53-
constexpr static const AlignFlags AlignFlags_MIXED_SELF = AlignFlags{ /* .bits = */ (uint8_t)(((1 << 5) | (AlignFlags_FLEX_START).bits) | (AlignFlags_END).bits) };
61+
constexpr static const AlignFlags AlignFlags_FLEX_START = AlignFlags{
62+
/* .bits = */ (uint8_t)(1 << 3)
63+
};
64+
constexpr static const AlignFlags AlignFlags_MIXED = AlignFlags{
65+
/* .bits = */ (uint8_t)(((1 << 4) | (AlignFlags_FLEX_START).bits) | (AlignFlags_END).bits)
66+
};
67+
constexpr static const AlignFlags AlignFlags_MIXED_SELF = AlignFlags{
68+
/* .bits = */ (uint8_t)(((1 << 5) | (AlignFlags_FLEX_START).bits) | (AlignFlags_END).bits)
69+
};
5470

5571
struct DebugFlags {
5672
uint32_t bits;
@@ -84,7 +100,9 @@ struct DebugFlags {
84100
}
85101
};
86102
/// Flag with the topmost bit set of the u32
87-
constexpr static const DebugFlags DebugFlags_BIGGEST_ALLOWED = DebugFlags{ /* .bits = */ (uint32_t)(1 << 31) };
103+
constexpr static const DebugFlags DebugFlags_BIGGEST_ALLOWED = DebugFlags{
104+
/* .bits = */ (uint32_t)(1 << 31)
105+
};
88106

89107
struct LargeFlags {
90108
uint64_t bits;
@@ -118,8 +136,12 @@ struct LargeFlags {
118136
}
119137
};
120138
/// Flag with a very large shift that usually would be narrowed.
121-
constexpr static const LargeFlags LargeFlags_LARGE_SHIFT = LargeFlags{ /* .bits = */ (uint64_t)(1ull << 44) };
122-
constexpr static const LargeFlags LargeFlags_INVERTED = LargeFlags{ /* .bits = */ (uint64_t)~(LargeFlags_LARGE_SHIFT).bits };
139+
constexpr static const LargeFlags LargeFlags_LARGE_SHIFT = LargeFlags{
140+
/* .bits = */ (uint64_t)(1ull << 44)
141+
};
142+
constexpr static const LargeFlags LargeFlags_INVERTED = LargeFlags{
143+
/* .bits = */ (uint64_t)~(LargeFlags_LARGE_SHIFT).bits
144+
};
123145

124146
struct OutOfLine {
125147
uint32_t _0;
@@ -152,9 +174,15 @@ struct OutOfLine {
152174
return *this;
153175
}
154176
};
155-
constexpr static const OutOfLine OutOfLine_A = OutOfLine{ /* ._0 = */ (uint32_t)1 };
156-
constexpr static const OutOfLine OutOfLine_B = OutOfLine{ /* ._0 = */ (uint32_t)2 };
157-
constexpr static const OutOfLine OutOfLine_AB = OutOfLine{ /* ._0 = */ (uint32_t)((OutOfLine_A)._0 | (OutOfLine_B)._0) };
177+
constexpr static const OutOfLine OutOfLine_A = OutOfLine{
178+
/* ._0 = */ (uint32_t)1
179+
};
180+
constexpr static const OutOfLine OutOfLine_B = OutOfLine{
181+
/* ._0 = */ (uint32_t)2
182+
};
183+
constexpr static const OutOfLine OutOfLine_AB = OutOfLine{
184+
/* ._0 = */ (uint32_t)((OutOfLine_A)._0 | (OutOfLine_B)._0)
185+
};
158186

159187
extern "C" {
160188

tests/expectations/const_generics_constant.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ using FontWeightFixedPoint = FixedPoint<FONT_WEIGHT_FRACTION_BITS>;
1616
struct FontWeight {
1717
FontWeightFixedPoint _0;
1818
};
19-
constexpr static const FontWeight FontWeight_NORMAL = FontWeight{ /* ._0 = */ FontWeightFixedPoint{ /* .value = */ (400 << FONT_WEIGHT_FRACTION_BITS) } };
19+
constexpr static const FontWeight FontWeight_NORMAL = FontWeight{
20+
/* ._0 = */ FontWeightFixedPoint{
21+
/* .value = */ (400 << FONT_WEIGHT_FRACTION_BITS)
22+
}
23+
};
2024

2125
extern "C" {
2226

tests/expectations/constant_constexpr.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ struct Foo {
1818
};
1919
inline const int64_t Foo::CONSTANT_I64_BODY = 216;
2020

21-
static const Foo SomeFoo = Foo{ /* .x = */ 99 };
21+
static const Foo SomeFoo = Foo{
22+
/* .x = */ 99
23+
};

tests/expectations/constant_user_defined_type.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ struct S {
1414

1515
using A = uint8_t;
1616

17-
constexpr static const S C1 = S{ /* .field = */ 0 };
17+
constexpr static const S C1 = S{
18+
/* .field = */ 0
19+
};
1820

1921
constexpr static const E C2 = V;
2022

tests/expectations/prefixed_struct_literal.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ struct PREFIXFoo {
88
int32_t a;
99
uint32_t b;
1010
};
11-
constexpr static const PREFIXFoo PREFIXFoo_FOO = PREFIXFoo{ /* .a = */ 42, /* .b = */ 47 };
11+
constexpr static const PREFIXFoo PREFIXFoo_FOO = PREFIXFoo{
12+
/* .a = */ 42,
13+
/* .b = */ 47
14+
};
1215

13-
constexpr static const PREFIXFoo PREFIXBAR = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337 };
16+
constexpr static const PREFIXFoo PREFIXBAR = PREFIXFoo{
17+
/* .a = */ 42,
18+
/* .b = */ 1337
19+
};
1420

1521
extern "C" {
1622

tests/expectations/prefixed_struct_literal_deep.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ struct PREFIXFoo {
1414
PREFIXBar bar;
1515
};
1616

17-
constexpr static const PREFIXFoo PREFIXVAL = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337, /* .bar = */ PREFIXBar{ /* .a = */ 323 } };
17+
constexpr static const PREFIXFoo PREFIXVAL = PREFIXFoo{
18+
/* .a = */ 42,
19+
/* .b = */ 1337,
20+
/* .bar = */ PREFIXBar{
21+
/* .a = */ 323
22+
}
23+
};
1824

1925
extern "C" {
2026

tests/expectations/struct_literal.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ struct Foo {
1010
int32_t a;
1111
uint32_t b;
1212
};
13-
constexpr static const Foo Foo_FOO = Foo{ /* .a = */ 42, /* .b = */ 47 };
14-
constexpr static const Foo Foo_FOO2 = Foo{ /* .a = */ 42, /* .b = */ 47 };
15-
constexpr static const Foo Foo_FOO3 = Foo{ /* .a = */ 42, /* .b = */ 47 };
13+
constexpr static const Foo Foo_FOO = Foo{
14+
/* .a = */ 42,
15+
/* .b = */ 47
16+
};
17+
constexpr static const Foo Foo_FOO2 = Foo{
18+
/* .a = */ 42,
19+
/* .b = */ 47
20+
};
21+
constexpr static const Foo Foo_FOO3 = Foo{
22+
/* .a = */ 42,
23+
/* .b = */ 47
24+
};
1625

1726

18-
constexpr static const Foo BAR = Foo{ /* .a = */ 42, /* .b = */ 1337 };
27+
constexpr static const Foo BAR = Foo{
28+
/* .a = */ 42,
29+
/* .b = */ 1337
30+
};
1931

2032

2133

tests/expectations/struct_literal_order.cpp

+30-6
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,42 @@ struct ABC {
99
uint32_t b;
1010
uint32_t c;
1111
};
12-
constexpr static const ABC ABC_abc = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 };
13-
constexpr static const ABC ABC_bac = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 };
14-
constexpr static const ABC ABC_cba = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 };
12+
constexpr static const ABC ABC_abc = ABC{
13+
/* .a = */ 1.0,
14+
/* .b = */ 2,
15+
/* .c = */ 3
16+
};
17+
constexpr static const ABC ABC_bac = ABC{
18+
/* .a = */ 1.0,
19+
/* .b = */ 2,
20+
/* .c = */ 3
21+
};
22+
constexpr static const ABC ABC_cba = ABC{
23+
/* .a = */ 1.0,
24+
/* .b = */ 2,
25+
/* .c = */ 3
26+
};
1527

1628
struct BAC {
1729
uint32_t b;
1830
float a;
1931
int32_t c;
2032
};
21-
constexpr static const BAC BAC_abc = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 };
22-
constexpr static const BAC BAC_bac = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 };
23-
constexpr static const BAC BAC_cba = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 };
33+
constexpr static const BAC BAC_abc = BAC{
34+
/* .b = */ 1,
35+
/* .a = */ 2.0,
36+
/* .c = */ 3
37+
};
38+
constexpr static const BAC BAC_bac = BAC{
39+
/* .b = */ 1,
40+
/* .a = */ 2.0,
41+
/* .c = */ 3
42+
};
43+
constexpr static const BAC BAC_cba = BAC{
44+
/* .b = */ 1,
45+
/* .a = */ 2.0,
46+
/* .c = */ 3
47+
};
2448

2549
extern "C" {
2650

0 commit comments

Comments
 (0)