blob: d8746c19842700c9bfab93f6ff5f807d05298a83 (
plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#!/usr/bin/env bash
set -e
script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
cd "${script_path}/.." || exit 1
if [ "$#" -eq "1" ]; then
mapfile -t files < <(
git ls-files -- \
'*.cpp' \
'*.h' \
':!:Base' \
':!:Kernel/FileSystem/Ext2FS/Definitions.h' \
':!:Userland/Libraries/LibCodeComprehension/Cpp/Tests/*' \
':!:Userland/Libraries/LibCpp/Tests/parser/*' \
':!:Userland/Libraries/LibCpp/Tests/preprocessor/*'
)
else
files=()
for file in "${@:2}"; do
if [[ "${file}" == *".cpp" || "${file}" == *".h" ]]; then
files+=("${file}")
fi
done
fi
if (( ${#files[@]} )); then
TOOLCHAIN_DIR=Toolchain/Local/clang/bin
CLANG_FORMAT=false
if command -v clang-format-15 >/dev/null 2>&1 ; then
CLANG_FORMAT=clang-format-15
elif command -v brew >/dev/null 2>&1 && command -v "$(brew --prefix llvm@15)"/bin/clang-format >/dev/null 2>&1 ; then
CLANG_FORMAT="$(brew --prefix llvm@15)"/bin/clang-format
elif command -v $TOOLCHAIN_DIR/clang-format >/dev/null 2>&1 && $TOOLCHAIN_DIR/clang-format --version | grep -qF ' 15.' ; then
CLANG_FORMAT=$TOOLCHAIN_DIR/clang-format
elif command -v clang-format >/dev/null 2>&1 ; then
CLANG_FORMAT=clang-format
if ! "${CLANG_FORMAT}" --version | awk '{ if (substr($NF, 1, index($NF, ".") - 1) < 15) exit 1; }'; then
echo "You are using '$("${CLANG_FORMAT}" --version)', which appears to not be clang-format 15 or later."
echo "It is very likely that the resulting changes are not what you wanted."
fi
else
echo "clang-format-15 is not available, but C or C++ files need linting! Either skip this script, or install clang-format-15."
echo "(If you install a package 'clang-format', please make sure it's version 15 or later.)"
exit 1
fi
if [ "$#" -gt "0" ] && [ "--overwrite-inplace" = "$1" ] ; then
true # The only way to run this script.
else
# Note that this branch also covers --help, -h, -help, -?, etc.
echo "USAGE: $0 --overwrite-inplace"
echo "The argument is necessary to make you aware that this *will* overwrite your local files."
exit 1
fi
echo "Using ${CLANG_FORMAT}"
"${CLANG_FORMAT}" -style=file -i "${files[@]}"
echo "Maybe some files have changed. Sorry, but clang-format doesn't indicate what happened."
else
echo "No .cpp or .h files to check."
fi
|