Skip to content

Commit

Permalink
Auto merge of #1427 - emilio:const-static, r=emilio
Browse files Browse the repository at this point in the history
codegen: Teach bindgen to respect constness of statics.

```c
const extern struct Foo gFoo;
```

Should never generate a `static mut`.
  • Loading branch information
bors-servo committed Oct 22, 2018
2 parents d7a74ba + 710cbc0 commit 0d0a245
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
repository = "https://github.com/rust-lang-nursery/rust-bindgen"
documentation = "https://docs.rs/bindgen"
homepage = "https://rust-lang-nursery.github.io/rust-bindgen/"
version = "0.42.3"
version = "0.43.0"
build = "build.rs"

include = [
Expand Down Expand Up @@ -44,7 +44,7 @@ shlex = "0.1"

[dependencies]
bitflags = "1.0.3"
cexpr = "0.3"
cexpr = "0.3.3"
cfg-if = "0.1.0"
# This kinda sucks: https://github.com/rust-lang/cargo/issues/1982
clap = "2"
Expand Down
8 changes: 7 additions & 1 deletion src/codegen/mod.rs
Expand Up @@ -575,10 +575,16 @@ impl CodeGenerator for Var {
attrs.push(attributes::link_name(self.name()));
}

let maybe_mut = if self.is_const() {
quote! { }
} else {
quote! { mut }
};

let mut tokens = quote!(
extern "C" {
#(#attrs)*
pub static mut #canonical_ident: #ty;
pub static #maybe_mut #canonical_ident: #ty;
}
);

Expand Down
46 changes: 46 additions & 0 deletions tests/expectations/tests/extern-const-struct.rs
@@ -0,0 +1,46 @@
/* automatically generated by rust-bindgen */

#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(C)]
#[derive(Copy, Clone)]
pub struct nsFoo {
pub details: [f32; 400usize],
}
#[test]
fn bindgen_test_layout_nsFoo() {
assert_eq!(
::std::mem::size_of::<nsFoo>(),
1600usize,
concat!("Size of: ", stringify!(nsFoo))
);
assert_eq!(
::std::mem::align_of::<nsFoo>(),
4usize,
concat!("Alignment of ", stringify!(nsFoo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<nsFoo>())).details as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(nsFoo),
"::",
stringify!(details)
)
);
}
impl Default for nsFoo {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
extern "C" {
#[link_name = "\u{1}gDetails"]
pub static gDetails: nsFoo;
}
11 changes: 8 additions & 3 deletions tests/expectations/tests/issue-511.rs
@@ -1,6 +1,11 @@
/* automatically generated by rust-bindgen */

#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

extern "C" {
#[link_name = "\u{1}a"]
Expand All @@ -12,9 +17,9 @@ extern "C" {
}
extern "C" {
#[link_name = "\u{1}c"]
pub static mut c: *mut ::std::os::raw::c_char;
pub static c: *mut ::std::os::raw::c_char;
}
extern "C" {
#[link_name = "\u{1}d"]
pub static mut d: *const ::std::os::raw::c_char;
pub static d: *const ::std::os::raw::c_char;
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/libclang-3.8/auto.rs
Expand Up @@ -11,7 +11,7 @@ pub struct Foo {
}
extern "C" {
#[link_name = "\u{1}_ZN3Foo4kFooE"]
pub static mut Foo_kFoo: bool;
pub static Foo_kFoo: bool;
}
#[test]
fn bindgen_test_layout_Foo() {
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/libclang-3.8/const_bool.rs
Expand Up @@ -6,7 +6,7 @@

extern "C" {
#[link_name = "\u{1}_ZL1k"]
pub static mut k: bool;
pub static k: bool;
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
Expand All @@ -15,7 +15,7 @@ pub struct A {
}
extern "C" {
#[link_name = "\u{1}_ZN1A1kE"]
pub static mut A_k: bool;
pub static A_k: bool;
}
#[test]
fn bindgen_test_layout_A() {
Expand All @@ -33,5 +33,5 @@ fn bindgen_test_layout_A() {
pub type foo = bool;
extern "C" {
#[link_name = "\u{1}_ZL2k2"]
pub static mut k2: foo;
pub static k2: foo;
}
12 changes: 6 additions & 6 deletions tests/expectations/tests/libclang-3.8/constant-evaluate.rs
Expand Up @@ -13,27 +13,27 @@ pub type EasyToOverflow = ::std::os::raw::c_ulonglong;
pub const k: EasyToOverflow = 2147483648;
extern "C" {
#[link_name = "\u{1}k_expr"]
pub static mut k_expr: EasyToOverflow;
pub static k_expr: EasyToOverflow;
}
extern "C" {
#[link_name = "\u{1}wow"]
pub static mut wow: EasyToOverflow;
pub static wow: EasyToOverflow;
}
extern "C" {
#[link_name = "\u{1}BAZ"]
pub static mut BAZ: ::std::os::raw::c_longlong;
pub static BAZ: ::std::os::raw::c_longlong;
}
extern "C" {
#[link_name = "\u{1}fuzz"]
pub static mut fuzz: f64;
pub static fuzz: f64;
}
extern "C" {
#[link_name = "\u{1}BAZZ"]
pub static mut BAZZ: ::std::os::raw::c_char;
pub static BAZZ: ::std::os::raw::c_char;
}
extern "C" {
#[link_name = "\u{1}WAT"]
pub static mut WAT: ::std::os::raw::c_char;
pub static WAT: ::std::os::raw::c_char;
}
extern "C" {
#[link_name = "\u{1}bytestring"]
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/libclang-3.9/constant-evaluate.rs
Expand Up @@ -17,7 +17,7 @@ pub const k: EasyToOverflow = 2147483648;
pub const k_expr: EasyToOverflow = 0;
extern "C" {
#[link_name = "\u{1}wow"]
pub static mut wow: EasyToOverflow;
pub static wow: EasyToOverflow;
}
pub const BAZ: ::std::os::raw::c_longlong = 24;
pub const fuzz: f64 = 51.0;
Expand Down
5 changes: 5 additions & 0 deletions tests/headers/extern-const-struct.h
@@ -0,0 +1,5 @@
struct nsFoo {
float details[400];
};

extern const struct nsFoo gDetails;

0 comments on commit 0d0a245

Please sign in to comment.