[15/33] cocci: Add coccicheck.sh, move igt.cocci

Submitted by Lyude Paul on June 10, 2019, 3:03 p.m.

Details

Message ID 20190610150321.512-15-lyude@redhat.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in IGT - Trybot

Not browsing as part of any series.

Commit Message

Lyude Paul June 10, 2019, 3:03 p.m.
From: Lyude Paul <lyude@redhat.com>

Currently we provide no way to run checks for individual rules, which is
kind of annoying. So, let's take the opportunity to do that and provide
a script, coccicheck.sh, to go through the directory of SmPL rules and
run cocci. This is somewhat similar to the coccicheck target that the
Linux kernel provides, except a good bit more limited since our codebase
is nowhere near as large or complex (and also doesn't use make).

Now we can start splitting out individual rules.

Signed-off-by: Lyude Paul <lyude@redhat.com>
---
 {lib => scripts/cocci}/igt.cocci |   0
 scripts/coccicheck.sh            | 106 +++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+)
 rename {lib => scripts/cocci}/igt.cocci (100%)
 create mode 100755 scripts/coccicheck.sh

Patch hide | download patch | download mbox

diff --git a/lib/igt.cocci b/scripts/cocci/igt.cocci
similarity index 100%
rename from lib/igt.cocci
rename to scripts/cocci/igt.cocci
diff --git a/scripts/coccicheck.sh b/scripts/coccicheck.sh
new file mode 100755
index 00000000..420b379b
--- /dev/null
+++ b/scripts/coccicheck.sh
@@ -0,0 +1,106 @@ 
+#!/bin/sh
+#
+# Copyright © 2019 Red Hat Inc.
+# Authors:
+#   Lyude Paul <lyude@redhat.com>
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice (including the next
+# paragraph) shall be included in all copies or substantial portions of the
+# Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+
+DEFAULT_CHECK_DIRS=( "lib" "tests" )
+COCCI_DIR="$(dirname "$0")/cocci"
+
+# Argument defaults
+nproc=$(nproc)
+mode=report
+
+function print_usage {
+	echo "Usage: coccicheck.sh [options]"
+	echo "Available options:"
+	echo "  -D <mode>    The coccinelle mode to use (default: report)"
+	echo "  -d <dir>     Only check files in dir (may be used more then"
+	echo "               once)"
+	echo "  -f <file>    Only check file (may be used more then once)"
+	echo "  -h           Display this help message"
+	echo "  -a <flags>   Flags to pass to coccinelle"
+	echo "  -p <file>    SmPL file to use. When not specified, defaults to"
+	echo "               running all SmPL files in scripts/cocci"
+	echo "  -j <N>       Number of parallel workers for spatch to use"
+	echo "               (autodetected default: $nproc)"
+	echo ""
+	echo "Only the 'report' and 'patch' modes are supported"
+	echo "See spatch --help for more information"
+}
+
+function usage_error {
+	echo "Error: $@" > /dev/stderr
+	print_usage > /dev/stderr
+	exit 1
+}
+
+declare -a check_dirs
+declare -a check_files
+while getopts ":hqD:d:f:a:p:j:" opt; do
+	case $opt in
+		D) mode="$OPTARG" ;;
+		d) check_dirs+=("$OPTARG") ;;
+		f) check_files+=("$OPTARG") ;;
+		h) print_usage; exit ;;
+		a) sp_flags="$OPTARG" ;;
+		p) sp_file="$OPTARG" ;;
+		j) nproc="$OPTARG" ;;
+		:) usage_error "Option -$OPTARG requires an argument" ;;
+		\?) usage_error "Unknown option: -$OPTARG" ;;
+	esac
+done
+
+# Parse either a -d or -f option, and check if we need to warn the user that
+# they specified multiple dir/file targets, which is unsupported
+
+function run_spatch {
+	if [ "x$sp_file" != "x" ]; then
+		spatch -j"$nproc" -D "$mode" --very-quiet $sp_flags --sp-file "$sp_file" "$@"
+	else
+		find "$COCCI_DIR" -name '*.cocci' \
+			-exec spatch -j"$nproc" -D "$mode" --very-quiet $sp_flags --sp-file '{}' "$@" \;
+	fi
+}
+
+function main {
+	for dir in "${check_dirs[@]}"; do
+		run_spatch --dir "$dir"
+	done
+
+	if [ "${#check_files[@]}" -ne 0 ]; then
+		run_spatch ${check_files[@]}
+	fi
+}
+
+if [ "${#check_dirs[@]}" -eq 0 ] && [ "${#check_files[@]}" -eq 0 ]; then
+	check_dirs=("${DEFAULT_CHECK_DIRS[@]}")
+fi
+
+if [ "$mode" == "report" ]; then
+	# For report mode, remove duplicate results from the output
+	main | sort -u | uniq
+elif [ "$mode" == "patch" ]; then
+	main
+else
+	usage_error "Unknown mode $mode"
+fi