Skip to content
Snippets Groups Projects
Unverified Commit 055536af authored by Grigoris Pavlakis's avatar Grigoris Pavlakis Committed by kongr45gpen
Browse files

Add line-based suppression feature. Credits to @dimst23 for the idea and implementation!

parent 6baa4696
No related branches found
No related tags found
No related merge requests found
#!/bin/env python3 #!/bin/env python3
import os
from argparse import ArgumentParser from argparse import ArgumentParser
""" """
...@@ -38,6 +39,12 @@ class Summarizer(object): ...@@ -38,6 +39,12 @@ class Summarizer(object):
file_name = line_contents[0] # first part is the filename (index 0) file_name = line_contents[0] # first part is the filename (index 0)
violation = (line_contents[1], line_contents[2].strip( violation = (line_contents[1], line_contents[2].strip(
'\n')) # index 1 is the line number, index 2 is the number of violated rule (both are strings) '\n')) # index 1 is the line number, index 2 is the number of violated rule (both are strings)
with open(os.path.abspath(file_name)) as code_file:
code_lines = code_file.readlines() # Read the source code file
line_of_interest = code_lines[int(violation[0]) - 1] # Get the desired violation line
if line_of_interest.find("// Ignore-MISRA") >= 0:
continue
if file_name not in self.violations_map.keys(): if file_name not in self.violations_map.keys():
self.violations_map[ self.violations_map[
...@@ -45,7 +52,7 @@ class Summarizer(object): ...@@ -45,7 +52,7 @@ class Summarizer(object):
# rule no. # rule no.
self.violations_map[file_name].append(violation) self.violations_map[file_name].append(violation)
else: else:
self.violations_map[file_name].append(violation) # do not append if it already exists self.violations_map[file_name].append(violation) # do not create a key if it already exists
for e in self.suppression_list: for e in self.suppression_list:
for file_name in self.violations_map.keys(): for file_name in self.violations_map.keys():
...@@ -55,6 +62,7 @@ class Summarizer(object): ...@@ -55,6 +62,7 @@ class Summarizer(object):
self.violations_map = {k: v for (k, v) in self.violations_map.items() if len(v) != 0} self.violations_map = {k: v for (k, v) in self.violations_map.items() if len(v) != 0}
# "delete" all keys whose lists are empty # "delete" all keys whose lists are empty
def pretty_print_violations(self): def pretty_print_violations(self):
""" """
Just a pretty printing function, no fancy logic here. Just a pretty printing function, no fancy logic here.
...@@ -64,6 +72,7 @@ class Summarizer(object): ...@@ -64,6 +72,7 @@ class Summarizer(object):
for file_name in self.violations_map: for file_name in self.violations_map:
print("") print("")
for violation in sorted(self.violations_map[file_name], key=lambda x: int(x[0])): for violation in sorted(self.violations_map[file_name], key=lambda x: int(x[0])):
name_string = f"{self.bold}{self.red}File {self.yellow}{file_name}{self.red}" name_string = f"{self.bold}{self.red}File {self.yellow}{file_name}{self.red}"
rule_violated_string = f"violates rule {self.yellow}#{violation[1]}{self.red} " \ rule_violated_string = f"violates rule {self.yellow}#{violation[1]}{self.red} " \
f"of the MISRA C 2012 standard" f"of the MISRA C 2012 standard"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment