From 4f62461418bb54f78069a125eb57a629abff76cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20=C3=87etin?= Date: Fri, 2 Feb 2024 14:51:00 +0300 Subject: [PATCH 1/7] add local precommit files --- .pre-commit-config.yaml | 7 +++---- .pre-commit-hooks.yaml | 13 +++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 .pre-commit-hooks.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 158483b5c..6ead4c762 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,11 +33,10 @@ repos: rev: 23.12.1 hooks: - id: black - - repo: https://github.com/pocc/pre-commit-hooks - rev: v1.3.5 + - repo: local hooks: - - id: clang-format - - id: cppcheck + - id: clang-format-docker + - id: cppcheck-docker args: ["--std=c++14", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr"] - repo: https://github.com/cmake-lint/cmake-lint rev: 1.4.2 diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 000000000..df2dc9cc1 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,13 @@ +- id: clang-format-docker + name: clang-format + description: Run `clang-format` + language: docker_image + files: \.(h\+\+|h|hh|hxx|hpp|cuh|c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$ + entry: egecetinn/precommit:latest clang-format -i + +- id: cppcheck-docker + name: cppcheck + description: Run `cppcheck` + language: docker_image + files: \.(c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$ + entry: egecetinn/precommit:latest cppcheck --error-exitcode=1 From 57f599756782611992d92bb2f72330e8f57fb0f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20=C3=87etin?= Date: Fri, 2 Feb 2024 15:04:20 +0300 Subject: [PATCH 2/7] update --- .pre-commit-config.yaml | 10 ++++++++++ .pre-commit-hooks.yaml | 13 ------------- 2 files changed, 10 insertions(+), 13 deletions(-) delete mode 100644 .pre-commit-hooks.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6ead4c762..f5acf925a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -36,7 +36,17 @@ repos: - repo: local hooks: - id: clang-format-docker + name: clang-format + description: Run `clang-format` + language: docker_image + types_or: [c, c++] + entry: egecetinn/precommit:latest clang-format -i - id: cppcheck-docker + name: cppcheck + description: Run `cppcheck` + language: docker_image + types_or: [c, c++] + entry: egecetinn/precommit:latest cppcheck --error-exitcode=1 args: ["--std=c++14", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr"] - repo: https://github.com/cmake-lint/cmake-lint rev: 1.4.2 diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml deleted file mode 100644 index df2dc9cc1..000000000 --- a/.pre-commit-hooks.yaml +++ /dev/null @@ -1,13 +0,0 @@ -- id: clang-format-docker - name: clang-format - description: Run `clang-format` - language: docker_image - files: \.(h\+\+|h|hh|hxx|hpp|cuh|c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$ - entry: egecetinn/precommit:latest clang-format -i - -- id: cppcheck-docker - name: cppcheck - description: Run `cppcheck` - language: docker_image - files: \.(c|cc|cpp|cu|c\+\+|cxx|tpp|txx)$ - entry: egecetinn/precommit:latest cppcheck --error-exitcode=1 From 162e5fa3727c84941cff4e51366c40d147947b41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20=C3=87etin?= Date: Fri, 2 Feb 2024 15:52:08 +0300 Subject: [PATCH 3/7] update --- .github/workflows/build_and_test.yml | 14 ++++++++++++-- .pre-commit-config.yaml | 15 --------------- cppcheckSuppressions.txt | 4 +--- src/connection/Zeromq.cpp | 3 +-- src/metrics/PrometheusServer.cpp | 5 +---- src/telnet/TelnetServer.cpp | 4 ++-- 6 files changed, 17 insertions(+), 28 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 001535e43..a215b8875 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -6,7 +6,7 @@ on: pull_request: jobs: - pre-commit: + clang-tidy: runs-on: ubuntu-latest container: egecetinn/ubuntu2204 steps: @@ -16,12 +16,22 @@ jobs: submodules: recursive - name: Prepare run: git config --global --add safe.directory "$GITHUB_WORKSPACE" - - uses: pre-commit/action@v3.0.0 - name: Configure run: cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -S . -B build - name: Clang-tidy run: run-clang-tidy -j`nproc` -p=build -header-filter=`pwd`/include/ src/*.cpp src/**/*.cpp + format: + runs-on: ubuntu-latest + container: egecetinn/alpine + steps: + - name: Checkout Code + uses: actions/checkout@v4 + - name: Run clang-format + run: clang-format -i include/*.hpp include/**/*.hpp --verbose + - name: Run cppcheck + run: cppcheck -Iinclude/ src --verbose --enable=all --error-exitcode=1 --std=c++14 --language=c++ --suppressions-list=cppcheckSuppressions.txt --inline-suppr + coverage: runs-on: ubuntu-latest container: egecetinn/ubuntu2204 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f5acf925a..093b7a2b7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,21 +33,6 @@ repos: rev: 23.12.1 hooks: - id: black - - repo: local - hooks: - - id: clang-format-docker - name: clang-format - description: Run `clang-format` - language: docker_image - types_or: [c, c++] - entry: egecetinn/precommit:latest clang-format -i - - id: cppcheck-docker - name: cppcheck - description: Run `cppcheck` - language: docker_image - types_or: [c, c++] - entry: egecetinn/precommit:latest cppcheck --error-exitcode=1 - args: ["--std=c++14", "--language=c++", "--suppressions-list=cppcheckSuppressions.txt", "--inline-suppr"] - repo: https://github.com/cmake-lint/cmake-lint rev: 1.4.2 hooks: diff --git a/cppcheckSuppressions.txt b/cppcheckSuppressions.txt index ef7865444..4c5eb1da7 100644 --- a/cppcheckSuppressions.txt +++ b/cppcheckSuppressions.txt @@ -1,4 +1,2 @@ -*:thirdparty/* -*:tests/* missingInclude:* -unusedStructMember:* +unusedFunction:* diff --git a/src/connection/Zeromq.cpp b/src/connection/Zeromq.cpp index da07f7429..d55580eb8 100644 --- a/src/connection/Zeromq.cpp +++ b/src/connection/Zeromq.cpp @@ -27,9 +27,8 @@ void ZeroMQ::init(const std::shared_ptr &ctx, const zmq::socket_ socketPtr->set(zmq::sockopt::heartbeat_timeout, ZEROMQ_HEARTBEAT_TIMEOUT_MS); } -ZeroMQ::ZeroMQ(const zmq::socket_type &type, const std::string &addr, bool isBind) +ZeroMQ::ZeroMQ(const zmq::socket_type &type, const std::string &addr, bool isBind): contextPtr(std::make_shared(1)) { - contextPtr = std::make_shared(1); init(contextPtr, type, addr, isBind); } diff --git a/src/metrics/PrometheusServer.cpp b/src/metrics/PrometheusServer.cpp index 6518b0640..d126d70f5 100644 --- a/src/metrics/PrometheusServer.cpp +++ b/src/metrics/PrometheusServer.cpp @@ -7,11 +7,8 @@ #include -PrometheusServer::PrometheusServer(const std::string &serverAddr) +PrometheusServer::PrometheusServer(const std::string &serverAddr) : mainExposer(std::make_unique(serverAddr, 1)) { - // Init service - mainExposer = std::make_unique(serverAddr, 1); - auto reg = std::make_shared(); // Basic information diff --git a/src/telnet/TelnetServer.cpp b/src/telnet/TelnetServer.cpp index fc3359f97..ef629a046 100644 --- a/src/telnet/TelnetServer.cpp +++ b/src/telnet/TelnetServer.cpp @@ -375,7 +375,7 @@ bool TelnetSession::processCommandHistory(std::string &buffer) { if (m_historyCursor != m_history.begin()) { - m_historyCursor--; + --m_historyCursor; } buffer = *m_historyCursor; @@ -392,7 +392,7 @@ bool TelnetSession::processCommandHistory(std::string &buffer) { if (next(m_historyCursor) != m_history.end()) { - m_historyCursor++; + ++m_historyCursor; } buffer = *m_historyCursor; From 3368e1d691a8f1a4539b75589d1b68224b53a279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20=C3=87etin?= Date: Fri, 2 Feb 2024 15:53:30 +0300 Subject: [PATCH 4/7] ingnore missingIncludeSystem --- cppcheckSuppressions.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/cppcheckSuppressions.txt b/cppcheckSuppressions.txt index 4c5eb1da7..bd8a47722 100644 --- a/cppcheckSuppressions.txt +++ b/cppcheckSuppressions.txt @@ -1,2 +1,3 @@ missingInclude:* +missingIncludeSystem:* unusedFunction:* From f888885d11a17e275fe94e9e808ffff608cb274f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20=C3=87etin?= Date: Fri, 2 Feb 2024 19:26:56 +0300 Subject: [PATCH 5/7] update clang-format command --- .github/workflows/build_and_test.yml | 2 +- cppcheckSuppressions.txt | 1 + src/connection/Zeromq.cpp | 3 ++- src/metrics/PrometheusServer.cpp | 3 ++- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index a215b8875..f76afda67 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -28,7 +28,7 @@ jobs: - name: Checkout Code uses: actions/checkout@v4 - name: Run clang-format - run: clang-format -i include/*.hpp include/**/*.hpp --verbose + run: clang-format include/*.hpp include/**/*.hpp src/*.cpp src/**/*.cpp --verbose --dry-run --Werror - name: Run cppcheck run: cppcheck -Iinclude/ src --verbose --enable=all --error-exitcode=1 --std=c++14 --language=c++ --suppressions-list=cppcheckSuppressions.txt --inline-suppr diff --git a/cppcheckSuppressions.txt b/cppcheckSuppressions.txt index bd8a47722..696267434 100644 --- a/cppcheckSuppressions.txt +++ b/cppcheckSuppressions.txt @@ -1,3 +1,4 @@ missingInclude:* missingIncludeSystem:* +unmatchedSuppression:* unusedFunction:* diff --git a/src/connection/Zeromq.cpp b/src/connection/Zeromq.cpp index d55580eb8..7e1d65b65 100644 --- a/src/connection/Zeromq.cpp +++ b/src/connection/Zeromq.cpp @@ -27,7 +27,8 @@ void ZeroMQ::init(const std::shared_ptr &ctx, const zmq::socket_ socketPtr->set(zmq::sockopt::heartbeat_timeout, ZEROMQ_HEARTBEAT_TIMEOUT_MS); } -ZeroMQ::ZeroMQ(const zmq::socket_type &type, const std::string &addr, bool isBind): contextPtr(std::make_shared(1)) +ZeroMQ::ZeroMQ(const zmq::socket_type &type, const std::string &addr, bool isBind) + : contextPtr(std::make_shared(1)) { init(contextPtr, type, addr, isBind); } diff --git a/src/metrics/PrometheusServer.cpp b/src/metrics/PrometheusServer.cpp index d126d70f5..bb84d8b0c 100644 --- a/src/metrics/PrometheusServer.cpp +++ b/src/metrics/PrometheusServer.cpp @@ -7,7 +7,8 @@ #include -PrometheusServer::PrometheusServer(const std::string &serverAddr) : mainExposer(std::make_unique(serverAddr, 1)) +PrometheusServer::PrometheusServer(const std::string &serverAddr) + : mainExposer(std::make_unique(serverAddr, 1)) { auto reg = std::make_shared(); From 434e5a89976ea1882143a82649b5e45cf25926a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20=C3=87etin?= Date: Fri, 2 Feb 2024 19:39:49 +0300 Subject: [PATCH 6/7] fix initializer --- include/connection/Zeromq.hpp | 2 +- src/connection/Zeromq.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/include/connection/Zeromq.hpp b/include/connection/Zeromq.hpp index 651fe1168..f0f010198 100644 --- a/include/connection/Zeromq.hpp +++ b/include/connection/Zeromq.hpp @@ -5,7 +5,7 @@ class ZeroMQ { private: // Internal context - std::shared_ptr contextPtr; + std::shared_ptr contextPtr{std::make_shared(1)}; // Internal socket std::unique_ptr socketPtr; diff --git a/src/connection/Zeromq.cpp b/src/connection/Zeromq.cpp index 7e1d65b65..2b8051b4d 100644 --- a/src/connection/Zeromq.cpp +++ b/src/connection/Zeromq.cpp @@ -28,7 +28,6 @@ void ZeroMQ::init(const std::shared_ptr &ctx, const zmq::socket_ } ZeroMQ::ZeroMQ(const zmq::socket_type &type, const std::string &addr, bool isBind) - : contextPtr(std::make_shared(1)) { init(contextPtr, type, addr, isBind); } From 60496b697932e3bbc804471e4cb6d2ef64fc1aea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20=C3=87etin?= Date: Fri, 2 Feb 2024 19:42:32 +0300 Subject: [PATCH 7/7] fix format issues --- src/connection/Http.cpp | 4 ++-- src/connection/RawSocket.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/connection/Http.cpp b/src/connection/Http.cpp index 642fa0946..f37e1d275 100644 --- a/src/connection/Http.cpp +++ b/src/connection/Http.cpp @@ -6,7 +6,7 @@ void HTTP::setCommonFields(const std::string &fullURL, std::string &receivedData, CURLoption method) { curl_easy_setopt(curl, CURLOPT_URL, fullURL.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&receivedData); // Register user-supplied memory + curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast(&receivedData)); // Register user-supplied memory curl_easy_setopt(curl, method, 1L); } @@ -16,7 +16,7 @@ void HTTP::setCommonFields(const std::string &fullURL, std::string &receivedData curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, payload.size()); curl_easy_setopt(curl, CURLOPT_URL, fullURL.c_str()); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&receivedData); // Register user-supplied memory + curl_easy_setopt(curl, CURLOPT_WRITEDATA, static_cast(&receivedData)); // Register user-supplied memory curl_easy_setopt(curl, method, 1L); } diff --git a/src/connection/RawSocket.cpp b/src/connection/RawSocket.cpp index a0491f61c..2995bdfd4 100644 --- a/src/connection/RawSocket.cpp +++ b/src/connection/RawSocket.cpp @@ -28,7 +28,7 @@ void RawSocket::init(int domain, int type, int protocol, sockaddr_ll &_addr) RawSocket::RawSocket(std::string iface, bool isWrite) : writeMode(isWrite), iFace(std::move(iface)) { // Prepare socket address - memset((void *)&addr, 0, sizeof(sockaddr_ll)); + memset(static_cast(&addr), 0, sizeof(sockaddr_ll)); addr.sll_family = AF_PACKET; addr.sll_protocol = htons(ETH_P_ALL); addr.sll_ifindex = static_cast(if_nametoindex(iFace.c_str())); @@ -39,14 +39,14 @@ RawSocket::RawSocket(std::string iface, bool isWrite) : writeMode(isWrite), iFac // Interface request ifreq ifr{}; - memset((void *)&ifr, 0, sizeof(ifreq)); + memset(static_cast(&ifr), 0, sizeof(ifreq)); memcpy(std::addressof(ifr.ifr_name), iFace.c_str(), iFace.size()); // Size should be sufficient because if_nametoindex not failed if (isWrite) { init(PF_PACKET, SOCK_RAW, IPPROTO_RAW, addr); - if (setsockopt(sockFd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) + if (setsockopt(sockFd, SOL_SOCKET, SO_BINDTODEVICE, static_cast(&ifr), sizeof(ifr)) < 0) { throw std::runtime_error(std::string("Can't set socket options: ") + getErrnoString(errno)); }