Skip to content

Commit

Permalink
refactor: fix readability-implicit-bool-conversion lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 16, 2024
1 parent 007f3e8 commit b35d1cc
Show file tree
Hide file tree
Showing 37 changed files with 310 additions and 304 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Checks: >-
readability*,
-readability-else-after-return,
-readability-function-cognitive-complexity,
-readability-implicit-bool-conversion,
-readability-identifier-length,
-readability-magic-numbers,
WarningsAsErrors: >-
Expand Down
6 changes: 3 additions & 3 deletions src/back-merkle-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void back_merkle_tree::push_back(const hash_type &new_leaf_hash) {
}
const int depth = m_log2_root_size - m_log2_leaf_size;
for (int i = 0; i <= depth; ++i) {
if (m_leaf_count & (address_type{1} << i)) {
if ((m_leaf_count & (address_type{1} << i)) != 0) {
const auto &left = m_context[i];
get_concat_hash(h, left, right, right);
} else {
Expand Down Expand Up @@ -125,7 +125,7 @@ back_merkle_tree::hash_type back_merkle_tree::get_root_hash() const {
if (m_leaf_count < m_max_leaves) {
auto root = m_pristine_hashes.get_hash(m_log2_leaf_size);
for (int i = 0; i < depth; ++i) {
if (m_leaf_count & (address_type{1} << i)) {
if ((m_leaf_count & (address_type{1} << i)) != 0) {
const auto &left = m_context[i];
get_concat_hash(h, left, root, root);
} else {
Expand All @@ -150,7 +150,7 @@ back_merkle_tree::proof_type back_merkle_tree::get_next_leaf_proof() const {
proof.set_target_hash(m_pristine_hashes.get_hash(m_log2_leaf_size));
hash_type hash = m_pristine_hashes.get_hash(m_log2_leaf_size);
for (int i = 0; i < depth; ++i) {
if (m_leaf_count & (address_type{1} << i)) {
if ((m_leaf_count & (address_type{1} << i)) != 0) {
const auto &left = m_context[i];
proof.set_sibling_hash(left, m_log2_leaf_size + i);
get_concat_hash(h, left, hash, hash);
Expand Down
4 changes: 2 additions & 2 deletions src/clint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static bool clint_read_msip(i_device_state_access *a, uint64_t *val, int log2_si
"code expects msip0, mtimcmp, and mtime to be in different pages");

if (log2_size == 2) {
*val = ((a->read_mip() & MIP_MSIP_MASK) == MIP_MSIP_MASK);
*val = static_cast<uint64_t>((a->read_mip() & MIP_MSIP_MASK) == MIP_MSIP_MASK);
return true;
}
return false;
Expand Down Expand Up @@ -88,7 +88,7 @@ static execute_status clint_write(void *context, i_device_state_access *a, uint6
//??D I don't yet know why Linux tries to raise MSIP when we only have a single hart
// It does so repeatedly before and after every command run in the shell
// Will investigate.
if (val & 1) {
if ((val & 1) != 0) {
a->set_mip(MIP_MSIP_MASK);
return execute_status::success_and_serve_interrupts;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ static int cartesi_mod_keccak(lua_State *L) {
if (lua_gettop(L) < 1) {
luaL_argerror(L, 1, "too few arguments");
}
if (lua_isinteger(L, 1)) {
if (lua_isinteger(L, 1) != 0) {
if (lua_gettop(L) > 1) {
luaL_argerror(L, 2, "too many arguments");
}
Expand Down
16 changes: 8 additions & 8 deletions src/clua-i-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static int machine_obj_index_read_iflags_H(lua_State *L) {
if (cm_read_iflags_H(m.get(), &val) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
lua_pushboolean(L, val);
lua_pushboolean(L, static_cast<int>(val));
return 1;
}

Expand All @@ -114,7 +114,7 @@ static int machine_obj_index_read_iflags_Y(lua_State *L) {
if (cm_read_iflags_Y(m.get(), &val) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
lua_pushboolean(L, val);
lua_pushboolean(L, static_cast<int>(val));
return 1;
}

Expand All @@ -126,7 +126,7 @@ static int machine_obj_index_read_iflags_X(lua_State *L) {
if (cm_read_iflags_X(m.get(), &val) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
lua_pushboolean(L, val);
lua_pushboolean(L, static_cast<int>(val));
return 1;
}

Expand Down Expand Up @@ -229,7 +229,7 @@ static int machine_obj_index_read_uarch_halt_flag(lua_State *L) {
if (cm_read_uarch_halt_flag(m.get(), &val) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
lua_pushboolean(L, val);
lua_pushboolean(L, static_cast<int>(val));
return 1;
}

Expand Down Expand Up @@ -322,7 +322,7 @@ static int machine_obj_index_verify_dirty_page_maps(lua_State *L) {
if (cm_verify_dirty_page_maps(m.get(), &result) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
lua_pushboolean(L, result);
lua_pushboolean(L, static_cast<int>(result));
return 1;
}

Expand All @@ -334,7 +334,7 @@ static int machine_obj_index_verify_merkle_tree(lua_State *L) {
if (cm_verify_merkle_tree(m.get(), &result) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
lua_pushboolean(L, result);
lua_pushboolean(L, static_cast<int>(result));
return 1;
}

Expand Down Expand Up @@ -395,7 +395,7 @@ static int machine_obj_index_replace_memory_range(lua_State *L) {
auto &m = clua_check<clua_managed_cm_ptr<cm_machine>>(L, 1);
const uint64_t start = luaL_checkinteger(L, 2);
const uint64_t length = luaL_checkinteger(L, 3);
const bool shared = lua_toboolean(L, 4);
const bool shared = lua_toboolean(L, 4) != 0;
const char *image_filename = luaL_optstring(L, 5, nullptr);
if (cm_replace_memory_range(m.get(), start, length, shared, image_filename) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
Expand Down Expand Up @@ -546,7 +546,7 @@ static const auto machine_obj_index = cartesi::clua_make_luaL_Reg_array({
});

int clua_i_virtual_machine_init(lua_State *L, int ctxidx) {
if (!clua_typeexists<clua_managed_cm_ptr<cm_machine>>(L, ctxidx)) {
if (clua_typeexists<clua_managed_cm_ptr<cm_machine>>(L, ctxidx) == 0) {
clua_createtype<clua_managed_cm_ptr<cm_machine>>(L, "cartesi machine object", ctxidx);
clua_setmethods<clua_managed_cm_ptr<cm_machine>>(L, machine_obj_index.data(), 0, ctxidx);
}
Expand Down
4 changes: 2 additions & 2 deletions src/clua-jsonrpc-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static int jsonrpc_machine_ctor(lua_State *L) {
auto &managed_jsonrpc_connection = clua_check<clua_managed_cm_ptr<cm_jsonrpc_connection>>(L, conidx, ctxidx);
auto &managed_machine = clua_push_to(L, clua_managed_cm_ptr<cm_machine>(nullptr), ctxidx);
const char *runtime_config = !lua_isnil(L, 3) ? clua_check_json_string(L, 3, -1, ctxidx) : nullptr;
if (!lua_isstring(L, 2)) {
if (lua_isstring(L, 2) == 0) {
const char *config = clua_check_json_string(L, 2, -1, ctxidx);
if (cm_jsonrpc_create_machine(managed_jsonrpc_connection.get(), lua_toboolean(L, 4), config, runtime_config,
&managed_machine.get()) != 0) {
Expand Down Expand Up @@ -201,7 +201,7 @@ static int jsonrpc_connection_class_rebind_server(lua_State *L) {
if (cm_jsonrpc_rebind_server(managed_jsonrpc_connection.get(), address, &new_address) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
if (new_address) {
if (new_address != nullptr) {
lua_pushstring(L, new_address);
} else {
lua_pushnil(L);
Expand Down
22 changes: 11 additions & 11 deletions src/clua-machine-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ cm_reg clua_check_cm_proc_reg(lua_State *L, int idx) try {
}

void clua_check_cm_hash(lua_State *L, int idx, cm_hash *c_hash) {
if (lua_isstring(L, idx)) {
if (lua_isstring(L, idx) != 0) {
size_t len = 0;
const char *data = lua_tolstring(L, idx, &len);
if (len != sizeof(cm_hash)) {
Expand All @@ -242,10 +242,10 @@ static int64_t clua_get_array_table_len(lua_State *L, int tabidx) {
return -1;
}
int64_t len = 0;
lua_pushvalue(L, tabidx); // push table
lua_pushnil(L); // push key
while (lua_next(L, -2)) { // replace key, push value
if (!lua_isinteger(L, -2)) { // non integer key, not an array
lua_pushvalue(L, tabidx); // push table
lua_pushnil(L); // push key
while (lua_next(L, -2) != 0) { // replace key, push value
if (lua_isinteger(L, -2) == 0) { // non integer key, not an array
lua_pop(L, 3);
return -1;
}
Expand Down Expand Up @@ -288,10 +288,10 @@ static nlohmann::json &clua_push_json_value_ref(lua_State *L, int idx, int ctxid
}
} else { // object
j = nlohmann::json::object();
lua_pushvalue(L, idx); // push table
lua_pushnil(L); // push key
while (lua_next(L, -2)) { // update key, push value
if (!lua_isstring(L, -2)) {
lua_pushvalue(L, idx); // push table
lua_pushnil(L); // push key
while (lua_next(L, -2) != 0) { // update key, push value
if (lua_isstring(L, -2) == 0) {
luaL_error(L, "table maps cannot contain keys of type %s", lua_typename(L, lua_type(L, -2)));
}
const char *field_name = lua_tostring(L, -2);
Expand All @@ -304,7 +304,7 @@ static nlohmann::json &clua_push_json_value_ref(lua_State *L, int idx, int ctxid
break;
}
case LUA_TNUMBER: {
if (lua_isinteger(L, idx)) {
if (lua_isinteger(L, idx) != 0) {
int64_t v = lua_tointeger(L, idx);
if (schema.is_string() && schema.template get<std::string_view>() == "ArrayIndex") {
v -= 1;
Expand Down Expand Up @@ -415,7 +415,7 @@ static void clua_push_json_value(lua_State *L, const nlohmann::json &j, int ctxi
lua_pushnumber(L, j.template get<double>());
break;
case nlohmann::json::value_t::boolean:
lua_pushboolean(L, j.template get<bool>());
lua_pushboolean(L, static_cast<int>(j.template get<bool>()));
break;
case nlohmann::json::value_t::null:
lua_pushnil(L);
Expand Down
4 changes: 2 additions & 2 deletions src/clua-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static int machine_ctor(lua_State *L) {
lua_settop(L, 3);
auto &managed_machine = clua_push_to(L, clua_managed_cm_ptr<cm_machine>(nullptr));
const char *runtime_config = !lua_isnil(L, 3) ? clua_check_json_string(L, 3) : nullptr;
if (!lua_isstring(L, 2)) {
if (lua_isstring(L, 2) == 0) {
const char *config = clua_check_json_string(L, 2);
if (cm_create(config, runtime_config, &managed_machine.get()) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
Expand All @@ -127,7 +127,7 @@ int clua_machine_init(lua_State *L, int ctxidx) {
clua_createnewtype<clua_managed_cm_ptr<unsigned char>>(L, ctxidx);
clua_createnewtype<clua_managed_cm_ptr<std::string>>(L, ctxidx);
clua_createnewtype<clua_managed_cm_ptr<nlohmann::json>>(L, ctxidx);
if (!clua_typeexists<machine_class>(L, ctxidx)) {
if (clua_typeexists<machine_class>(L, ctxidx) == 0) {
clua_createtype<machine_class>(L, "cartesi machine class", ctxidx);
clua_setmethods<machine_class>(L, machine_class_index.data(), 0, ctxidx);
static const auto machine_class_meta = cartesi::clua_make_luaL_Reg_array({
Expand Down
2 changes: 1 addition & 1 deletion src/dtb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static std::string misa_to_isa_string(uint64_t misa) {
std::ostringstream ss;
ss << "rv64";
for (int i = 0; i < 26; i++) {
if (misa & (1 << i)) {
if ((misa & (1 << i)) != 0) {
ss << static_cast<char>('a' + i);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/htif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static bool htif_read(void *context, i_device_state_access *a, uint64_t offset,

static execute_status htif_halt(i_device_state_access *a, uint64_t cmd, uint64_t data) {
(void) a;
if (cmd == HTIF_HALT_CMD_HALT && (data & 1)) {
if (cmd == HTIF_HALT_CMD_HALT && ((data & 1) != 0)) {
a->set_iflags_H();
return execute_status::success_and_halt;
}
Expand All @@ -78,7 +78,7 @@ static execute_status htif_yield(i_device_state_access *a, uint64_t cmd, uint64_
(void) data;
execute_status status = execute_status::success;
// If yield command is enabled, yield and acknowledge
if (cmd < 64 && (a->read_htif_iyield() >> cmd) & 1) {
if (cmd < 64 && (((a->read_htif_iyield() >> cmd) & 1) != 0)) {
if (cmd == HTIF_YIELD_CMD_MANUAL) {
a->set_iflags_Y();
status = execute_status::success_and_yield;
Expand All @@ -96,12 +96,12 @@ static execute_status htif_yield(i_device_state_access *a, uint64_t cmd, uint64_
static execute_status htif_console(htif_runtime_config *runtime_config, i_device_state_access *a, uint64_t cmd,
uint64_t data) {
// If console command is enabled, perform it and acknowledge
if (cmd < 64 && (a->read_htif_iconsole() >> cmd) & 1) {
if (cmd < 64 && (((a->read_htif_iconsole() >> cmd) & 1) != 0)) {
if (cmd == HTIF_CONSOLE_CMD_PUTCHAR) {
const uint8_t ch = data & 0xff;
// In microarchitecture runtime_config will always be nullptr,
// therefore the HTIF runtime config is actually ignored.
if (!runtime_config || !runtime_config->no_console_putchar) {
if ((runtime_config == nullptr) || !runtime_config->no_console_putchar) {
os_putchar(ch);
}
a->write_htif_fromhost(HTIF_BUILD(HTIF_DEV_CONSOLE, cmd, 0, 0));
Expand Down
2 changes: 1 addition & 1 deletion src/interpret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ static inline uint32_t get_highest_priority_irq_num(uint32_t v) {
MIP_SEIP_MASK, MIP_SSIP_MASK, MIP_STIP_MASK // Supervisor interrupts
};
for (const uint32_t mask : interrupts_priority) {
if (v & mask) {
if ((v & mask) != 0) {
return ilog2(mask);
}
}
Expand Down
Loading

0 comments on commit b35d1cc

Please sign in to comment.