me
me copied to clipboard
学习C++ (Part 6: clang-format)
ClangFormat — Clang 15.0.0git documentation
- clangformat 是一个代码格式化工具,支持C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C#
- macos 通过
brew install clangformat
安装 - 可以通过
clang-format -style=llvm -dump-config > .clang-format
形成一个配置文件 - 设置选项: Clang-Format Style Options
自己搭配了一下:
.clang-format
# 可选套餐: LLVM, google,
BasedOnStyle: LLVM
# 列宽
ColumnLimit: 200
# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行)
AlignAfterOpenBracket: Align
# 连续赋值时,对齐所有等号
AlignConsecutiveAssignments: true
# 连续声明时,对齐所有声明的变量名
AlignConsecutiveDeclarations: true
# 左对齐逃脱换行(使用反斜杠换行)的反斜杠
AlignEscapedNewlinesLeft: true
# 指针的*的挨着哪边
PointerAlignment: Right
# 缩进宽度
IndentWidth: 2
# 是否允许短方法单行
AllowShortFunctionsOnASingleLine: false
# 是否允许短if单行 If true, if (a) return; 可以放到同一行
AllowShortIfStatementsOnASingleLine: false
# 注释对齐
AlignTrailingComments: true
# 小括号两边添加空格
SpacesInParentheses : false
# 多行声明语句按照=对齐
AlignConsecutiveDeclarations: true
# 连续的赋值语句以 = 为中心对齐
AlignConsecutiveAssignments: true
# 等号两边的空格
SpaceBeforeAssignmentOperators: true
# 缩进
IndentWrappedFunctionNames: true
# 在构造函数初始化时按逗号断行,并以冒号对齐
BreakConstructorInitializersBeforeComma: false
# 括号后添加空格
SpaceAfterCStyleCast: true
# tab键盘的宽度
TabWidth: 2
# 采用空格
UseTab: Never
clang-format main.cpp
int main(int argc, int[] argv) {
std::cout << "hello world" << std::endl;
return 0;
}
Qt Creator
- 在plugins中打开beautifier,然后在配置中Clang-fomat选项卡中配置
/opt/homebrew/bin/clang-format
,然后再下面勾选Use customize style,Edit输入上面的配置,Qt Creator不care .clang-format文件。
VSCode
- plugins安装clang-format,然后vscode支持当前目录或者home下的.clang-format
- 如果有需要可以在settings中勾上format on save
- 或者shift-option-F 格式化代码
shell
单一文件: clang-format -i file
直接将格式化后的内容覆盖原文件。
多个文件: find . -type f -name "*.h" -o -iname "*.c" | xargs clang-format -i
obs
- 我一开始想用clang-format obs的代码,发现不行,原因是obs下自带.clang-format
# please use clang-format version 8 or later
Standard: Cpp11
AccessModifierOffset: -8
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
#AllowAllArgumentsOnNextLine: false # requires clang-format 9
#AllowAllConstructorInitializersOnNextLine: false # requires clang-format 9
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: false
#AllowShortLambdasOnASingleLine: Inline # requires clang-format 9
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakStringLiterals: false # apparently unpredictable
ColumnLimit: 80
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
FixNamespaceComments: false
ForEachMacros:
- 'json_object_foreach'
- 'json_object_foreach_safe'
- 'json_array_foreach'
IncludeBlocks: Preserve
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 8
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
#ObjCBinPackProtocolList: Auto # requires clang-format 7
ObjCBlockIndentWidth: 8
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
#SpaceAfterLogicalNot: false # requires clang-format 9
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
#SpaceBeforeCtorInitializerColon: true # requires clang-format 7
#SpaceBeforeInheritanceColon: true # requires clang-format 7
SpaceBeforeParens: ControlStatements
#SpaceBeforeRangeBasedForLoopColon: true # requires clang-format 7
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
#StatementMacros: # requires clang-format 8
# - 'Q_OBJECT'
TabWidth: 8
#TypenameMacros: # requires clang-format 9
# - 'DARRAY'
UseTab: ForContinuationAndIndentation
---
Language: ObjC
参考阅读: