Skip to content

Commit 905e31f

Browse files
authoredJul 27, 2023
Add C++ formatting pre-commit hook (#247)
1 parent 01cac03 commit 905e31f

File tree

3 files changed

+129
-5
lines changed

3 files changed

+129
-5
lines changed
 

Diff for: ‎.githooks/pre-commit

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
project_folder=$(git rev-parse --show-toplevel)
4+
if git rev-parse --verify HEAD >/dev/null 2>&1; then
5+
against=HEAD
6+
else
7+
# Initial commit: diff against an empty tree object
8+
against=$(git hash-object -t tree /dev/null)
9+
fi
10+
11+
# Redirect output to stderr.
12+
exec 1>&2
13+
14+
tmpdir=$(mktemp -d repo-XXXXXXXX)
15+
trap "rm -rf $tmpdir" EXIT INT
16+
17+
modified_files=$(git diff --cached --name-only --diff-filter=AM $against | sed -nE "/.*\.(cpp|cc|cxx|c|h|hpp)$/p")
18+
FAIL=0
19+
20+
for file in $modified_files; do
21+
echo "Checking $file..."
22+
23+
cp $project_folder/cpp/.clang-format $project_folder/cpp/.clang-tidy $tmpdir
24+
25+
git checkout-index --prefix="$tmpdir/" -- $file
26+
27+
CODE=$?
28+
if [ $CODE -ne 0 ]; then
29+
FAIL=1
30+
fi
31+
done
32+
33+
return ${FAIL}

Diff for: ‎cpp/.clang-format

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
Language: Cpp
2+
Language: Cpp
33
BasedOnStyle: Google
4-
Standard: "c++17"
5-
UseTab: Never
4+
Standard: "c++20"
5+
UseTab: Never
66
DerivePointerAlignment: false
77
PointerAlignment: Right
8-
ColumnLimit : 120
9-
IncludeBlocks: Preserve
8+
ColumnLimit: 120
9+
IncludeBlocks: Preserve

Diff for: ‎cpp/.clang-tidy

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
Checks: "*,
3+
-abseil-string-find-str-contains,
4+
-altera-id-dependent-backward-branch,
5+
-altera-struct-pack-align,
6+
-altera-unroll-loops,
7+
-android-*,
8+
-cert-err58-cpp,
9+
-cppcoreguidelines-avoid-c-arrays,
10+
-cppcoreguidelines-avoid-goto,
11+
-cppcoreguidelines-avoid-magic-numbers,
12+
-cppcoreguidelines-no-malloc,
13+
-cppcoreguidelines-non-private-member-variables-in-classes,
14+
-cppcoreguidelines-owning-memory,
15+
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
16+
-cppcoreguidelines-pro-bounds-constant-array-index,
17+
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
18+
-cppcoreguidelines-pro-type-member-init,
19+
-cppcoreguidelines-pro-type-reinterpret-cast,
20+
-cppcoreguidelines-pro-type-static-cast-downcast,
21+
-cppcoreguidelines-pro-type-union-access,
22+
-cppcoreguidelines-pro-type-vararg,
23+
-cppcoreguidelines-special-member-functions,
24+
-fuchsia-default-arguments,
25+
-fuchsia-default-arguments-calls,
26+
-fuchsia-default-arguments-declarations,
27+
-fuchsia-overloaded-operator,
28+
-fuchsia-statically-constructed-objects,
29+
-fuchsia-trailing-return,
30+
-fuchsia-virtual-inheritance,
31+
-google-explicit-constructor,
32+
-google-readability-*,
33+
-google-runtime-references,
34+
-hicpp-avoid-c-arrays,
35+
-hicpp-avoid-goto,
36+
-hicpp-braces-around-statements,
37+
-hicpp-member-init,
38+
-hicpp-no-array-decay,
39+
-hicpp-no-assembler,
40+
-hicpp-no-malloc,
41+
-hicpp-use-equals-default,
42+
-hicpp-use-nullptr,
43+
-hicpp-vararg,
44+
-llvm-header-guard,
45+
-llvm-include-order,
46+
-llvmlibc-callee-namespace,
47+
-llvmlibc-implementation-in-namespace,
48+
-llvmlibc-restrict-system-libc-headers,
49+
-misc-non-private-member-variables-in-classes,
50+
-modernize-avoid-c-arrays,
51+
-modernize-concat-nested-namespaces,
52+
-modernize-pass-by-value,
53+
-modernize-use-equals-default,
54+
-modernize-use-nodiscard,
55+
-modernize-use-trailing-return-type,
56+
-performance-unnecessary-value-param,
57+
-readability-braces-around-statements,
58+
-readability-else-after-return,
59+
-readability-function-cognitive-complexity,
60+
-readability-implicit-bool-conversion,
61+
-readability-magic-numbers,
62+
-readability-named-parameter,
63+
-misc-no-recursion,
64+
-concurrency-mt-unsafe,
65+
-bugprone-easily-swappable-parameters"
66+
67+
WarningsAsErrors: ""
68+
HeaderFilterRegex: "src/.*"
69+
AnalyzeTemporaryDtors: false
70+
FormatStyle: none
71+
CheckOptions:
72+
- key: google-readability-braces-around-statements.ShortStatementLines
73+
value: "1"
74+
- key: google-readability-function-size.StatementThreshold
75+
value: "800"
76+
- key: google-readability-namespace-comments.ShortNamespaceLines
77+
value: "10"
78+
- key: google-readability-namespace-comments.SpacesBeforeComments
79+
value: "2"
80+
- key: modernize-loop-convert.MaxCopySize
81+
value: "16"
82+
- key: modernize-loop-convert.MinConfidence
83+
value: reasonable
84+
- key: modernize-loop-convert.NamingStyle
85+
value: CamelCase
86+
- key: modernize-pass-by-value.IncludeStyle
87+
value: llvm
88+
- key: modernize-replace-auto-ptr.IncludeStyle
89+
value: llvm
90+
- key: modernize-use-nullptr.NullMacros
91+
value: "NULL"

0 commit comments

Comments
 (0)
Please sign in to comment.