当前位置:   article > 正文

使用clang-format格式化c++代码_clang-format 指定风格文件

clang-format 指定风格文件

1 前言

clang-format是一个可以格式化代码的工具,本文将介绍如何在命令行中使用clang-format来格式化c++代码,然后会给出一种在c++工程中利用脚本来格式化所有c++文件的工作流程,最后将介绍clang-format中的配置字段,根据自定义的配置字段可以创建自己的代码格式风格。

2 clang-format的使用

查看clang-format版本信息的命令:

clang-format --version

输出结果为安装的clang-format版本:

clang-format version 17.0.6

假如有一个需要格式化的文件test.cpp

  1. #include <iostream>
  2. int main()
  3. {
  4. int a = 0;
  5. int b = 0;
  6. return 0;
  7. }

只需要在test.cpp文件所在的目录打开终端,并输入以下命令:

clang-format -style=llvm -i test.cpp 

test.cpp文件即可被格式化为如下:

  1. #include <iostream>
  2. int main() {
  3. int a = 0;
  4. int b = 0;
  5. return 0;
  6. }

命令行中的-style指定了目标格式的风格,可以选择llvm、google、gnu等格式风格。

另外,我们也可以在test.cpp目录下放置一个.clang-format文件

  1. .
  2. ├── .clang-format
  3. └── test.cpp

然后使用命令

clang-format -i test.cpp 

即可将test.cpp格式化为目标格式。需要注意的是这种方式不仅对.clang-format文件所在的目录有效,对其子目录的文件也可以格式化。我们可以使用以下命令来快速生成一个.clang-format文件:

clang-format -style=llvm -dump-config > .clang-format

然后可以根据需要修改这个文件形成我们自己的代码格式化风格。

3 在c++项目中使用clang-format

假设有一个c++项目目录结构如下图所示:

  1. .
  2. ├── .clang-format
  3. ├── Script
  4. │ └── FormatFile.sh
  5. ├── Source
  6. │ ├── App
  7. │ │ └── main.cpp
  8. │ ├── LibA
  9. │ │ ├── LibA.cpp
  10. │ │ └── LibA.h
  11. │ └── LibB
  12. │ ├── LibB.cpp
  13. │ └── LibB.h
  14. └── format.sh

其中Source目录包含了cpp源文件,.clang-format文件是自定义的格式风格文件,Script目录包含了项目使用的脚本文件,Script中的FormatFile.sh提供了格式化某个目录下所有文件的接口,实现如下:

  1. function FormatFile()
  2. {
  3. for file in $(ls $1)
  4. do
  5. if [ -d $1"/"$file ]; then
  6. FormatFile $1"/"$file
  7. else
  8. if [[ $1"/"$file = *.h || $1"/"$file = *.cpp ]]; then
  9. clang-format -i $1"/"$file
  10. echo format $1"/"$file
  11. fi
  12. fi
  13. done
  14. }

format.sh脚本通过调用FormatFile.sh中的接口对整个项目中的c++文件进行格式化,每次修改或者提交代码时都可以运行这个脚本,其实现如下:

  1. source ./Script/FormatFile.sh
  2. echo -e "--- start clang-format ---"
  3. FormatFile Source
  4. echo -e "--- finish clang-format ---"

对FormatFile接口传入Source参数,即可将Source目录下的所有c++文件格式化

4 自定义配置字段

我们可以通过修改.clang-format文件中的字段来实现自己的代码风格,可参考clang-format的官方文档: 

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

本节内容基于clang-format 17

4.1 Language

指定编程语言:

Language:        Cpp

4.2 AccessModifierOffset

访问修饰符的缩进(例如public,private等)

  1. AccessModifierOffset: -4
  2. // 修饰符缩进为-4(假设缩进参数为4)
  3. class MyClass {
  4. public:
  5. MyClass();
  6. private:
  7. int m_Num = 0;
  8. };

4.3 AlignAfterOpenBracket

当需要换行时,控制参数的对齐风格

  1. // AlignAfterOpenBracket: Align
  2. someLooooooooooooongFunction(LoooooooooooooooooooooooooooooooooooooongArgument1,
  3. LoooooooooooooooooooooooooooooooooooooongArgument2,
  4. Argument3);
  5. // AlignAfterOpenBracket: DontAlign
  6. someLooooooooooooongFunction(LoooooooooooooooooooooooooooooooooooooongArgument1,
  7. LoooooooooooooooooooooooooooooooooooooongArgument2, Argument3);
  8. // AlignAfterOpenBracket: AlwaysBreak
  9. someLooooooooooooongFunction(
  10. LoooooooooooooooooooooooooooooooooooooongArgument1,
  11. LoooooooooooooooooooooooooooooooooooooongArgument2, Argument3);
  12. // AlignAfterOpenBracket: BlockIndent
  13. someLooooooooooooongFunction(
  14. LoooooooooooooooooooooooooooooooooooooongArgument1,
  15. LoooooooooooooooooooooooooooooooooooooongArgument2, Argument3
  16. );

4.4 AlignArrayOfStructures

对结构数组初始化时,字段排列成列的方式

  1. // None
  2. AlignArrayOfStructures: None
  3. int Nums[4][3] = {
  4. {56, 23, 1557}, {-1, 93463, 422}, {7, 5, 10}, {7801, 15, 8010}};
  5. // 按左对齐
  6. AlignArrayOfStructures: Left
  7. int Nums[4][3] = {
  8. {56, 23, 1557},
  9. {-1, 93463, 422 },
  10. {7, 5, 10 },
  11. {7801, 15, 8010}
  12. };
  13. // 按右对齐
  14. AlignArrayOfStructures: Right
  15. int Nums[4][3] = {
  16. { 56, 23, 1557},
  17. { -1, 93463, 422},
  18. { 7, 5, 10},
  19. {7801, 15, 8010}
  20. };

4.5 AlignConsecutiveAssignments

连续赋值语句的对齐方式,有Enabled、AcrossEmptyLines、AcrossComments、AlignCompound、PadOperators五个选项,分别可以有true和false两个值

Enabled: 是否开启对齐

  1. // Enabled: false
  2. int a = 0;
  3. int bbbbb = 1;
  4. int cc = 5;
  5. // Enabled: true
  6. int a = 0;
  7. int bbbbb = 1;
  8. int cc = 5;

AcrossEmptyLines: 是否跨空行对齐

  1. // AcrossEmptyLines: false
  2. int a = 0;
  3. int bbbbb = 1;
  4. int cc = 5;
  5. int somelongname = 2;
  6. double c = 3;
  7. // AcrossEmptyLines: true
  8. int a = 0;
  9. int bbbbb = 1;
  10. int cc = 5;
  11. int somelongname = 2;
  12. double c = 3;

 AcrossComments: 是否跨注释对齐

  1. // AcrossComments: false
  2. int a = 3;
  3. int bbb = 10;
  4. /* comment. */
  5. double e = 4;
  6. float fffff = 4;
  7. // AcrossComments: true
  8. int a = 3;
  9. int bbb = 10;
  10. /* comment. */
  11. double e = 4;
  12. float fffff = 4;

 AlignCompound: 复合赋值是否和等号赋值一起对齐

  1. // AlignCompound: false
  2. aaaaaa &= 2;
  3. bb = 2;
  4. // AlignCompound: true
  5. aaaaaa &= 2;
  6. bb = 2;

PadOperators: 短赋值运算符号是否左填充到和长赋值运算符相同的长度

  1. // AlignCompound需要为true
  2. // PadOperators: false
  3. a >>= 2;
  4. bbb = 2;
  5. a = 2;
  6. bbb >>= 2;
  7. // PadOperators: true
  8. a >>= 2;
  9. bbb = 2;
  10. a = 2;
  11. bbb >>= 2;

4.6 AlignConsecutiveBitFields 

连续位字段的对齐方式

  1. // Enabled: false
  2. struct MyStruct {
  3. int aaaa : 1;
  4. int b : 12;
  5. int ccc : 8;
  6. };
  7. // Enabled: true
  8. struct MyStruct {
  9. int aaaa : 1;
  10. int b : 12;
  11. int ccc : 8;
  12. };

4.7 AlignConsecutiveDeclarations

连续的声明语句的对齐方式

  1. // AlignConsecutiveDeclarations: false
  2. int aaaa = 12;
  3. float b = 23;
  4. std::string ccc;
  5. // AlignConsecutiveDeclarations: true
  6. int aaaa = 12;
  7. float b = 23;
  8. std::string ccc;

4.8 AlignConsecutiveMacros

连续的宏定义的对齐方式

  1. // AlignConsecutiveMacros: false
  2. #define SHORT_NAME 42
  3. #define LONGER_NAME 0x007f
  4. #define EVEN_LONGER_NAME (2)
  5. #define foo(x) (x * x)
  6. #define bar(y, z) (y + z)
  7. // AlignConsecutiveMacros: true
  8. #define SHORT_NAME 42
  9. #define LONGER_NAME 0x007f
  10. #define EVEN_LONGER_NAME (2)
  11. #define foo(x) (x * x)
  12. #define bar(y, z) (y + z)

4.9 AlignConsecutiveShortCaseStatements

用于控制连续的case语句的对齐方式,需要设置AllowShortCaseLabelsOnASingleLine为true

Enabled

  1. // Enabled: false
  2. switch (level)
  3. {
  4. case log::info: return "info:";
  5. case log::warning: return "warning:";
  6. default: return "";
  7. }
  8. // Enabled: true
  9. switch (level)
  10. {
  11. case log::info: return "info:";
  12. case log::warning: return "warning:";
  13. default: return "";
  14. }

AcrossEmptyLines

  1. // AcrossEmptyLines: false
  2. switch (level)
  3. {
  4. case 123: return 123;
  5. case 12'344'444: return 12'344'444;
  6. case 234: return 234;
  7. default: return 234;
  8. }
  9. // AcrossEmptyLines: true
  10. switch (level)
  11. {
  12. case 123: return 123;
  13. case 12'344'444: return 12'344'444;
  14. case 234: return 234;
  15. default: return 234;
  16. }

AcrossComments

  1. // AcrossComments: false
  2. switch (level)
  3. {
  4. case 123: return 123;
  5. case 12'344'444: return 12'344'444;
  6. // A comment
  7. case 234: return 234;
  8. default: return 234;
  9. }
  10. // AcrossComments: true
  11. switch (level)
  12. {
  13. case 123: return 123;
  14. case 12'344'444: return 12'344'444;
  15. // A comment
  16. case 234: return 234;
  17. default: return 234;
  18. }

 AlignCaseColons

  1. // AlignCaseColons: false
  2. switch (level)
  3. {
  4. case 123: return 123;
  5. case 12'344'444: return 12'344'444;
  6. case 234: return 234;
  7. default: return 234;
  8. }
  9. // AlignCaseColons: true
  10. switch (level)
  11. {
  12. case 123 : return 123;
  13. case 12'344'444: return 12'344'444;
  14. case 234 : return 234;
  15. default : return 234;
  16. }

4.10 AlignEscapedNewlines

转义换行符的对齐方式

  1. // AlignEscapedNewlines: DontAlign
  2. #define A \
  3. int aaaa; \
  4. int b; \
  5. int dddddddddd;
  6. // AlignEscapedNewlines: Left
  7. #define A \
  8. int aaaa; \
  9. int b; \
  10. int dddddddddd;
  11. // AlignEscapedNewlines: Right
  12. #define A \
  13. int aaaa; \
  14. int b; \
  15. int dddddddddd;

4.11 AlignOperands

换行时,二元和三元操作符的对齐方式

  1. // BreakBeforeBinaryOperators设置为All
  2. // AlignOperands: DontAlign
  3. int aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  4. + ccccccccccccccccccccccccccccccccccccccccc;
  5. // AlignOperands: Align
  6. int aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  7. + ccccccccccccccccccccccccccccccccccccccccc;
  8. // AlignOperands: AlignAfterOperator
  9. int aaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  10. + ccccccccccccccccccccccccccccccccccccccccc;

4.12 AlignTrailingComments

控制尾部注释的对齐方式,有Kind和OverEmptyLines两个选项,Kind控制对齐的方式,OverEmptyLines控制对齐的空行数

  1. // Kind: Leave
  2. int a; // comment
  3. int abbbbbb; // comment
  4. // Kind: Always
  5. int a; // comment
  6. int abbbbbb; // comment
  7. // Kind: Never
  8. int a; // comment
  9. int abbbbbb; // comment

4.13 AllowAllArgumentsOnNextLine

函数调用需要换行时,是否允许把所有的参数放到下一行

  1. // BinPackArguments设置为false
  2. // AllowAllArgumentsOnNextLine: false
  3. void func()
  4. {
  5. dosomething();
  6. funccccccccccccccccccccc(
  7. LoooooooooooooooooooooooogArg1,
  8. LongArg2,
  9. LongArg2
  10. );
  11. }
  12. // AllowAllArgumentsOnNextLine: true
  13. void func()
  14. {
  15. dosomething();
  16. funccccccccccccccccccccc(
  17. LoooooooooooooooooooooooogArg1, LongArg2, LongArg2
  18. );
  19. }

4.14 AllowAllParametersOfDeclarationOnNextLine

是否允许把所有的参数声明放到下一行

4.15 AllowShortBlocksOnASingleLine

是否允许短块在一行中
 

  1. // AllowShortBlocksOnASingleLine设置为Always
  2. // AllowShortBlocksOnASingleLine: Never
  3. while (true)
  4. {
  5. }
  6. while (true)
  7. {
  8. continue;
  9. }
  10. // AllowShortBlocksOnASingleLine: Empty
  11. while (true)
  12. {
  13. }
  14. while (true)
  15. {
  16. continue;
  17. }
  18. // AllowShortBlocksOnASingleLine: Always
  19. while (true) { }
  20. while (true) { continue; }

4.16 AllowShortCaseLabelsOnASingleLine

是否允许短的case标签在一行中

  1. // AllowShortCaseLabelsOnASingleLine: false
  2. switch (a) {
  3. case 1:
  4. x = 1;
  5. break;
  6. case 2:
  7. x = 5;
  8. break;
  9. default:
  10. break;
  11. }
  12. // AllowShortCaseLabelsOnASingleLine: true
  13. switch (a) {
  14. case 1: x = 1; break;
  15. case 2: x = 5; break;
  16. default: break;
  17. }

4.17 AllowShortEnumsOnASingleLine

是否允许短的枚举类型在一行中

  1. // AllowShortEnumsOnASingleLine: false
  2. enum class Color {
  3. Red,
  4. Green,
  5. Blue
  6. };
  7. // AllowShortEnumsOnASingleLine: true
  8. enum class Color { Red, Green, Blue };

4.18 AllowShortFunctionsOnASingleLine

是否允许短函数在一行中

  1. // AllowShortFunctionsOnASingleLine: None
  2. // 不允许单行
  3. class Foo {
  4. void Func1() {
  5. foo();
  6. }
  7. void Func2() {
  8. }
  9. };
  10. void Func3() {
  11. foo();
  12. }
  13. void Func4() {
  14. }
  15. // AllowShortFunctionsOnASingleLine: InlineOnly
  16. // 只有类里面的短函数单行
  17. class Foo {
  18. void Func1() { foo(); }
  19. void Func2() {}
  20. };
  21. void Func3() {
  22. foo();
  23. }
  24. void Func4() {
  25. }
  26. // AllowShortFunctionsOnASingleLine: Inline
  27. // 只有类里面的短函数以及空函数单行
  28. class Foo {
  29. void Func1() { foo(); }
  30. void Func2() {}
  31. };
  32. void Func3() {
  33. foo();
  34. }
  35. void Func4() {}
  36. // AllowShortFunctionsOnASingleLine: All
  37. // 所有短函数单行
  38. class Foo {
  39. void Func1() { foo(); }
  40. void Func2() {}
  41. };
  42. void Func3() { foo(); }
  43. void Func4() {}

4.19 AllowShortIfStatementsOnASingleLine

是否允许短的if语句在一行中

  1. // AllowShortIfStatementsOnASingleLine: Never
  2. if (a)
  3. return;
  4. // AllowShortIfStatementsOnASingleLine: WithoutElse
  5. if (a) return;
  6. if (a)
  7. return;
  8. else
  9. return;
  10. // AllowShortIfStatementsOnASingleLine: OnlyFirstIf
  11. if (a) return;
  12. if (a == 1) return;
  13. else if (a == 2)
  14. return;
  15. else
  16. return;
  17. // AllowShortIfStatementsOnASingleLine: AllIfsAndElse
  18. if (a) return;
  19. if (a == 1) return;
  20. else if (a == 2) return;
  21. else return;

4.20 AllowShortLambdasOnASingleLine

是否允许短的lambda函数在一行

  1. // AllowShortLambdasOnASingleLine: None
  2. auto lambda = [](int x, int y) {
  3. };
  4. auto lambda2 = [](int x, int y) {
  5. return x < y;
  6. };
  7. std::sort(a.begin(), a.end(), [](int x, int y) {
  8. return x < y;
  9. });
  10. // AllowShortLambdasOnASingleLine: Empty
  11. auto lambda = [](int x, int y) {};
  12. auto lambda2 = [](int x, int y) {
  13. return x < y;
  14. };
  15. std::sort(a.begin(), a.end(), [](int x, int y) {
  16. return x < y;
  17. });
  18. // AllowShortLambdasOnASingleLine: Inline
  19. auto lambda = [](int x, int y) {
  20. };
  21. auto lambda2 = [](int x, int y) {
  22. return x < y;
  23. };
  24. std::sort(a.begin(), a.end(), [](int x, int y) { return x < y; });
  25. // AllowShortLambdasOnASingleLine: All
  26. auto lambda = [](int x, int y) {};
  27. auto lambda2 = [](int x, int y) { return x < y; };
  28. std::sort(a.begin(), a.end(), [](int x, int y) { return x < y; });

4.21 AllowShortLoopsOnASingleLine

  1. // AllowShortLoopsOnASingleLine: false
  2. for (int i = 0; i < 10; ++i) {
  3. doSomething(i);
  4. }
  5. while (true) {
  6. doSomething(i);
  7. }
  8. // AllowShortLoopsOnASingleLine: true
  9. for (int i = 0; i < 10; ++i) { doSomething(i); }
  10. while (true) { doSomething(i); }

4.22 AlwaysBreakAfterDefinitionReturnType

废弃的字段

4.23 AlwaysBreakAfterReturnType

函数声明返回的换行方式

  1. // AlwaysBreakAfterReturnType: None
  2. class A
  3. {
  4. int f() { return 0; };
  5. };
  6. int f();
  7. int f() { return 1; }
  8. // AlwaysBreakAfterReturnType: All
  9. class A
  10. {
  11. int
  12. f()
  13. {
  14. return 0;
  15. };
  16. };
  17. int
  18. f();
  19. int
  20. f()
  21. {
  22. return 1;
  23. }
  24. // AlwaysBreakAfterReturnType: TopLevel
  25. class A
  26. {
  27. int f() { return 0; };
  28. };
  29. int
  30. f();
  31. int
  32. f()
  33. {
  34. return 1;
  35. }
  36. // AlwaysBreakAfterReturnType: AllDefinitions
  37. class A
  38. {
  39. int
  40. f()
  41. {
  42. return 0;
  43. };
  44. };
  45. int f();
  46. int
  47. f()
  48. {
  49. return 1;
  50. }
  51. // AlwaysBreakAfterReturnType: TopLevelDefinitions
  52. class A
  53. {
  54. int f() { return 0; };
  55. };
  56. int f();
  57. int
  58. f()
  59. {
  60. return 1;
  61. }

4.24 AlwaysBreakBeforeMultilineStrings

决定多行字符前是否换行

  1. // AlwaysBreakBeforeMultilineStrings: false
  2. aaaa = "bbbb"
  3. "cccc";
  4. // AlwaysBreakBeforeMultilineStrings: true
  5. aaaa =
  6. "bbbb"
  7. "cccc";

4.25 AlwaysBreakTemplateDeclarations

模版声明的换行方式

  1. // AlwaysBreakTemplateDeclarations: No
  2. template <typename T> T foo() { }
  3. template <typename T> T foo(
  4. int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  5. )
  6. {
  7. }
  8. // AlwaysBreakTemplateDeclarations: MultiLine
  9. template <typename T>
  10. T foo(
  11. int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  12. )
  13. {
  14. }
  15. // AlwaysBreakTemplateDeclarations: Yes
  16. template <typename T>
  17. T foo()
  18. {
  19. }
  20. template <typename T>
  21. T foo(
  22. int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
  23. )
  24. {
  25. }

4.26 AttributeMacros

设置的字符串被解释为属性/限定符,而不是标识符

4.27 BinPackArguments

在函数调用时,设置为false时,如果参数需要换行,那么每个参数都换行

  1. // BinPackArguments: false
  2. void func()
  3. {
  4. funccccccccccccccccccccc(
  5. LoooooooooooooooooooooooooooooooooooonoooooooooooooogArg1,
  6. LongArg2,
  7. LongArg2
  8. );
  9. }
  10. // BinPackArguments: true
  11. void func()
  12. {
  13. funccccccccccccccccccccc(
  14. LoooooooooooooooooooooooooooooooooooonoooooooooooooogArg1, LongArg2,
  15. LongArg2
  16. );
  17. }

4.28 BinPackParameters

在函数声明/定义时,设置为false时,如果参数需要换行,那么每个参数都换行

  1. // BinPackParameters: false
  2. void f(
  3. int aaaaaaaaaaaaaaaaaaaa1,
  4. int aaaaaaaaaaaaaaaaaaaa2,
  5. int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3
  6. )
  7. {
  8. }
  9. // BinPackParameters: true
  10. void f(
  11. int aaaaaaaaaaaaaaaaaaaa1, int aaaaaaaaaaaaaaaaaaaa2,
  12. int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3
  13. )
  14. {
  15. }

4.29 BitFieldColonSpacing

控制位域操作符两边是否有空格

  1. // BitFieldColonSpacing: Both
  2. struct MyStruct {
  3. int a : 8;
  4. char b : 10;
  5. }
  6. // BitFieldColonSpacing: None
  7. struct MyStruct {
  8. int a:8;
  9. char b:10;
  10. }
  11. // BitFieldColonSpacing: Before
  12. struct MyStruct {
  13. int a :8;
  14. char b :10;
  15. }
  16. // BitFieldColonSpacing: After
  17. struct MyStruct {
  18. int a: 8;
  19. char b: 10;
  20. }

4.30 BraceWrapping

用于控制大括号的换行,想要BraceWrapping生效需要设置BreakBeforeBraces为Custom

AfterCaseLabel: case标签的大括号

  1. // AfterCaseLabel: false
  2. switch (a) {
  3. case 1: {
  4. dosomething();
  5. break;
  6. }
  7. case 1: {
  8. dosomething();
  9. break;
  10. }
  11. default: {
  12. break;
  13. }
  14. }
  15. // AfterCaseLabel: true
  16. switch (a) {
  17. case 1:
  18. {
  19. dosomething();
  20. break;
  21. }
  22. case 1:
  23. {
  24. dosomething();
  25. break;
  26. }
  27. default:
  28. {
  29. break;
  30. }
  31. }

AfterClass: 类定义的大括号

  1. // AfterClass: false
  2. class MyClass {
  3. public:
  4. MyClass();
  5. }
  6. // AfterClass: true
  7. class MyClass
  8. {
  9. public:
  10. MyClass();
  11. }

AfterControlStatement: 控制语句(if/for/while/switch/..)的大括号

  1. // AfterControlStatement: Never
  2. if (a && b) {
  3. }
  4. if (aaaaaaaaaaaaaaaaaaaa &&
  5. bbbbbbbbbbbbbbbbbbbbbbbbbbbbb & ccccccccccccccccccccc) {
  6. }
  7. // AfterControlStatement: MultiLine
  8. if (a && b) {
  9. }
  10. if (aaaaaaaaaaaaaaaaaaaa &&
  11. bbbbbbbbbbbbbbbbbbbbbbbbbbbbb & ccccccccccccccccccccc)
  12. {
  13. }
  14. // AfterControlStatement: Always
  15. if (a && b)
  16. {
  17. }
  18. if (aaaaaaaaaaaaaaaaaaaa &&
  19. bbbbbbbbbbbbbbbbbbbbbbbbbbbbb & ccccccccccccccccccccc)
  20. {
  21. }

AfterEnum: enum定义的大括号

  1. // AfterEnum: false
  2. enum class Color {
  3. Red,
  4. Green,
  5. Blue
  6. };
  7. // AfterEnum: true
  8. enum class Color
  9. {
  10. Red,
  11. Green,
  12. Blue
  13. };

AfterExternBlock: extern代码块的大括号

  1. // AfterExternBlock: false
  2. extern "C" {
  3. int foo();
  4. }
  5. // AfterExternBlock: true
  6. extern "C"
  7. {
  8. int foo();
  9. }

AfterFunction: 函数定义的大括号

  1. // AfterFunction: false
  2. void foo() {
  3. dosomething();
  4. dosomething();
  5. }
  6. // AfterFunction: true
  7. void foo()
  8. {
  9. dosomething();
  10. dosomething();
  11. }

AfterNamespace: namespace定义的大括号

  1. // AfterNamespace: false
  2. namespace MyNamespace {
  3. void Foo1();
  4. void Foo2();
  5. } // namespace MyNamespace
  6. // AfterNamespace: true
  7. namespace MyNamespace
  8. {
  9. void Foo1();
  10. void Foo2();
  11. } // namespace MyNamespace

AfterObjCDeclaration: 控制Objective-C声明后的代码格式

AfterStruct: struct定义的大括号

  1. // AfterStruct: false
  2. struct MyStruct {
  3. int a;
  4. int b;
  5. }
  6. // AfterStruct: true
  7. struct MyStruct
  8. {
  9. int a;
  10. int b;
  11. }

AfterUnion: union定义的大括号

  1. // AfterUnion: false
  2. union MyUnion {
  3. int a;
  4. double b;
  5. }
  6. // AfterUnion: true
  7. union MyUnion
  8. {
  9. int a;
  10. double b;
  11. }

BeforeCatch: catch前的大括号

  1. // BeforeCatch: false
  2. try {
  3. foo();
  4. } catch {
  5. dosomething();
  6. }
  7. // BeforeCatch: true
  8. try {
  9. foo();
  10. }
  11. catch {
  12. dosomething();
  13. }

BeforeElse: else之前的大括号

  1. // BeforeElse: false
  2. if (a) {
  3. foo1();
  4. } else {
  5. foo2();
  6. }
  7. // BeforeElse: true
  8. if (a) {
  9. foo1();
  10. }
  11. else {
  12. foo2();
  13. }

BeforeLambdaBody: lambda函数的大括号

  1. // BeforeLambdaBody: false
  2. auto lambda2 = [](int x, int y) {
  3. dosomething();
  4. dosomething();
  5. };
  6. // BeforeLambdaBody: true
  7. auto lambda2 = [](int x, int y)
  8. {
  9. dosomething();
  10. dosomething();
  11. };

BeforeWhile: while前的大括号

  1. // BeforeWhile: false
  2. do {
  3. foo();
  4. } while (1);
  5. // BeforeWhile: true
  6. do {
  7. foo();
  8. }
  9. while (1);

IndentBraces: 

不知道有什么用

SplitEmptyFunction: 决定空函数的大括号是否放在一行

  1. // AfterFunction设置为true
  2. // SplitEmptyFunction: false
  3. void Func()
  4. {}
  5. // SplitEmptyFunction: true
  6. void Func()
  7. {
  8. }

SplitEmptyRecord: 控制空的class/stuct/union的大括号是否放在一行

  1. // AfterStruct设置为true
  2. // SplitEmptyRecord: false
  3. struct MyStruct
  4. {}
  5. // SplitEmptyRecord: true
  6. struct MyStruct
  7. {
  8. }

SplitEmptyNamespace: 空namespace的大括号是否放在一行

  1. // AfterNamespace设置为true
  2. // SplitEmptyNamespace: false
  3. namespace MyNamespace
  4. {}
  5. // SplitEmptyNamespace: true
  6. namespace MyNamespace
  7. {
  8. }

4.31 BreakAfterAttributes

是否在属性声明后换行

  1. // BreakAfterAttributes: Never
  2. void Func()
  3. {
  4. [[maybe_unused]] const int i;
  5. [[gnu::const]] [[maybe_unused]] int j;
  6. [[likely]] if (a)
  7. f();
  8. else
  9. g();
  10. }
  11. [[nodiscard]] inline int f();
  12. [[gnu::const]] [[nodiscard]] int g();
  13. // BreakAfterAttributes: Always
  14. void Func()
  15. {
  16. [[maybe_unused]] const int i;
  17. [[gnu::const]] [[maybe_unused]] int j;
  18. [[likely]] if (a)
  19. f();
  20. else
  21. g();
  22. }
  23. [[nodiscard]]
  24. inline int f();
  25. [[gnu::const]] [[nodiscard]]
  26. int g();
  27. // BreakAfterAttributes: Leave 保持原样

4.32 BreakAfterJavaFieldAnnotations

是否在Java字段注解之后换行

4.33 BreakArrays

用于决定Json文件中的数组是否换行

4.34 BreakBeforeBinaryOperators

用于控制当需要换行时,二元操作符的换行方式

  1. // BreakBeforeBinaryOperators: None
  2. // 在操作符后换行
  3. LooooooooooongType loooooooooooooooooooooongVariable =
  4. someLooooooooooooooooongFunction();
  5. bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
  6. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==
  7. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
  8. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >
  9. ccccccccccccccccccccccccccccccccccccccccc;
  10. // BreakBeforeBinaryOperators: NonAssignment
  11. // 对于非赋值符号,在操作符前换行
  12. LooooooooooongType loooooooooooooooooooooongVariable =
  13. someLooooooooooooooooongFunction();
  14. bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  15. + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  16. == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  17. && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  18. > ccccccccccccccccccccccccccccccccccccccccc;
  19. // BreakBeforeBinaryOperators: All
  20. // 在操作符前换行
  21. LooooooooooongType loooooooooooooooooooooongVariable
  22. = someLooooooooooooooooongFunction();
  23. bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  24. + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  25. == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  26. && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  27. > ccccccccccccccccccccccccccccccccccccccccc;

4.35 BreakBeforeConceptDeclarations

concept声明的换行格式

  1. // BreakBeforeConceptDeclarations: Never
  2. template <typename T> concept C = ...;
  3. // BreakBeforeConceptDeclarations: Always
  4. template <typename T>
  5. concept C = ...;
  6. // BreakBeforeConceptDeclarations: Allowed
  7. // 允许concept声明换行,具体和代码内容相关

4.36 BreakBeforeBraces

大括号的换行方式,如果定义为Custom,则使用BraceWrapping来自定义大括号的换行方式,其他的值有

Attach: 大括号始终和周围的上下文保持一行

Allman: 大括号始终换行

以及Linux、Mozilla、Stroustrup、Whitesmiths、GNU、WebKit等可选值

4.37 BreakBeforeInlineASMColon

用于控制是否在汇编代码中的冒号前换行

4.38 BreakBeforeTernaryOperators

控制三元运算符的换行

  1. // BreakBeforeTernaryOperators: false
  2. veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ?
  3. firstValue :
  4. SecondValueVeryVeryVeryVeryLong;
  5. // BreakBeforeTernaryOperators: true
  6. veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription
  7. ? firstValue
  8. : SecondValueVeryVeryVeryVeryLong;

4.39 BreakConstructorInitializers

控制构造函数初始化列表这么换行

  1. // 设置PackConstructorInitializers为Never
  2. // BreakConstructorInitializers: BeforeColon
  3. struct MyClass
  4. {
  5. MyClass()
  6. : m_a(0),
  7. m_b(0)
  8. {
  9. }
  10. int m_a;
  11. int m_b;
  12. };
  13. // BreakConstructorInitializers: BeforeComma
  14. struct MyClass
  15. {
  16. MyClass()
  17. : m_a(0)
  18. , m_b(0)
  19. {
  20. }
  21. int m_a;
  22. int m_b;
  23. };
  24. // BreakConstructorInitializers: AfterColon
  25. struct MyClass
  26. {
  27. MyClass():
  28. m_a(0),
  29. m_b(0)
  30. {
  31. }
  32. int m_a;
  33. int m_b;
  34. };

4.40 BreakInheritanceList

设置继承列表的换行风格

  1. // BreakInheritanceList: BeforeColon
  2. class Foo : Base1, Base2, Base3
  3. {
  4. };
  5. // BreakInheritanceList: BeforeComma
  6. class Foo
  7. : Base1
  8. , Base2
  9. , Base3
  10. {
  11. };
  12. // BreakInheritanceList: AfterColon
  13. class Foo : Base1, Base2, Base3
  14. {
  15. };
  16. // BreakInheritanceList: AfterComma
  17. class Foo : Base1,
  18. Base2,
  19. Base3
  20. {
  21. };

4.41 BreakStringLiterals

当代码列数超过限制时,是否允许字符串字面量换行

  1. // origin code
  2. const char *x
  3. = "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";
  4. // BreakStringLiterals: false
  5. const char *x
  6. = "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString";
  7. // BreakStringLiterals: true
  8. const char *x
  9. = "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLong"
  10. "String";

4.42 ColumnLimit

设置代码的最大列数,如果设置为0则没有限制

4.43 CommentPragmas

用于控制注释的格式,符合正则表达式的注释保持在同一行

  1. // CommentPragmas: 'TODO'
  2. void Func()
  3. {
  4. int a
  5. = 0; // TODO : a very loooooooooooooooooooooooooooooooooooooooooooooog
  6. }
  7. // CommentPragmas: 'Other'
  8. void Func()
  9. {
  10. int a = 0; // TODO : a very
  11. // loooooooooooooooooooooooooooooooooooooooooooooog
  12. }

4.44 CompactNamespaces

是否将连续的namespace放在一行

  1. // CompactNamespaces: false
  2. namespace Foo {
  3. namespace Bar { }
  4. } // namespace Foo
  5. // CompactNamespaces: true
  6. namespace Foo { namespace Bar {
  7. }} // namespace Foo::Bar

4.45 ConstructorInitializerIndentWidth

构造函数初始化列表以及的继承列表的缩进宽度

  1. // ConstructorInitializerIndentWidth: 4
  2. struct MyClass
  3. {
  4. MyClass():
  5. m_a(0),
  6. m_b(0),
  7. m_c(0)
  8. };
  9. struct MyClass
  10. : Base1
  11. , Base2
  12. {
  13. };
  14. // ConstructorInitializerIndentWidth: 10
  15. struct MyClass
  16. {
  17. MyClass():
  18. m_a(0),
  19. m_b(0),
  20. m_c(0)
  21. };
  22. struct MyClass
  23. : Base1
  24. , Base2
  25. {
  26. };

4.46 ContinuationIndentWidth

连续行的缩进宽度

  1. // ContinuationIndentWidth: 4
  2. int i = // VeryVeryVeryVeryVeryLongComment
  3. longFunction( // Again a long comment
  4. arg1, // Again a long comment
  5. arg2);

4.47 Cpp11BracedListStyle

设置包围列表的格式

  1. // Cpp11BracedListStyle: false
  2. std::vector<int> x{ 1, 2, 3, 4 };
  3. std::vector<T> x{ {}, {}, {}, {} };
  4. f(MyMap[{ composite, key }]);
  5. new int[3]{ 1, 2, 3 };
  6. // Cpp11BracedListStyle: true
  7. std::vector<int> x{1, 2, 3, 4};
  8. std::vector<T> x{{}, {}, {}, {}};
  9. f(MyMap[{composite, key}]);
  10. new int[3]{1, 2, 3};

4.48 DerivePointerAlignment

是否自动推断指针的对齐方式

4.49 DisableFormat

完全禁止格式化

4.50 EmptyLineAfterAccessModifier

是否在访问修饰符后放置空行

  1. // EmptyLineAfterAccessModifier: Never
  2. struct foo {
  3. private:
  4. int i;
  5. protected:
  6. int j;
  7. /* comment */
  8. public:
  9. foo() {
  10. }
  11. private:
  12. protected:
  13. };
  14. // EmptyLineAfterAccessModifier: Leave
  15. // 保持原样
  16. // EmptyLineAfterAccessModifier: Always
  17. struct foo {
  18. private:
  19. int i;
  20. protected:
  21. int j;
  22. /* comment */
  23. public:
  24. foo() {
  25. }
  26. private:
  27. protected:
  28. };

4.51 EmptyLineBeforeAccessModifier

是否在访问修饰符前放置空行

  1. // EmptyLineAfterAccessModifier: Leave 保持原状
  2. // EmptyLineAfterAccessModifier: Never
  3. struct foo
  4. {
  5. private:
  6. int i;
  7. protected:
  8. int j;
  9. /* comment */
  10. public:
  11. foo() { }
  12. private:
  13. protected:
  14. };
  15. // EmptyLineAfterAccessModifier: Always
  16. struct foo
  17. {
  18. private:
  19. int i;
  20. protected:
  21. int j;
  22. /* comment */
  23. public:
  24. foo() { }
  25. private:
  26. protected:
  27. };

4.52 ExperimentalAutoDetectBinPacking

自动检测和调整二进制。这是一个试验性字段,尽量不要使用

4.53 FixNamespaceComments

自动修复命名空间注释的格式问题

  1. // FixNamespaceComments: false
  2. namespace longNamespace {
  3. void foo();
  4. void bar();
  5. }
  6. namespace shortNamespace {
  7. void baz();
  8. }
  9. // FixNamespaceComments: true
  10. namespace longNamespace {
  11. void foo();
  12. void bar();
  13. } // namespace longNamespace
  14. namespace shortNamespace {
  15. void baz();
  16. }

4.54 ForEachMacros

定义的宏应该被解释为foreach循环,而不是函数调用

  1. ForEachMacros:
  2. - foreach
  3. - Q_FOREACH
  4. - BOOST_FOREACH

4.56 IfMacros

定义的宏应该被解释为条件语句,而不是函数调用

  1. IfMacros:
  2. - KJ_IF_MAYBE

4.57 IncludeBlocks

对头文件块进行划分,然后根据IncludeCategories策略对头文件进行排序

  1. // IncludeBlocks: Preserve
  2. // 每个头文件各自排序
  3. #include "a.h"
  4. #include "b.h"
  5. #include <iostream>
  6. // IncludeBlocks: Merge
  7. // 所有的头文件块合成一组后排序
  8. #include "a.h"
  9. #include "b.h"
  10. #include <iostream>
  11. // IncludeBlocks: Regroup
  12. // 所有的头文件块合成一组排序,然后根据种类分成不同的块
  13. #include "a.h"
  14. #include "b.h"
  15. #include <iostream>

4.58 IncludeCategories

定义不同类型的头文件的优先级用于排序

  1. IncludeCategories:
  2. - Regex: '^<ext/.*\.h>'
  3. Priority: 2
  4. SortPriority: 0
  5. CaseSensitive: false
  6. - Regex: '^<.*\.h>'
  7. Priority: 1
  8. SortPriority: 0
  9. CaseSensitive: false
  10. - Regex: '^<.*'
  11. Priority: 2
  12. SortPriority: 0
  13. CaseSensitive: false
  14. - Regex: '.*'
  15. Priority: 3
  16. SortPriority: 0
  17. CaseSensitive: false

Regex用于匹配头文件

Priority定义头文件的优先级,用于排序

SortPriority用于当IncludeBlocks设置为Regroup时,头文件所在组的优先级,如果SortPriority没有赋值,默认和Priority的值相同

CaseSensitive定义是否区分大小写

4.59 IncludeIsMainRegex

用于匹配非标准扩展名的文件,这些文件可以被视为主包含文件

4.60 IncludeIsMainSourceRegex

用于匹配非标准扩展名的文件,这些文件可以被视为主源文件

4.61 IndentAccessModifiers

控制访问修饰符的缩进,如果为false,那么就根据后面的成员以及AccessModifierOffset的值进行缩进,如果为true,那么会将访问修饰符进行单独的缩进

  1. // IndentAccessModifiers: false
  2. class MyClass {
  3. public:
  4. void foo();
  5. public:
  6. int m_b;
  7. };
  8. // IndentAccessModifiers: true
  9. class MyClass {
  10. public:
  11. void foo();
  12. public:
  13. int m_b;
  14. };

4.62 IndentCaseBlocks

case标签后面的大括号是否缩进

  1. // IndentCaseBlocks: false
  2. switch (variable)
  3. {
  4. case 1:
  5. {
  6. dosomething();
  7. break;
  8. }
  9. case 2:
  10. {
  11. dosomething();
  12. break;
  13. }
  14. default:
  15. {
  16. dosomething();
  17. break;
  18. }
  19. }
  20. // IndentCaseBlocks: true
  21. switch (variable)
  22. {
  23. case 1:
  24. {
  25. dosomething();
  26. break;
  27. }
  28. case 2:
  29. {
  30. dosomething();
  31. break;
  32. }
  33. default:
  34. {
  35. dosomething();
  36. break;
  37. }
  38. }

4.63 IndentCaseLabels

case标签是否缩进

  1. // IndentCaseLabels: false
  2. switch (variable)
  3. {
  4. case 1:
  5. dosomething();
  6. break;
  7. case 2:
  8. dosomething();
  9. break;
  10. default:
  11. dosomething();
  12. break;
  13. }
  14. // IndentCaseLabels: true
  15. switch (variable)
  16. {
  17. case 1:
  18. dosomething();
  19. break;
  20. case 2:
  21. dosomething();
  22. break;
  23. default:
  24. dosomething();
  25. break;
  26. }

4.64 IndentExternBlock

控制extern代码块的缩进

  1. // BreakBeforeBraces: Custom
  2. // BraceWrapping.AfterExternBlock: true
  3. // IndentExternBlock: AfterExternBlock
  4. extern "C"
  5. {
  6. void foo();
  7. }
  8. // BreakBeforeBraces: Custom
  9. // BraceWrapping.AfterExternBlock: false
  10. // IndentExternBlock: AfterExternBlock
  11. extern "C" {
  12. void foo();
  13. }
  14. // IndentExternBlock: NoIndent
  15. extern "C"
  16. {
  17. void foo();
  18. }
  19. // IndentExternBlock: Indent
  20. extern "C"
  21. {
  22. void foo();
  23. }

4.65 IndentGotoLabels

当IndentGotoLabels为false时,goto的标签会靠左放置

  1. // IndentGotoLabels: false
  2. int foo()
  3. {
  4. if (foo())
  5. {
  6. label1:
  7. bar();
  8. }
  9. label2:
  10. return 1;
  11. }
  12. // IndentGotoLabels: true
  13. int foo()
  14. {
  15. if (foo())
  16. {
  17. label1:
  18. bar();
  19. }
  20. label2:
  21. return 1;
  22. }

4.66 IndentPPDirectives

控制与处理器的缩进格式

  1. // IndentPPDirectives: None
  2. #if FOO
  3. #if BAR
  4. #include <foo>
  5. #endif
  6. #endif
  7. // IndentPPDirectives: AfterHash
  8. #if FOO
  9. # if BAR
  10. # include <foo>
  11. # endif
  12. #endif
  13. // IndentPPDirectives: BeforeHash
  14. #if FOO
  15. #if BAR
  16. #include <foo>
  17. #endif
  18. #endif

4.67 IndentRequiresClause

是否缩进模版中的requires子句,需要设置RequiresClausePosition为OwnLine或者WithFollowing

  1. // IndentRequiresClause: false
  2. template <typename It>
  3. requires Iterator<It>
  4. void sort(It begin, It end)
  5. {
  6. //....
  7. }
  8. // IndentRequiresClause: true
  9. template <typename It>
  10. requires Iterator<It>
  11. void sort(It begin, It end)
  12. {
  13. //....
  14. }

4.68 IndentWidth

缩进宽度

4.69 IndentWrappedFunctionNames

是否缩进函数名

  1. // IndentWrappedFunctionNames: false
  2. LoooooooooooooooooooooooooooooooooooooooongReturnType
  3. LoooooooooooooooooooooooooooooooongFunctionDeclaration(int a, int b);
  4. // IndentWrappedFunctionNames: true
  5. LoooooooooooooooooooooooooooooooooooooooongReturnType
  6. LoooooooooooooooooooooooooooooooongFunctionDeclaration(int a, int b);

4.70 InsertBraces

是否添加大括号。谨慎使用,可能会生成错误的代码

  1. // InsertBraces: false
  2. if (1)
  3. foo1();
  4. else if (2)
  5. foo2();
  6. else
  7. foo3();
  8. // InsertBraces: true
  9. if (1)
  10. {
  11. foo1();
  12. }
  13. else if (2)
  14. {
  15. foo2();
  16. }
  17. else
  18. {
  19. foo3();
  20. }

4.71 InsertNewlineAtEOF

是否在文件末尾添加一个空行

4.72 IntegerLiteralSeparator

对整数文本添加分隔符,值为0表示保持文本原样,值为负数表示去掉所有的分隔符,值为正数表示分隔符的间隔数

  1. IntegerLiteralSeparator:
  2. Binary: 0
  3. BinaryMinDigits: 0
  4. Decimal: 3
  5. DecimalMinDigits: 5
  6. Hex: -1
  7. HexMinDigits: 0

DecimalMinDigits表示十进制数至少是5位数才添加分隔符

  1. a = 0b10011'11'0110'1;
  2. b = 4592;
  3. c = 18'446'744'073'709'550'592;
  4. d = 0xDEADBEEFDEADBEEFuz;

4.73 KeepEmptyLinesAtTheStartOfBlocks

是否保留代码块第一行的空行

  1. // KeepEmptyLinesAtTheStartOfBlocks: true
  2. if (0)
  3. {
  4. dosomething();
  5. }
  6. // KeepEmptyLinesAtTheStartOfBlocks: false
  7. if (0)
  8. {
  9. dosomething();
  10. }

4.74 KeepEmptyLinesAtEOF

是否保留文件结尾的空行

4.75 LambdaBodyIndentation

lambda函数体的缩进方式

  1. // LambdaBodyIndentation: Signature
  2. someMethod(
  3. [](int a)
  4. {
  5. dosomething();
  6. return;
  7. });
  8. // LambdaBodyIndentation: OuterScope
  9. someMethod(
  10. [](int a)
  11. {
  12. dosomething();
  13. return;
  14. });

4.76 LineEnding

代码行的结束方式,可以选择LE_LF、LE_CRLF、LE_DeriveLF、LE_DeriveCRLF等

4.78 MacroBlockBegin,MacroBlockEnd

MacroBlockBegin和MacroBlockEnd之间的代码会被缩进,MacroBlockBegin和MacroBlockEnd是正则表达式

  1. // MacroBlockBegin: 'M_BEGIN'
  2. // MacroBlockEnd: 'M_END'
  3. M_BEGIN
  4. int a = 0;
  5. double b = 0;
  6. M_END

4.79 MaxEmptyLinesToKeep

设置最大的连续空行

  1. // MaxEmptyLinesToKeep: 3
  2. int func()
  3. {
  4. int a = 1;
  5. int b = 1;
  6. int c = 1;
  7. return 0;
  8. }
  9. // MaxEmptyLinesToKeep: 1
  10. int func()
  11. {
  12. int a = 1;
  13. int b = 1;
  14. int c = 1;
  15. return 0;
  16. }

4.80 NamespaceIndentation

命名空间的缩进方式

  1. // NamespaceIndentation: None
  2. namespace out
  3. {
  4. int i;
  5. namespace in
  6. {
  7. int i;
  8. }
  9. } // namespace out
  10. // NamespaceIndentation: Inner
  11. namespace out
  12. {
  13. int i;
  14. namespace in
  15. {
  16. int i;
  17. }
  18. } // namespace out
  19. // NamespaceIndentation: All
  20. namespace out
  21. {
  22. int i;
  23. namespace in
  24. {
  25. int i;
  26. }
  27. } // namespace out

4.81 PackConstructorInitializers

构造函数初始化使用的格式

  1. // PackConstructorInitializers: Never
  2. // 总是每个初始化单独放一行
  3. struct MyClass
  4. {
  5. MyClass():
  6. m_a(0),
  7. m_b(0),
  8. m_c(0)
  9. {
  10. }
  11. };
  12. // PackConstructorInitializers: BinPack
  13. // 尝试将构造函数初始化列表紧密地放在一行上,以减少行数
  14. struct MyClass
  15. {
  16. MyClass():
  17. aaaaaaaaaaaaaaaaaaaa(), bbbbbbbbbbbbbbbbbbbb(), cccccccccccccccccccc()
  18. {
  19. }
  20. };
  21. // PackConstructorInitializers: CurrentLine
  22. // 如果可以放在一行就全部放在一行,否则就每个初始化各一行
  23. struct MyClass
  24. {
  25. MyClass(): aaaaaaaaaaaaaaaaa(), bbbbbbbbbbbbbbbbb(), ccccccccccccccccc() { }
  26. };
  27. struct MyClass
  28. {
  29. MyClass():
  30. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(),
  31. bbbbbbbbb(),
  32. ccccc()
  33. {
  34. }
  35. };
  36. // PackConstructorInitializers: NextLine
  37. // 和CurrentLine相同,不过如果当前行放不下,尽量放在下一行,否则就单独一行
  38. struct MyClass
  39. {
  40. MyClass(): aaaaaaaaaaaaaaaaaaaa(), bbbbbbbbbbbbbbbbbbbb(), ddddddddddddd()
  41. {
  42. }
  43. };
  44. struct MyClass
  45. {
  46. MyClass():
  47. aaaaaaaaaaaaaaaaa(), bbbbbbbbbbbbbbbbb(), cccccccccccccccccccccc()
  48. {
  49. }
  50. };
  51. struct MyClass
  52. {
  53. MyClass():
  54. aaaaaaaaaaaaaaaaa(),
  55. bbbbbbbbbbbbbbbbb(),
  56. ccccccccccccccccccccccccccccccc()
  57. {
  58. }
  59. };
  60. // PackConstructorInitializers: NextLineOnly
  61. // 如果下一行放不下,就单独放一行
  62. struct MyClass
  63. {
  64. MyClass():
  65. aaaaaaaaaaaaaaaaaaaa(), bbbbbbbbbbbbbbbbbbbb(), ddddddddddddd()
  66. {
  67. }
  68. };
  69. struct MyClass
  70. {
  71. MyClass():
  72. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(),
  73. bbbbbbbbbbbbbbbbbbbb(),
  74. ddddddddddddd()
  75. {
  76. }
  77. };

4.82 PenaltyBreakAssignment

4.83 PenaltyBreakBeforeFirstCallParameter

4.84 PenaltyBreakComment

4.85 PenaltyBreakFirstLessLess

4.86 PenaltyBreakOpenParenthesis

4.87 PenaltyBreakString

4.88 PenaltyBreakTemplateDeclaration

4.89 PenaltyExcessCharacter

4.90 PenaltyIndentedWhitespace

4.91 PenaltyReturnTypeOnItsOwnLine

4.92 PointerAlignment

指针和引用的对齐风格

  1. // 设置DerivePointerAlignment为false
  2. // PointerAlignment: Left
  3. int* a = nullptr;
  4. int& b = *a;
  5. int*& c = a;
  6. // PointerAlignment: Right
  7. int *a = nullptr;
  8. int &b = *a;
  9. int *&c = a;
  10. // PointerAlignment: Middle
  11. int * a = nullptr;
  12. int & b = *a;
  13. int *& c = a;

4.93 PPIndentWidth

预处理器的缩进宽度,为-1时和IndentWidth保持一致

  1. // PPIndentWidth: -1
  2. #ifdef __linux__
  3. #define FOO
  4. #else
  5. #define BAR
  6. #endif
  7. // PPIndentWidth: 10
  8. #ifdef __linux__
  9. #define FOO
  10. #else
  11. #define BAR
  12. #endif

4.94 QualifierAlignment

说明符和限定符的排列方式,最好设置为Leave,否则可能生成错误的代码

  1. // QualifierAlignment: Leave
  2. int const a;
  3. const int* a;
  4. // QualifierAlignment: Left
  5. const int a;
  6. const int* a;
  7. // QualifierAlignment: Right
  8. int const a;
  9. int const* a;
  10. // QualifierAlignment: Custom
  11. // 使用QualifierOrder的定义

4.95 RawStringFormats

设置原始字符串的格式与特定语言的风格指南保持一致

  1. // 设置原始字符串的格式与为基于google的c++风格
  2. RawStringFormats:
  3. - Language: Cpp
  4. Delimiters:
  5. - cc
  6. - CC
  7. - cpp
  8. - Cpp
  9. - CPP
  10. - 'c++'
  11. - 'C++'
  12. CanonicalDelimiter: ''
  13. BasedOnStyle: google

4.96 ReferenceAlignment

引用的对齐方式,值为Pointer时,和指针的对齐方式相同,还可以设置为Left,Right,Middle

4.97 ReflowComments

注释过长时,是否重新排列注释

  1. // ReflowComments: false
  2. // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information
  3. /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */
  4. // ReflowComments: true
  5. // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
  6. // information
  7. /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of
  8. * information */

4.98 RemoveBracesLLVM

根据llvm的风格决定是否去掉不必要的大括号,最好设置为false,否则可能会生成错误的代码

4.99 RemoveParentheses

删除多余的括号,最好设置为Leave,否则可能会生成错误的代码

4.100 RemoveSemicolon

删除非空函数的右大括号后面的分号,最好设置为false,否则可能会生成错误的代码

4.101 RequiresClausePosition

设置requires子句的位置

  1. // RequiresClausePosition: OwnLine
  2. template <typename T>
  3. requires C<T>
  4. struct Foo
  5. {
  6. };
  7. template <typename T>
  8. requires C<T>
  9. void bar(T t)
  10. {
  11. }
  12. template <typename T>
  13. void baz(T t)
  14. requires C<T>
  15. {
  16. }
  17. // RequiresClausePosition: WithPreceding
  18. template <typename T>
  19. requires C<T>
  20. struct Foo
  21. {
  22. };
  23. template <typename T>
  24. requires C<T>
  25. void bar(T t)
  26. {
  27. }
  28. template <typename T>
  29. void baz(T t) requires C<T>
  30. {
  31. }
  32. // RequiresClausePosition: WithFollowing
  33. template <typename T>
  34. requires C<T> struct Foo
  35. {
  36. };
  37. template <typename T>
  38. requires C<T> void bar(T t)
  39. {
  40. }
  41. template <typename T>
  42. void baz(T t)
  43. requires C<T>
  44. {
  45. }
  46. // RequiresClausePosition: SingleLine
  47. template <typename T>
  48. requires C<T> struct Foo
  49. {
  50. };
  51. template <typename T>
  52. requires C<T> void bar(T t)
  53. {
  54. }
  55. template <typename T>
  56. void baz(T t) requires C<T>
  57. {
  58. }

4.102 RequiresExpressionIndentation

用于控制require表达式的缩进

  1. // RequiresExpressionIndentation: OuterScope
  2. template <typename T>
  3. concept C = requires(T t) {
  4. ...;
  5. ...;
  6. }
  7. // RequiresExpressionIndentation: Keyword
  8. template <typename T>
  9. concept C = requires(T t) {
  10. ...;
  11. ...;
  12. }

4.103 SeparateDefinitionBlocks

是否使用空行分隔定义块,包括class、struct、enum、函数等

  1. // SeparateDefinitionBlocks: Leave
  2. #include <iostream>
  3. class MyClass
  4. {
  5. };
  6. struct MyStruct
  7. {
  8. };
  9. enum class MyEnum
  10. {
  11. };
  12. void Func() {}
  13. void Func2() {}
  14. // SeparateDefinitionBlocks: Always
  15. #include <iostream>
  16. class MyClass
  17. {
  18. };
  19. struct MyStruct
  20. {
  21. };
  22. enum class MyEnum
  23. {
  24. };
  25. void Func() {}
  26. void Func2() {}
  27. // SeparateDefinitionBlocks: Never
  28. #include <iostream>
  29. class MyClass
  30. {
  31. };
  32. struct MyStruct
  33. {
  34. };
  35. enum class MyEnum
  36. {
  37. };
  38. void Func() {}
  39. void Func2() {}

4.104 ShortNamespaceLines

当命令空间中的代码行数小于设定的值时,这类命令空间称为短命名空间。短命名空间可以不受FixNamespaceComments字段的影响

  1. // FixNamespaceComments: true
  2. // ShortNamespaceLines: 2
  3. namespace A
  4. {
  5. int a;
  6. int b;
  7. int c;
  8. } // namespace A
  9. namespace B
  10. {
  11. int a;
  12. }
  13. // ShortNamespaceLines: 0
  14. namespace A
  15. {
  16. int a;
  17. int b;
  18. int c;
  19. } // namespace A
  20. namespace B
  21. {
  22. int a;
  23. } // namespace B

4.105 SortIncludes

控制排序头文件的方式,最高设置为Never,c++中的头文件顺序可能对代码有影响

  1. // SortIncludes: Never
  2. #include "B/A.h"
  3. #include "A/B.h"
  4. #include "a/b.h"
  5. #include "A/b.h"
  6. #include "B/a.h"
  7. // SortIncludes: CaseSensitive
  8. #include "A/B.h"
  9. #include "A/b.h"
  10. #include "B/A.h"
  11. #include "B/a.h"
  12. #include "a/b.h"
  13. // SortIncludes: SI_CaseInsensitive
  14. #include "A/B.h"
  15. #include "A/b.h"
  16. #include "a/b.h"
  17. #include "B/A.h"
  18. #include "B/a.h"

4.106 SortUsingDeclarations

控制排序using声明的方式

  1. // SortUsingDeclarations: Never
  2. using std::chrono::duration_cast;
  3. using std::move;
  4. using boost::regex;
  5. using boost::regex_constants::icase;
  6. using std::string;
  7. // SortUsingDeclarations: Lexicographic
  8. using boost::regex;
  9. using boost::regex_constants::icase;
  10. using std::chrono::duration_cast;
  11. using std::move;
  12. using std::string;
  13. // SortUsingDeclarations: LexicographicNumeric
  14. using boost::regex;
  15. using boost::regex_constants::icase;
  16. using std::move;
  17. using std::string;
  18. using std::chrono::duration_cast;

4.107 SpaceAfterCStyleCast

c风格的强制转换是否添加空格

  1. // SpaceAfterCStyleCast: false
  2. (int)i;
  3. // SpaceAfterCStyleCast: true
  4. (int) i;

4.108 SpaceAfterLogicalNot

是否在逻辑非运算符号(!)后面添加一个空格

  1. // SpaceAfterLogicalNot: false
  2. if (!true)
  3. {
  4. dosomething();
  5. }
  6. // SpaceAfterLogicalNot: true
  7. if (! true)
  8. {
  9. dosomething();
  10. }

4.109 SpaceAfterTemplateKeyword

template关键字后面是否加空格

  1. // SpaceAfterTemplateKeyword: false
  2. template<class T>
  3. void Func(T a)
  4. {
  5. }
  6. // SpaceAfterTemplateKeyword: true
  7. template <class T>
  8. void Func(T a)
  9. {
  10. }

4.110 SpaceAroundPointerQualifiers

指针限定符附近是否放置空格

  1. // 设置PointerAlignment为Left
  2. // SpaceAroundPointerQualifiers: Default
  3. void* const* x = NULL;
  4. // SpaceAroundPointerQualifiers: Before
  5. void* const* x = NULL;
  6. // SpaceAroundPointerQualifiers: After
  7. void* const * x = NULL;
  8. // SpaceAroundPointerQualifiers: Both
  9. void* const * x = NULL;
  10. // 设置PointerAlignment为Right
  11. // SpaceAroundPointerQualifiers: Default
  12. void *const *x = NULL;
  13. // SpaceAroundPointerQualifiers: Before
  14. void * const *x = NULL;
  15. // SpaceAroundPointerQualifiers: After
  16. void *const *x = NULL;
  17. // SpaceAroundPointerQualifiers: Both
  18. void * const *x = NULL;

4.111 SpaceBeforeAssignmentOperators

赋值符号的左边是否添加空格

  1. // SpaceBeforeAssignmentOperators: false
  2. int a= 5;
  3. a+= 42;
  4. // SpaceBeforeAssignmentOperators: true
  5. int a = 5;
  6. a += 42;

4.112 SpaceBeforeCaseColon

是否添加case冒号之前的空格

  1. // SpaceBeforeCaseColon: false
  2. switch (a)
  3. {
  4. case 1:
  5. break;
  6. case 2:
  7. break;
  8. default:
  9. break;
  10. }
  11. // SpaceBeforeCaseColon: true
  12. switch (a)
  13. {
  14. case 1 :
  15. break;
  16. case 2 :
  17. break;
  18. default :
  19. break;
  20. }

4.113 SpaceBeforeCpp11BracedList

设置为true时,使用c++11的包围列表初始化的大括号左边会添加一个空格

  1. // SpaceBeforeCpp11BracedList: false
  2. std::vector<int> a{1, 2, 3};
  3. // SpaceBeforeCpp11BracedList: true
  4. std::vector<int> a {1, 2, 3};

4.114 SpaceBeforeCtorInitializerColon

是否添加构造函数中初始化列表的冒号前的空格

  1. // SpaceBeforeCtorInitializerColon: false
  2. struct MyClass
  3. {
  4. MyClass(): m_a(0), m_b(0) {}
  5. int m_a;
  6. int m_b;
  7. };
  8. // SpaceBeforeCtorInitializerColon: true
  9. struct MyClass
  10. {
  11. MyClass() : m_a(0), m_b(0) {}
  12. int m_a;
  13. int m_b;
  14. };

4.115 SpaceBeforeInheritanceColon

是否添加继承的冒号前的空格

  1. // SpaceBeforeInheritanceColon: false
  2. class Foo: Bar
  3. {
  4. }
  5. // SpaceBeforeInheritanceColon: true
  6. class Foo : Bar
  7. {
  8. }

4.116 SpaceBeforeParens

定义是否添加圆括号前面的空格。当设置为Custom时,使用SpaceBeforeParensOptions字段的定义

  1. // SpaceBeforeParens: Never
  2. void foo()
  3. {
  4. dosomething();
  5. dosomething();
  6. }
  7. if(0)
  8. {
  9. }
  10. func1();
  11. func2(a, b);
  12. std::for_each(vec.begin(), vec.end(), [](int) {});
  13. // SpaceBeforeParens: ControlStatements
  14. void foo()
  15. {
  16. dosomething();
  17. dosomething();
  18. }
  19. if (0)
  20. {
  21. }
  22. func1();
  23. func2(a, b);
  24. std::for_each(vec.begin(), vec.end(), [](int) {});
  25. // SpaceBeforeParens: ControlStatementsExceptControlMacros
  26. void foo()
  27. {
  28. dosomething();
  29. dosomething();
  30. }
  31. if (0)
  32. {
  33. }
  34. func1();
  35. func2(a, b);
  36. std::for_each(vec.begin(), vec.end(), [](int) {});
  37. // SpaceBeforeParens: NonEmptyParentheses
  38. void foo()
  39. {
  40. dosomething();
  41. dosomething();
  42. }
  43. if (0)
  44. {
  45. }
  46. func1();
  47. func2 (a, b);
  48. std::for_each (vec.begin(), vec.end(), [] (int) {});
  49. // SpaceBeforeParens: Always
  50. void foo ()
  51. {
  52. dosomething ();
  53. dosomething ();
  54. }
  55. if (0)
  56. {
  57. }
  58. func1 ();
  59. func2 (a, b);
  60. std::for_each (vec.begin (), vec.end (), [] (int) {});

4.117 SpaceBeforeParensOptions

定义是否添加圆括号前面的空格,需要设置SpaceBeforeParens为Custom

AfterControlStatements

  1. // AfterControlStatements: false
  2. if(0)
  3. {
  4. }
  5. // AfterControlStatements: true
  6. if (0)
  7. {
  8. }

AfterForeachMacros

没有生效,不知道原因

AfterFunctionDefinitionName

  1. // AfterFunctionDefinitionName: false
  2. void Func()
  3. {
  4. dosomething();
  5. dosomething();
  6. }
  7. void Func(int a, int b)
  8. {
  9. dosomething();
  10. dosomething();
  11. }
  12. // AfterFunctionDefinitionName: true
  13. void Func ()
  14. {
  15. dosomething();
  16. dosomething();
  17. }
  18. void Func (int a, int b)
  19. {
  20. dosomething();
  21. dosomething();
  22. }

AfterFunctionDeclarationName

  1. // AfterFunctionDeclarationName: false
  2. void Func();
  3. void Func(int a, int b);
  4. // AfterFunctionDeclarationName: true
  5. void Func ();
  6. void Func (int a, int b);

AfterIfMacros

没有生效,不知道原因

AfterOverloadedOperator

  1. // AfterOverloadedOperator: false
  2. void operator++(int a);
  3. // AfterOverloadedOperator: true
  4. void operator++ (int a);

AfterRequiresInClause

  1. // AfterRequiresInClause: false
  2. template <typename T>
  3. requires(A<T> && B<T>)
  4. {
  5. }
  6. // AfterRequiresInClause: true
  7. template <typename T>
  8. requires (A<T> && B<T>)
  9. {
  10. }

AfterRequiresInExpression

  1. // AfterRequiresInExpression: false
  2. template <typename T>
  3. concept C = requires(T t) {
  4. ...;
  5. ...;
  6. }
  7. // AfterRequiresInExpression: true
  8. concept C = requires (T t) {
  9. ...;
  10. ...;
  11. }

BeforeNonEmptyParentheses

  1. // BeforeNonEmptyParentheses: false
  2. void Func();
  3. void Func(int a, int b);
  4. // BeforeNonEmptyParentheses: true
  5. void Func();
  6. void Func (int a, int b);

4.118 SpaceBeforeRangeBasedForLoopColon

设置基于范围的for循环冒号左边是否添加空格

  1. // SpaceBeforeRangeBasedForLoopColon: false
  2. for (auto a: vec)
  3. {
  4. }
  5. // SpaceBeforeRangeBasedForLoopColon: true
  6. for (auto a : vec)
  7. {
  8. }

4.119 SpaceBeforeSquareBrackets

控制方括号[]前是否添加括号,lambda函数的方括号不受影响

  1. // SpaceBeforeSquareBrackets: false
  2. int a[5][5];
  3. // SpaceBeforeSquareBrackets: true
  4. int a [5][5];

4.120 SpaceInEmptyBlock

空代码块是否添加空格

  1. // SpaceInEmptyBlock: false
  2. void f() {}
  3. // SpaceInEmptyBlock: true
  4. void f() { }

4.121 SpacesBeforeTrailingComments

尾部注释的空格数量

  1. // SpacesBeforeTrailingComments: 4
  2. int a; // comment
  3. int b; // comment
  4. int c; /* comment */

4.122 SpacesInAngles

模版参数列表是否添加空格

  1. // SpacesInAngles: Never
  2. std::function<void(int)> fct;
  3. // SpacesInAngles: Always
  4. std::function< void(int) > fct;
  5. // SpacesInAngles: Leave
  6. // 如果有一个空格,则保持不变
  7. std::function< void(int)> fct;

4.123 SpacesInLineCommentPrefix

控制单行注释的空格数量,有Minimum和Maximum两个选项,Maximum可以设置为-1来表示禁用最大值

  1. // SpacesInLineCommentPrefix:
  2. // Minimum: 2
  3. // Maximum: 5
  4. // comment1
  5. // comment2
  6. // comment3
  7. // comment4

4.124 SpacesInParens

定义左括号后和右括号前是否有空格,可以定义为Never或者Custom,如果定义为Custom,那么使用SpacesInParensOptions的定义

  1. // SpacesInParens: Never
  2. void foo()
  3. {
  4. if (true && 1)
  5. {
  6. f();
  7. }
  8. }

4.125 SpacesInParensOptions

控制左括号后和右括号前是否有空格,需要定义SpacesInParens为Custom

InCStyleCasts: C风格强转是否有空格

  1. // InCStyleCasts: false
  2. x = (int)y;
  3. // InCStyleCasts: true
  4. x = ( int )y;

InConditionalStatements: 条件语句(for/if/while/switch...)是否有空格

  1. // InConditionalStatements: false
  2. if (a)
  3. {
  4. dosomething();
  5. }
  6. while (i < 5)
  7. {
  8. dosomething();
  9. }
  10. // InConditionalStatements: true
  11. if ( a )
  12. {
  13. dosomething();
  14. }
  15. while ( i < 5 )
  16. {
  17. dosomething();
  18. }

InEmptyParentheses: 空括号是否有空格

  1. // InEmptyParentheses: false
  2. void foo()
  3. {
  4. if (true)
  5. {
  6. f();
  7. }
  8. }
  9. // InEmptyParentheses: true
  10. void foo( )
  11. {
  12. if (true)
  13. {
  14. f( );
  15. }
  16. }

Other: 除去上述选项的其他情况是否添加空格

  1. // Other: false
  2. void Func(int a, int b);
  3. // Other: true
  4. void Func( int a, int b );

4.126 SpacesInSquareBrackets

方括号内是否添加空格,没有参数的lambda函数和没有指定大小的数组不受影响

  1. // SpacesInSquareBrackets: false
  2. int a[5];
  3. int b[];
  4. auto func1 = []() { return 1 };
  5. auto func1 = [&]() { return 1 };
  6. // SpacesInSquareBrackets: true
  7. int a[ 5 ];
  8. int b[];
  9. auto func1 = []() { return 1 };
  10. auto func1 = [ & ]() { return 1 };

4.127 Standard

解析并格式化与此标准兼容的c++格式

  1. // Standard: c++03
  2. std::vector<std::set<int> > x;
  3. // Latest
  4. std::vector<std::set<int>> x;

4.128 StatementAttributeLikeMacros

忽略语句前面的宏,把它们看作一个属性

4.129 StatementMacros

 把定义的宏解释为一个完整的语句

4.130 TabWidth

指定制表符的宽度

4.131 UseTab

是否使用制表符,可选Never、ForIndentation、ForContinuationAndIndentation、AlignWithSpaces、Always等

4.132 WhitespaceSensitiveMacros

指定对空白字符敏感的宏

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/239929
推荐阅读
  

闽ICP备14008679号