Merge pull request #181 from DaveGamble/msvc-fixes

MSVC compiler handling
This commit is contained in:
Max Bruckner 2017-06-17 15:06:55 +02:00
commit b26e71f960
4 changed files with 96 additions and 34 deletions

View File

@ -16,37 +16,46 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT
set(custom_compiler_flags) set(custom_compiler_flags)
include(CheckCCompilerFlag) 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) if (ENABLE_CUSTOM_COMPILER_FLAGS)
list(APPEND custom_compiler_flags if (("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang") OR ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU"))
-std=c89 list(APPEND custom_compiler_flags
-pedantic -std=c89
-Wall -pedantic
-Wextra -Wall
-Werror -Wextra
-Wstrict-prototypes -Werror
-Wwrite-strings -Wstrict-prototypes
-Wshadow -Wwrite-strings
-Winit-self -Wshadow
-Wcast-align -Winit-self
-Wformat=2 -Wcast-align
-Wmissing-prototypes -Wformat=2
-Wstrict-overflow=2 -Wmissing-prototypes
-Wcast-qual -Wstrict-overflow=2
-Wundef -Wcast-qual
-Wswitch-default -Wundef
-Wconversion -Wswitch-default
-Wc++-compat -Wconversion
-fstack-protector-strong -Wc++-compat
-Wcomma -fstack-protector-strong
-Wdouble-promotion -Wcomma
-Wparentheses -Wdouble-promotion
-Wformat-overflow -Wparentheses
-Wunused-macros -Wformat-overflow
-Wmissing-variable-declarations -Wunused-macros
-Wused-but-marked-unused -Wmissing-variable-declarations
-Wswitch-enum -Wused-but-marked-unused
-Wswitch-enum
) )
elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
list(APPEND custom_compiler_flags
/GS
/Za
/sdl
/W4
)
endif()
endif() endif()
option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF) option(ENABLE_SANITIZERS "Enables AddressSanitizer and UndefinedBehaviorSanitizer." OFF)

View File

@ -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_TEST=On`: Enable building the tests. (on by default)
* `-DENABLE_CJSON_UTILS=On`: Enable building cJSON_Utils. (off 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_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_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) * `-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) * `-DBUILD_SHARED_LIBS=On`: Build the shared libraries. (on by default)

42
cJSON.c
View File

@ -23,9 +23,19 @@
/* cJSON */ /* cJSON */
/* JSON parser in C. */ /* 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__ #ifdef __GNUC__
#pragma GCC visibility push(default) #pragma GCC visibility push(default)
#endif #endif
#if defined(_MSC_VER)
#pragma warning (push)
/* disable warning about single line comments in system headers */
#pragma warning (disable : 4001)
#endif
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -36,6 +46,9 @@
#include <ctype.h> #include <ctype.h>
#include <locale.h> #include <locale.h>
#if defined(_MSC_VER)
#pragma warning (pop)
#endif
#ifdef __GNUC__ #ifdef __GNUC__
#pragma GCC visibility pop #pragma GCC visibility pop
#endif #endif
@ -101,7 +114,27 @@ typedef struct internal_hooks
void *(*reallocate)(void *pointer, size_t size); void *(*reallocate)(void *pointer, size_t size);
} internal_hooks; } 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) static unsigned char* cJSON_strdup(const unsigned char* string, const internal_hooks * const hooks)
{ {
@ -114,7 +147,8 @@ static unsigned char* cJSON_strdup(const unsigned char* string, const internal_h
} }
length = strlen((const char*)string) + sizeof(""); length = strlen((const char*)string) + sizeof("");
if (!(copy = (unsigned char*)hooks->allocate(length))) copy = (unsigned char*)hooks->allocate(length);
if (copy == NULL)
{ {
return NULL; return NULL;
} }
@ -1815,7 +1849,7 @@ CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSO
item->type &= ~cJSON_StringIsConst; 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 #pragma GCC diagnostic push
#endif #endif
#ifdef __GNUC__ #ifdef __GNUC__
@ -1837,7 +1871,7 @@ CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJ
item->type |= cJSON_StringIsConst; item->type |= cJSON_StringIsConst;
cJSON_AddItemToArray(object, item); 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 #pragma GCC diagnostic pop
#endif #endif

View File

@ -20,13 +20,32 @@
THE SOFTWARE. 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
#ifdef __GNUCC__
#pragma GCC visibility push(default) #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 <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <limits.h> #include <limits.h>
#if defined(_MSC_VER)
#pragma warning (pop)
#endif
#ifdef __GNUCC__
#pragma GCC visibility pop #pragma GCC visibility pop
#endif
#include "cJSON_Utils.h" #include "cJSON_Utils.h"