Skip to content
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
31 changes: 22 additions & 9 deletions ctest/templates/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#if defined(__cplusplus)
#define CTEST_ALIGNOF(T) alignof(T)
#define CTEST_EXTERN extern "C"
#define CTEST_EXTERN extern "C"
#else
#define CTEST_ALIGNOF(T) _Alignof(T)
#define CTEST_EXTERN
Expand Down Expand Up @@ -96,9 +96,15 @@ ctest_field_ptr__{{ item.id }}__{{ item.field.ident() }}({{ item.c_ty }} *b) {
{%- endfor +%}

#ifdef _MSC_VER
// Disable signed/unsigned conversion warnings on MSVC.
// These trigger even if the conversion is explicit.
# pragma warning(disable:4365)
// Disable signed/unsigned conversion warnings on MSVC.
// These trigger even if the conversion is explicit.
#pragma warning(disable:4365)
#endif

#ifdef __GNUC__
// GCC emits a warning with `-Wextra` if we return a typedef to a type marked `volatile`.
// #pragma GCC diagnostic push
// #pragma GCC diagnostic ignored "-Wignored-qualifiers"
#endif

{%- for item in ctx.roundtrip_tests +%}
Expand Down Expand Up @@ -132,14 +138,20 @@ CTEST_EXTERN {{ item.c_ty }} ctest_roundtrip__{{ item.id }}(

{%- endfor +%}

#ifdef __GNUC__
// Pop allow for `-Wignored-qualifiers`
// #pragma GCC diagnostic pop
#endif

#ifdef _MSC_VER
# pragma warning(default:4365)
// Pop allow for 4365
#pragma warning(default:4365)
#endif

#ifdef _MSC_VER
// Disable function pointer type conversion warnings on MSVC.
// The conversion may fail only if we call that function, however we only check its address.
# pragma warning(disable:4191)
// Disable function pointer type conversion warnings on MSVC.
// The conversion may fail only if we call that function, however we only check its address.
#pragma warning(disable:4191)
#endif

{%- for item in ctx.foreign_fn_tests +%}
Expand All @@ -151,7 +163,8 @@ CTEST_EXTERN ctest_void_func ctest_foreign_fn__{{ item.id }}(void) {
{%- endfor +%}

#ifdef _MSC_VER
# pragma warning(default:4191)
// Pop allow for 4191
#pragma warning(default:4191)
#endif

{%- for static_ in ctx.foreign_static_tests +%}
Expand Down
31 changes: 22 additions & 9 deletions ctest/tests/input/hierarchy.out.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#if defined(__cplusplus)
#define CTEST_ALIGNOF(T) alignof(T)
#define CTEST_EXTERN extern "C"
#define CTEST_EXTERN extern "C"
#else
#define CTEST_ALIGNOF(T) _Alignof(T)
#define CTEST_EXTERN
Expand Down Expand Up @@ -38,9 +38,15 @@ CTEST_EXTERN uint32_t ctest_signededness_of__in6_addr(void) {
}

#ifdef _MSC_VER
// Disable signed/unsigned conversion warnings on MSVC.
// These trigger even if the conversion is explicit.
# pragma warning(disable:4365)
// Disable signed/unsigned conversion warnings on MSVC.
// These trigger even if the conversion is explicit.
#pragma warning(disable:4365)
#endif

#ifdef __GNUC__
// GCC emits a warning with `-Wextra` if we return a typedef to a type marked `volatile`.
// #pragma GCC diagnostic push
// #pragma GCC diagnostic ignored "-Wignored-qualifiers"
#endif

// Tests whether the struct/union/alias `x` when passed by value to C and back to Rust
Expand Down Expand Up @@ -70,14 +76,20 @@ CTEST_EXTERN in6_addr ctest_roundtrip__in6_addr(
return value;
}

#ifdef __GNUC__
// Pop allow for `-Wignored-qualifiers`
// #pragma GCC diagnostic pop
#endif

#ifdef _MSC_VER
# pragma warning(default:4365)
// Pop allow for 4365
#pragma warning(default:4365)
#endif

#ifdef _MSC_VER
// Disable function pointer type conversion warnings on MSVC.
// The conversion may fail only if we call that function, however we only check its address.
# pragma warning(disable:4191)
// Disable function pointer type conversion warnings on MSVC.
// The conversion may fail only if we call that function, however we only check its address.
#pragma warning(disable:4191)
#endif

// Return a function pointer.
Expand All @@ -86,7 +98,8 @@ CTEST_EXTERN ctest_void_func ctest_foreign_fn__malloc(void) {
}

#ifdef _MSC_VER
# pragma warning(default:4191)
// Pop allow for 4191
#pragma warning(default:4191)
#endif

// Return a pointer to the static variable content.
Expand Down
31 changes: 22 additions & 9 deletions ctest/tests/input/macro.out.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#if defined(__cplusplus)
#define CTEST_ALIGNOF(T) alignof(T)
#define CTEST_EXTERN extern "C"
#define CTEST_EXTERN extern "C"
#else
#define CTEST_ALIGNOF(T) _Alignof(T)
#define CTEST_EXTERN
Expand Down Expand Up @@ -108,9 +108,15 @@ ctest_field_ptr__VecU16__y(struct VecU16 *b) {
}

#ifdef _MSC_VER
// Disable signed/unsigned conversion warnings on MSVC.
// These trigger even if the conversion is explicit.
# pragma warning(disable:4365)
// Disable signed/unsigned conversion warnings on MSVC.
// These trigger even if the conversion is explicit.
#pragma warning(disable:4365)
#endif

#ifdef __GNUC__
// GCC emits a warning with `-Wextra` if we return a typedef to a type marked `volatile`.
// #pragma GCC diagnostic push
// #pragma GCC diagnostic ignored "-Wignored-qualifiers"
#endif

// Tests whether the struct/union/alias `x` when passed by value to C and back to Rust
Expand Down Expand Up @@ -167,16 +173,23 @@ CTEST_EXTERN struct VecU16 ctest_roundtrip__VecU16(
return value;
}

#ifdef __GNUC__
// Pop allow for `-Wignored-qualifiers`
// #pragma GCC diagnostic pop
#endif

#ifdef _MSC_VER
# pragma warning(default:4365)
// Pop allow for 4365
#pragma warning(default:4365)
#endif

#ifdef _MSC_VER
// Disable function pointer type conversion warnings on MSVC.
// The conversion may fail only if we call that function, however we only check its address.
# pragma warning(disable:4191)
// Disable function pointer type conversion warnings on MSVC.
// The conversion may fail only if we call that function, however we only check its address.
#pragma warning(disable:4191)
#endif

#ifdef _MSC_VER
# pragma warning(default:4191)
// Pop allow for 4191
#pragma warning(default:4191)
#endif
2 changes: 2 additions & 0 deletions ctest/tests/input/simple.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ typedef unsigned long gregset_t[32];

Byte byte = 0x42;

typedef volatile char volatile_char;

enum Color
{
RED,
Expand Down
71 changes: 62 additions & 9 deletions ctest/tests/input/simple.out.with-renames.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#if defined(__cplusplus)
#define CTEST_ALIGNOF(T) alignof(T)
#define CTEST_EXTERN extern "C"
#define CTEST_EXTERN extern "C"
#else
#define CTEST_ALIGNOF(T) _Alignof(T)
#define CTEST_EXTERN
Expand Down Expand Up @@ -62,6 +62,12 @@ CTEST_EXTERN uint64_t ctest_size_of__Byte(void) { return sizeof(Byte); }
// Return the alignment of a type.
CTEST_EXTERN uint64_t ctest_align_of__Byte(void) { return CTEST_ALIGNOF(Byte); }

// Return the size of a type.
CTEST_EXTERN uint64_t ctest_size_of__volatile_char(void) { return sizeof(volatile_char); }

// Return the alignment of a type.
CTEST_EXTERN uint64_t ctest_align_of__volatile_char(void) { return CTEST_ALIGNOF(volatile_char); }

// Return the size of a type.
CTEST_EXTERN uint64_t ctest_size_of__gregset_t(void) { return sizeof(gregset_t); }

Expand Down Expand Up @@ -93,6 +99,13 @@ CTEST_EXTERN uint32_t ctest_signededness_of__Byte(void) {
return all_ones < 0;
}

// Return `1` if the type is signed, otherwise return `0`.
// Casting -1 to the aliased type if signed evaluates to `-1 < 0`, if unsigned to `MAX_VALUE < 0`
CTEST_EXTERN uint32_t ctest_signededness_of__volatile_char(void) {
volatile_char all_ones = (volatile_char) -1;
return all_ones < 0;
}

// Return the offset of a struct/union field.
CTEST_EXTERN uint64_t ctest_offset_of__Person__name(void) {
return offsetof(struct Person, name);
Expand Down Expand Up @@ -208,9 +221,15 @@ ctest_field_ptr__Word__byte(union Word *b) {
}

#ifdef _MSC_VER
// Disable signed/unsigned conversion warnings on MSVC.
// These trigger even if the conversion is explicit.
# pragma warning(disable:4365)
// Disable signed/unsigned conversion warnings on MSVC.
// These trigger even if the conversion is explicit.
#pragma warning(disable:4365)
#endif

#ifdef __GNUC__
// GCC emits a warning with `-Wextra` if we return a typedef to a type marked `volatile`.
// #pragma GCC diagnostic push
// #pragma GCC diagnostic ignored "-Wignored-qualifiers"
#endif

// Tests whether the struct/union/alias `x` when passed by value to C and back to Rust
Expand Down Expand Up @@ -240,6 +259,33 @@ CTEST_EXTERN Byte ctest_roundtrip__Byte(
return value;
}

// Tests whether the struct/union/alias `x` when passed by value to C and back to Rust
// remains unchanged.
// It checks if the size is the same as well as if the padding bytes are all in the correct place.
CTEST_EXTERN volatile_char ctest_roundtrip__volatile_char(
volatile_char value,
const uint8_t is_padding_byte[sizeof(volatile_char)],
uint8_t value_bytes[sizeof(volatile_char)]
) {
int size = (int)sizeof(volatile_char);
// Mark `p` as volatile so that the C compiler does not optimize away the pattern we create.
// Otherwise the Rust side would not be able to see it.
volatile uint8_t* p = (volatile uint8_t*)&value;
int i = 0;
for (i = 0; i < size; ++i) {
// We skip padding bytes in both Rust and C because writing to it is undefined.
// Instead we just make sure the the placement of the padding bytes remains the same.
if (is_padding_byte[i]) { continue; }
value_bytes[i] = p[i];
// After we check that the pattern remained unchanged from Rust to C, we invert the pattern
// and send it back to Rust to make sure that it remains unchanged from C to Rust.
uint8_t d = (uint8_t)(255) - (uint8_t)(i % 256);
d = d == 0 ? 42: d;
p[i] = d;
}
return value;
}

// Tests whether the struct/union/alias `x` when passed by value to C and back to Rust
// remains unchanged.
// It checks if the size is the same as well as if the padding bytes are all in the correct place.
Expand Down Expand Up @@ -321,14 +367,20 @@ CTEST_EXTERN union Word ctest_roundtrip__Word(
return value;
}

#ifdef __GNUC__
// Pop allow for `-Wignored-qualifiers`
// #pragma GCC diagnostic pop
#endif

#ifdef _MSC_VER
# pragma warning(default:4365)
// Pop allow for 4365
#pragma warning(default:4365)
#endif

#ifdef _MSC_VER
// Disable function pointer type conversion warnings on MSVC.
// The conversion may fail only if we call that function, however we only check its address.
# pragma warning(disable:4191)
// Disable function pointer type conversion warnings on MSVC.
// The conversion may fail only if we call that function, however we only check its address.
#pragma warning(disable:4191)
#endif

// Return a function pointer.
Expand All @@ -337,7 +389,8 @@ CTEST_EXTERN ctest_void_func ctest_foreign_fn__calloc(void) {
}

#ifdef _MSC_VER
# pragma warning(default:4191)
// Pop allow for 4191
#pragma warning(default:4191)
#endif

// Return a pointer to the static variable content.
Expand Down
Loading
Loading