2021-01-24 14:56:37 -08:00
#!/bin/bash
script_folder = " $( dirname $( readlink -f $0 ) ) "
# Initialize variables with defaults
source_folder = " $script_folder /../speeduino " # -s, --source
file_exts = "ino" # -e, --exts
out_folder = " $script_folder /.results " # -o, --out
cppcheck_path = "" # -c, --cppcheck
2022-09-06 23:04:51 -07:00
quiet = 0 # -q, --quiet
2021-01-24 14:56:37 -08:00
function parse_command_line( ) {
while [ $# -gt 0 ] ; do
case " $1 " in
-s | --source) source_folder = " $2 " ; ;
-e | --exts) file_exts = " $2 " ; ;
-o | --out) out_folder = " $2 " ; ;
-c | --cppcheck) cppcheck_path = " $2 " ; ;
2022-09-06 23:04:51 -07:00
-q | --quiet) quiet = 1 ; ;
2021-01-24 14:56:37 -08:00
-*)
echo "Unknown option: " $1
exit 1
; ;
esac
shift
done
}
function run_cppcheck( ) {
shopt -s nullglob nocaseglob
for i in " $source_folder " /*.{ " $file_exts " ,} ; do
2022-09-07 19:57:05 -07:00
# cppcheck currently has no way of excluding files that are #include'd. If maths.ino is scanned on versions of cppcheck 2.8+, the scan will run for a significant period of time (15+ mins) due to all the static code from libdivide.
# All violations from included libraries (*src* folders) are ignored
if [ [ $i != *"maths.ino" * ] ] ; then
" $cppcheck_bin " \
--inline-suppr \
--language= c++ \
--addon= " $script_folder /misra.json " \
--suppressions-list= " $script_folder /suppressions.txt " \
--platform= avr8 \
-DCORE_AVR= 1 \
-D__AVR_ATmega2560__ \
--suppress= "*:*src*" \
--report-progress \
$i 2>> " $cpp_result_file "
fi
2021-01-24 14:56:37 -08:00
done
shopt -u nullglob nocaseglob
}
function process_cpp_results( ) {
local intermediate_file = " $out_folder /tmp.txt "
# It's grouped into chunks of 3 lines: fold those into one line
sed '$!N;$!N;s/\n/~/g' < " $cpp_result_file " | \
# Remove duplicate lines
sort | uniq > " $intermediate_file "
2022-09-07 19:57:05 -07:00
# Count lines for Mandatory or Required rules
local __error_count = ` grep -i "Mandatory\|Required" < " $intermediate_file " | wc -l`
2021-01-24 14:56:37 -08:00
# Unfold the line groups for readability
tr '~' '\n' < " $intermediate_file " > " $result_file "
rm -f " $intermediate_file "
echo " $__error_count "
}
parse_command_line " $@ "
2017-06-15 20:34:47 -07:00
2021-01-24 14:56:37 -08:00
cppcheck_bin = " ${ cppcheck_path } /cppcheck "
cppcheck_misra = " ${ cppcheck_path } /addons/misra.py "
cpp_result_file = " $out_folder /cpp_results.txt "
result_file = " $out_folder /results.txt "
2017-06-15 20:34:47 -07:00
2021-01-24 14:56:37 -08:00
mkdir -p " $out_folder "
rm -f " $cpp_result_file "
rm -f " $result_file "
2017-06-15 20:34:47 -07:00
2021-01-24 14:56:37 -08:00
run_cppcheck
error_count = " $( process_cpp_results) "
2017-06-15 20:34:47 -07:00
2022-09-06 23:04:51 -07:00
if [ $quiet -eq 0 ] ; then
cat " $result_file "
fi
2021-01-24 14:56:37 -08:00
echo $error_count MISRA violations
2022-09-06 23:04:51 -07:00
echo $error_count > ".results/error_count.txt"
2017-06-15 23:07:33 -07:00
2022-09-07 19:57:05 -07:00
exit 0