我的clang-format配置
---
# 語言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
Language: Cpp
BasedOnStyle: google
# 訪問說明符(public、private等)的偏移
AccessModifierOffset: -4
# 開括號(開圓括號、開尖括號、開方括號)後的對齊: Align, DontAlign, AlwaysBreak(總是在開括號後換行)
AlignAfterOpenBracket: Align
# 連續賦值時,對齊所有等號
AlignConsecutiveAssignments: false
# 連續聲明時,對齊所有聲明的變量名
AlignConsecutiveDeclarations: false
# 水平對齊二元和三元表達式的操作數
AlignOperands: true
# 對齊連續的尾隨的註釋
AlignTrailingComments: false
# 允許短的case標籤放在同一行
AllowShortCaseLabelsOnASingleLine: false
# 允許短的函數放在同一行: None, InlineOnly(定義在類中), Empty(空函數), Inline(定義在類中,空函數), All
AllowShortFunctionsOnASingleLine: Empty
# 允許短的if語句保持在同一行
AllowShortIfStatementsOnASingleLine: false
# 允許短的循環保持在同一行
AllowShortLoopsOnASingleLine: false
# 總是在返回類型後換行: None, All, TopLevel(頂級函數,不包括在類中的函數),
# AllDefinitions(所有的定義,不包括聲明), TopLevelDefinitions(所有的頂級函數的定義)
AlwaysBreakAfterReturnType: None
# 總是在多行string字面量前換行
AlwaysBreakBeforeMultilineStrings: false
# 總是在template聲明後換行
AlwaysBreakTemplateDeclarations: false
# false表示函數實參要麼都在同一行,要麼都各自一行
BinPackArguments: true
# false表示所有形參要麼都在同一行,要麼都各自一行
BinPackParameters: true
# 在二元運算符前換行: None(在操作符後換行), NonAssignment(在非賦值的操作符前換行), All(在操作符前換行)
BreakBeforeBinaryOperators: NonAssignment
# 在大括號前換行: Attach(始終將大括號附加到周圍的上下文), Linux(除函數、命名空間和類定義,與Attach類似),
# Mozilla(除枚舉、函數、記錄定義,與Attach類似), Stroustrup(除函數定義、catch、else,與Attach類似),
# Allman(總是在大括號前換行), GNU(總是在大括號前換行,並對於控制語句的大括號增加額外的縮進), WebKit(在函數前換行), Custom
# 注:這裏認爲語句塊也屬於函數
#總是在大括號前換行
BreakBeforeBraces: Allman
# 在三元運算符前換行
BreakBeforeTernaryOperators: false
# 在構造函數的初始化列表的逗號前換行
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
# 每行字符的限制,0表示沒有限制
ColumnLimit: 80
# 描述具有特殊意義的註釋的正則表達式,它不應該被分割爲多行或以其它方式改變
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
# 構造函數的初始化列表要麼都在同一行,要麼都各自一行
ConstructorInitializerAllOnOneLineOrOnePerLine: false
# 構造函數的初始化列表的縮進寬度
ConstructorInitializerIndentWidth: 4
# 延續的行的縮進寬度
ContinuationIndentWidth: 4
# 去除C++11的列表初始化的大括號{後和}前的空格
Cpp11BracedListStyle: true
# 繼承最常用的指針和引用的對齊方式
DerivePointerAlignment: false
# 關閉格式化
DisableFormat: false
# 自動檢測函數的調用和定義是否被格式爲每行一個參數(Experimental)
ExperimentalAutoDetectBinPacking: false
# 需要被解讀爲foreach循環而不是函數調用的宏
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
# 縮進case標籤
IndentCaseLabels: false
# 宏#define的縮進
IndentPPDirectives: None
# 縮進寬度
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
# 指針和引用的對齊: Left, Right, Middle
PointerAlignment: Right
# 允許重新排版註釋
#ReflowComments: true
# 允許排序#include
SortIncludes: true
# 在C風格類型轉換後添加空格
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
# 在賦值運算符之前添加空格
SpaceBeforeAssignmentOperators: true
# 開圓括號之前添加一個空格: Never, ControlStatements, Always
SpaceBeforeParens: ControlStatements
# 在空的圓括號中添加空格
SpaceInEmptyParentheses: false
# 在尾隨的評論前添加的空格數(只適用於//)
SpacesBeforeTrailingComments: 2
# 在尖括號的<後和>前添加空格
SpacesInAngles: false
# 在容器(ObjC和JavaScript的數組和字典等)字面量中添加空格
SpacesInContainerLiterals: false
# 在C風格類型轉換的括號中添加空格
SpacesInCStyleCastParentheses: false
# 在圓括號的(後和)前添加空格
SpacesInParentheses: false
# 在方括號的[後和]前添加空格,lamda表達式和未指明大小的數組的聲明不受影響
SpacesInSquareBrackets: false
# tab寬度
TabWidth: 4
# 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always
UseTab: Never
示例:
原始代碼
/* BasedOnStyle: google
SortIncludes: true
可按google的方式重新對include進行排序
*/
#include <math.h>
#include <stdlib.h>
#include "test.h"
#include <sys/mman.h>
#include "base/log.h"
#include <iostream>
// CompactNamespaces: false
// NamespaceIndentation: Inner
namespace test { namespace testiner
{
class A
{
public: // AccessModifierOffset: -4
int*a; //PointerAlignment: Right
};
}
}
static void someFunction(int ssssssssssssssssssss, char *tttttttttttttttttttttt, const std::string &dljdjddljd)
{
// SpacesBeforeTrailingComments: 2
int aaaa = 12;//My comment a
// AlignConsecutiveDeclarations:| false
float b = 23;
// SpaceBeforeAssignmentOperators: true
std::string ccc=23;
// BreakBeforeBinaryOperators: NonAssignment
bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >
ccccccccccccccccccccccccccccccccccccccccc;
}
// AlignAfterOpenBracket: Align
static void
someOtherFunction(int ssssssssssssssssssss, char *tttttttttttttttttttttt,
const std::string &dljdjddljd) {
void test()
{
switch (x) {
case 0: // IndentCaseLabels: false
case 1:
do_something();
break;
case 2:
do_something_else();
break;
default: break; // AllowShortCaseLabelsOnASingleLine:| false
}
if(condition) // SpaceBeforeParens: ControlStatements
do_something_completely_different();
// BreakBeforeBraces: Allman
if (x == y) {
q();
} else if (x > y) {
w();
} else {
r();
}
}
}
// BinPackParameters: true
void foofunc(int ssssssssssssssssssss, char *tttttttttttttttttttttt,
const std::string &dljdjddljd)
{
// AllowShortBlocksOnASingleLine
while (true) ;
if (true) { func(); }
// SpacesInConditionalStatement
// SpaceBeforeRangeBasedForLoopColon
for( int i = 0; i < len; ++i)
{
printf("pattern");
}
// AlignAfterOpenBracket: Align
if (foo &&
bar) {
baz();
}
}
#格式化後的代碼
/* BasedOnStyle: google
SortIncludes: true
可按google的方式重新對include進行排序
*/
#include <math.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <iostream>
#include "base/log.h"
#include "test.h"
// CompactNamespaces: false
// NamespaceIndentation: Inner
namespace test
{
namespace testiner
{
class A
{
public: // AccessModifierOffset: -4
int *a; // PointerAlignment: Right
};
} // namespace testiner
} // namespace test
static void someFunction(int ssssssssssssssssssss, char *tttttttttttttttttttttt,
const std::string &dljdjddljd)
{
// SpacesBeforeTrailingComments: 2
int aaaa = 12; // My comment a
// AlignConsecutiveDeclarations:| false
float b = 23;
// SpaceBeforeAssignmentOperators: true
std::string ccc = 23;
// BreakBeforeBinaryOperators: NonAssignment
bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
== aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
&& aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
> ccccccccccccccccccccccccccccccccccccccccc;
}
// AlignAfterOpenBracket: Align
static void someOtherFunction(int ssssssssssssssssssss,
char *tttttttttttttttttttttt,
const std::string &dljdjddljd)
{
void test()
{
switch (x)
{
case 0: // IndentCaseLabels: false
case 1:
do_something();
break;
case 2:
do_something_else();
break;
default:
break; // AllowShortCaseLabelsOnASingleLine:| false
}
if (condition) // SpaceBeforeParens: ControlStatements
do_something_completely_different();
// BreakBeforeBraces: Allman
if (x == y)
{
q();
}
else if (x > y)
{
w();
}
else
{
r();
}
}
}
// BinPackParameters: true
void foofunc(int ssssssssssssssssssss, char *tttttttttttttttttttttt,
const std::string &dljdjddljd)
{
// AllowShortBlocksOnASingleLine
while (true)
;
if (true)
{
func();
}
// SpacesInConditionalStatement
// SpaceBeforeRangeBasedForLoopColon
for (int i = 0; i < len; ++i)
{
printf("pattern");
}
// AlignAfterOpenBracket: Align
if (foo && bar)
{
baz();
}
}
團隊代碼規範/格式化工具clang-format一
團隊代碼規範/格式化工具clang-format二
團隊代碼規範/格式化工具clang-format三
作者:帥得不敢出門