C++ Code Formatting Specification
Apache Doris uses clang-format to format C++ code, and provides wrapper scripts under the build-support directory so that developers can unify the format before submitting code.
Wrapper Scripts
| Script | Purpose |
|---|---|
build-support/clang-format.sh | Formats C/C++ code under be/src and be/test |
build-support/check-format.sh | Checks the code format of be/src and be/test and prints the diff without modifying any files |
Code Style Customization
- The Apache Doris code style is based on Google Style with minor customizations. The configuration file
.clang-formatis located at the Doris root directory. .clang-formatis compatible withclang-format-16.0.0and later.- The
.clang-format-ignorefile lists code that should not be formatted, typically third-party libraries that should keep their original style.
Environment Setup
You need to install clang-format, or use a clang-format plugin provided by your IDE/editor. Doris currently uses clang-format 16 for code formatting (different versions of clang-format may produce different results).
Install clang-format
| System | Recommended approach |
|---|---|
| Linux | Use the LDB toolchain (which already includes the matching version), or build/install the binary yourself |
| macOS | brew install clang-format@16 |
LDB toolchain notes: the latest version (>= v0.18) already includes a prebuilt clang-format 16.0.0 binary. For downloads, see ldb_toolchain_gen Releases.
Use an IDE Plugin
| IDE | Plugin | Notes |
|---|---|---|
| CLion | ClangFormat (search and download under File -> Setting -> Plugins) | Make sure the clang-format version is 16 |
| VS Code | Clang-Format extension | You need to manually specify the path to the clang-format executable |
Usage
Run from the Command Line
-
Enter the Doris root directory:
cd ${DORIS_HOME} -
Run the formatting script:
build-support/clang-format.sh
Note: the
clang-format.shscript requires Python 3 to be installed on the machine.
Use clang-format in CLion
After installing the ClangFormat plugin, click Reformat Code to format the current file.
Use clang-format in VS Code
-
Install the
Clang-Formatextension. -
Open the VS Code settings page, search for
clang_format, and fill in the following configuration:{
"clang_format_path": "$clang-format path$",
"clang_format_style": "file"
} -
Right-click and select
Format Documentto format the file.
FAQ
Q: Will different clang-format versions cause inconsistent formatting?
Yes. Use the clang-format 16 series strictly, otherwise the format may not match what the project expects and CI may fail.
Q: Will third-party code also be formatted?
No. Directories listed in .clang-format-ignore are skipped, so that third-party dependencies keep their original code style.