Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ pub struct ConstantConfig {
pub allow_static_const: bool,
/// Whether a generated constant should be constexpr in C++ mode.
pub allow_constexpr: bool,
/// Whether a generated constexpr should be inlined
pub allow_inline: bool,
/// Sort key for constants
pub sort_by: Option<SortKey>,
}
Expand All @@ -723,6 +725,7 @@ impl Default for ConstantConfig {
ConstantConfig {
allow_static_const: true,
allow_constexpr: true,
allow_inline: true,
sort_by: None,
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,13 @@ impl Constant {
}

if config.constant.allow_static_const {
out.write(if in_body { "inline " } else { "static " });
out.write(if in_body && config.constant.allow_inline {
"inline "
} else if !in_body {
"static "
} else {
""
});
}

if let Type::Ptr { is_const: true, .. } = self.ty {
Expand Down
1 change: 1 addition & 0 deletions template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private_default_tagged_enum_constructor = false
[const]
allow_static_const = true
allow_constexpr = false
allow_inline = true # Set to 'false' if compiling before C++17
sort_by = "Name"


Expand Down
19 changes: 19 additions & 0 deletions tests/expectations/no_inline_constant_constexpr.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define CONSTANT_I64 216

#define CONSTANT_FLOAT32 312.292

#define DELIMITER ':'

#define LEFTCURLY '{'

typedef struct {
int32_t x;
} Foo;
#define Foo_CONSTANT_I64_BODY 216

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

static const int64_t CONSTANT_I64 = 216;

static const float CONSTANT_FLOAT32 = 312.292;

static const uint32_t DELIMITER = ':';

static const uint32_t LEFTCURLY = '{';

struct Foo {
int32_t x;
static const int64_t CONSTANT_I64_BODY;
};
const int64_t Foo::CONSTANT_I64_BODY = 216;

static const Foo SomeFoo = Foo{
/* .x = */ 99
};
21 changes: 21 additions & 0 deletions tests/expectations/no_inline_constant_constexpr.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list

cdef extern from *:

const int64_t CONSTANT_I64 # = 216

const float CONSTANT_FLOAT32 # = 312.292

const uint32_t DELIMITER # = ':'

const uint32_t LEFTCURLY # = '{'

ctypedef struct Foo:
int32_t x;
const int64_t Foo_CONSTANT_I64_BODY # = 216

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

#define CONSTANT_I64 216

#define CONSTANT_FLOAT32 312.292

#define DELIMITER ':'

#define LEFTCURLY '{'

typedef struct Foo {
int32_t x;
} Foo;
#define Foo_CONSTANT_I64_BODY 216

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

#define CONSTANT_I64 216

#define CONSTANT_FLOAT32 312.292

#define DELIMITER ':'

#define LEFTCURLY '{'

struct Foo {
int32_t x;
};
#define Foo_CONSTANT_I64_BODY 216

#define SomeFoo (Foo){ .x = 99 }
21 changes: 21 additions & 0 deletions tests/expectations/no_inline_constant_constexpr_tag.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list

cdef extern from *:

const int64_t CONSTANT_I64 # = 216

const float CONSTANT_FLOAT32 # = 312.292

const uint32_t DELIMITER # = ':'

const uint32_t LEFTCURLY # = '{'

cdef struct Foo:
int32_t x;
const int64_t Foo_CONSTANT_I64_BODY # = 216

const Foo SomeFoo # = <Foo>{ 99 }
14 changes: 14 additions & 0 deletions tests/rust/no_inline_constant_constexpr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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, };

impl Foo {
pub const CONSTANT_I64_BODY: i64 = 216;
}
6 changes: 6 additions & 0 deletions tests/rust/no_inline_constant_constexpr.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[const]
allow_constexpr = false
allow_inline = false

[struct]
associated_constants_in_body = true