Skip to content

Commit 083c4aa

Browse files
authored
Fix #12079 (Make misra-config a critical error) (#5578)
1 parent 77bfec4 commit 083c4aa

3 files changed

Lines changed: 36 additions & 25 deletions

File tree

addons/misra.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,23 +3259,26 @@ def misra_17_3(self, cfg):
32593259

32603260
def misra_config(self, data):
32613261
for token in data.tokenlist:
3262-
if token.str not in ["while", "if"]:
3263-
continue
3264-
if token.next.str != "(":
3262+
if token.str not in ("while", "if"):
32653263
continue
32663264
tok = token.next
3267-
while tok != token.next.link:
3265+
if token is None or tok.str != "(":
3266+
continue
3267+
end_token = tok.link
3268+
while tok != end_token:
3269+
tok = tok.next
32683270
if tok.str == "(" and tok.isCast:
32693271
tok = tok.link
32703272
continue
3271-
if not tok.isName or tok.function or tok.variable or tok.varId or tok.valueType \
3272-
or tok.next.str == "(" or tok.str in ["EOF"] \
3273-
or isKeyword(tok.str) or isStdLibId(tok.str):
3274-
tok = tok.next
3273+
if not tok.isName:
32753274
continue
3276-
errmsg = tok.str + " Variable is unknown"
3277-
self.reportError(token, 0, 0, "config")
3278-
break
3275+
if tok.function or tok.variable or tok.varId or tok.valueType:
3276+
continue
3277+
if tok.next.str == "(" or tok.str in ["EOF"]:
3278+
continue
3279+
if isKeyword(tok.str) or isStdLibId(tok.str):
3280+
continue
3281+
self.report_config_error(tok, "Variable '%s' is unknown" % tok.str)
32793282

32803283
def misra_17_6(self, rawTokens):
32813284
for token in rawTokens:
@@ -4184,30 +4187,29 @@ def setSuppressionList(self, suppressionlist):
41844187

41854188
self.addSuppressedRule(ruleNum)
41864189

4187-
def reportError(self, location, num1, num2, other_id = None):
4188-
if not other_id:
4189-
ruleNum = num1 * 100 + num2
4190+
def report_config_error(self, location, errmsg):
4191+
cppcheck_severity = 'error'
4192+
error_id = 'config'
4193+
if self.settings.verify:
4194+
self.verify_actual.append('%s:%d %s' % (location.file, location.linenr, error_id))
41904195
else:
4191-
ruleNum = other_id
4196+
cppcheckdata.reportError(location, cppcheck_severity, errmsg, 'misra', error_id)
4197+
4198+
def reportError(self, location, num1, num2):
4199+
ruleNum = num1 * 100 + num2
41924200

41934201
if self.isRuleGloballySuppressed(ruleNum):
41944202
return
41954203

41964204
if self.settings.verify:
4197-
if not other_id:
4198-
self.verify_actual.append('%s:%d %d.%d' % (location.file, location.linenr, num1, num2))
4199-
else:
4200-
self.verify_actual.append('%s:%d %s' % (location.file, location.linenr, other_id))
4205+
self.verify_actual.append('%s:%d %d.%d' % (location.file, location.linenr, num1, num2))
42014206
elif self.isRuleSuppressed(location.file, location.linenr, ruleNum):
42024207
# Error is suppressed. Ignore
42034208
self.suppressionStats.setdefault(ruleNum, 0)
42044209
self.suppressionStats[ruleNum] += 1
42054210
return
42064211
else:
4207-
if not other_id:
4208-
errorId = 'c2012-' + str(num1) + '.' + str(num2)
4209-
else:
4210-
errorId = 'c2012-' + other_id
4212+
errorId = 'c2012-' + str(num1) + '.' + str(num2)
42114213
misra_severity = 'Undefined'
42124214
cppcheck_severity = 'style'
42134215
if ruleNum in self.ruleTexts:

addons/test/test-misra.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
TEST_SOURCE_FILES = ['./addons/test/misra/misra-test.c']
1818

1919

20+
def remove_misra_config(s:str):
21+
ret = ''
22+
for line in s.splitlines():
23+
if '[misra-config]' not in line:
24+
ret += line + '\n'
25+
return ret
26+
27+
2028
def setup_module(module):
2129
for f in TEST_SOURCE_FILES:
2230
dump_create(f)
@@ -92,7 +100,7 @@ def test_rules_cppcheck_severity(checker, capsys):
92100
checker.loadRuleTexts("./addons/test/misra/misra_rules_dummy.txt")
93101
checker.parseDump("./addons/test/misra/misra-test.c.dump")
94102
captured = capsys.readouterr().err
95-
assert("(error)" not in captured)
103+
assert("(error)" not in remove_misra_config(captured))
96104
assert("(warning)" not in captured)
97105
assert("(style)" in captured)
98106

@@ -101,7 +109,7 @@ def test_rules_cppcheck_severity_custom(checker, capsys):
101109
checker.setSeverity("custom-severity")
102110
checker.parseDump("./addons/test/misra/misra-test.c.dump")
103111
captured = capsys.readouterr().err
104-
assert("(error)" not in captured)
112+
assert("(error)" not in remove_misra_config(captured))
105113
assert("(warning)" not in captured)
106114
assert("(style)" not in captured)
107115
assert("(custom-severity)" in captured)

lib/errorlogger.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const std::set<std::string> ErrorLogger::mCriticalErrorIds{
4545
"internalAstError",
4646
"instantiationError",
4747
"internalError",
48+
"misra-config",
4849
"premium-internalError",
4950
"premium-invalidArgument",
5051
"premium-invalidLicense",

0 commit comments

Comments
 (0)