当前位置:网站首页>The most complete format description of clang format

The most complete format description of clang format

2022-06-11 02:21:00 Charles Ren

vscode To configure

stay vscode To configure
 Insert picture description here
Notice the following Clang_format_style It will read from the root directory first .clong-format The configuration file .
If it doesn't read , Will use the above Clang_format_fallback style. Some built-in styles , For example, optional WebKit, Google etc.

So if we want to customize the style implementation , Then we can build a new one .clang_format Put the file in your project root directory .
Specifically .clang_format What styles are defined , Let's explain .

Important part

I'll just pick some common and obvious ones here
AlignAfterOpenBracket
AlignEscapedNewlines
AlignOperands
AlignTrailingComments:
AllowAllArgumentsOnNextLine
AllowAllParametersOfDeclarationOnNextLine
AllowShortBlocksOnASingleLine
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine
BreakBeforeBraces:Linux
BreakBeforeTernaryOperators
BreakConstructorInitializers
BreakStringLiterals: false
ColumnLimit: 80
IncludeBlocks:Merge
IndentPPDirectives: BeforeHash
IndentWidth: 4
InsertBraces : true
PointerAlignment: Right
ReferenceAlignment: Pointer
SortIncludes: CaseSensitive
SortUsingDeclarations: true
SpaceBeforeRangeBasedForLoopColon: true
SpacesBeforeTrailingComments: 3
Standard: Auto
TabWidth: 4

Official documents

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

Custom configuration example

The following sequence follows the official documentation

---
#  Language : None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
Language:        Cpp
BasedOnStyle:  WebKit
#  Access specifier (public、private etc. ) The migration 
AccessModifierOffset: -4
#  Open bracket ( Open parentheses 、 Open angle bracket 、 Square bracket ) Alignment after : Align, DontAlign, AlwaysBreak( Always wrap after opening parentheses )
AlignAfterOpenBracket: Align
#  Align array columns 
AlignArrayOfStructures: None
#  In continuous assignment , Align all equal signs 
AlignConsecutiveAssignments: None
#  Align consecutive bit fields 
AlignConsecutiveBitFields: None
#  Continuous declaration , Align all declared variable names 
AlignConsecutiveDeclarations: None
#  Align successive macro definitions 
AlignConsecutiveMacros: None
#  Escape line break ( Wrap with backslash ) The backslash of 
AlignEscapedNewlines: Left
#  Operand alignment 
AlignOperands:   Align
#  Alignment of trailing notes 
AlignTrailingComments: true
#  Allow function arguments on one line 
AllowAllArgumentsOnNextLine: true
#  Allow all parameters of a function declaration to be placed on one line 
AllowAllParametersOfDeclarationOnNextLine: true
#  Allow short blocks to be placed on the same line 
AllowShortBlocksOnASingleLine: Never
#  Allow short case Put the labels on the same line 
AllowShortCaseLabelsOnASingleLine: false
#  Allow short enumerations to be placed on the same line 
AllowShortEnumsOnASingleLine: false
#  Allow short functions to be placed on the same line 
AllowShortFunctionsOnASingleLine: Empty
#  Allow short if Statements remain on the same line 
AllowShortIfStatementsOnASingleLine: Never
#  Allow short anonymous functions lamda Expressions on the same line 
AllowShortLambdasOnASingleLine: Empty
#  Allow short loops while Keep in the same line 
AllowShortLoopsOnASingleLine: false
#  Always wrap after return type 
AlwaysBreakAfterReturnType: None
#  Always on multiple lines string Wrap before literal 
AlwaysBreakBeforeMultilineStrings: false
#  Always in template Line wrap after declaration 
AlwaysBreakTemplateDeclarations: MultiLine
#  Macro properties 
AttributeMacros: ['__capability', '__output', '__ununsed']
#  Indicates that the function arguments are either on the same line , Or they all go their own way 
BinPackArguments: true
# false Indicates that all formal parameters are either on the same line , Or they all go their own way 
BinPackParameters: true
#  Bit field colon alignment 
BitFieldColonSpacing : Both
#  Curly braces wrap , Only when BreakBeforeBraces Set to Custom Only when effective , Setting other configurations will not take effect 
BraceWrapping:
  AfterCaseLabel:  false
  AfterClass:      false
  AfterControlStatement: Never
  AfterEnum:       false
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  AfterExternBlock: false
  BeforeCatch:     false
  BeforeElse:      false
  BeforeLambdaBody: false
  BeforeWhile:     false
  IndentBraces:    false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
#  stay java The comment of the field is wrapped 
BreakAfterJavaFieldAnnotations: false
#  Line feed before binary operator 
BreakBeforeBinaryOperators: None
#  Wrap before braces 
BreakBeforeBraces: Linux
#  stay concept Wrap before 
BreakBeforeConceptDeclarations: Never
#  Wrap before ternary operator 
BreakBeforeTernaryOperators: true
#  Constructor initializer newline style 
BreakConstructorInitializers: BeforeColon
#  Inherit list style 
BreakInheritanceList: AfterColon
#  String wrap style 
BreakStringLiterals: false
#  Limit of characters per line ,0 There is no limit 
ColumnLimit:     80
#  Regular expressions that describe annotations of special significance , It should not be split into multiple lines or changed in other ways 
CommentPragmas:  '^ IWYU pragma:'
#  Declare each namespace on a new line 
CompactNamespaces: false
#  The indent width of the constructor's initialization list 
ConstructorInitializerIndentWidth: 4
#  The indent width of the continuation line 
ContinuationIndentWidth: 4
#  Remove C++11 List initialization braces { And after } The space before 
Cpp11BracedListStyle: true
#  Inherit the most common line breaks 
DeriveLineEnding: true
#  Inherit the most common alignment of pointers and references 
DerivePointerAlignment: false
#  Turn off formatting 
DisableFormat:   false
#  Delete all empty lines after the access modifier 
EmptyLineAfterAccessModifier: Never
#  Empty lines are added only when the access modifier starts a new logical block 
EmptyLineBeforeAccessModifier: Never
#  Automatically detect whether the function call and definition are formatted as one parameter per line (Experimental)
ExperimentalAutoDetectBinPacking: false
#  Auto supplement namespace notes 
FixNamespaceComments: false
#  Need to be interpreted as foreach Macros that loop instead of function calls 
ForEachMacros: ['RANGES_FOR', 'FOREACH']
IfMacros: ['IF']
#  Multiple #include The blocks are merged together and sorted into one 
IncludeBlocks:   Merge
#  Negative priorities can be defined to ensure that some #include Always in the front 
IncludeCategories:
  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:        2
    SortPriority:    0
    CaseSensitive:   false
  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
    Priority:        3
    SortPriority:    0
    CaseSensitive:   false
  - Regex:           '.*'
    Priority:        1
    SortPriority:    0
    CaseSensitive:   false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
#  Indent access modifier 
IndentAccessModifiers: false
#  Indent case label 
IndentCaseLabels: false
# case  The block behind the label is used with  case  The same indentation level as the label 
IndentCaseBlocks: false
#  Backward compatible indent outer block 
IndentExternBlock: AfterExternBlock
#  Indent goto label .
IndentGotoLabels: false
#  Indent preprocessor instructions 
IndentPPDirectives: BeforeHash
#  Indent... In template requires Clause 
IndentRequires:  false
#  Indent width 
IndentWidth: 4
#  When the return type of a function wraps a line , Indent the function name of the function declaration or function definition 
IndentWrappedFunctionNames: false
InsertBraces: true
#  Insert trailing comma 
InsertTrailingCommas: None
#  Retain JavaScript String quotes 
JavaScriptQuotes: Leave
#  packing  JavaScript  Import / Export statement 
JavaScriptWrapImports: true
#  Leave blank lines at the beginning of the block 
KeepEmptyLinesAtTheStartOfBlocks: false
#  be relative to  lambda  Signature alignment  lambda  The main body 
LambdaBodyIndentation: Signature
#  Start a regular expression for a block macro 
MacroBlockBegin: ''
#  A regular expression that ends a block's macro 
MacroBlockEnd:   ''
#  The maximum number of consecutive blank lines 
MaxEmptyLinesToKeep: 1
#  Indentation of namespace 
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
#  Use ObjC The indent width of the block 
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
#  stay ObjC Of @property Add a space after 
ObjCSpaceAfterProperty: false
#  stay ObjC Of protocol Add a space before the list 
ObjCSpaceBeforeProtocolList: true
#  Indent the number of columns of the preprocessor statement 
PPIndentWidth:   -1
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
#  Pointer alignment : Left, Right, Middle
PointerAlignment: Right
#  Alignment of references 
ReferenceAlignment: Pointer
#  Allow reformatting comments 
ReflowComments:  true
RemoveBracesLLVM: false
#  The maximum number of expanded rows spanned by a short namespace 
ShortNamespaceLines: 1
#  Allow sorting #include
SortIncludes:    CaseSensitive
# java Static import before non static import 
SortJavaStaticImport: Before
#  Yes using Sort declaration 
SortUsingDeclarations: true
#  stay C Add space after style type conversion 
SpaceAfterCStyleCast: false
#  stay ! Add space after 
SpaceAfterLogicalNot: false
#  stay Template Add a space after the keyword 
SpaceAfterTemplateKeyword: true
#  Don't make sure there are spaces around the pointer qualifier 
SpaceAroundPointerQualifiers: Default
#  Add a space before the assignment operator 
SpaceBeforeAssignmentOperators: true
#  be not in case Add a space before the colon 
SpaceBeforeCaseColon: false
#  be not in C++11 Add a space before the brace list 
SpaceBeforeCpp11BracedList: false
#  Add a space before the constructor initializer colon 
SpaceBeforeCtorInitializerColon: true
#  Add a space before the inherited colon 
SpaceBeforeInheritanceColon: true
#  Add a space before opening the parentheses 
SpaceBeforeParens: ControlStatements
#  In scope based for Add a space before the loop colon 
SpaceBeforeRangeBasedForLoopColon: true
#  Space before brackets 
SpaceBeforeSquareBrackets: false
# {} Middle space 
SpaceInEmptyBlock: false
#  Add spaces in empty parentheses 
SpaceInEmptyParentheses: false
#  The number of spaces added before trailing comments ( Only applicable to //)
SpacesBeforeTrailingComments: 3
#  In angle brackets < And after > Add space before 
SpacesInAngles:  Never
#  stay C Add spaces in parentheses for style type conversion 
SpacesInCStyleCastParentheses: false
#  be not in if/for/switch/while Insert a space around the condition 
SpacesInConditionalStatement: false
#  In the container (ObjC and JavaScript Arrays and dictionaries, etc ) Add a space to the literal 
SpacesInContainerLiterals: true
#  How many spaces are allowed at the beginning of a line comment . To disable the maximum , Please set it to -1, besides , The maximum takes precedence over the minimum 
SpacesInLineCommentPrefix:
  Minimum:         1
  Maximum:         -1
#  In parentheses ( And after ) Add space before 
SpacesInParentheses: false
#  In square brackets [ And after ] Add space before ,lamda Declarations of expressions and arrays of unspecified size are not affected 
SpacesInSquareBrackets: false
#  standard 
Standard:        Auto
#  Macro definition ignored before statement , As if they were an attribute 
StatementAttributeLikeMacros:
  - Q_EMIT
#  Macro definitions that should be interpreted as complete statements 
StatementMacros:
  - Q_UNUSED
  - QT_REQUIRE_VERSION
# tab Width 
TabWidth:        4
#  Use \r\n Newline substitution \n
UseCRLF:         false
#  Use tab character :ForIndentation—— Use tabs only for indents 
UseTab:          ForIndentation
#  Space sensitive macro definitions 
WhitespaceSensitiveMacros:
  - STRINGIZE
  - PP_STRINGIZE
  - BOOST_PP_STRINGIZE
  - NS_SWIFT_NAME
  - CF_SWIFT_NAME
...
原网站

版权声明
本文为[Charles Ren]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110108324317.html