From 7ed067e68039542c9f873475c17fab6aadffd5b7 Mon Sep 17 00:00:00 2001 From: Stephan Gatzka Date: Sun, 27 Nov 2016 18:16:28 +0100 Subject: [PATCH 1/7] Rename some variables to avoid shadowing. --- test_utils.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test_utils.c b/test_utils.c index 961b84e1..6a836880 100644 --- a/test_utils.c +++ b/test_utils.c @@ -96,14 +96,14 @@ int main(void) printf("JSON Apply Patch Tests\n"); for (i = 0; i < 15; i++) { - cJSON *object = cJSON_Parse(patches[i][0]); + cJSON *object_to_be_patched = cJSON_Parse(patches[i][0]); cJSON *patch = cJSON_Parse(patches[i][1]); - int err = cJSONUtils_ApplyPatches(object, patch); - char *output = cJSON_Print(object); + int err = cJSONUtils_ApplyPatches(object_to_be_patched, patch); + char *output = cJSON_Print(object_to_be_patched); printf("Test %d (err %d):\n%s\n\n", i + 1, err, output); free(output); - cJSON_Delete(object); + cJSON_Delete(object_to_be_patched); cJSON_Delete(patch); } @@ -168,19 +168,19 @@ int main(void) printf("JSON Merge Patch tests\n"); for (i = 0; i < 15; i++) { - cJSON *object = cJSON_Parse(merges[i][0]); + cJSON *object_to_be_merged = cJSON_Parse(merges[i][0]); cJSON *patch = cJSON_Parse(merges[i][1]); - char *before = cJSON_PrintUnformatted(object); + char *before_merge = cJSON_PrintUnformatted(object_to_be_merged); patchtext = cJSON_PrintUnformatted(patch); - printf("Before: [%s] -> [%s] = ", before, patchtext); - object = cJSONUtils_MergePatch(object, patch); - after = cJSON_PrintUnformatted(object); + printf("Before: [%s] -> [%s] = ", before_merge, patchtext); + object_to_be_merged = cJSONUtils_MergePatch(object_to_be_merged, patch); + after = cJSON_PrintUnformatted(object_to_be_merged); printf("[%s] vs [%s] (%s)\n", after, merges[i][2], strcmp(after, merges[i][2]) ? "FAIL" : "OK"); - free(before); + free(before_merge); free(patchtext); free(after); - cJSON_Delete(object); + cJSON_Delete(object_to_be_merged); cJSON_Delete(patch); } From 1568015de6d28435a1063c3f8b1993fae5608fbf Mon Sep 17 00:00:00 2001 From: Stephan Gatzka Date: Sun, 27 Nov 2016 18:18:00 +0100 Subject: [PATCH 2/7] Warn about shadowing variables and functions. --- CMakeLists.txt | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21344ede..5f03bb3d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON) if (ENABLE_CUSTOM_COMPILER_FLAGS) if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow") endif() endif() diff --git a/Makefile b/Makefile index 9326886e..87de70d6 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) INSTALL ?= cp -a -R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings $(CFLAGS) +R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow $(CFLAGS) uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') From 9591ecddbe22bd4ed90bc10d5ab5b137c4281e4e Mon Sep 17 00:00:00 2001 From: Stephan Gatzka Date: Sun, 27 Nov 2016 18:19:07 +0100 Subject: [PATCH 3/7] Warn about variables initialized by themselves. --- CMakeLists.txt | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f03bb3d..6f38b6f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON) if (ENABLE_CUSTOM_COMPILER_FLAGS) if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self") endif() endif() diff --git a/Makefile b/Makefile index 87de70d6..08b1680a 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) INSTALL ?= cp -a -R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow $(CFLAGS) +R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self $(CFLAGS) uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') From 91d11cd050491d3dd269f5a0d63e74db534b4098 Mon Sep 17 00:00:00 2001 From: Stephan Gatzka Date: Sun, 27 Nov 2016 18:19:52 +0100 Subject: [PATCH 4/7] Warn if casting a pointer increases alignment of target. --- CMakeLists.txt | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6f38b6f3..2c985318 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON) if (ENABLE_CUSTOM_COMPILER_FLAGS) if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align") endif() endif() diff --git a/Makefile b/Makefile index 08b1680a..e57ae456 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) INSTALL ?= cp -a -R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self $(CFLAGS) +R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align $(CFLAGS) uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') From bea0be48b68b536dadb1fc08f518920bd20ad6eb Mon Sep 17 00:00:00 2001 From: Stephan Gatzka Date: Sun, 27 Nov 2016 18:20:41 +0100 Subject: [PATCH 5/7] Perform additional format checks. --- CMakeLists.txt | 2 +- Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c985318..70c1d86a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON) if (ENABLE_CUSTOM_COMPILER_FLAGS) if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2") endif() endif() diff --git a/Makefile b/Makefile index e57ae456..e4b8be9e 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) INSTALL ?= cp -a -R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align $(CFLAGS) +R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 $(CFLAGS) uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') From a8e1368697f6a32e6688e126a2c0addd7b4d5f30 Mon Sep 17 00:00:00 2001 From: Stephan Gatzka Date: Sat, 26 Nov 2016 15:18:07 +0100 Subject: [PATCH 6/7] Make function static when possible. This is a prerequisite to later enable -Wmissing-prototypes. --- test.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test.c b/test.c index 7706d97b..e079d0a2 100644 --- a/test.c +++ b/test.c @@ -25,7 +25,7 @@ #include "cJSON.h" /* Parse text to JSON, then render back to text, and print! */ -void doit(char *text) +static void doit(char *text) { char *out = NULL; cJSON *json = NULL; @@ -44,8 +44,9 @@ void doit(char *text) } } +#if 0 /* Read a file, parse, render back, etc. */ -void dofile(char *filename) +static void dofile(char *filename) { FILE *f = NULL; long len = 0; @@ -67,6 +68,7 @@ void dofile(char *filename) doit(data); free(data); } +#endif /* Used by some code below as an example datatype. */ struct record @@ -82,7 +84,7 @@ struct record }; /* Create a bunch of objects as demonstration. */ -void create_objects(void) +static void create_objects(void) { /* declare a few. */ cJSON *root = NULL; From 29b6643bab23a5ec2dc0eedbc848de4ee8445b18 Mon Sep 17 00:00:00 2001 From: Stephan Gatzka Date: Sun, 27 Nov 2016 18:22:12 +0100 Subject: [PATCH 7/7] Warn about missing prototypes. This switch warns about missing prototypes. It does not warn if functions are declared static. So this switch encourages the usage of static whenever possible. This enables the compiler to perform inlining. --- CMakeLists.txt | 2 +- Makefile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70c1d86a..3bf7cf8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,7 @@ set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT option(ENABLE_CUSTOM_COMPILER_FLAGS "Enables custom compiler flags for Clang and GCC" ON) if (ENABLE_CUSTOM_COMPILER_FLAGS) if(("${CMAKE_C_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes") endif() endif() diff --git a/Makefile b/Makefile index e4b8be9e..9b1c63e6 100644 --- a/Makefile +++ b/Makefile @@ -20,14 +20,14 @@ INSTALL_LIBRARY_PATH = $(DESTDIR)$(PREFIX)/$(LIBRARY_PATH) INSTALL ?= cp -a -R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 $(CFLAGS) +R_CFLAGS = -fPIC -std=c89 -pedantic -Wall -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes $(CFLAGS) uname := $(shell sh -c 'uname -s 2>/dev/null || echo false') #library file extensions SHARED = so STATIC = a - + ## create dynamic (shared) library on Darwin (base OS for MacOSX and IOS) ifeq (Darwin, $(uname)) SHARED = dylib