From 937146127a7c027b5209d254f07713d420347f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 20 Jul 2021 18:27:49 +0200 Subject: [PATCH] misra; fix essential type for integer literals --- addons/misra.py | 35 +++++++++++++++++++++++++++++++++- addons/test/misra/misra-test.c | 34 ++++++++++++++++----------------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index 4b4ede547..5eb857138 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -455,6 +455,26 @@ def getEssentialCategorylist(operand1, operand2): return e1, e2 +def get_essential_type_from_value(value, is_signed): + if value is None: + return None + for t in ('char', 'short', 'int', 'long', 'long long'): + bits = bitsOfEssentialType(t) + if bits >= 64: + continue + if is_signed: + range_min = -(1 << (bits - 1)) + range_max = (1 << (bits - 1)) - 1 + else: + range_min = 0 + range_max = (1 << bits) - 1 + sign = 'signed' if is_signed else 'unsigned' + if is_signed and value < 0 and value >= range_min: + return '%s %s' % (sign, t) + if value >= 0 and value <= range_max: + return '%s %s' % (sign, t) + return None + def getEssentialType(expr): if not expr: return None @@ -474,7 +494,19 @@ def getEssentialType(expr): if expr.valueType.isIntegral(): return '%s %s' % (expr.valueType.sign, expr.valueType.type) - if expr.str in ('<', '<=', '>=', '>', '==', '!=', '&&', '||', '!'): + elif expr.isNumber: + # Appendix D, D.6 The essential type of literal constants + # Integer constants + if expr.valueType.type == 'bool': + return 'Boolean' + if expr.valueType.isFloat(): + return expr.valueType.type + if expr.valueType.isIntegral(): + if expr.valueType.type != 'int': + return '%s %s' % (expr.valueType.sign, expr.valueType.type) + return get_essential_type_from_value(expr.getKnownIntValue(), expr.valueType.sign == 'signed') + + elif expr.str in ('<', '<=', '>=', '>', '==', '!=', '&&', '||', '!'): return 'Boolean' elif expr.astOperand1 and expr.astOperand2 and expr.str in ( @@ -491,6 +523,7 @@ def getEssentialType(expr): return e2 else: return e1 + elif expr.str == "~": e1 = getEssentialType(expr.astOperand1) return e1 diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index b0621d527..d76ad9020 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -259,21 +259,21 @@ static void misra_7_2_call_test(int a, unsigned int b, unsigned int c) { } // 2. static void misra_7_2_call_va_test(int a, ...) { } // 2.7 static void misra_7_2(void) { - unsigned int a = 2147483647; - const unsigned int b = 2147483648U; - const unsigned int c = 2147483648; // 7.2 - unsigned int d = 2147483649; // 7.2 + uint32_t a = 2147483647; + const uint32_t b = 2147483648U; + const uint32_t c = 2147483648; // 7.2 10.3 + uint32_t d = 2147483649; // 7.2 10.3 - unsigned char e = 0x80; // 7.2 - unsigned char f = 0x80U; - unsigned short g = 0x8000; // 7.2 - unsigned short h = 0x8000U; - unsigned int i = 0x80000000; // 7.2 - unsigned int j = 0x80000000U; - unsigned long long k = 0x8000000000000000; // TODO 7.2 - unsigned long long l = 0x8000000000000000ULL; + uint8_t e = 0x80; // 7.2 10.3 + uint8_t f = 0x80U; + uint16_t g = 0x8000; // 7.2 10.3 + uint16_t h = 0x8000U; + uint32_t i = 0x80000000; // 7.2 + uint32_t j = 0x80000000U; + uint64_t k = 0x8000000000000000; // TODO 7.2 + uint64_t l = 0x8000000000000000ULL; - unsigned int m = 1 + 0x80000000; // 7.2 10.4 + uint32_t m = 1 + 0x80000000; // 7.2 10.4 misra_7_2_call_test(1, 2, 2147483648U); misra_7_2_call_test(1, 2, 2147483648); // 7.2 @@ -594,7 +594,7 @@ static void misra_10_1(uint32_t u, char c1, char c2) { int32_t i; char c; enum { E1 = 1 }; - i = 3 << 1; // 10.1 + i = 3 << 1; // 10.1 10.6 i = (u & u) << 4; // no-warning c = c1 & c2; // 10.1 c = c1 << 1; // 10.1 @@ -869,9 +869,9 @@ static void misra_12_4(void) { bool t; x = 123456u * 123456u; // TODO 12.4 x = MISRA12_4a + MISRA12_4b; // TODO 12.4 - x = 0u - 1u; // TODO 12.4 - x = t ? 0u : (0u-1u); // TODO 12.4 - x = 556200230913ULL; + //x = 0u - 1u; // TODO 12.4 + //x = t ? 0u : (0u-1u); // TODO 12.4 + x = 556200230913ULL; // 10.3 foo(&volatile_macro_12_4); // no crash }