clang-format配置教程

Posted by DEVIN on Tue, Aug 29, 2023

配置clang-format

Qt Creator使用clang-format_利白的博客-CSDN博客
Git 如何将clang-formatting添加到预提交钩子|极客教程

clang-format二进制文件下载:https://llvm.org/builds
Qt Creator自定义clang-format配置文件位置:C:\Users\<username>\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat

clang-format选项

Clang-Format Style Options — Clang 18.0.0git documentation
.clang-format配置文件是yaml语法格式。
注意:配置文件要保存为UTF-8编码,否则可能会格式化代码失败。

 1# 语言
 2Language: Cpp
 3# 基础样式
 4BasedOnStyle: LLVM
 5# 允许修改头文件顺序
 6SortIncludes: false
 7# 指针的*的挨着哪边,例如: int* ptr
 8DerivePointerAlignment: true
 9PointerAlignment: Left
10# 访问修饰符前的空格,例如: public/private
11AccessModifierOffset: -4
12# 缩进宽度
13IndentWidth: 4
14# 要保留的最大连续空行数
15MaxEmptyLinesToKeep: 2
16# 大括号{}的换行方式,也可以定义为Custom,然后对if/class等分别设置
17BreakBeforeBraces: Linux
18# 是否允许短方法单行,例如: int f() { return 0; }
19AllowShortFunctionsOnASingleLine: true
20# 支持一行的if表达式,例如: if (a) return;
21AllowShortIfStatementsOnASingleLine: false
22# 在未封闭(括号的开始和结束不在同一行)的括号中的代码是否对齐,为true,则将参数在左方括号后水平对齐
23AlignAfterOpenBracket: true
24# switch的case缩进
25IndentCaseLabels: true
26# 每行字符的长度
27ColumnLimit: 120
28# 注释对齐
29AlignTrailingComments: true
30# 括号后加空格,例如: (int) i;
31SpaceAfterCStyleCast: false
32# 换行的时候对齐操作符
33AlignOperands: true
34# 中括号两边空格 []
35SpacesInSquareBrackets: false
36# 多行声明语句按照=对齐
37AlignConsecutiveDeclarations: false
38# 容器类的空格 例如: OC的字典
39SpacesInContainerLiterals: false
40# 构造函数初始化列表,冒号后面断行
41BreakConstructorInitializers: AfterColon
42# 函数参数换行
43AllowAllParametersOfDeclarationOnNextLine: true
44# 在续行(#下一行)时的缩进长度
45ContinuationIndentWidth: 4
46# tab键盘的宽度
47TabWidth: 4
48# 赋值运算符前加空格
49SpaceBeforeAssignmentOperators: true
50# 行尾的注释前加1个空格
51SpacesBeforeTrailingComments: 1