From 10c2dcf7211a583c3383b12eed6c793ea1faec68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 15 Aug 2021 12:04:55 +0200 Subject: [PATCH] misra: implement rule 22.7 --- addons/misra.py | 16 ++++++++++++++++ addons/test/misra/misra-test.c | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/addons/misra.py b/addons/misra.py index 242121a43..7069230a4 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -3463,6 +3463,21 @@ class MisraChecker: if fileptr.variable and cppcheckdata.simpleMatch(fileptr.variable.typeStartToken, 'FILE *'): self.reportError(token, 22, 5) + def misra_22_7(self, cfg): + for eofToken in cfg.tokenlist: + if eofToken.str != 'EOF': + continue + if eofToken.astParent is None or not eofToken.astParent.isComparisonOp: + continue + if eofToken.astParent.astOperand1 == eofToken: + eofTokenSibling = eofToken.astParent.astOperand2 + else: + eofTokenSibling = eofToken.astParent.astOperand1 + while isCast(eofTokenSibling) and eofTokenSibling.valueType and eofTokenSibling.valueType.type and eofTokenSibling.valueType.type == 'int': + eofTokenSibling = eofTokenSibling.astOperand2 if eofTokenSibling.astOperand2 else eofTokenSibling.astOperand1 + if eofTokenSibling is not None and eofTokenSibling.valueType and eofTokenSibling.valueType and eofTokenSibling.valueType.type in ('bool', 'char', 'short'): + self.reportError(eofToken, 22, 7) + def misra_22_8(self, cfg): is_zero = False for token in cfg.tokenlist: @@ -4080,6 +4095,7 @@ class MisraChecker: self.executeCheck(2115, self.misra_21_15, cfg) # 22.4 is already covered by Cppcheck writeReadOnlyFile self.executeCheck(2205, self.misra_22_5, cfg) + self.executeCheck(2207, self.misra_22_7, cfg) self.executeCheck(2208, self.misra_22_8, cfg) self.executeCheck(2209, self.misra_22_9, cfg) self.executeCheck(2210, self.misra_22_10, cfg) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 4a6dab95a..71e02ff2e 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -1787,6 +1787,11 @@ static void misra_22_5(FILE *f) { int y = f->pos; // 22.5 } +static void misra_22_7(char ch) +{ + if (EOF == ch) {} // 22.7 +} + static void misra_22_8(void) { (void)strtoll("123", NULL, 10); // 22.8