check_cocci_parse.sh 2.7 KB

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