Skip to content

Commit

Permalink
Merge pull request #42 from gnzlbg/arrs
Browse files Browse the repository at this point in the history
Add support for arrays in extern statics
  • Loading branch information
alexcrichton committed Oct 20, 2018
2 parents f8bd332 + 26dc2b1 commit f0fea68
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 20 deletions.
36 changes: 27 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
language: rust
rust:
- stable
- beta
- nightly
dist: trusty
sudo: false
before_script:
- pip install 'travis-cargo<0.2' --user && export PATH=$HOME/.local/bin:$PATH
language: rust

matrix:
fast_finish: true
include:
- name: "stable"
rust: stable
- name: "beta"
rust: beta
- name: "nightly"
rust: nightly
after_success:
- travis-cargo --only nightly doc-upload
- name: "libc-test"
rust: nightly
os: osx
osx_image: xcode9.4
after_script:
- git clone https://github.com/rust-lang/libc
- sed -i '' 's@ctest = "0.2.2"@ctest = { path = "../.." }@g' libc/libc-test/Cargo.toml
- cd libc
- cargo generate-lockfile --manifest-path libc-test/Cargo.toml
- |
export TARGET=x86_64-apple-darwin
sh ci/run.sh $TARGET
script:
- cargo test
- cargo test --manifest-path testcrate/Cargo.toml
- cargo doc --no-deps
after_success:
- travis-cargo --only nightly doc-upload

notifications:
email:
on_success: never
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ctest"
version = "0.2.2"
version = "0.2.3"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ my-sys-library = { path = "../my-sys-library" }
libc = "0.2"

[build-dependencies]
ctest = "0.1"
ctest = "0.2"
```

Next, add a build script to `systest/build.rs`:
Expand Down
46 changes: 43 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,37 @@ impl<'a> Generator<'a> {
}}
}}
"#, name = name, ty = rust_ty));
} else if rust_ty.starts_with("[") && rust_ty.ends_with("]") {
let c_ptr_ty = c_ty.split(" ").nth(0).unwrap();
let mut lens = Vec::new();
for i in c_ty.split(" ").skip(1) {
lens.push(i);
}
lens.reverse();
let array_test_name = format!("{mutbl} {elem} (*__test_static_{name}(void)){lens}",
mutbl = if mutbl { "" } else { "const" },
elem = c_ptr_ty,
name = name,
lens = lens.join("")
);
t!(writeln!(self.c, r#"
{array_test_name} {{
return &{cname};
}}
"#, array_test_name = array_test_name, cname = cname));
t!(writeln!(self.rust, r#"
#[inline(never)]
fn static_{name}() {{
extern {{
fn __test_static_{name}() -> *{mutbl} {ty};
}}
unsafe {{
same(&{name} as *const _ as usize,
__test_static_{name}() as usize,
"{name} static");
}}
}}
"#, name = name, mutbl = if mutbl { "mut" } else { "const" }, ty = rust_ty));
} else {
let c_ty = self.rust_ty_to_c_ty(rust_ty);
t!(writeln!(self.c, r#"
Expand Down Expand Up @@ -1327,8 +1358,13 @@ impl<'a> Generator<'a> {
}
}
ast::TyKind::Array(ref t, ref e) => {
assert!(rust);
format!("[{}; {}]", self.ty2name(t, rust), self.expr2str(e))
if rust {
format!("[{}; {}]", self.ty2name(t, rust), self.expr2str(e))
} else {
let len = self.expr2str(e);
let ty = self.ty2name(t, rust);
format!("{} [{}]", ty, len)
}
}
ast::TyKind::Rptr(_, ast::MutTy {
ref ty,
Expand Down Expand Up @@ -1362,7 +1398,11 @@ impl<'a> Generator<'a> {
format!("char*")
}
}
ast::TyKind::Tup(ref v) if v.is_empty() => if rust { "()".to_string() } else { "void".to_string() },
ast::TyKind::Tup(ref v) if v.is_empty() => if rust {
"()".to_string()
} else {
"void".to_string()
},
_ => panic!("unknown ty {:?}", ty),
}
}
Expand Down
10 changes: 10 additions & 0 deletions testcrate/src/t1.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ uint8_t (*T1_static_right2)(uint8_t, uint8_t) = foo;

uint32_t (*(*T1_fn_ptr_s)(uint8_t))(uint16_t) = nested;
uint32_t (*(*T1_fn_ptr_s2)(uint8_t(*arg0)(uint8_t), uint16_t(*arg1)(uint16_t)))(uint16_t) = nested2;

const int32_t T1_arr0[2] = {0, 0};
const int32_t T1_arr1[2][3] = {{0, 0, 0}, {0, 0, 0}};
const int32_t T1_arr2[1][2][3] = {{{0, 0, 0}, {0, 0, 0}}};

int32_t T1_arr3[2] = {0, 0};
int32_t T1_arr4[2][3] = {{0, 0, 0}, {0, 0, 0}};
int32_t T1_arr5[1][2][3] = {{{0, 0, 0}, {0, 0, 0}}};

int32_t T1_arr42[1][2][3] = {{{0, 0, 0}, {0, 0, 0}}};
26 changes: 21 additions & 5 deletions testcrate/src/t1.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ void T1j(int32_t a[4]);
#define T1C 4

extern uint32_t T1static;
const uint8_t T1_static_u8;
extern const uint8_t T1_static_u8;
uint8_t T1_static_mut_u8;
uint8_t (*T1_static_mut_fn_ptr)(uint8_t, uint8_t);
uint8_t (*const T1_static_const_fn_ptr_unsafe)(uint8_t, uint8_t);
void (*const T1_static_const_fn_ptr_unsafe2)(uint8_t);
void (*const T1_static_const_fn_ptr_unsafe3)(void);
extern uint8_t (*const T1_static_const_fn_ptr_unsafe)(uint8_t, uint8_t);
extern void (*const T1_static_const_fn_ptr_unsafe2)(uint8_t);
extern void (*const T1_static_const_fn_ptr_unsafe3)(void);

const uint8_t T1_static_right;
extern const uint8_t T1_static_right;
uint8_t (*T1_static_right2)(uint8_t, uint8_t);

// T1_fn_ptr_nested: function pointer to a function, taking a uint8_t, and
Expand All @@ -66,3 +66,19 @@ uint32_t (*(*T1_fn_ptr_s)(uint8_t))(uint16_t);
// uint8_t -> uint8_t, and returning a function pointer to a function taking a
// uint16_t and returning a uint32_t
uint32_t (*(*T1_fn_ptr_s2)(uint8_t(*)(uint8_t), uint16_t(*)(uint16_t)))(uint16_t);

extern const int32_t T1_arr0[2];
extern const int32_t T1_arr1[2][3];
extern const int32_t T1_arr2[1][2][3];

extern int32_t T1_arr3[2];
extern int32_t T1_arr4[2][3];
extern int32_t T1_arr5[1][2][3];

extern int32_t T1_arr42[1][2][3];

struct Q {
uint8_t* q0;
uint8_t** q1;
uint8_t q2;
};
21 changes: 20 additions & 1 deletion testcrate/src/t1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,24 @@ extern "C" {

pub static T1_fn_ptr_s: unsafe extern "C" fn(u8) -> extern fn(u16)->u32;
pub static T1_fn_ptr_s2: unsafe extern "C" fn(extern fn(u8)->u8,
extern fn(u16)->u16) -> extern fn(u16)->u32;
extern fn(u16)->u16)
-> extern fn(u16)->u32;

pub static T1_arr0: [i32; 2];
pub static T1_arr1: [[i32; 3]; 2];
pub static T1_arr2: [[[i32; 3]; 2]; 1];

pub static mut T1_arr3: [i32; 2];
pub static mut T1_arr4: [[i32; 3]; 2];
pub static mut T1_arr5: [[[i32; 3]; 2]; 1];

#[link_name = "T1_arr42"]
pub static mut T1_arr6: [[[i32; 3]; 2]; 1];
}

#[repr(C)]
pub struct Q {
pub q0: *mut u8,
pub q1: *mut *mut u8,
pub q2: u8,
}

0 comments on commit f0fea68

Please sign in to comment.