Skip to content

Commit

Permalink
Make MISRA results consistent across cppcheck runs (#1867)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
0x41head and adeebshihadeh authored Feb 14, 2024
1 parent be7c405 commit ef68fea
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
1 change: 1 addition & 0 deletions tests/misra/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.pdf
*.txt
.output.log
new_table
cppcheck/
15 changes: 9 additions & 6 deletions tests/misra/test_misra.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ if [ -z "${SKIP_BUILD}" ]; then
fi

cppcheck() {
hashed_args=$(echo -n "$@$DIR" | md5sum | awk '{print $1}')
build_dir=/tmp/cppcheck_build/$hashed_args
mkdir -p $build_dir

# note that cppcheck build cache results in inconsistent results as of v2.13.0
OUTPUT=$DIR/.output.log
$CPPCHECK_DIR/cppcheck --enable=all --force --inline-suppr -I $PANDA_DIR/board/ \
-I $gcc_inc "$(arm-none-eabi-gcc -print-file-name=include)" \
--suppressions-list=$DIR/suppressions.txt --suppress=*:*inc/* \
--suppress=*:*include/* --error-exitcode=2 --addon=misra \
--check-level=exhaustive --cppcheck-build-dir=$build_dir \
"$@"
--check-level=exhaustive "$@" |& tee $OUTPUT

# cppcheck bug: some MISRA errors won't result in the error exit code,
# so check the output (https://trac.cppcheck.net/ticket/12440#no1)
if grep "misra violation" $OUTPUT > /dev/null; then
exit 1
fi
}

printf "\n${GREEN}** PANDA F4 CODE **${NC}\n"
Expand Down
28 changes: 10 additions & 18 deletions tests/misra/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import shutil
import subprocess
import tempfile
import hashlib
import random

HERE = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -68,25 +67,18 @@

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

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

# apply patch
if fn is not None:
r = os.system(f"cd {tmp} && sed -i '{patch}' {fn}")
assert r == 0

# run test
env = {'SKIP_BUILD': '1'} | os.environ.copy()
r = subprocess.run("tests/misra/test_misra.sh", cwd=tmp, shell=True, env=env)
failed = r.returncode != 0
assert failed == should_fail

shutil.rmtree(tmp)
# run test
r = subprocess.run("tests/misra/test_misra.sh", cwd=tmp, shell=True)
failed = r.returncode != 0
assert failed == should_fail

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

0 comments on commit ef68fea

Please sign in to comment.