clang_tidy.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env bash
  2. # This script runs clang-tidy on all relevant files in the repo.
  3. # This is more thorough than clang-format and thus slower; it should only be run manually.
  4. set -uo pipefail
  5. # Loops through all code files tracked by Git.
  6. git ls-files -- '*.c' '*.h' '*.cpp' '*.hpp' '*.cc' '*.hh' '*.cxx' '*.m' '*.mm' '*.inc' '*.java' '*.glsl' \
  7. ':!:.git/*' ':!:thirdparty/*' ':!:platform/android/java/lib/src/com/google/*' ':!:*-so_wrap.*' |
  8. while read -r f; do
  9. # Run clang-tidy.
  10. clang-tidy --quiet --fix "$f" &> /dev/null
  11. # Run clang-format. This also fixes the output of clang-tidy.
  12. clang-format --Wno-error=unknown -i "$f"
  13. done
  14. diff=$(git diff --color)
  15. # If no diff has been generated all is OK, clean up, and exit.
  16. if [ -z "$diff" ] ; then
  17. printf "\e[1;32m*** Files in this commit comply with the clang-tidy style rules.\e[0m\n"
  18. exit 0
  19. fi
  20. # A diff has been created, notify the user, clean up, and exit.
  21. printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
  22. # Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
  23. printf "%s\n" "$diff" | perl -pe 's/(.*[^ ])( +)(\e\[m)$/my $spaces="·" x length($2); sprintf("$1$spaces$3")/ge' | perl -pe 's/(.*[^\t])(\t+)(\e\[m)$/my $tabs="<TAB>" x length($2); sprintf("$1$tabs$3")/ge'
  24. printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
  25. exit 1