xref: /XiangShan/scripts/coverage/coverage.py (revision d70a22729e83ef255c566a9eed5c479baa1c6bf4)
1#/usr/bin/python3
2# -*- coding: UTF-8 -*-
3import sys
4import re
5import copy
6
7if __name__ == "__main__":
8    assert len(sys.argv) == 3, "Expect input_file and output_file"
9    input_file = sys.argv[1]
10    output_file = sys.argv[2]
11    lines = []
12    line_count = 0
13    synthesis_nest_level = 0
14    with open(input_file) as f:
15        for line in f:
16            line_count += 1
17
18            ifdef = re.compile('`ifdef')
19            ifndef = re.compile('`ifndef')
20            endif = re.compile('`endif')
21            synthesis = re.compile('`ifndef SYNTHESIS')
22            line_coverage = re.compile('^\s*([%]?\d+)\s+if')
23
24            ifdef_match = ifdef.search(line)
25            ifndef_match = ifndef.search(line)
26            endif_match = endif.search(line)
27            synthesis_match = synthesis.search(line)
28            line_coverage_match = line_coverage.search(line)
29
30            # enter synthesis block
31            if synthesis_match:
32                assert synthesis_nest_level == 0, "Should not nest SYNTHESIS macro"
33                synthesis_nest_level = 1
34
35            if ifdef_match or (ifndef_match and not synthesis_match):
36                synthesis_nest_level += 1
37            if endif_match:
38                synthesis_nest_level -= 1
39                assert synthesis_nest_level >= 0, "Macro nest level should be >= 0"
40
41            # remove line coverage results in systhesis block
42            if synthesis_nest_level > 0 and line_coverage_match:
43                coverage_stat = line_coverage_match.group(1)
44                line = line.replace(line_coverage_match.group(1), " " * len(coverage_stat))
45
46            lines += line
47
48    with open(output_file, "w") as f:
49        for line in lines:
50            f.write(line)
51