diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..7051425 --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +Language: Cpp +BasedOnStyle: Google +AccessModifierOffset: -2 +AlignConsecutiveAssignments: true +AllowShortFunctionsOnASingleLine: Empty +ColumnLimit: 96 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +SortIncludes: false +SpacesBeforeTrailingComments: 1 +Standard: Cpp11 diff --git a/tools/formatAll.sh b/tools/formatAll.sh new file mode 100755 index 0000000..47e04cd --- /dev/null +++ b/tools/formatAll.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +. "$(dirname "$0")/isTopLevel.sh" + +for f in $(git ls-files | grep -E '.*\.(cc|h)$'); do + echo "formatting ${f}" + clang-format -i "${f}" +done diff --git a/tools/isTopLevel.sh b/tools/isTopLevel.sh new file mode 100644 index 0000000..97f7cc8 --- /dev/null +++ b/tools/isTopLevel.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then + exit 1 +fi + +if [ "$(pwd)" != "$(git rev-parse --show-toplevel)" ]; then + exit 1 +fi diff --git a/tools/validate/format.sh b/tools/validate/format.sh new file mode 100755 index 0000000..3140408 --- /dev/null +++ b/tools/validate/format.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +. "$(dirname "$0")/../isTopLevel.sh" + +RETVAL=0 + +for f in $(git ls-files | grep -E '.*\.(cc|h)$'); do + if ! clang-format "${f}" | diff "${f}" - > /dev/null 2>&1; then + echo "Error: Invalid code formatting in ${f}" >&2 + RETVAL=1 + fi +done + +exit $RETVAL diff --git a/tools/validate/includeGuard.sh b/tools/validate/includeGuard.sh new file mode 100755 index 0000000..a66b80f --- /dev/null +++ b/tools/validate/includeGuard.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +. "$(dirname "$0")/../isTopLevel.sh" + +RETVAL=0 + +for f in $(git ls-files | grep -E '^src\/.*\.h$'); do + # ファイルパスから正しいインクルードガードの文字列を生成する + s1="$(echo "$f" | sed -r 's/^src\///; s/[\/\.-]+/_/g; s/^.*$/\U&/')_" + + # ファイルからインクルードガードを読み込む + s2=$(grep -Pzo '#ifndef\s+\K\b(\w+)\b(?=\s+#define\s+\b\1\b)' "$f" | tr '\0' '\n') + + if [ -z "$s2" ]; then + echo "error: $f: include guard is missing or incorrect" >&2 + echo " ($s1)" >&2 + RETVAL=1 + elif [ "$s2" != "$s1" ]; then + echo "error: $f: include guard is incorrect" >&2 + echo " ($s2 => $s1)" >&2 + RETVAL=1 + fi +done + +exit $RETVAL