header_guards.sh 3.7 KB

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