Adopting .clang-format-include and .clang-format-ignore (#3047)

This commit is contained in:
Antoine Cœur 2022-05-14 07:48:33 +08:00 committed by GitHub
parent 5026d43920
commit 7ab2fbfbd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 19 deletions

22
.clang-format-ignore Normal file
View File

@ -0,0 +1,22 @@
# Skipped files must be justified as untracked or third-party maintained
# untracked files
.*/*
build/*
libtransmission/version.h
web/node_modules/*
# third-party maintained projects
third-party/*
# maintained at https://github.com/sparkle-project/Sparkle
macosx/Sparkle.framework/*
# maintained at https://github.com/bdkjones/VDKQueue
macosx/VDKQueue/*
# maintained at https://github.com/mnunberg/jsonsl
libtransmission/jsonsl.*
# missing official maintainer (XConsortium: wildmat.c,v 1.2 94/04/13); TODO: clang-format it
libtransmission/wildmat.*

5
.clang-format-include Normal file
View File

@ -0,0 +1,5 @@
*.c
*.cc
*.h
*.m
*.mm

View File

@ -5,6 +5,12 @@
set -o noglob
# PATH workaround for SourceTree
# for Intel Mac
PATH="${PATH}:/usr/local/bin"
# for Apple Silicon Mac
PATH="${PATH}:/opt/homebrew/bin"
if [[ "x$1" == *"check"* ]]; then
echo "checking code format"
else
@ -15,24 +21,13 @@ root="$(dirname "$0")"
root="$(cd "${root}" && pwd)"
cd "${root}" || exit 1
cfile_includes=(
'*.c'
'*.cc'
'*.h'
'*.m'
'*.mm'
)
cfile_excludes=(
'build/*'
'libtransmission/ConvertUTF.*'
'libtransmission/jsonsl.*'
'libtransmission/wildmat.*'
'macosx/Sparkle.framework/*'
'macosx/VDKQueue/*'
'third-party/*'
'web/*'
'.git/*'
)
trim_comments() {
# 1. remove comments, ignoring backslash-escaped characters
# 2. trim spaces
# 3. remove empty lines
# note: GNU extensions like +?| aren't supported on macOS
sed 's/^\(\(\(\\.\)*[^\\#]*\)*\)#.*/\1/;s/^[[:blank:]]*//;s/[[:blank:]]*$//;/^$/d' "$@"
}
get_find_path_args() {
local args=$(printf " -o -path ./%s" "$@")
@ -40,7 +35,9 @@ get_find_path_args() {
}
find_cfiles() {
find . \( $(get_find_path_args "${cfile_includes[@]}") \) ! \( $(get_find_path_args "${cfile_excludes[@]}") \) "$@"
# We use the same files as Meson: https://mesonbuild.com/Code-formatting.html
find . \( $(get_find_path_args $(trim_comments .clang-format-include)) \)\
! \( $(get_find_path_args $(trim_comments .clang-format-ignore)) \) "$@"
}
# We're targeting clang-format version 12 and other versions give slightly