Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Oct 23, 2024
1 parent bdda657 commit b8178cf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
61 changes: 27 additions & 34 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -4357,8 +4357,8 @@ def loadRuleTexts(self, filename):

Rule_pattern = re.compile(r'^Rule ([0-9]+)\.([0-9]+)')
severity_pattern = re.compile(r'.*[ ]*(Advisory|Required|Mandatory)$')
xA_Z_pattern = re.compile(r'^[ ]*[#A-Z].*')
a_z_pattern = re.compile(r'^[ ]*[a-z].*')
xA_Z_pattern = re.compile(r'^[#A-Z].*')
a_z_pattern = re.compile(r'^[a-z].*')
# Try to detect the file encoding
file_stream = None
encodings = ['ascii', 'utf-8', 'windows-1250', 'windows-1252']
Expand All @@ -4383,70 +4383,63 @@ def loadRuleTexts(self, filename):
file_stream = open(filename, 'rt')

rule = None
have_severity = False
severity_loc = 0
rule_line_number = 0

for line in file_stream:

line = line.replace('\r', '').replace('\n', '')
line = line.strip()
if len(line) == 0:
expect_more = False
continue

if not appendixA:
if line.find('Appendix A') >= 0 and line.find('Summary of guidelines') >= 10:
appendixA = True
continue
if line.find('Appendix B') >= 0:
break
if len(line) == 0:
continue

# Parse rule declaration.
res = Rule_pattern.match(line)

if res:
have_severity = False
expect_more = False
severity_loc = 0
rule_line_number = 0
num1 = int(res.group(1))
num2 = int(res.group(2))
rule = Rule(num1, num2)

if not have_severity and rule is not None:
res = severity_pattern.match(line)

if res:
rule.misra_severity = res.group(1)
have_severity = True
else:
severity_loc += 1

# Only look for severity on the Rule line
# or the next non-blank line after
# If it's not in either of those locations then
# assume a severity was not provided.

if severity_loc < 2:
continue

rule.misra_severity = ''
have_severity = True
rule_line_number += 1
continue

if rule is None:
continue

# Parse continuing of rule text.
if expect_more:
if a_z_pattern.match(line):
self.ruleTexts[rule.num].text += ' ' + line.strip()
rule_line_number += 1

if rule_line_number == 1:
res = severity_pattern.match(line)

if res:
rule.misra_severity = res.group(1)
continue

expect_more = False
continue
rule_line_number = 2

# Parse beginning of rule text.
if xA_Z_pattern.match(line):
if not rule.text and xA_Z_pattern.match(line):
rule.text = line.strip()
self.ruleTexts[rule.num] = rule
expect_more = True
continue

# Parse continuing of rule text.
if a_z_pattern.match(line):
self.ruleTexts[rule.num].text += ' ' + line.strip()
continue

rule = None

file_stream.close()

Expand Down
4 changes: 4 additions & 0 deletions addons/test/misra/misra_rules_structure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Appendix A Summary of guidelines
Rule 1.2
Rule text.

Rule 2.1
Rule text for 2.1.
Rule text for 2.2.

Stop parsing after this line:
Appendix B

Expand Down
2 changes: 2 additions & 0 deletions addons/test/misra_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def test_loadRuleTexts_structure(checker):
assert(checker.ruleTexts.get(101, None) is None)
assert(checker.ruleTexts[102].text == "Rule text.")
assert(checker.ruleTexts.get(103, None) is None)
assert(checker.ruleTexts[201].text.startswith("Rule text for 2.1"))
assert(checker.ruleTexts.get(202, None) is None)


def test_loadRuleTexts_empty_lines(checker):
Expand Down

0 comments on commit b8178cf

Please sign in to comment.