coccicheck 4.6 KB
Newer Older
1
#!/bin/bash
2 3 4

SPATCH="`which ${SPATCH:=spatch}`"

K
Kees Cook 已提交
5 6 7
trap kill_running SIGTERM SIGINT
declare -a SPATCH_PID

8 9 10 11
# The verbosity may be set by the environmental parameter V=
# as for example with 'make V=1 coccicheck'

if [ -n "$V" -a "$V" != "0" ]; then
K
Kees Cook 已提交
12
	VERBOSE="$V"
13 14 15 16
else
	VERBOSE=0
fi

K
Kees Cook 已提交
17 18 19 20 21 22
if [ -z "$J" ]; then
	NPROC=$(getconf _NPROCESSORS_ONLN)
else
	NPROC="$J"
fi

23
FLAGS="$SPFLAGS --very-quiet"
24 25 26 27 28 29

# spatch only allows include directories with the syntax "-I include"
# while gcc also allows "-Iinclude" and "-include include"
COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
COCCIINCLUDE=${COCCIINCLUDE//-include/-I}

30 31 32
if [ "$C" = "1" -o "$C" = "2" ]; then
    ONLINE=1

33 34 35
    # Take only the last argument, which is the C file to test
    shift $(( $# - 1 ))
    OPTIONS="$COCCIINCLUDE $1"
36 37
else
    ONLINE=0
38
    if [ "$KBUILD_EXTMOD" = "" ] ; then
39
        OPTIONS="--dir $srctree $COCCIINCLUDE"
40
    else
41
        OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
42
    fi
43 44
fi

45
if [ "$KBUILD_EXTMOD" != "" ] ; then
46
    OPTIONS="--patch $srctree $OPTIONS"
47 48
fi

49 50 51 52 53 54
if [ ! -x "$SPATCH" ]; then
    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
    exit 1
fi

if [ "$MODE" = "" ] ; then
55
    if [ "$ONLINE" = "0" ] ; then
56 57
	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
	echo 'Available modes are the following: patch, report, context, org'
58
	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
59 60 61 62 63 64 65 66 67
	echo 'Note however that some modes are not implemented by some semantic patches.'
    fi
    MODE="report"
fi

if [ "$MODE" = "chain" ] ; then
    if [ "$ONLINE" = "0" ] ; then
	echo 'You have selected the "chain" mode.'
	echo 'All available modes will be tried (in that order): patch, report, context, org'
68
    fi
69
elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
70
    FLAGS="$FLAGS --no-show-diff"
71 72
fi

73 74 75 76 77 78
if [ "$ONLINE" = "0" ] ; then
    echo ''
    echo 'Please check for false positives in the output before submitting a patch.'
    echo 'When using "patch" mode, carefully review the patch before submitting it.'
    echo ''
fi
79

80
run_cmd() {
K
Kees Cook 已提交
81
	local i
82
	if [ $VERBOSE -ne 0 ] ; then
K
Kees Cook 已提交
83
		echo "Running ($NPROC in parallel): $@"
84
	fi
K
Kees Cook 已提交
85
	for i in $(seq 0 $(( NPROC - 1)) ); do
86
		eval "$@ --max $NPROC --index $i &"
K
Kees Cook 已提交
87 88 89 90 91 92
		SPATCH_PID[$i]=$!
		if [ $VERBOSE -eq 2 ] ; then
			echo "${SPATCH_PID[$i]} running"
		fi
	done
	wait
93 94
}

K
Kees Cook 已提交
95 96 97 98 99 100 101 102
kill_running() {
	for i in $(seq $(( NPROC - 1 )) ); do
		if [ $VERBOSE -eq 2 ] ; then
			echo "Killing ${SPATCH_PID[$i]}"
		fi
		kill ${SPATCH_PID[$i]} 2>/dev/null
	done
}
103

104
coccinelle () {
105 106 107 108
    COCCI="$1"

    OPT=`grep "Option" $COCCI | cut -d':' -f2`

109
#   The option '--parse-cocci' can be used to syntactically check the SmPL files.
110 111
#
#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
112

113
    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
114

115
	FILE=`echo $COCCI | sed "s|$srctree/||"`
116

117 118 119
	echo "Processing `basename $COCCI`"
	echo "with option(s) \"$OPT\""
	echo ''
120 121
	echo 'Message example to submit a patch:'

122
	sed -ne 's|^///||p' $COCCI
123

N
Nicolas Palix 已提交
124 125 126 127 128 129 130 131 132 133 134
	if [ "$MODE" = "patch" ] ; then
	    echo ' The semantic patch that makes this change is available'
	elif [ "$MODE" = "report" ] ; then
	    echo ' The semantic patch that makes this report is available'
	elif [ "$MODE" = "context" ] ; then
	    echo ' The semantic patch that spots this code is available'
	elif [ "$MODE" = "org" ] ; then
	    echo ' The semantic patch that makes this Org report is available'
	else
	    echo ' The semantic patch that makes this output is available'
	fi
135 136 137 138 139 140
	echo " in $FILE."
	echo ''
	echo ' More information about semantic patching is available at'
	echo ' http://coccinelle.lip6.fr/'
	echo ''

141 142 143 144 145
	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
	    echo 'Semantic patch information:'
	    sed -ne 's|^//#||p' $COCCI
	    echo ''
	fi
146
    fi
147

148
    if [ "$MODE" = "chain" ] ; then
149
	run_cmd $SPATCH -D patch   \
150
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
151
	run_cmd $SPATCH -D report  \
152
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
153
	run_cmd $SPATCH -D context \
154
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
155
	run_cmd $SPATCH -D org     \
156
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
N
Nicolas Palix 已提交
157
    elif [ "$MODE" = "rep+ctxt" ] ; then
158
	run_cmd $SPATCH -D report  \
159
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
160
	run_cmd $SPATCH -D context \
161
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
162
    else
163
	run_cmd $SPATCH -D $MODE   $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
164
    fi
165 166 167 168 169

}

if [ "$COCCI" = "" ] ; then
    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
170
	coccinelle $f
171 172
    done
else
173
    coccinelle $COCCI
174
fi