From e0e5b7f0ba1b0440bdc2b557e2b2cfae24706cbd Mon Sep 17 00:00:00 2001 From: bfredl Date: Fri, 9 Feb 2024 11:42:40 +0100 Subject: [PATCH] refactor(api): make cstr_as_string accept "const char*" In the context a String inside an Object/Dictionary etc is consumed, it is considered to be read-only. --- src/nvim/api/private/helpers.c | 4 ++-- src/nvim/api/ui.c | 4 ++-- src/nvim/api/vim.c | 4 ++-- src/nvim/api/win_config.c | 6 +++--- src/nvim/autocmd.c | 4 ++-- src/nvim/decoration.c | 6 +++--- src/nvim/eval/funcs.c | 6 +++--- src/nvim/highlight.c | 2 +- src/nvim/highlight_group.c | 2 +- src/nvim/message.c | 2 +- src/nvim/msgpack_rpc/channel.c | 6 +++--- src/nvim/os/fs.c | 2 +- src/nvim/popupmenu.c | 2 +- src/nvim/sign.c | 2 +- src/nvim/ui.c | 5 ++--- src/nvim/window.c | 2 +- 16 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index cc95f46baf..8b45af7c71 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -425,12 +425,12 @@ String cstrn_as_string(char *str, size_t maxsize) /// @param str the C string to use /// @return The resulting String, or an empty String if /// str was NULL -String cstr_as_string(char *str) FUNC_ATTR_PURE +String cstr_as_string(const char *str) FUNC_ATTR_PURE { if (str == NULL) { return (String)STRING_INIT; } - return (String){ .data = str, .size = strlen(str) }; + return (String){ .data = (char *)str, .size = strlen(str) }; } /// Return the owned memory of a ga as a String diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c index f955b315a8..c6e868ca88 100644 --- a/src/nvim/api/ui.c +++ b/src/nvim/api/ui.c @@ -789,7 +789,7 @@ void remote_ui_hl_attr_define(UI *ui, Integer id, HlAttrs rgb_attrs, HlAttrs cte // system. So we add them here. if (rgb_attrs.url >= 0) { const char *url = hl_get_url((uint32_t)rgb_attrs.url); - PUT_C(rgb, "url", STRING_OBJ(cstr_as_string((char *)url))); + PUT_C(rgb, "url", CSTR_AS_OBJ(url)); } ADD_C(args, DICTIONARY_OBJ(rgb)); @@ -857,7 +857,7 @@ void remote_ui_put(UI *ui, const char *cell) UIData *data = ui->data; data->client_col++; Array args = data->call_buf; - ADD_C(args, CSTR_AS_OBJ((char *)cell)); + ADD_C(args, CSTR_AS_OBJ(cell)); push_call(ui, "put", args); } diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 1453de30d4..6b46f579e4 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -2289,7 +2289,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Arena * opts->use_winbar, stc_hl_id); PUT_C(hl_info, "start", INTEGER_OBJ(0)); - PUT_C(hl_info, "group", CSTR_AS_OBJ((char *)grpname)); + PUT_C(hl_info, "group", CSTR_AS_OBJ(grpname)); ADD_C(hl_values, DICTIONARY_OBJ(hl_info)); } @@ -2308,7 +2308,7 @@ Dictionary nvim_eval_statusline(String str, Dict(eval_statusline) *opts, Arena * snprintf(user_group, sizeof(user_group), "User%d", sp->userhl); grpname = arena_memdupz(arena, user_group, strlen(user_group)); } - PUT_C(hl_info, "group", CSTR_AS_OBJ((char *)grpname)); + PUT_C(hl_info, "group", CSTR_AS_OBJ(grpname)); ADD_C(hl_values, DICTIONARY_OBJ(hl_info)); } PUT_C(result, "highlights", ARRAY_OBJ(hl_values)); diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index a9d7a8d05c..8e299d264c 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -626,7 +626,7 @@ Dict(float_config) nvim_win_get_config(Window window, Arena *arena, Error *err) PUT_KEY_X(rv, bufpos, pos); } } - PUT_KEY_X(rv, anchor, cstr_as_string((char *)float_anchor_str[config->anchor])); + PUT_KEY_X(rv, anchor, cstr_as_string(float_anchor_str[config->anchor])); PUT_KEY_X(rv, row, config->row); PUT_KEY_X(rv, col, config->col); PUT_KEY_X(rv, zindex, config->zindex); @@ -659,12 +659,12 @@ Dict(float_config) nvim_win_get_config(Window window, Arena *arena, Error *err) PUT_KEY_X(rv, width, wp->w_width); PUT_KEY_X(rv, height, wp->w_height); WinSplit split = win_split_dir(wp); - PUT_KEY_X(rv, split, cstr_as_string((char *)win_split_str[split])); + PUT_KEY_X(rv, split, cstr_as_string(win_split_str[split])); } const char *rel = (wp->w_floating && !config->external ? float_relative_str[config->relative] : ""); - PUT_KEY_X(rv, relative, cstr_as_string((char *)rel)); + PUT_KEY_X(rv, relative, cstr_as_string(rel)); return rv; } diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index a385161beb..8c7831688f 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -121,7 +121,7 @@ static void augroup_map_del(int id, const char *name) { if (name != NULL) { String key; - map_del(String, int)(&map_augroup_name_to_id, cstr_as_string((char *)name), &key); + map_del(String, int)(&map_augroup_name_to_id, cstr_as_string(name), &key); api_free_string(key); } if (id > 0) { @@ -476,7 +476,7 @@ void augroup_del(char *name, bool stupid_legacy_mode) int augroup_find(const char *name) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { - int existing_id = map_get(String, int)(&map_augroup_name_to_id, cstr_as_string((char *)name)); + int existing_id = map_get(String, int)(&map_augroup_name_to_id, cstr_as_string(name)); if (existing_id == AUGROUP_DELETED) { return existing_id; } diff --git a/src/nvim/decoration.c b/src/nvim/decoration.c index a593b8eda1..9742a2020a 100644 --- a/src/nvim/decoration.c +++ b/src/nvim/decoration.c @@ -977,12 +977,12 @@ void decor_to_dict_legacy(Dictionary *dict, DecorInline decor, bool hl_name, Are } if (sh_hl.url != NULL) { - PUT_C(*dict, "url", STRING_OBJ(cstr_as_string((char *)sh_hl.url))); + PUT_C(*dict, "url", STRING_OBJ(cstr_as_string(sh_hl.url))); } if (virt_text) { if (virt_text->hl_mode) { - PUT_C(*dict, "hl_mode", CSTR_AS_OBJ((char *)hl_mode_str[virt_text->hl_mode])); + PUT_C(*dict, "hl_mode", CSTR_AS_OBJ(hl_mode_str[virt_text->hl_mode])); } Array chunks = virt_text_to_array(virt_text->data.virt_text, hl_name, arena); @@ -992,7 +992,7 @@ void decor_to_dict_legacy(Dictionary *dict, DecorInline decor, bool hl_name, Are if (virt_text->pos == kVPosWinCol) { PUT_C(*dict, "virt_text_win_col", INTEGER_OBJ(virt_text->col)); } - PUT_C(*dict, "virt_text_pos", CSTR_AS_OBJ((char *)virt_text_pos_str[virt_text->pos])); + PUT_C(*dict, "virt_text_pos", CSTR_AS_OBJ(virt_text_pos_str[virt_text->pos])); priority = virt_text->priority; } diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 655d6c9ab3..e4d2a219d9 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -2093,8 +2093,8 @@ static void f_feedkeys(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) flags = tv_get_string_buf(&argvars[1], nbuf); } - nvim_feedkeys(cstr_as_string((char *)keys), - cstr_as_string((char *)flags), true); + nvim_feedkeys(cstr_as_string(keys), + cstr_as_string(flags), true); } /// "filereadable()" function @@ -4528,7 +4528,7 @@ static void f_luaeval(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) return; } - nlua_typval_eval(cstr_as_string((char *)str), &argvars[1], rettv); + nlua_typval_eval(cstr_as_string(str), &argvars[1], rettv); } static void find_some_match(typval_T *const argvars, typval_T *const rettv, diff --git a/src/nvim/highlight.c b/src/nvim/highlight.c index 17d743784f..8b1c226851 100644 --- a/src/nvim/highlight.c +++ b/src/nvim/highlight.c @@ -135,7 +135,7 @@ void ui_send_all_hls(UI *ui) api_free_array(inspect); } for (size_t hlf = 0; hlf < HLF_COUNT; hlf++) { - remote_ui_hl_group_set(ui, cstr_as_string((char *)hlf_names[hlf]), + remote_ui_hl_group_set(ui, cstr_as_string(hlf_names[hlf]), highlight_attr[hlf]); } } diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c index 67b33e6ef7..a1ce206a2b 100644 --- a/src/nvim/highlight_group.c +++ b/src/nvim/highlight_group.c @@ -2241,7 +2241,7 @@ void highlight_changed(void) HlAttrs attrs = syn_attr2entry(highlight_attr[hlf]); msg_grid.blending = attrs.hl_blend > -1; } - ui_call_hl_group_set(cstr_as_string((char *)hlf_names[hlf]), + ui_call_hl_group_set(cstr_as_string(hlf_names[hlf]), highlight_attr[hlf]); highlight_attr_last[hlf] = highlight_attr[hlf]; } diff --git a/src/nvim/message.c b/src/nvim/message.c index cb89d34e23..991ed65ffe 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -3017,7 +3017,7 @@ void msg_ext_ui_flush(void) msg_ext_emit_chunk(); if (msg_ext_chunks.size > 0) { - ui_call_msg_show(cstr_as_string((char *)msg_ext_kind), + ui_call_msg_show(cstr_as_string(msg_ext_kind), msg_ext_chunks, msg_ext_overwrite); if (!msg_ext_overwrite) { msg_ext_visible++; diff --git a/src/nvim/msgpack_rpc/channel.c b/src/nvim/msgpack_rpc/channel.c index 0178ef622b..36fa7e77fc 100644 --- a/src/nvim/msgpack_rpc/channel.c +++ b/src/nvim/msgpack_rpc/channel.c @@ -547,7 +547,7 @@ static void send_error(Channel *chan, MsgpackRpcRequestHandler handler, MessageT static void send_request(Channel *channel, uint32_t id, const char *name, Array args) { - const String method = cstr_as_string((char *)name); + const String method = cstr_as_string(name); channel_write(channel, serialize_request(channel->id, id, method, @@ -558,7 +558,7 @@ static void send_request(Channel *channel, uint32_t id, const char *name, Array static void send_event(Channel *channel, const char *name, Array args) { - const String method = cstr_as_string((char *)name); + const String method = cstr_as_string(name); channel_write(channel, serialize_request(channel->id, 0, method, @@ -583,7 +583,7 @@ static void broadcast_event(const char *name, Array args) goto end; } - const String method = cstr_as_string((char *)name); + const String method = cstr_as_string(name); WBuffer *buffer = serialize_request(0, 0, method, diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 566d51f30a..ade745df2c 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -95,7 +95,7 @@ int os_chdir(const char *path) } int err = uv_chdir(path); if (err == 0) { - ui_call_chdir(cstr_as_string((char *)path)); + ui_call_chdir(cstr_as_string(path)); } return err; } diff --git a/src/nvim/popupmenu.c b/src/nvim/popupmenu.c index 0350ff6928..e24ebec458 100644 --- a/src/nvim/popupmenu.c +++ b/src/nvim/popupmenu.c @@ -488,7 +488,7 @@ void pum_redraw(void) if (ui_has(kUIMultigrid)) { const char *anchor = pum_above ? "SW" : "NW"; int row_off = pum_above ? -pum_height : 0; - ui_call_win_float_pos(pum_grid.handle, -1, cstr_as_string((char *)anchor), pum_anchor_grid, + ui_call_win_float_pos(pum_grid.handle, -1, cstr_as_string(anchor), pum_anchor_grid, pum_row - row_off, pum_left_col, false, pum_grid.zindex); } diff --git a/src/nvim/sign.c b/src/nvim/sign.c index dc09bcc5a0..5b4d4191b9 100644 --- a/src/nvim/sign.c +++ b/src/nvim/sign.c @@ -84,7 +84,7 @@ static int64_t group_get_ns(const char *group) return UINT32_MAX; // All namespaces } // Specific or non-existing namespace - int ns = map_get(String, int)(&namespace_ids, cstr_as_string((char *)group)); + int ns = map_get(String, int)(&namespace_ids, cstr_as_string(group)); return ns ? ns : -1; } diff --git a/src/nvim/ui.c b/src/nvim/ui.c index 2744f68951..ca0ec1e4ab 100644 --- a/src/nvim/ui.c +++ b/src/nvim/ui.c @@ -229,7 +229,7 @@ void ui_refresh(void) } ui_ext[i] = ext_widgets[i]; if (i < kUIGlobalCount) { - ui_call_option_set(cstr_as_string((char *)ui_ext_names[i]), + ui_call_option_set(cstr_as_string(ui_ext_names[i]), BOOLEAN_OBJ(ext_widgets[i])); } } @@ -451,8 +451,7 @@ void ui_set_ext_option(UI *ui, UIExtension ext, bool active) return; } if (ui_ext_names[ext][0] != '_' || active) { - remote_ui_option_set(ui, cstr_as_string((char *)ui_ext_names[ext]), - BOOLEAN_OBJ(active)); + remote_ui_option_set(ui, cstr_as_string(ui_ext_names[ext]), BOOLEAN_OBJ(active)); } if (ext == kUITermColors) { ui_default_colors_set(); diff --git a/src/nvim/window.c b/src/nvim/window.c index d07ae22c31..06d2bcac0d 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -796,7 +796,7 @@ void ui_ext_win_position(win_T *wp, bool validate) wp->w_grid_alloc.zindex = wp->w_float_config.zindex; if (ui_has(kUIMultigrid)) { - String anchor = cstr_as_string((char *)float_anchor_str[c.anchor]); + String anchor = cstr_as_string(float_anchor_str[c.anchor]); if (!c.hide) { ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor, grid->handle, row, col, c.focusable,