From 95539c89adbaaa233a83cd374d23cf1d2acd4451 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Sat, 5 Sep 2015 22:58:45 +0200 Subject: [PATCH] Removed VLA and ?: GNU extension occurences --- src/common.h | 37 +++++++++++++++++++++++++++++++++++++ src/event.c | 3 ++- src/extmatch.c | 13 +++++++------ src/i18n.c | 5 ++--- src/i18n.h | 2 ++ src/log/normal.c | 6 ++---- src/log/tap.c | 8 ++++---- src/main.c | 19 ++++++++++--------- src/ordered-set.c | 6 +++--- src/report.c | 2 +- src/report.h | 4 +++- src/runner.c | 19 ++++++++++--------- src/stats.c | 4 ++-- 13 files changed, 85 insertions(+), 43 deletions(-) create mode 100644 src/common.h diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..b9add0b --- /dev/null +++ b/src/common.h @@ -0,0 +1,37 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2015 Franklin "Snaipe" Mathieu + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef COMMON_H_ +# define COMMON_H_ + +#ifdef __GNUC__ +# define INLINE __attribute__((always_inline)) inline +#elif defined(_MSC_VER) +# define INLINE __forceinline +#else +# define INLINE +#endif + +# define DEF(X, Y) ((X) ? (X) : (Y)) + +#endif /* !COMMON_H_ */ diff --git a/src/event.c b/src/event.c index 38b25b2..83a92e0 100644 --- a/src/event.c +++ b/src/event.c @@ -103,9 +103,10 @@ struct event *read_event(FILE *f) { } void send_event(int kind, void *data, size_t size) { - unsigned char buf[sizeof (int) + size]; + unsigned char *buf = malloc(sizeof (int) + size); memcpy(buf, &kind, sizeof (int)); memcpy(buf + sizeof (int), data, size); if (fwrite(buf, sizeof (int) + size, 1, g_event_pipe) == 0) abort(); + free(buf); } diff --git a/src/extmatch.c b/src/extmatch.c index 604d418..79a7ad3 100644 --- a/src/extmatch.c +++ b/src/extmatch.c @@ -28,6 +28,7 @@ #include #include "criterion/common.h" +#include "common.h" struct context { int depth; @@ -107,20 +108,20 @@ static int is_eos(struct context *ctx) { static inline void handle_special(struct context *ctx, handler_arg strs[5]) { if (peek_char(ctx) == '(') { - if ((strs[0].validator ?: inactive)(ctx)) + if (DEF(strs[0].validator, inactive)(ctx)) copy_str(ctx, strs[0].str); dup_char(ctx); - if ((strs[1].validator ?: inactive)(ctx)) + if (DEF(strs[1].validator, inactive)(ctx)) copy_str(ctx, strs[1].str); transform_rec(ctx); - if ((strs[2].validator ?: inactive)(ctx)) + if (DEF(strs[2].validator, inactive)(ctx)) copy_str(ctx,strs[2].str); copy_char(ctx, ')'); - if ((strs[3].validator ?: inactive)(ctx)) + if (DEF(strs[3].validator, inactive)(ctx)) copy_str(ctx, strs[3].str); - } else if ((strs[4].validator ?: inactive)(ctx)) { + } else if (DEF(strs[4].validator, inactive)(ctx)) { copy_str(ctx, strs[4].str); } } @@ -191,7 +192,7 @@ void transform_impl(struct context *ctx) { if (c == ')' && ctx->depth > 0) return; - (handler ?: copy_char)(ctx, c); + (handler ? handler : copy_char)(ctx, c); if (ctx->eos) return; diff --git a/src/i18n.c b/src/i18n.c index 5b9899b..7a7c51b 100644 --- a/src/i18n.c +++ b/src/i18n.c @@ -1,8 +1,7 @@ #include "i18n.h" -#if ENABLE_NLS -__attribute__ ((constructor)) void init_i18n(void) { +#if ENABLE_NLS bindtextdomain (PACKAGE, LOCALEDIR); -} #endif +} diff --git a/src/i18n.h b/src/i18n.h index 74c159e..2657c1e 100644 --- a/src/i18n.h +++ b/src/i18n.h @@ -13,4 +13,6 @@ dngettext(PACKAGE, String, Plural, (Quantity)) # endif +void init_i18n(void); + #endif /* !I18N_H_ */ diff --git a/src/log/normal.c b/src/log/normal.c index 8116033..1ce002c 100644 --- a/src/log/normal.c +++ b/src/log/normal.c @@ -34,8 +34,7 @@ #include "config.h" #include "i18n.h" #include "posix-compat.h" - -#define USED __attribute__ ((used)) +#include "common.h" #ifdef VANILLA_WIN32 // fallback to strtok on windows since strtok_s is not available everywhere @@ -119,8 +118,7 @@ void normal_log_post_test(struct criterion_test_stats *stats) { stats->elapsed_time); } -__attribute__((always_inline)) -static inline bool is_disabled(struct criterion_test *t, +static INLINE bool is_disabled(struct criterion_test *t, struct criterion_suite *s) { return t->data->disabled || (s->data && s->data->disabled); } diff --git a/src/log/tap.c b/src/log/tap.c index 23898f4..b406ba3 100644 --- a/src/log/tap.c +++ b/src/log/tap.c @@ -32,6 +32,7 @@ #include "timer.h" #include "config.h" #include "posix-compat.h" +#include "common.h" void tap_log_pre_all(struct criterion_test_set *set) { size_t enabled_count = 0; @@ -54,8 +55,7 @@ void tap_log_pre_suite(struct criterion_suite_set *set) { set->suite.name); } -__attribute__((always_inline)) -static inline bool is_disabled(struct criterion_test *t, struct criterion_suite *s) { +static INLINE bool is_disabled(struct criterion_test *t, struct criterion_suite *s) { return t->data->disabled || (s->data && s->data->disabled); } @@ -65,7 +65,7 @@ void tap_log_post_suite(struct criterion_suite_stats *stats) { criterion_important("ok - %s::%s %s # SKIP %s is disabled\n", ts->test->category, ts->test->name, - ts->test->data->description ?: "", + DEF(ts->test->data->description, ""), ts->test->data->disabled ? "test" : "suite"); } } @@ -78,7 +78,7 @@ void tap_log_post_test(struct criterion_test_stats *stats) { stats->failed ? "not ok" : "ok", stats->test->category, stats->test->name, - stats->test->data->description ?: "", + DEF(stats->test->data->description, ""), stats->elapsed_time); for (struct criterion_assert_stats *asrt = stats->asserts; asrt; asrt = asrt->next) { if (!asrt->passed) { diff --git a/src/main.c b/src/main.c index a3ca0ac..7995b34 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,7 @@ #include #include "runner.h" #include "config.h" +#include "common.h" #if ENABLE_NLS # include @@ -137,8 +138,8 @@ int main(int argc, char *argv[]) { {0, 0, 0, 0 } }; - bool use_ascii = !strcmp("1", getenv("CRITERION_USE_ASCII") ?: "0") - || !strcmp("dumb", getenv("TERM") ?: "dumb"); + bool use_ascii = !strcmp("1", DEF(getenv("CRITERION_USE_ASCII"), "0")) + || !strcmp("dumb", DEF(getenv("TERM"), "dumb")); setlocale(LC_ALL, ""); #if ENABLE_NLS @@ -146,24 +147,24 @@ int main(int argc, char *argv[]) { #endif struct criterion_options *opt = &criterion_options; - opt->always_succeed = !strcmp("1", getenv("CRITERION_ALWAYS_SUCCEED") ?: "0"); - opt->no_early_exit = !strcmp("1", getenv("CRITERION_NO_EARLY_EXIT") ?: "0"); - opt->fail_fast = !strcmp("1", getenv("CRITERION_FAIL_FAST") ?: "0"); + opt->always_succeed = !strcmp("1", DEF(getenv("CRITERION_ALWAYS_SUCCEED"), "0")); + opt->no_early_exit = !strcmp("1", DEF(getenv("CRITERION_NO_EARLY_EXIT") , "0")); + opt->fail_fast = !strcmp("1", DEF(getenv("CRITERION_FAIL_FAST") , "0")); opt->use_ascii = use_ascii; - opt->logging_threshold = atoi(getenv("CRITERION_VERBOSITY_LEVEL") ?: "2"); - opt->short_filename = !strcmp("1", getenv("CRITERION_SHORT_FILENAME") ?: "0"); + opt->logging_threshold = atoi(DEF(getenv("CRITERION_VERBOSITY_LEVEL"), "2")); + opt->short_filename = !strcmp("1", DEF(getenv("CRITERION_SHORT_FILENAME"), "0")); #ifdef HAVE_PCRE opt->pattern = getenv("CRITERION_TEST_PATTERN"); #endif - bool use_tap = !strcmp("1", getenv("CRITERION_ENABLE_TAP") ?: "0"); + bool use_tap = !strcmp("1", DEF(getenv("CRITERION_ENABLE_TAP"), "0")); bool do_list_tests = false; bool do_print_version = false; bool do_print_usage = false; for (int c; (c = getopt_long(argc, argv, "hvlfS", opts, NULL)) != -1;) { switch (c) { - case 'b': criterion_options.logging_threshold = atoi(optarg ?: "1"); break; + case 'b': criterion_options.logging_threshold = atoi(DEF(optarg, "1")); break; case 'y': criterion_options.always_succeed = true; break; case 'z': criterion_options.no_early_exit = true; break; case 'k': criterion_options.use_ascii = true; break; diff --git a/src/ordered-set.c b/src/ordered-set.c index 3115ca2..cecc87e 100644 --- a/src/ordered-set.c +++ b/src/ordered-set.c @@ -25,18 +25,18 @@ #include #include #include +#include "common.h" static void destroy_ordered_set(void *ptr, UNUSED void *meta) { sfree(((struct criterion_ordered_set *) ptr)->first); } -__attribute__ ((always_inline)) -static inline void nothing() {} +static INLINE void nothing() {} static void destroy_ordered_set_node(void *ptr, void *meta) { struct criterion_ordered_set *set = *(void **) meta; struct criterion_ordered_set_node *n = ptr; - (set->dtor ?: nothing)(n->data, NULL); + DEF(set->dtor, nothing)(n->data, NULL); sfree(((struct criterion_ordered_set_node *) ptr)->next); } diff --git a/src/report.c b/src/report.c index b839542..3b709e0 100644 --- a/src/report.c +++ b/src/report.c @@ -41,7 +41,7 @@ static inline void nothing() {} for (f_report_hook *hook = GET_SECTION_START(HOOK_SECTION(Kind)); \ hook < (f_report_hook*) GET_SECTION_END(HOOK_SECTION(Kind)); \ ++hook) { \ - (*hook ?: nothing)(data); \ + (*hook ? *hook : nothing)(data); \ } \ } diff --git a/src/report.h b/src/report.h index 02aeed9..2322267 100644 --- a/src/report.h +++ b/src/report.h @@ -45,6 +45,8 @@ DECL_CALL_REPORT_HOOKS(POST_SUITE); DECL_CALL_REPORT_HOOKS(POST_ALL); #define log(Type, Arg) \ - (criterion_options.output_provider->log_ ## Type ?: nothing)(Arg); + log_(criterion_options.output_provider->log_ ## Type, Arg); +#define log_(Log, Arg) \ + (Log ? Log : nothing)(Arg); #endif /* !REPORT_H_ */ diff --git a/src/runner.c b/src/runner.c index 67f3ed8..37342a3 100644 --- a/src/runner.c +++ b/src/runner.c @@ -37,6 +37,8 @@ #include "posix-compat.h" #include "abort.h" #include "config.h" +#include "i18n.h" +#include "common.h" #ifdef HAVE_PCRE #include "extmatch.h" @@ -49,8 +51,7 @@ IMPL_SECTION_LIMITS(struct criterion_suite, cr_sts); TestSuite(); Test(,) {}; -__attribute__ ((always_inline)) -static inline void nothing() {} +static INLINE void nothing() {} int cmp_suite(void *a, void *b) { struct criterion_suite *s1 = a, *s2 = b; @@ -161,14 +162,14 @@ static void run_test_child(struct criterion_test *test, send_event(PRE_INIT, NULL, 0); if (suite->data) - (suite->data->init ?: nothing)(); - (test->data->init ?: nothing)(); + (suite->data->init ? suite->data->init : nothing)(); + (test->data->init ? test->data->init : nothing)(); send_event(PRE_TEST, NULL, 0); struct timespec_compat ts; if (setup_abort_test()) { timer_start(&ts); - (test->test ?: nothing)(); + (test->test ? test->test : nothing)(); } double elapsed_time; @@ -176,14 +177,13 @@ static void run_test_child(struct criterion_test *test, elapsed_time = -1; send_event(POST_TEST, &elapsed_time, sizeof (double)); - (test->data->fini ?: nothing)(); + (test->data->fini ? test->data->fini : nothing)(); if (suite->data) - (suite->data->fini ?: nothing)(); + (suite->data->fini ? suite->data->fini : nothing)(); send_event(POST_FINI, NULL, 0); } -__attribute__((always_inline)) -static inline bool is_disabled(struct criterion_test *t, +static INLINE bool is_disabled(struct criterion_test *t, struct criterion_suite *s) { return t->data->disabled || (s->data && s->data->disabled); @@ -367,6 +367,7 @@ cleanup: } int criterion_run_all_tests(void) { + init_i18n(); set_runner_process(); int res = criterion_run_all_tests_impl(); unset_runner_process(); diff --git a/src/stats.c b/src/stats.c index f46b957..b017c8a 100644 --- a/src/stats.c +++ b/src/stats.c @@ -25,6 +25,7 @@ #include #include "criterion/common.h" #include "stats.h" +#include "common.h" #include @@ -130,8 +131,7 @@ static void push_pre_suite(s_glob_stats *stats, ++stats->nb_suites; } -__attribute__((always_inline)) -static inline bool is_disabled(struct criterion_test *t, +static INLINE bool is_disabled(struct criterion_test *t, struct criterion_suite *s) { return t->data->disabled || (s->data && s->data->disabled);