MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / loadRuleTexts

Method loadRuleTexts

addons/misra.py:4530–4620  ·  view source on GitHub ↗
(self, filename)

Source from the content-addressed store, hash-verified

4528 self.violations[misra_severity].append('misra-' + errorId)
4529
4530 def loadRuleTexts(self, filename):
4531 num1 = 0
4532 num2 = 0
4533 appendixA = False
4534
4535 Rule_pattern = re.compile(r'^Rule ([0-9]+)\.([0-9]+)')
4536 severity_pattern = re.compile(r'.*[ ]*(Advisory|Required|Mandatory)$')
4537 xA_Z_pattern = re.compile(r'^[#A-Z].*')
4538 a_z_pattern = re.compile(r'^[a-z].*')
4539 # Try to detect the file encoding
4540 file_stream = None
4541 encodings = ['ascii', 'utf-8', 'windows-1250', 'windows-1252']
4542 for e in encodings:
4543 try:
4544 file_stream = open(filename, 'r', encoding=e)
4545 file_stream.readlines()
4546 file_stream.seek(0)
4547 except UnicodeDecodeError:
4548 file_stream.close()
4549 file_stream = None
4550 else:
4551 break
4552 if not file_stream:
4553 print('Could not find a suitable codec for "' + filename + '".')
4554 print('If you know the codec please report it to the developers so the list can be enhanced.')
4555 print('Trying with default codec now and ignoring errors if possible ...')
4556 try:
4557 file_stream = open(filename, 'rt', errors='ignore')
4558 except TypeError:
4559 # Python 2 does not support the errors parameter
4560 file_stream = open(filename, 'rt')
4561
4562 rule = None
4563 rule_line_number = 0
4564
4565 for line in file_stream:
4566
4567 line = line.strip()
4568 if len(line) == 0:
4569 continue
4570
4571 if not appendixA:
4572 if line.find('Appendix A') >= 0 and line.find('Summary of guidelines') >= 10:
4573 appendixA = True
4574 continue
4575 if line.find('Appendix B') >= 0:
4576 break
4577
4578 # Parse rule declaration.
4579 res = Rule_pattern.match(line)
4580
4581 if res:
4582 rule_line_number = 0
4583 num1 = int(res.group(1))
4584 num2 = int(res.group(2))
4585 rule = Rule(num1, num2)
4586
4587 res = severity_pattern.match(line)

Callers 9

mainFunction · 0.95
test_verifyRuleTextsFunction · 0.80
test_json_outFunction · 0.80

Calls 5

compileMethod · 0.80
RuleClass · 0.70
closeMethod · 0.45
findMethod · 0.45
matchMethod · 0.45