From b6d8889163bceccd5f35148da0acd2d0edc46be4 Mon Sep 17 00:00:00 2001 From: Andy MacGregor Date: Thu, 14 Mar 2024 10:06:05 -0400 Subject: [PATCH 1/2] Fix #12515: FP misra-6.1 on bool bitfields in >c99 --- addons/misra.py | 2 +- addons/test/misra/misra-test.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/addons/misra.py b/addons/misra.py index 662d66d8a64..fcdca17e766 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1853,7 +1853,7 @@ def misra_6_1(self, data): if data.standards.c == 'c89': if token.valueType.type != 'int' and not isUnsignedType(token.variable.typeStartToken.str): self.reportError(token, 6, 1) - elif data.standards.c == 'c99': + elif data.standards.c in ('c99', 'c11', 'c17', 'c18'): if token.valueType.type == 'bool': continue diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 7ec3d12940b..bfe9a562591 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -273,6 +273,7 @@ struct struct_with_bitfields unsigned int g:1; // Compliant signed int h:1; // 6.2 - signed int with size 1 is not compliant uint16_t i:1; // Compliant + bool j:1; // 6.1 - bool not legal until c99 }; static void misra6_1_fn(void) { From 08dcdbe986cb7d85a7fd9574bebc7c0f85b04cf5 Mon Sep 17 00:00:00 2001 From: Andy MacGregor Date: Thu, 14 Mar 2024 10:44:30 -0400 Subject: [PATCH 2/2] Add tests for MISRA tests in --std=c11 to test #12515 --- .github/workflows/CI-unixish.yml | 3 +++ .github/workflows/CI-windows.yml | 3 +++ addons/test/misra/misra-test-c11.c | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 addons/test/misra/misra-test-c11.c diff --git a/.github/workflows/CI-unixish.yml b/.github/workflows/CI-unixish.yml index f0393093701..2f7dd0de492 100644 --- a/.github/workflows/CI-unixish.yml +++ b/.github/workflows/CI-unixish.yml @@ -465,6 +465,9 @@ jobs: # rules 5.4 and 5.5 which have standard-dependent options. ../../cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test.c --std=c89 --platform=unix64 python3 ../misra.py -verify misra/misra-test.c.dump + # Test slight MISRA differences in C11 standard + ../../cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test-c11.c --std=c11 --platform=unix64 + python3 ../misra.py -verify misra/misra-test-c11.c.dump # TODO: do we need to verify something here? ../../cppcheck --dump -DDUMMY --suppress=uninitvar --suppress=uninitStructMember --std=c89 misra/misra-test.h ../../cppcheck --dump misra/misra-test.cpp diff --git a/.github/workflows/CI-windows.yml b/.github/workflows/CI-windows.yml index 3d0db730702..5ed1648e770 100644 --- a/.github/workflows/CI-windows.yml +++ b/.github/workflows/CI-windows.yml @@ -200,6 +200,9 @@ jobs: rem rules 5.4 and 5.5 which have standard-dependent options. ..\..\cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra\misra-test.c --std=c89 --platform=unix64 || exit /b !errorlevel! python3 ..\misra.py -verify misra\misra-test.c.dump || exit /b !errorlevel! + rem Test slight MISRA differences in C11 standard + ..\..\cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra\misra-test-c11.c --std=c11 --platform=unix64 || exit /b !errorlevel! + python3 ..\misra.py -verify misra\misra-test-c11.c.dump || exit /b !errorlevel! rem TODO: do we need to verify something here? ..\..\cppcheck --dump -DDUMMY --suppress=uninitvar --suppress=uninitStructMember --std=c89 misra\misra-test.h || exit /b !errorlevel! ..\..\cppcheck --dump misra\misra-test.cpp || exit /b !errorlevel! diff --git a/addons/test/misra/misra-test-c11.c b/addons/test/misra/misra-test-c11.c new file mode 100644 index 00000000000..031bc361e5f --- /dev/null +++ b/addons/test/misra/misra-test-c11.c @@ -0,0 +1,25 @@ +// To test: +// ~/cppcheck/cppcheck --dump misra/misra-test-c11.c --std=c11 +// ~/cppcheck/cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test-c11.c --std=c11 --platform=unix64 && python3 ../misra.py -verify misra/misra-test-c11.c.dump + +#include + +typedef unsigned int UINT_TYPEDEF; +struct struct_with_bitfields +{ + unsigned int a:2; // Compliant + signed int b:2; // Compliant + UINT_TYPEDEF c:2; // Compliant + int d:2; // 6.1 - plain int not compliant + signed long f:2; // Compliant in c99 or later - explicitly signed integer type + unsigned int g:1; // Compliant + signed int h:1; // 6.2 - signed int with size 1 is not compliant + uint16_t i:1; // Compliant + bool j:1; // Compliant in C99 or later +}; + +static void misra6_1_fn(void) { + // "Use" occurrence should not generate warnings + struct_with_bitfields s; + s.h = 61; +}