coccicheck 4.7 KB
Newer Older
1
#!/bin/bash
2

3 4 5 6 7
#
# This script requires at least spatch
# version 1.0.0-rc11.
#

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

10 11 12 13 14
if [ ! -x "$SPATCH" ]; then
    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
    exit 1
fi

K
Kees Cook 已提交
15 16 17
trap kill_running SIGTERM SIGINT
declare -a SPATCH_PID

18 19 20 21
# 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 已提交
22
	VERBOSE="$V"
23 24 25 26
else
	VERBOSE=0
fi

K
Kees Cook 已提交
27 28 29 30 31 32
if [ -z "$J" ]; then
	NPROC=$(getconf _NPROCESSORS_ONLN)
else
	NPROC="$J"
fi

33
FLAGS="--very-quiet"
34 35 36 37

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

40 41 42
if [ "$C" = "1" -o "$C" = "2" ]; then
    ONLINE=1

43 44 45
    # Take only the last argument, which is the C file to test
    shift $(( $# - 1 ))
    OPTIONS="$COCCIINCLUDE $1"
46 47
else
    ONLINE=0
48
    if [ "$KBUILD_EXTMOD" = "" ] ; then
49
        OPTIONS="--dir $srctree $COCCIINCLUDE"
50
    else
51
        OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
52
    fi
53 54
fi

55
if [ "$KBUILD_EXTMOD" != "" ] ; then
56
    OPTIONS="--patch $srctree $OPTIONS"
57 58
fi

59
if [ "$MODE" = "" ] ; then
60
    if [ "$ONLINE" = "0" ] ; then
61 62
	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
	echo 'Available modes are the following: patch, report, context, org'
63
	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
64 65 66 67 68 69 70 71 72
	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'
73
    fi
74
elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
75
    FLAGS="--no-show-diff $FLAGS"
76 77
fi

78 79 80 81 82 83
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
84

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

K
Kees Cook 已提交
100
kill_running() {
101
	for i in $(seq 0 $(( NPROC - 1 )) ); do
K
Kees Cook 已提交
102 103 104 105 106 107
		if [ $VERBOSE -eq 2 ] ; then
			echo "Killing ${SPATCH_PID[$i]}"
		fi
		kill ${SPATCH_PID[$i]} 2>/dev/null
	done
}
108

109 110 111
# You can override heuristics with SPFLAGS, these must always go last
OPTIONS="$OPTIONS $SPFLAGS"

112
coccinelle () {
113 114 115 116
    COCCI="$1"

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

117
#   The option '--parse-cocci' can be used to syntactically check the SmPL files.
118 119
#
#    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
120

121
    if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
122

123
	FILE=`echo $COCCI | sed "s|$srctree/||"`
124

125 126 127
	echo "Processing `basename $COCCI`"
	echo "with option(s) \"$OPT\""
	echo ''
128 129
	echo 'Message example to submit a patch:'

130
	sed -ne 's|^///||p' $COCCI
131

N
Nicolas Palix 已提交
132 133 134 135 136 137 138 139 140 141 142
	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
143 144 145 146 147 148
	echo " in $FILE."
	echo ''
	echo ' More information about semantic patching is available at'
	echo ' http://coccinelle.lip6.fr/'
	echo ''

149 150 151 152 153
	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
	    echo 'Semantic patch information:'
	    sed -ne 's|^//#||p' $COCCI
	    echo ''
	fi
154
    fi
155

156
    if [ "$MODE" = "chain" ] ; then
157
	run_cmd $SPATCH -D patch   \
158
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
159
	run_cmd $SPATCH -D report  \
160
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
161
	run_cmd $SPATCH -D context \
162
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
163
	run_cmd $SPATCH -D org     \
164
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
N
Nicolas Palix 已提交
165
    elif [ "$MODE" = "rep+ctxt" ] ; then
166
	run_cmd $SPATCH -D report  \
167
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
168
	run_cmd $SPATCH -D context \
169
		$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
170
    else
171
	run_cmd $SPATCH -D $MODE   $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
172
    fi
173 174 175 176 177

}

if [ "$COCCI" = "" ] ; then
    for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
178
	coccinelle $f
179 180
    done
else
181
    coccinelle $COCCI
182
fi