From a81cc54025a15bc2c77a7d4eda58514fa9a2a87f Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 15 Apr 2025 08:48:41 +0800 Subject: [PATCH] =?UTF-8?q?change=EF=BC=9A=E6=94=B9=E4=B8=BAclangd?= =?UTF-8?q?=E5=BC=95=E6=93=8E=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 10 +-- .clang-tidy | 135 +++++++++++++++++++++++++++++++++++++ .clangd | 9 +++ .vscode/settings.json | 152 +++++------------------------------------- CMakeLists.txt | 1 + filecomplete | 2 +- server/server.cpp | 22 +++--- server/server.h | 15 ++--- util/util.cpp | 4 +- util/util.h | 22 +++--- 10 files changed, 190 insertions(+), 182 deletions(-) create mode 100644 .clang-tidy create mode 100644 .clangd diff --git a/.clang-format b/.clang-format index 89337b5..36e4997 100644 --- a/.clang-format +++ b/.clang-format @@ -6,20 +6,12 @@ BreakBeforeBraces: Custom BraceWrapping: AfterFunction: true AfterClass: true - AfterLambdaBody: false - BeforeLambdaBody: false Cpp11BracedListStyle: true ReflowComments: true SpacesBeforeTrailingComments: 3 TabWidth: 4 ConstructorInitializerAllOnOneLineOrOnePerLine: true -ColumnLimit: 110 +ColumnLimit: 130 AllowShortBlocksOnASingleLine: Never AllowShortFunctionsOnASingleLine: None AllowShortEnumsOnASingleLine: false -IncludeBlocks: Regroup -IncludeCategories: - - Regex: '^<.*>' - Priority: 1 - - Regex: '^".*"' - Priority: 2 \ No newline at end of file diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..1f932b1 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,135 @@ +Checks: "-*, + + clang-*, + + google-build-explicit-make-pair, + google-build-namespaces, + google-default-arguments, + google-explicit-constructor, + google-readability-avoid-underscore-in-googletest-name, + google-runtime-int, + google-runtime-operator, + + + -readability-non-const-parameter, + readability-avoid-const-params-in-decls, + readability-const-return-type, + readability-container-size-empty, + -readability-convert-member-functions-to-static, + readability-delete-null-pointer, + readability-deleted-default, + readability-make-member-function-const, + readability-misplaced-array-index, + readability-qualified-auto, + readability-redundant-control-flow, + readability-redundant-function-ptr-dereference, + readability-redundant-smartptr-get, + readability-redundant-string-cstr, + readability-static-definition-in-anonymous-namespace, + readability-string-compare, + readability-uniqueptr-delete-release, + readability-redundant-member-init, + readability-simplify-subscript-expr, + readability-simplify-boolean-expr, + readability-inconsistent-declaration-parameter-name, + + cert-dcl21-cpp, + cert-dcl50-cpp, + # 不要使用 system() + -cert-env33-c, + cert-err34-c, + cert-err52-cpp, + cert-flp30-c, + cert-mem57-cpp, + cert-msc50-cpp, + cert-oop58-cpp, + + modernize-avoid-bind, + modernize-deprecated-headers, + modernize-loop-convert, + modernize-make-shared, + modernize-make-unique, + -modernize-pass-by-value, + modernize-raw-string-literal, + modernize-redundant-void-arg, + modernize-replace-auto-ptr, + modernize-shrink-to-fit, + modernize-use-bool-literals, + modernize-use-emplace, + modernize-use-equals-delete, + modernize-use-nullptr, + modernize-use-override, + + cppcoreguidelines-pro-type-reinterpret-cast, + -cppcoreguidelines-narrowing-conversions, + cppcoreguidelines-pro-type-member-init, + + misc-unconventional-assign-operator, + -misc-unused-parameters, + misc-throw-by-value-catch-by-reference, + misc-misplaced-const, + misc-redundant-expression, + misc-static-assert, + misc-uniqueptr-reset-release, + -misc-unused-alias-decls, + -misc-unused-using-decls, + + bugprone-undelegated-constructor, + bugprone-macro-parentheses, + bugprone-macro-repeated-side-effects, + bugprone-forward-declaration-namespace, + bugprone-bool-pointer-implicit-conversion, + bugprone-misplaced-widening-cast, + bugprone-argument-comment, + bugprone-bad-signal-to-kill-thread, + bugprone-copy-constructor-init, + bugprone-dangling-handle, + bugprone-fold-init-type, + bugprone-inaccurate-erase, + bugprone-incorrect-roundings, + bugprone-integer-division, + bugprone-misplaced-operator-in-strlen-in-alloc, + bugprone-misplaced-pointer-arithmetic-in-alloc, + bugprone-move-forwarding-reference, + bugprone-multiple-statement-macro, + bugprone-parent-virtual-call, + bugprone-posix-return, + bugprone-reserved-identifier, + bugprone-signed-char-misuse, + bugprone-sizeof-container, + bugprone-sizeof-expression, + bugprone-string-constructor, + bugprone-string-integer-assignment, + bugprone-string-literal-with-embedded-nul, + bugprone-suspicious-enum-usage, + bugprone-suspicious-include, + bugprone-suspicious-memset-usage, + bugprone-suspicious-missing-comma, + bugprone-suspicious-string-compare, + bugprone-swapped-arguments, + bugprone-terminating-continue, + bugprone-throw-keyword-missing, + bugprone-too-small-loop-variable, + bugprone-undefined-memory-manipulation, + bugprone-unhandled-self-assignment, + bugprone-unused-raii, + bugprone-unused-return-value, + bugprone-use-after-move, + bugprone-virtual-near-miss, + + hicpp-exception-baseclass, + hicpp-ignored-remove-result, + hicpp-no-assembler, + -hicpp-signed-bitwise, + + performance-faster-string-find, + performance-for-range-copy, + performance-implicit-conversion-in-loop, + performance-inefficient-algorithm, + performance-inefficient-vector-operation, + performance-move-constructor-init, + performance-no-automatic-move, + -performance-trivially-destructible, + performance-unnecessary-copy-initialization' + +WarningsAsErrors: "*" diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..41d163f --- /dev/null +++ b/.clangd @@ -0,0 +1,9 @@ +Hover: + ShowAKA: true +Diagnostics: + UnusedIncludes: None # 禁用未使用头文件提示 + Suppress: + - anon_type_definition # 禁用匿名的typedef提示 + - unused-variable # 禁用未使用变量提示 + - unused-function # 禁用未使用函数提示 + - unused-includes # 禁用未使用的头文件提示 \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index a37fd74..d6f28a8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,6 @@ { "files.autoSave": "onFocusChange", "editor.fontSize": 14, - "editor.fontFamily": "'Source Code Pro', 'Source Code Pro', 'Source Code Pro'", - "terminal.integrated.fontFamily": "Source Code Pro", - "editor.fontLigatures": true, - //"C_Cpp.default.configurationProvider": "tboox.xmake-vscode", "cmake.configureOnOpen": true, "cmake.debugConfig": { "console": "integratedTerminal", @@ -19,147 +15,33 @@ "ignoreFailures": true } ], - "visualizerFile": "${workspaceRoot}/.vscode/qt5.natvis", "args": [ - //"-u", "0" + "9999" ] }, - "cmake.environment": { - "PATH": "${env:PATH};" + "cmake.configureSettings": { + //"CMAKE_TOOLCHAIN_FILE": "${env:VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" }, - "cmake.configureArgs": [ - "-Wno-dev", - "-DCMAKE_PREFIX_PATH:STRING=C:/dev/wxwigets", - "-DUSE_GUI=ON", - "-DUSE_TRANSM_TEST=ON" - ], "cmake.options.statusBarVisibility": "visible", "cmake.generator": "Ninja", - "cmake.ctestArgs": ["-V"], - "C_Cpp.default.compileCommands": "${workspaceRoot}/build/compile_commands.json", - "C_Cpp.default.cppStandard": "c++17", + "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", "editor.inlayHints.enabled": "off", "editor.unicodeHighlight.allowedLocales": { "ja": true, "zh-hant": true, "zh-hans": true }, - "files.associations": { - "xstring": "cpp", - "vector": "cpp", - "string": "cpp", - "algorithm": "cpp", - "any": "cpp", - "array": "cpp", - "atomic": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "chrono": "cpp", - "cinttypes": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "codecvt": "cpp", - "complex": "cpp", - "condition_variable": "cpp", - "csignal": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "exception": "cpp", - "filesystem": "cpp", - "forward_list": "cpp", - "fstream": "cpp", - "functional": "cpp", - "future": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "iterator": "cpp", - "limits": "cpp", - "list": "cpp", - "locale": "cpp", - "map": "cpp", - "memory": "cpp", - "mutex": "cpp", - "new": "cpp", - "numeric": "cpp", - "optional": "cpp", - "ostream": "cpp", - "queue": "cpp", - "random": "cpp", - "ratio": "cpp", - "set": "cpp", - "shared_mutex": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "string_view": "cpp", - "strstream": "cpp", - "system_error": "cpp", - "thread": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "unordered_map": "cpp", - "utility": "cpp", - "valarray": "cpp", - "variant": "cpp", - "xfacet": "cpp", - "xhash": "cpp", - "xiosbase": "cpp", - "xlocale": "cpp", - "xlocbuf": "cpp", - "xlocinfo": "cpp", - "xlocmes": "cpp", - "xlocmon": "cpp", - "xlocnum": "cpp", - "xloctime": "cpp", - "xmemory": "cpp", - "xmemory0": "cpp", - "xstddef": "cpp", - "xtr1common": "cpp", - "xtree": "cpp", - "xutility": "cpp", - "qbytearray": "cpp", - "*.ipp": "cpp", - "xthread": "cpp", - "*.tcc": "cpp", - "regex": "cpp", - "bit": "cpp", - "memory_resource": "cpp", - "source_location": "cpp", - "charconv": "cpp", - "compare": "cpp", - "concepts": "cpp", - "coroutine": "cpp", - "format": "cpp", - "stop_token": "cpp", - "expected": "cpp", - "numbers": "cpp", - "semaphore": "cpp", - "span": "cpp", - "text_encoding": "cpp", - "*.in": "cpp", - "hash_map": "cpp", - "stdfloat": "cpp", - "unordered_set": "cpp", - "cfenv": "cpp", - "cassert": "cpp", - "version": "cpp", - "resumable": "cpp", - "stack": "cpp" - }, - "makefile.configureOnOpen": false, - "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools" + "C_Cpp.intelliSenseEngine": "disabled", + "clangd.arguments": [ + "--header-insertion=never", + "--all-scopes-completion", + "--completion-style=detailed", + "-j=4", + "--clang-tidy", + "--pch-storage=memory", + "--compile-commands-dir=build", + "--background-index", + "--ranking-model=heuristics", + "--function-arg-placeholders=false" + ], } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index cf50b33..dd4ea31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ set(PROJECT_URL "https://www.sinxmiao.cn/taynpg/transm") set(COMPILER_ID ${CMAKE_CXX_COMPILER_ID}) if(MSVC) add_compile_options(/source-charset:utf-8) +add_definitions(-D_CRT_SECURE_NO_WARNINGS) endif() if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows") diff --git a/filecomplete b/filecomplete index 2a59b10..4b6612c 160000 --- a/filecomplete +++ b/filecomplete @@ -1 +1 @@ -Subproject commit 2a59b1066a076fc27157992aa4adbe39d2758cb5 +Subproject commit 4b6612cc63f21b4d092a0b5731ceb7f817f20d23 diff --git a/server/server.cpp b/server/server.cpp index bd12682..84549bf 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -82,7 +82,7 @@ void TransmServer::get_client_list(CMessageInfo& msg_info) std::string online_time; std::string uuid; std::string task; - uint64_t timestamp; + uint64_t timestamp{}; }; std::vector vec; std::string msg; @@ -101,8 +101,7 @@ void TransmServer::get_client_list(CMessageInfo& msg_info) } // 排序 vec 根据 client->timestamp - std::sort(vec.begin(), vec.end(), - [](const TmpInfo& a, const TmpInfo& b) { return a.timestamp < b.timestamp; }); + std::sort(vec.begin(), vec.end(), [](const TmpInfo& a, const TmpInfo& b) { return a.timestamp < b.timestamp; }); int index = 1; for (const auto& item : vec) { @@ -190,8 +189,7 @@ void TransmServer::trans_data(CFrameBuffer* buf) } } -bool TransmServer::check_double(CFrameBuffer* buf, std::shared_ptr& fcli, - std::shared_ptr& tcli) +bool TransmServer::check_double(CFrameBuffer* buf, std::shared_ptr& fcli, std::shared_ptr& tcli) { std::shared_lock lock(cli_mut_); if (client_map_.count(buf->fid_)) { @@ -255,8 +253,7 @@ void TransmServer::accept_client() }); } -void TransmServer::th_client(const std::shared_ptr& socket, - const std::string& client_key) +void TransmServer::th_client(const std::shared_ptr& socket, const std::string& client_key) { std::shared_ptr deleter(new int(0), [&](int* p) { std::unique_lock lock(cli_mut_); @@ -344,14 +341,13 @@ bool TransmServer::send_frame(const std::shared_ptr& sock } try { if (!socket->send(asio::buffer(out_buf, out_len))) { - TLOGE("{} send failed, buf type:{}, fid:{}, tid:{}", __FUNCTION__, static_cast(buf->type_), - buf->fid_, buf->tid_); + TLOGE("{} send failed, buf type:{}, fid:{}, tid:{}", __FUNCTION__, static_cast(buf->type_), buf->fid_, + buf->tid_); delete[] out_buf; return false; } } catch (const std::exception& e) { - TLOGE("send failed, type:{}, fid:{}, tid:{}, mark:{}", static_cast(buf->type_), buf->fid_, - buf->tid_, buf->mark_); + TLOGE("send failed, type:{}, fid:{}, tid:{}, mark:{}", static_cast(buf->type_), buf->fid_, buf->tid_, buf->mark_); } delete[] out_buf; @@ -369,9 +365,7 @@ void TransmServer::monitor_idle() std::unique_lock lock(cli_mut_); for (auto& item : client_map_) { auto now = std::chrono::high_resolution_clock::now(); - auto duration = - std::chrono::duration_cast(now - item.second->last_active_time_) - .count(); + auto duration = std::chrono::duration_cast(now - item.second->last_active_time_).count(); if (duration >= remove_after_time) { TLOGW("OnLine Time [{}] sec, Proactively disconnect:{}", duration, item.first); remove_vec.push_back(item.first); diff --git a/server/server.h b/server/server.h index 2804015..3f2cf52 100644 --- a/server/server.h +++ b/server/server.h @@ -13,12 +13,12 @@ using namespace ofen; using high_c = std::chrono::time_point; struct ClientCache { std::shared_ptr socket_; - CMutBuffer buffer_{}; + CMutBuffer buffer_; std::array tmp_buf_{}; - std::string task_{}; - std::string uuid{}; - std::string task_time_{}; - std::string online_time_{}; + std::string task_; + std::string uuid; + std::string task_time_; + std::string online_time_; uint64_t timestamp{}; high_c last_active_time_; FrameType cur_type_{TYPE_DEFAULT}; @@ -27,7 +27,7 @@ struct ClientCache { class TransmServer { public: - TransmServer(asio::io_context& io_context); + explicit TransmServer(asio::io_context& io_context); ~TransmServer(); public: @@ -39,8 +39,7 @@ private: private: void trans_data(CFrameBuffer* buf); - bool check_double(CFrameBuffer* buf, std::shared_ptr& fcli, - std::shared_ptr& tcli); + bool check_double(CFrameBuffer* buf, std::shared_ptr& fcli, std::shared_ptr& tcli); private: void accept_client(); diff --git a/util/util.cpp b/util/util.cpp index 2a5cae2..a6584d3 100644 --- a/util/util.cpp +++ b/util/util.cpp @@ -340,7 +340,7 @@ bool encrypt(const uint8_t* k, uint8_t* m, size_t len) rdm(nonce, sizeof(nonce) - 4); memcpy(m, nonce, kz); - struct AES_ctx ctx; + struct AES_ctx ctx{}; AES_init_ctx_iv(&ctx, k, nonce); AES_CTR_xcrypt_buffer(&ctx, m + kz, len - kz); return true; @@ -355,7 +355,7 @@ bool decrypt(const uint8_t* k, uint8_t* m, size_t len) uint8_t nonce[kz]{}; memcpy(nonce, m, kz); - struct AES_ctx ctx; + struct AES_ctx ctx{}; AES_init_ctx_iv(&ctx, k, nonce); AES_CTR_xcrypt_buffer(&ctx, m + kz, len - kz); return true; diff --git a/util/util.h b/util/util.h index 9436c52..b615ca4 100644 --- a/util/util.h +++ b/util/util.h @@ -44,7 +44,7 @@ enum FrameType : int16_t { // 此结构体成员顺序不可变动,涉及到序列化反序列化。 struct CMessageInfo { - CMessageInfo(const std::string& id); + explicit CMessageInfo(const std::string& id); CMessageInfo(const CMessageInfo& info); CMessageInfo& operator=(const CMessageInfo& info); std::string id; @@ -72,8 +72,8 @@ public: ~CFrameBuffer(); public: - std::string fid_{}; - std::string tid_{}; + std::string fid_; + std::string tid_; public: FrameType type_{}; @@ -113,8 +113,8 @@ inline std::string now_str() auto milliseconds = std::chrono::duration_cast(now.time_since_epoch()) % 1000; std::ostringstream timestamp; - timestamp << std::put_time(std::localtime(&time_t_now), "[%m-%d %H:%M:%S") << "." << std::setfill('0') - << std::setw(3) << milliseconds.count() << "] "; + timestamp << std::put_time(std::localtime(&time_t_now), "[%m-%d %H:%M:%S") << "." << std::setfill('0') << std::setw(3) + << milliseconds.count() << "] "; return timestamp.str(); } @@ -122,31 +122,27 @@ inline std::string now_str() template void TLOGI(const std::string& format, Args&&... args) { fc_lock_print(); - std::cout << ConsoleColor::Green << fmt::format(now_str() + format, std::forward(args)...) - << std::endl; + std::cout << ConsoleColor::Green << fmt::format(now_str() + format, std::forward(args)...) << std::endl; fc_unlock_print(); } template void TLOGW(const std::string& format, Args&&... args) { fc_lock_print(); - std::cout << ConsoleColor::Yellow << fmt::format(now_str() + format, std::forward(args)...) - << std::endl; + std::cout << ConsoleColor::Yellow << fmt::format(now_str() + format, std::forward(args)...) << std::endl; fc_unlock_print(); } template void TLOGE(const std::string& format, Args&&... args) { fc_lock_print(); - std::cout << ConsoleColor::Red << fmt::format(now_str() + format, std::forward(args)...) - << std::endl; + std::cout << ConsoleColor::Red << fmt::format(now_str() + format, std::forward(args)...) << std::endl; fc_unlock_print(); } template void TLOGD(const std::string& format, Args&&... args) { fc_lock_print(); - std::cout << ConsoleColor::Cyan << fmt::format(now_str() + format, std::forward(args)...) - << std::endl; + std::cout << ConsoleColor::Cyan << fmt::format(now_str() + format, std::forward(args)...) << std::endl; fc_unlock_print(); } \ No newline at end of file