From ab840bdfd9173805bae624809f90409e22991868 Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Sat, 29 Jun 2019 20:16:15 +0300 Subject: [PATCH 1/4] misra.py: Add 18.7 check --- addons/misra.py | 40 +++++++++++++++++++++++++++++++++++++++ addons/test/misra-test.c | 18 ++++++++++++++++++ addons/test/test-misra.py | 7 +++++++ 3 files changed, 65 insertions(+) diff --git a/addons/misra.py b/addons/misra.py index 8c5f99d4227..a327c389a82 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -468,6 +468,16 @@ def isNoReturnScope(tok): return False +def isStruct(token): + if token.valueType is None: + return False + if token.valueType.typeScope is None: + return False + if token.valueType.typeScope.type != "Struct": + return False + return True + + class Define: def __init__(self, directive): self.args = [] @@ -1628,6 +1638,35 @@ def misra_18_5(self, data): self.reportError(var.nameToken, 18, 5) + def misra_18_7(self, data): + structScopes = [] + for token in data.tokenlist: + if not isStruct(token): + continue + structScopes.append(token.valueType.typeScope) + + for scope in structScopes: + token = scope.bodyStart.next + while token != scope.bodyEnd and token is not None: + # Handle nested structures to not duplicate an error. + if token.str == 'struct': + while not isStruct(token): + token = token.next + nestedStructScope = token.valueType.typeScope + while not nestedStructScope.nestedInId != scope.Id: + token = token.next + if isStruct(token): + nestedStructScope = token.valueType.typeScope + token = nestedStructScope.bodyEnd.next.next.next + continue + + if token.str == '[' and token.next.str == ']': + 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 +2326,7 @@ def parseDump(self, dumpfile): 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 0fd22a59fd4..aa6b7e783b3 100644 --- a/addons/test/misra-test.c +++ b/addons/test/misra-test.c @@ -507,6 +507,24 @@ 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[]; // 18.7 +} _18_7_struct; + void misra_18_8(int x) { int buf1[10]; int buf2[sizeof(int)]; diff --git a/addons/test/test-misra.py b/addons/test/test-misra.py index e686000c678..7abc689bebc 100644 --- a/addons/test/test-misra.py +++ b/addons/test/test-misra.py @@ -137,3 +137,10 @@ def test_rules_cppcheck_severity(checker): assert("(error)" not in captured) assert("(warning)" not in captured) assert("(style)" in captured) + + +def test_misra_18_7(checker): + with CapturingStderr() as output: + checker.parseDump("./addons/test/misra-test.c.dump") + captured = ''.join(output.captured) + assert(captured.count("[misra-c2012-18.7]") == 4) From adeaf9b3d1bc8acb3db0295e69843ca1d70bc817 Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Sun, 30 Jun 2019 11:07:22 +0300 Subject: [PATCH 2/4] Simplify R18.7 check --- addons/misra.py | 9 +++------ addons/test/misra-test.c | 3 ++- addons/test/test-misra.py | 7 ------- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index a327c389a82..d13ca0ffc4c 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1639,13 +1639,10 @@ def misra_18_5(self, data): def misra_18_7(self, data): - structScopes = [] - for token in data.tokenlist: - if not isStruct(token): + for scope in data.scopes: + if scope.type != 'Struct': continue - structScopes.append(token.valueType.typeScope) - for scope in structScopes: token = scope.bodyStart.next while token != scope.bodyEnd and token is not None: # Handle nested structures to not duplicate an error. @@ -1660,7 +1657,7 @@ def misra_18_7(self, data): token = nestedStructScope.bodyEnd.next.next.next continue - if token.str == '[' and token.next.str == ']': + if cppcheckdata.simpleMatch(token, "[ ]"): self.reportError(token, 18, 7) break token = token.next diff --git a/addons/test/misra-test.c b/addons/test/misra-test.c index aa6b7e783b3..bf12361e028 100644 --- a/addons/test/misra-test.c +++ b/addons/test/misra-test.c @@ -522,7 +522,8 @@ struct { } _18_7_struct; struct { uint16_t len; - uint8_t data[]; // 18.7 + uint8_t data_1[ 19 ]; + uint8_t data_2[ ]; // 18.7 } _18_7_struct; void misra_18_8(int x) { diff --git a/addons/test/test-misra.py b/addons/test/test-misra.py index 7abc689bebc..e686000c678 100644 --- a/addons/test/test-misra.py +++ b/addons/test/test-misra.py @@ -137,10 +137,3 @@ def test_rules_cppcheck_severity(checker): assert("(error)" not in captured) assert("(warning)" not in captured) assert("(style)" in captured) - - -def test_misra_18_7(checker): - with CapturingStderr() as output: - checker.parseDump("./addons/test/misra-test.c.dump") - captured = ''.join(output.captured) - assert(captured.count("[misra-c2012-18.7]") == 4) From e87b61a2295964c1e6918c2260b2a6744afa80c2 Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Sun, 30 Jun 2019 22:38:13 +0300 Subject: [PATCH 3/4] use token.link --- addons/misra.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index d13ca0ffc4c..6ca2e63d33d 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1646,16 +1646,8 @@ def misra_18_7(self, data): token = scope.bodyStart.next while token != scope.bodyEnd and token is not None: # Handle nested structures to not duplicate an error. - if token.str == 'struct': - while not isStruct(token): - token = token.next - nestedStructScope = token.valueType.typeScope - while not nestedStructScope.nestedInId != scope.Id: - token = token.next - if isStruct(token): - nestedStructScope = token.valueType.typeScope - token = nestedStructScope.bodyEnd.next.next.next - continue + if token.str == '{': + token = token.link if cppcheckdata.simpleMatch(token, "[ ]"): self.reportError(token, 18, 7) From b1f3287270b7edda4621afc9b6097e1c852309a2 Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Mon, 1 Jul 2019 07:16:37 +0300 Subject: [PATCH 4/4] Remove isStruct method --- addons/misra.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index 6ca2e63d33d..bec39767284 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -468,16 +468,6 @@ def isNoReturnScope(tok): return False -def isStruct(token): - if token.valueType is None: - return False - if token.valueType.typeScope is None: - return False - if token.valueType.typeScope.type != "Struct": - return False - return True - - class Define: def __init__(self, directive): self.args = []