mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-14 03:58:18 +00:00
cf4da09ce9
Add `scripts/check_standard_headers.sh` to check that no C standard header is included in our code. The script prints an error message for every C header used as well as the corresponding C++ header that should be used instead. The script is added to the style checking CI.
34 lines
1.4 KiB
Bash
Executable file
34 lines
1.4 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
# List of C headers and their corresponding C++ headers
|
|
c_headers=(assert complex ctype errno fenv float inttypes iso646 limits locale math setjmp signal stdarg stdbool stddef stdint stdio stdlib string tgmath time wchar wctype)
|
|
c_headers_map=(cassert complex cctype cerrno cfenv cfloat cinttypes ciso646 climits clocale cmath csetjmp csignal cstdarg cstdbool cstddef cstdint cstdio cstdlib cstring ctgmath ctime cwchar cwctype)
|
|
|
|
# Create regex dynamically from the array to match any C header
|
|
c_headers_regex=$(IFS="|"; echo "${c_headers[*]}")
|
|
|
|
# Find all C++ source and header files
|
|
files=$(find ./src -type f \( -name '*.cpp' -o -name '*.hpp' -o -name '*.h' \) ! -path "./src/engine/external/*")
|
|
|
|
error_found=0
|
|
|
|
# Check each source file for C headers
|
|
for file in $files; do
|
|
# First check if the file includes any C headers for more efficiency when no C header is used
|
|
if grep -E "#include\s+<($c_headers_regex)\.h>" "$file" >/dev/null; then
|
|
# Check each C header individually to print an error message with the appropriate replacement C++ header
|
|
for (( i=0; i < ${#c_headers[@]}; i++ )); do
|
|
if grep -E "#include\s+<${c_headers[i]}\.h>" "$file" >/dev/null; then
|
|
echo "Error: '$file' includes C header '${c_headers[i]}.h'. Include the C++ header '${c_headers_map[i]}' instead."
|
|
fi
|
|
done
|
|
error_found=1
|
|
fi
|
|
done
|
|
|
|
if [ $error_found -eq 1 ]; then
|
|
exit 1
|
|
fi
|
|
|
|
echo "Success: No standard C headers are used."
|