Skip to content

Commit f38975a

Browse files
committed
Use clang-tidy to auto fix C++ code style issues (this time addressing only superficial problems).
1 parent d38adf1 commit f38975a

Some content is hidden

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

91 files changed

+853
-909
lines changed

include/swoole.h

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@
4040
#endif
4141

4242
/*--- C standard library ---*/
43-
#include <assert.h>
44-
#include <ctype.h>
45-
#include <errno.h>
46-
#include <stdarg.h>
47-
#include <stddef.h>
48-
#include <stdio.h>
49-
#include <stdlib.h>
50-
#include <string.h>
51-
#include <limits.h>
43+
#include <cassert>
44+
#include <cctype>
45+
#include <cstdarg>
46+
#include <cstddef>
47+
#include <cstdio>
48+
#include <cstdlib>
49+
#include <cstring>
50+
#include <climits>
5251
#include <unistd.h>
5352
#include <sched.h> /* sched_yield() */
5453
#include <pthread.h>
@@ -268,7 +267,7 @@ int sw_printf(const char *format, ...);
268267

269268
#define sw_memset_zero(s, n) memset(s, '\0', n)
270269

271-
static sw_inline int sw_mem_equal(const void *v1, size_t s1, const void *v2, size_t s2) {
270+
static inline int sw_mem_equal(const void *v1, size_t s1, const void *v2, size_t s2) {
272271
return s1 == s2 && memcmp(v1, v2, s2) == 0;
273272
}
274273

@@ -284,15 +283,15 @@ static inline size_t swoole_strlcpy(char *dest, const char *src, size_t size) {
284283

285284
static inline char *swoole_strdup(const char *s) {
286285
size_t l = strlen(s) + 1;
287-
char *p = (char *) sw_malloc(l);
286+
char *p = static_cast<char *>(sw_malloc(l));
288287
if (sw_likely(p)) {
289288
memcpy(p, s, l);
290289
}
291290
return p;
292291
}
293292

294-
static inline char *swoole_strndup(const char *s, size_t n) {
295-
char *p = (char *) sw_malloc(n + 1);
293+
static inline char *swoole_strndup(const char *s, const size_t n) {
294+
char *p = static_cast<char *>(sw_malloc(n + 1));
296295
if (sw_likely(p)) {
297296
strncpy(p, s, n)[n] = '\0';
298297
}
@@ -321,57 +320,53 @@ static inline const char *swoole_strnstr(const char *haystack,
321320
const char *needle,
322321
uint32_t needle_length) {
323322
assert(needle_length > 0);
324-
uint32_t i;
325323

326324
if (sw_likely(needle_length <= haystack_length)) {
327-
for (i = 0; i < haystack_length - needle_length + 1; i++) {
325+
for (uint32_t i = 0; i < haystack_length - needle_length + 1; i++) {
328326
if ((haystack[0] == needle[0]) && (0 == memcmp(haystack, needle, needle_length))) {
329327
return haystack;
330328
}
331329
haystack++;
332330
}
333331
}
334332

335-
return NULL;
333+
return nullptr;
336334
}
337335

338336
static inline const char *swoole_strncasestr(const char *haystack,
339337
uint32_t haystack_length,
340338
const char *needle,
341339
uint32_t needle_length) {
342340
assert(needle_length > 0);
343-
uint32_t i;
344341

345342
if (sw_likely(needle_length <= haystack_length)) {
346-
for (i = 0; i < haystack_length - needle_length + 1; i++) {
343+
for (uint32_t i = 0; i < haystack_length - needle_length + 1; i++) {
347344
if ((haystack[0] == needle[0]) && (0 == strncasecmp(haystack, needle, needle_length))) {
348345
return haystack;
349346
}
350347
haystack++;
351348
}
352349
}
353350

354-
return NULL;
351+
return nullptr;
355352
}
356353

357354
static inline ssize_t swoole_strnpos(const char *haystack,
358355
uint32_t haystack_length,
359356
const char *needle,
360357
uint32_t needle_length) {
361358
assert(needle_length > 0);
362-
const char *pos;
363359

364-
pos = swoole_strnstr(haystack, haystack_length, needle, needle_length);
365-
return pos == NULL ? -1 : pos - haystack;
360+
const char *pos = swoole_strnstr(haystack, haystack_length, needle, needle_length);
361+
return pos == nullptr ? -1 : pos - haystack;
366362
}
367363

368364
static inline ssize_t swoole_strrnpos(const char *haystack, const char *needle, uint32_t length) {
369365
uint32_t needle_length = strlen(needle);
370366
assert(needle_length > 0);
371-
uint32_t i;
372367
haystack += (length - needle_length);
373368

374-
for (i = length - needle_length; i > 0; i--) {
369+
for (uint32_t i = length - needle_length; i > 0; i--) {
375370
if ((haystack[0] == needle[0]) && (0 == memcmp(haystack, needle, needle_length))) {
376371
return i;
377372
}
@@ -381,14 +376,12 @@ static inline ssize_t swoole_strrnpos(const char *haystack, const char *needle,
381376
return -1;
382377
}
383378

384-
static inline void swoole_strtolower(char *str, int length) {
385-
char *c, *e;
386-
387-
c = str;
388-
e = c + length;
379+
static inline void swoole_strtolower(char *str, const int length) {
380+
char *c = str;
381+
const char *e = c + length;
389382

390383
while (c < e) {
391-
*c = tolower(*c);
384+
*c = static_cast<char>(tolower(*c));
392385
c++;
393386
}
394387
}
@@ -503,17 +496,17 @@ void swoole_random_string(std::string &str, size_t size);
503496
uint64_t swoole_random_int();
504497
size_t swoole_random_bytes(char *buf, size_t size);
505498

506-
static sw_inline char *swoole_strlchr(char *p, char *last, char c) {
499+
static inline char *swoole_strlchr(char *p, const char *last, char c) {
507500
while (p < last) {
508501
if (*p == c) {
509502
return p;
510503
}
511504
p++;
512505
}
513-
return NULL;
506+
return nullptr;
514507
}
515508

516-
static sw_inline size_t swoole_size_align(size_t size, int pagesize) {
509+
static inline size_t swoole_size_align(size_t size, int pagesize) {
517510
return size + (pagesize - (size % pagesize));
518511
}
519512

@@ -564,18 +557,18 @@ int swoole_rand(int min, int max);
564557
int swoole_system_random(int min, int max);
565558

566559
int swoole_version_compare(const char *version1, const char *version2);
567-
void swoole_print_backtrace(void);
568-
void swoole_print_backtrace_on_error(void);
560+
void swoole_print_backtrace();
561+
void swoole_print_backtrace_on_error();
569562
char *swoole_string_format(size_t n, const char *format, ...);
570563
bool swoole_get_env(const char *name, int *value);
571564
int swoole_get_systemd_listen_fds();
572565

573-
void swoole_init(void);
574-
void swoole_clean(void);
575-
void swoole_exit(int __status);
566+
void swoole_init();
567+
void swoole_clean();
568+
void swoole_exit(int _status);
576569
pid_t swoole_fork(int flags);
577-
pid_t swoole_fork_exec(const std::function<void(void)> &child_fn);
578-
pid_t swoole_waitpid(pid_t __pid, int *__stat_loc, int __options);
570+
pid_t swoole_fork_exec(const std::function<void()> &child_fn);
571+
pid_t swoole_waitpid(pid_t _pid, int *_stat_loc, int _options);
579572
void swoole_thread_init(bool main_thread);
580573
void swoole_thread_clean(bool main_thread);
581574
void swoole_redirect_stdout(int new_fd);
@@ -606,18 +599,18 @@ int swoole_get_cpu_affinity(cpu_set_t *set);
606599
int swoole_clock_realtime(struct timespec *t);
607600
#endif
608601

609-
static inline struct timespec swoole_time_until(int milliseconds) {
610-
struct timespec t;
602+
static inline timespec swoole_time_until(time_t milliseconds) {
603+
timespec t;
611604
swoole_clock_realtime(&t);
612605

613-
int sec = milliseconds / 1000;
614-
int msec = milliseconds - (sec * 1000);
606+
const time_t sec = milliseconds / 1000;
607+
const time_t msec = milliseconds - (sec * 1000);
615608

616609
t.tv_sec += sec;
617610
t.tv_nsec += msec * 1000 * 1000;
618611

619612
if (t.tv_nsec > SW_NUM_BILLION) {
620-
int _sec = t.tv_nsec / SW_NUM_BILLION;
613+
const time_t _sec = t.tv_nsec / SW_NUM_BILLION;
621614
t.tv_sec += _sec;
622615
t.tv_nsec -= _sec * SW_NUM_BILLION;
623616
}
@@ -631,11 +624,11 @@ typedef long SessionId;
631624
typedef long TaskId;
632625
typedef uint8_t ReactorId;
633626
typedef uint32_t WorkerId;
634-
typedef enum swEventType EventType;
635-
typedef enum swSocketType SocketType;
636-
typedef enum swFdType FdType;
637-
typedef enum swReturnCode ReturnCode;
638-
typedef enum swResultCode ResultCode;
627+
typedef swEventType EventType;
628+
typedef swSocketType SocketType;
629+
typedef swFdType FdType;
630+
typedef swReturnCode ReturnCode;
631+
typedef swResultCode ResultCode;
639632

640633
struct Event {
641634
int fd;
@@ -663,11 +656,11 @@ struct EventData {
663656
DataHead info;
664657
char data[SW_IPC_BUFFER_SIZE];
665658

666-
uint32_t size() {
659+
uint32_t size() const {
667660
return sizeof(info) + len();
668661
}
669662

670-
uint32_t len() {
663+
uint32_t len() const {
671664
return info.len;
672665
}
673666
};
@@ -718,7 +711,7 @@ struct NameResolver {
718711
};
719712
std::function<std::string(const std::string &, Context *, void *)> resolve;
720713
void *private_data;
721-
enum Type type;
714+
Type type;
722715
};
723716

724717
struct DnsServer {
@@ -790,7 +783,7 @@ struct Global {
790783
std::string dirname(const std::string &file);
791784
int hook_add(void **hooks, int type, const Callback &func, int push_back);
792785
void hook_call(void **hooks, int type, void *arg);
793-
double microtime(void);
786+
double microtime();
794787
} // namespace swoole
795788

796789
extern swoole::Global SwooleG; // Local Global Variable
@@ -802,22 +795,22 @@ static inline void swoole_set_last_error(int error) {
802795
SwooleTG.error = error;
803796
}
804797

805-
static inline int swoole_get_last_error(void) {
798+
static inline int swoole_get_last_error() {
806799
return SwooleTG.error;
807800
}
808801

809-
static inline void swoole_clear_last_error(void) {
802+
static inline void swoole_clear_last_error() {
810803
SwooleTG.error = 0;
811804
}
812805

813-
void swoole_clear_last_error_msg(void);
814-
const char *swoole_get_last_error_msg(void);
806+
void swoole_clear_last_error_msg();
807+
const char *swoole_get_last_error_msg();
815808

816-
static inline int swoole_get_thread_id(void) {
809+
static inline int swoole_get_thread_id() {
817810
return SwooleTG.id;
818811
}
819812

820-
static inline int swoole_get_thread_type(void) {
813+
static inline int swoole_get_thread_type() {
821814
return SwooleTG.type;
822815
}
823816

include/swoole_async.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ class AsyncThreads {
103103
return task_num;
104104
}
105105

106-
size_t get_queue_size();
107-
size_t get_worker_num();
108-
void notify_one();
106+
size_t get_queue_size() const;
107+
size_t get_worker_num() const;
108+
void notify_one() const;
109109

110110
static int callback(Reactor *reactor, Event *event);
111111
};

include/swoole_atomic.h

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -44,65 +44,5 @@ typedef sw_atomic_uint32_t sw_atomic_t;
4444

4545
void sw_spinlock(sw_atomic_t *lock);
4646
#define sw_spinlock_release(lock) __sync_lock_release(lock)
47-
48-
#ifdef HAVE_FUTEX
49-
#include <linux/futex.h>
50-
#include <syscall.h>
51-
52-
static inline int sw_atomic_futex_wait(sw_atomic_t *atomic, double timeout) {
53-
if (sw_atomic_cmp_set(atomic, 1, 0)) {
54-
return 0;
55-
}
56-
57-
int ret;
58-
struct timespec _timeout;
59-
60-
if (timeout > 0) {
61-
_timeout.tv_sec = (long) timeout;
62-
_timeout.tv_nsec = (timeout - _timeout.tv_sec) * 1000 * 1000 * 1000;
63-
ret = syscall(SYS_futex, atomic, FUTEX_WAIT, 0, &_timeout, NULL, 0);
64-
} else {
65-
ret = syscall(SYS_futex, atomic, FUTEX_WAIT, 0, NULL, NULL, 0);
66-
}
67-
if (ret == 0 && sw_atomic_cmp_set(atomic, 1, 0)) {
68-
return 0;
69-
} else {
70-
return -1;
71-
}
72-
}
73-
74-
static inline int sw_atomic_futex_wakeup(sw_atomic_t *atomic, int n) {
75-
if (sw_atomic_cmp_set(atomic, 0, 1)) {
76-
return syscall(SYS_futex, atomic, FUTEX_WAKE, n, NULL, NULL, 0);
77-
} else {
78-
return 0;
79-
}
80-
}
81-
82-
#else
83-
static inline int sw_atomic_futex_wait(sw_atomic_t *atomic, double timeout) {
84-
if (sw_atomic_cmp_set(atomic, (sw_atomic_t) 1, (sw_atomic_t) 0)) {
85-
return 0;
86-
}
87-
timeout = timeout <= 0 ? INT_MAX : timeout;
88-
int32_t i = (int32_t) sw_atomic_sub_fetch(atomic, 1);
89-
while (timeout > 0) {
90-
if ((int32_t) *atomic > i) {
91-
return 0;
92-
} else {
93-
usleep(1000);
94-
timeout -= 0.001;
95-
}
96-
}
97-
sw_atomic_fetch_add(atomic, 1);
98-
return -1;
99-
}
100-
101-
static inline int sw_atomic_futex_wakeup(sw_atomic_t *atomic, int n) {
102-
if (1 == (int32_t) *atomic) {
103-
return 0;
104-
}
105-
sw_atomic_fetch_add(atomic, n);
106-
return 0;
107-
}
108-
#endif
47+
int sw_atomic_futex_wait(sw_atomic_t *atomic, double timeout);
48+
int sw_atomic_futex_wakeup(sw_atomic_t *atomic, int n);

include/swoole_base64.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
#pragma once
1818

19-
#include <stdint.h>
20-
#include <stdio.h>
21-
#include <string.h>
19+
#include <cstdint>
20+
#include <cstdio>
21+
#include <cstring>
2222

2323
#define BASE64_ENCODE_OUT_SIZE(s) (((s) + 2) / 3 * 4)
2424
#define BASE64_DECODE_OUT_SIZE(s) (((s)) / 4 * 3)

include/swoole_buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Buffer {
5858

5959
BufferChunk *alloc(BufferChunk::Type type, uint32_t size);
6060

61-
BufferChunk *front() {
61+
BufferChunk *front() const {
6262
return queue_.front();
6363
}
6464

0 commit comments

Comments
 (0)