Skip to content

Commit a6798a1

Browse files
authored
Use clang-format to change the code style to google c++ style (#3461)
* add clang-format and CPPLINT.cfg * indent: 4 char, line width: 120 char * batch operation * fix tests
1 parent 76125ed commit a6798a1

File tree

203 files changed

+15961
-25465
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

203 files changed

+15961
-25465
lines changed

.clang-format

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: Google
4+
AccessModifierOffset: -1
5+
AlignAfterOpenBracket: Align
6+
AlignConsecutiveAssignments: false
7+
AlignConsecutiveDeclarations: false
8+
AlignEscapedNewlines: Right
9+
AlignOperands: true
10+
AlignTrailingComments: true
11+
AllowAllParametersOfDeclarationOnNextLine: true
12+
AllowShortBlocksOnASingleLine: false
13+
AllowShortCaseLabelsOnASingleLine: false
14+
AllowShortFunctionsOnASingleLine: Inline
15+
AllowShortIfStatementsOnASingleLine: true
16+
AllowShortLoopsOnASingleLine: true
17+
AlwaysBreakAfterDefinitionReturnType: None
18+
AlwaysBreakAfterReturnType: None
19+
AlwaysBreakBeforeMultilineStrings: false
20+
AlwaysBreakTemplateDeclarations: true
21+
BinPackArguments: false
22+
BinPackParameters: false
23+
BraceWrapping:
24+
AfterClass: false
25+
AfterControlStatement: false
26+
AfterEnum: false
27+
AfterFunction: false
28+
AfterNamespace: false
29+
AfterObjCDeclaration: false
30+
AfterStruct: false
31+
AfterUnion: false
32+
AfterExternBlock: false
33+
BeforeCatch: false
34+
BeforeElse: false
35+
IndentBraces: false
36+
SplitEmptyFunction: true
37+
SplitEmptyRecord: true
38+
SplitEmptyNamespace: true
39+
BreakBeforeBinaryOperators: None
40+
BreakBeforeBraces: Attach
41+
BreakBeforeInheritanceComma: false
42+
BreakBeforeTernaryOperators: true
43+
BreakConstructorInitializersBeforeComma: false
44+
BreakConstructorInitializers: BeforeColon
45+
BreakAfterJavaFieldAnnotations: false
46+
BreakStringLiterals: true
47+
ColumnLimit: 120
48+
CommentPragmas: '^ IWYU pragma:'
49+
CompactNamespaces: false
50+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
51+
ConstructorInitializerIndentWidth: 4
52+
ContinuationIndentWidth: 4
53+
Cpp11BracedListStyle: true
54+
DerivePointerAlignment: false
55+
DisableFormat: false
56+
ExperimentalAutoDetectBinPacking: false
57+
FixNamespaceComments: true
58+
ForEachMacros:
59+
- foreach
60+
- Q_FOREACH
61+
- BOOST_FOREACH
62+
IncludeBlocks: Preserve
63+
IncludeCategories:
64+
- Regex: '^<ext/.*\.h>'
65+
Priority: 2
66+
- Regex: '^<.*\.h>'
67+
Priority: 1
68+
- Regex: '^<.*'
69+
Priority: 2
70+
- Regex: '.*'
71+
Priority: 3
72+
IncludeIsMainRegex: '([-_](test|unittest))?$'
73+
IndentCaseLabels: false
74+
IndentPPDirectives: None
75+
IndentWidth: 4
76+
IndentWrappedFunctionNames: false
77+
JavaScriptQuotes: Leave
78+
JavaScriptWrapImports: true
79+
KeepEmptyLinesAtTheStartOfBlocks: false
80+
MacroBlockBegin: ''
81+
MacroBlockEnd: ''
82+
MaxEmptyLinesToKeep: 1
83+
NamespaceIndentation: None
84+
ObjCBlockIndentWidth: 2
85+
ObjCSpaceAfterProperty: false
86+
ObjCSpaceBeforeProtocolList: false
87+
PenaltyBreakAssignment: 2
88+
PenaltyBreakBeforeFirstCallParameter: 1
89+
PenaltyBreakComment: 300
90+
PenaltyBreakFirstLessLess: 120
91+
PenaltyBreakString: 1000
92+
PenaltyExcessCharacter: 1000000
93+
PenaltyReturnTypeOnItsOwnLine: 200
94+
PointerAlignment: Right
95+
ReflowComments: true
96+
SortIncludes: false
97+
SortUsingDeclarations: true
98+
SpaceAfterCStyleCast: true
99+
SpaceAfterTemplateKeyword: true
100+
SpaceBeforeAssignmentOperators: true
101+
SpaceBeforeParens: ControlStatements
102+
SpaceInEmptyParentheses: false
103+
SpacesBeforeTrailingComments: 2
104+
SpacesInAngles: false
105+
SpacesInContainerLiterals: true
106+
SpacesInCStyleCastParentheses: false
107+
SpacesInParentheses: false
108+
SpacesInSquareBrackets: false
109+
Standard: Auto
110+
TabWidth: 8
111+
UseTab: Never

CODE-STYLE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Code Style
2+
=======
3+
* Google C++ Style
4+
* Indent adjusted to 4 characters
5+
* Line width adjusted to 120 characters
6+

CPPLINT.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set noparent
2+
filter=-build/include_alpha,-build/include_subdir,-build/include_what_you_use,-legal/copyright,-readability/nolint
3+
braces=4
4+
linelength=120

code_format.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# core source file
2+
clang-format -i src/core/*.cc
3+
clang-format -i src/coroutine/*.cc
4+
clang-format -i src/lock/*.cc
5+
clang-format -i src/memory/*.cc
6+
clang-format -i src/network/*.cc
7+
clang-format -i src/os/*.cc
8+
clang-format -i src/pipe/*.cc
9+
clang-format -i src/protocol/*.cc
10+
clang-format -i src/reactor/*.cc
11+
clang-format -i src/server/*.cc
12+
clang-format -i src/wrapper/*.cc
13+
# core header file
14+
clang-format -i include/*.h
15+
16+
# ext source file
17+
clang-format -i *.cc
18+
19+
20+
clang-format -i examples/cpp/*.cc
21+
22+
# core-tests source file
23+
clang-format -i core-tests/src/client/*.cpp
24+
clang-format -i core-tests/src/core/*.cpp
25+
clang-format -i core-tests/src/coroutine/*.cpp
26+
clang-format -i core-tests/src/lock/*.cpp
27+
clang-format -i core-tests/src/memory/*.cpp
28+
clang-format -i core-tests/src/network/*.cpp
29+
clang-format -i core-tests/src/os/*.cpp
30+
clang-format -i core-tests/src/process/*.cpp
31+
clang-format -i core-tests/src/protocol/*.cpp
32+
clang-format -i core-tests/src/reactor/*.cpp
33+
clang-format -i core-tests/src/server/*.cpp
34+
clang-format -i core-tests/src/main.cpp

core-tests/src/client/client.cpp

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,33 @@
66
#define GREETER "Hello Swoole"
77
#define GREETER_SIZE sizeof(GREETER)
88

9+
using swoole::AsyncClient;
910
using swoole::test::Process;
1011
using swoole::test::Server;
11-
using swoole::AsyncClient;
1212

13-
TEST(client, tcp)
14-
{
13+
TEST(client, tcp) {
1514
int ret;
1615
swClient cli;
1716
char buf[128];
1817

1918
pid_t pid;
2019

21-
Process proc([](Process *proc)
22-
{
23-
on_receive_lambda_type receive_fn = [](ON_RECEIVE_PARAMS)
24-
{
20+
Process proc([](Process *proc) {
21+
on_receive_lambda_type receive_fn = [](ON_RECEIVE_PARAMS) {
2522
char *data_ptr = NULL;
2623
size_t data_len = SERVER_THIS->get_packet(req, (char **) &data_ptr);
2724

2825
SERVER_THIS->send(req->info.fd, data_ptr, data_len);
2926
};
30-
27+
3128
Server serv(TEST_HOST, TEST_PORT, SW_MODE_BASE, SW_SOCK_TCP);
3229
serv.on("onReceive", (void *) receive_fn);
3330
serv.start();
3431
});
3532

3633
pid = proc.start();
3734

38-
sleep(1); // wait for the test server to start
35+
sleep(1); // wait for the test server to start
3936

4037
ret = swClient_create(&cli, SW_SOCK_TCP, SW_SOCK_SYNC);
4138
ASSERT_EQ(ret, 0);
@@ -50,18 +47,15 @@ TEST(client, tcp)
5047
kill(pid, SIGKILL);
5148
}
5249

53-
TEST(client, udp)
54-
{
50+
TEST(client, udp) {
5551
int ret;
5652
swClient cli;
5753
char buf[128];
5854

5955
pid_t pid;
6056

61-
Process proc([](Process *proc)
62-
{
63-
on_packet_lambda_type packet_fn = [](ON_PACKET_PARAMS)
64-
{
57+
Process proc([](Process *proc) {
58+
on_packet_lambda_type packet_fn = [](ON_PACKET_PARAMS) {
6559
swDgramPacket *packet = nullptr;
6660
SERVER_THIS->get_packet(req, (char **) &packet);
6761

@@ -75,7 +69,7 @@ TEST(client, udp)
7569

7670
pid = proc.start();
7771

78-
sleep(1); // wait for the test server to start
72+
sleep(1); // wait for the test server to start
7973

8074
ret = swClient_create(&cli, SW_SOCK_UDP, SW_SOCK_SYNC);
8175
ASSERT_EQ(ret, 0);
@@ -90,17 +84,14 @@ TEST(client, udp)
9084
kill(pid, SIGKILL);
9185
}
9286

93-
TEST(client, async_tcp)
94-
{
87+
TEST(client, async_tcp) {
9588
pid_t pid;
9689

9790
swPipe p;
9891
ASSERT_EQ(swPipeNotify_auto(&p, 1, 1), 0);
9992

100-
Process proc([&p](Process *proc)
101-
{
102-
on_receive_lambda_type receive_fn = [](ON_RECEIVE_PARAMS)
103-
{
93+
Process proc([&p](Process *proc) {
94+
on_receive_lambda_type receive_fn = [](ON_RECEIVE_PARAMS) {
10495
char *data_ptr = NULL;
10596
size_t data_len = SERVER_THIS->get_packet(req, (char **) &data_ptr);
10697

@@ -113,8 +104,7 @@ TEST(client, async_tcp)
113104

114105
serv.on("onReceive", (void *) receive_fn);
115106

116-
on_workerstart_lambda_type worker_start_fn = [] (ON_WORKERSTART_PARAMS)
117-
{
107+
on_workerstart_lambda_type worker_start_fn = [](ON_WORKERSTART_PARAMS) {
118108
swPipe *p = (swPipe *) SERVER_THIS->get_private_data("pipe");
119109
int64_t value = 1;
120110
p->write(p, &value, sizeof(value));
@@ -135,22 +125,16 @@ TEST(client, async_tcp)
135125

136126
AsyncClient ac(SW_SOCK_TCP);
137127

138-
ac.on_connect([](AsyncClient *ac)
139-
{
140-
ac->send(SW_STRS(GREETER));
141-
});
128+
ac.on_connect([](AsyncClient *ac) { ac->send(SW_STRS(GREETER)); });
142129

143-
ac.on_close([](AsyncClient *ac)
144-
{
130+
ac.on_close([](AsyncClient *ac) {
145131

146132
});
147-
ac.on_error([](AsyncClient *ac)
148-
{
133+
ac.on_error([](AsyncClient *ac) {
149134

150135
});
151136

152-
ac.on_receive([](AsyncClient *ac, const char *data, size_t len)
153-
{
137+
ac.on_receive([](AsyncClient *ac, const char *data, size_t len) {
154138
ASSERT_EQ(len, GREETER_SIZE);
155139
ASSERT_STREQ(GREETER, data);
156140
ac->close();

core-tests/src/core/base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "tests.h"
2121

2222
TEST(base, DataHead_dump) {
23-
swDataHead data = { };
23+
swDataHead data = {};
2424
data.fd = 123;
2525
char buf[128];
2626
size_t n = data.dump(buf, sizeof(buf));

core-tests/src/core/channel.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ TEST(channel, push) {
4545

4646
swTrace("size=%d\n", m.size());
4747

48-
thread t1([&]()
49-
{
48+
thread t1([&]() {
5049
auto next = m.find(0);
5150
int index = 1;
5251
size_t bytes = 0;
5352

54-
while(bytes < N) {
53+
while (bytes < N) {
5554
if (c->push(next->second.c_str(), next->second.length()) == SW_OK) {
5655
swTrace("[PUSH] index=%d, size=%d\n", index, next->second.length());
5756
bytes += next->second.length();
@@ -65,12 +64,11 @@ TEST(channel, push) {
6564
}
6665
});
6766

68-
thread t2([&]()
69-
{
67+
thread t2([&]() {
7068
char buf[8000];
7169
size_t bytes = 0;
7270
int index = 0;
73-
while(bytes < N) {
71+
while (bytes < N) {
7472
int retval = c->pop(buf, sizeof(buf));
7573
if (retval > 0) {
7674
swTrace("[POP] index=%d, size=%ld\n", index, retval);

core-tests/src/core/heap.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
#include "tests.h"
22
#include <map>
33

4-
typedef struct node_t
5-
{
4+
typedef struct node_t {
65
int pri;
76
int val;
87
} node_t;
98

10-
#define SIZE 100
9+
#define SIZE 100
1110

12-
TEST(heap, random)
13-
{
11+
TEST(heap, random) {
1412
swHeap *pq;
1513
node_t *ns;
1614
node_t *n;
@@ -21,18 +19,16 @@ TEST(heap, random)
2119
std::map<int, int> _map;
2220

2321
int i;
24-
for (i = 0; i < SIZE - 1; i++)
25-
{
22+
for (i = 0; i < SIZE - 1; i++) {
2623
int pri = swoole_system_random(10000, 99999);
27-
ns = (node_t*) malloc(sizeof(node_t));
24+
ns = (node_t *) malloc(sizeof(node_t));
2825
ns->val = i;
2926
ns->pri = pri;
3027
swHeap_push(pq, pri, ns);
3128
_map[i] = pri;
3229
}
3330

34-
while ((n = (node_t*) swHeap_pop(pq)))
35-
{
31+
while ((n = (node_t *) swHeap_pop(pq))) {
3632
ASSERT_EQ(_map[n->val], n->pri);
3733
free(n);
3834
}

0 commit comments

Comments
 (0)