2021-01-22 16:44:05 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-05-28 18:40:53 +00:00
|
|
|
|
|
|
|
script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
|
|
|
|
cd "$script_path/.." || exit 1
|
|
|
|
|
2021-05-14 23:28:30 +00:00
|
|
|
# Ensure copyright headers match this format and are followed by a blank line:
|
2020-05-28 18:40:53 +00:00
|
|
|
# /*
|
2021-04-28 21:48:05 +00:00
|
|
|
# * Copyright (c) YYYY(-YYYY), Whatever
|
|
|
|
# * ... more of these ...
|
|
|
|
# *
|
|
|
|
# * SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
# */
|
2021-05-14 23:28:30 +00:00
|
|
|
GOOD_LICENSE_HEADER_PATTERN=$'^/\*\n( \* Copyright \(c\) [0-9]{4}(-[0-9]{4})?, .*\n)+ \*\n \* SPDX-License-Identifier: BSD-2-Clause\n \*/\n\n'
|
2021-04-28 21:48:05 +00:00
|
|
|
BAD_LICENSE_HEADER_ERRORS=()
|
2021-05-17 08:06:30 +00:00
|
|
|
LICENSE_HEADER_CHECK_EXCLUDES=(AK/Checked.h AK/Function.h Userland/Libraries/LibC/elf.h Userland/DevTools/HackStudio/LanguageServers/Cpp/Tests/* Userland/Libraries/LibCpp/Tests/*)
|
2020-05-28 18:40:53 +00:00
|
|
|
|
|
|
|
# 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=()
|
|
|
|
|
2020-08-12 11:49:49 +00:00
|
|
|
# We check that "#include <LibM/math.h>" is not being used
|
|
|
|
LIBM_MATH_H_INCLUDE_PATTERN='#include <LibM/math.h>'
|
|
|
|
LIBM_MATH_H_INCLUDE_ERRORS=()
|
|
|
|
|
2020-05-28 18:40:53 +00:00
|
|
|
while IFS= read -r f; do
|
|
|
|
file_content="$(< "$f")"
|
2021-05-16 15:50:45 +00:00
|
|
|
if [[ ! "${LICENSE_HEADER_CHECK_EXCLUDES[*]} " =~ $f ]]; then
|
2021-04-28 21:48:05 +00:00
|
|
|
if [[ ! "$file_content" =~ $GOOD_LICENSE_HEADER_PATTERN ]]; then
|
|
|
|
BAD_LICENSE_HEADER_ERRORS+=("$f")
|
|
|
|
fi
|
2020-05-28 18:40:53 +00:00
|
|
|
fi
|
2020-08-12 11:49:49 +00:00
|
|
|
if [[ "$file_content" =~ $LIBM_MATH_H_INCLUDE_PATTERN ]]; then
|
|
|
|
LIBM_MATH_H_INCLUDE_ERRORS+=("$f")
|
|
|
|
fi
|
2020-05-28 18:40:53 +00:00
|
|
|
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' \
|
|
|
|
':!:Base' \
|
|
|
|
':!:Kernel/FileSystem/ext2_fs.h' \
|
|
|
|
)
|
|
|
|
|
|
|
|
exit_status=0
|
2021-04-28 21:48:05 +00:00
|
|
|
if (( ${#BAD_LICENSE_HEADER_ERRORS[@]} )); then
|
|
|
|
echo "Files with missing or incorrect license header: ${BAD_LICENSE_HEADER_ERRORS[*]}"
|
2020-05-28 18:40:53 +00:00
|
|
|
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
|
2020-08-12 11:49:49 +00:00
|
|
|
if (( ${#LIBM_MATH_H_INCLUDE_ERRORS[@]} )); then
|
|
|
|
echo "\"#include <LibM/math.h>\" should be replaced with just \"#include <math.h>\" in these files: ${LIBM_MATH_H_INCLUDE_ERRORS[*]}"
|
|
|
|
exit_status=1
|
|
|
|
fi
|
2020-05-28 18:40:53 +00:00
|
|
|
exit "$exit_status"
|