File: check_standard_headers.sh

package info (click to toggle)
ddnet 19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,960 kB
  • sloc: cpp: 195,050; ansic: 58,572; python: 5,568; asm: 946; sh: 941; java: 366; xml: 206; makefile: 31
file content (33 lines) | stat: -rwxr-xr-x 1,457 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/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=$(printf "%s" "${c_headers[*]}" | tr ' ' '|')

# 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."