clang-format for R-Type
This document explains how to configure and use clang-format to ensure consistent coding standards in the R-Type project.
Why use clang-format?
- Ensures a uniform coding style for all contributors.
- Compatible with most IDEs (Visual Studio Code, CLion, etc.).
- Automates code formatting to focus on development.
Installing clang-format
Linux
- Install clang-format using your package manager:
sudo apt install clang-format
MacOS
- Install clang-format using Homebrew:
brew install clang-format
Verification
- Make sure clang-format is installed correctly:
clang-format --version
Configuration with .clang-format
Add the following .clang-format file to the root of the R-Type project to define the style rules.
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 100
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: false
AlwaysBreakTemplateDeclarations: true
SortIncludes: true
IncludeBlocks: Preserve
SpacesInParentheses: false
Using clang-format
Manual
-
Format a specific file:
clang-format -i path/to/file.cpp -
Format the entire project:
find . -name '*.cpp' -o -name '*.h' | xargs clang-format -i
With IDEs
VS Code
- Install the C/C++ and Clang-Format extensions.
- Add the following to
settings.json:{ "C_Cpp.clang_format_style": "file", "editor.formatOnSave": true }
CLion
- Navigate to File > Settings > Editor > Code Style > C/C++.
- Enable Use clang-format file.
CI/CD Integration
Add a step in your CI pipeline to validate formatting:
- name: clang-format check
run: |
find . -name '*.cpp' -o -name '*.h' | xargs clang-format --dry-run --Werror
Essential Commands
| Action | Command |
|---|---|
| Format a file | clang-format -i file.cpp |
| Format all files | `find . -name '.cpp' -o -name '.h' |
| Verify without modifying | clang-format --dry-run file.cpp |