xref: /btstack/tool/misc/fix-misra-12.1.py (revision 5df9dc7819db26ff2f834e23204a29e4dd68d594)
1ea1be655SMatthias Ringwald#!/usr/bin/env python3
2ea1be655SMatthias Ringwaldimport os
3ea1be655SMatthias Ringwaldimport sys
4ea1be655SMatthias Ringwaldimport re
5ea1be655SMatthias Ringwaldimport fileinput
6ea1be655SMatthias Ringwaldimport string
7ea1be655SMatthias Ringwald
8ea1be655SMatthias Ringwald# find root
9ea1be655SMatthias Ringwaldbtstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/../../')
10ea1be655SMatthias Ringwaldprint(btstack_root)
11ea1be655SMatthias Ringwald
12ea1be655SMatthias Ringwald# messages
13ea1be655SMatthias Ringwaldcstat_file = 'cstat.txt'
14ea1be655SMatthias Ringwald
15ea1be655SMatthias Ringwald# project prefix
16ea1be655SMatthias Ringwaldproject_prefix = 'c:\\projects\\iar\\btstack\\btstack\\'
17ea1be655SMatthias Ringwald
18*5df9dc78SMatthias Ringwalddef remove_whitespace(line):
19*5df9dc78SMatthias Ringwald    return line.replace(' ','')
20*5df9dc78SMatthias Ringwald
21ea1be655SMatthias Ringwalddef add_parantheses(line, expression):
22ea1be655SMatthias Ringwald    stripped_line = ''
23ea1be655SMatthias Ringwald    positions = []
24ea1be655SMatthias Ringwald    for (char,pos) in zip(line, range(len(line))):
25ea1be655SMatthias Ringwald        if char in string.whitespace:
26ea1be655SMatthias Ringwald            continue
27ea1be655SMatthias Ringwald        stripped_line += char
28ea1be655SMatthias Ringwald        positions.append(pos)
29ea1be655SMatthias Ringwald    pos = stripped_line.find(expression)
30ea1be655SMatthias Ringwald    if pos < 0:
31ea1be655SMatthias Ringwald        return line
32ea1be655SMatthias Ringwald    pos_start = positions[pos]
33ea1be655SMatthias Ringwald    pos_end   = positions[pos + len(expression)-1]
34ea1be655SMatthias Ringwald    new_line = line[0:pos_start] + '(' + line[pos_start:pos_end+1] + ")" + line[pos_end+1:]
35ea1be655SMatthias Ringwald    return new_line
36ea1be655SMatthias Ringwald
37ea1be655SMatthias Ringwalddef fix(path, lineno, expression):
38ea1be655SMatthias Ringwald    full_path = btstack_root + "/" + path
39ea1be655SMatthias Ringwald    print(full_path, lineno, expression)
40ea1be655SMatthias Ringwald    for line in fileinput.input(full_path, inplace=True):
41ea1be655SMatthias Ringwald        if fileinput.lineno() == lineno:
42ea1be655SMatthias Ringwald            line = add_parantheses(line, expression)
43ea1be655SMatthias Ringwald        sys.stdout.write(line)
44ea1be655SMatthias Ringwald
45ea1be655SMatthias Ringwaldwith open(cstat_file, 'rt') as fin:
46ea1be655SMatthias Ringwald    fixed = 0
47ea1be655SMatthias Ringwald    total = 0
48ea1be655SMatthias Ringwald    for line in fin:
49ea1be655SMatthias Ringwald        chunks = line.strip().split('\t')
50ea1be655SMatthias Ringwald        if len(chunks) != 4: continue
51ea1be655SMatthias Ringwald        (msg, rule, severity, location) = chunks
52ea1be655SMatthias Ringwald        if not rule.startswith('MISRAC2012-Rule-12.1'): continue
53ea1be655SMatthias Ringwald        parts = re.match(".*Suggest parentheses.*`(.*)'", msg)
54ea1be655SMatthias Ringwald        total += 1
55ea1be655SMatthias Ringwald        expression = parts.groups()[0]
56*5df9dc78SMatthias Ringwald        expression = remove_whitespace(expression)
57ea1be655SMatthias Ringwald        # skip some expressions
58ea1be655SMatthias Ringwald        if expression.endswith('=='): continue
59ea1be655SMatthias Ringwald        if expression.endswith('!='): continue
60ea1be655SMatthias Ringwald        if expression.endswith('>'): continue
61ea1be655SMatthias Ringwald        if expression.endswith('<'): continue
62ea1be655SMatthias Ringwald        if expression.endswith('>='): continue
63ea1be655SMatthias Ringwald        if expression.endswith('<='): continue
64ea1be655SMatthias Ringwald        # remove windows prefix
65ea1be655SMatthias Ringwald        location = location.replace(project_prefix, '').replace('\\','/')
66ea1be655SMatthias Ringwald        parts = location.split(':')
67ea1be655SMatthias Ringwald        (path, lineno) = parts
68ea1be655SMatthias Ringwald        fix(path, int(lineno), expression)
69ea1be655SMatthias Ringwald        fixed += 1
70ea1be655SMatthias Ringwald    print ("Fixed %u of %u messsages" % (fixed, total))
71