From 515649217fb98a13d6eca8a1c260ee378986d371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 11 Feb 2022 21:04:46 +0100 Subject: [PATCH] fix --- addons/misra.py | 25 +++++++++++++++++++++++-- addons/test/misra/misra-test.c | 8 ++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index f2bfbe458..6dbca19bc 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -422,6 +422,28 @@ def get_type_conversion_to_from(token): return None + +def is_composite_expr(expr, composite_operator=False): + """MISRA C 2012, section 8.10.3""" + if expr is None: + return False + + if not composite_operator: + if (expr.str in ('+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":", '~')): + return is_composite_expr(expr.astOperand1,True) or is_composite_expr(expr.astOperand2, True) + if expr.str == '?' and simpleMatch(expr.astOperand2, ':'): + colon = expr.astOperand2 + return is_composite_expr(colon.astOperand1,True) or is_composite_expr(colon.astOperand2, True) + return False + + # non constant expression? + if expr.isNumber: + return False + if expr.astOperand1 or expr.astOperand2: + return is_composite_expr(expr.astOperand1,True) or is_composite_expr(expr.astOperand2, True) + return True + + def getEssentialTypeCategory(expr): if not expr: return None @@ -2277,8 +2299,7 @@ class MisraChecker: for token in data.tokenlist: if token.str != '=' or not token.astOperand1 or not token.astOperand2: continue - if (token.astOperand2.str not in ('+', '-', '*', '/', '%', '&', '|', '^', '>>', "<<", "?", ":", '~') and - not isCast(token.astOperand2)): + if not is_composite_expr(token.astOperand2): continue vt1 = token.astOperand1.valueType vt2 = token.astOperand2.valueType diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index e0f031e11..d7a77f014 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -623,7 +623,7 @@ static void misra_10_1(uint32_t u, char c1, char c2, uint8_t u8) { int32_t i; char c; enum { E1 = 1 }; - i = 3 << 1; // 10.1 10.6 + i = 3 << 1; // 10.1 i = (u & u) << 4; // no-warning c = c1 & c2; // 10.1 c = c1 << 1; // 10.1 @@ -736,10 +736,10 @@ static void misra_10_5(uint16_t x) { struct misra_10_6_s { unsigned int a:4; }; -static void misra_10_6(u8 x, u32 a, u32 b, char c1, char c2) { - u16 y = x+x; // 10.6 +static void misra_10_6(u8 x, char c1, char c2) { + u16 y1 = x+x; // 10.6 + u16 y2 = (0x100u - 0x80u); // rhs is not a composite expression because it's a constant expression u16 z = ~u8 x ;//10.6 - u32 c = ( u16) ( u32 a + u32 b ); //10.6 s32 i = c1 - c2; // 10.3 FIXME: False positive for 10.6 (this is compliant). Trac #9488 struct misra_10_6_s s; s.a = x & 1U; // no-warning (#10487)