Qt 配置Clang Format步驟
配置環境:Windows10 64bit + Qt5.12.8
1 啟動Beautifier插件
菜單>幫助>關於插件
勾選后,重啟Qt Creator
2 安裝LVMM
主頁:
根據自己的情況,選擇下載相應的系統版本
下載完畢后,安裝,在安裝時,需勾選添加環境變量,便於后續操作。根據自己的情況選擇安裝路
徑,然后一路下一步。
由於根據系統環境,選擇安裝了64位的LLVM,所以可能lldb.exe報找不到python36.dll,安裝完
LLVM后,bin文件夾下沒有python36.dll,所以需要下載對應版本的庫,拷貝到D:\LLVM\bin(根據
自己安裝的目錄)
3 Qt中啟用Clang Format
菜單>工具>選項
勾選第一項之后,在保存時會自動格式化當前的文件
4 編寫自定義規則
創建了自定義的規則之后,規則文件在,其中dkwc++-baseon-google時我配置的名字:
C:\Users\Admin\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat\dkwc++-
baseon-google
也可以直接使用編輯器進行編輯詞文件
5 設置快捷鍵
可以設置為Ctrl+Alt+K,表示,在當前文件下按下這個快捷鍵,即可格式化當前的文件
6 參考的自定義規則
詳細規則參考官方說明文檔:
根據我們的編碼規范,我在Google(工具已經內置的風格)的基礎上,進行了調整。后續發現有遺漏
的可以一起來完善。
1 BasedOnStyle: Google
2
3 IndentWidth: 4
4 ColumnLimit: 80
5 AccessModifierOffset: -4
6 MaxEmptyLinesToKeep: 1
7 SpacesBeforeTrailingComments: 4
8
9 AllowAllArgumentsOnNextLine: true
10 AllowAllParametersOfDeclarationOnNextLine: false
11 AllowAllConstructorInitializersOnNextLine: false
12 AllowShortFunctionsOnASingleLine: false
13 AllowShortIfStatementsOnASingleLine: false
14 AllowShortBlocksOnASingleLine: false
15 AllowShortCaseLabelsOnASingleLine: false
16 AllowShortEnumsOnASingleLine: false
https://clang.llvm.org/docs/ClangFormatStyleOptions.html
17 AllowShortLambdasOnASingleLine: false
18 AllowShortLoopsOnASingleLine: false
19
20 AlignAfterOpenBracket: true
21 AlignTrailingComments: true
22 AlignEscapedNewlinesLeft: true
23 AlignOperands: true
24 AlignConsecutiveAssignments: true
25 AlignConsecutiveMacros: true
26 AlignEscapedNewlines: true
27
28 AlwaysBreakBeforeMultilineStrings: false
29
30 #BinPackArguments: true
31 #BinPackParameters: true
32
33 # space around ":"
34 BitFieldColonSpacing: Both
35
36 #BreakBeforeBraces: Allman
37 # if need custom brace wrapping
38 BreakBeforeBraces: Custom
39 BraceWrapping:
40 AfterControlStatement: Always
41 AfterCaseLabel: true
42 AfterClass: true
43 AfterFunction: true
44 AfterEnum: true
45 AfterStruct: true
46 AfterNamespace: true
47 AfterUnion: true
48 AfterExternBlock: true
49 BeforeElse: true
50 BeforeLambdaBody: true
51 BeforeCatch: true
52 BeforeWhile: true
53 SplitEmptyFunction: true
54 SplitEmptyRecord: true
55 SplitEmptyNamespace: true
56 # end of custom brace wrapping
57
58 BreakBeforeBinaryOperators: true
59 BreakBeforeTernaryOperators: true
60 BreakConstructorInitializers: AfterColon
61 #BreakInheritanceList: AfterColon
62 BreakStringLiterals: true
63
64 CompactNamespaces: false
65
66 ConstructorInitializerAllOnOneLineOrOnePerLine: false
67
68 #IndentPPDirectives: AfterHash
69
70 # true
71 # bool
72 # LooooooooooooooooooooooooooooooooooooooooooooooooooooongFunctionDeclaration();
73 # false
74 # bool
75 # LooooooooooooooooooooooooooooooooooooooooooooooooooooongFunctionDeclaration();
76 IndentWrappedFunctionNames: false
77
78 NamespaceIndentation: Inner
79
80 PointerAlignment: Left
81
82 SpaceAfterCStyleCast: false
83 SpaceAfterLogicalNot: false
84 SpaceAfterTemplateKeyword: false
85 SpaceBeforeSquareBrackets: false
86 SpaceInEmptyBlock: false
87 SpaceInEmptyParentheses: false
88 SpacesInParentheses: false
89 SpacesInSquareBrackets: false
90
91 SpaceBeforeAssignmentOperators: true
92 SpaceBeforeRangeBasedForLoopColon: true
93 SpacesInContainerLiterals: true
特殊情況
1 某些代碼塊不使用格式化
如果某個特殊的寫法具有特定的含義,不需要進行格式化的化,可以使用如下方法
1 // clang-format off
2 不會被格式化的代碼塊
3 // clang-format on
參考連接
1. google編碼規范
中文翻譯:
2. LLVM
主頁:
下載相應的系統版本
3. Artistic Style
主頁:
4. 相關博客
Clang-format 配置文件
Qt Creator使用clang-format格式化代碼
Qt高級——QtCreator代碼格式化
https://zh-google-styleguide.readthedocs.io/en/latest/contents/
https://releases.llvm.org/
http://astyle.sourceforge.net/
https://blog.csdn.net/deeplee021/article/details/100877960
https://blog.csdn.net/m0_37194132/article/details/85134229
https://blog.51cto.com/9291927/2112402
Qt 配置Clang Format步驟
配置環境:Windows10 64bit + Qt5.12.8
1 啟動Beautifier插件
菜單>幫助>關於插件
勾選后,重啟Qt Creator
2 安裝LVMM
主頁:
根據自己的情況,選擇下載相應的系統版本
下載完畢后,安裝,在安裝時,需勾選添加環境變量,便於后續操作。根據自己的情況選擇安裝路
徑,然后一路下一步。
由於根據系統環境,選擇安裝了64位的LLVM,所以可能lldb.exe報找不到python36.dll,安裝完
LLVM后,bin文件夾下沒有python36.dll,所以需要下載對應版本的庫,拷貝到D:\LLVM\bin(根據
自己安裝的目錄)
3 Qt中啟用Clang Format
菜單>工具>選項
勾選第一項之后,在保存時會自動格式化當前的文件
4 編寫自定義規則
創建了自定義的規則之后,規則文件在,其中dkwc++-baseon-google時我配置的名字:
C:\Users\Admin\AppData\Roaming\QtProject\qtcreator\beautifier\clangformat\dkwc++-
baseon-google
也可以直接使用編輯器進行編輯詞文件
5 設置快捷鍵
可以設置為Ctrl+Alt+K,表示,在當前文件下按下這個快捷鍵,即可格式化當前的文件
7 參考的自定義規則
詳細規則參考官方說明文檔:
根據我們的編碼規范,我在Google(工具已經內置的風格)的基礎上,進行了調整。后續發現有遺漏
的可以一起來完善。
1 BasedOnStyle: Google
2
3 IndentWidth: 4
4 ColumnLimit: 80
5 AccessModifierOffset: -4
6 MaxEmptyLinesToKeep: 1
7 SpacesBeforeTrailingComments: 4
8
9 AllowAllArgumentsOnNextLine: true
10 AllowAllParametersOfDeclarationOnNextLine: false
11 AllowAllConstructorInitializersOnNextLine: false
12 AllowShortFunctionsOnASingleLine: false
13 AllowShortIfStatementsOnASingleLine: false
14 AllowShortBlocksOnASingleLine: false
15 AllowShortCaseLabelsOnASingleLine: false
16 AllowShortEnumsOnASingleLine: false
https://clang.llvm.org/docs/ClangFormatStyleOptions.html
17 AllowShortLambdasOnASingleLine: false
18 AllowShortLoopsOnASingleLine: false
19
20 AlignAfterOpenBracket: true
21 AlignTrailingComments: true
22 AlignEscapedNewlinesLeft: true
23 AlignOperands: true
24 AlignConsecutiveAssignments: true
25 AlignConsecutiveMacros: true
26 AlignEscapedNewlines: true
27
28 AlwaysBreakBeforeMultilineStrings: false
29
30 #BinPackArguments: true
31 #BinPackParameters: true
32
33 # space around ":"
34 BitFieldColonSpacing: Both
35
36 #BreakBeforeBraces: Allman
37 # if need custom brace wrapping
38 BreakBeforeBraces: Custom
39 BraceWrapping:
40 AfterControlStatement: Always
41 AfterCaseLabel: true
42 AfterClass: true
43 AfterFunction: true
44 AfterEnum: true
45 AfterStruct: true
46 AfterNamespace: true
47 AfterUnion: true
48 AfterExternBlock: true
49 BeforeElse: true
50 BeforeLambdaBody: true
51 BeforeCatch: true
52 BeforeWhile: true
53 SplitEmptyFunction: true
54 SplitEmptyRecord: true
55 SplitEmptyNamespace: true
56 # end of custom brace wrapping
57
58 BreakBeforeBinaryOperators: true
59 BreakBeforeTernaryOperators: true
60 BreakConstructorInitializers: AfterColon
61 #BreakInheritanceList: AfterColon
62 BreakStringLiterals: true
63
64 CompactNamespaces: false
65
66 ConstructorInitializerAllOnOneLineOrOnePerLine: false
67
68 #IndentPPDirectives: AfterHash
69
70 # true
71 # bool
72 # LooooooooooooooooooooooooooooooooooooooooooooooooooooongFunctionDeclaration();
73 # false
74 # bool
75 # LooooooooooooooooooooooooooooooooooooooooooooooooooooongFunctionDeclaration();
76 IndentWrappedFunctionNames: false
77
78 NamespaceIndentation: Inner
79
80 PointerAlignment: Left
81
82 SpaceAfterCStyleCast: false
83 SpaceAfterLogicalNot: false
84 SpaceAfterTemplateKeyword: false
85 SpaceBeforeSquareBrackets: false
86 SpaceInEmptyBlock: false
87 SpaceInEmptyParentheses: false
88 SpacesInParentheses: false
89 SpacesInSquareBrackets: false
90
91 SpaceBeforeAssignmentOperators: true
92 SpaceBeforeRangeBasedForLoopColon: true
93 SpacesInContainerLiterals: true
特殊情況
1 某些代碼塊不使用格式化
如果某個特殊的寫法具有特定的含義,不需要進行格式化的化,可以使用如下方法
1 // clang-format off
2 不會被格式化的代碼塊
3 // clang-format on
參考連接
1. google編碼規范
中文翻譯:
2. LLVM
主頁:
下載相應的系統版本
3. Artistic Style
主頁:
4. 相關博客
Clang-format 配置文件
Qt Creator使用clang-format格式化代碼
Qt高級——QtCreator代碼格式化
https://zh-google-styleguide.readthedocs.io/en/latest/contents/
https://releases.llvm.org/
http://astyle.sourceforge.net/
https://blog.csdn.net/deeplee021/article/details/100877960
https://blog.csdn.net/m0_37194132/article/details/85134229
https://blog.51cto.com/9291927/2112402