Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: format #56

Merged
merged 14 commits into from
Nov 29, 2023
16 changes: 15 additions & 1 deletion .github/workflows/pikiwidb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Pikiwidb
on:
push:
pull_request:
branches: [ "unstable" ]

jobs:
build_on_macos:
Expand All @@ -11,10 +12,19 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Install Deps
run: |
brew install clang-format

- name: Build
run: |
sh build.sh

- name: Check Format
working-directory: ${{ github.workspace }}/build
run: |
make check-format

- name: GTest
working-directory: ${{ github.workspace }}/build
# Execute tests defined by the CMake configuration.
Expand All @@ -31,8 +41,12 @@ jobs:
run: |
bash build.sh

- name: Check Format
working-directory: ${{ github.workspace }}/build
run: make check-format

- name: GTest
working-directory: ${{ github.workspace }}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest
run: ctest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ compile_commands.json

# build support
build_support/__pycache__
build_support/clang_format_exclusions.txt
17 changes: 13 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,33 +123,42 @@ ADD_SUBDIRECTORY(src)
SET(CMAKE_EXPORT_COMPILE_COMMANDS ON)

STRING(CONCAT FORMAT_DIRS "${PROJECT_SOURCE_DIR}/src,")
FILE(WRITE
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
"${PROJECT_SOURCE_DIR}/src/redis_intset.c\n${PROJECT_SOURCE_DIR}/src/redis_zip_list.c")
ADD_CUSTOM_TARGET(format
COMMAND ${BUILD_SUPPORT_DIR}/run_clang_format.py
/usr/bin/clang-format
${CLANG_FORMAT_BIN}
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
--source_dirs ${FORMAT_DIRS}
--quiet
--fix
)
ADD_CUSTOM_TARGET(check-format
COMMAND ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CLANG_FORMAT_BIN}
${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt
--source_dirs ${FORMAT_DIRS}
)

ADD_CUSTOM_TARGET(clang-tidy
COMMAND ${BUILD_SUPPORT_DIR}/run_clang_tidy.py
-clang-tidy-binary /usr/bin/clang-tidy
-clang-tidy-binary ${CLANG_TIDY_BIN}
-p ${CMAKE_BINARY_DIR}
-extra-arg=-std=c++17
)

ADD_CUSTOM_TARGET(clang-tidy-fix
COMMAND ${BUILD_SUPPORT_DIR}/run_clang_tidy.py
-clang-tidy-binary /usr/bin/clang-tidy
-clang-tidy-binary ${CLANG_TIDY_BIN}
-p ${CMAKE_BINARY_DIR}
-extra-arg=-std=c++17
-fix
)

FILE(GLOB_RECURSE LINT_FILES
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cc"
)
ADD_CUSTOM_TARGET(cpplint echo '${LINT_FILES}' | xargs -n12 -P8
${CPPLINT_BIN}
Expand Down
2 changes: 1 addition & 1 deletion build_support/run_clang_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def check(arguments, source_dir):
fullpaths = (os.path.join(directory, filename)
for filename in filenames)
source_files = [x for x in fullpaths
if x.endswith(".h") or x.endswith(".cpp")]
if x.endswith(".h") or x.endswith(".cpp") or x.endswith(".cc") or x.endswith(".c")]
formatted_filenames.extend(
# Filter out files that match the globs in the globs file
[filename for filename in source_files
Expand Down
11 changes: 10 additions & 1 deletion cmake/findTools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ ENDIF()

#set(CLANG_SEARCH_PATH "/usr/local/bin" "/usr/bin" "/usr/local/opt/llvm/bin"
# "/usr/local/opt/llvm@12/bin")
FIND_PROGRAM(CLANG_FORMAT_BIN
NAMES clang-format
HINTS ${CLANG_SEARCH_PATH})
IF("${CLANG_FORMAT_BIN}" STREQUAL "CLANG_FORMAT_BIN-NOTFOUND")
MESSAGE(WARNING "couldn't find clang-format.")
ELSE()
MESSAGE(STATUS "found clang-format at ${CLANG_FORMAT_BIN}")
ENDIF()

FIND_PROGRAM(CLANG_TIDY_BIN
NAMES clang-tidy clang-tidy-12
HINTS ${CLANG_SEARCH_PATH})
Expand Down Expand Up @@ -47,4 +56,4 @@ ELSE()
MESSAGE(STATUS "make DEBUG version")
ADD_DEFINITIONS(-DBUILD_DEBUG)
SET(BuildType "Debug")
ENDIF()
ENDIF()
4 changes: 1 addition & 3 deletions src/base_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ bool BaseCmd::CheckArg(size_t num) const {
return num >= -arity_;
}

std::vector<std::string> BaseCmd::CurrentKey(PClient* client) const {
return std::vector<std::string>{client->Key()};
}
std::vector<std::string> BaseCmd::CurrentKey(PClient* client) const { return std::vector<std::string>{client->Key()}; }

void BaseCmd::Execute(PClient* client) {
if (!DoInitial(client)) {
Expand Down
1 change: 0 additions & 1 deletion src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const std::string kCmdNameBitCount = "bitcount";

const std::string kCmdNameAuth = "auth";


enum CmdFlags {
CmdFlagsWrite = (1 << 0), // May modify the dataset
CmdFlagsReadonly = (1 << 1), // Doesn't modify the dataset
Expand Down
2 changes: 1 addition & 1 deletion src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ void PClient::FeedMonitors(const std::vector<std::string>& params) {
}
}
void PClient::SetKey(std::vector<std::string>& names) {
keys_ = std::move(names); // use std::move clear copy expense
keys_ = std::move(names); // use std::move clear copy expense
}

} // namespace pikiwidb
6 changes: 4 additions & 2 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
void SetSubCmdName(const std::string& name);
const std::string& SubCmdName() const { return subCmdName_; }
std::string FullCmdName() const; // the full name of the command, such as config set|get|rewrite
void SetKey(const std::string& name) { keys_.clear(); keys_.emplace_back(name); }
void SetKey(const std::string& name) {
keys_.clear();
keys_.emplace_back(name);
}
void SetKey(std::vector<std::string>& names);
const std::string& Key() const { return keys_.at(0); }
const std::vector<std::string>& Keys() const { return keys_; }
Expand Down Expand Up @@ -219,7 +222,6 @@ class PClient : public std::enable_shared_from_this<PClient>, public CmdRes {
std::string cmdName_; // suchAs config
std::vector<std::string> keys_;


// All parameters of this command (including the command itself)
// e.g:["set","key","value"]
std::vector<std::string> params_;
Expand Down
2 changes: 1 addition & 1 deletion src/cmd_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CmdConfig : public BaseCmdGroup {
bool DoInitial(PClient* client) override { return true; };

private:
// std::vector<std::string> subCmd_;
// std::vector<std::string> subCmd_;

void DoCmd(PClient* client) override{};
};
Expand Down
4 changes: 2 additions & 2 deletions src/cmd_kv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ BitCountCmd::BitCountCmd(const std::string& name, int16_t arity)

bool BitCountCmd::DoInitial(PClient* client) {
size_t paramSize = client->argv_.size();
if(paramSize != 2 && paramSize != 4) {
if (paramSize != 2 && paramSize != 4) {
client->SetRes(CmdRes::kSyntaxErr, kCmdNameBitCount);
return false;
}
Expand All @@ -188,7 +188,7 @@ void BitCountCmd::DoCmd(PClient* client) {
if (pstd::String2int(client->argv_[2], &start_offset_) == 0 ||
pstd::String2int(client->argv_[3], &end_offset_) == 0) {
client->SetRes(CmdRes::kInvalidInt);
return ;
return;
}

auto str = GetDecodedString(value);
Expand Down
3 changes: 1 addition & 2 deletions src/cmd_table_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "base_cmd.h"


namespace pikiwidb {

using CmdTable = std::unordered_map<std::string, std::unique_ptr<BaseCmd>>;
Expand All @@ -28,7 +27,7 @@ class CmdTableManager {

public:
void InitCmdTable();
std::pair<BaseCmd*,CmdRes::CmdRet> GetCommand(const std::string& cmdName, PClient* client);
std::pair<BaseCmd*, CmdRes::CmdRet> GetCommand(const std::string& cmdName, PClient* client);
// uint32_t DistributeKey(const std::string& key, uint32_t slot_num);
bool CmdExist(const std::string& cmd) const;
uint32_t GetCmdId();
Expand Down
4 changes: 2 additions & 2 deletions src/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

#include <map>
#include <vector>
#include "delegate.h"

#include "common.h"
#include "delegate.h"
#include "pstring.h"

namespace pikiwidb {
Expand Down Expand Up @@ -218,4 +219,3 @@ class PCommandTable {
};

} // namespace pikiwidb

3 changes: 2 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

#pragma once

#include <cstdio>
#include <strings.h>
#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <functional>
#include <vector>

#include "pstring.h"

#define CRLF "\r\n"
Expand Down
1 change: 0 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,3 @@ extern PConfig g_config;
extern bool LoadPikiwiDBConfig(const char* cfgFile, PConfig& cfg);

} // namespace pikiwidb

Loading