代码格式化工具 Clang-format

介绍

平时团队进行合作的时候需要注意代码的格式,虽然很难统一每个人的编码风格,但是通过工具能够很好的管理代码格式。这里介绍下clang-format,它是基于clang的一个命令行工具,能够自动化格式C/C++/Obj-C代码,支持多种代码风格:Google, Chromium, LLVM, Mozilla, WebKit,也支持自定义风格(通过编写.clang-format文件)很方便的同意代码格式。

使用方法
  • Linux下下安装Clang-format : sudo apt-get install clang-format
  • 以LLVM代码风格格式化main.cpp, 结果直接写到main.cpp

    clang g-format -i main.cpp -style=LLVM

  • 当然也支持对指定行格式化,格式化main.cpp的第1,2行

    clang-format -lines=1:2 main.cpp

  • vim 中也可以集成该插件1

  • 它提供一个clang-format-diff.py脚本,用来格式化patch,code review提交代码2
配置文件说明
  • 配置文件.clang-format详细说明
# 基于那个配置文件
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单行 If true, if (a) return; 可以放到同一行
AllowShortIfStatementsOnASingleLine: false
# 允许短的循环保持在同一行   
AllowShortLoopsOnASingleLine: false 
# 总是在定义返回类型后换行(deprecated)   
AlwaysBreakAfterDefinitionReturnType: None
# 每行字符的限制,0表示没有限制  
ColumnLimit: 100
# 描述具有特殊意义的注释的正则表达式,它不应该被分割为多行或以其它方式改变
CommentPragmas: '^ IWYU pragma:'
# 语言: None Cpp Java Objc Protp
Language: Cpp 
#指针的*的挨着哪边
PointerAlignment: Right
#缩进宽度
IndentWidth: 4
# 连续的空行保留几行
MaxEmptyLinesToKeep: 1
# 在 @property 后面添加空格, \@property (readonly) 而不是 \@property(readonly).
ObjCSpaceAfterProperty: true
# OC block后面的缩进
ObjCBlockIndentWidth: 4
# 是否允许短方法单行
AllowShortFunctionsOnASingleLine: false
# 换行的时候对齐操作符
#AlignOperands: true
# 中括号两边空格 [] 
SpacesInSquareBrackets: true
# 小括号两边添加空格
SpacesInParentheses : false
#等号两边的空格
SpaceBeforeAssignmentOperators: true
# 容器类的空格 例如 OC的字典
SpacesInContainerLiterals: true
#缩进
IndentWrappedFunctionNames: true
#在block从空行开始
KeepEmptyLinesAtTheStartOfBlocks: true
#在构造函数初始化时按逗号断行,并以冒号对齐
BreakConstructorInitializersBeforeComma: true
#括号后添加空格
SpaceAfterCStyleCast: false
# 允许排序#include, 造成编译错误
# SortIncludes: true 
# 缩进case 标签
IndentCaseLabels: true 
#tab键盘的宽度
TabWidth: 4
UseTab: Never
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
参考
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值