Skip to content

Commit

Permalink
Merge pull request #25 from sheredom/fix_clang_warning
Browse files Browse the repository at this point in the history
Fix the awful unsafe buffer usage warning in Clang.
  • Loading branch information
sheredom authored Oct 20, 2023
2 parents b3c419d + 6639853 commit 10a0bcb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
11 changes: 11 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ struct c_my_fixture {
char *data;
};

#if defined(__clang__)
#pragma clang diagnostic push
#if __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif
#endif

UBENCH_F_SETUP(c_my_fixture) {
const int size = 128 * 1024 * 1024;
ubench_fixture->data = (char *)malloc(size);
Expand All @@ -85,3 +92,7 @@ UBENCH_EX_F(c_my_fixture, strrchr_ex) {

UBENCH_DO_BENCHMARK() { UBENCH_DO_NOTHING(strchr(data, 'f')); }
}

#if defined(__clang__)
#pragma clang diagnostic pop
#endif
11 changes: 11 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ struct cpp_my_fixture {
char *data;
};

#if defined(__clang__)
#pragma clang diagnostic push
#if __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif
#endif

UBENCH_F_SETUP(cpp_my_fixture) {
const int size = 128 * 1024 * 1024;
ubench_fixture->data = static_cast<char *>(malloc(size));
Expand All @@ -85,3 +92,7 @@ UBENCH_EX_F(cpp_my_fixture, strrchr_ex) {

UBENCH_DO_BENCHMARK() { UBENCH_DO_NOTHING(strchr(data, 'f')); }
}

#if defined(__clang__)
#pragma clang diagnostic pop
#endif
11 changes: 11 additions & 0 deletions test/test11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ struct cpp11_my_fixture {
char *data;
};

#if defined(__clang__)
#pragma clang diagnostic push
#if __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif
#endif

UBENCH_F_SETUP(cpp11_my_fixture) {
const int size = 128 * 1024 * 1024;
ubench_fixture->data = static_cast<char *>(malloc(size));
Expand All @@ -99,3 +106,7 @@ UBENCH_EX_F(cpp11_my_fixture, strchr_ex) {

UBENCH_DO_BENCHMARK() { UBENCH_DO_NOTHING(strchr(data, 'f')); }
}

#if defined(__clang__)
#pragma clang diagnostic pop
#endif
47 changes: 42 additions & 5 deletions ubench.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,30 @@ UBENCH_EXTERN struct ubench_state_s ubench_state;
#pragma clang diagnostic pop
#endif

static UBENCH_INLINE int ubench_do_benchmark(struct ubench_run_state_s *ubs) {
ubench_int64_t curr_sample = ubs->sample++;
ubs->ns[curr_sample] = ubench_ns();
return curr_sample < ubs->size ? 1 : 0;
}
#if defined(__clang__)
#if __has_warning("-Wunsafe-buffer-usage")
#define UBENCH_SURPRESS_WARNINGS_BEGIN \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wunsafe-buffer-usage\"")
#define UBENCH_SURPRESS_WARNINGS_END _Pragma("clang diagnostic pop")
#else
#define UBENCH_SURPRESS_WARNINGS_BEGIN
#define UBENCH_SURPRESS_WARNINGS_END
#endif
#elif defined(__GNUC__) && __GNUC__ >= 8 && defined(__cplusplus)
#define UBENCH_SURPRESS_WARNINGS_BEGIN \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wclass-memaccess\"")
#define UBENCH_SURPRESS_WARNINGS_END _Pragma("GCC diagnostic pop")
#else
#define UBENCH_SURPRESS_WARNINGS_BEGIN
#define UBENCH_SURPRESS_WARNINGS_END
#endif

#define UBENCH_DO_BENCHMARK() while (ubench_do_benchmark(ubench_run_state) > 0)

#define UBENCH_EX(SET, NAME) \
UBENCH_SURPRESS_WARNINGS_BEGIN \
UBENCH_EXTERN struct ubench_state_s ubench_state; \
static void ubench_##SET##_##NAME(struct ubench_run_state_s *ubs); \
UBENCH_INITIALIZER(ubench_register_##SET##_##NAME) { \
Expand All @@ -384,6 +399,7 @@ static UBENCH_INLINE int ubench_do_benchmark(struct ubench_run_state_s *ubs) {
ubench_state.benchmarks[index].name = name; \
UBENCH_SNPRINTF(name, name_size, "%s", name_part); \
} \
UBENCH_SURPRESS_WARNINGS_END \
void ubench_##SET##_##NAME(struct ubench_run_state_s *ubench_run_state)

#define UBENCH(SET, NAME) \
Expand All @@ -400,6 +416,7 @@ static UBENCH_INLINE int ubench_do_benchmark(struct ubench_run_state_s *ubs) {
static void ubench_f_teardown_##FIXTURE(struct FIXTURE *ubench_fixture)

#define UBENCH_EX_F(FIXTURE, NAME) \
UBENCH_SURPRESS_WARNINGS_BEGIN \
UBENCH_EXTERN struct ubench_state_s ubench_state; \
static void ubench_f_setup_##FIXTURE(struct FIXTURE *); \
static void ubench_f_teardown_##FIXTURE(struct FIXTURE *); \
Expand Down Expand Up @@ -427,6 +444,7 @@ static UBENCH_INLINE int ubench_do_benchmark(struct ubench_run_state_s *ubs) {
ubench_state.benchmarks[index].name = name; \
UBENCH_SNPRINTF(name, name_size, "%s", name_part); \
} \
UBENCH_SURPRESS_WARNINGS_END \
void ubench_run_ex_##FIXTURE##_##NAME( \
struct FIXTURE *ubench_fixture, \
struct ubench_run_state_s *ubench_run_state)
Expand All @@ -438,6 +456,21 @@ static UBENCH_INLINE int ubench_do_benchmark(struct ubench_run_state_s *ubs) {
} \
void ubench_run_##FIXTURE##_##NAME(struct FIXTURE *ubench_fixture)

#ifdef __clang__
#pragma clang diagnostic push

#if __has_warning("-Wunsafe-buffer-usage")
#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
#endif
#endif

static UBENCH_INLINE int
ubench_do_benchmark(struct ubench_run_state_s *const ubs) {
const ubench_int64_t curr_sample = ubs->sample++;
ubs->ns[curr_sample] = ubench_ns();
return curr_sample < ubs->size ? 1 : 0;
}

static UBENCH_INLINE int ubench_should_filter(const char *filter,
const char *benchmark);
int ubench_should_filter(const char *filter, const char *benchmark) {
Expand Down Expand Up @@ -791,6 +824,10 @@ int ubench_main(int argc, const char *const argv[]) {
return UBENCH_CAST(int, failed);
}

#ifdef __clang__
#pragma clang diagnostic pop
#endif

UBENCH_C_FUNC UBENCH_NOINLINE void ubench_do_nothing(void *const);

#define UBENCH_DO_NOTHING(x) ubench_do_nothing(x)
Expand Down

0 comments on commit 10a0bcb

Please sign in to comment.