Clang Format¶
This project uses clang-format to enforce consistent C++ code formatting. Run it before committing to ensure your changes pass style checks.
Installation¶
Clang-format is installed automatically as part of the development environment:
task env:setup
Usage¶
Check formatting (dry-run)¶
task format:check
Displays violations without modifying files. Exit code 1 means violations were found.
Fix formatting (in-place)¶
task format:fix
Automatically formats all C++ source files to match the project style.
Before Committing¶
task format:check # check for violations
task format:fix # fix them if found
git add src/ include/
git commit -m "fix(formatting): apply clang-format"
Clang-format is not enforced by pre-commit hooks — run it manually before each commit.
Configuration¶
Rules are defined in .clang-format at the project root, based on the Google C++ Style Guide:
- Indentation: 2 spaces
- Line length: 100 characters
- Brace style: Attached (K&R — braces on same line)
- Pointer alignment: Left (pointer symbol with variable name)
Naming Conventions¶
| Element | Convention | Example |
|---|---|---|
| File names | snake_case with .cpp/.h | v2_detector.h, dac_manager.cpp |
| Class names | PascalCase | CosmicDetector, WifiManager |
| Function names | snake_case | adc_init(), config_get_version() |
| Local variables | snake_case | poll_count, threshold1 |
| Global variables | g_ prefix + snake_case |
g_config, g_stream_enabled |
| Constants | UPPER_SNAKE_CASE | DETECT_POLL_COUNT, GPIO_PIN_ADDRESS |
| Macros | UPPER_SNAKE_CASE | ENABLE_TIMESTAMP, FIRMWARE_VERSION |
The g_ prefix makes global variable scope immediately visible in code.
Troubleshooting¶
| Problem | Fix |
|---|---|
clang-format: command not found |
Run task env:setup |
| Formatting conflicts with editor | Disable editor auto-format for this project |
| Want to format a single file | uv run clang-format -i src/main.cpp |