Skip to content

Commit 4628716

Browse files
AlaskanEmilyemilio
authored andcommitted
Add option to output constexpr generated constant primitive values
This is controlled by the [const] allow_constexpr option, similar to the allow_static_const option. It's only applied to primitives currently.
1 parent 61e55ce commit 4628716

File tree

8 files changed

+93
-5
lines changed

8 files changed

+93
-5
lines changed

docs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,11 @@ private_default_tagged_enum_constructor = false
786786
# default: true
787787
allow_static_const = true
788788

789+
# Whether a generated constant can be constexpr in C++ mode.
790+
#
791+
# default: false
792+
allow_constexpr = false
793+
789794

790795

791796

src/bindgen/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,15 @@ impl EnumConfig {
555555
pub struct ConstantConfig {
556556
/// Whether a generated constant can be a static const in C++ mode.
557557
pub allow_static_const: bool,
558+
/// Whether a generated constant should be constexpr in C++ mode.
559+
pub allow_constexpr: bool,
558560
}
559561

560562
impl Default for ConstantConfig {
561563
fn default() -> ConstantConfig {
562564
ConstantConfig {
563565
allow_static_const: true,
566+
allow_constexpr: false,
564567
}
565568
}
566569
}

src/bindgen/ir/constant.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,24 @@ impl Constant {
491491

492492
self.documentation.write(config, out);
493493

494-
if config.constant.allow_static_const && config.language == Language::Cxx {
495-
out.write(if in_body { "inline " } else { "static " });
496-
if let Type::ConstPtr(..) = self.ty {
497-
// Nothing.
494+
let allow_constexpr = if let Type::Primitive(..) = self.ty {
495+
config.constant.allow_constexpr
496+
} else {
497+
false
498+
};
499+
500+
if (config.constant.allow_static_const || allow_constexpr)
501+
&& config.language == Language::Cxx
502+
{
503+
if allow_constexpr {
504+
out.write("constexpr ")
498505
} else {
499-
out.write("const ");
506+
out.write(if in_body { "inline " } else { "static " });
507+
if let Type::ConstPtr(..) = self.ty {
508+
// Nothing.
509+
} else {
510+
out.write("const ");
511+
}
500512
}
501513
self.ty.write(config, out);
502514
write!(out, " {} = ", name);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdarg.h>
2+
#include <stdbool.h>
3+
#include <stdint.h>
4+
#include <stdlib.h>
5+
6+
#define CONSTANT_FLOAT32 312.292
7+
8+
#define CONSTANT_I64 216
9+
10+
#define DELIMITER ':'
11+
12+
#define LEFTCURLY '{'
13+
14+
typedef struct {
15+
int32_t x;
16+
} Foo;
17+
18+
#define SomeFoo (Foo){ .x = 99 }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdarg.h>
2+
#include <stdbool.h>
3+
#include <stdint.h>
4+
#include <stdlib.h>
5+
6+
#define CONSTANT_FLOAT32 312.292
7+
8+
#define CONSTANT_I64 216
9+
10+
#define DELIMITER ':'
11+
12+
#define LEFTCURLY '{'
13+
14+
typedef struct {
15+
int32_t x;
16+
} Foo;
17+
18+
#define SomeFoo (Foo){ .x = 99 }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <cstdarg>
2+
#include <cstdint>
3+
#include <cstdlib>
4+
#include <new>
5+
6+
constexpr float CONSTANT_FLOAT32 = 312.292;
7+
8+
constexpr int64_t CONSTANT_I64 = 216;
9+
10+
constexpr uint32_t DELIMITER = ':';
11+
12+
constexpr uint32_t LEFTCURLY = '{';
13+
14+
struct Foo {
15+
int32_t x;
16+
};
17+
18+
static const Foo SomeFoo = Foo{ /* .x = */ 99 };

tests/rust/constant_constexpr.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub const SomeName: &'static str = "hello world";
2+
pub const CONSTANT_I64: i64 = 216;
3+
pub const CONSTANT_FLOAT32: f32 = 312.292;
4+
pub const DELIMITER: char = ':';
5+
pub const LEFTCURLY: char = '{';
6+
#[repr(C)]
7+
struct Foo {
8+
x: i32,
9+
}
10+
11+
pub const SomeFoo: Foo = Foo{ x: 99, };

tests/rust/constant_constexpr.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[const]
2+
allow_constexpr = true
3+
allow_static_const = true

0 commit comments

Comments
 (0)