Skip to content

Commit

Permalink
constant: Make const.allow_constexpr default to true.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Apr 21, 2022
1 parent 5b33796 commit 2c42fa1
Show file tree
Hide file tree
Showing 29 changed files with 75 additions and 75 deletions.
2 changes: 1 addition & 1 deletion docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/assoc_const_conflict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#include <ostream>
#include <new>

static const uint32_t Foo_FOO = 42;
constexpr static const uint32_t Foo_FOO = 42;
4 changes: 2 additions & 2 deletions tests/expectations/assoc_constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" {

Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/cfg_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ DEF NOT_DEFINED = 0
#include <new>

#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))
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/const_conflict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#include <ostream>
#include <new>

static const uint32_t Foo_FOO = 42;
constexpr static const uint32_t Foo_FOO = 42;
2 changes: 1 addition & 1 deletion tests/expectations/const_transparent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

using Transparent = uint8_t;

static const Transparent FOO = 0;
constexpr static const Transparent FOO = 0;
34 changes: 17 additions & 17 deletions tests/expectations/constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@
#include <ostream>
#include <new>

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];
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/constant_big.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include <ostream>
#include <new>

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;
12 changes: 6 additions & 6 deletions tests/expectations/constant_constexpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#include <ostream>
#include <new>

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 };
4 changes: 2 additions & 2 deletions tests/expectations/constant_sort_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <ostream>
#include <new>

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" {

Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/constant_sort_none.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <ostream>
#include <new>

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" {

Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/constant_user_defined_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion tests/expectations/enum_discriminant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ostream>
#include <new>

static const int8_t FOURTY_FOUR = 4;
constexpr static const int8_t FOURTY_FOUR = 4;

enum class E : int8_t {
A = 1,
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/mod_2015.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ostream>
#include <new>

static const uint8_t EXPORT_ME_TOO = 42;
constexpr static const uint8_t EXPORT_ME_TOO = 42;

struct ExportMe {
uint64_t val;
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/mod_2018.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ostream>
#include <new>

static const uint8_t EXPORT_ME_TOO = 42;
constexpr static const uint8_t EXPORT_ME_TOO = 42;

struct ExportMe {
uint64_t val;
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/mod_attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ DEF BAR = 0
#include <new>

#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)
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/mod_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ostream>
#include <new>

static const uint8_t EXPORT_ME_TOO = 42;
constexpr static const uint8_t EXPORT_ME_TOO = 42;

struct ExportMe {
uint64_t val;
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/namespace_constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/namespaces_constant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/prefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#include <ostream>
#include <new>

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];

Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/prefixed_struct_literal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" {

Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/prefixed_struct_literal_deep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" {

Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/rename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ostream>
#include <new>

static const int32_t C_H = 10;
constexpr static const int32_t C_H = 10;

enum class C_E : uint8_t {
x = 0,
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/struct_literal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };



Expand Down
12 changes: 6 additions & 6 deletions tests/expectations/struct_literal_order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" {

Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/transparent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ template<typename T>
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" {

Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <ostream>
#include <new>

static const int32_t EXT_CONST = 0;
constexpr static const int32_t EXT_CONST = 0;

struct ExtType {
uint32_t data;
Expand Down
2 changes: 1 addition & 1 deletion tests/rust/constant_constexpr.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[const]
allow_constexpr = true
allow_constexpr = false

[struct]
associated_constants_in_body = true

0 comments on commit 2c42fa1

Please sign in to comment.