clang-format.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #! /bin/bash
  2. function setup_for_format() {
  3. if [ -z "${CLANG_FORMAT}" ]; then
  4. CLANG_FORMAT=clang-format
  5. fi
  6. echo "LINT: Using binary $CLANG_FORMAT"
  7. CLANG_FORMAT_WHITELIST="util/ci/clang-format-whitelist.txt"
  8. files_to_lint="$(find src/ -name '*.cpp' -or -name '*.h')"
  9. }
  10. function check_format() {
  11. echo "Checking format..."
  12. setup_for_format
  13. local errorcount=0
  14. local fail=0
  15. for f in ${files_to_lint}; do
  16. d=$(diff -u "$f" <(${CLANG_FORMAT} "$f") || true)
  17. if ! [ -z "$d" ]; then
  18. whitelisted=$(awk '$1 == "'$f'" { print 1 }' "$CLANG_FORMAT_WHITELIST")
  19. # If file is not whitelisted, mark a failure
  20. if [ -z "${whitelisted}" ]; then
  21. errorcount=$((errorcount+1))
  22. printf "The file %s is not compliant with the coding style" "$f"
  23. if [ ${errorcount} -gt 50 ]; then
  24. printf "\nToo many errors encountered previously, this diff is hidden.\n"
  25. else
  26. printf ":\n%s\n" "$d"
  27. fi
  28. fail=1
  29. fi
  30. fi
  31. done
  32. if [ "$fail" = 1 ]; then
  33. echo "LINT reports failure."
  34. exit 1
  35. fi
  36. echo "LINT OK"
  37. }
  38. function fix_format() {
  39. echo "Fixing format..."
  40. setup_for_format
  41. for f in ${files_to_lint}; do
  42. whitelisted=$(awk '$1 == "'$f'" { print 1 }' "$CLANG_FORMAT_WHITELIST")
  43. if [ -z "${whitelisted}" ]; then
  44. echo "$f"
  45. $CLANG_FORMAT -i "$f"
  46. fi
  47. done
  48. }