2. 代码风格
本项目使用 clang-format 统一 C/C++ 代码风格,配置文件为根目录 .clang-format。
2.1 安装 clang-format
macOS
brew install clang-format
Linux(Ubuntu/Debian)
sudo apt-get install clang-format
CentOS/RHEL
sudo yum install clang-tools-extra
2.2 使用格式化脚本
# 格式化全部
python3 scripts/format.py
# 预览
python3 scripts/format.py --dry-run
# CI 检查
python3 scripts/format.py --check
# 指定目录
python3 scripts/format.py autonomy/navigator
详见 18 Tools · 开发脚本。
2.3 .clang-format 要点
选项 |
值 |
|---|---|
基础风格 |
Google( |
缩进 |
4 空格( |
Tab |
不使用( |
列宽 |
80( |
指针对齐 |
左对齐( |
include 排序 |
开启( |
命名空间注释 |
自动修复( |
完整选项见项目根目录 .clang-format。
2.4 注意事项
格式化会修改文件,建议先提交或备份
全量格式化 1700+ 文件可能耗时较长
CI 可使用
python3 scripts/format.py --check门禁脚本自动查找
clang-format-13…clang-format-17
2.5 示例工作流
python3 scripts/format.py --dry-run
python3 scripts/format.py
python3 scripts/format.py --check