Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cpp] 使用 clang-format 格式化代码 #53

Open
yangruihan opened this issue Feb 7, 2022 · 0 comments
Open

[Cpp] 使用 clang-format 格式化代码 #53

yangruihan opened this issue Feb 7, 2022 · 0 comments

Comments

@yangruihan
Copy link
Owner

使用 clang-format 格式化代码

一 创建配置文件

在工程下创建名为.clang-format文件,在其中进行配置

配置选项参考:https://clang.llvm.org/docs/ClangFormatStyleOptions.html

二 格式化源代码

可以通过如下命令快速格式化工程中所有源码

cd project # .clang-format 位于此
fd . 'xxx' -e h 'xxx' -e hpp 'xxx' -e cpp | xargs clang-format -i # 其中xxx替换成想要格式化的目录

注:此命令依赖fd

三 附录

3.1 参考配置

BasedOnStyle: llvm
---
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Right
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveBitFields: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignConsecutiveMacros: Consecutive
AlignEscapedNewlines: Left
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortBlocksOnASingleLine: Empty
AllowShortEnumsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: true
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: true
ColumnLimit: 80
DerivePointerAlignment: false
IncludeCategories:
  - Regex: '<[[:alnum:]_]+>'
    Priority: 1
  - Regex: '<(gtest|gmock)/'
    Priority: 2
  - Regex: '<[[:alnum:]_.]+/[[:alnum:]_.]+>'
    Priority: 3
  - Regex: '<[[:alnum:]_.]+/[[:alnum:]_.]+/[[:alnum:]_./]+>'
    Priority: 4
  - Regex: '.*'
    Priority: 5
IndentPPDirectives: AfterHash
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
PointerAlignment: Right
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: false
SpaceAroundPointerQualifiers: After
SpaceBeforeCaseColon: false
SpaceBeforeCtorInitializerColon: false
SpaceBeforeInheritanceColon: false
SpaceBeforeParens: Never
SpaceBeforeRangeBasedForLoopColon: false
Standard: Latest
TabWidth: 4
UseTab: Never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant