Formatting with clang-format
OpenVPN usesclang-format for automated code formatting. The configuration is stored in the .clang-format file in the repository root.
Always run clang-format on your code before submitting patches. This ensures consistent formatting and speeds up the review process.
Installing clang-format
Install clang-format using your system’s package manager:Using clang-format
Format a single file:Pre-commit hook
OpenVPN provides a git pre-commit hook that automatically runs clang-format each time you commit. This is the recommended approach to ensure your code is always properly formatted. Install the pre-commit hook:- Automatically check formatting when you commit
- Allow you to format your code conveniently if needed
- Prevent commits with incorrect formatting
Style configuration
The OpenVPN.clang-format configuration is based on the Mozilla style with customizations:
Key formatting rules
Base style
Mozilla style with OpenVPN-specific customizations
Indentation
4 spaces (no tabs), indent case labels
Braces
Allman style - braces on new lines
Line length
No column limit (0)
Detailed configuration
The.clang-format file specifies:
Alignment:
- Align consecutive macros across comments
- Align trailing comments (allow 1 empty line)
- Align operands and escaped newlines
- Align parameters after open brackets
- 2 spaces before trailing comments
- Space before parentheses in control statements
- Space before assignment operators
- No space after C-style casts
- Break before binary operators (non-assignment)
- Break before ternary operators
- Break after return type in all definitions
- Allman brace style (braces on new lines)
- 4 spaces for indentation
- 4 spaces for continuation
- Indent case labels
- Do not indent goto labels
- Do not indent wrapped function names
- No short functions on single line
- No short if statements on single line
- No short loops on single line
- No short blocks on single line
- Insert braces around control statements
- Pointer alignment: Right (e.g.,
int *ptr) - Do not derive pointer alignment from context
- Keep maximum 2 empty lines
- Do not keep empty lines at start of blocks
- Reflow comments for readability
- Do not sort includes (preserve order)
- Do not break string literals
Complete configuration
The full.clang-format configuration:
Before submitting
Install the pre-commit hook
Run
dev-tools/git-pre-commit-format.sh install to automatically format code on commit.Patches that don’t follow the code style guidelines may be rejected during review. Using the pre-commit hook is the easiest way to ensure compliance.
Additional resources
Code style wiki
Official code style documentation on the community wiki
Contributing guide
Learn how to contribute patches to OpenVPN
Developer docs
Comprehensive developer documentation
Community overview
Connect with the OpenVPN community