Skip to content

Commit 305ccfe

Browse files
committed
constant: Make const.allow_constexpr default to true.
1 parent 15e1131 commit 305ccfe

29 files changed

+75
-75
lines changed

docs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ allow_static_const = true
913913

914914
# Whether a generated constant can be constexpr in C++ mode.
915915
#
916-
# default: false
916+
# default: true
917917
allow_constexpr = false
918918

919919
# This rule specifies the order in which constants will be sorted.

src/bindgen/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ impl Default for ConstantConfig {
706706
fn default() -> ConstantConfig {
707707
ConstantConfig {
708708
allow_static_const: true,
709-
allow_constexpr: false,
709+
allow_constexpr: true,
710710
sort_by: None,
711711
}
712712
}

tests/expectations/assoc_const_conflict.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
#include <ostream>
55
#include <new>
66

7-
static const uint32_t Foo_FOO = 42;
7+
constexpr static const uint32_t Foo_FOO = 42;

tests/expectations/assoc_constant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
struct Foo {
88

99
};
10-
static const int32_t Foo_GA = 10;
11-
static const float Foo_ZO = 3.14;
10+
constexpr static const int32_t Foo_GA = 10;
11+
constexpr static const float Foo_ZO = 3.14;
1212

1313
extern "C" {
1414

tests/expectations/cfg_2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ DEF NOT_DEFINED = 0
1111
#include <new>
1212

1313
#if defined(NOT_DEFINED)
14-
static const int32_t DEFAULT_X = 8;
14+
constexpr static const int32_t DEFAULT_X = 8;
1515
#endif
1616

1717
#if defined(DEFINED)
18-
static const int32_t DEFAULT_X = 42;
18+
constexpr static const int32_t DEFAULT_X = 42;
1919
#endif
2020

2121
#if (defined(NOT_DEFINED) || defined(DEFINED))

tests/expectations/const_conflict.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
#include <ostream>
55
#include <new>
66

7-
static const uint32_t Foo_FOO = 42;
7+
constexpr static const uint32_t Foo_FOO = 42;

tests/expectations/const_transparent.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
using Transparent = uint8_t;
88

9-
static const Transparent FOO = 0;
9+
constexpr static const Transparent FOO = 0;

tests/expectations/constant.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,44 @@
44
#include <ostream>
55
#include <new>
66

7-
static const int32_t FOO = 10;
7+
constexpr static const int32_t FOO = 10;
88

9-
static const uint32_t DELIMITER = ':';
9+
constexpr static const uint32_t DELIMITER = ':';
1010

11-
static const uint32_t LEFTCURLY = '{';
11+
constexpr static const uint32_t LEFTCURLY = '{';
1212

13-
static const uint32_t QUOTE = '\'';
13+
constexpr static const uint32_t QUOTE = '\'';
1414

15-
static const uint32_t TAB = '\t';
15+
constexpr static const uint32_t TAB = '\t';
1616

17-
static const uint32_t NEWLINE = '\n';
17+
constexpr static const uint32_t NEWLINE = '\n';
1818

19-
static const uint32_t HEART = U'\U00002764';
19+
constexpr static const uint32_t HEART = U'\U00002764';
2020

21-
static const uint32_t EQUID = U'\U00010083';
21+
constexpr static const uint32_t EQUID = U'\U00010083';
2222

23-
static const float ZOM = 3.14;
23+
constexpr static const float ZOM = 3.14;
2424

2525
/// A single-line doc comment.
26-
static const int8_t POS_ONE = 1;
26+
constexpr static const int8_t POS_ONE = 1;
2727

2828
/// A
2929
/// multi-line
3030
/// doc
3131
/// comment.
32-
static const int8_t NEG_ONE = -1;
32+
constexpr static const int8_t NEG_ONE = -1;
3333

34-
static const int64_t SHIFT = 3;
34+
constexpr static const int64_t SHIFT = 3;
3535

36-
static const int64_t XBOOL = 1;
36+
constexpr static const int64_t XBOOL = 1;
3737

38-
static const int64_t XFALSE = ((0 << SHIFT) | XBOOL);
38+
constexpr static const int64_t XFALSE = ((0 << SHIFT) | XBOOL);
3939

40-
static const int64_t XTRUE = (1 << (SHIFT | XBOOL));
40+
constexpr static const int64_t XTRUE = (1 << (SHIFT | XBOOL));
4141

42-
static const uint8_t CAST = (uint8_t)'A';
42+
constexpr static const uint8_t CAST = (uint8_t)'A';
4343

44-
static const uint32_t DOUBLE_CAST = (uint32_t)(float)1;
44+
constexpr static const uint32_t DOUBLE_CAST = (uint32_t)(float)1;
4545

4646
struct Foo {
4747
int32_t x[FOO];

tests/expectations/constant_big.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include <ostream>
55
#include <new>
66

7-
static const uint64_t UNSIGNED_NEEDS_ULL_SUFFIX = 9223372036854775808ULL;
7+
constexpr static const uint64_t UNSIGNED_NEEDS_ULL_SUFFIX = 9223372036854775808ULL;
88

9-
static const uint64_t UNSIGNED_DOESNT_NEED_ULL_SUFFIX = 8070450532247928832;
9+
constexpr static const uint64_t UNSIGNED_DOESNT_NEED_ULL_SUFFIX = 8070450532247928832;
1010

11-
static const int64_t SIGNED_NEEDS_ULL_SUFFIX = -9223372036854775808ULL;
11+
constexpr static const int64_t SIGNED_NEEDS_ULL_SUFFIX = -9223372036854775808ULL;
1212

13-
static const int64_t SIGNED_DOESNT_NEED_ULL_SUFFIX = -9223372036854775807;
13+
constexpr static const int64_t SIGNED_DOESNT_NEED_ULL_SUFFIX = -9223372036854775807;

tests/expectations/constant_constexpr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
#include <ostream>
55
#include <new>
66

7-
constexpr static const int64_t CONSTANT_I64 = 216;
7+
static const int64_t CONSTANT_I64 = 216;
88

9-
constexpr static const float CONSTANT_FLOAT32 = 312.292;
9+
static const float CONSTANT_FLOAT32 = 312.292;
1010

11-
constexpr static const uint32_t DELIMITER = ':';
11+
static const uint32_t DELIMITER = ':';
1212

13-
constexpr static const uint32_t LEFTCURLY = '{';
13+
static const uint32_t LEFTCURLY = '{';
1414

1515
struct Foo {
1616
int32_t x;
1717
static const int64_t CONSTANT_I64_BODY;
1818
};
19-
constexpr inline const int64_t Foo::CONSTANT_I64_BODY = 216;
19+
inline const int64_t Foo::CONSTANT_I64_BODY = 216;
2020

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

tests/expectations/constant_sort_name.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include <ostream>
55
#include <new>
66

7-
static const uint8_t A = 0;
7+
constexpr static const uint8_t A = 0;
88

9-
static const uint8_t B = 0;
9+
constexpr static const uint8_t B = 0;
1010

1111
extern "C" {
1212

tests/expectations/constant_sort_none.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include <ostream>
55
#include <new>
66

7-
static const uint8_t B = 0;
7+
constexpr static const uint8_t B = 0;
88

9-
static const uint8_t A = 0;
9+
constexpr static const uint8_t A = 0;
1010

1111
extern "C" {
1212

tests/expectations/constant_user_defined_type.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ struct S {
1414

1515
using A = uint8_t;
1616

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

19-
static const E C2 = V;
19+
constexpr static const E C2 = V;
2020

21-
static const A C3 = 0;
21+
constexpr static const A C3 = 0;

tests/expectations/enum_discriminant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ostream>
55
#include <new>
66

7-
static const int8_t FOURTY_FOUR = 4;
7+
constexpr static const int8_t FOURTY_FOUR = 4;
88

99
enum class E : int8_t {
1010
A = 1,

tests/expectations/mod_2015.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ostream>
55
#include <new>
66

7-
static const uint8_t EXPORT_ME_TOO = 42;
7+
constexpr static const uint8_t EXPORT_ME_TOO = 42;
88

99
struct ExportMe {
1010
uint64_t val;

tests/expectations/mod_2018.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ostream>
55
#include <new>
66

7-
static const uint8_t EXPORT_ME_TOO = 42;
7+
constexpr static const uint8_t EXPORT_ME_TOO = 42;
88

99
struct ExportMe {
1010
uint64_t val;

tests/expectations/mod_attr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ DEF BAR = 0
1111
#include <new>
1212

1313
#if defined(FOO)
14-
static const int32_t FOO = 1;
14+
constexpr static const int32_t FOO = 1;
1515
#endif
1616

1717
#if defined(BAR)
18-
static const int32_t BAR = 2;
18+
constexpr static const int32_t BAR = 2;
1919
#endif
2020

2121
#if defined(FOO)

tests/expectations/mod_path.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ostream>
55
#include <new>
66

7-
static const uint8_t EXPORT_ME_TOO = 42;
7+
constexpr static const uint8_t EXPORT_ME_TOO = 42;
88

99
struct ExportMe {
1010
uint64_t val;

tests/expectations/namespace_constant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
namespace constants {
88

9-
static const int32_t FOO = 10;
9+
constexpr static const int32_t FOO = 10;
1010

11-
static const float ZOM = 3.14;
11+
constexpr static const float ZOM = 3.14;
1212

1313
struct Foo {
1414
int32_t x[FOO];

tests/expectations/namespaces_constant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
namespace constants {
88
namespace test {
99

10-
static const int32_t FOO = 10;
10+
constexpr static const int32_t FOO = 10;
1111

12-
static const float ZOM = 3.14;
12+
constexpr static const float ZOM = 3.14;
1313

1414
struct Foo {
1515
int32_t x[FOO];

tests/expectations/prefix.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#include <ostream>
55
#include <new>
66

7-
static const int32_t PREFIX_LEN = 22;
7+
constexpr static const int32_t PREFIX_LEN = 22;
88

9-
static const int64_t PREFIX_X = (22 << 22);
9+
constexpr static const int64_t PREFIX_X = (22 << 22);
1010

11-
static const int64_t PREFIX_Y = (PREFIX_X + PREFIX_X);
11+
constexpr static const int64_t PREFIX_Y = (PREFIX_X + PREFIX_X);
1212

1313
using PREFIX_NamedLenArray = int32_t[PREFIX_LEN];
1414

tests/expectations/prefixed_struct_literal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ struct PREFIXFoo {
88
int32_t a;
99
uint32_t b;
1010
};
11-
static const PREFIXFoo PREFIXFoo_FOO = PREFIXFoo{ /* .a = */ 42, /* .b = */ 47 };
11+
constexpr static const PREFIXFoo PREFIXFoo_FOO = PREFIXFoo{ /* .a = */ 42, /* .b = */ 47 };
1212

13-
static const PREFIXFoo PREFIXBAR = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337 };
13+
constexpr static const PREFIXFoo PREFIXBAR = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337 };
1414

1515
extern "C" {
1616

tests/expectations/prefixed_struct_literal_deep.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct PREFIXFoo {
1414
PREFIXBar bar;
1515
};
1616

17-
static const PREFIXFoo PREFIXVAL = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337, /* .bar = */ PREFIXBar{ /* .a = */ 323 } };
17+
constexpr static const PREFIXFoo PREFIXVAL = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337, /* .bar = */ PREFIXBar{ /* .a = */ 323 } };
1818

1919
extern "C" {
2020

tests/expectations/rename.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ostream>
55
#include <new>
66

7-
static const int32_t C_H = 10;
7+
constexpr static const int32_t C_H = 10;
88

99
enum class C_E : uint8_t {
1010
x = 0,

tests/expectations/struct_literal.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ struct Foo {
1010
int32_t a;
1111
uint32_t b;
1212
};
13-
static const Foo Foo_FOO = Foo{ /* .a = */ 42, /* .b = */ 47 };
14-
static const Foo Foo_FOO2 = Foo{ /* .a = */ 42, /* .b = */ 47 };
15-
static const Foo Foo_FOO3 = Foo{ /* .a = */ 42, /* .b = */ 47 };
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 };
1616

1717

18-
static const Foo BAR = Foo{ /* .a = */ 42, /* .b = */ 1337 };
18+
constexpr static const Foo BAR = Foo{ /* .a = */ 42, /* .b = */ 1337 };
1919

2020

2121

tests/expectations/struct_literal_order.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ struct ABC {
99
uint32_t b;
1010
uint32_t c;
1111
};
12-
static const ABC ABC_abc = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 };
13-
static const ABC ABC_bac = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 };
14-
static const ABC ABC_cba = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 };
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 };
1515

1616
struct BAC {
1717
uint32_t b;
1818
float a;
1919
int32_t c;
2020
};
21-
static const BAC BAC_abc = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 };
22-
static const BAC BAC_bac = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 };
23-
static const BAC BAC_cba = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 };
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 };
2424

2525
extern "C" {
2626

tests/expectations/transparent.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ template<typename T>
2323
using TransparentPrimitiveWrapper = uint32_t;
2424

2525
using TransparentPrimitiveWithAssociatedConstants = uint32_t;
26-
static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ZERO = 0;
27-
static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ONE = 1;
26+
constexpr static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ZERO = 0;
27+
constexpr static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ONE = 1;
2828

29-
static const TransparentPrimitiveWrappingStructure EnumWithAssociatedConstantInImpl_TEN = 10;
29+
constexpr static const TransparentPrimitiveWrappingStructure EnumWithAssociatedConstantInImpl_TEN = 10;
3030

3131
extern "C" {
3232

tests/expectations/workspace.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <ostream>
55
#include <new>
66

7-
static const int32_t EXT_CONST = 0;
7+
constexpr static const int32_t EXT_CONST = 0;
88

99
struct ExtType {
1010
uint32_t data;

tests/rust/constant_constexpr.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[const]
2-
allow_constexpr = true
2+
allow_constexpr = false
33

44
[struct]
55
associated_constants_in_body = true

0 commit comments

Comments
 (0)