clang-format 格式化工具简易配置

来源:互联网 发布:mysql execute into 编辑:程序博客网 时间:2024/06/07 05:31

安装就不说了。

格式化风格可使用内置风格或使用参数文件指定风格:

$ clang-format -style=LLVM -i test.cc # -style参数指定所选风格,可选项为 LLVM、Google、Chromium、Mozilla、WebKit 和 file,其中 file 指定参数文件。-i 表示将格式化后的内容写入原文件

若在当前目录或祖先目录中已有 .clang-format 文件,则可直接

$ clang-format -i test.cc

.clang-format 可使用如下命令生成:

$ clang-format -style=llvm -dump-config > .clang-format

然后修改参数,以适应自己的要求。我习惯了 Linux kernel 风格,要修改以下几项:

BasedOnStyle: LLVMColumnLimit:  120    # 每行最多显示120个字符Standard:     Cpp11IndentWidth: 4TabWidth:    4UseTab: AlwaysBreakBeforeBraces: LinuxAllowShortIfStatementsOnASingleLine: falseIndentCaseLabels: falseAllowShortFunctionsOnASingleLine: Inline

CentOS 7 上修改后的内容如下:

---# BasedOnStyle:  LLVMAccessModifierOffset: -2ConstructorInitializerIndentWidth: 4AlignEscapedNewlinesLeft: falseAlignTrailingComments: trueAllowAllParametersOfDeclarationOnNextLine: trueAllowShortIfStatementsOnASingleLine: falseAllowShortLoopsOnASingleLine: falseAllowShortFunctionsOnASingleLine: NoneAlwaysBreakTemplateDeclarations: falseAlwaysBreakBeforeMultilineStrings: falseBreakBeforeBinaryOperators: falseBreakBeforeTernaryOperators: trueBreakConstructorInitializersBeforeComma: falseBinPackParameters: trueColumnLimit:     120ConstructorInitializerAllOnOneLineOrOnePerLine: falseDerivePointerBinding: falseExperimentalAutoDetectBinPacking: falseIndentCaseLabels: falseMaxEmptyLinesToKeep: 1NamespaceIndentation: NoneObjCSpaceBeforeProtocolList: truePenaltyBreakBeforeFirstCallParameter: 19PenaltyBreakComment: 60PenaltyBreakString: 1000PenaltyBreakFirstLessLess: 120PenaltyExcessCharacter: 1000000PenaltyReturnTypeOnItsOwnLine: 60PointerBindsToType: falseSpacesBeforeTrailingComments: 1Cpp11BracedListStyle: falseStandard:        Cpp11IndentWidth:     4TabWidth:        4UseTab:          AlwaysBreakBeforeBraces: LinuxIndentFunctionDeclarationAfterType: falseSpacesInParentheses: falseSpacesInAngles:  falseSpaceInEmptyParentheses: falseSpacesInCStyleCastParentheses: falseSpaceAfterControlStatementKeyword: trueSpaceBeforeAssignmentOperators: trueContinuationIndentWidth: 4...


具体参数设置参考:http://clang.llvm.org/docs/ClangFormatStyleOptions.html

vscode 只需在用户设置里面加一行:

"C_Cpp.clang_format_style": "{ BasedOnStyle: LLVM, AllowShortFunctionsOnASingleLine: Inline, AlwaysBreakTemplateDeclarations: true, SpaceAfterTemplateKeyword: false, ColumnLimit: 120, Standard: Cpp11, IndentWidth: 4, TabWidth: 4, UseTab: Always, BreakBeforeBraces: Linux }",


原创粉丝点击