tests: Add tests for attribute annotations.

This commit is contained in:
Emilio Cobos Álvarez 2020-04-22 01:26:37 +02:00
parent 5ee5b3ee88
commit a05a223704
18 changed files with 330 additions and 13 deletions

View File

@ -1,4 +1,5 @@
#define MY_ASSERT(...) do { } while (0)
#define MY_ATTRS __attribute((noinline))
#include <stdarg.h>

View File

@ -1,4 +1,5 @@
#define MY_ASSERT(...) do { } while (0)
#define MY_ATTRS __attribute((noinline))
#include <stdarg.h>

View File

@ -1,4 +1,5 @@
#define MY_ASSERT(...) do { } while (0)
#define MY_ATTRS __attribute((noinline))
#include <cstdarg>
@ -46,7 +47,7 @@ struct H {
return foo._0;
}
int16_t& AsH_Foo() {
MY_ATTRS int16_t& AsH_Foo() {
MY_ASSERT(IsH_Foo());
return foo._0;
}
@ -64,7 +65,7 @@ struct H {
return tag == Tag::H_Bar;
}
const H_Bar_Body& AsH_Bar() const {
MY_ATTRS const H_Bar_Body& AsH_Bar() const {
MY_ASSERT(IsH_Bar());
return bar;
}
@ -80,7 +81,7 @@ struct H {
return result;
}
bool IsH_Baz() const {
MY_ATTRS bool IsH_Baz() const {
return tag == Tag::H_Baz;
}
};

View File

@ -1,4 +1,5 @@
#define MY_ASSERT(...) do { } while (0)
#define MY_ATTRS __attribute((noinline))
#include <stdarg.h>

View File

@ -1,4 +1,5 @@
#define MY_ASSERT(...) do { } while (0)
#define MY_ATTRS __attribute((noinline))
#include <stdarg.h>

View File

@ -1,3 +1,7 @@
#define NOINLINE __attribute__((noinline))
#define NODISCARD [[nodiscard]]
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
@ -203,9 +207,32 @@ typedef union Tazzzz {
Taz7_Body taz7;
} Tazzzz;
enum Qux_Tag {
Qux1,
Qux2,
};
typedef uint8_t Qux_Tag;
typedef struct Qux1_Body {
Qux_Tag tag;
int32_t _0;
} Qux1_Body;
typedef struct Qux2_Body {
Qux_Tag tag;
uint32_t _0;
} Qux2_Body;
typedef union Qux {
Qux_Tag tag;
Qux1_Body qux1;
Qux2_Body qux2;
} Qux;
void root(const Foo_u32 *a,
const Baz_i32 *b,
const Taz *c,
Tazz d,
const Tazzz *e,
const Tazzzz *f);
const Tazzzz *f,
const Qux *g);

View File

@ -1,3 +1,7 @@
#define NOINLINE __attribute__((noinline))
#define NODISCARD [[nodiscard]]
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
@ -245,6 +249,34 @@ typedef union Tazzzz {
Taz7_Body taz7;
} Tazzzz;
enum Qux_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
Qux1,
Qux2,
};
#ifndef __cplusplus
typedef uint8_t Qux_Tag;
#endif // __cplusplus
typedef struct Qux1_Body {
Qux_Tag tag;
int32_t _0;
} Qux1_Body;
typedef struct Qux2_Body {
Qux_Tag tag;
uint32_t _0;
} Qux2_Body;
typedef union Qux {
Qux_Tag tag;
Qux1_Body qux1;
Qux2_Body qux2;
} Qux;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@ -254,7 +286,8 @@ void root(const Foo_u32 *a,
const Taz *c,
Tazz d,
const Tazzz *e,
const Tazzzz *f);
const Tazzzz *f,
const Qux *g);
#ifdef __cplusplus
} // extern "C"

View File

@ -1,3 +1,7 @@
#define NOINLINE __attribute__((noinline))
#define NODISCARD [[nodiscard]]
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
@ -203,9 +207,32 @@ typedef union {
Taz7_Body taz7;
} Tazzzz;
enum Qux_Tag {
Qux1,
Qux2,
};
typedef uint8_t Qux_Tag;
typedef struct {
Qux_Tag tag;
int32_t _0;
} Qux1_Body;
typedef struct {
Qux_Tag tag;
uint32_t _0;
} Qux2_Body;
typedef union {
Qux_Tag tag;
Qux1_Body qux1;
Qux2_Body qux2;
} Qux;
void root(const Foo_u32 *a,
const Baz_i32 *b,
const Taz *c,
Tazz d,
const Tazzz *e,
const Tazzzz *f);
const Tazzzz *f,
const Qux *g);

View File

@ -1,3 +1,7 @@
#define NOINLINE __attribute__((noinline))
#define NODISCARD [[nodiscard]]
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
@ -245,6 +249,34 @@ typedef union {
Taz7_Body taz7;
} Tazzzz;
enum Qux_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
Qux1,
Qux2,
};
#ifndef __cplusplus
typedef uint8_t Qux_Tag;
#endif // __cplusplus
typedef struct {
Qux_Tag tag;
int32_t _0;
} Qux1_Body;
typedef struct {
Qux_Tag tag;
uint32_t _0;
} Qux2_Body;
typedef union {
Qux_Tag tag;
Qux1_Body qux1;
Qux2_Body qux2;
} Qux;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@ -254,7 +286,8 @@ void root(const Foo_u32 *a,
const Taz *c,
Tazz d,
const Tazzz *e,
const Tazzzz *f);
const Tazzzz *f,
const Qux *g);
#ifdef __cplusplus
} // extern "C"

View File

@ -1,3 +1,7 @@
#define NOINLINE __attribute__((noinline))
#define NODISCARD [[nodiscard]]
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
@ -594,6 +598,106 @@ union Tazzzz {
}
};
union Qux {
enum class Tag : uint8_t {
Qux1,
Qux2,
};
struct Qux1_Body {
Tag tag;
int32_t _0;
bool operator==(const Qux1_Body& other) const {
return _0 == other._0;
}
};
struct Qux2_Body {
Tag tag;
uint32_t _0;
bool operator==(const Qux2_Body& other) const {
return _0 == other._0;
}
};
struct {
Tag tag;
};
Qux1_Body qux1;
Qux2_Body qux2;
static Qux Qux1(const int32_t &a0) {
Qux result;
::new (&result.qux1._0) (int32_t)(a0);
result.tag = Tag::Qux1;
return result;
}
bool IsQux1() const {
return tag == Tag::Qux1;
}
static Qux Qux2(const uint32_t &a0) {
Qux result;
::new (&result.qux2._0) (uint32_t)(a0);
result.tag = Tag::Qux2;
return result;
}
bool IsQux2() const {
return tag == Tag::Qux2;
}
NODISCARD bool operator==(const Qux& other) const {
if (tag != other.tag) {
return false;
}
switch (tag) {
case Tag::Qux1: return qux1 == other.qux1;
case Tag::Qux2: return qux2 == other.qux2;
}
return true;
}
NODISCARD bool operator!=(const Qux& other) const {
return !(*this == other);
}
private:
Qux() {
}
public:
NOINLINE ~Qux() {
switch (tag) {
case Tag::Qux1: qux1.~Qux1_Body(); break;
case Tag::Qux2: qux2.~Qux2_Body(); break;
}
}
NOINLINE Qux(const Qux& other)
: tag(other.tag) {
switch (tag) {
case Tag::Qux1: ::new (&qux1) (Qux1_Body)(other.qux1); break;
case Tag::Qux2: ::new (&qux2) (Qux2_Body)(other.qux2); break;
}
}
NOINLINE Qux& operator=(const Qux& other) {
if (this != &other) {
this->~Qux();
new (this) Qux(other);
}
return *this;
}
};
extern "C" {
void root(const Foo<uint32_t> *a,
@ -601,6 +705,7 @@ void root(const Foo<uint32_t> *a,
const Taz *c,
Tazz d,
const Tazzz *e,
const Tazzzz *f);
const Tazzzz *f,
const Qux *g);
} // extern "C"

View File

@ -1,4 +1,5 @@
#define MY_ASSERT(...) do { } while (0)
#define MY_ATTRS __attribute((noinline))
#include <stdarg.h>

View File

@ -1,4 +1,5 @@
#define MY_ASSERT(...) do { } while (0)
#define MY_ATTRS __attribute((noinline))
#include <stdarg.h>

View File

@ -1,3 +1,7 @@
#define NOINLINE __attribute__((noinline))
#define NODISCARD [[nodiscard]]
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
@ -203,9 +207,32 @@ union Tazzzz {
struct Taz7_Body taz7;
};
enum Qux_Tag {
Qux1,
Qux2,
};
typedef uint8_t Qux_Tag;
struct Qux1_Body {
Qux_Tag tag;
int32_t _0;
};
struct Qux2_Body {
Qux_Tag tag;
uint32_t _0;
};
union Qux {
Qux_Tag tag;
struct Qux1_Body qux1;
struct Qux2_Body qux2;
};
void root(const struct Foo_u32 *a,
const union Baz_i32 *b,
const union Taz *c,
union Tazz d,
const union Tazzz *e,
const union Tazzzz *f);
const union Tazzzz *f,
const union Qux *g);

View File

@ -1,3 +1,7 @@
#define NOINLINE __attribute__((noinline))
#define NODISCARD [[nodiscard]]
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
@ -245,6 +249,34 @@ union Tazzzz {
struct Taz7_Body taz7;
};
enum Qux_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
Qux1,
Qux2,
};
#ifndef __cplusplus
typedef uint8_t Qux_Tag;
#endif // __cplusplus
struct Qux1_Body {
Qux_Tag tag;
int32_t _0;
};
struct Qux2_Body {
Qux_Tag tag;
uint32_t _0;
};
union Qux {
Qux_Tag tag;
struct Qux1_Body qux1;
struct Qux2_Body qux2;
};
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@ -254,7 +286,8 @@ void root(const struct Foo_u32 *a,
const union Taz *c,
union Tazz d,
const union Tazzz *e,
const union Tazzzz *f);
const union Tazzzz *f,
const union Qux *g);
#ifdef __cplusplus
} // extern "C"

View File

@ -1,15 +1,20 @@
/// cbindgen:prefix-with-name
#[repr(C, u8)]
pub enum H {
/// cbindgen:variant-mut-cast-attributes=MY_ATTRS
Foo(i16),
/// cbindgen:variant-const-cast-attributes=MY_ATTRS
Bar { x: u8, y: i16 },
/// cbindgen:variant-is-attributes=MY_ATTRS
Baz
}
/// cbindgen:prefix-with-name
#[repr(C, u8, u16)]
pub enum I {
/// cbindgen:variant-constructor-attributes=MY_ATTRS
Foo(i16),
/// cbindgen:eq-attributes=MY_ATTRS
Bar { x: u8, y: i16 },
Baz
}

View File

@ -1,6 +1,6 @@
# This is a bit of an abuse of the warning config...
autogen_warning = """
header = """
#define MY_ASSERT(...) do { } while (0)
#define MY_ATTRS __attribute((noinline))
"""
[enum]

View File

@ -77,6 +77,21 @@ pub enum Tazzzz {
Taz7(u32),
}
/// cbindgen:derive-eq=true
/// cbindgen:derive-neq=true
/// cbindgen:neq-attributes=NODISCARD
/// cbindgen:eq-attributes=NODISCARD
/// cbindgen:destructor-attributes=NOINLINE
/// cbindgen:copy-constructor-attributes=NOINLINE
/// cbindgen:copy-assignment-attributes=NOINLINE
#[repr(u8)]
pub enum Qux {
/// cbindgen:derive-eq=true
Qux1(i32),
/// cbindgen:derive-eq=true
Qux2(u32),
}
#[no_mangle]
pub extern "C" fn root(a: &Foo<u32>, b: &Baz<i32>, c: &Taz, d: Tazz, e: &Tazzz, f: &Tazzzz) {}
pub extern "C" fn root(a: &Foo<u32>, b: &Baz<i32>, c: &Taz, d: Tazz, e: &Tazzz, f: &Tazzzz, g: &Qux) {}

View File

@ -1,3 +1,8 @@
header = """
#define NOINLINE __attribute__((noinline))
#define NODISCARD [[nodiscard]]
"""
[enum]
derive_tagged_enum_destructor = true
derive_tagged_enum_copy_constructor = true