Skip to content

Commit

Permalink
Adds static analysis script
Browse files Browse the repository at this point in the history
  • Loading branch information
Figueroa committed Mar 11, 2024
1 parent b78fb53 commit ac1a10d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
14 changes: 9 additions & 5 deletions runCFSscript.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

cfs_apps=( apps/sch_lab apps/ci_lab apps/to_lab apps/sample_app tools/cFS-GroundSystem tools/elf2cfetbl tools/tblCRCTool apps/cf apps/cs apps/ds apps/fm apps/hk apps/hs apps/lc apps/md apps/mm apps/sc )
scriptNums=( 1 2 3 4 5 6 7 8 )
scriptNums=( 1 2 3 4 5 6 7 8 9 )

# Process each element to get the substring after the last '/'
for app in "${cfs_apps[@]}"; do
Expand All @@ -23,14 +23,15 @@ cat << EOF
4) format-check.sh
5) run-clang-format.sh
6) dox.sh
7) cfe_functionaltests.sh
8) cfe_lcov.sh
7) static-analysis.sh
8) cfe_functionaltests.sh
9) cfe_lcov.sh
EOF

read userNum
while [[ ! " ${scriptNums[*]} " =~ " ${userNum} " ]]; do echo "try again"; read userNum; done

if [[ "$userNum" -ge 2 && "$userNum" -le 6 ]]; then
if [[ "$userNum" -ge 2 && "$userNum" -le 7 ]]; then
# get user cFS application
echo -e "\n\
Enter the cFS application name that you want to run the script on\n\
Expand Down Expand Up @@ -63,9 +64,12 @@ case $userNum in
./scripts/dox.sh "${userApp##*/}"
;;
7)
./scripts/cfe_functionaltests.sh
./scripts/static-analysis.sh "${userApp##*/}"
;;
8)
./scripts/cfe_functionaltests.sh
;;
9)
./scripts/cfe_lcov.sh
;;
esac
77 changes: 77 additions & 0 deletions scripts/static-analysis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

app=$1

errorDir="${app}_staticAnalysis_errors"
DIR=$(pwd) # Save the current working directory to DIR
cppcheck_xslt_path="nasa/cFS/main/.github/scripts"
mkdir ${errorDir}

# software dependencies
sudo apt-get install cppcheck xsltproc -y
npm install @microsoft/sarif-multitool

if [[ " $applist " =~ " $app " ]]; then
# apps
source="apps/${app}"
strict_dir_list="./fsw"
cmake_project_options=""
elif [ ${app} = "cfe" ]; then
# cfe
source="cfe"
strict_dir_list="./modules/core_api/fsw ./modules/core_private/fsw ./modules/es/fsw ./modules/evs/fsw ./modules/fs/fsw ./modules/msg/fsw ./modules/resourceid/fsw ./modules/sb/fsw ./modules/sbr/fsw ./modules/tbl/fsw ./modules/time/fsw -UCFE_PLATFORM_TIME_CFG_CLIENT -DCFE_PLATFORM_TIME_CFG_SERVER"
cmake_project_options=""
elif [ ${app} = "osal" ]; then
# osal
source="osal"
strict_dir_list="./src/bsp ./src/os"
cmake_project_options="-DENABLE_UNIT_TESTS=TRUE -DOSAL_OMIT_DEPRECATED=TRUE -DOSAL_SYSTEM_BSPTYPE=generic-linux"
elif [ ${app} = "psp" ]; then
# psp
source="psp"
strict_dir_list="./fsw"
cmake_project_options=""
fi

# Fetch conversion XSLT
wget -O cppcheck-xml2text.xslt https://raw.githubusercontent.com/${cppcheck_xslt_path}/cppcheck-xml2text.xslt
wget -O cppcheck-merge.xslt https://raw.githubusercontent.com/${cppcheck_xslt_path}/cppcheck-merge.xslt

# For a CMake-based project, get the list of files by setting up a build with CMAKE_EXPORT_COMPILE_COMMANDS=ON and
# referencing the compile_commands.json file produced by the tool. This will capture the correct include paths and
# compile definitions based on how the source is actually compiled.
if [ -n "$cmake_project_options" ]; then
# CMake Setup
cmake -DCMAKE_INSTALL_PREFIX=$DIR/staging -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=debug ${cmake_project_options} -S ${source} -B build
export CPPCHECK_OPTS="--project=${DIR}/build/compile_commands.json"
else
# Non-CMake Setup
export CPPCHECK_OPTS="${DIR}/${source}"
fi

# Run general cppcheck
cppcheck --force --inline-suppr --xml ${CPPCHECK_OPTS} 2> ${errorDir}/cppcheck_err.xml

# Run strict static analysis for selected portions of source code
if [ -n "$strict_dir_list" ]; then

cd ${source}

# Run stric cpp check
cppcheck --force --inline-suppr --std=c99 --language=c --enable=warning,performance,portability,style --suppress=variableScope --inconclusive --xml ${strict_dir_list} 2> ${DIR}/${errorDir}/strict_cppcheck_err.xml

cd "${DIR}"

# Merge cppcheck results
mv ${errorDir}/cppcheck_err.xml ${errorDir}/general_cppcheck_err.xml
xsltproc --stringparam merge_file ${errorDir}/strict_cppcheck_err.xml cppcheck-merge.xslt ${errorDir}/general_cppcheck_err.xml > ${errorDir}/cppcheck_err.xml
fi

# Convert cppcheck results to SARIF
npx "@microsoft/sarif-multitool" convert "${errorDir}/cppcheck_err.xml" --tool "CppCheck" --output "${errorDir}/cppcheck_err.sarif"

# Convert cppcheck results to Markdown
xsltproc cppcheck-xml2text.xslt ${errorDir}/cppcheck_err.xml | tee ${errorDir}/cppcheck_err.txt

# Check for reported errors
tail -n 1 ${errorDir}/cppcheck_err.txt | grep -q '^\*\*0 error(s) reported\*\*$'

0 comments on commit ac1a10d

Please sign in to comment.