Skip to content

Commit a56c14c

Browse files
mbyxJohnTitor
authored andcommitted
ctest: automatically skip roundtrip testing for aliases to arrays
1 parent 01cb1e3 commit a56c14c

File tree

7 files changed

+36
-5
lines changed

7 files changed

+36
-5
lines changed

ctest/src/generator.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -930,16 +930,14 @@ impl TestGenerator {
930930
self
931931
}
932932

933-
// FIXME(ctest): should arrays be handled differently?
934-
935933
/// Configures whether the ABI roundtrip tests for a type are emitted.
936934
///
937935
/// The closure is passed the name of a Rust type and returns whether the
938936
/// tests are generated.
939937
///
940938
/// By default all types undergo ABI roundtrip tests. Arrays cannot undergo
941939
/// an ABI roundtrip because they cannot be returned by C functions, and
942-
/// have to be manually skipped here.
940+
/// are automatically skipped.
943941
///
944942
/// # Examples
945943
/// ```no_run

ctest/src/template.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ impl TestTemplate {
257257
helper: &TranslateHelper,
258258
) -> Result<(), TranslationError> {
259259
for alias in helper.filtered_ffi_items.aliases() {
260+
// Arrays cannot be roundtripped, and are automatically skipped.
261+
if let syn::Type::Array(_) = alias.ty {
262+
continue;
263+
}
260264
let c_ty = helper.c_type(alias)?;
261265
self.add_roundtrip_test(helper, alias.ident(), &[], &c_ty, true);
262266
}

ctest/tests/basic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn test_skip_simple() {
101101
let (mut gen_, out_dir) = default_generator(1, Some("simple.h")).unwrap();
102102
gen_.skip_const(|c| c.ident() == "B" || c.ident() == "A")
103103
.skip_c_enum(|e| e == "Color")
104-
.skip_alias(|a| a.ident() == "Byte")
104+
.skip_alias(|a| a.ident() == "Byte" || a.ident() == "gregset_t")
105105
.skip_struct(|s| s.ident() == "Person")
106106
.skip_union(|u| u.ident() == "Word")
107107
.skip_fn(|f| f.ident() == "calloc")

ctest/tests/input/simple.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
typedef uint8_t Byte;
44

5+
typedef unsigned long gregset_t[32];
6+
57
Byte byte = 0x42;
68

79
struct Person

ctest/tests/input/simple.out.with-renames.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ CTEST_EXTERN uint64_t ctest_size_of__Byte(void) { return sizeof(Byte); }
6262
// Return the alignment of a type.
6363
CTEST_EXTERN uint64_t ctest_align_of__Byte(void) { return CTEST_ALIGNOF(Byte); }
6464

65+
// Return the size of a type.
66+
CTEST_EXTERN uint64_t ctest_size_of__gregset_t(void) { return sizeof(gregset_t); }
67+
68+
// Return the alignment of a type.
69+
CTEST_EXTERN uint64_t ctest_align_of__gregset_t(void) { return CTEST_ALIGNOF(gregset_t); }
70+
6571
// Return the size of a type.
6672
CTEST_EXTERN uint64_t ctest_size_of__Color(void) { return sizeof(enum Color); }
6773

ctest/tests/input/simple.out.with-renames.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,23 @@ mod generated_tests {
186186
check_same(rust_align, c_align, "Byte align");
187187
}
188188

189+
/// Compare the size and alignment of the type in Rust and C, making sure they are the same.
190+
pub fn ctest_size_align_gregset_t() {
191+
extern "C" {
192+
fn ctest_size_of__gregset_t() -> u64;
193+
fn ctest_align_of__gregset_t() -> u64;
194+
}
195+
196+
let rust_size = size_of::<gregset_t>() as u64;
197+
let c_size = unsafe { ctest_size_of__gregset_t() };
198+
199+
let rust_align = align_of::<gregset_t>() as u64;
200+
let c_align = unsafe { ctest_align_of__gregset_t() };
201+
202+
check_same(rust_size, c_size, "gregset_t size");
203+
check_same(rust_align, c_align, "gregset_t align");
204+
}
205+
189206
/// Compare the size and alignment of the type in Rust and C, making sure they are the same.
190207
pub fn ctest_size_align_Color() {
191208
extern "C" {
@@ -996,6 +1013,7 @@ fn run_all() {
9961013
ctest_const_BLUE();
9971014
ctest_const_GREEN();
9981015
ctest_size_align_Byte();
1016+
ctest_size_align_gregset_t();
9991017
ctest_size_align_Color();
10001018
ctest_size_align_Person();
10011019
ctest_size_align_Word();

ctest/tests/input/simple.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use std::ffi::{c_char, c_int, c_void};
1+
use std::ffi::{c_char, c_int, c_ulong, c_void};
22

33
pub type Byte = u8;
44

5+
// This should be automatically skipped for roundtripping.
6+
pub type gregset_t = [c_ulong; 32];
7+
58
#[repr(C)]
69
pub struct Person {
710
pub name: *const c_char,

0 commit comments

Comments
 (0)