From 12b28465eff31a3b88744a5cd7b3895d8e698ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 19 Oct 2023 21:23:38 +0200 Subject: [PATCH 1/3] Fix #12079 (Make misra-config a critical error) --- addons/misra.py | 46 ++++++++++++++++++++++----------------------- lib/errorlogger.cpp | 1 + 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index 35b6cafcd07..39dbfd3fc6a 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -3203,23 +3203,26 @@ def misra_17_3(self, cfg): def misra_config(self, data): for token in data.tokenlist: - if token.str not in ["while", "if"]: - continue - if token.next.str != "(": + if token.str not in ("while", "if"): continue tok = token.next - while tok != token.next.link: + if token is None or tok.str != "(": + continue + end_token = tok.link + while tok != end_token: + tok = tok.next if tok.str == "(" and tok.isCast: tok = tok.link continue - if not tok.isName or tok.function or tok.variable or tok.varId or tok.valueType \ - or tok.next.str == "(" or tok.str in ["EOF"] \ - or isKeyword(tok.str) or isStdLibId(tok.str): - tok = tok.next + if not tok.isName: continue - errmsg = tok.str + " Variable is unknown" - self.reportError(token, 0, 0, "config") - break + if tok.function or tok.variable or tok.varId or tok.valueType: + continue + if tok.next.str == "(" or tok.str in ["EOF"]: + continue + if isKeyword(tok.str) or isStdLibId(tok.str): + continue + self.report_config_error(tok, "Variable '%s' is unknown" % tok.str) def misra_17_6(self, rawTokens): for token in rawTokens: @@ -4128,30 +4131,27 @@ def setSuppressionList(self, suppressionlist): self.addSuppressedRule(ruleNum) - def reportError(self, location, num1, num2, other_id = None): - if not other_id: - ruleNum = num1 * 100 + num2 + def report_config_error(self, location, errmsg): + if self.settings.verify: + self.verify_actual.append('%s:%d %s' % (location.file, location.linenr, other_id)) else: - ruleNum = other_id + cppcheckdata.reportError(location, 'error', errmsg, 'misra', 'config') + + def reportError(self, location, num1, num2): + ruleNum = num1 * 100 + num2 if self.isRuleGloballySuppressed(ruleNum): return if self.settings.verify: - if not other_id: - self.verify_actual.append('%s:%d %d.%d' % (location.file, location.linenr, num1, num2)) - else: - self.verify_actual.append('%s:%d %s' % (location.file, location.linenr, other_id)) + self.verify_actual.append('%s:%d %d.%d' % (location.file, location.linenr, num1, num2)) elif self.isRuleSuppressed(location.file, location.linenr, ruleNum): # Error is suppressed. Ignore self.suppressionStats.setdefault(ruleNum, 0) self.suppressionStats[ruleNum] += 1 return else: - if not other_id: - errorId = 'c2012-' + str(num1) + '.' + str(num2) - else: - errorId = 'c2012-' + other_id + errorId = 'c2012-' + str(num1) + '.' + str(num2) misra_severity = 'Undefined' cppcheck_severity = 'style' if ruleNum in self.ruleTexts: diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 41b73f9cbdb..886ebd516d8 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -45,6 +45,7 @@ const std::set ErrorLogger::mCriticalErrorIds{ "internalAstError", "instantiationError", "internalError", + "misra-config", "premium-internalError", "premium-invalidArgument", "premium-invalidLicense", From f4ea6c3619592800a2412e219f9c74259c20638b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 19 Oct 2023 22:22:30 +0200 Subject: [PATCH 2/3] fix --- addons/test/test-misra.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/addons/test/test-misra.py b/addons/test/test-misra.py index 3701dc2f460..debd4f2a68a 100644 --- a/addons/test/test-misra.py +++ b/addons/test/test-misra.py @@ -17,6 +17,14 @@ TEST_SOURCE_FILES = ['./addons/test/misra/misra-test.c'] +def remove_misra_config(s:str): + ret = '' + for line in s.splitlines(): + if '[misra-config]' not in line: + ret += line + '\n' + return ret + + def setup_module(module): for f in TEST_SOURCE_FILES: dump_create(f) @@ -92,7 +100,7 @@ def test_rules_cppcheck_severity(checker, capsys): checker.loadRuleTexts("./addons/test/misra/misra_rules_dummy.txt") checker.parseDump("./addons/test/misra/misra-test.c.dump") captured = capsys.readouterr().err - assert("(error)" not in captured) + assert("(error)" not in remove_misra_config(captured)) assert("(warning)" not in captured) assert("(style)" in captured) @@ -101,7 +109,7 @@ def test_rules_cppcheck_severity_custom(checker, capsys): checker.setSeverity("custom-severity") checker.parseDump("./addons/test/misra/misra-test.c.dump") captured = capsys.readouterr().err - assert("(error)" not in captured) + assert("(error)" not in remove_misra_config(captured)) assert("(warning)" not in captured) assert("(style)" not in captured) assert("(custom-severity)" in captured) From 076eaf008d1944a82a075b9c6e5449c78b85f493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 19 Oct 2023 22:58:37 +0200 Subject: [PATCH 3/3] fix --- addons/misra.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/addons/misra.py b/addons/misra.py index 39dbfd3fc6a..fb4ebc71e15 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -4132,10 +4132,12 @@ def setSuppressionList(self, suppressionlist): self.addSuppressedRule(ruleNum) def report_config_error(self, location, errmsg): + cppcheck_severity = 'error' + error_id = 'config' if self.settings.verify: - self.verify_actual.append('%s:%d %s' % (location.file, location.linenr, other_id)) + self.verify_actual.append('%s:%d %s' % (location.file, location.linenr, error_id)) else: - cppcheckdata.reportError(location, 'error', errmsg, 'misra', 'config') + cppcheckdata.reportError(location, cppcheck_severity, errmsg, 'misra', error_id) def reportError(self, location, num1, num2): ruleNum = num1 * 100 + num2