From f8c350fc700efc2ce1735e91d99794bea54584da Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Mon, 1 Jul 2019 08:00:43 +0300 Subject: [PATCH] misra.py: Add R18.7 check (#1934) * misra.py: Add 18.7 check * Simplify R18.7 check * use token.link * Remove isStruct method --- addons/misra.py | 19 +++++++++++++++++++ addons/test/misra-test.c | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/addons/misra.py b/addons/misra.py index 8c5f99d42..bec397672 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1628,6 +1628,24 @@ class MisraChecker: self.reportError(var.nameToken, 18, 5) + def misra_18_7(self, data): + for scope in data.scopes: + if scope.type != 'Struct': + continue + + token = scope.bodyStart.next + while token != scope.bodyEnd and token is not None: + # Handle nested structures to not duplicate an error. + if token.str == '{': + token = token.link + + if cppcheckdata.simpleMatch(token, "[ ]"): + self.reportError(token, 18, 7) + break + token = token.next + + + def misra_18_8(self, data): for var in data.variables: if not var.isArray or not var.isLocal: @@ -2287,6 +2305,7 @@ class MisraChecker: self.misra_17_7(cfg) self.misra_17_8(cfg) self.misra_18_5(cfg) + self.misra_18_7(cfg) self.misra_18_8(cfg) self.misra_19_2(cfg) self.misra_20_1(cfg) diff --git a/addons/test/misra-test.c b/addons/test/misra-test.c index 0fd22a59f..bf12361e0 100644 --- a/addons/test/misra-test.c +++ b/addons/test/misra-test.c @@ -507,6 +507,25 @@ void misra_18_5() { int *** p; // 18.5 } +struct { + uint16_t len; + struct { + uint8_t data_1[]; // 18.7 + } nested_1; + struct named { + struct { + uint8_t len_1; + uint32_t data_2[]; // 18.7 + } nested_2; + uint8_t data_3[]; // 18.7 + } nested_3; +} _18_7_struct; +struct { + uint16_t len; + uint8_t data_1[ 19 ]; + uint8_t data_2[ ]; // 18.7 +} _18_7_struct; + void misra_18_8(int x) { int buf1[10]; int buf2[sizeof(int)];