Merge branch 'maint-0.4.6'
[tor.git] / scripts / coccinelle / check_cocci_parse.sh
blobaaa586c0933191fcb93a36ceb53bc3841b0243b2
1 #!/bin/sh
3 # If we have coccinelle installed, run try_parse.sh on every filename passed
4 # as an argument. If no filenames are supplied, scan a standard Tor 0.3.5 or
5 # later directory layout.
7 # Uses the default coccinelle exceptions file, or $TOR_COCCI_EXCEPTIONS_FILE,
8 # if it is set.
10 # Use TOR_COCCI_EXCEPTIONS_FILE=/dev/null check_cocci_parse.sh to disable
11 # the default exception file.
13 # If spatch is not installed, remind the user to install it, but exit with
14 # a success error status.
16 scripts_cocci="$(dirname "$0")"
17 top="$scripts_cocci/../.."
18 try_parse="$scripts_cocci/try_parse.sh"
20 exitcode=0
22 export TOR_COCCI_EXCEPTIONS_FILE="${TOR_COCCI_EXCEPTIONS_FILE:-$scripts_cocci/exceptions.txt}"
24 PURPOSE="cocci C parsing"
26 echo "Checking spatch:"
28 if ! command -v spatch ; then
29 echo "Install coccinelle's spatch to check $PURPOSE."
30 exit "$exitcode"
33 # Returns true if $1 is greater than or equal to $2
34 version_ge()
36 if test "$1" = "$2" ; then
37 # return true
38 return 0
40 LOWER_VERSION="$(printf '%s\n' "$1" "$2" | $SORT_V | head -n 1)"
41 # implicit return
42 test "$LOWER_VERSION" != "$1"
45 # 'sort -V' is a gnu extension
46 SORT_V="sort -V"
47 # Use 'sort -n' if 'sort -V' doesn't work
48 if ! version_ge "1" "0" ; then
49 echo "Your 'sort -V' command appears broken. Falling back to 'sort -n'."
50 echo "Some spatch version checks may give the wrong result."
51 SORT_V="sort -n"
54 # Print the full spatch version, for diagnostics
55 spatch --version
57 MIN_SPATCH_V="1.0.4"
58 # This pattern needs to handle version strings like:
59 # spatch version 1.0.0-rc19
60 # spatch version 1.0.6 compiled with OCaml version 4.05.0
61 SPATCH_V=$(spatch --version | head -1 | \
62 sed 's/spatch version \([0-9][^ ]*\).*/\1/')
64 if ! version_ge "$SPATCH_V" "$MIN_SPATCH_V" ; then
65 echo "Tor requires coccinelle spatch >= $MIN_SPATCH_V to check $PURPOSE."
66 echo "But you have $SPATCH_V. Please install a newer version."
67 exit "$exitcode"
70 if test $# -ge 1 ; then
71 "$try_parse" "$@"
72 exitcode=$?
73 else
74 cd "$top" || exit 1
75 # This is the layout in 0.3.5
76 # Keep these lists consistent:
77 # - OWNED_TOR_C_FILES in Makefile.am
78 # - CHECK_FILES in pre-commit.git-hook and pre-push.git-hook
79 # - try_parse in check_cocci_parse.sh
80 "$try_parse" \
81 src/lib/*/*.[ch] \
82 src/core/*/*.[ch] \
83 src/feature/*/*.[ch] \
84 src/app/*/*.[ch] \
85 src/test/*.[ch] \
86 src/test/*/*.[ch] \
87 src/tools/*.[ch]
88 exitcode=$?
91 if test "$exitcode" != 0 ; then
92 echo "Please fix these $PURPOSE errors in the above files"
93 echo "Set VERBOSE=1 for more details"
94 echo "Try running test-operator-cleanup or 'make autostyle-operators'"
95 echo "As a last resort, you can modify scripts/coccinelle/exceptions.txt"
98 exit "$exitcode"