aboutsummaryrefslogtreecommitdiff
path: root/scripts/coccinelle/check_cocci_parse.sh
blob: aaa586c0933191fcb93a36ceb53bc3841b0243b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/sh

# If we have coccinelle installed, run try_parse.sh on every filename passed
# as an argument. If no filenames are supplied, scan a standard Tor 0.3.5 or
# later directory layout.
#
# Uses the default coccinelle exceptions file, or $TOR_COCCI_EXCEPTIONS_FILE,
# if it is set.
#
# Use TOR_COCCI_EXCEPTIONS_FILE=/dev/null check_cocci_parse.sh to disable
# the default exception file.
#
# If spatch is not installed, remind the user to install it, but exit with
# a success error status.

scripts_cocci="$(dirname "$0")"
top="$scripts_cocci/../.."
try_parse="$scripts_cocci/try_parse.sh"

exitcode=0

export TOR_COCCI_EXCEPTIONS_FILE="${TOR_COCCI_EXCEPTIONS_FILE:-$scripts_cocci/exceptions.txt}"

PURPOSE="cocci C parsing"

echo "Checking spatch:"

if ! command -v spatch ; then
    echo "Install coccinelle's spatch to check $PURPOSE."
    exit "$exitcode"
fi

# Returns true if $1 is greater than or equal to $2
version_ge()
{
    if test "$1" = "$2" ; then
        # return true
        return 0
    fi
    LOWER_VERSION="$(printf '%s\n' "$1" "$2" | $SORT_V | head -n 1)"
    # implicit return
    test "$LOWER_VERSION" != "$1"
}

# 'sort -V' is a gnu extension
SORT_V="sort -V"
# Use 'sort -n' if 'sort -V' doesn't work
if ! version_ge "1" "0" ; then
    echo "Your 'sort -V' command appears broken. Falling back to 'sort -n'."
    echo "Some spatch version checks may give the wrong result."
    SORT_V="sort -n"
fi

# Print the full spatch version, for diagnostics
spatch --version

MIN_SPATCH_V="1.0.4"
# This pattern needs to handle version strings like:
# spatch version 1.0.0-rc19
# spatch version 1.0.6 compiled with OCaml version 4.05.0
SPATCH_V=$(spatch --version | head -1 | \
               sed 's/spatch version \([0-9][^ ]*\).*/\1/')

if ! version_ge "$SPATCH_V" "$MIN_SPATCH_V" ; then
    echo "Tor requires coccinelle spatch >= $MIN_SPATCH_V to check $PURPOSE."
    echo "But you have $SPATCH_V. Please install a newer version."
    exit "$exitcode"
fi

if test $# -ge 1 ; then
  "$try_parse" "$@"
  exitcode=$?
else
  cd "$top" || exit 1
  # This is the layout in 0.3.5
  # Keep these lists consistent:
  #   - OWNED_TOR_C_FILES in Makefile.am
  #   - CHECK_FILES in pre-commit.git-hook and pre-push.git-hook
  #   - try_parse in check_cocci_parse.sh
  "$try_parse" \
    src/lib/*/*.[ch] \
    src/core/*/*.[ch] \
    src/feature/*/*.[ch] \
    src/app/*/*.[ch] \
    src/test/*.[ch] \
    src/test/*/*.[ch] \
    src/tools/*.[ch]
  exitcode=$?
fi

if test "$exitcode" != 0 ; then
    echo "Please fix these $PURPOSE errors in the above files"
    echo "Set VERBOSE=1 for more details"
    echo "Try running test-operator-cleanup or 'make autostyle-operators'"
    echo "As a last resort, you can modify scripts/coccinelle/exceptions.txt"
fi

exit "$exitcode"