Blame view

scripts/coccicheck 4.65 KB
9e3955507   Nicolas Palix   Coccinelle: Clean...
1
  #!/bin/bash
74425eee7   Nicolas Palix   Add a target to u...
2

ec97946ed   Nicolas Palix   Coccinelle: Updat...
3
4
5
6
  #
  # This script requires at least spatch
  # version 1.0.0-rc11.
  #
74425eee7   Nicolas Palix   Add a target to u...
7
  SPATCH="`which ${SPATCH:=spatch}`"
90d06a468   Kees Cook   coccicheck: span ...
8
9
  trap kill_running SIGTERM SIGINT
  declare -a SPATCH_PID
26e567209   Bernd Schubert   coccicheck: Allow...
10
11
12
13
  # 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
90d06a468   Kees Cook   coccicheck: span ...
14
  	VERBOSE="$V"
26e567209   Bernd Schubert   coccicheck: Allow...
15
16
17
  else
  	VERBOSE=0
  fi
90d06a468   Kees Cook   coccicheck: span ...
18
19
20
21
22
  if [ -z "$J" ]; then
  	NPROC=$(getconf _NPROCESSORS_ONLN)
  else
  	NPROC="$J"
  fi
93f144684   Nicolas Palix   Coccinelle: Updat...
23
  FLAGS="$SPFLAGS --very-quiet"
9e3955507   Nicolas Palix   Coccinelle: Clean...
24
25
26
27
28
  
  # 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}
1e9dea2a6   Nicolas Palix   Add support for t...
29
30
  if [ "$C" = "1" -o "$C" = "2" ]; then
      ONLINE=1
9e3955507   Nicolas Palix   Coccinelle: Clean...
31
32
33
      # Take only the last argument, which is the C file to test
      shift $(( $# - 1 ))
      OPTIONS="$COCCIINCLUDE $1"
1e9dea2a6   Nicolas Palix   Add support for t...
34
35
  else
      ONLINE=0
d0bc1fb46   Greg Dietsche   coccicheck: add M...
36
      if [ "$KBUILD_EXTMOD" = "" ] ; then
93f144684   Nicolas Palix   Coccinelle: Updat...
37
          OPTIONS="--dir $srctree $COCCIINCLUDE"
d0bc1fb46   Greg Dietsche   coccicheck: add M...
38
      else
93f144684   Nicolas Palix   Coccinelle: Updat...
39
          OPTIONS="--dir $KBUILD_EXTMOD $COCCIINCLUDE"
d0bc1fb46   Greg Dietsche   coccicheck: add M...
40
      fi
1e9dea2a6   Nicolas Palix   Add support for t...
41
  fi
bad6a4092   Nicolas Palix   Coccinelle: Fix p...
42
  if [ "$KBUILD_EXTMOD" != "" ] ; then
93f144684   Nicolas Palix   Coccinelle: Updat...
43
      OPTIONS="--patch $srctree $OPTIONS"
bad6a4092   Nicolas Palix   Coccinelle: Fix p...
44
  fi
74425eee7   Nicolas Palix   Add a target to u...
45
46
47
48
49
50
  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
1e9dea2a6   Nicolas Palix   Add support for t...
51
      if [ "$ONLINE" = "0" ] ; then
1f0a6742d   Nicolas Palix   Coccinelle: Make ...
52
53
  	echo 'You have not explicitly specified the mode to use. Using default "report" mode.'
  	echo 'Available modes are the following: patch, report, context, org'
1e9dea2a6   Nicolas Palix   Add support for t...
54
  	echo 'You can specify the mode with "make coccicheck MODE=<mode>"'
1f0a6742d   Nicolas Palix   Coccinelle: Make ...
55
56
57
58
59
60
61
62
63
  	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'
1e9dea2a6   Nicolas Palix   Add support for t...
64
      fi
03ee0c42a   Nicolas Palix   Coccinelle: Use t...
65
  elif [ "$MODE" = "report" -o "$MODE" = "org" ] ; then
93f144684   Nicolas Palix   Coccinelle: Updat...
66
      FLAGS="$FLAGS --no-show-diff"
74425eee7   Nicolas Palix   Add a target to u...
67
  fi
1e9dea2a6   Nicolas Palix   Add support for t...
68
69
70
71
72
73
  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
74425eee7   Nicolas Palix   Add a target to u...
74

5303265a4   Bernd Schubert   coccicheck: Allow...
75
  run_cmd() {
90d06a468   Kees Cook   coccicheck: span ...
76
  	local i
5303265a4   Bernd Schubert   coccicheck: Allow...
77
  	if [ $VERBOSE -ne 0 ] ; then
90d06a468   Kees Cook   coccicheck: span ...
78
  		echo "Running ($NPROC in parallel): $@"
5303265a4   Bernd Schubert   coccicheck: Allow...
79
  	fi
90d06a468   Kees Cook   coccicheck: span ...
80
  	for i in $(seq 0 $(( NPROC - 1)) ); do
93f144684   Nicolas Palix   Coccinelle: Updat...
81
  		eval "$@ --max $NPROC --index $i &"
90d06a468   Kees Cook   coccicheck: span ...
82
83
84
85
86
87
  		SPATCH_PID[$i]=$!
  		if [ $VERBOSE -eq 2 ] ; then
  			echo "${SPATCH_PID[$i]} running"
  		fi
  	done
  	wait
5303265a4   Bernd Schubert   coccicheck: Allow...
88
  }
90d06a468   Kees Cook   coccicheck: span ...
89
90
91
92
93
94
95
96
  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
  }
5303265a4   Bernd Schubert   coccicheck: Allow...
97

1e9dea2a6   Nicolas Palix   Add support for t...
98
  coccinelle () {
74425eee7   Nicolas Palix   Add a target to u...
99
      COCCI="$1"
74425eee7   Nicolas Palix   Add a target to u...
100
101
  
      OPT=`grep "Option" $COCCI | cut -d':' -f2`
74425eee7   Nicolas Palix   Add a target to u...
102

93f144684   Nicolas Palix   Coccinelle: Updat...
103
  #   The option '--parse-cocci' can be used to syntactically check the SmPL files.
1e9dea2a6   Nicolas Palix   Add support for t...
104
105
  #
  #    $SPATCH -D $MODE $FLAGS -parse_cocci $COCCI $OPT > /dev/null
74425eee7   Nicolas Palix   Add a target to u...
106

35d88a387   Nicolas Palix   Coccinelle: Resto...
107
      if [ $VERBOSE -ne 0 -a $ONLINE -eq 0 ] ; then
74425eee7   Nicolas Palix   Add a target to u...
108

1e9dea2a6   Nicolas Palix   Add support for t...
109
  	FILE=`echo $COCCI | sed "s|$srctree/||"`
74425eee7   Nicolas Palix   Add a target to u...
110

3c9084176   Nicolas Palix   Coccinelle: Impro...
111
112
113
  	echo "Processing `basename $COCCI`"
  	echo "with option(s) \"$OPT\""
  	echo ''
1e9dea2a6   Nicolas Palix   Add support for t...
114
  	echo 'Message example to submit a patch:'
3c9084176   Nicolas Palix   Coccinelle: Impro...
115
  	sed -ne 's|^///||p' $COCCI
1e9dea2a6   Nicolas Palix   Add support for t...
116

062c1825a   Nicolas Palix   Coccinelle: Add c...
117
118
119
120
121
122
123
124
125
126
127
  	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
1e9dea2a6   Nicolas Palix   Add support for t...
128
129
130
131
132
  	echo " in $FILE."
  	echo ''
  	echo ' More information about semantic patching is available at'
  	echo ' http://coccinelle.lip6.fr/'
  	echo ''
3c9084176   Nicolas Palix   Coccinelle: Impro...
133
134
135
136
137
  	if [ "`sed -ne 's|^//#||p' $COCCI`" ] ; then
  	    echo 'Semantic patch information:'
  	    sed -ne 's|^//#||p' $COCCI
  	    echo ''
  	fi
2c1160c87   Nicolas Palix   Coccinelle: Add a...
138
      fi
3c9084176   Nicolas Palix   Coccinelle: Impro...
139

2c1160c87   Nicolas Palix   Coccinelle: Add a...
140
      if [ "$MODE" = "chain" ] ; then
5303265a4   Bernd Schubert   coccicheck: Allow...
141
  	run_cmd $SPATCH -D patch   \
93f144684   Nicolas Palix   Coccinelle: Updat...
142
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
5303265a4   Bernd Schubert   coccicheck: Allow...
143
  	run_cmd $SPATCH -D report  \
93f144684   Nicolas Palix   Coccinelle: Updat...
144
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || \
5303265a4   Bernd Schubert   coccicheck: Allow...
145
  	run_cmd $SPATCH -D context \
93f144684   Nicolas Palix   Coccinelle: Updat...
146
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS               || \
5303265a4   Bernd Schubert   coccicheck: Allow...
147
  	run_cmd $SPATCH -D org     \
93f144684   Nicolas Palix   Coccinelle: Updat...
148
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff || exit 1
c05cd6ddb   Nicolas Palix   coccicheck: Add t...
149
      elif [ "$MODE" = "rep+ctxt" ] ; then
5303265a4   Bernd Schubert   coccicheck: Allow...
150
  	run_cmd $SPATCH -D report  \
93f144684   Nicolas Palix   Coccinelle: Updat...
151
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS --no-show-diff && \
5303265a4   Bernd Schubert   coccicheck: Allow...
152
  	run_cmd $SPATCH -D context \
93f144684   Nicolas Palix   Coccinelle: Updat...
153
  		$FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a6   Nicolas Palix   Add support for t...
154
      else
93f144684   Nicolas Palix   Coccinelle: Updat...
155
  	run_cmd $SPATCH -D $MODE   $FLAGS --cocci-file $COCCI $OPT $OPTIONS || exit 1
1e9dea2a6   Nicolas Palix   Add support for t...
156
      fi
74425eee7   Nicolas Palix   Add a target to u...
157

74425eee7   Nicolas Palix   Add a target to u...
158
159
160
161
  }
  
  if [ "$COCCI" = "" ] ; then
      for f in `find $srctree/scripts/coccinelle/ -name '*.cocci' -type f | sort`; do
1e9dea2a6   Nicolas Palix   Add support for t...
162
  	coccinelle $f
74425eee7   Nicolas Palix   Add a target to u...
163
164
      done
  else
1e9dea2a6   Nicolas Palix   Add support for t...
165
      coccinelle $COCCI
74425eee7   Nicolas Palix   Add a target to u...
166
  fi