From 305ccfe74676a371e3f69b560dbaf0b81a6a4094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Thu, 21 Apr 2022 13:07:15 +0200 Subject: [PATCH] constant: Make const.allow_constexpr default to true. --- docs.md | 2 +- src/bindgen/config.rs | 2 +- tests/expectations/assoc_const_conflict.cpp | 2 +- tests/expectations/assoc_constant.cpp | 4 +-- tests/expectations/cfg_2.cpp | 4 +-- tests/expectations/const_conflict.cpp | 2 +- tests/expectations/const_transparent.cpp | 2 +- tests/expectations/constant.cpp | 34 +++++++++---------- tests/expectations/constant_big.cpp | 8 ++--- tests/expectations/constant_constexpr.cpp | 12 +++---- tests/expectations/constant_sort_name.cpp | 4 +-- tests/expectations/constant_sort_none.cpp | 4 +-- .../constant_user_defined_type.cpp | 6 ++-- tests/expectations/enum_discriminant.cpp | 2 +- tests/expectations/mod_2015.cpp | 2 +- tests/expectations/mod_2018.cpp | 2 +- tests/expectations/mod_attr.cpp | 4 +-- tests/expectations/mod_path.cpp | 2 +- tests/expectations/namespace_constant.cpp | 4 +-- tests/expectations/namespaces_constant.cpp | 4 +-- tests/expectations/prefix.cpp | 6 ++-- .../expectations/prefixed_struct_literal.cpp | 4 +-- .../prefixed_struct_literal_deep.cpp | 2 +- tests/expectations/rename.cpp | 2 +- tests/expectations/struct_literal.cpp | 8 ++--- tests/expectations/struct_literal_order.cpp | 12 +++---- tests/expectations/transparent.cpp | 6 ++-- tests/expectations/workspace.cpp | 2 +- tests/rust/constant_constexpr.toml | 2 +- 29 files changed, 75 insertions(+), 75 deletions(-) diff --git a/docs.md b/docs.md index e97e16f..9a434aa 100644 --- a/docs.md +++ b/docs.md @@ -913,7 +913,7 @@ allow_static_const = true # Whether a generated constant can be constexpr in C++ mode. # -# default: false +# default: true allow_constexpr = false # This rule specifies the order in which constants will be sorted. diff --git a/src/bindgen/config.rs b/src/bindgen/config.rs index d0dbf22..b4a0cb7 100644 --- a/src/bindgen/config.rs +++ b/src/bindgen/config.rs @@ -706,7 +706,7 @@ impl Default for ConstantConfig { fn default() -> ConstantConfig { ConstantConfig { allow_static_const: true, - allow_constexpr: false, + allow_constexpr: true, sort_by: None, } } diff --git a/tests/expectations/assoc_const_conflict.cpp b/tests/expectations/assoc_const_conflict.cpp index a342401..f031e27 100644 --- a/tests/expectations/assoc_const_conflict.cpp +++ b/tests/expectations/assoc_const_conflict.cpp @@ -4,4 +4,4 @@ #include #include -static const uint32_t Foo_FOO = 42; +constexpr static const uint32_t Foo_FOO = 42; diff --git a/tests/expectations/assoc_constant.cpp b/tests/expectations/assoc_constant.cpp index 4a9f51b..07a0dbd 100644 --- a/tests/expectations/assoc_constant.cpp +++ b/tests/expectations/assoc_constant.cpp @@ -7,8 +7,8 @@ struct Foo { }; -static const int32_t Foo_GA = 10; -static const float Foo_ZO = 3.14; +constexpr static const int32_t Foo_GA = 10; +constexpr static const float Foo_ZO = 3.14; extern "C" { diff --git a/tests/expectations/cfg_2.cpp b/tests/expectations/cfg_2.cpp index cddb7a4..40aee48 100644 --- a/tests/expectations/cfg_2.cpp +++ b/tests/expectations/cfg_2.cpp @@ -11,11 +11,11 @@ DEF NOT_DEFINED = 0 #include #if defined(NOT_DEFINED) -static const int32_t DEFAULT_X = 8; +constexpr static const int32_t DEFAULT_X = 8; #endif #if defined(DEFINED) -static const int32_t DEFAULT_X = 42; +constexpr static const int32_t DEFAULT_X = 42; #endif #if (defined(NOT_DEFINED) || defined(DEFINED)) diff --git a/tests/expectations/const_conflict.cpp b/tests/expectations/const_conflict.cpp index a342401..f031e27 100644 --- a/tests/expectations/const_conflict.cpp +++ b/tests/expectations/const_conflict.cpp @@ -4,4 +4,4 @@ #include #include -static const uint32_t Foo_FOO = 42; +constexpr static const uint32_t Foo_FOO = 42; diff --git a/tests/expectations/const_transparent.cpp b/tests/expectations/const_transparent.cpp index d536dd9..920af50 100644 --- a/tests/expectations/const_transparent.cpp +++ b/tests/expectations/const_transparent.cpp @@ -6,4 +6,4 @@ using Transparent = uint8_t; -static const Transparent FOO = 0; +constexpr static const Transparent FOO = 0; diff --git a/tests/expectations/constant.cpp b/tests/expectations/constant.cpp index c8196d3..a43099d 100644 --- a/tests/expectations/constant.cpp +++ b/tests/expectations/constant.cpp @@ -4,44 +4,44 @@ #include #include -static const int32_t FOO = 10; +constexpr static const int32_t FOO = 10; -static const uint32_t DELIMITER = ':'; +constexpr static const uint32_t DELIMITER = ':'; -static const uint32_t LEFTCURLY = '{'; +constexpr static const uint32_t LEFTCURLY = '{'; -static const uint32_t QUOTE = '\''; +constexpr static const uint32_t QUOTE = '\''; -static const uint32_t TAB = '\t'; +constexpr static const uint32_t TAB = '\t'; -static const uint32_t NEWLINE = '\n'; +constexpr static const uint32_t NEWLINE = '\n'; -static const uint32_t HEART = U'\U00002764'; +constexpr static const uint32_t HEART = U'\U00002764'; -static const uint32_t EQUID = U'\U00010083'; +constexpr static const uint32_t EQUID = U'\U00010083'; -static const float ZOM = 3.14; +constexpr static const float ZOM = 3.14; /// A single-line doc comment. -static const int8_t POS_ONE = 1; +constexpr static const int8_t POS_ONE = 1; /// A /// multi-line /// doc /// comment. -static const int8_t NEG_ONE = -1; +constexpr static const int8_t NEG_ONE = -1; -static const int64_t SHIFT = 3; +constexpr static const int64_t SHIFT = 3; -static const int64_t XBOOL = 1; +constexpr static const int64_t XBOOL = 1; -static const int64_t XFALSE = ((0 << SHIFT) | XBOOL); +constexpr static const int64_t XFALSE = ((0 << SHIFT) | XBOOL); -static const int64_t XTRUE = (1 << (SHIFT | XBOOL)); +constexpr static const int64_t XTRUE = (1 << (SHIFT | XBOOL)); -static const uint8_t CAST = (uint8_t)'A'; +constexpr static const uint8_t CAST = (uint8_t)'A'; -static const uint32_t DOUBLE_CAST = (uint32_t)(float)1; +constexpr static const uint32_t DOUBLE_CAST = (uint32_t)(float)1; struct Foo { int32_t x[FOO]; diff --git a/tests/expectations/constant_big.cpp b/tests/expectations/constant_big.cpp index 2b5b87a..d86c3e7 100644 --- a/tests/expectations/constant_big.cpp +++ b/tests/expectations/constant_big.cpp @@ -4,10 +4,10 @@ #include #include -static const uint64_t UNSIGNED_NEEDS_ULL_SUFFIX = 9223372036854775808ULL; +constexpr static const uint64_t UNSIGNED_NEEDS_ULL_SUFFIX = 9223372036854775808ULL; -static const uint64_t UNSIGNED_DOESNT_NEED_ULL_SUFFIX = 8070450532247928832; +constexpr static const uint64_t UNSIGNED_DOESNT_NEED_ULL_SUFFIX = 8070450532247928832; -static const int64_t SIGNED_NEEDS_ULL_SUFFIX = -9223372036854775808ULL; +constexpr static const int64_t SIGNED_NEEDS_ULL_SUFFIX = -9223372036854775808ULL; -static const int64_t SIGNED_DOESNT_NEED_ULL_SUFFIX = -9223372036854775807; +constexpr static const int64_t SIGNED_DOESNT_NEED_ULL_SUFFIX = -9223372036854775807; diff --git a/tests/expectations/constant_constexpr.cpp b/tests/expectations/constant_constexpr.cpp index dab7437..f600980 100644 --- a/tests/expectations/constant_constexpr.cpp +++ b/tests/expectations/constant_constexpr.cpp @@ -4,18 +4,18 @@ #include #include -constexpr static const int64_t CONSTANT_I64 = 216; +static const int64_t CONSTANT_I64 = 216; -constexpr static const float CONSTANT_FLOAT32 = 312.292; +static const float CONSTANT_FLOAT32 = 312.292; -constexpr static const uint32_t DELIMITER = ':'; +static const uint32_t DELIMITER = ':'; -constexpr static const uint32_t LEFTCURLY = '{'; +static const uint32_t LEFTCURLY = '{'; struct Foo { int32_t x; static const int64_t CONSTANT_I64_BODY; }; -constexpr inline const int64_t Foo::CONSTANT_I64_BODY = 216; +inline const int64_t Foo::CONSTANT_I64_BODY = 216; -constexpr static const Foo SomeFoo = Foo{ /* .x = */ 99 }; +static const Foo SomeFoo = Foo{ /* .x = */ 99 }; diff --git a/tests/expectations/constant_sort_name.cpp b/tests/expectations/constant_sort_name.cpp index 6de3dda..1a7dd50 100644 --- a/tests/expectations/constant_sort_name.cpp +++ b/tests/expectations/constant_sort_name.cpp @@ -4,9 +4,9 @@ #include #include -static const uint8_t A = 0; +constexpr static const uint8_t A = 0; -static const uint8_t B = 0; +constexpr static const uint8_t B = 0; extern "C" { diff --git a/tests/expectations/constant_sort_none.cpp b/tests/expectations/constant_sort_none.cpp index 6e7360b..c5571e9 100644 --- a/tests/expectations/constant_sort_none.cpp +++ b/tests/expectations/constant_sort_none.cpp @@ -4,9 +4,9 @@ #include #include -static const uint8_t B = 0; +constexpr static const uint8_t B = 0; -static const uint8_t A = 0; +constexpr static const uint8_t A = 0; extern "C" { diff --git a/tests/expectations/constant_user_defined_type.cpp b/tests/expectations/constant_user_defined_type.cpp index c00e83f..f127292 100644 --- a/tests/expectations/constant_user_defined_type.cpp +++ b/tests/expectations/constant_user_defined_type.cpp @@ -14,8 +14,8 @@ struct S { using A = uint8_t; -static const S C1 = S{ /* .field = */ 0 }; +constexpr static const S C1 = S{ /* .field = */ 0 }; -static const E C2 = V; +constexpr static const E C2 = V; -static const A C3 = 0; +constexpr static const A C3 = 0; diff --git a/tests/expectations/enum_discriminant.cpp b/tests/expectations/enum_discriminant.cpp index 30b383f..8183823 100644 --- a/tests/expectations/enum_discriminant.cpp +++ b/tests/expectations/enum_discriminant.cpp @@ -4,7 +4,7 @@ #include #include -static const int8_t FOURTY_FOUR = 4; +constexpr static const int8_t FOURTY_FOUR = 4; enum class E : int8_t { A = 1, diff --git a/tests/expectations/mod_2015.cpp b/tests/expectations/mod_2015.cpp index 0259968..b3caaed 100644 --- a/tests/expectations/mod_2015.cpp +++ b/tests/expectations/mod_2015.cpp @@ -4,7 +4,7 @@ #include #include -static const uint8_t EXPORT_ME_TOO = 42; +constexpr static const uint8_t EXPORT_ME_TOO = 42; struct ExportMe { uint64_t val; diff --git a/tests/expectations/mod_2018.cpp b/tests/expectations/mod_2018.cpp index 4d195ac..cd3ca85 100644 --- a/tests/expectations/mod_2018.cpp +++ b/tests/expectations/mod_2018.cpp @@ -4,7 +4,7 @@ #include #include -static const uint8_t EXPORT_ME_TOO = 42; +constexpr static const uint8_t EXPORT_ME_TOO = 42; struct ExportMe { uint64_t val; diff --git a/tests/expectations/mod_attr.cpp b/tests/expectations/mod_attr.cpp index dd8a63d..10d0fe8 100644 --- a/tests/expectations/mod_attr.cpp +++ b/tests/expectations/mod_attr.cpp @@ -11,11 +11,11 @@ DEF BAR = 0 #include #if defined(FOO) -static const int32_t FOO = 1; +constexpr static const int32_t FOO = 1; #endif #if defined(BAR) -static const int32_t BAR = 2; +constexpr static const int32_t BAR = 2; #endif #if defined(FOO) diff --git a/tests/expectations/mod_path.cpp b/tests/expectations/mod_path.cpp index 22b3f70..0cda2e6 100644 --- a/tests/expectations/mod_path.cpp +++ b/tests/expectations/mod_path.cpp @@ -4,7 +4,7 @@ #include #include -static const uint8_t EXPORT_ME_TOO = 42; +constexpr static const uint8_t EXPORT_ME_TOO = 42; struct ExportMe { uint64_t val; diff --git a/tests/expectations/namespace_constant.cpp b/tests/expectations/namespace_constant.cpp index 7f405f0..889472c 100644 --- a/tests/expectations/namespace_constant.cpp +++ b/tests/expectations/namespace_constant.cpp @@ -6,9 +6,9 @@ namespace constants { -static const int32_t FOO = 10; +constexpr static const int32_t FOO = 10; -static const float ZOM = 3.14; +constexpr static const float ZOM = 3.14; struct Foo { int32_t x[FOO]; diff --git a/tests/expectations/namespaces_constant.cpp b/tests/expectations/namespaces_constant.cpp index 2167fa9..2da55d7 100644 --- a/tests/expectations/namespaces_constant.cpp +++ b/tests/expectations/namespaces_constant.cpp @@ -7,9 +7,9 @@ namespace constants { namespace test { -static const int32_t FOO = 10; +constexpr static const int32_t FOO = 10; -static const float ZOM = 3.14; +constexpr static const float ZOM = 3.14; struct Foo { int32_t x[FOO]; diff --git a/tests/expectations/prefix.cpp b/tests/expectations/prefix.cpp index abde339..7f2c8ba 100644 --- a/tests/expectations/prefix.cpp +++ b/tests/expectations/prefix.cpp @@ -4,11 +4,11 @@ #include #include -static const int32_t PREFIX_LEN = 22; +constexpr static const int32_t PREFIX_LEN = 22; -static const int64_t PREFIX_X = (22 << 22); +constexpr static const int64_t PREFIX_X = (22 << 22); -static const int64_t PREFIX_Y = (PREFIX_X + PREFIX_X); +constexpr static const int64_t PREFIX_Y = (PREFIX_X + PREFIX_X); using PREFIX_NamedLenArray = int32_t[PREFIX_LEN]; diff --git a/tests/expectations/prefixed_struct_literal.cpp b/tests/expectations/prefixed_struct_literal.cpp index d18fe77..218d1c8 100644 --- a/tests/expectations/prefixed_struct_literal.cpp +++ b/tests/expectations/prefixed_struct_literal.cpp @@ -8,9 +8,9 @@ struct PREFIXFoo { int32_t a; uint32_t b; }; -static const PREFIXFoo PREFIXFoo_FOO = PREFIXFoo{ /* .a = */ 42, /* .b = */ 47 }; +constexpr static const PREFIXFoo PREFIXFoo_FOO = PREFIXFoo{ /* .a = */ 42, /* .b = */ 47 }; -static const PREFIXFoo PREFIXBAR = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337 }; +constexpr static const PREFIXFoo PREFIXBAR = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337 }; extern "C" { diff --git a/tests/expectations/prefixed_struct_literal_deep.cpp b/tests/expectations/prefixed_struct_literal_deep.cpp index 7749e3e..4dbfb7b 100644 --- a/tests/expectations/prefixed_struct_literal_deep.cpp +++ b/tests/expectations/prefixed_struct_literal_deep.cpp @@ -14,7 +14,7 @@ struct PREFIXFoo { PREFIXBar bar; }; -static const PREFIXFoo PREFIXVAL = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337, /* .bar = */ PREFIXBar{ /* .a = */ 323 } }; +constexpr static const PREFIXFoo PREFIXVAL = PREFIXFoo{ /* .a = */ 42, /* .b = */ 1337, /* .bar = */ PREFIXBar{ /* .a = */ 323 } }; extern "C" { diff --git a/tests/expectations/rename.cpp b/tests/expectations/rename.cpp index 59862a7..cfa3285 100644 --- a/tests/expectations/rename.cpp +++ b/tests/expectations/rename.cpp @@ -4,7 +4,7 @@ #include #include -static const int32_t C_H = 10; +constexpr static const int32_t C_H = 10; enum class C_E : uint8_t { x = 0, diff --git a/tests/expectations/struct_literal.cpp b/tests/expectations/struct_literal.cpp index a751b75..68fa0f5 100644 --- a/tests/expectations/struct_literal.cpp +++ b/tests/expectations/struct_literal.cpp @@ -10,12 +10,12 @@ struct Foo { int32_t a; uint32_t b; }; -static const Foo Foo_FOO = Foo{ /* .a = */ 42, /* .b = */ 47 }; -static const Foo Foo_FOO2 = Foo{ /* .a = */ 42, /* .b = */ 47 }; -static const Foo Foo_FOO3 = Foo{ /* .a = */ 42, /* .b = */ 47 }; +constexpr static const Foo Foo_FOO = Foo{ /* .a = */ 42, /* .b = */ 47 }; +constexpr static const Foo Foo_FOO2 = Foo{ /* .a = */ 42, /* .b = */ 47 }; +constexpr static const Foo Foo_FOO3 = Foo{ /* .a = */ 42, /* .b = */ 47 }; -static const Foo BAR = Foo{ /* .a = */ 42, /* .b = */ 1337 }; +constexpr static const Foo BAR = Foo{ /* .a = */ 42, /* .b = */ 1337 }; diff --git a/tests/expectations/struct_literal_order.cpp b/tests/expectations/struct_literal_order.cpp index e73339a..9f4021c 100644 --- a/tests/expectations/struct_literal_order.cpp +++ b/tests/expectations/struct_literal_order.cpp @@ -9,18 +9,18 @@ struct ABC { uint32_t b; uint32_t c; }; -static const ABC ABC_abc = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 }; -static const ABC ABC_bac = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 }; -static const ABC ABC_cba = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 }; +constexpr static const ABC ABC_abc = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 }; +constexpr static const ABC ABC_bac = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 }; +constexpr static const ABC ABC_cba = ABC{ /* .a = */ 1.0, /* .b = */ 2, /* .c = */ 3 }; struct BAC { uint32_t b; float a; int32_t c; }; -static const BAC BAC_abc = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 }; -static const BAC BAC_bac = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 }; -static const BAC BAC_cba = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 }; +constexpr static const BAC BAC_abc = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 }; +constexpr static const BAC BAC_bac = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 }; +constexpr static const BAC BAC_cba = BAC{ /* .b = */ 1, /* .a = */ 2.0, /* .c = */ 3 }; extern "C" { diff --git a/tests/expectations/transparent.cpp b/tests/expectations/transparent.cpp index f51491a..4c06739 100644 --- a/tests/expectations/transparent.cpp +++ b/tests/expectations/transparent.cpp @@ -23,10 +23,10 @@ template using TransparentPrimitiveWrapper = uint32_t; using TransparentPrimitiveWithAssociatedConstants = uint32_t; -static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ZERO = 0; -static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ONE = 1; +constexpr static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ZERO = 0; +constexpr static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ONE = 1; -static const TransparentPrimitiveWrappingStructure EnumWithAssociatedConstantInImpl_TEN = 10; +constexpr static const TransparentPrimitiveWrappingStructure EnumWithAssociatedConstantInImpl_TEN = 10; extern "C" { diff --git a/tests/expectations/workspace.cpp b/tests/expectations/workspace.cpp index 1077bbd..b7b31a1 100644 --- a/tests/expectations/workspace.cpp +++ b/tests/expectations/workspace.cpp @@ -4,7 +4,7 @@ #include #include -static const int32_t EXT_CONST = 0; +constexpr static const int32_t EXT_CONST = 0; struct ExtType { uint32_t data; diff --git a/tests/rust/constant_constexpr.toml b/tests/rust/constant_constexpr.toml index bc413fd..5deb0ed 100644 --- a/tests/rust/constant_constexpr.toml +++ b/tests/rust/constant_constexpr.toml @@ -1,5 +1,5 @@ [const] -allow_constexpr = true +allow_constexpr = false [struct] associated_constants_in_body = true