scripts/coccinelle: check for field address argument to kfree
[linux-2.6-block.git] / scripts / coccicheck
CommitLineData
9e395550 1#!/bin/bash
74425eee
NP
2
3SPATCH="`which ${SPATCH:=spatch}`"
4
26e56720
BS
5# The verbosity may be set by the environmental parameter V=
6# as for example with 'make V=1 coccicheck'
7
8if [ -n "$V" -a "$V" != "0" ]; then
9 VERBOSE=1
10else
11 VERBOSE=0
12fi
13
ed621cc4 14FLAGS="$SPFLAGS -very_quiet"
9e395550
NP
15
16# spatch only allows include directories with the syntax "-I include"
17# while gcc also allows "-Iinclude" and "-include include"
18COCCIINCLUDE=${LINUXINCLUDE//-I/-I }
19COCCIINCLUDE=${COCCIINCLUDE//-include/-I}
20
1e9dea2a
NP
21if [ "$C" = "1" -o "$C" = "2" ]; then
22 ONLINE=1
23
9e395550
NP
24 # Take only the last argument, which is the C file to test
25 shift $(( $# - 1 ))
26 OPTIONS="$COCCIINCLUDE $1"
1e9dea2a
NP
27else
28 ONLINE=0
d0bc1fb4 29 if [ "$KBUILD_EXTMOD" = "" ] ; then
9e395550 30 OPTIONS="-dir $srctree $COCCIINCLUDE"
d0bc1fb4 31 else
bad6a409 32 OPTIONS="-dir $KBUILD_EXTMOD $COCCIINCLUDE"
d0bc1fb4 33 fi
1e9dea2a
NP
34fi
35
bad6a409
NP
36if [ "$KBUILD_EXTMOD" != "" ] ; then
37 OPTIONS="-patch $srctree $OPTIONS"
38fi
39
74425eee
NP
40if [ ! -x "$SPATCH" ]; then
41 echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
42 exit 1
43fi
44
45if [ "$MODE" = "" ] ; then
1e9dea2a 46 if [ "$ONLINE" = "0" ] ; then
1f0a6742
NP
47 echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
48 echo 'Available modes are the following: patch, report, context, org'
1e9dea2a 49 echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
1f0a6742
NP
50 echo 'Note however that some modes are not implemented by some semantic patches.'
51 fi
52 MODE="report"
53fi
54
55if [ "$MODE" = "chain" ] ; then
56 if [ "$ONLINE" = "0" ] ; then
57 echo 'You have selected the "chain" mode.'
58 echo 'All available modes will be tried (in that order): patch, report, context, org'
1e9dea2a 59 fi
03ee0c42 60elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
062c1825 61 FLAGS="$FLAGS -no_show_diff"
74425eee
NP
62fi
63
1e9dea2a
NP
64if [ "$ONLINE" = "0" ] ; then
65 echo ''
66 echo 'Please check for false positives in the output before submitting a patch.'
67 echo 'When using "patch" mode, carefully review the patch before submitting it.'
68 echo ''
69fi
74425eee 70
5303265a
BS
71run_cmd() {
72 if [ $VERBOSE -ne 0 ] ; then
73 echo "Running: $@"
74 fi
75 eval $@
76}
77
78
1e9dea2a 79coccinelle () {
74425eee 80 COCCI="$1"
74425eee
NP
81
82 OPT=`grep "Option" $COCCI | cut -d':' -f2`
74425eee 83
062c1825 84# The option '-parse_cocci' can be used to syntactically check the SmPL files.
1e9dea2a
NP
85#
86# $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
74425eee 87
35d88a38 88 if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
74425eee 89
1e9dea2a 90 FILE=`echo $COCCI | sed "s|$srctree/||"`
74425eee 91
3c908417
NP
92 echo "Processing `basename $COCCI`"
93 echo "with option(s) \"$OPT\""
94 echo ''
1e9dea2a
NP
95 echo 'Message example to submit a patch:'
96
3c908417 97 sed -ne 's|^///||p' $COCCI
1e9dea2a 98
062c1825
NP
99 if [ "$MODE" = "patch" ] ; then
100 echo ' The semantic patch that makes this change is available'
101 elif [ "$MODE" = "report" ] ; then
102 echo ' The semantic patch that makes this report is available'
103 elif [ "$MODE" = "context" ] ; then
104 echo ' The semantic patch that spots this code is available'
105 elif [ "$MODE" = "org" ] ; then
106 echo ' The semantic patch that makes this Org report is available'
107 else
108 echo ' The semantic patch that makes this output is available'
109 fi
1e9dea2a
NP
110 echo " in $FILE."
111 echo ''
112 echo ' More information about semantic patching is available at'
113 echo ' http://coccinelle.lip6.fr/'
114 echo ''
115
3c908417
NP
116 if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
117 echo 'Semantic patch information:'
118 sed -ne 's|^//#||p' $COCCI
119 echo ''
120 fi
2c1160c8 121 fi
3c908417 122
2c1160c8 123 if [ "$MODE" = "chain" ] ; then
5303265a
BS
124 run_cmd $SPATCH -D patch \
125 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
126 run_cmd $SPATCH -D report \
127 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || \
128 run_cmd $SPATCH -D context \
129 $FLAGS -sp_file $COCCI $OPT $OPTIONS || \
130 run_cmd $SPATCH -D org \
131 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff || exit 1
c05cd6dd 132 elif [ "$MODE" = "rep+ctxt" ] ; then
5303265a
BS
133 run_cmd $SPATCH -D report \
134 $FLAGS -sp_file $COCCI $OPT $OPTIONS -no_show_diff && \
135 run_cmd $SPATCH -D context \
136 $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a 137 else
5303265a 138 run_cmd $SPATCH -D $MODE $FLAGS -sp_file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a 139 fi
74425eee 140
74425eee
NP
141}
142
143if [ "$COCCI" = "" ] ; then
144 for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1e9dea2a 145 coccinelle $f
74425eee
NP
146 done
147else
1e9dea2a 148 coccinelle $COCCI
74425eee 149fi