blob: 689e338c835c2dd7e75766d0edd05b3bc1bd89e4 [file] [log] [blame]
Roman Okhrimenko0142a682022-03-31 14:40:48 +03001#!/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
31app_name="$1"
32platform="$2"
33app_defines="$3"
34app_includes="$4"
35CPP_CHECK_FILES="$5"
36scope="$6"
37buildcfg="$7"
38
39if [[ ${scope} != "" ]]; then
40 SCOPE="--enable=${scope}"
41else
42 SCOPE=""
43fi
44
45#Retrieve list of files need to be ignored
46while IFS= read -r line
47do
48 CPP_CHECK_IGNORE_FILES="$CPP_CHECK_IGNORE_FILES -i $line"
49done < "cppcheck/${app_name}/ignore_files.list"
50
51#Retrieve list of cppcheck directives
52while IFS= read -r line
53do
54 CPP_CHECK_SUPPRESS="$CPP_CHECK_SUPPRESS --suppress=$line"
55done < "cppcheck/${app_name}/suppress_types.list"
56
57echo "-------------------------------------------"
58echo "Suppress options:" "$CPP_CHECK_SUPPRESS"
59echo "-------------------------------------------"
60echo "Additional files:" "$CPP_CHECK_FILES"
61echo "-------------------------------------------"
62echo "Ignoring files:" "$CPP_CHECK_IGNORE_FILES"
63echo "-------------------------------------------"
64echo "CppCheck scope of messages defined with option " ${SCOPE}
65echo "-------------------------------------------"
66echo "Run CppCheck for platform" ${platform}
67echo "-------------------------------------------"
68echo "Defines passed to CppCheck:"
69echo ${app_defines}
70echo "-------------------------------------------"
71echo "Include dirs passed to CppCheck:"
72echo ${app_includes}
73echo "-------------------------------------------"
74
75mkdir -p cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_html
76
77dos2unix cppcheck/${app_name}/suppress_messages.list
78
79dos2unix cppcheck/${app_name}/ignore_files.list
80
81#Generate file with list of additional files for cppcheck
82CPP_CHECK_FILES_LIST_FILE=cppcheck/${app_name}/cpp-check-files.list
83echo ${CPP_CHECK_FILES} | sed -e "s+ +\n+g" > ${CPP_CHECK_FILES_LIST_FILE}
84
85#Generate xml file
86cppcheck ${SCOPE} ${CPP_CHECK_SUPPRESS} -D__GNUC__ -D${platform} ${app_defines} ${app_includes} --file-list=${CPP_CHECK_FILES_LIST_FILE} ${CPP_CHECK_IGNORE_FILES} \
87 --xml 2> cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.xml
88
89#Generate html file
90python cppcheck/cppcheck-htmlreport.py --file=cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.xml --report-dir=cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_html --title=${app_name}
91
92cppcheck ${SCOPE} ${CPP_CHECK_SUPPRESS} -D__GNUC__ -D${platform} ${app_defines} ${app_includes} --file-list=${CPP_CHECK_FILES_LIST_FILE} ${CPP_CHECK_IGNORE_FILES} \
93 --template="{severity}\n{id}\n{message}\n{file}\n{line}:{column}\n{code}\n" 2> cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full
94
95#Generate csv file
96echo "severity@id@message@file@line" > cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.csv
97while IFS= read -r line
98do
99 read -r line2
100 read -r line3
101 read -r line4
102 read -r line5
103 line4=$(echo $line4 | sed 's/.*\\cy_mcuboot\\//' | tr '\\' '/')
104 if grep -xq "${line}@${line2}@${line3}@${line4}@${line5}" cppcheck/${app_name}/suppress_messages.list
105 then
106 :;#suppress current warning
107 else
108 if grep -xq "${line4}" cppcheck/${app_name}/ignore_files.list
109 then
110 :;#suppress current warning
111 else
112 echo ${line}@${line2}@${line3}@${line4}@${line5}
113 fi
114 fi
115 read -r line
116 read -r line
117 read -r line
118done \
119< cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full \
120>>cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.csv
121
122#Generate log file
123while IFS= read -r line
124do
125 read -r line2
126 read -r line3
127 read -r line4
128 read -r line5
129 line4=$(echo $line4 | sed 's/.*\\cy_mcuboot\\//' | tr '\\' '/')
130 if grep -xq "${line}@${line2}@${line3}@${line4}@${line5}" cppcheck/${app_name}/suppress_messages.list
131 then
132 read -r line
133 read -r line
134 read -r line
135 else
136 if grep -xq "${line4}" cppcheck/${app_name}/ignore_files.list
137 then
138 read -r line
139 read -r line
140 read -r line
141 else
142 echo ${line} : ${line2}
143 echo ${line3}
144 echo "${line4} (${line5})"
145 read -r line
146 echo ${line}
147 read -r line
148 echo ${line}
149 read -r line
150 echo "-------------------------------------------"
151 fi
152 fi
153done \
154< cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full \
155> cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.log
156
157rm cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full
158cat cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.log
159
160ERRORS=$(( $(wc -l cppcheck/${app_name}/results/${platform}/${buildcfg}/cppcheck_report_${scope}.csv | cut -d' ' -f1) -1 ))
161echo "${app_name} CPPCHECK FOR ${platform} KIT FOUND $ERRORS ERRORS"
162exit $ERRORS