From ef62dc2075ce2dd6029d710ab769caaa79172aac Mon Sep 17 00:00:00 2001 From: youmetme <321640253@qq.com> Date: Wed, 20 Aug 2025 16:06:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=92=8C=E7=B1=BB=E5=9E=8B=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-tidy | 92 ++++++++++++++++++++++++++++++ CMakeLists.txt | 1 + src/filter/logging-filter-substr.c | 30 ++++++---- src/handler/logging-handler-file.c | 17 ++++-- src/logging.c | 8 ++- 5 files changed, 127 insertions(+), 21 deletions(-) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..0837559 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,92 @@ +--- +Checks: '-*, + clang-analyzer-core.*, + clang-analyzer-cplusplus.*, + modernize-redundant-void-arg, + modernize-use-bool-literals, + modernize-use-equals-default, + modernize-use-nullptr, + modernize-use-override, + google-explicit-constructor, + google-readability-casting, + readability-braces-around-statements, + readability-identifier-naming.ClassCase, + readability-identifier-naming.StructCase, + readability-identifier-naming.TypedefCase, + readability-identifier-naming.EnumCase, + readability-non-const-parameter, + cert-dcl21-cpp, + bugprone-undelegated-constructor, + bugprone-macro-parentheses, + bugprone-macro-repeated-side-effects, + bugprone-forward-declaration-namespace, + bugprone-bool-pointer-implicit-conversion, + bugprone-misplaced-widening-cast, + cppcoreguidelines-narrowing-conversions, + misc-unconventional-assign-operator, + misc-unused-parameters' +WarningsAsErrors: '' +HeaderFilterRegex: '' +CheckOptions: + # 现代化(Modernize) + - key: modernize-redundant-void-arg + value: 'true' # 检查并移除函数声明中冗余的 void 参数。 + - key: modernize-use-bool-literals + value: 'true' # 建议使用布尔字面量 true 和 false 代替整数值 0 和 1。 + - key: modernize-use-equals-default + value: 'true' # 建议在默认构造函数、复制构造函数和赋值运算符中使用 = default,以简化代码。 + - key: modernize-use-nullptr + value: 'true' # 建议使用 nullptr 代替 NULL 或 0 来表示空指针。 + - key: modernize-use-override + value: 'true' # 建议在覆盖基类虚函数时使用 override 关键字,以增加代码的清晰性和安全性。 + + # Google 代码风格(Google) + - key: google-explicit-constructor + value: 'true' # 检查并建议在单参数构造函数中使用 explicit 关键字,以防止隐式转换。 + - key: google-readability-casting + value: 'true' # 检查并建议使用 C++ 风格的类型转换(如 static_cast、dynamic_cast、const_cast 和 reinterpret_cast)代替 C 风格的类型转换。 + + # 可读性(Readability) + - key: readability-braces-around-statements + value: 'true' # 建议在单行语句周围添加大括号,以提高代码的可读性和一致性。 + - key: readability-identifier-naming.ClassCase + value: 'CamelCase' # 类名应使用 CamelCase 风格,例如 MyClassName。 + - key: readability-identifier-naming.StructCase + value: 'CamelCase' # 结构体名应使用 CamelCase 风格,例如 MyStructName。 + - key: readability-identifier-naming.TypedefCase + value: 'CamelCase' # 类型定义应使用 CamelCase 风格,例如 MyTypeDef。 + - key: readability-identifier-naming.EnumCase + value: 'CamelCase' # 枚举名应使用 CamelCase 风格,例如 MyEnumName。 + - key: readability-non-const-parameter + value: 'true' # 检查并标识非 const 参数,以提高代码的可读性和安全性。 + + # CERT 安全编码标准(CERT) + - key: cert-dcl21-cpp + value: 'true' # 检查并标识在头文件中不应包含无命名空间的 using 声明和指令,以防止命名空间污染。 + + # Bug 检测(Bugprone) + - key: bugprone-undelegated-constructor + value: 'true' # 检查并标识未委托的构造函数,以确保构造函数的正确性。 + - key: bugprone-macro-parentheses + value: 'true' # 检查并建议在宏定义中使用括号,以防止潜在的错误。 + - key: bugprone-macro-repeated-side-effects + value: 'true' # 检查并标识宏中重复的副作用,以防止潜在的错误。 + - key: bugprone-forward-declaration-namespace + value: 'true' # 检查并标识命名空间前向声明的潜在问题。 + - key: bugprone-bool-pointer-implicit-conversion + value: 'true' # 检查并标识布尔指针的隐式转换,以防止潜在的错误。 + - key: bugprone-misplaced-widening-cast + value: 'true' # 检查并标识错误的宽化转换,以防止潜在的错误。 + + # C++ 核心指南(CppCoreGuidelines) + - key: cppcoreguidelines-narrowing-conversions + value: 'true' # 检查并标识可能导致数据丢失的窄化转换。 + + # 杂项(Miscellaneous) + - key: misc-unconventional-assign-operator + value: 'true' # 检查并标识不常见的赋值操作符重载,以确保代码的一致性和可维护性。 + - key: misc-unused-parameters + value: 'true' # 检测未使用的参数。 +... + + diff --git a/CMakeLists.txt b/CMakeLists.txt index 61dd1cb..5275266 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.28...3.30) project(logging) set(CMAKE_C_STANDARD 99) +set(CMAKE_C_CLANG_TIDY "clang-tidy") if(MSVC) add_compile_options("/source-charset:utf-8") diff --git a/src/filter/logging-filter-substr.c b/src/filter/logging-filter-substr.c index f8c66f7..8a23000 100644 --- a/src/filter/logging-filter-substr.c +++ b/src/filter/logging-filter-substr.c @@ -24,15 +24,17 @@ static void get_next(char *str, int *next) { } static bool kmp_search(char *substr, char *master) { - if (substr == NULL) + if (substr == NULL) { return true; // 空串全匹配 - if (master == NULL) + } + if (master == NULL) { return false; - int i = 0; - int j = 0; - int substrlen = strlen(substr); - int masterlen = strlen(master); - int *next = (int *)malloc(sizeof(int) * (substrlen + 1)); + } + int i = 0; + int j = 0; + size_t substrlen = strlen(substr); + size_t masterlen = strlen(master); + int *next = (int *)malloc(sizeof(int) * (substrlen + 1)); get_next(substr, next); while (i < masterlen && j < substrlen) { @@ -49,10 +51,11 @@ static bool kmp_search(char *substr, char *master) { } free(next); - if (j == substrlen) + if (j == substrlen) { return true; - else + } else { return false; + } } static bool _disposeSubstring(log_filter *filter, @@ -63,14 +66,16 @@ static bool _disposeSubstring(log_filter *filter, keywords_t *keyword = (keywords_t *)(filter + 1); if (keyword->key == NULL && keyword->next == NULL) { - if (level <= filter->level) + if (level <= filter->level) { return true; + } return false; } while (keyword != NULL && level <= filter->level) { - if (kmp_search(keyword->key, (char *)message)) + if (kmp_search(keyword->key, (char *)message)) { return true; + } keyword = keyword->next; } @@ -94,8 +99,9 @@ static void _freeFilter(log_filter *filter) { filter->handler->_free(filter->handler); } - if (it_keyword->key != NULL) + if (it_keyword->key != NULL) { free(it_keyword->key); + } free(filter); } diff --git a/src/handler/logging-handler-file.c b/src/handler/logging-handler-file.c index 5c7c2c4..505e4cd 100644 --- a/src/handler/logging-handler-file.c +++ b/src/handler/logging-handler-file.c @@ -7,7 +7,7 @@ // log_Handler_file_ex_t与log_Handler处于连续内存中 // 使用char*指针进行偏移,达到以偏移1个字节为单位的偏移 #define Handler_file_EX_PRT(handler) \ - ((log_Handler_file_ex_t *)((char *)handler + sizeof(log_Handler))) + ((log_Handler_file_ex_t *)((char *)(handler) + sizeof(log_Handler))) #define FILE_NAME_MAX_SIZE 50 @@ -46,9 +46,10 @@ static void outputFileHandler(log_Handler *handler, const char *message) { fputs(message, handler->stream); log_Handler_file_ex_t *handler_ex = Handler_file_EX_PRT(handler); handler_ex->file_size += strlen(message); - if (handler_ex->file_size > handler_ex->file_size_max) + if (handler_ex->file_size > handler_ex->file_size_max) { changeFile(handler); } +} log_Handler *loggingHandlerFile(const char *file_name, unsigned int max_size) { char new_file_name[FILE_NAME_MAX_SIZE]; @@ -62,24 +63,27 @@ log_Handler *loggingHandlerFile(const char *file_name, unsigned int max_size) { do { sprintf(new_file_name, "%s_%d.log", file_name, suffix++); fp = fopen(new_file_name, "at"); - if (fp == NULL) + if (fp == NULL) { goto ERROR; +} file_size = getFileSize(fp); } while (file_size > max_size); /// 分配log_Handler与记录文件大小的空间 handler = (log_Handler *)malloc(sizeof(log_Handler) + sizeof(log_Handler_file_ex_t)); - if (handler == NULL) + if (handler == NULL) { goto ERROR; +} handler_ex = Handler_file_EX_PRT(handler); handler_ex->file_size_max = max_size; handler_ex->file_size = file_size; handler_ex->suffix = suffix; handler_ex->file_name = strdup(file_name); - if (handler_ex->file_name == NULL) + if (handler_ex->file_name == NULL) { goto ERROR; +} handler->stream = fp; handler->apply_color = false; @@ -88,8 +92,9 @@ log_Handler *loggingHandlerFile(const char *file_name, unsigned int max_size) { return handler; ERROR: - if (fp) + if (fp) { fclose(fp); +} if (handler) { free(Handler_file_EX_PRT(handler)->file_name); // 直接释放,无需检查NULL free(handler); diff --git a/src/logging.c b/src/logging.c index 60222ad..45e925f 100644 --- a/src/logging.c +++ b/src/logging.c @@ -87,7 +87,7 @@ static void output_to_handler(log_Handler *handler, char timeStr[20]; getTimeStr(timeStr); char logStr[LOG_BUFFER_SIZE * 2]; - if (handler->apply_color) + if (handler->apply_color) { snprintf(logStr, LOG_BUFFER_SIZE * 2, "[%s]: %s %s%s%s %s\n", @@ -97,7 +97,7 @@ static void output_to_handler(log_Handler *handler, level, RESET, message); - else + } else { snprintf(logStr, LOG_BUFFER_SIZE * 2, "[%s]: %s %s %s\n", @@ -105,6 +105,7 @@ static void output_to_handler(log_Handler *handler, timeStr, level, message); + } handler->output(handler, logStr); } @@ -134,8 +135,9 @@ static void log_cope(log_level level_e, while (it != NULL) { if (it->_dispose(it, level_e, message)) { output_to_handler(it->handler, level, color, message); - if (it->jump_out) + if (it->jump_out) { return; + } } it = it->next; }