Skip to content

Commit b52d1bc

Browse files
committed
Apply consistent code formatting with clang-format
- Add .clang-format config: Allman braces, 4-space indent, no braces for single statements - Add .githooks/pre-commit for auto-formatting on commit - Add CI workflow to check formatting on PRs - Format all source files for consistency
1 parent 6a978b1 commit b52d1bc

105 files changed

Lines changed: 11463 additions & 9555 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.clang-format

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Clang-Format configuration for fastmcpp
2+
# Style: Allman braces, 4-space indent, no braces for single statements
3+
4+
BasedOnStyle: LLVM
5+
6+
# Braces on their own line (Allman style)
7+
BreakBeforeBraces: Allman
8+
9+
# Indentation - 4 spaces
10+
IndentWidth: 4
11+
TabWidth: 4
12+
UseTab: Never
13+
ColumnLimit: 100
14+
15+
# Remove braces from single-statement if/else/loop bodies
16+
RemoveBracesLLVM: true
17+
18+
# Allow short control statements
19+
AllowShortBlocksOnASingleLine: Empty
20+
AllowShortFunctionsOnASingleLine: Empty
21+
AllowShortIfStatementsOnASingleLine: Never
22+
AllowShortLoopsOnASingleLine: false
23+
24+
# Alignment
25+
AlignAfterOpenBracket: Align
26+
AlignConsecutiveAssignments: false
27+
AlignConsecutiveDeclarations: false
28+
AlignOperands: true
29+
AlignTrailingComments: true
30+
31+
# Spacing
32+
SpaceAfterCStyleCast: false
33+
SpaceAfterTemplateKeyword: true
34+
SpaceBeforeAssignmentOperators: true
35+
SpaceBeforeParens: ControlStatements
36+
SpaceInEmptyParentheses: false
37+
SpacesInAngles: false
38+
SpacesInContainerLiterals: false
39+
SpacesInCStyleCastParentheses: false
40+
SpacesInParentheses: false
41+
SpacesInSquareBrackets: false
42+
43+
# Breaking
44+
AlwaysBreakAfterReturnType: None
45+
AlwaysBreakTemplateDeclarations: Yes
46+
BreakBeforeBinaryOperators: None
47+
BreakBeforeTernaryOperators: true
48+
BreakConstructorInitializers: BeforeColon
49+
BreakInheritanceList: BeforeColon
50+
51+
# Other
52+
PointerAlignment: Left
53+
SortIncludes: true
54+
IncludeBlocks: Regroup

.githooks/pre-commit

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
# Pre-commit hook: auto-format staged C++ files
3+
#
4+
# Enable with: git config core.hooksPath .githooks
5+
6+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|hpp)$')
7+
8+
if [ -z "$STAGED_FILES" ]; then
9+
exit 0
10+
fi
11+
12+
# Check if clang-format is available
13+
if ! command -v clang-format &> /dev/null; then
14+
echo "Warning: clang-format not found, skipping auto-format"
15+
exit 0
16+
fi
17+
18+
# Auto-format and re-stage
19+
for file in $STAGED_FILES; do
20+
if [ -f "$file" ]; then
21+
clang-format -i "$file"
22+
git add "$file"
23+
fi
24+
done
25+
26+
exit 0

.github/workflows/format-check.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Format Check
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
format-check:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
14+
- name: Install clang-format
15+
run: |
16+
sudo apt-get update
17+
sudo apt-get install -y clang-format-17
18+
19+
- name: Check formatting
20+
run: |
21+
# Find all source files (excluding build dirs and deps)
22+
FILES=$(find src include examples tests -name "*.cpp" -o -name "*.hpp" 2>/dev/null)
23+
24+
# Check if any files need formatting
25+
UNFORMATTED=""
26+
for f in $FILES; do
27+
if ! clang-format-17 --dry-run --Werror "$f" 2>/dev/null; then
28+
UNFORMATTED="$UNFORMATTED $f"
29+
fi
30+
done
31+
32+
if [ -n "$UNFORMATTED" ]; then
33+
echo "::error::The following files need formatting:"
34+
for f in $UNFORMATTED; do
35+
echo " - $f"
36+
done
37+
echo ""
38+
echo "Run: find src include examples tests -name '*.cpp' -o -name '*.hpp' | xargs clang-format -i"
39+
exit 1
40+
fi
41+
42+
echo "All files are properly formatted!"

0 commit comments

Comments
 (0)