Removed VLA and ?: GNU extension occurences

This commit is contained in:
Snaipe 2015-09-05 22:58:45 +02:00
parent 4817fb709f
commit 95539c89ad
13 changed files with 85 additions and 43 deletions

37
src/common.h Normal file
View file

@ -0,0 +1,37 @@
/*
* The MIT License (MIT)
*
* Copyright © 2015 Franklin "Snaipe" Mathieu <http://snai.pe/>
*
* 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_ */

View file

@ -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);
}

View file

@ -28,6 +28,7 @@
#include <stdio.h>
#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;

View file

@ -1,8 +1,7 @@
#include "i18n.h"
#if ENABLE_NLS
__attribute__ ((constructor))
void init_i18n(void) {
#if ENABLE_NLS
bindtextdomain (PACKAGE, LOCALEDIR);
}
#endif
}

View file

@ -13,4 +13,6 @@
dngettext(PACKAGE, String, Plural, (Quantity))
# endif
void init_i18n(void);
#endif /* !I18N_H_ */

View file

@ -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);
}

View file

@ -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) {

View file

@ -31,6 +31,7 @@
#include <csptr/smalloc.h>
#include "runner.h"
#include "config.h"
#include "common.h"
#if ENABLE_NLS
# include <libintl.h>
@ -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;

View file

@ -25,18 +25,18 @@
#include <criterion/common.h>
#include <criterion/ordered-set.h>
#include <csptr/smalloc.h>
#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);
}

View file

@ -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); \
} \
}

View file

@ -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_ */

View file

@ -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();

View file

@ -25,6 +25,7 @@
#include <csptr/smalloc.h>
#include "criterion/common.h"
#include "stats.h"
#include "common.h"
#include <assert.h>
@ -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);