VSCode C++代码自动格式化配置

日常开发时,不论是企业或组织都会对代码格式有统一的要求,本文主要讲解如何在vscode中自动按Google C++代码规范进行代码自动格式化和检查提示。如有需要也可以在此基础上进行自定义调整。

一、C++代码自动格式化

1、安装clang-format

# clang format
sudo apt install -y clang-format

2、vscode安装clang-format扩展

3、vscode工作空间添加格式设置文件.clangfomat

.clangfomat需要放到vscode工程目录下 (就是vscode打开的目录下,与.vscode目录同级)
$ vim .clang-format

# 风格:Google, LLVM, Chromium, Mozilla, WebKit, Microsoft, GUN
BasedOnStyle: Google
# 语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
Language: Cpp
# 标准: Cpp03, Cpp11, Auto
Standard: Cpp11
# 去除C++11的列表初始化的大括号{后和}前的空格
Cpp11BracedListStyle: true
# 访问说明符(public、private等)的偏移
AccessModifierOffset: -4
# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行)
AlignAfterOpenBracket: AlwaysBreak
# 水平对齐二元和三元表达式的操作数
AlignOperands: false
# 允许函数声明的所有参数在放在下一行
AllowAllParametersOfDeclarationOnNextLine: false
# 允许短的块放在同一行
AllowShortBlocksOnASingleLine: false
# 允许短的case标签放在同一行
AllowShortCaseLabelsOnASingleLine: false
# 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All
AllowShortFunctionsOnASingleLine: Empty
# 允许短的if语句保持在同一行
AllowShortIfStatementsOnASingleLine: false
# 允许短的循环保持在同一行
AllowShortLoopsOnASingleLine: false
# 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数),  # AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义)
AlwaysBreakAfterReturnType: None
# 总是在template声明后换行
AlwaysBreakTemplateDeclarations: true
# false表示函数实参要么都在同一行,要么都各自一行
BinPackArguments: false
# false表示所有形参要么都在同一行,要么都各自一行
BinPackParameters: false
# 构造函数的初始化列表换行
BreakConstructorInitializers: AfterColon
# 每行字符的限制,0表示没有限制
ColumnLimit: 120
# 构造函数的初始化列表的缩进宽度
ConstructorInitializerIndentWidth: 8
# 延续的行的缩进宽度
ContinuationIndentWidth: 4
# 继承最常用的指针和引用的对齐方式
DerivePointerAlignment: true
# 为命名空间添加缺失的命名空间结束注释
FixNamespaceComments: true
# 缩进case标签
IndentCaseLabels: false
# 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always
UseTab: Never
# 缩进宽度
IndentWidth: 4
# 连续空行的最大数量
MaxEmptyLinesToKeep: 1
# 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), All
NamespaceIndentation: None
PenaltyBreakAssignment: 2
# 在call(后对函数调用换行的penalty
PenaltyBreakBeforeFirstCallParameter: 1
# 在一个注释中引入换行的penalty
PenaltyBreakComment: 500
# 第一次在<<前换行的penalty
PenaltyBreakFirstLessLess: 120
# 在一个字符串字面量中引入换行的penalty
PenaltyBreakString: 1000
# 对于每个在行字符数限制之外的字符的penalty
PenaltyExcessCharacter: 1000000
# 将函数的返回类型放到它自己的行的penalty
PenaltyReturnTypeOnItsOwnLine: 400
# 指针和引用的对齐: Left, Right, Middle
PointerAlignment: Right
# 允许排序#include
SortIncludes: false
# 连续宏定义的值对齐
AlignConsecutiveMacros: true

4、使用格式化

重启vscode,使用ctrl + shift + I或右键格式化单个c++代码文件。

或使用以下命令批量格式化:

// 需确保目录已经有.clang-format文件
find $project_path -iname *.h -o -iname *.hpp -o -iname *.cpp -o -iname *.cc | xargs clang-format -i

二、C++代码格式化检查和提示

1、安装cpplint

# cpplint
sudo apt install -y python python3 python-pip
pip install cpplint

2、vscode安装cpplint扩展

3、插件设置

点击cpplint插件设置按钮,把Cp不断lint: Line Length设置为120:

代码格式不对时,会自动小黄线提醒。

5、批量检查

# 检查单个文件或目录
cpplint --linelength=120 --counting=detailed --extensions=c,cc,cpp,hpp,h --filter=-build/c++11,-build/header_guard,-build/include_order,-build/include_what_you_use,-legal/copyright,-build/include_subdir,-whitespace/indent,-runtime/references,-runtime/string $CODE_FILE_OR_PROJECT_PATH

 

yan 23.7.5

 

参考:

【C++】Clang-Format:代码自动格式化

欢迎关注下方“非著名资深码农“公众号进行交流~

发表评论

邮箱地址不会被公开。