From c1c3d8b4a9cf9d090ad3e24ea8dc70f0a7870490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=89OUX?= <romain.deoux@proton.me> Date: Tue, 13 Aug 2024 20:59:47 +0200 Subject: [PATCH 1/8] build: set the `-Werror` compile option and fix a warning --- meson.build | 1 + src/capi/stream.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index ef3ac903..1f96feec 100644 --- a/meson.build +++ b/meson.build @@ -44,6 +44,7 @@ add_project_arguments( cc.get_supported_arguments([ '-Wno-unused-parameter', '-Wno-unused-value', + '-Werror', '-fvisibility=hidden', '-fexceptions', diff --git a/src/capi/stream.c b/src/capi/stream.c index f3fcccdd..54a03fff 100644 --- a/src/capi/stream.c +++ b/src/capi/stream.c @@ -97,7 +97,7 @@ char *cr_user_stream_tostr(const struct cr_stream *m) void cr_stream_init(struct cr_stream *s) { - s->cri_data = calloc(sizeof (*s->cri_data), 1); + s->cri_data = calloc(1, sizeof (*s->cri_data)); s->cri_data->ref = 1; } From db9f823f42ecea6c72e03934d6db3826090eb14e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=89OUX?= <romain.deoux@proton.me> Date: Tue, 13 Aug 2024 21:49:08 +0200 Subject: [PATCH 2/8] test: add `const` specifiers for custom `dummy_struct` type --- samples/asserts.c | 4 ++-- test/full/failmessages.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/samples/asserts.c b/samples/asserts.c index 78d39a08..94ab9b06 100644 --- a/samples/asserts.c +++ b/samples/asserts.c @@ -83,12 +83,12 @@ struct dummy_struct { }; /* We need to provide basic functions for our dummy struct */ -int cr_user_dummy_struct_eq(struct dummy_struct *a, struct dummy_struct *b) +int cr_user_dummy_struct_eq(const struct dummy_struct *a, const struct dummy_struct *b) { return a->a == b->a && a->b == b->b; } -char *cr_user_dummy_struct_tostr(struct dummy_struct *d) +char *cr_user_dummy_struct_tostr(const struct dummy_struct *d) { char *out; diff --git a/test/full/failmessages.c b/test/full/failmessages.c index 3b1150f4..6e1beb9d 100644 --- a/test/full/failmessages.c +++ b/test/full/failmessages.c @@ -8,17 +8,17 @@ struct dummy_struct { }; /* We need to provide basic functions for our dummy struct */ -int cr_user_dummy_struct_eq(struct dummy_struct *a, struct dummy_struct *b) +int cr_user_dummy_struct_eq(const struct dummy_struct *a, const struct dummy_struct *b) { return a->a == b->a && a->b == b->b; } -int cr_user_dummy_struct_zero(struct dummy_struct *a) +int cr_user_dummy_struct_zero(const struct dummy_struct *a) { return !a->a && !a->b; } -char *cr_user_dummy_struct_tostr(struct dummy_struct *d) +char *cr_user_dummy_struct_tostr(const struct dummy_struct *d) { char *out; From ed07791f127b8360160a639f6cffe3fbcdfce5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=89OUX?= <romain.deoux@proton.me> Date: Tue, 13 Aug 2024 21:51:00 +0200 Subject: [PATCH 3/8] add const specifiers for tostr() on iptr & uptr tags --- include/criterion/internal/assert/tag.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/criterion/internal/assert/tag.h b/include/criterion/internal/assert/tag.h index 27bf879d..22472642 100644 --- a/include/criterion/internal/assert/tag.h +++ b/include/criterion/internal/assert/tag.h @@ -287,7 +287,7 @@ CRI_ASSERT_DECLARE_NATIVE_CMP_FN(iptr) CRI_ASSERT_DECLARE_NATIVE_CMP_FN(uptr) CRI_ASSERT_DECLARE_NATIVE_CMP_FN(ptr) -static inline char *CRI_USER_TAG_ID(tostr, iptr)(intptr_t *e) +static inline char *CRI_USER_TAG_ID(tostr, iptr)(const intptr_t *e) { uintptr_t absptr = (uintptr_t) *e; if (absptr > (uintptr_t)INTPTR_MAX) @@ -298,7 +298,7 @@ static inline char *CRI_USER_TAG_ID(tostr, iptr)(intptr_t *e) return str; } -static inline char *CRI_USER_TAG_ID(tostr, uptr)(uintptr_t *e) +static inline char *CRI_USER_TAG_ID(tostr, uptr)(const uintptr_t *e) { char *str = NULL; cr_asprintf(&str, "0x%" CRI_PRIxPTR, *e); From 87bd2e168b5793e433f9976395a0710c9c0d4fc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=89OUX?= <romain.deoux@proton.me> Date: Tue, 13 Aug 2024 21:57:44 +0200 Subject: [PATCH 4/8] add `const` specifiers to functions for native types --- include/criterion/internal/assert/tag.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/criterion/internal/assert/tag.h b/include/criterion/internal/assert/tag.h index 22472642..4b5769df 100644 --- a/include/criterion/internal/assert/tag.h +++ b/include/criterion/internal/assert/tag.h @@ -195,19 +195,19 @@ #define CRI_ASSERT_DECLARE_NATIVE_CMP_FN(Tag) \ static inline int CRI_USER_TAG_ID(lt, Tag)( \ - CRI_ASSERT_TYPE_TAG(Tag) *actual, \ - CRI_ASSERT_TYPE_TAG(Tag) *expected) \ + CRI_ASSERT_TYPE_TAG(Tag) const *actual, \ + CRI_ASSERT_TYPE_TAG(Tag) const *expected) \ { \ return *actual < *expected; \ } \ static inline int CRI_USER_TAG_ID(eq, Tag)( \ - CRI_ASSERT_TYPE_TAG(Tag) *actual, \ - CRI_ASSERT_TYPE_TAG(Tag) *expected) \ + CRI_ASSERT_TYPE_TAG(Tag) const *actual, \ + CRI_ASSERT_TYPE_TAG(Tag) const *expected) \ { \ return *actual == *expected; \ } \ static inline int CRI_USER_TAG_ID(zero, Tag)( \ - CRI_ASSERT_TYPE_TAG(Tag) *val) \ + CRI_ASSERT_TYPE_TAG(Tag) const *val) \ { \ return !*val; \ } @@ -215,7 +215,7 @@ #define CRI_ASSERT_DECLARE_NATIVE_FN(Tag, Fmt) \ CRI_ASSERT_DECLARE_NATIVE_CMP_FN(Tag) \ static inline char *CRI_USER_TAG_ID(tostr, Tag)( \ - CRI_ASSERT_TYPE_TAG(Tag) *e) \ + CRI_ASSERT_TYPE_TAG(Tag) const *e) \ { \ char *str = NULL; \ cr_asprintf(&str, "%" Fmt, *e); \ From c50612e48082a40665ea484effcf89582fac7ee6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=89OUX?= <romain.deoux@proton.me> Date: Tue, 13 Aug 2024 21:59:28 +0200 Subject: [PATCH 5/8] fix: comparison on constant sized arrays --- include/criterion/internal/assert/op.h | 2 +- samples/asserts.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/criterion/internal/assert/op.h b/include/criterion/internal/assert/op.h index 98271bcb..470ccebb 100644 --- a/include/criterion/internal/assert/op.h +++ b/include/criterion/internal/assert/op.h @@ -205,7 +205,7 @@ #define CRI_ASSERT_OP_TYPE_TAGGED(Type, Var, Name, MkNode, Val) Type #define CRI_ASSERT_OP_TYPE_SINGLE(Type, Var, Name, MkNode, Val) Type -#define CRI_ASSERT_OP_ARRTYPE_TAGGED(Type, Var, Name, MkNode, Val) Type * +#define CRI_ASSERT_OP_ARRTYPE_TAGGED(Type, Var, Name, MkNode, Val) Type const * #define CRI_ASSERT_OP_ARRTYPE_SINGLE(Type, Var, Name, MkNode, Val) Type #define CRI_ASSERT_OPKIND(Kind, Type, Var, Name, MkNode, Val) Kind diff --git a/samples/asserts.c b/samples/asserts.c index 94ab9b06..9d6b6265 100644 --- a/samples/asserts.c +++ b/samples/asserts.c @@ -97,8 +97,8 @@ char *cr_user_dummy_struct_tostr(const struct dummy_struct *d) } Test(asserts, array) { - int arr1[] = { 1, 2, 3, 4 }; - int arr2[] = { 4, 3, 2, 1 }; + static const int arr1[] = { 1, 2, 3, 4 }; + static const int arr2[] = { 4, 3, 2, 1 }; /* For primitive types we can compare their byte-to-byte representation */ struct cr_mem mem_arr1 = { .data = arr1, .size = 4 * sizeof (int) }; From 9b8425bb5655a83edb8ec6e8b4d11af914f0e6f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=89OUX?= <romain.deoux@proton.me> Date: Tue, 13 Aug 2024 22:45:21 +0200 Subject: [PATCH 6/8] add `const` specifiers for operations on streams --- include/criterion/new/stream.h | 4 ++-- src/capi/stream.c | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/criterion/new/stream.h b/include/criterion/new/stream.h index c5ae0a11..52905248 100644 --- a/include/criterion/new/stream.h +++ b/include/criterion/new/stream.h @@ -74,8 +74,8 @@ CR_BEGIN_C_API CR_API void cr_stream_init(struct cr_stream *s); CR_API void cr_stream_close(struct cr_stream *s); -CR_API int cr_user_stream_eq(struct cr_stream *s1, struct cr_stream *s2); -CR_API int cr_user_stream_lt(struct cr_stream *s1, struct cr_stream *s2); +CR_API int cr_user_stream_eq(const struct cr_stream *s1, const struct cr_stream *s2); +CR_API int cr_user_stream_lt(const struct cr_stream *s1, const struct cr_stream *s2); CR_API char *cr_user_stream_tostr(const struct cr_stream *s); CR_END_C_API diff --git a/src/capi/stream.c b/src/capi/stream.c index 54a03fff..c40cc95b 100644 --- a/src/capi/stream.c +++ b/src/capi/stream.c @@ -27,7 +27,8 @@ #include "criterion/new/stream.h" #include "string/xxd.h" -static int streams_eval_cmp(struct cr_stream *m1, struct cr_stream *m2) +static int streams_eval_cmp(const struct cr_stream *m1, + const struct cr_stream *m2) { /* It doesn't make sense to compare consumed streams, but it still happens in some case within an assertion. It's best to provide @@ -80,12 +81,12 @@ static int streams_eval_cmp(struct cr_stream *m1, struct cr_stream *m2) return cmp; } -int cr_user_stream_eq(struct cr_stream *m1, struct cr_stream *m2) +int cr_user_stream_eq(const struct cr_stream *m1, const struct cr_stream *m2) { return !streams_eval_cmp(m1, m2); } -int cr_user_stream_lt(struct cr_stream *m1, struct cr_stream *m2) +int cr_user_stream_lt(const struct cr_stream *m1, const struct cr_stream *m2) { return streams_eval_cmp(m1, m2) < 0; } From e3cad1a7328fe4ebba5d23cb3f1b37fefb44c444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=89OUX?= <romain.deoux@proton.me> Date: Sun, 18 Aug 2024 11:18:55 +0200 Subject: [PATCH 7/8] fix: allow asserting const strings --- include/criterion/internal/assert/tag.h | 28 +++++++++++++++---------- src/capi/specifiers.c | 17 ++++++++------- test/full/bug521.c | 14 +++++++++++++ test/full/meson.build | 1 + 4 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 test/full/bug521.c diff --git a/include/criterion/internal/assert/tag.h b/include/criterion/internal/assert/tag.h index 4b5769df..73eb2af7 100644 --- a/include/criterion/internal/assert/tag.h +++ b/include/criterion/internal/assert/tag.h @@ -36,6 +36,8 @@ #define CRI_ASSERT_TYPE_TAG(Tag) CR_EXPAND(CRI_ASSERT_TYPE_TAG_(Tag)) #define CRI_ASSERT_TYPE_TAG_(Tag) CRI_IF_DEFINED_NODEFER(CRI_ASSERT_TEST_TAG_ ## Tag, CR_VA_HEAD(CRI_ASSERT_TYPE_TAG_ ## Tag), , CRI_ASSERT_TYPE_TAG_UNKNOWN, (Tag)) +#define CRI_ASSERT_MUT_TYPE_TAG(Tag) CR_EXPAND(CRI_ASSERT_MUT_TYPE_TAG_(Tag)) +#define CRI_ASSERT_MUT_TYPE_TAG_(Tag) CRI_IF_DEFINED_NODEFER(CRI_ASSERT_TEST_TAG_ ## Tag, CR_VA_HEAD(CRI_ASSERT_MUT_TYPE_TAG_ ## Tag), , CRI_ASSERT_TYPE_TAG_UNKNOWN, (Tag)) #define CRI_USER_TAG_ID(Id, Tag) CR_CONCAT(CR_CONCAT(cr_user_, CRI_ASSERT_TYPE_TAG_ID(Tag)), _ ## Id) @@ -135,7 +137,8 @@ #ifdef __cplusplus # define CRI_ASSERT_TYPE_TAG_str std::string, #else -# define CRI_ASSERT_TYPE_TAG_str char *, +# define CRI_ASSERT_TYPE_TAG_str const char *, +# define CRI_ASSERT_MUT_TYPE_TAG_str char *, #endif #define CRI_ASSERT_TYPE_TAG_ID_str str, @@ -144,7 +147,8 @@ #ifdef __cplusplus # define CRI_ASSERT_TYPE_TAG_wcs std::wstring, #else -# define CRI_ASSERT_TYPE_TAG_wcs wchar_t *, +# define CRI_ASSERT_TYPE_TAG_wcs const wchar_t *, +# define CRI_ASSERT_MUT_TYPE_TAG_wcs wchar_t *, #endif #define CRI_ASSERT_TYPE_TAG_ID_wcs wcs, @@ -222,15 +226,17 @@ return str; \ } -#define CRI_ASSERT_DECLARE_STR_FN(Tag, Prefix, Fmt) \ - CR_API int CRI_USER_TAG_ID(lt, Tag)( \ - CRI_ASSERT_TYPE_TAG(Tag) *actual, \ - CRI_ASSERT_TYPE_TAG(Tag) *expected); \ - CR_API int CRI_USER_TAG_ID(eq, Tag)( \ - CRI_ASSERT_TYPE_TAG(Tag) *actual, \ - CRI_ASSERT_TYPE_TAG(Tag) *expected); \ - CR_API int CRI_USER_TAG_ID(zero, Tag)(CRI_ASSERT_TYPE_TAG(Tag) *val); \ - CR_API char *CRI_USER_TAG_ID(tostr, Tag)(CRI_ASSERT_TYPE_TAG(Tag) *e); +#define CRI_ASSERT_DECLARE_STR_FN(Tag, Prefix, Fmt) \ + CR_API int CRI_USER_TAG_ID(lt, Tag)( \ + CRI_ASSERT_TYPE_TAG(Tag) const *actual, \ + CRI_ASSERT_TYPE_TAG(Tag) const *expected); \ + CR_API int CRI_USER_TAG_ID(eq, Tag)( \ + CRI_ASSERT_TYPE_TAG(Tag) const *actual, \ + CRI_ASSERT_TYPE_TAG(Tag) const *expected); \ + CR_API int CRI_USER_TAG_ID(zero, Tag)( \ + CRI_ASSERT_TYPE_TAG(Tag) const *val); \ + CR_API char *CRI_USER_TAG_ID(tostr, Tag)( \ + CRI_ASSERT_TYPE_TAG(Tag) const *e); #ifndef __cplusplus # include <string.h> diff --git a/src/capi/specifiers.c b/src/capi/specifiers.c index deb14bf9..5bdaa2e8 100644 --- a/src/capi/specifiers.c +++ b/src/capi/specifiers.c @@ -33,25 +33,26 @@ /* *INDENT-OFF* */ #define CRI_ASSERT_DEFINE_STR_FN(Tag, Prefix, Fmt) \ int CRI_USER_TAG_ID(lt, Tag)( \ - CRI_ASSERT_TYPE_TAG(Tag) *actual, \ - CRI_ASSERT_TYPE_TAG(Tag) *expected) \ + CRI_ASSERT_TYPE_TAG(Tag) const *actual, \ + CRI_ASSERT_TYPE_TAG(Tag) const *expected) \ { \ return Tag ## cmp(*actual, *expected) < 0; \ } \ int CRI_USER_TAG_ID(eq, Tag)( \ - CRI_ASSERT_TYPE_TAG(Tag) *actual, \ - CRI_ASSERT_TYPE_TAG(Tag) *expected) \ + CRI_ASSERT_TYPE_TAG(Tag) const *actual, \ + CRI_ASSERT_TYPE_TAG(Tag) const *expected) \ { \ return !Tag ## cmp(*actual, *expected); \ } \ - int CRI_USER_TAG_ID(zero, Tag)(CRI_ASSERT_TYPE_TAG(Tag) *val) \ + int CRI_USER_TAG_ID(zero, Tag)(CRI_ASSERT_TYPE_TAG(Tag) const *val) \ { \ return !Tag ## len(*val); \ } \ - char *CRI_USER_TAG_ID(tostr, Tag)(CRI_ASSERT_TYPE_TAG(Tag) *e) \ + char *CRI_USER_TAG_ID(tostr, Tag)(CRI_ASSERT_TYPE_TAG(Tag) const *e) \ { \ - CRI_ASSERT_TYPE_TAG(Tag) cri_dup = Tag ## dup(*e); \ - CRI_ASSERT_TYPE_TAG(Tag) cri_line = cri_ ## Tag ## tokc(cri_dup, '\n'); \ + CRI_ASSERT_MUT_TYPE_TAG(Tag) cri_dup = Tag ## dup(*e); \ + CRI_ASSERT_MUT_TYPE_TAG(Tag) cri_line; \ + cri_line = cri_ ## Tag ## tokc(cri_dup, '\n'); \ \ char *cri_str = NULL; \ size_t cri_off = 0; \ diff --git a/test/full/bug521.c b/test/full/bug521.c new file mode 100644 index 00000000..a5cefeae --- /dev/null +++ b/test/full/bug521.c @@ -0,0 +1,14 @@ +#include <criterion/criterion.h> +#include <criterion/new/assert.h> + +Test(bug521, str) { + static const char *foo = "foo"; + cr_assert(eq(str, foo, "foo")); + cr_assert(ne(str, foo, "oof")); +} + +Test(bug521, wcs) { + static const wchar_t *foo = L"foo"; + cr_assert(eq(wcs, foo, L"foo")); + cr_assert(ne(wcs, foo, L"oof")); +} \ No newline at end of file diff --git a/test/full/meson.build b/test/full/meson.build index d64015f3..34702c2b 100644 --- a/test/full/meson.build +++ b/test/full/meson.build @@ -8,6 +8,7 @@ full_tests = [ # bug-specific programs 'bug463.c', + 'bug521.c', ] if get_option('theories').enabled() From c13c376a5cf9cee70231ffb2fb78dcc8ddf99c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=89OUX?= <romain.deoux@proton.me> Date: Sun, 18 Aug 2024 11:28:13 +0200 Subject: [PATCH 8/8] fix: allow asserting pointers to const data --- include/criterion/internal/assert/tag.h | 4 ++-- test/full/bug521.c | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/criterion/internal/assert/tag.h b/include/criterion/internal/assert/tag.h index 73eb2af7..2535a4b9 100644 --- a/include/criterion/internal/assert/tag.h +++ b/include/criterion/internal/assert/tag.h @@ -129,7 +129,7 @@ #define CRI_ASSERT_TEST_TAG_ptr , #define CRI_ASSERT_TEST_TAGC_ptr() , -#define CRI_ASSERT_TYPE_TAG_ptr void *, +#define CRI_ASSERT_TYPE_TAG_ptr const void *, #define CRI_ASSERT_TYPE_TAG_ID_ptr ptr, #define CRI_ASSERT_TEST_TAG_str , @@ -311,7 +311,7 @@ static inline char *CRI_USER_TAG_ID(tostr, uptr)(const uintptr_t *e) return str; } -static inline char *CRI_USER_TAG_ID(tostr, ptr)(void **e) +static inline char *CRI_USER_TAG_ID(tostr, ptr)(const void **e) { char *str = NULL; cr_asprintf(&str, "0x%" CRI_PRIxPTR, (uintptr_t) *e); diff --git a/test/full/bug521.c b/test/full/bug521.c index a5cefeae..6a360d2a 100644 --- a/test/full/bug521.c +++ b/test/full/bug521.c @@ -2,13 +2,18 @@ #include <criterion/new/assert.h> Test(bug521, str) { - static const char *foo = "foo"; + static const char *const foo = "foo"; cr_assert(eq(str, foo, "foo")); cr_assert(ne(str, foo, "oof")); } Test(bug521, wcs) { - static const wchar_t *foo = L"foo"; + static const wchar_t *const foo = L"foo"; cr_assert(eq(wcs, foo, L"foo")); cr_assert(ne(wcs, foo, L"oof")); +} + +Test(bug521, ptr) { + static const char *const foo = "foo"; + cr_assert(eq(ptr, foo, foo)); } \ No newline at end of file