Skip to content

Commit 42d53f0

Browse files
committed
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 3b6647b commit 42d53f0

File tree

7 files changed

+88
-5
lines changed

7 files changed

+88
-5
lines changed

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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,22 @@ 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) && config.language == Language::Cxx {
501+
if allow_constexpr {
502+
out.write("constexpr ")
498503
} else {
499-
out.write("const ");
504+
out.write(if in_body { "inline " } else { "static " });
505+
if let Type::ConstPtr(..) = self.ty {
506+
// Nothing.
507+
} else {
508+
out.write("const ");
509+
}
500510
}
501511
self.ty.write(config, out);
502512
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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, };
12+

tests/rust/constant_constexpr.toml

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

0 commit comments

Comments
 (0)