check-style.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/bash
  2. #
  3. # Script to check include/test code for common pybind11 code style errors.
  4. #
  5. # This script currently checks for
  6. #
  7. # 1. missing space between keyword and parenthesis, e.g.: for(, if(, while(
  8. # 2. Missing space between right parenthesis and brace, e.g. 'for (...){'
  9. # 3. opening brace on its own line. It should always be on the same line as the
  10. # if/while/for/do statement.
  11. #
  12. # Invoke as: tools/check-style.sh <filenames>
  13. #
  14. check_style_errors=0
  15. IFS=$'\n'
  16. found="$(grep '\<\(if\|for\|while\|catch\)(\|){' "$@" -rn --color=always)"
  17. if [ -n "$found" ]; then
  18. echo -e '\033[31;01mError: found the following coding style problems:\033[0m'
  19. check_style_errors=1
  20. echo "${found//^/ /}"
  21. fi
  22. found="$(awk '
  23. function prefix(filename, lineno) {
  24. return " \033[35m" filename "\033[36m:\033[32m" lineno "\033[36m:\033[0m"
  25. }
  26. function mark(pattern, string) { sub(pattern, "\033[01;31m&\033[0m", string); return string }
  27. last && /^\s*{/ {
  28. print prefix(FILENAME, FNR-1) mark("\\)\\s*$", last)
  29. print prefix(FILENAME, FNR) mark("^\\s*{", $0)
  30. last=""
  31. }
  32. { last = /(if|for|while|catch|switch)\s*\(.*\)\s*$/ ? $0 : "" }
  33. ' "$(find include -type f)" "$@")"
  34. if [ -n "$found" ]; then
  35. check_style_errors=1
  36. echo -e '\033[31;01mError: braces should occur on the same line as the if/while/.. statement. Found issues in the following files:\033[0m'
  37. echo "$found"
  38. fi
  39. exit $check_style_errors