From b320a092d0330c4da563b1217d094cad3f974e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 20 Jul 2021 16:39:59 +0200 Subject: [PATCH] misra; fix essential type for char literals --- addons/misra.py | 34 ++++++++++++++++++---------------- addons/test/misra/misra-test.c | 12 ++++++------ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index 8ca4c7986..ae3607f21 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -459,6 +459,12 @@ def getEssentialType(expr): if not expr: return None + # See Appendix D, section D.6, Character constants + if expr.str[0] == "'" and expr.str[-1] == "'": + if len(expr.str) == 3 or (len(expr.str) == 4 and expr.str[1] == '\\'): + return 'char' + return '%s %s' % (expr.valueType.sign, expr.valueType.type) + if expr.variable: if expr.valueType: if expr.valueType.type == 'bool': @@ -1964,37 +1970,33 @@ class MisraChecker: def misra_10_2(self, data): def isEssentiallySignedOrUnsigned(op): - if op and op.valueType: - if op.valueType.sign in ['unsigned', 'signed']: - return True - return False + e = getEssentialType(op) + return e and (e.split(' ')[0] in ('unsigned', 'signed')) def isEssentiallyChar(op): - if op.isName: - return getEssentialType(op).split(' ')[-1] == 'char' + if op is None: + return False + if op.str == '+': + return isEssentiallyChar(op.astOperand1) or isEssentiallyChar(op.astOperand2) return op.isChar for token in data.tokenlist: - if not token.isArithmeticalOp or token.str not in ['+', '-']: + if token.str not in ('+', '-'): continue - operand1 = token.astOperand1 - operand2 = token.astOperand2 - if not operand1 or not operand2: - continue - if not operand1.isChar and not operand2.isChar: + if (not isEssentiallyChar(token.astOperand1)) and (not isEssentiallyChar(token.astOperand2)): continue if token.str == '+': - if isEssentiallyChar(operand1) and not isEssentiallySignedOrUnsigned(operand2): + if isEssentiallyChar(token.astOperand1) and not isEssentiallySignedOrUnsigned(token.astOperand2): self.reportError(token, 10, 2) - if isEssentiallyChar(operand2) and not isEssentiallySignedOrUnsigned(operand1): + if isEssentiallyChar(token.astOperand2) and not isEssentiallySignedOrUnsigned(token.astOperand1): self.reportError(token, 10, 2) if token.str == '-': - if not isEssentiallyChar(operand1): + if getEssentialType(token.astOperand1).split(' ')[-1] != 'char': self.reportError(token, 10, 2) - if not isEssentiallyChar(operand2) and not isEssentiallySignedOrUnsigned(operand2): + if not isEssentiallyChar(token.astOperand2) and not isEssentiallySignedOrUnsigned(token.astOperand2): self.reportError(token, 10, 2) def misra_10_3(self, cfg): diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index e8a2f4c39..b0621d527 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -643,10 +643,10 @@ static void misra_10_1_ternary(void) } static void misra_10_2(void) { - unsigned int u8a = 0; - signed char cha = 0; - signed int s8a = 0; - signed short s16a = 0; + uint8_t u8a = 0; + char cha = 0; + int8_t s8a = 0; + int16_t s16a = 0; float f32a = 0.0; char res; @@ -654,9 +654,9 @@ static void misra_10_2(void) { res = s8a + '0'; res = cha - '0'; res = '0' - s8a; - res = cha + ':'; + res = cha + ':'; // 10.2 - res = s16a - 'a'; // 10.2 + res = s16a - 'a'; // 10.2 10.3 res = '0' + f32a; // 10.2 10.4 }