From 7edf9d9ac538785deb9f469a6f60dbd567f52435 Mon Sep 17 00:00:00 2001 From: Fabian Zahn Date: Sat, 27 Feb 2021 08:53:53 +0100 Subject: [PATCH 1/7] Fix #510 (-Wextra-semi-stmt with clang compiler) --- CMakeLists.txt | 1 + src/unity.c | 16 +++++++++------- src/unity.h | 2 +- src/unity_internals.h | 24 ++++++++++++------------ 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a16cdc..59b2ab2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,6 +103,7 @@ target_compile_options(${PROJECT_NAME} -Wcast-qual -Wconversion -Wexit-time-destructors + -Wextra-semi-stmt -Wglobal-constructors -Wmissing-noreturn -Wmissing-prototypes diff --git a/src/unity.c b/src/unity.c index be3528f..2d32853 100644 --- a/src/unity.c +++ b/src/unity.c @@ -19,9 +19,9 @@ void UNITY_OUTPUT_CHAR(int); #endif /* Helpful macros for us to use here in Assert functions */ -#define UNITY_FAIL_AND_BAIL { Unity.CurrentTestFailed = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); } -#define UNITY_IGNORE_AND_BAIL { Unity.CurrentTestIgnored = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); } -#define RETURN_IF_FAIL_OR_IGNORE if (Unity.CurrentTestFailed || Unity.CurrentTestIgnored) TEST_ABORT() +#define UNITY_FAIL_AND_BAIL do { Unity.CurrentTestFailed = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); } while (0) +#define UNITY_IGNORE_AND_BAIL do { Unity.CurrentTestIgnored = 1; UNITY_OUTPUT_FLUSH(); TEST_ABORT(); } while (0) +#define RETURN_IF_FAIL_OR_IGNORE do { if (Unity.CurrentTestFailed || Unity.CurrentTestIgnored) { TEST_ABORT(); } } while (0) struct UNITY_STORAGE_T Unity; @@ -765,11 +765,12 @@ void UnityAssertGreaterOrLessOrEqualNumber(const UNITY_INT threshold, } #define UnityPrintPointlessAndBail() \ -{ \ +do { \ UnityTestResultsFailBegin(lineNumber); \ UnityPrint(UnityStrPointless); \ UnityAddMsgIfSpecified(msg); \ - UNITY_FAIL_AND_BAIL; } + UNITY_FAIL_AND_BAIL; \ +} while (0) /*-----------------------------------------------*/ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, @@ -884,11 +885,12 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, #ifndef UNITY_EXCLUDE_FLOAT_PRINT #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \ - { \ + do { \ UnityPrint(UnityStrExpected); \ UnityPrintFloat(expected); \ UnityPrint(UnityStrWas); \ - UnityPrintFloat(actual); } + UnityPrintFloat(actual); \ + } while (0) #else #define UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual) \ UnityPrint(UnityStrDelta) diff --git a/src/unity.h b/src/unity.h index ab986c4..7f3db26 100644 --- a/src/unity.h +++ b/src/unity.h @@ -111,7 +111,7 @@ void verifyTest(void); /* It is not necessary for you to call PASS. A PASS condition is assumed if nothing fails. * This method allows you to abort a test immediately with a PASS state, ignoring the remainder of the test. */ #define TEST_PASS() TEST_ABORT() -#define TEST_PASS_MESSAGE(message) do { UnityMessage((message), __LINE__); TEST_ABORT(); } while(0) +#define TEST_PASS_MESSAGE(message) do { UnityMessage((message), __LINE__); TEST_ABORT(); } while (0) /* This macro does nothing, but it is useful for build tools (like Ceedling) to make use of this to figure out * which files should be linked to in order to perform a test. Use it like TEST_FILE("sandwiches.c") */ diff --git a/src/unity_internals.h b/src/unity_internals.h index b86fefa..08c876f 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -273,10 +273,10 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; #ifdef UNITY_USE_FLUSH_STDOUT /* We want to use the stdout flush utility */ #include - #define UNITY_OUTPUT_FLUSH() (void)fflush(stdout) + #define UNITY_OUTPUT_FLUSH() (void)fflush(stdout) #else /* We've specified nothing, therefore flush should just be ignored */ - #define UNITY_OUTPUT_FLUSH() + #define UNITY_OUTPUT_FLUSH() (void)0 #endif #else /* If defined as something else, make sure we declare it here so it's ready for use */ @@ -288,11 +288,11 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; #ifndef UNITY_OUTPUT_FLUSH #define UNITY_FLUSH_CALL() #else -#define UNITY_FLUSH_CALL() UNITY_OUTPUT_FLUSH() +#define UNITY_FLUSH_CALL() UNITY_OUTPUT_FLUSH() #endif #ifndef UNITY_PRINT_EOL -#define UNITY_PRINT_EOL() UNITY_OUTPUT_CHAR('\n') +#define UNITY_PRINT_EOL() UNITY_OUTPUT_CHAR('\n') #endif #ifndef UNITY_OUTPUT_START @@ -351,19 +351,19 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; #endif #ifndef UNITY_EXEC_TIME_START -#define UNITY_EXEC_TIME_START() do{}while(0) +#define UNITY_EXEC_TIME_START() do { /* nothing*/ } while (0) #endif #ifndef UNITY_EXEC_TIME_STOP -#define UNITY_EXEC_TIME_STOP() do{}while(0) +#define UNITY_EXEC_TIME_STOP() do { /* nothing*/ } while (0) #endif #ifndef UNITY_TIME_TYPE -#define UNITY_TIME_TYPE UNITY_UINT +#define UNITY_TIME_TYPE UNITY_UINT #endif #ifndef UNITY_PRINT_EXEC_TIME -#define UNITY_PRINT_EXEC_TIME() do{}while(0) +#define UNITY_PRINT_EXEC_TIME() do { /* nothing*/ } while (0) #endif /*------------------------------------------------------- @@ -502,9 +502,9 @@ void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int #define UNITY_SET_DETAIL(d1) #define UNITY_SET_DETAILS(d1,d2) #else -#define UNITY_CLR_DETAILS() { Unity.CurrentDetail1 = 0; Unity.CurrentDetail2 = 0; } -#define UNITY_SET_DETAIL(d1) { Unity.CurrentDetail1 = (d1); Unity.CurrentDetail2 = 0; } -#define UNITY_SET_DETAILS(d1,d2) { Unity.CurrentDetail1 = (d1); Unity.CurrentDetail2 = (d2); } +#define UNITY_CLR_DETAILS() do { Unity.CurrentDetail1 = 0; Unity.CurrentDetail2 = 0; } while (0) +#define UNITY_SET_DETAIL(d1) do { Unity.CurrentDetail1 = (d1); Unity.CurrentDetail2 = 0; } while (0) +#define UNITY_SET_DETAILS(d1,d2) do { Unity.CurrentDetail1 = (d1); Unity.CurrentDetail2 = (d2); } while (0) #ifndef UNITY_DETAIL1_NAME #define UNITY_DETAIL1_NAME "Function" @@ -772,7 +772,7 @@ int UnityTestMatches(void); * Test Asserts *-------------------------------------------------------*/ -#define UNITY_TEST_ASSERT(condition, line, message) do {if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));}} while(0) +#define UNITY_TEST_ASSERT(condition, line, message) do { if (condition) { /* nothing*/ } else { UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message)); } } while (0) #define UNITY_TEST_ASSERT_NULL(pointer, line, message) UNITY_TEST_ASSERT(((pointer) == NULL), (UNITY_LINE_TYPE)(line), (message)) #define UNITY_TEST_ASSERT_NOT_NULL(pointer, line, message) UNITY_TEST_ASSERT(((pointer) != NULL), (UNITY_LINE_TYPE)(line), (message)) #define UNITY_TEST_ASSERT_EMPTY(pointer, line, message) UNITY_TEST_ASSERT(((pointer[0]) == 0), (UNITY_LINE_TYPE)(line), (message)) From fa32e530ba1aecc37113067714d29459d12bbdec Mon Sep 17 00:00:00 2001 From: Fabian Zahn Date: Sat, 27 Feb 2021 10:49:34 +0100 Subject: [PATCH 2/7] Remove "error: assuming signed overflow does not occur when reducing constant in comparison [-Werror=strict-overflow]" --- src/unity.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/unity.c b/src/unity.c index 2d32853..b880248 100644 --- a/src/unity.c +++ b/src/unity.c @@ -369,10 +369,12 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number) } else { - UNITY_INT32 n_int = 0, n; - int exponent = 0; - int decimals, digits; - char buf[16] = {0}; + UNITY_INT32 n_int = 0; + UNITY_INT32 n; + int exponent = 0; + int decimals; + int digits; + char buf[16] = {0}; /* * Scale up or down by powers of 10. To minimize rounding error, @@ -445,14 +447,19 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number) /* build up buffer in reverse order */ digits = 0; - while ((n != 0) || (digits < (decimals + 1))) + while ((n != 0) || (digits <= decimals)) { buf[digits++] = (char)('0' + n % 10); n /= 10; } + + /* print out buffer (backwards) */ while (digits > 0) { - if (digits == decimals) { UNITY_OUTPUT_CHAR('.'); } + if (digits == decimals) + { + UNITY_OUTPUT_CHAR('.'); + } UNITY_OUTPUT_CHAR(buf[--digits]); } From 4cfb39290ad8e9f81ddc9da6abe5bb65aef4d641 Mon Sep 17 00:00:00 2001 From: Fabian Zahn Date: Sat, 27 Feb 2021 11:14:43 +0100 Subject: [PATCH 3/7] Refactor generator expressions for CMake --- CMakeLists.txt | 75 +++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 59b2ab2..766a5bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,38 +99,49 @@ set_target_properties(${PROJECT_NAME} target_compile_options(${PROJECT_NAME} PRIVATE - $<$:-Wcast-align - -Wcast-qual - -Wconversion - -Wexit-time-destructors - -Wextra-semi-stmt - -Wglobal-constructors - -Wmissing-noreturn - -Wmissing-prototypes - -Wno-missing-braces - -Wold-style-cast - -Wshadow - -Wweak-vtables - -Werror - -Wall> - $<$:-Waddress - -Waggregate-return - -Wformat-nonliteral - -Wformat-security - -Wformat - -Winit-self - -Wmissing-declarations - -Wmissing-include-dirs - -Wno-multichar - -Wno-parentheses - -Wno-type-limits - -Wno-unused-parameter - -Wunreachable-code - -Wwrite-strings - -Wpointer-arith - -Werror - -Wall> - $<$:/Wall> + # Clang + $<$: + -Wcast-align + -Wcast-qual + -Wconversion + -Wexit-time-destructors + -Wglobal-constructors + -Wmissing-noreturn + -Wmissing-prototypes + -Wno-missing-braces + -Wold-style-cast + -Wshadow + -Wweak-vtables + -Werror + -Wall + $<$,8.0.0>:-Wextra-semi-stmt> + > + + # GCC + $<$: + -Waddress + -Waggregate-return + -Wformat-nonliteral + -Wformat-security + -Wformat + -Winit-self + -Wmissing-declarations + -Wmissing-include-dirs + -Wno-multichar + -Wno-parentheses + -Wno-type-limits + -Wno-unused-parameter + -Wunreachable-code + -Wwrite-strings + -Wpointer-arith + -Werror + -Wall + > + + # MSVC + $<$: + /Wall + > ) write_basic_package_version_file(${PROJECT_NAME}ConfigVersion.cmake From 8e1e9c18ab3067a001eb2c5196103ad7bdde5cf1 Mon Sep 17 00:00:00 2001 From: Erik Flodin Date: Thu, 15 Apr 2021 22:22:33 +0200 Subject: [PATCH 4/7] Fix ruby style warnings as reported by rubocop --- auto/generate_module.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/auto/generate_module.rb b/auto/generate_module.rb index 0a88bec..8e33d95 100644 --- a/auto/generate_module.rb +++ b/auto/generate_module.rb @@ -170,16 +170,16 @@ class UnityModuleGenerator ############################ def neutralize_filename(name, start_cap = true) return name if name.empty? - name = name.split(/(?:\s+|_|(?=[A-Z][a-z]))|(?<=[a-z])(?=[A-Z])/).map { |v| v.capitalize }.join('_') + name = name.split(/(?:\s+|_|(?=[A-Z][a-z]))|(?<=[a-z])(?=[A-Z])/).map(&:capitalize).join('_') name = name[0].downcase + name[1..-1] unless start_cap - return name + name end ############################ def create_filename(part1, part2 = '') name = part2.empty? ? part1 : part1 + '_' + part2 case (@options[:naming]) - when 'bumpy' then neutralize_filename(name,false).delete('_') + when 'bumpy' then neutralize_filename(name, false).delete('_') when 'camel' then neutralize_filename(name).delete('_') when 'snake' then neutralize_filename(name).downcase when 'caps' then neutralize_filename(name).upcase @@ -211,8 +211,8 @@ class UnityModuleGenerator f.write("#{file[:boilerplate]}\n" % [file[:name]]) unless file[:boilerplate].nil? f.write(file[:template] % [file[:name], file[:includes].map { |ff| "#include \"#{ff}\"\n" }.join, - file[:name].upcase.gsub(/-/, '_'), - file[:name].gsub(/-/, '_')]) + file[:name].upcase.tr('-', '_'), + file[:name].tr('-', '_')]) end if @options[:update_svn] `svn add \"#{file[:path]}\"` From dc96c3e6ddda456d6ed3bbf097213b5aad7f402b Mon Sep 17 00:00:00 2001 From: Erik Flodin Date: Sat, 17 Apr 2021 19:00:06 +0200 Subject: [PATCH 5/7] Fix strict-overflow compiler warning By replacing "x < y + 1" with "x <= y" the compiler doesn't have to do it and the warning "assuming signed overflow does not occur when reducing constant in comparison [-Werror=strict-overflow]" is avoided. --- src/unity.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unity.c b/src/unity.c index be3528f..764a42b 100644 --- a/src/unity.c +++ b/src/unity.c @@ -445,7 +445,7 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number) /* build up buffer in reverse order */ digits = 0; - while ((n != 0) || (digits < (decimals + 1))) + while ((n != 0) || (digits <= decimals)) { buf[digits++] = (char)('0' + n % 10); n /= 10; From f98e2c868fa3a6eca5504e62fae6495d7d337b61 Mon Sep 17 00:00:00 2001 From: "Andres O. Vela" Date: Mon, 20 Sep 2021 10:49:08 +0200 Subject: [PATCH 6/7] Fix typo in CMakeLists.txt --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a16cdc..55417a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,11 +49,11 @@ set(UNITY_EXTENSION_FIXTURE_ENABLED $) set(UNITY_EXTENSION_MEMORY_ENABLED $>) if(${UNITY_EXTENSION_FIXTURE}) - message(STATUS "Unity: Bulding with the fixture extension.") + message(STATUS "Unity: Building with the fixture extension.") endif() if(${UNITY_EXTENSION_MEMORY}) - message(STATUS "Unity: Bulding with the memory extension.") + message(STATUS "Unity: Building with the memory extension.") endif() # Main target ------------------------------------------------------------------ From 13e40e84eec7fe513b7b5eab9c7f36d315286588 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 3 Dec 2021 11:43:50 +0100 Subject: [PATCH 7/7] extras/fixture: add missing C++ include guards This fixes linking errors when test cases based on Unity fixture are defined in a .cpp file. unity_internals.h doesn't have C++ guards, and is included from unity.h from within C++ header guard block. Same approach is taken in this commit --- extras/fixture/src/unity_fixture.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/extras/fixture/src/unity_fixture.h b/extras/fixture/src/unity_fixture.h index 4cc403e..6575066 100644 --- a/extras/fixture/src/unity_fixture.h +++ b/extras/fixture/src/unity_fixture.h @@ -9,13 +9,20 @@ #define UNITY_FIXTURE_H_ #include "unity.h" -#include "unity_internals.h" #include "unity_fixture_internals.h" #ifndef UNITY_FIXTURE_NO_EXTRAS #include "unity_memory.h" #endif +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "unity_internals.h" + + int UnityMain(int argc, const char* argv[], void (*runAllTests)(void)); @@ -80,4 +87,8 @@ int UnityMain(int argc, const char* argv[], void (*runAllTests)(void)); #define DOUBLES_EQUAL(expected, actual, delta) TEST_ASSERT_DOUBLE_WITHIN((delta), (expected), (actual)) #endif +#ifdef __cplusplus +} +#endif + #endif /* UNITY_FIXTURE_H_ */