Overview
KiCad uses automated formatting tools to enforce many (but not all) style requirements:- clang-format - Primary formatting tool (minimum version 10)
- uncrustify - Legacy formatting configuration (for reference)
Automated Formatting
Using clang-format
The clang-format configuration is stored in_clang-format at the repository root.
Format a Single File
Check Formatting (Dry Run)
Format All Modified Files
CI Format Checks
When you create a merge request, the CI pipeline includes a formatting check. This automatic check is not always 100% correct, so keep in mind:- Exceptions exist: Some formatting guidelines have exceptions or only apply to certain situations. clang-format doesn’t know about these nuances.
- Sweeping changes: clang-format may suggest changes to areas of a file near your code, even if you didn’t modify that code. Rule 7 of the style guide: When there is flexibility or doubt, follow the existing formatting of the file you are editing.
-
Limitations: clang-format doesn’t handle:
- Column alignment formatting (Rule 4.1.2)
- Preferred lambda format (Rule 4.10)
- Alphabetizing
#includedirectives (don’t do this for existing files unless making sweeping changes)
Code Style Configuration
clang-format Settings
Key settings from_clang-format:
Basic Style Rules
Indentation and Spacing
- Indentation: 4 spaces (no tabs)
- Line length: Maximum 120 characters
- Spaces in parentheses: Yes -
foo( a, b )notfoo(a, b) - No space before parentheses:
function()notfunction ()
Naming Conventions
Classes and Types:Bracing Style (Allman)
Pointer and Reference Alignment
Header Guards
Use#ifndef include guards:
Include Order
While clang-format suggests alphabetizing includes, do not alphabetize includes in existing files unless you are making sweeping changes to the include list. Typical include order:C++ Specific Guidelines
Modern C++ (C++20)
KiCad uses C++20. Prefer modern C++ features:Lambda Functions
clang-format doesn’t support KiCad’s preferred lambda format. Format lambdas manually:Templates
Constructor Initializer Lists
Comments and Documentation
File Headers
All source files should include a license header:Doxygen Comments
Use Doxygen-style comments for classes, functions, and important members:Inline Comments
wxWidgets Specific
wxString Usage
Event Handlers
Best Practices
Column Alignment (Rule 4.1.2)
When appropriate, align similar code for readability:Error Handling
Boolean Comparisons
Magic Numbers
Avoid magic numbers, use named constants:User Interface Guidelines
For UI-related code, also follow the User Interface Guidelines:- Consistent dialog layouts
- Proper use of wxWidgets sizers
- Keyboard shortcuts and mnemonics
- Accessibility considerations
Pre-Commit Checks
Before committing your changes:-
Format your code:
-
Build without warnings:
-
Run tests:
-
Check for style violations:
When to Deviate
Rule 7: When there is flexibility or doubt, follow the existing formatting of the file you are editing rather than rigidly following clang-format. If you’re modifying an existing file:- Match the existing style in that file
- Don’t reformat code you didn’t change
- Focus on making your changes consistent with the surroundings
Additional Resources
- Official Code Style Guide: https://dev-docs.kicad.org/en/rules-guidelines/code-style/
- UI Guidelines: https://dev-docs.kicad.org/en/rules-guidelines/ui/
- clang-format Documentation: https://clang.llvm.org/docs/ClangFormat.html
- Doxygen Manual: https://www.doxygen.nl/manual/
Next Steps
Now that you understand the code style guidelines:- Practice formatting your code with clang-format
- Review existing code to see style in practice
- Start contributing with a merge request