Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # this must be the first non-commented line in this script. It ensures |
| 4 | # bash doesn't choke on \r on Windows |
| 5 | (set -o igncr) 2>/dev/null && set -o igncr; # this comment is needed |
| 6 | |
| 7 | # |
| 8 | # This script does static code analysis using Cppcheck tool |
| 9 | # Copyright (c) 2019 Cypress Semiconductor. |
| 10 | # |
| 11 | |
| 12 | # It performs Cppcheck code analysis with following inputs |
| 13 | # 1. CypressBootloader/sources - Code analysis is done on all the sources of CypressBootloader. |
| 14 | # 2. Additional source files to be analyzed are grabbed from config file that is provided as a first argument to the script. |
| 15 | # 3. Files to be ignored are grabbed from config file that is provided as a first argument to the script. |
| 16 | # 4. To ignore a file its name need to be added to the config file with word "ignore" as perfix |
| 17 | # 5. To add any additional files, apart the files from CypressBootloader/sources, those names need |
| 18 | # to be added in a config file. |
| 19 | # Example |
| 20 | # A). add below entries in cpp_check.dat file |
| 21 | # ignore cy_bootloader_hw.c |
| 22 | # file1.c |
| 23 | # file2.c |
| 24 | # ignore cy_bootloader_services.c |
| 25 | # B). invoke cpp_check shell script |
| 26 | # cpp_check.sh cpp_check.dat |
| 27 | # |
| 28 | # Above example performs Cppcheck analysis on CypressBootloader/sources, ignore cy_bootloader_hw.c, file1.c, file2.c and ignores cy_bootloader_services.c |
| 29 | |
| 30 | |
| 31 | app_name="$1" |
| 32 | platform="$2" |
| 33 | app_defines="$3" |
| 34 | app_includes="$4" |
| 35 | CPP_CHECK_FILES="$5" |
| 36 | scope="$6" |
| 37 | buildcfg="$7" |
| 38 | |
| 39 | if [[ ${scope} != "" ]]; then |
| 40 | SCOPE="--enable=${scope}" |
| 41 | else |
| 42 | SCOPE="" |
| 43 | fi |
| 44 | |
| 45 | #Retrieve list of files need to be ignored |
| 46 | while IFS= read -r line |
| 47 | do |
| 48 | CPP_CHECK_IGNORE_FILES="$CPP_CHECK_IGNORE_FILES -i $line" |
| 49 | done < "${app_name}/cppcheck/ignore_files.list" |
| 50 | |
| 51 | #Retrieve list of cppcheck directives |
| 52 | while IFS= read -r line |
| 53 | do |
| 54 | CPP_CHECK_SUPPRESS="$CPP_CHECK_SUPPRESS --suppress=$line" |
| 55 | done < "${app_name}/cppcheck/suppress_types.list" |
| 56 | |
| 57 | echo "-------------------------------------------" |
| 58 | echo "Suppress options:" "$CPP_CHECK_SUPPRESS" |
| 59 | echo "-------------------------------------------" |
| 60 | echo "Additional files:" "$CPP_CHECK_FILES" |
| 61 | echo "-------------------------------------------" |
| 62 | echo "Ignoring files:" "$CPP_CHECK_IGNORE_FILES" |
| 63 | echo "-------------------------------------------" |
| 64 | echo "CppCheck scope of messages defined with option " ${SCOPE} |
| 65 | echo "-------------------------------------------" |
| 66 | echo "Run CppCheck for platform" ${platform} |
| 67 | echo "-------------------------------------------" |
| 68 | echo "Defines passed to CppCheck:" |
| 69 | echo ${app_defines} |
| 70 | echo "-------------------------------------------" |
| 71 | echo "Include dirs passed to CppCheck:" |
| 72 | echo ${app_includes} |
| 73 | echo "-------------------------------------------" |
| 74 | |
| 75 | mkdir -p ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_html |
| 76 | |
| 77 | dos2unix ${app_name}/cppcheck/suppress_messages.list |
| 78 | |
| 79 | #Generate xml file |
| 80 | cppcheck ${SCOPE} ${CPP_CHECK_SUPPRESS} -D__GNUC__ -D${platform} ${app_defines} ${app_includes} ${CPP_CHECK_FILES} ${CPP_CHECK_IGNORE_FILES} \ |
| 81 | --xml 2> ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.xml |
| 82 | |
| 83 | #Generate html file |
| 84 | python scripts/cppcheck-htmlreport.py --file=${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.xml --report-dir=${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_html --title=${app_name} |
| 85 | |
| 86 | cppcheck ${SCOPE} ${CPP_CHECK_SUPPRESS} -D__GNUC__ -D${platform} ${app_defines} ${app_includes} ${CPP_CHECK_FILES} ${CPP_CHECK_IGNORE_FILES} \ |
| 87 | --template="{severity}\n{id}\n{message}\n{file}\n{line}:{column}\n{code}\n" 2> ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full |
| 88 | |
| 89 | #Generate csv file |
| 90 | echo "severity@id@message@file@line" > ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.csv |
| 91 | while IFS= read -r line |
| 92 | do |
| 93 | read -r line2 |
| 94 | read -r line3 |
| 95 | read -r line4 |
| 96 | read -r line5 |
| 97 | line4=$(echo $line4 | sed 's/.*\\cy_mcuboot\\//' | tr '\\' '/') |
| 98 | if grep -xq "${line}@${line2}@${line3}@${line4}@${line5}" ${app_name}/cppcheck/suppress_messages.list |
| 99 | then |
| 100 | :;#suppress current warning |
| 101 | else |
| 102 | echo ${line}@${line2}@${line3}@${line4}@${line5} |
| 103 | fi |
| 104 | read -r line |
| 105 | read -r line |
| 106 | read -r line |
| 107 | done \ |
| 108 | < ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full \ |
| 109 | >>${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.csv |
| 110 | |
| 111 | #Generate log file |
| 112 | while IFS= read -r line |
| 113 | do |
| 114 | read -r line2 |
| 115 | read -r line3 |
| 116 | read -r line4 |
| 117 | read -r line5 |
| 118 | line4=$(echo $line4 | sed 's/.*\\cy_mcuboot\\//' | tr '\\' '/') |
| 119 | if grep -xq "${line}@${line2}@${line3}@${line4}@${line5}" ${app_name}/cppcheck/suppress_messages.list |
| 120 | then |
| 121 | read -r line |
| 122 | read -r line |
| 123 | read -r line |
| 124 | else |
| 125 | echo ${line} : ${line2} |
| 126 | echo ${line3} |
| 127 | echo "${line4} (${line5})" |
| 128 | read -r line |
| 129 | echo ${line} |
| 130 | read -r line |
| 131 | echo ${line} |
| 132 | read -r line |
| 133 | echo "-------------------------------------------" |
| 134 | fi |
| 135 | done \ |
| 136 | < ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full \ |
| 137 | > ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.log |
| 138 | |
| 139 | rm ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full |
| 140 | cat ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.log |
| 141 | |
| 142 | RC=$(( $(wc -l ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.csv | cut -d' ' -f1) -1 )) |
| 143 | echo "${app_name} CPPCHECK FOR ${platform} KIT FOUND $RC ERRORS" |
| 144 | |
| 145 | exit $RC |