tool/misc: add tool to convert signed to unsigned literals when used in unsigned expressions

This commit is contained in:
Matthias Ringwald 2020-07-22 13:53:20 +02:00
parent 8322fb3a90
commit 19d4ba9182
2 changed files with 72 additions and 0 deletions

55
tool/misc/fix-misra-10.4a.py Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env python3
import os
import sys
import re
import fileinput
# find root
btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/../../')
print(btstack_root)
# messages
cstat_file = 'cstat.txt'
# project prefix
project_prefixes = [ 'c:\\projects\\iar\\btstack\\btstack\\',
'c:/users/buildbot/buildbot-worker/cstat-develop/btstack/']
def fix(path, lineno, expression):
source_path = btstack_root + "/" + path
print(source_path, lineno, expression)
with open(source_path + '.cocci_res') as cocci_fd:
cocci_lines = cocci_fd.readlines()
for line_source, line_fix in zip(fileinput.input(source_path, inplace=True), cocci_lines):
if fileinput.lineno() == lineno:
sys.stdout.write(line_fix)
else:
sys.stdout.write(line_source)
with open(cstat_file, 'rt') as fin:
fixed = 0
total = 0
for line in fin:
chunks = line.strip().split('\t')
if len(chunks) != 4: continue
(msg, rule, severity, location) = chunks
if not rule.startswith('MISRAC2012-Rule-10.4_a'): continue
total += 1
# remove project prefix
for project_prefix in project_prefixes:
location = location.replace(project_prefix, '').replace('\\','/')
parts = location.split(':')
(path, lineno) = parts
match = re.match("The operands `(.+)' and `(.+)' have essential type categories (.*) and (.*), which do not match.", msg)
# fix if operand is signed literal and cstat complains about signednesss
if match:
(op1, op2, t1, t2) = match.groups()
if re.match("[(0x)0-9]+", op1) and t1.startswith('signed'):
fix(path, int(lineno), op1)
fixed += 1
continue
if re.match("[(0x)0-9]+", op2) and t2.startswith('signed'):
fix(path, int(lineno), op2)
fixed += 1
continue
print ("Fixed %u of %u messages" % (fixed, total))

17
tool/misc/fix-misra-10a.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/sh
BTSTACK_REL=`dirname $0`'/../..'
BTSTACK_ROOT=`realpath $BTSTACK_REL`
# check coccinelle
command -v spatch >/dev/null 2>&1 || { echo >&2 "spatch from cocinelle required but not installed. Aborting."; exit 1; }
# append u to all literals
spatch --sp-file $BTSTACK_ROOT/tool/misc/append_u_to_constants.cocci --out-place --max-width 200 --dir $BTSTACK_ROOT/src/
spatch --sp-file $BTSTACK_ROOT/tool/misc/append_u_to_constants.cocci --out-place --max-width 200 --dir $BTSTACK_ROOT/3rd-party/
# update only lines that are listed in cstat report
$BTSTACK_ROOT/tool/misc/fix-misra-10a.py
# delete cocci output files
find $BTSTACK_ROOT/ -name "*.cocci_res" -delete