ladybird/Meta/check-style.sh
Emanuele Torre 937d0be762 Meta: Add a script check the presence of "#pragma once" in header files
.. and make travis run it.

I renamed check-license-headers.sh to check-style.sh and expanded it so
that it now also checks for the presence of "#pragma once" in .h files.

It also checks the presence of a (single) blank line above and below the
"#pragma once" line.

I also added "#pragma once" to all the files that need it: even the ones
we are not check.
I also added/removed blank lines in order to make the script not fail.

I also ran clang-format on the files I modified.
2020-05-29 07:59:45 +02:00

57 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
cd "$script_path/.." || exit 1
# We simply check if the file starts with:
# /*
# * Copyright
LICENSE_HEADER_PATTERN=$'^/\*\n \* Copyright'
MISSING_LICENSE_HEADER_ERRORS=()
# We check that "#pragma once" is present
PRAGMA_ONCE_PATTERN='#pragma once'
MISSING_PRAGMA_ONCE_ERRORS=()
# We make sure that there's a blank line before and after pragma once
GOOD_PRAGMA_ONCE_PATTERN=$'(^|\\S\n\n)#pragma once(\n\n\\S.|$)'
BAD_PRAGMA_ONCE_ERRORS=()
while IFS= read -r f; do
file_content="$(< "$f")"
if [[ ! "$file_content" =~ $LICENSE_HEADER_PATTERN ]]; then
MISSING_LICENSE_HEADER_ERRORS+=("$f")
fi
if [[ "$f" =~ \.h$ ]]; then
if [[ ! "$file_content" =~ $PRAGMA_ONCE_PATTERN ]]; then
MISSING_PRAGMA_ONCE_ERRORS+=("$f")
elif [[ ! "$file_content" =~ $GOOD_PRAGMA_ONCE_PATTERN ]]; then
BAD_PRAGMA_ONCE_ERRORS+=("$f")
fi
fi
done < <(git ls-files -- \
'*.cpp' \
'*.h' \
':!:Tests' \
':!:Base' \
':!:Kernel/FileSystem/ext2_fs.h' \
':!:Libraries/LibC/getopt.cpp' \
':!:Libraries/LibCore/puff.h' \
':!:Libraries/LibELF/exec_elf.h' \
)
exit_status=0
if (( ${#MISSING_LICENSE_HEADER_ERRORS[@]} )); then
echo "Files missing license headers: ${MISSING_LICENSE_HEADER_ERRORS[*]}"
exit_status=1
fi
if (( ${#MISSING_PRAGMA_ONCE_ERRORS[@]} )); then
echo "Header files missing \"#pragma once\": ${MISSING_PRAGMA_ONCE_ERRORS[*]}"
exit_status=1
fi
if (( ${#BAD_PRAGMA_ONCE_ERRORS[@]} )); then
echo "\"#pragma once\" should have a blank line before and after in these files: ${BAD_PRAGMA_ONCE_ERRORS[*]}"
exit_status=1
fi
exit "$exit_status"