From 5e85302663fc6dc172b96a34fd4a440676a4d42d Mon Sep 17 00:00:00 2001 From: Paul Moore Date: Mon, 21 Oct 2013 10:50:18 -0400 Subject: tools: add a basic C style/format checking tool This is far from perfect, but it is something, and it provides an objective answer to "how do I style my code for this project?". Signed-off-by: Paul Moore --- Makefile | 6 ++- tools/check-syntax | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100755 tools/check-syntax diff --git a/Makefile b/Makefile index 932fb6c..1f61f20 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,8 @@ CONFIGS = configure.mk configure.h version_info.mk libseccomp.pc SUBDIRS_BUILD = include src tests tools SUBDIRS_INSTALL = include src tools doc -.PHONY: tarball install check ctags cstags clean dist-clean $(SUBDIRS_BUILD) +.PHONY: tarball install check check-syntax ctags cstags clean dist-clean \ + $(SUBDIRS_BUILD) all: $(SUBDIRS_BUILD) @@ -102,6 +103,9 @@ check: tools tests @$(ECHO_INFO) "checking in directory tests/ ..." @$(MAKE) -C tests check +check-syntax: + @./tools/check-syntax + ctags: @$(ECHO_INFO) "generating ctags for the project ..." @ctags -R * diff --git a/tools/check-syntax b/tools/check-syntax new file mode 100755 index 0000000..b5ae9cc --- /dev/null +++ b/tools/check-syntax @@ -0,0 +1,116 @@ +#!/bin/bash + +# +# libseccomp code syntax checking tool +# +# Copyright (c) 2013 Red Hat +# Author: Paul Moore +# + +# +# This library is free software; you can redistribute it and/or modify it +# under the terms of version 2.1 of the GNU Lesser General Public License as +# published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +# for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library; if not, see . +# + +CHK_C_LIST="include/seccomp.h.in \ + src/*.c src/*.h \ + tests/*.c tests/*.h \ + tools/*.c tools/*.h" +CHK_C_EXCLUDE="src/hash.c" + +#### +# functions + +# +# Dependency verification +# +# Arguments: +# 1 Dependency to check for +# +function verify_deps() { + [[ -z "$1" ]] && return + if ! which "$1" >& /dev/null; then + echo "error: install \"$1\" and include it in your \$PATH" + exit 1 + fi +} + +# +# Print out script usage details +# +function usage() { +cat << EOF +usage: check-syntax [-h] + +libseccomp code syntax checking tool +optional arguments: + -h show this help message and exit +EOF +} + +# +# Check the formatting on a C source/header file +# +# Arguments: +# 1 File to check +# +function tool_c_style() { + [[ -z "$1" || ! -r "$1" ]] && return + + astyle --options=none --lineend=linux --mode=c \ + --style=linux \ + --indent=force-tab=8 \ + --indent-preprocessor \ + --indent-col1-comments \ + --min-conditional-indent=0 \ + --max-instatement-indent=80 \ + --pad-oper \ + --align-pointer=name \ + --align-reference=name \ + --max-code-length=80 \ + --break-after-logical < "$1" \ + | diff -pu --label="$1" "$1" --label="$1 [CORRECTED]" - +} + +# +# Perform all known syntax checks for the configured C sources/headers +# +function check_c() { + for i in $CHK_C_LIST; do + echo "$CHK_C_EXCLUDE" | grep -q "$i" && continue + echo "Differences for $i" + tool_c_style "$i" + done +} + +#### +# main + +verify_deps astyle + +while getopts "h" opt; do + case $opt in + h|*) + usage + exit 1 + ;; + esac +done + +# display the results +echo "=============== $(date) ===============" +echo "Code Syntax Check Results (\"check-syntax $*\")" +check_c +echo "============================================================" + +# exit +exit 0 -- cgit v1.2.1