From c546414657673c9e0bef646ee33e7005d180a85b Mon Sep 17 00:00:00 2001 From: Mark VanderVoord Date: Thu, 1 Aug 2024 16:01:09 -0400 Subject: [PATCH] - Protect against people not defining UNITY_USE_COMMAND)LINES_ARGS but enabling cmd_lines in test runner generator. (Cherry-pick PR 739) - Fix UNITY_NORETURN usage (Cherry-pick PR 742) - Other standards and formatting tweaks. --- auto/generate_test_runner.rb | 2 + src/unity_internals.h | 71 ++++++++++++++++++------------ test/Makefile | 1 + test/tests/self_assessment_utils.h | 10 ++--- test/tests/test_unity_core.c | 7 +-- 5 files changed, 54 insertions(+), 37 deletions(-) diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index 2f95fd2b..45f0425a 100755 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -402,6 +402,7 @@ def create_main(output, filename, tests, used_mocks) end output.puts("#{@options[:main_export_decl]} int #{main_name}(int argc, char** argv)") output.puts('{') + output.puts('#ifdef UNITY_USE_COMMAND_LINE_ARGS') output.puts(' int parse_status = UnityParseOptions(argc, argv);') output.puts(' if (parse_status != 0)') output.puts(' {') @@ -424,6 +425,7 @@ def create_main(output, filename, tests, used_mocks) output.puts(' }') output.puts(' return parse_status;') output.puts(' }') + output.puts('#endif') else main_return = @options[:omit_begin_end] ? 'void' : 'int' if main_name != 'main' diff --git a/src/unity_internals.h b/src/unity_internals.h index ca962af1..192f7ffa 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -47,38 +47,51 @@ #define UNITY_FUNCTION_ATTR(a) /* ignore */ #endif -#ifndef UNITY_NORETURN - #if defined(__cplusplus) - #if __cplusplus >= 201103L +/* UNITY_NORETURN is only required if we have setjmp.h. */ +#ifndef UNITY_EXCLUDE_SETJMP_H + #ifndef UNITY_NORETURN + #if defined(__cplusplus) + #if __cplusplus >= 201103L + #define UNITY_NORETURN [[ noreturn ]] + #endif + #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && __STDC_VERSION__ < 202311L + /* _Noreturn keyword is used from C11 but deprecated in C23. */ + #if defined(_WIN32) && defined(_MSC_VER) + /* We are using MSVC compiler on Windows platform. */ + /* Not all Windows SDKs supports , but compiler can support C11: */ + /* https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/ */ + /* Not sure, that Mingw compilers has Windows SDK headers at all. */ + #include + #endif + + /* Using Windows SDK predefined macro for detecting supported SDK with MSVC compiler. */ + /* Mingw GCC should work without that fixes. */ + /* Based on: */ + /* https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt?view=msvc-170 */ + /* NTDDI_WIN10_FE is equal to Windows 10 SDK 2104 */ + #if defined(_MSC_VER) && ((!defined(NTDDI_WIN10_FE)) || WDK_NTDDI_VERSION < NTDDI_WIN10_FE) + /* Based on tests and: */ + /* https://docs.microsoft.com/en-us/cpp/c-language/noreturn?view=msvc-170 */ + /* https://en.cppreference.com/w/c/language/_Noreturn */ + #define UNITY_NORETURN _Noreturn + #else /* Using newer Windows SDK or not MSVC compiler */ + #if defined(__GNUC__) + /* The header collides with __attribute(noreturn)__ from GCC. */ + #define UNITY_NORETURN _Noreturn + #else + #include + #define UNITY_NORETURN noreturn + #endif + #endif + #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L + /* Since C23, the keyword _Noreturn has been replaced by the attribute noreturn, based on: */ + /* https://en.cppreference.com/w/c/language/attributes/noreturn */ #define UNITY_NORETURN [[ noreturn ]] #endif - #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L - #if defined(_WIN32) && defined(_MSC_VER) - /* We are using MSVC compiler on Windows platform. */ - /* Not all Windows SDKs supports , but compiler can support C11: */ - /* https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/ */ - /* Not sure, that Mingw compilers has Windows SDK headers at all. */ - #include - #endif - - /* Using Windows SDK predefined macro for detecting supported SDK with MSVC compiler. */ - /* Mingw GCC should work without that fixes. */ - /* Based on: */ - /* https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt?view=msvc-170 */ - /* NTDDI_WIN10_FE is equal to Windows 10 SDK 2104 */ - #if defined(_MSC_VER) && ((!defined(NTDDI_WIN10_FE)) || WDK_NTDDI_VERSION < NTDDI_WIN10_FE) - /* Based on tests and: */ - /* https://docs.microsoft.com/en-us/cpp/c-language/noreturn?view=msvc-170 */ - /* https://en.cppreference.com/w/c/language/_Noreturn */ - #define UNITY_NORETURN _Noreturn - #else /* Using newer Windows SDK or not MSVC compiler */ - #include - #define UNITY_NORETURN noreturn - #endif #endif -#endif -#ifndef UNITY_NORETURN - #define UNITY_NORETURN UNITY_FUNCTION_ATTR(__noreturn__) + #ifndef UNITY_NORETURN + #define UNITY_NORETURN UNITY_FUNCTION_ATTR(__noreturn__) + #endif #endif /*------------------------------------------------------- diff --git a/test/Makefile b/test/Makefile index 917a8749..82b61af9 100644 --- a/test/Makefile +++ b/test/Makefile @@ -17,6 +17,7 @@ CFLAGS += -std=c99 -pedantic -Wall -Wextra -Werror #CFLAGS += -Wconversion #disabled because if falsely complains about the isinf and isnan macros CFLAGS += -Wno-switch-enum -Wno-double-promotion CFLAGS += -Wno-poison-system-directories +CFLAGS += -Wno-covered-switch-default CFLAGS += -Wbad-function-cast -Wcast-qual -Wold-style-definition -Wshadow -Wstrict-overflow \ -Wstrict-prototypes -Wswitch-default -Wundef #DEBUG = -O0 -g diff --git a/test/tests/self_assessment_utils.h b/test/tests/self_assessment_utils.h index 670dddc8..39953e36 100644 --- a/test/tests/self_assessment_utils.h +++ b/test/tests/self_assessment_utils.h @@ -69,8 +69,8 @@ static const UNITY_DOUBLE d_zero = 0.0; #define SPY_BUFFER_MAX 40 static char putcharSpyBuffer[SPY_BUFFER_MAX]; #endif -static int indexSpyBuffer; -static int putcharSpyEnabled; +static UNITY_COUNTER_TYPE indexSpyBuffer; +static UNITY_COUNTER_TYPE putcharSpyEnabled; void startPutcharSpy(void) { @@ -108,8 +108,8 @@ void putcharSpy(int c) } /* This is for counting the calls to the flushSpy */ -static int flushSpyEnabled; -static int flushSpyCalls = 0; +static UNITY_COUNTER_TYPE flushSpyEnabled; +static UNITY_COUNTER_TYPE flushSpyCalls = 0; void startFlushSpy(void) { @@ -123,7 +123,7 @@ void endFlushSpy(void) flushSpyEnabled = 0; } -int getFlushSpyCalls(void) +UNITY_COUNTER_TYPE getFlushSpyCalls(void) { return flushSpyCalls; } diff --git a/test/tests/test_unity_core.c b/test/tests/test_unity_core.c index 6638fb94..e4a3fdd9 100644 --- a/test/tests/test_unity_core.c +++ b/test/tests/test_unity_core.c @@ -293,8 +293,9 @@ void testFailureCountIncrementsAndIsReturnedAtEnd(void) #ifndef USING_OUTPUT_SPY TEST_IGNORE(); #else - UNITY_UINT savedGetFlushSpyCalls = 0; - UNITY_UINT savedFailures = Unity.TestFailures; + int failures = 0; + UNITY_COUNTER_TYPE savedGetFlushSpyCalls = 0; + UNITY_COUNTER_TYPE savedFailures = Unity.TestFailures; Unity.CurrentTestFailed = 1; startPutcharSpy(); /* Suppress output */ startFlushSpy(); @@ -311,7 +312,7 @@ void testFailureCountIncrementsAndIsReturnedAtEnd(void) endFlushSpy(); startPutcharSpy(); /* Suppress output */ - int failures = UnityEnd(); + failures = UnityEnd(); Unity.TestFailures--; endPutcharSpy(); TEST_ASSERT_EQUAL(savedFailures + 1, failures);