From f5fe5620509eed70bc309c15b41224e28909a3cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 21 Jul 2021 10:56:17 +0200 Subject: [PATCH] misra; improved checking of 10.3 --- addons/misra.py | 23 +++++++++++++++++++---- addons/test/misra/misra-test.c | 7 ++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index ca8927a9f..34dc6ef4c 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -488,7 +488,7 @@ def getEssentialType(expr): if expr.variable or isCast(expr): if expr.valueType: if expr.valueType.type == 'bool': - return 'Boolean' + return 'bool' if expr.valueType.isFloat(): return expr.valueType.type if expr.valueType.isIntegral(): @@ -498,7 +498,7 @@ def getEssentialType(expr): # Appendix D, D.6 The essential type of literal constants # Integer constants if expr.valueType.type == 'bool': - return 'Boolean' + return 'bool' if expr.valueType.isFloat(): return expr.valueType.type if expr.valueType.isIntegral(): @@ -507,7 +507,7 @@ def getEssentialType(expr): return get_essential_type_from_value(expr.getKnownIntValue(), expr.valueType.sign == 'signed') elif expr.str in ('<', '<=', '>=', '>', '==', '!=', '&&', '||', '!'): - return 'Boolean' + return 'bool' elif expr.astOperand1 and expr.astOperand2 and expr.str in ( '+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":"): @@ -2053,13 +2053,28 @@ class MisraChecker: self.reportError(token, 10, 2) def misra_10_3(self, cfg): + def get_category(essential_type): + if essential_type: + if essential_type in ('bool', 'char'): + return essential_type + if essential_type.split(' ')[-1] in ('float', 'double'): + return 'floating' + if essential_type.split(' ')[0] in ('unsigned', 'signed'): + return essential_type.split(' ')[0] + return None for tok in cfg.tokenlist: if tok.isAssignmentOp: lhs = getEssentialType(tok.astOperand1) rhs = getEssentialType(tok.astOperand2) #print(lhs) #print(rhs) - if lhs and rhs and bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs): + if lhs is None or rhs is None: + continue + lhs_category = get_category(lhs) + rhs_category = get_category(rhs) + if lhs_category and rhs_category and lhs_category != rhs_category and rhs_category not in ('signed','unsigned'): + self.reportError(tok, 10, 3) + if bitsOfEssentialType(lhs) < bitsOfEssentialType(rhs): self.reportError(tok, 10, 3) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index ff50617cc..26ce67129 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -165,7 +165,7 @@ const char *s41_10 = "simple\nsequence"; const char *s41_11 = "string"; int c41_3 = '\141t'; // 4.1 int c41_4 = '\141\t'; -int c41_5 = '\0'; +int c41_5 = '\0'; // 10.3 int c41_6 = '\0\t'; int c41_7 = '\12\t'; int c41_8 = '\0t'; // 4.1 @@ -175,7 +175,7 @@ int c41_11 = '\12n'; // 4.1 int c41_12 = '\12323'; // 4.1 int c41_13 = '\123\3'; // TODO int c41_14 = '\777\777'; -int c41_15 = 'a'; +int c41_15 = 'a'; // 10.3 static void misra_4_1(void) { @@ -600,7 +600,7 @@ static void misra_10_1(uint32_t u, char c1, char c2) { i = (u & u) << 4; // no-warning c = c1 & c2; // 10.1 c = c1 << 1; // 10.1 - i = c1 > c2; // no-warning + i = c1 > c2; // 10.3 i = E1 + i; // no-warning char ch1 = 'a'; @@ -667,6 +667,7 @@ static void misra_10_3(uint32_t u32a, uint32_t u32b) { res = u32a + u32b; // 10.3 res = (uint16_t)(2U + 3U); // 10.3 10.8 res = (uint16_t)2U + (uint16_t)3U; + res = 0.1f; // 10.3 } static void misra_10_4(u32 x, s32 y) {