フォーマットツールclang-formatの紹介

11253 ワード

一、clang-format概要
普段、チームが協力するときはコードのフォーマットに注意する必要があります.一人一人のコードスタイルを統一するのは難しいですが、ツールを通じてコードのフォーマットをよく管理することができます.ここではclang-formatについて説明します.clangベースのコマンドラインツールで、フォーマット:C、C++、Object-Cコードを自動化でき、Google、Chromium、LLVM、Mozilla、WebKitなど多くのコードスタイルをサポートします.カスタムスタイル(.clang-formatファイルを作成することによって)もサポートされ、便利な統一コードフォーマットです.
二、配置ファイルの説明
---
#   : None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
Language: Cpp
# BasedOnStyle:	LLVM
#      (public、private )   
AccessModifierOffset: -4
#    (    、    、    )    : Align, DontAlign, AlwaysBreak(         )
AlignAfterOpenBracket: Align
#      ,      
AlignConsecutiveAssignments: true
#      ,          
AlignConsecutiveDeclarations: true
#        (       )    
AlignEscapedNewlinesLeft: true
#                 
AlignOperands: true
#           
AlignTrailingComments: true
#                  
AllowAllParametersOfDeclarationOnNextLine: true
#           
AllowShortBlocksOnASingleLine: false
#     case       
AllowShortCaseLabelsOnASingleLine: false
#            : None, InlineOnly(     ), Empty(   ), Inline(     ,   ), All
AllowShortFunctionsOnASingleLine: Empty
#     if        
AllowShortIfStatementsOnASingleLine: false
#             
AllowShortLoopsOnASingleLine: false
#             (deprecated)
AlwaysBreakAfterDefinitionReturnType: None
#           : None, All, TopLevel(    ,         ), 
#   AllDefinitions(     ,     ), TopLevelDefinitions(          )
AlwaysBreakAfterReturnType: None
#      string      
AlwaysBreakBeforeMultilineStrings: false
#    template     
AlwaysBreakTemplateDeclarations: false
# false             ,       
BinPackArguments: true
# false             ,       
BinPackParameters: true
#      ,   BreakBeforeBraces   Custom    
BraceWrapping:   
  # class    
  AfterClass: false
  #       
  AfterControlStatement: false
  # enum    
  AfterEnum: false
  #       
  AfterFunction: false
  #         
  AfterNamespace: false
  # ObjC    
  AfterObjCDeclaration:	false
  # struct    
  AfterStruct: false
  # union    
  AfterUnion: false
  # catch  
  BeforeCatch: true
  # else  
  BeforeElse: true
  #      
  IndentBraces:	false
#          : None(       ), NonAssignment(           ), All(       )
BreakBeforeBinaryOperators:	NonAssignment
#        : Attach(               ), Linux(   、        , Attach  ), 
#   Mozilla(   、  、    , Attach  ), Stroustrup(     、catch、else, Attach  ), 
#   Allman(         ), GNU(         ,                  ), WebKit(      ), Custom
#    :            
BreakBeforeBraces: Custom
#          
BreakBeforeTernaryOperators: true
#                  
BreakConstructorInitializersBeforeComma: false
#        ,0      
ColumnLimit: 200
#                  ,                  
CommentPragmas:	'^ IWYU pragma:'
#                  ,       
ConstructorInitializerAllOnOneLineOrOnePerLine:	false
#                
ConstructorInitializerIndentWidth: 4
#          
ContinuationIndentWidth: 4
#   C++11          {  }    
Cpp11BracedListStyle: false
#                 
DerivePointerAlignment:	false
#      
DisableFormat: false
#                         (Experimental)
ExperimentalAutoDetectBinPacking: false
#       foreach           
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
#  #include    ,          #include        ,            INT_MAX(          ),
#                  #include      
IncludeCategories: 
 - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
    Priority: 2
 - Regex: '^(
    Priority: 3
 - Regex: '.*'
    Priority: 1
#   case  
IndentCaseLabels: false
#     
IndentWidth: 4
#          ,               
IndentWrappedFunctionNames:	false
#           
KeepEmptyLinesAtTheStartOfBlocks: true
#              
MacroBlockBegin: ''
#              
MacroBlockEnd: ''
#          
MaxEmptyLinesToKeep: 1
#        : None, Inner(             ), All
NamespaceIndentation: Inner
#   ObjC      
ObjCBlockIndentWidth: 4
#  ObjC @property       
ObjCSpaceAfterProperty:	false
#  ObjC protocol         
ObjCSpaceBeforeProtocolList: true
#  call(         penalty
PenaltyBreakBeforeFirstCallParameter: 19
#            penalty
PenaltyBreakComment: 300
#     <
PenaltyBreakFirstLessLess: 120
#                penalty
PenaltyBreakString:	1000
#                  penalty
PenaltyExcessCharacter:	1000000
#                 penalty
PenaltyReturnTypeOnItsOwnLine: 60
#         : Left, Right, Middle
PointerAlignment: Left
#         
ReflowComments:	true
#     #include
SortIncludes: true
#  C           
SpaceAfterCStyleCast: false
#             
SpaceBeforeAssignmentOperators:	true
#             : Never, ControlStatements, Always
SpaceBeforeParens: ControlStatements
#            
SpaceInEmptyParentheses: false
#              (    //)
SpacesBeforeTrailingComments: 2
#           
SpacesInAngles:	true
#    (ObjC JavaScript       )        
SpacesInContainerLiterals: true
#  C              
SpacesInCStyleCastParentheses: true
#      (  )     
SpacesInParentheses: true
#      [  ]     ,lamda                   
SpacesInSquareBrackets:	true
#   : Cpp03, Cpp11, Auto
Standard: Cpp11
# tab  
TabWidth: 4
#   tab  : Never, ForIndentation, ForContinuationAndIndentation, Always
UseTab:	Never

三、vscode clang-formatプラグインの使用
  • まずClang-Formatプラグインを取り付けます.
  • 「プリファレンス」->「設定」でClang-format: Executableの位置を実際の位置に変更します.一般的なプラグインはD:\Users\xxx\.vscode\extensions\ms-vscode.cpptools-x.xx.x\LLVM\binディレクトリの下に配置されます.ここでは実行可能なファイルを指定する必要があります.たとえば、D:\Users\jaron\.vscode\extensions\ms-vscode.cpptools-0.29.0\LLVM\bin\clang-format.exe
  • です.
  • C_Cpp: Clang_format_styleC_Cpp: Clang_format_fallback Styleを設定する値はfileで、workspaceディレクトリから.clang-formatファイルを使用することを指定できます.
  • は、.clang-formatファイルを対応するエンジニアリングディレクトリの下にコピーします.
  • 開いているソースコードの下でAlt + Shift + Fを実行すると、ソースコードをフォーマットするか、右クリックして「ドキュメントのフォーマット」を選択します.

  • 四、コマンドライン
  • カスタム構成によるフォーマットコード./clang-format.exe --assume-filename=E:/test/.clang-format -i E:/test/src/main.c
  • は、Webkit ./clang-format.exe -style=Webkit -i D:/test/src/main.c
  • を指定する組み込み構成フォーマットコードを使用する.
  • コマンドライン特定スタイルのプロファイル(Webkitを例に)./clang-format.exe -dump-config -style=Webkit >.clang-format
  • をエクスポートする.