1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#!/usr/bin/env bash
#
# cppcheck.sh
# Script to run CppCheck Static Analyzer.
# http://cppcheck.sourceforge.net/
#
# Usage: tools/cppcheck/cppcheck.sh [options] [file]
# Where options can be:
# -a disable suppression list (see $CPPCHECK_DIR/suppressions)
# -c colorize html output
# -h html output (default is gcc)
# -x xml output (default is gcc)
# -j n threads (default: 4)
# -l n check files from the last [n] commits
# -o check modified files
# -v quiet mode
# If argument file is omitted then checking all files in the current directory.
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
# Copyright 2012 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
CPPCHECK=$(type -p cppcheck)
CPPCHECK_DIR=$(dirname "$0")
if [ -z "$CPPCHECK" ] ; then
echo "cppcheck not found"
exit 1
fi
THREADS=4
LAST_COMMITS=0
TARGET=""
QUIET="--quiet"
SUPPRESSIONS="--suppressions-list=$CPPCHECK_DIR/suppressions"
INCLUDES="--includes-file=$CPPCHECK_DIR/includes"
LIBRARIES="--library=gtk --library=qt --library=$CPPCHECK_DIR/glib-patches.cfg --library=$CPPCHECK_DIR/ws-includes.cfg"
MODE="gcc"
COLORIZE_HTML_MODE="no"
OPEN_FILES="no"
XML_ARG=""
colorize_worker()
{
# always uses stdin/stdout
[ "$COLORIZE_HTML_MODE" = "yes" ] && \
sed -e '/<td>warning<\/td>/s/^<tr>/<tr bgcolor="#ff3">/' \
-e '/<td>error<\/td>/s/^<tr>/<tr bgcolor="#faa">/' \
|| sed ''
}
# switcher
colorize()
{
[ -z "$1" ] && colorize_worker || colorize_worker <<< "$1"
}
exit_cleanup() {
if [ "$MODE" = "html" ]; then
echo "</table></body></html>"
fi
if [ -n "$1" ] ; then
exit "$1"
fi
}
while getopts "achxj:l:ov" OPTCHAR ; do
case $OPTCHAR in
a) SUPPRESSIONS=" " ;;
c) COLORIZE_HTML_MODE="yes" ;;
h) MODE="html" ;;
x) MODE="xml" ;;
j) THREADS="$OPTARG" ;;
l) LAST_COMMITS="$OPTARG" ;;
o) OPEN_FILES="yes" ;;
v) QUIET=" " ;;
*) printf "Unknown option %s" "$OPTCHAR"
esac
done
shift $(( OPTIND - 1 ))
if [ "$MODE" = "gcc" ]; then
TEMPLATE="gcc"
elif [ "$MODE" = "html" ]; then
echo "<html><body><table border=1>"
echo "<tr><th>File</th><th>Line</th><th>Severity</th>"
echo "<th>Message</th><th>ID</th></tr>"
TEMPLATE="<tr><td>{file}</td><td>{line}</td><td>{severity}</td><td>{message}</td><td>{id}</td></tr>"
fi
# Ensure that the COLORIZE_HTML_MODE option is used only with HTML-mode and not with GCC-mode.
[ "$MODE" = "html" ] && [ "$COLORIZE_HTML_MODE" = "yes" ] || COLORIZE_HTML_MODE="no"
if [ "$LAST_COMMITS" -gt 0 ] ; then
TARGET=$( git diff --name-only --diff-filter=d HEAD~"$LAST_COMMITS".. | grep -E '\.(c|cpp)$' )
if [ -z "${TARGET//[[:space:]]/}" ] ; then
>&2 echo "No C or C++ files found in the last $LAST_COMMITS commit(s)."
exit_cleanup 0
fi
fi
if [ "$OPEN_FILES" = "yes" ] ; then
TARGET=$(git diff --name-only | grep -E '\.(c|cpp)$' )
TARGET="$TARGET $(git diff --staged --name-only | grep -E '\.(c|cpp)$' )"
if [ -z "${TARGET//[[:space:]]/}" ] ; then
>&2 echo "No C or C++ files are currently opened (modified or added for next commit)."
exit_cleanup 0
fi
fi
if [ $# -gt 0 ]; then
TARGET="$TARGET $*"
fi
if [ -z "$TARGET" ] ; then
TARGET=.
fi
if [ "$MODE" = "xml" ]; then
XML_ARG="--xml"
fi
# Use a little-documented feature of the shell to pass SIGINTs only to the
# child process (cppcheck in this case). That way the final 'echo' still
# runs and we aren't left with broken HTML.
trap : INT
if [ "$QUIET" = " " ]; then
echo "Examining:"
echo $TARGET
echo
fi
# shellcheck disable=SC2086
$CPPCHECK --force --enable=style $QUIET \
$SUPPRESSIONS $INCLUDES $LIBRARIES \
-i doc/ \
-i epan/dissectors/asn1/ \
--std=c11 --template=$TEMPLATE \
-j $THREADS $TARGET $XML_ARG 2>&1 | colorize
exit_cleanup
#
# Editor modelines - https://www.wireshark.org/tools/modelines.html
#
# Local variables:
# c-basic-offset: 4
# tab-width: 8
# indent-tabs-mode: nil
# End:
#
# vi: set shiftwidth=4 tabstop=8 expandtab:
# :indentSize=4:tabSize=8:noTabs=true:
#
|