Skip to content

Commit

Permalink
Fix a null compare against an opaque struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
sheredom committed Apr 8, 2024
1 parent bc61349 commit 938858a
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 10 deletions.
11 changes: 11 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,14 @@ UTEST(c, Near) {
}

UTEST(c, Todo) { UTEST_SKIP("Not yet implemented!"); }

// TODO: TCC cannot handle comparing null opaque structs at all.
#if !defined(__TINYC__)
struct Opaque;

UTEST(c, Opaque) {
struct Opaque *opaque = NULL;
EXPECT_EQ(NULL, opaque);
ASSERT_EQ(NULL, opaque);
}
#endif
8 changes: 8 additions & 0 deletions test/test11.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,11 @@ UTEST(c11, Near) {
}

UTEST(c11, Todo) { UTEST_SKIP("Not yet implemented!"); }

struct Opaque;

UTEST(c11, Opaque) {
struct Opaque *opaque = NULL;
EXPECT_EQ(NULL, opaque);
ASSERT_EQ(NULL, opaque);
}
17 changes: 17 additions & 0 deletions test/test11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,20 @@ UTEST(cpp11, Null) {
}

#endif

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#endif

struct Opaque;

UTEST(cpp11, Opaque) {
struct Opaque *opaque = nullptr;
EXPECT_EQ(nullptr, opaque);
ASSERT_EQ(nullptr, opaque);
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif
17 changes: 17 additions & 0 deletions test/test14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,20 @@ UTEST(cpp14, Null) {
}

#endif

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#endif

struct Opaque;

UTEST(cpp14, Opaque) {
struct Opaque *opaque = nullptr;
EXPECT_EQ(nullptr, opaque);
ASSERT_EQ(nullptr, opaque);
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif
17 changes: 17 additions & 0 deletions test/test17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,20 @@ UTEST(cpp17, Null) {
}

#endif

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#endif

struct Opaque;

UTEST(cpp17, Opaque) {
struct Opaque *opaque = nullptr;
EXPECT_EQ(nullptr, opaque);
ASSERT_EQ(nullptr, opaque);
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif
8 changes: 8 additions & 0 deletions test/test99.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,11 @@ UTEST(c99, Near) {
}

UTEST(c99, Todo) { UTEST_SKIP("Not yet implemented!"); }

struct Opaque;

UTEST(c99, Opaque) {
struct Opaque *opaque = NULL;
EXPECT_EQ(NULL, opaque);
ASSERT_EQ(NULL, opaque);
}
39 changes: 29 additions & 10 deletions utest.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ utest_type_printer(long long unsigned int i) {
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
!(defined(__MINGW32__) || defined(__MINGW64__)) || \
defined(__TINYC__)

#if (!defined(__clang__) && defined(__GNUC__)) || defined(__TINYC__)

// For GCC and TCC we cannot print by pointer because of how they
// handle opaque struct pointers.
#define utest_type_printer(val) \
UTEST_PRINTF(_Generic((val), signed char \
: "%d", unsigned char \
Expand All @@ -650,11 +655,29 @@ utest_type_printer(long long unsigned int i) {
: "%f", double \
: "%f", long double \
: "%Lf", default \
: _Generic((val - val), ptrdiff_t \
: "%p", default \
: "undef")), \
: "%p"), \
(val))
#else
UTEST_PRINTF(_Generic((val), signed char
: "%d", unsigned char
: "%u", short
: "%d", unsigned short
: "%u", int
: "%d", long
: "%ld", long long
: "%lld", unsigned
: "%u", unsigned long
: "%lu", unsigned long long
: "%llu", float
: "%f", double
: "%f", long double
: "%Lf", default
: _Generic((val - val), ptrdiff_t
: "%p", default
: "undef")),
(val))
#endif
#else
/*
we don't have the ability to print the values we got, so we create a macro
to tell our users we can't do anything fancy
Expand All @@ -674,24 +697,20 @@ utest_type_printer(long long unsigned int i) {

#if defined(__cplusplus) && (__cplusplus >= 201103L)
#define UTEST_AUTO(x) auto
#elif !defined(__cplusplus)

#if defined(__clang__)
#elif defined(__clang__)
/* clang-format off */
/* had to disable clang-format here because it malforms the pragmas */
#define UTEST_AUTO(x) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wgnu-auto-type\"") __auto_type \
_Pragma("clang diagnostic pop")
/* clang-format on */
#elif !defined(__clang__) && defined(__GNUC__)
#define UTEST_AUTO(x) __auto_type
#else
#define UTEST_AUTO(x) __typeof__(x + 0)
#endif

#else
#define UTEST_AUTO(x) typeof(x + 0)
#endif

#if defined(__clang__)
#define UTEST_STRNCMP(x, y, size) \
_Pragma("clang diagnostic push") \
Expand Down

0 comments on commit 938858a

Please sign in to comment.