From e872d402235f1e5aecb99a32960fc0bbb478b9c9 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 17 Jun 2017 14:21:08 +0200 Subject: [PATCH 1/7] MSVC: Disable deprecation warnings for C89 functions C89 sadly doesn't provide safe alternatives for strcpy, sprintf and the like. --- cJSON.c | 5 +++++ cJSON_Utils.c | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/cJSON.c b/cJSON.c index 6e8255a..9c86128 100644 --- a/cJSON.c +++ b/cJSON.c @@ -23,6 +23,11 @@ /* cJSON */ /* JSON parser in C. */ +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif + #ifdef __GNUC__ #pragma GCC visibility push(default) #endif diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 84319cc..e7bf802 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -20,6 +20,10 @@ THE SOFTWARE. */ +/* disable warnings about old C89 functions in MSVC */ +#if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +#define _CRT_SECURE_NO_DEPRECATE +#endif #pragma GCC visibility push(default) #include #include From 217ab0261248a0b698f8201987169266f71a8373 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 17 Jun 2017 14:23:12 +0200 Subject: [PATCH 2/7] cJSON_Utils: Guard gcc pragmas with a check for __GCC__ --- cJSON_Utils.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index e7bf802..2baa492 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -24,13 +24,18 @@ #if !defined(_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) #define _CRT_SECURE_NO_DEPRECATE #endif + +#ifdef __GNUCC__ #pragma GCC visibility push(default) +#endif #include #include #include #include #include +#ifdef __GNUCC__ #pragma GCC visibility pop +#endif #include "cJSON_Utils.h" From 0d675cb048b9fd4ad5365e6df336b33ae3374275 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 17 Jun 2017 14:26:25 +0200 Subject: [PATCH 3/7] MSVC: Disable warning about single line comments in system headers --- cJSON.c | 8 ++++++++ cJSON_Utils.c | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/cJSON.c b/cJSON.c index 9c86128..e816cde 100644 --- a/cJSON.c +++ b/cJSON.c @@ -31,6 +31,11 @@ #ifdef __GNUC__ #pragma GCC visibility push(default) #endif +#if defined(_MSC_VER) +#pragma warning (push) +/* disable warning about single line comments in system headers */ +#pragma warning (disable : 4001) +#endif #include #include @@ -41,6 +46,9 @@ #include #include +#if defined(_MSC_VER) +#pragma warning (pop) +#endif #ifdef __GNUC__ #pragma GCC visibility pop #endif diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 2baa492..b83cfcd 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -28,11 +28,21 @@ #ifdef __GNUCC__ #pragma GCC visibility push(default) #endif +#if defined(_MSC_VER) +#pragma warning (push) +/* disable warning about single line comments in system headers */ +#pragma warning (disable : 4001) +#endif + #include #include #include #include #include + +#if defined(_MSC_VER) +#pragma warning (pop) +#endif #ifdef __GNUCC__ #pragma GCC visibility pop #endif From d1c2e2df4a7cddaae621d2d9c973fa8b8f6b672d Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 17 Jun 2017 14:33:04 +0200 Subject: [PATCH 4/7] MSVC: workaround for C2322 --- cJSON.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index e816cde..f6ee1ad 100644 --- a/cJSON.c +++ b/cJSON.c @@ -114,7 +114,27 @@ typedef struct internal_hooks void *(*reallocate)(void *pointer, size_t size); } internal_hooks; -static internal_hooks global_hooks = { malloc, free, realloc }; +#if defined(_MSC_VER) +/* work around MSVC error C2322: '...' address of dillimport '...' is not static */ +static void *internal_malloc(size_t size) +{ + return malloc(size); +} +static void internal_free(void *pointer) +{ + free(pointer); +} +static void *internal_realloc(void *pointer, size_t size) +{ + return realloc(pointer, size); +} +#else +#define internal_malloc malloc +#define internal_free free +#define internal_realloc realloc +#endif + +static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc }; static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks) { From ac368e9dfb3bc5ab23e2495fbb8234f090424b8b Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 17 Jun 2017 14:33:55 +0200 Subject: [PATCH 5/7] MSVC: Fix warning about assignment in condition --- cJSON.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index f6ee1ad..81b9197 100644 --- a/cJSON.c +++ b/cJSON.c @@ -147,7 +147,8 @@ static unsigned char* cJSON_strdup(const unsigned char* string, const internal_h } length = strlen((const char*)string) + sizeof(""); - if (!(copy = (unsigned char*)hooks->allocate(length))) + copy = (unsigned char*)hooks->allocate(length); + if (copy == NULL) { return NULL; } From e174831819e46afc5797974efd92c92cb13bc9bd Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 17 Jun 2017 14:42:52 +0200 Subject: [PATCH 6/7] CMake: Add custom compiler flags for MSVC --- CMakeLists.txt | 67 ++++++++++++++++++++++++++++---------------------- README.md | 2 +- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f012f95..a55acac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,37 +16,46 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT set(custom_compiler_flags) include(CheckCCompilerFlag) -option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON) +option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags" ON) if (ENABLE_CUSTOM_COMPILER_FLAGS) - list(APPEND custom_compiler_flags - -std=c89 - -pedantic - -Wall - -Wextra - -Werror - -Wstrict-prototypes - -Wwrite-strings - -Wshadow - -Winit-self - -Wcast-align - -Wformat=2 - -Wmissing-prototypes - -Wstrict-overflow=2 - -Wcast-qual - -Wundef - -Wswitch-default - -Wconversion - -Wc++-compat - -fstack-protector-strong - -Wcomma - -Wdouble-promotion - -Wparentheses - -Wformat-overflow - -Wunused-macros - -Wmissing-variable-declarations - -Wused-but-marked-unused - -Wswitch-enum + if (("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")) + list(APPEND custom_compiler_flags + -std=c89 + -pedantic + -Wall + -Wextra + -Werror + -Wstrict-prototypes + -Wwrite-strings + -Wshadow + -Winit-self + -Wcast-align + -Wformat=2 + -Wmissing-prototypes + -Wstrict-overflow=2 + -Wcast-qual + -Wundef + -Wswitch-default + -Wconversion + -Wc++-compat + -fstack-protector-strong + -Wcomma + -Wdouble-promotion + -Wparentheses + -Wformat-overflow + -Wunused-macros + -Wmissing-variable-declarations + -Wused-but-marked-unused + -Wswitch-enum + ) + elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC") + list(APPEND custom_compiler_flags + /GS + /Za + /sdl + /W4 ) + endif() endif() option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF) diff --git a/README.md b/README.md index b22025a..a363f52 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ You can change the build process with a list of different options that you can p * `-DENABLE_CJSON_TEST=On`: Enable building the tests. (on by default) * `-DENABLE_CJSON_UTILS=On`: Enable building cJSON_Utils. (off by default) * `-DENABLE_TARGET_EXPORT=On`: Enable the export of CMake targets. Turn off if it makes problems. (on by default) -* `-DENABLE_CUSTOM_COMPILER_FLAGS=On`: Enable custom compiler flags (currently for Clang and GCC). Turn off if it makes problems. (on by default) +* `-DENABLE_CUSTOM_COMPILER_FLAGS=On`: Enable custom compiler flags (currently for Clang, GCC and MSVC). Turn off if it makes problems. (on by default) * `-DENABLE_VALGRIND=On`: Run tests with [valgrind](http://valgrind.org). (off by default) * `-DENABLE_SANITIZERS=On`: Compile cJSON with [AddressSanitizer](https://github.com/google/sanitizers/wiki/AddressSanitizer) and [UndefinedBehaviorSanitizer](https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html) enabled (if possible). (off by default) * `-DBUILD_SHARED_LIBS=On`: Build the shared libraries. (on by default) From 7a2615c2315713ca1a96d37e8a2015b371e6258d Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sat, 17 Jun 2017 15:04:29 +0200 Subject: [PATCH 7/7] Fix: Check if __GNUCC__ is defined This has been detected via MSVC's Warning C4668 --- cJSON.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index 81b9197..b689361 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1849,7 +1849,7 @@ CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSO item->type &= ~cJSON_StringIsConst; } -#if defined (__clang__) || ((__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) #pragma GCC diagnostic push #endif #ifdef __GNUC__ @@ -1871,7 +1871,7 @@ CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJ item->type |= cJSON_StringIsConst; cJSON_AddItemToArray(object, item); } -#if defined (__clang__) || ((__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) +#if defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) #pragma GCC diagnostic pop #endif