|
6 | 6 | from drivers.base_driver import BaseDriver, Flags |
7 | 7 |
|
8 | 8 |
|
9 | | -# --- GNATcheck output parsing functions |
10 | | - |
11 | | -_flag_line_pattern = re.compile( |
12 | | - r"^([a-zA-Z][a-zA-Z0-9_\.\-]*\.(adb|ads|ada|ada_spec)):(\d+):\d+: .*$" |
13 | | -) |
14 | | - |
15 | | -def _parse_full(output: str) -> Flags: |
16 | | - """ |
17 | | - Parse the full formatted gnatcheck output. |
18 | | - """ |
19 | | - # Prepare the result |
20 | | - res = Flags() |
21 | | - |
22 | | - # Parse the gnatcheck full output |
23 | | - is_parsing = False |
24 | | - for line in output.splitlines(): |
25 | | - if not is_parsing: |
26 | | - is_parsing = "2. Exempted Coding Standard Violations" in line |
27 | | - else: |
28 | | - search_result = _flag_line_pattern.search(line) |
29 | | - if search_result is not None: |
30 | | - (file, _, line_num) = search_result.groups() |
31 | | - res.add_flag(file, int(line_num)) |
32 | | - is_parsing = "5. Language violations" not in line |
33 | | - |
34 | | - # Return the result |
35 | | - return res |
36 | | - |
37 | | -def _parse_short_and_brief(output: str) -> Flags: |
38 | | - """ |
39 | | - Parse the short formatted gnatcheck output. |
40 | | - """ |
41 | | - # Prepare the result |
42 | | - res = Flags() |
43 | | - |
44 | | - # Parse the output |
45 | | - for line in output.splitlines(): |
46 | | - search_result = _flag_line_pattern.search(line) |
47 | | - if search_result is not None: |
48 | | - (file, _, line_num) = search_result.groups() |
49 | | - res.add_flag(file, int(line_num)) |
50 | | - |
51 | | - # Return the result |
52 | | - return res |
53 | | - |
54 | | -def _parse_xml(output: str) -> Flags: |
55 | | - """ |
56 | | - Parse the xml formatted gnatcheck output. |
57 | | - """ |
58 | | - # Prepare the result |
59 | | - res = Flags() |
60 | | - |
61 | | - # Parse the xml result |
62 | | - xml_tree = ET.fromstring(output) |
63 | | - violations = xml_tree.find("violations") |
64 | | - |
65 | | - # If the "violations" tag exists in the output, parse it as a full XML output |
66 | | - if violations is not None: |
67 | | - for violation in violations: |
68 | | - file, line_num = violation.attrib["file"], int(violation.attrib["line"]) |
69 | | - res.add_flag(file, line_num) |
70 | | - |
71 | | - # Else the ouput is a brief XML one |
72 | | - else: |
73 | | - for elem in xml_tree.findall("*"): |
74 | | - if elem.tag in ("violation", "exemption-problem", "exempted-violation"): |
75 | | - file, line_num = elem.attrib["file"], int(elem.attrib["line"]) |
76 | | - res.add_flag(file, line_num) |
77 | | - |
78 | | - # Return the result |
79 | | - return res |
80 | | - |
81 | | - |
82 | 9 | class GnatcheckDriver(BaseDriver): |
83 | 10 | """ |
84 | 11 | This driver runs gnatcheck with the given arguments and compares the output |
@@ -191,12 +118,88 @@ class GnatcheckDriver(BaseDriver): |
191 | 118 | "gnatkp": "gnatkp" |
192 | 119 | } |
193 | 120 | output_formats = set(['brief', 'full', 'short', 'xml']) |
194 | | - parsers = { |
195 | | - 'full': _parse_full, |
196 | | - 'short': _parse_short_and_brief, |
197 | | - 'brief': _parse_short_and_brief, |
198 | | - 'xml': _parse_xml |
199 | | - } |
| 121 | + |
| 122 | + flag_line_pattern = re.compile( |
| 123 | + rf"^({BaseDriver.ada_file_pattern}):(\d+):\d+: (rule violation|warning|error): .*$" |
| 124 | + ) |
| 125 | + |
| 126 | + @classmethod |
| 127 | + def _parse_full(cls, output: str) -> Flags: |
| 128 | + """ |
| 129 | + Parse the full formatted GNATcheck output. |
| 130 | + """ |
| 131 | + # Prepare the result |
| 132 | + res = Flags() |
| 133 | + |
| 134 | + # Parse the gnatcheck full output |
| 135 | + is_parsing = False |
| 136 | + for line in output.splitlines(): |
| 137 | + if not is_parsing: |
| 138 | + is_parsing = "2. Exempted Coding Standard Violations" in line |
| 139 | + else: |
| 140 | + search_result = cls.flag_line_pattern.search(line) |
| 141 | + if search_result is not None: |
| 142 | + (file, _, line_num) = search_result.groups() |
| 143 | + res.add_flag(file, int(line_num)) |
| 144 | + is_parsing = "5. Language violations" not in line |
| 145 | + |
| 146 | + # Return the result |
| 147 | + return res |
| 148 | + |
| 149 | + @classmethod |
| 150 | + def _parse_short_and_brief(cls, output: str) -> Flags: |
| 151 | + """ |
| 152 | + Parse the short formatted GNATcheck output. |
| 153 | + """ |
| 154 | + # Prepare the result |
| 155 | + res = Flags() |
| 156 | + |
| 157 | + # Parse the output |
| 158 | + for line in output.splitlines(): |
| 159 | + search_result = cls.flag_line_pattern.search(line) |
| 160 | + if search_result is not None: |
| 161 | + (file, _, line_num) = search_result.groups() |
| 162 | + res.add_flag(file, int(line_num)) |
| 163 | + |
| 164 | + # Return the result |
| 165 | + return res |
| 166 | + |
| 167 | + @classmethod |
| 168 | + def _parse_xml(cls, output: str) -> Flags: |
| 169 | + """ |
| 170 | + Parse the XML formatted GNATcheck output. |
| 171 | + """ |
| 172 | + # Prepare the result |
| 173 | + res = Flags() |
| 174 | + |
| 175 | + # Parse the xml result |
| 176 | + xml_tree = ET.fromstring(output) |
| 177 | + violations = xml_tree.find("violations") |
| 178 | + |
| 179 | + # If the "violations" tag exists in the output, parse it as a full XML output |
| 180 | + if violations is not None: |
| 181 | + for violation in violations: |
| 182 | + file, line_num = violation.attrib["file"], int(violation.attrib["line"]) |
| 183 | + res.add_flag(file, line_num) |
| 184 | + |
| 185 | + # Else the ouput is a brief XML one |
| 186 | + else: |
| 187 | + for elem in xml_tree.findall("*"): |
| 188 | + if elem.tag in ("violation", "exemption-problem", "exempted-violation"): |
| 189 | + file, line_num = elem.attrib["file"], int(elem.attrib["line"]) |
| 190 | + res.add_flag(file, line_num) |
| 191 | + |
| 192 | + # Return the result |
| 193 | + return res |
| 194 | + |
| 195 | + @classmethod |
| 196 | + def parsers(cls): |
| 197 | + return { |
| 198 | + 'full': cls._parse_full, |
| 199 | + 'short': cls._parse_short_and_brief, |
| 200 | + 'brief': cls._parse_short_and_brief, |
| 201 | + 'xml': cls._parse_xml |
| 202 | + } |
200 | 203 |
|
201 | 204 | @property |
202 | 205 | def default_process_timeout(self): |
@@ -525,4 +528,4 @@ def run_one_test(test_data: dict[str, any]) -> None: |
525 | 528 |
|
526 | 529 | def parse_flagged_lines(self, output: str, format: str) -> Flags: |
527 | 530 | assert format in self.output_formats |
528 | | - return self.parsers[format](output) |
| 531 | + return self.parsers()[format](output) |
0 commit comments