blob: 862b92628f00a15a05fbd2f6adf9d316182e9fbc [file] [log] [blame]
Roman Okhrimenko977b3752022-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 < "${app_name}/cppcheck/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 < "${app_name}/cppcheck/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 ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_html
76
77dos2unix ${app_name}/cppcheck/suppress_messages.list
78
79#Generate xml file
80cppcheck ${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
84python 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
86cppcheck ${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
90echo "severity@id@message@file@line" > ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.csv
91while IFS= read -r line
92do
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
107done \
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
112while IFS= read -r line
113do
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
135done \
136< ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full \
137> ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.log
138
139rm ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.full
140cat ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.log
141
142RC=$(( $(wc -l ${app_name}/cppcheck/results/${platform}/${buildcfg}/cppcheck_report_${scope}.csv | cut -d' ' -f1) -1 ))
143echo "${app_name} CPPCHECK FOR ${platform} KIT FOUND $RC ERRORS"
144
145exit $RC