diff --git a/.cmake/Modules/Capabilities.cmake b/.cmake/Modules/Capabilities.cmake new file mode 100644 index 0000000..6b6395e --- /dev/null +++ b/.cmake/Modules/Capabilities.cmake @@ -0,0 +1,12 @@ +# Copyright (C) 2015 Franklin "Snaipe" Mathieu. +# Redistribution and use of this file is allowed according to the terms of the MIT license. +# For details see the LICENSE file distributed with Criterion. + +include(CheckPrototypeDefinition) + +check_prototype_definition( + strtok_s + "char *strtok_s(char *strToken, const char *strDelimit, char **context)" + NULL + "string.h" + HAVE_STRTOK_S) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7024e7c..559f093 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(LIBCSPTR_DISABLE_TESTS ON) set(LIBCSPTR_DISABLE_COVERALLS ON) include(Submodules) +include(Capabilities) if (MSVC) add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) diff --git a/include/criterion/common.h b/include/criterion/common.h index 41cc2ff..8b547e5 100644 --- a/include/criterion/common.h +++ b/include/criterion/common.h @@ -101,7 +101,7 @@ # define CR_NORETURN CR_ATTRIBUTE(noreturn) # define CR_INLINE CR_ATTRIBUTE(always_inline) inline # elif CR_IS_MSVC -# define CR_UNUSED +# define CR_UNUSED __pragma(warning(suppress:4100)) # define CR_NORETURN __declspec(noreturn) # define CR_INLINE __forceinline # else diff --git a/src/config.h.in b/src/config.h.in index fef0a5c..6455bff 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -4,6 +4,7 @@ #cmakedefine ENABLE_NLS @ENABLE_NLS@ #cmakedefine HAVE_PCRE @HAVE_PCRE@ #cmakedefine ENABLE_VALGRIND_ERRORS @ENABLE_VALGRIND_ERRORS@ +#cmakedefine01 HAVE_STRTOK_S # define LOCALEDIR "${LOCALEDIR}" # define PACKAGE "${PROJECT_NAME}" diff --git a/src/log/normal.c b/src/log/normal.c index e549f1f..ba86e6f 100644 --- a/src/log/normal.c +++ b/src/log/normal.c @@ -37,8 +37,13 @@ #include "common.h" #ifdef VANILLA_WIN32 -// fallback to strtok on windows since strtok_s is not available everywhere -# define strtok_r(str, delim, saveptr) strtok(str, delim) +# if HAVE_STRTOK_S +# define strtok_r strtok_s +# else + static char *strtok_r(char *str, const char *delim, CR_UNUSED char **saveptr) { + return strtok(str, delim); + } +# endif #endif #ifdef _MSC_VER @@ -161,13 +166,8 @@ void normal_log_post_all(struct criterion_global_stats *stats) { void normal_log_assert(struct criterion_assert_stats *stats) { if (!stats->passed) { char *dup = strdup(*stats->message ? stats->message : ""); - -#ifdef VANILLA_WIN32 - char *line = strtok(dup, "\n"); -#else char *saveptr = NULL; char *line = strtok_r(dup, "\n", &saveptr); -#endif bool sf = criterion_options.short_filename; criterion_pimportant(CRITERION_PREFIX_DASHES, @@ -176,11 +176,7 @@ void normal_log_assert(struct criterion_assert_stats *stats) { CR_FG_RED, stats->line, CR_RESET, line); -#ifdef VANILLA_WIN32 - while ((line = strtok(NULL, "\n"))) -#else while ((line = strtok_r(NULL, "\n", &saveptr))) -#endif criterion_pimportant(CRITERION_PREFIX_DASHES, _(msg_desc), line); free(dup); } @@ -235,13 +231,8 @@ void normal_log_test_timeout(CR_UNUSED struct criterion_test_stats *stats) { void normal_log_test_abort(CR_UNUSED struct criterion_test_stats *stats, const char *msg) { char *dup = strdup(msg); - -#ifdef VANILLA_WIN32 - char *line = strtok(dup, "\n"); -#else char *saveptr = NULL; char *line = strtok_r(dup, "\n", &saveptr); -#endif criterion_pimportant(CRITERION_PREFIX_DASHES, _(msg_test_abort), @@ -249,11 +240,7 @@ void normal_log_test_abort(CR_UNUSED struct criterion_test_stats *stats, const c stats->test->name, line); -#ifdef VANILLA_WIN32 - while ((line = strtok(NULL, "\n"))) -#else while ((line = strtok_r(NULL, "\n", &saveptr))) -#endif criterion_pimportant(CRITERION_PREFIX_DASHES, _(msg_desc), line); free(dup); diff --git a/src/log/tap.c b/src/log/tap.c index 70edb14..8e3bab9 100644 --- a/src/log/tap.c +++ b/src/log/tap.c @@ -34,6 +34,16 @@ #include "config.h" #include "common.h" +#ifdef VANILLA_WIN32 +# if HAVE_STRTOK_S +# define strtok_r strtok_s +# else + static char *strtok_r(char *str, const char *delim, CR_UNUSED char **saveptr) { + return strtok(str, delim); + } +# endif +#endif + #ifdef _MSC_VER # define strdup _strdup #endif @@ -69,22 +79,15 @@ static void print_test_normal(struct criterion_test_stats *stats) { for (struct criterion_assert_stats *asrt = stats->asserts; asrt; asrt = asrt->next) { if (!asrt->passed) { char *dup = strdup(*asrt->message ? asrt->message : ""); -#ifdef VANILLA_WIN32 - char *line = strtok(dup, "\n"); -#else char *saveptr = NULL; char *line = strtok_r(dup, "\n", &saveptr); -#endif bool sf = criterion_options.short_filename; criterion_important(" %s:%u: Assertion failed: %s\n", sf ? basename_compat(asrt->file) : asrt->file, asrt->line, line); -#ifdef VANILLA_WIN32 - while ((line = strtok(NULL, "\n"))) -#else + while ((line = strtok_r(NULL, "\n", &saveptr))) -#endif criterion_important(" %s\n", line); free(dup); }