Skip to content

Commit a91c1f4

Browse files
committed
Restore header guards too.
1 parent eca30e6 commit a91c1f4

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

misc/scripts/header_guards.sh

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

0 commit comments

Comments
 (0)