Skip to content

Commit

Permalink
refactor: fix readability-else-after-return lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 16, 2024
1 parent ec40d60 commit d003604
Show file tree
Hide file tree
Showing 25 changed files with 290 additions and 276 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Checks: >-
-performance-enum-size,
portability*,
readability*,
-readability-else-after-return,
-readability-function-cognitive-complexity,
-readability-identifier-length,
-readability-magic-numbers,
Expand Down
3 changes: 1 addition & 2 deletions src/back-merkle-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ back_merkle_tree::hash_type back_merkle_tree::get_root_hash() const {
}
}
return root;
} else {
return m_context[depth];
}
return m_context[depth];
}

back_merkle_tree::proof_type back_merkle_tree::get_next_leaf_proof() const {
Expand Down
8 changes: 3 additions & 5 deletions src/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ static size_t b64decode(uint8_t c, uint8_t *input, size_t size, std::ostringstre
if (b64unbase[c] > 64) {
if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r') { // ignore whitespace characters
return size;
} else { // throw an error for invalid characters
throw std::domain_error(std::string("invalid base64 character code ") + std::to_string(c));
}
} // throw an error for invalid characters
throw std::domain_error(std::string("invalid base64 character code ") + std::to_string(c));
}
input[size++] = c;
// decode atom
Expand Down Expand Up @@ -142,9 +141,8 @@ static size_t b64decode(uint8_t c, uint8_t *input, size_t size, std::ostringstre
sout << std::string_view(reinterpret_cast<char *>(decoded), valid);
return 0;
// need more data
} else {
return size;
}
return size;
}

std::string encode_base64(const std::string_view &input) {
Expand Down
4 changes: 2 additions & 2 deletions src/clint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ static execute_status clint_write(void *context, i_device_state_access *a, uint6
if ((val & 1) != 0) {
a->set_mip(MIP_MSIP_MASK);
return execute_status::success_and_serve_interrupts;
} else {
a->reset_mip(MIP_MSIP_MASK);
}
a->reset_mip(MIP_MSIP_MASK);

return execute_status::success;
}
return execute_status::failure;
Expand Down
27 changes: 13 additions & 14 deletions src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,20 @@ static int cartesi_mod_keccak(lua_State *L) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
lua_pushlstring(L, reinterpret_cast<const char *>(hash.data()), hash.size());
return 1;
} else {
h.begin();
size_t len1 = 0;
const char *hash1 = luaL_checklstring(L, 1, &len1);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
h.add_data(reinterpret_cast<const unsigned char *>(hash1), len1);
size_t len2 = 0;
const char *hash2 = luaL_optlstring(L, 2, "", &len2);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
h.add_data(reinterpret_cast<const unsigned char *>(hash2), len2);
h.end(hash);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
lua_pushlstring(L, reinterpret_cast<const char *>(hash.data()), hash.size());
return 1;
}
h.begin();
size_t len1 = 0;
const char *hash1 = luaL_checklstring(L, 1, &len1);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
h.add_data(reinterpret_cast<const unsigned char *>(hash1), len1);
size_t len2 = 0;
const char *hash2 = luaL_optlstring(L, 2, "", &len2);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
h.add_data(reinterpret_cast<const unsigned char *>(hash2), len2);
h.end(hash);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
lua_pushlstring(L, reinterpret_cast<const char *>(hash.data()), hash.size());
return 1;
}

static int cartesi_mod_tobase64(lua_State *L) try {
Expand Down
3 changes: 1 addition & 2 deletions src/complete-merkle-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ const complete_merkle_tree::hash_type &complete_merkle_tree::get_node_hash(addre
}
if (address < level.size()) {
return level[address];
} else {
return m_pristine.get_hash(log2_size);
}
return m_pristine.get_hash(log2_size);
}

void complete_merkle_tree::bubble_up() {
Expand Down
98 changes: 44 additions & 54 deletions src/interpret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,8 @@ static NO_INLINE std::pair<bool, uint64_t> read_virtual_memory_slow(STATE_ACCESS
const uint64_t hoffset = vaddr & PAGE_OFFSET_MASK;
a.read_memory_word(paddr, hpage, hoffset, pval);
return {true, pc};
} else if (likely(pma.get_istart_IO())) {
}
if (likely(pma.get_istart_IO())) {
const uint64_t offset = paddr - pma.get_start();
uint64_t val{};
// If we do not know how to read, we treat this as a PMA violation
Expand Down Expand Up @@ -917,7 +918,8 @@ static NO_INLINE std::pair<execute_status, uint64_t> write_virtual_memory_slow(S
const uint64_t hoffset = vaddr & PAGE_OFFSET_MASK;
a.write_memory_word(paddr, hpage, hoffset, static_cast<T>(val64));
return {execute_status::success, pc};
} else if (likely(pma.get_istart_IO())) {
}
if (likely(pma.get_istart_IO())) {
const uint64_t offset = paddr - pma.get_start();
auto status =
a.write_device(pma, mcycle, offset, static_cast<U>(static_cast<T>(val64)), log2_size<U>::value);
Expand Down Expand Up @@ -1192,9 +1194,8 @@ static FORCE_INLINE execute_status execute_AMOMIN_W(STATE_ACCESS &a, uint64_t &p
return execute_AMO<int32_t>(a, pc, mcycle, insn, [](int32_t valm, int32_t valr) -> int32_t {
if (valm < valr) {
return valm;
} else {
return valr;
}
return valr;
});
}

Expand All @@ -1205,9 +1206,8 @@ static FORCE_INLINE execute_status execute_AMOMAX_W(STATE_ACCESS &a, uint64_t &p
return execute_AMO<int32_t>(a, pc, mcycle, insn, [](int32_t valm, int32_t valr) -> int32_t {
if (valm > valr) {
return valm;
} else {
return valr;
}
return valr;
});
}

Expand All @@ -1218,9 +1218,8 @@ static FORCE_INLINE execute_status execute_AMOMINU_W(STATE_ACCESS &a, uint64_t &
return execute_AMO<int32_t>(a, pc, mcycle, insn, [](int32_t valm, int32_t valr) -> int32_t {
if (static_cast<uint32_t>(valm) < static_cast<uint32_t>(valr)) {
return valm;
} else {
return valr;
}
return valr;
});
}

Expand All @@ -1231,9 +1230,8 @@ static FORCE_INLINE execute_status execute_AMOMAXU_W(STATE_ACCESS &a, uint64_t &
return execute_AMO<int32_t>(a, pc, mcycle, insn, [](int32_t valm, int32_t valr) -> int32_t {
if (static_cast<uint32_t>(valm) > static_cast<uint32_t>(valr)) {
return valm;
} else {
return valr;
}
return valr;
});
}

Expand Down Expand Up @@ -1302,9 +1300,8 @@ static FORCE_INLINE execute_status execute_AMOMIN_D(STATE_ACCESS &a, uint64_t &p
return execute_AMO<int64_t>(a, pc, mcycle, insn, [](int64_t valm, int64_t valr) -> int64_t {
if (valm < valr) {
return valm;
} else {
return valr;
}
return valr;
});
}

Expand All @@ -1315,9 +1312,8 @@ static FORCE_INLINE execute_status execute_AMOMAX_D(STATE_ACCESS &a, uint64_t &p
return execute_AMO<int64_t>(a, pc, mcycle, insn, [](int64_t valm, int64_t valr) -> int64_t {
if (valm > valr) {
return valm;
} else {
return valr;
}
return valr;
});
}

Expand All @@ -1328,9 +1324,8 @@ static FORCE_INLINE execute_status execute_AMOMINU_D(STATE_ACCESS &a, uint64_t &
return execute_AMO<uint64_t>(a, pc, mcycle, insn, [](uint64_t valm, uint64_t valr) -> uint64_t {
if (valm < valr) {
return valm;
} else {
return valr;
}
return valr;
});
}

Expand All @@ -1341,9 +1336,8 @@ static FORCE_INLINE execute_status execute_AMOMAXU_D(STATE_ACCESS &a, uint64_t &
return execute_AMO<uint64_t>(a, pc, mcycle, insn, [](uint64_t valm, uint64_t valr) -> uint64_t {
if (valm > valr) {
return valm;
} else {
return valr;
}
return valr;
});
}

Expand Down Expand Up @@ -1433,11 +1427,11 @@ static FORCE_INLINE execute_status execute_DIVW(STATE_ACCESS &a, uint64_t &pc, u
auto rs2w = static_cast<int32_t>(rs2);
if (unlikely(rs2w == 0)) {
return static_cast<uint64_t>(-1);
} else if (unlikely(rs2w == -1 && rs1w == (static_cast<int32_t>(1) << (32 - 1)))) {
}
if (unlikely(rs2w == -1 && rs1w == (static_cast<int32_t>(1) << (32 - 1)))) {
return static_cast<uint64_t>(rs1w);
} else {
return static_cast<uint64_t>(rs1w / rs2w);
}
return static_cast<uint64_t>(rs1w / rs2w);
});
}

Expand All @@ -1450,9 +1444,8 @@ static FORCE_INLINE execute_status execute_DIVUW(STATE_ACCESS &a, uint64_t &pc,
auto rs2w = static_cast<uint32_t>(rs2);
if (unlikely(rs2w == 0)) {
return static_cast<uint64_t>(-1);
} else {
return static_cast<uint64_t>(static_cast<int32_t>(rs1w / rs2w));
}
return static_cast<uint64_t>(static_cast<int32_t>(rs1w / rs2w));
});
}

Expand All @@ -1468,11 +1461,11 @@ static FORCE_INLINE execute_status execute_REMW(STATE_ACCESS &a, uint64_t &pc, u
auto rs2w = static_cast<int32_t>(rs2);
if (unlikely(rs2w == 0)) {
return static_cast<uint64_t>(rs1w);
} else if (unlikely(rs2w == -1 && rs1w == (static_cast<int32_t>(1) << (32 - 1)))) {
}
if (unlikely(rs2w == -1 && rs1w == (static_cast<int32_t>(1) << (32 - 1)))) {
return static_cast<uint64_t>(0);
} else {
return static_cast<uint64_t>(rs1w % rs2w);
}
return static_cast<uint64_t>(rs1w % rs2w);
});
}

Expand All @@ -1491,9 +1484,8 @@ static FORCE_INLINE execute_status execute_REMUW(STATE_ACCESS &a, uint64_t &pc,
auto rs2w = static_cast<uint32_t>(rs2);
if (unlikely(rs2w == 0)) {
return static_cast<uint64_t>(static_cast<int32_t>(rs1w));
} else {
return static_cast<uint64_t>(static_cast<int32_t>(rs1w % rs2w));
}
return static_cast<uint64_t>(static_cast<int32_t>(rs1w % rs2w));
});
}

Expand Down Expand Up @@ -1524,9 +1516,8 @@ template <typename STATE_ACCESS>
static inline uint64_t read_csr_cycle(STATE_ACCESS &a, uint64_t mcycle, bool *status) {
if (rdcounteren(a, MCOUNTEREN_CY_MASK)) {
return read_csr_success(mcycle, status);
} else {
return read_csr_fail(status);
}
return read_csr_fail(status);
}

template <typename STATE_ACCESS>
Expand Down Expand Up @@ -2813,11 +2804,11 @@ static FORCE_INLINE execute_status execute_DIV(STATE_ACCESS &a, uint64_t &pc, ui
auto srs2 = static_cast<int64_t>(rs2);
if (unlikely(srs2 == 0)) {
return static_cast<uint64_t>(-1);
} else if (unlikely(srs2 == -1 && srs1 == (INT64_C(1) << (XLEN - 1)))) {
}
if (unlikely(srs2 == -1 && srs1 == (INT64_C(1) << (XLEN - 1)))) {
return static_cast<uint64_t>(srs1);
} else {
return static_cast<uint64_t>(srs1 / srs2);
}
return static_cast<uint64_t>(srs1 / srs2);
});
}

Expand All @@ -2828,9 +2819,8 @@ static FORCE_INLINE execute_status execute_DIVU(STATE_ACCESS &a, uint64_t &pc, u
return execute_arithmetic(a, pc, insn, [](uint64_t rs1, uint64_t rs2) -> uint64_t {
if (unlikely(rs2 == 0)) {
return static_cast<uint64_t>(-1);
} else {
return rs1 / rs2;
}
return rs1 / rs2;
});
}

Expand All @@ -2843,11 +2833,11 @@ static FORCE_INLINE execute_status execute_REM(STATE_ACCESS &a, uint64_t &pc, ui
auto srs2 = static_cast<int64_t>(rs2);
if (unlikely(srs2 == 0)) {
return srs1;
} else if (unlikely(srs2 == -1 && srs1 == (INT64_C(1) << (XLEN - 1)))) {
}
if (unlikely(srs2 == -1 && srs1 == (INT64_C(1) << (XLEN - 1)))) {
return 0;
} else {
return static_cast<uint64_t>(srs1 % srs2);
}
return static_cast<uint64_t>(srs1 % srs2);
});
}

Expand All @@ -2858,9 +2848,8 @@ static FORCE_INLINE execute_status execute_REMU(STATE_ACCESS &a, uint64_t &pc, u
return execute_arithmetic(a, pc, insn, [](uint64_t rs1, uint64_t rs2) -> uint64_t {
if (unlikely(rs2 == 0)) {
return rs1;
} else {
return rs1 % rs2;
}
return rs1 % rs2;
});
}

Expand Down Expand Up @@ -5610,13 +5599,12 @@ static NO_INLINE execute_status interpret_loop(STATE_ACCESS &a, uint64_t mcycle_
if (likely(status == execute_status::success_and_serve_interrupts)) {
// We have to break the inner loop to check and serve any pending interrupt immediately
break;
} else { // execute_status::success_and_yield or execute_status::success_and_halt
// Commit machine state
a.write_pc(pc);
a.write_mcycle(mcycle);
// Got an interruption that must be handled externally
return status;
}
} // execute_status::success_and_yield or execute_status::success_and_halt
// Commit machine state
a.write_pc(pc);
a.write_mcycle(mcycle);
// Got an interruption that must be handled externally
return status;
}
}
}
Expand Down Expand Up @@ -5669,16 +5657,18 @@ interpreter_break_reason interpret(STATE_ACCESS &a, uint64_t mcycle_end) {
// Detect and return the reason for stopping the interpreter loop
if (a.read_iflags_H()) {
return interpreter_break_reason::halted;
} else if (a.read_iflags_Y()) {
}
if (a.read_iflags_Y()) {
return interpreter_break_reason::yielded_manually;
} else if (a.read_iflags_X()) {
}
if (a.read_iflags_X()) {
return interpreter_break_reason::yielded_automatically;
} else if (status == execute_status::success_and_yield) {
return interpreter_break_reason::yielded_softly;
} else { // Reached mcycle_end
assert(a.read_mcycle() == mcycle_end); // LCOV_EXCL_LINE
return interpreter_break_reason::reached_target_mcycle;
}
if (status == execute_status::success_and_yield) {
return interpreter_break_reason::yielded_softly;
} // Reached mcycle_end
assert(a.read_mcycle() == mcycle_end); // LCOV_EXCL_LINE
return interpreter_break_reason::reached_target_mcycle;
}

#ifdef MICROARCHITECTURE
Expand Down
3 changes: 2 additions & 1 deletion src/jsonrpc-machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ cm_error cm_jsonrpc_spawn_server(const char *address, int detach_server, cm_json
exit(1);
};
return cm_result_success(); // code never reaches here
} else if (grand_child > 0) { // parent and double-fork() succeeded
}
if (grand_child > 0) { // parent and double-fork() succeeded
restore_grand_child = true; // make sure grand-child is killed if we fail
static THREAD_LOCAL std::string bound_address_storage = endpoint_to_string(a.local_endpoint());
a.close();
Expand Down
Loading

0 comments on commit d003604

Please sign in to comment.