Skip to content

Commit ef68fea

Browse files
Make MISRA results consistent across cppcheck runs (commaai#1867)
* fixed uninitialized data error * update ci * revert old changes * add styling * switch misra.py to misra * remove cache from mutation.py * works on local * jobs increased to 8 for mutation test * works locally * add env variable back * removed env variable * cleanup --------- Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
1 parent be7c405 commit ef68fea

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

tests/misra/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.pdf
22
*.txt
3+
.output.log
34
new_table
45
cppcheck/

tests/misra/test_misra.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ if [ -z "${SKIP_BUILD}" ]; then
2929
fi
3030

3131
cppcheck() {
32-
hashed_args=$(echo -n "$@$DIR" | md5sum | awk '{print $1}')
33-
build_dir=/tmp/cppcheck_build/$hashed_args
34-
mkdir -p $build_dir
35-
32+
# note that cppcheck build cache results in inconsistent results as of v2.13.0
33+
OUTPUT=$DIR/.output.log
3634
$CPPCHECK_DIR/cppcheck --enable=all --force --inline-suppr -I $PANDA_DIR/board/ \
3735
-I $gcc_inc "$(arm-none-eabi-gcc -print-file-name=include)" \
3836
--suppressions-list=$DIR/suppressions.txt --suppress=*:*inc/* \
3937
--suppress=*:*include/* --error-exitcode=2 --addon=misra \
40-
--check-level=exhaustive --cppcheck-build-dir=$build_dir \
41-
"$@"
38+
--check-level=exhaustive "$@" |& tee $OUTPUT
39+
40+
# cppcheck bug: some MISRA errors won't result in the error exit code,
41+
# so check the output (https://trac.cppcheck.net/ticket/12440#no1)
42+
if grep "misra violation" $OUTPUT > /dev/null; then
43+
exit 1
44+
fi
4245
}
4346

4447
printf "\n${GREEN}** PANDA F4 CODE **${NC}\n"

tests/misra/test_mutation.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import shutil
66
import subprocess
77
import tempfile
8-
import hashlib
98
import random
109

1110
HERE = os.path.abspath(os.path.dirname(__file__))
@@ -68,25 +67,18 @@
6867

6968
@pytest.mark.parametrize("fn, patch, should_fail", mutations)
7069
def test_misra_mutation(fn, patch, should_fail):
71-
key = hashlib.md5((str(fn) + str(patch)).encode()).hexdigest()
72-
tmp = os.path.join(tempfile.gettempdir(), key)
70+
with tempfile.TemporaryDirectory() as tmp:
71+
shutil.copytree(ROOT, tmp, dirs_exist_ok=True)
7372

74-
if os.path.exists(tmp):
75-
shutil.rmtree(tmp)
76-
shutil.copytree(ROOT, tmp)
73+
# apply patch
74+
if fn is not None:
75+
r = os.system(f"cd {tmp} && sed -i '{patch}' {fn}")
76+
assert r == 0
7777

78-
# apply patch
79-
if fn is not None:
80-
r = os.system(f"cd {tmp} && sed -i '{patch}' {fn}")
81-
assert r == 0
82-
83-
# run test
84-
env = {'SKIP_BUILD': '1'} | os.environ.copy()
85-
r = subprocess.run("tests/misra/test_misra.sh", cwd=tmp, shell=True, env=env)
86-
failed = r.returncode != 0
87-
assert failed == should_fail
88-
89-
shutil.rmtree(tmp)
78+
# run test
79+
r = subprocess.run("tests/misra/test_misra.sh", cwd=tmp, shell=True)
80+
failed = r.returncode != 0
81+
assert failed == should_fail
9082

9183
if __name__ == "__main__":
9284
pytest.main([__file__, "-n 8"])

0 commit comments

Comments
 (0)