Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to output constexpr generated constant primitive values #481

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,11 @@ private_default_tagged_enum_constructor = false
# default: true
allow_static_const = true

# Whether a generated constant can be constexpr in C++ mode.
#
# default: false
allow_constexpr = false




Expand Down
3 changes: 3 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,15 @@ impl EnumConfig {
pub struct ConstantConfig {
/// Whether a generated constant can be a static const in C++ mode.
pub allow_static_const: bool,
/// Whether a generated constant should be constexpr in C++ mode.
pub allow_constexpr: bool,
}

impl Default for ConstantConfig {
fn default() -> ConstantConfig {
ConstantConfig {
allow_static_const: true,
allow_constexpr: false,
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,22 @@ impl Constant {

self.documentation.write(config, out);

if config.constant.allow_static_const && config.language == Language::Cxx {
out.write(if in_body { "inline " } else { "static " });
if let Type::ConstPtr(..) = self.ty {
// Nothing.
let allow_constexpr = if let Type::Primitive(..) = self.ty {
config.constant.allow_constexpr
} else {
false
};

if (config.constant.allow_static_const || allow_constexpr) && config.language == Language::Cxx {
if allow_constexpr {
out.write("constexpr ")
} else {
out.write("const ");
out.write(if in_body { "inline " } else { "static " });
if let Type::ConstPtr(..) = self.ty {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation here looks off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

// Nothing.
} else {
out.write("const ");
}
}
self.ty.write(config, out);
write!(out, " {} = ", name);
Expand Down
18 changes: 18 additions & 0 deletions tests/expectations/constant_constexpr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define CONSTANT_FLOAT32 312.292

#define CONSTANT_I64 216

#define DELIMITER ':'

#define LEFTCURLY '{'

typedef struct {
int32_t x;
} Foo;

#define SomeFoo (Foo){ .x = 99 }
18 changes: 18 additions & 0 deletions tests/expectations/constant_constexpr.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define CONSTANT_FLOAT32 312.292

#define CONSTANT_I64 216

#define DELIMITER ':'

#define LEFTCURLY '{'

typedef struct {
int32_t x;
} Foo;

#define SomeFoo (Foo){ .x = 99 }
18 changes: 18 additions & 0 deletions tests/expectations/constant_constexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>

constexpr float CONSTANT_FLOAT32 = 312.292;

constexpr int64_t CONSTANT_I64 = 216;

constexpr uint32_t DELIMITER = ':';

constexpr uint32_t LEFTCURLY = '{';

struct Foo {
int32_t x;
};

static const Foo SomeFoo = Foo{ /* .x = */ 99 };
11 changes: 11 additions & 0 deletions tests/rust/constant_constexpr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub const SomeName: &'static str = "hello world";
pub const CONSTANT_I64: i64 = 216;
pub const CONSTANT_FLOAT32: f32 = 312.292;
pub const DELIMITER: char = ':';
pub const LEFTCURLY: char = '{';
#[repr(C)]
struct Foo {
x: i32,
}

pub const SomeFoo: Foo = Foo{ x: 99, };
3 changes: 3 additions & 0 deletions tests/rust/constant_constexpr.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[const]
allow_constexpr = true
allow_static_const = true