Skip to content

Commit

Permalink
Fix -bugprone-sizeof-expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltoli committed Jan 15, 2024
1 parent 817ab71 commit e8a4748
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Checks:
-bugprone-easily-swappable-parameters
-bugprone-narrowing-conversions
-bugprone-exception-escape
-bugprone-sizeof-expression
-bugprone-implicit-widening-of-multiplication-result
-bugprone-unchecked-optional-access # false positives

Expand Down
2 changes: 2 additions & 0 deletions runtime/io/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ list hook_KREFLECTION_argv() {

list l{};

// NOLINTBEGIN(*-sizeof-expression)
for (int i = 0; i < llvm_backend_argc; i++) {
stringbuffer *buf = hook_BUFFER_empty();
buf = hook_BUFFER_concat_raw(
Expand All @@ -628,6 +629,7 @@ list hook_KREFLECTION_argv() {
memcpy(b->children, &str, sizeof(str));
l = l.push_back(KElem(b));
}
// NOLINTEND(*-sizeof-expression)

return l;
}
Expand Down
25 changes: 10 additions & 15 deletions runtime/strings/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

extern "C" {

#define KCHAR char

mpz_ptr move_int(mpz_t);
floating *move_float(floating *);

Expand Down Expand Up @@ -85,9 +83,9 @@ SortString hook_STRING_chr(SortInt ord) {
KLLVM_HOOK_INVALID_ARGUMENT("Ord must be <= 255: {}", uord);
}
auto ret
= static_cast<string *>(koreAllocToken(sizeof(string) + sizeof(KCHAR)));
= static_cast<string *>(koreAllocToken(sizeof(string) + sizeof(char)));
init_with_len(ret, 1);
ret->data[0] = static_cast<KCHAR>(uord);
ret->data[0] = static_cast<char>(uord);
return ret;
}

Expand All @@ -113,10 +111,9 @@ SortInt hook_STRING_find(SortString haystack, SortString needle, SortInt pos) {
return move_int(result);
}
auto out = std::search(
haystack->data + upos * sizeof(KCHAR),
haystack->data + len(haystack) * sizeof(KCHAR), needle->data,
needle->data + len(needle) * sizeof(KCHAR));
int64_t ret = (out - haystack->data) / sizeof(KCHAR);
haystack->data + upos, haystack->data + len(haystack), needle->data,
needle->data + len(needle));
int64_t ret = out - haystack->data;
// search returns the end of the range if it is not found, but we want -1 in
// such a case.
auto res = (ret < len(haystack)) ? ret : -1;
Expand Down Expand Up @@ -150,10 +147,9 @@ hook_STRING_findChar(SortString haystack, SortString needle, SortInt pos) {
return move_int(result);
}
auto out = std::find_first_of(
haystack->data + upos * sizeof(KCHAR),
haystack->data + len(haystack) * sizeof(KCHAR), needle->data,
needle->data + len(needle) * sizeof(KCHAR));
int64_t ret = (out - haystack->data) / sizeof(KCHAR);
haystack->data + upos, haystack->data + len(haystack), needle->data,
needle->data + len(needle));
int64_t ret = out - haystack->data;
// search returns the end of the range if it is not found, but we want -1 in
// such a case.
auto res = (ret < len(haystack)) ? ret : -1;
Expand All @@ -180,7 +176,7 @@ hook_STRING_rfindChar(SortString haystack, SortString needle, SortInt pos) {
return move_int(result);
}

string *makeString(const KCHAR *input, ssize_t len = -1) {
string *makeString(const char *input, ssize_t len = -1) {
if (len == -1) {
len = strlen(input);
}
Expand Down Expand Up @@ -308,8 +304,7 @@ inline SortString hook_STRING_replace(
}
auto diff = len(needle) - len(replacer);
size_t new_len = len(haystack) - i * diff;
auto ret = static_cast<string *>(
koreAllocToken(sizeof(string) + new_len * sizeof(KCHAR)));
auto ret = static_cast<string *>(koreAllocToken(sizeof(string) + new_len));
init_with_len(ret, new_len);
int m = 0;
for (size_t r = 0, h = 0; r < new_len;) {
Expand Down

0 comments on commit e8a4748

Please sign in to comment.