header_guards.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. if [ ! -f "version.py" ]; then
  3. echo "Warning: This script is intended to be run from the root of the Godot repository."
  4. echo "Some of the paths checks may not work as intended from a different folder."
  5. fi
  6. files_invalid_guard=""
  7. for file in $(find -name "thirdparty" -prune -o -name "*.h" -print); do
  8. # Skip *.gen.h and *-so_wrap.h, they're generated.
  9. if [[ "$file" == *".gen.h" || "$file" == *"-so_wrap.h" ]]; then continue; fi
  10. # Has important define before normal header guards.
  11. if [[ "$file" == *"thread.h" || "$file" == *"platform_config.h" ]]; then continue; fi
  12. # Obj-C files don't use header guards.
  13. if grep -q "#import " "$file"; then continue; fi
  14. bname=$(basename $file .h)
  15. # Add custom prefix or suffix for generic filenames with a well-defined namespace.
  16. prefix=
  17. if [[ "$file" == "./modules/"*"/register_types.h" ]]; then
  18. module=$(echo $file | sed "s@.*modules/\([^/]*\).*@\1@")
  19. prefix="${module^^}_"
  20. fi
  21. if [[ "$file" == "./platform/"*"/api/api.h" || "$file" == "./platform/"*"/export/"* ]]; then
  22. platform=$(echo $file | sed "s@.*platform/\([^/]*\).*@\1@")
  23. prefix="${platform^^}_"
  24. fi
  25. if [[ "$file" == "./modules/mono/utils/"* && "$bname" != *"mono"* ]]; then prefix="MONO_"; fi
  26. if [[ "$file" == "./servers/rendering/storage/utilities.h" ]]; then prefix="RENDERER_"; fi
  27. suffix=
  28. if [[ "$file" == *"dummy"* && "$bname" != *"dummy"* ]]; then suffix="_DUMMY"; fi
  29. if [[ "$file" == *"gles3"* && "$bname" != *"gles3"* ]]; then suffix="_GLES3"; fi
  30. if [[ "$file" == *"renderer_rd"* && "$bname" != *"rd"* ]]; then suffix="_RD"; fi
  31. if [[ "$file" == *"ustring.h" ]]; then suffix="_GODOT"; fi
  32. # ^^ is bash builtin for UPPERCASE.
  33. guard="${prefix}${bname^^}${suffix}_H"
  34. # Replaces guards to use computed name.
  35. # We also add some \n to make sure there's a proper separation.
  36. sed -i $file -e "0,/ifndef/s/#ifndef.*/\n#ifndef $guard/"
  37. sed -i $file -e "0,/define/s/#define.*/#define $guard\n/"
  38. sed -i $file -e "$ s/#endif.*/\n#endif \/\/ $guard/"
  39. # Removes redundant \n added before, if they weren't needed.
  40. sed -i $file -e "/^$/N;/^\n$/D"
  41. # Check that first ifndef (should be header guard) is at the expected position.
  42. # If not it can mean we have some code before the guard that should be after.
  43. # "31" is the expected line with the copyright header.
  44. first_ifndef=$(grep -n -m 1 "ifndef" $file | sed 's/\([0-9]*\).*/\1/')
  45. if [[ "$first_ifndef" != "31" ]]; then
  46. files_invalid_guard+="$file\n"
  47. fi
  48. done
  49. if [[ ! -z "$files_invalid_guard" ]]; then
  50. echo -e "The following files were found to have potentially invalid header guard:\n"
  51. echo -e "$files_invalid_guard"
  52. fi
  53. diff=$(git diff --color)
  54. # If no diff has been generated all is OK, clean up, and exit.
  55. if [ -z "$diff" ] ; then
  56. printf "\e[1;32m*** Files in this commit comply with the header guards formatting rules.\e[0m\n"
  57. exit 0
  58. fi
  59. # A diff has been created, notify the user, clean up, and exit.
  60. printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
  61. # Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
  62. printf "$diff\n" | 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'
  63. printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
  64. exit 1