aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval/userfunc.c
diff options
context:
space:
mode:
authordundargoc <gocdundar@gmail.com>2023-11-13 23:40:37 +0100
committerdundargoc <33953936+dundargoc@users.noreply.github.com>2023-11-19 15:08:35 +0100
commitac1113ded5f8f09dd99a9894d7a7e795626fb728 (patch)
tree9cf615d03efafe2c44e539cb45f1b3df171b3e85 /src/nvim/eval/userfunc.c
parent1798a4b5e9f0ae56cd800095f79423fea5cae8ca (diff)
downloadrneovim-ac1113ded5f8f09dd99a9894d7a7e795626fb728.tar.gz
rneovim-ac1113ded5f8f09dd99a9894d7a7e795626fb728.tar.bz2
rneovim-ac1113ded5f8f09dd99a9894d7a7e795626fb728.zip
refactor: follow style guide
- reduce variable scope - prefer initialization over declaration and assignment
Diffstat (limited to 'src/nvim/eval/userfunc.c')
-rw-r--r--src/nvim/eval/userfunc.c107
1 files changed, 35 insertions, 72 deletions
diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c
index 959dabafb5..822ac4d16a 100644
--- a/src/nvim/eval/userfunc.c
+++ b/src/nvim/eval/userfunc.c
@@ -274,14 +274,13 @@ int get_lambda_tv(char **arg, typval_T *rettv, evalarg_T *evalarg)
ufunc_T *fp = NULL;
partial_T *pt = NULL;
int varargs;
- int ret;
bool *old_eval_lavars = eval_lavars_used;
bool eval_lavars = false;
char *tofree = NULL;
// First, check if this is a lambda expression. "->" must exists.
char *s = skipwhite(*arg + 1);
- ret = get_function_args(&s, '-', NULL, NULL, NULL, true);
+ int ret = get_function_args(&s, '-', NULL, NULL, NULL, true);
if (ret == FAIL || *s != '>') {
return NOTDONE;
}
@@ -794,8 +793,6 @@ static void cleanup_function_call(funccall_T *fc)
/// @param[in] force When true, we are exiting.
static void funccal_unref(funccall_T *fc, ufunc_T *fp, bool force)
{
- int i;
-
if (fc == NULL) {
return;
}
@@ -810,7 +807,7 @@ static void funccal_unref(funccall_T *fc, ufunc_T *fp, bool force)
}
}
}
- for (i = 0; i < fc->fc_ufuncs.ga_len; i++) {
+ for (int i = 0; i < fc->fc_ufuncs.ga_len; i++) {
if (((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] == fp) {
((ufunc_T **)(fc->fc_ufuncs.ga_data))[i] = NULL;
}
@@ -1846,16 +1843,13 @@ char *trans_function_name(char **pp, bool skip, int flags, funcdict_T *fdp, part
FUNC_ATTR_NONNULL_ARG(1)
{
char *name = NULL;
- const char *start;
- const char *end;
- int lead;
int len;
lval_T lv;
if (fdp != NULL) {
CLEAR_POINTER(fdp);
}
- start = *pp;
+ const char *start = *pp;
// Check for hard coded <SNR>: already translated function ID (from a user
// command).
@@ -1867,14 +1861,14 @@ char *trans_function_name(char **pp, bool skip, int flags, funcdict_T *fdp, part
// A name starting with "<SID>" or "<SNR>" is local to a script. But
// don't skip over "s:", get_lval() needs it for "s:dict.func".
- lead = eval_fname_script(start);
+ int lead = eval_fname_script(start);
if (lead > 2) {
start += lead;
}
// Note that TFN_ flags use the same values as GLV_ flags.
- end = get_lval((char *)start, NULL, &lv, false, skip, flags | GLV_READ_ONLY,
- lead > 2 ? 0 : FNE_CHECK_START);
+ const char *end = get_lval((char *)start, NULL, &lv, false, skip, flags | GLV_READ_ONLY,
+ lead > 2 ? 0 : FNE_CHECK_START);
if (end == start) {
if (!skip) {
emsg(_("E129: Function name required"));
@@ -2134,10 +2128,7 @@ void ex_function(exarg_T *eap)
{
char *theline;
char *line_to_free = NULL;
- char c;
bool saved_wait_return = need_wait_return;
- char *name = NULL;
- char *p;
char *arg;
char *line_arg = NULL;
garray_T newargs;
@@ -2147,16 +2138,9 @@ void ex_function(exarg_T *eap)
int flags = 0;
ufunc_T *fp;
bool overwrite = false;
- int indent;
- int nesting;
- dictitem_T *v;
funcdict_T fudi;
static int func_nr = 0; // number for nameless function
- int paren;
hashtab_T *ht;
- hashitem_T *hi;
- linenr_T sourcing_lnum_off;
- linenr_T sourcing_lnum_top;
bool is_heredoc = false;
char *skip_until = NULL;
char *heredoc_trimmed = NULL;
@@ -2174,11 +2158,11 @@ void ex_function(exarg_T *eap)
// ":function /pat": list functions matching pattern.
if (*eap->arg == '/') {
- p = skip_regexp(eap->arg + 1, '/', true);
+ char *p = skip_regexp(eap->arg + 1, '/', true);
if (!eap->skip) {
regmatch_T regmatch;
- c = *p;
+ char c = *p;
*p = NUL;
regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
*p = c;
@@ -2209,9 +2193,9 @@ void ex_function(exarg_T *eap)
// "fudi.fd_di" set, "fudi.fd_newkey" == NULL
// s:func script-local function name
// g:func global function name, same as "func"
- p = eap->arg;
- name = save_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi);
- paren = (vim_strchr(p, '(') != NULL);
+ char *p = eap->arg;
+ char *name = save_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi);
+ int paren = (vim_strchr(p, '(') != NULL);
if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip) {
// Return on an invalid expression in braces, unless the expression
// evaluation has been cancelled due to an aborting error, an
@@ -2395,10 +2379,10 @@ void ex_function(exarg_T *eap)
}
// Save the starting line number.
- sourcing_lnum_top = SOURCING_LNUM;
+ linenr_T sourcing_lnum_top = SOURCING_LNUM;
- indent = 2;
- nesting = 0;
+ int indent = 2;
+ int nesting = 0;
while (true) {
if (KeyTyped) {
msg_scroll = true;
@@ -2442,7 +2426,7 @@ void ex_function(exarg_T *eap)
}
// Detect line continuation: SOURCING_LNUM increased more than one.
- sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
+ linenr_T sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
if (SOURCING_LNUM < sourcing_lnum_off) {
sourcing_lnum_off -= SOURCING_LNUM;
} else {
@@ -2641,7 +2625,7 @@ void ex_function(exarg_T *eap)
// If there are no errors, add the function
if (fudi.fd_dict == NULL) {
- v = find_var(name, strlen(name), &ht, false);
+ dictitem_T *v = find_var(name, strlen(name), &ht, false);
if (v != NULL && v->di_tv.v_type == VAR_FUNC) {
emsg_funcname(N_("E707: Function name conflicts with variable: %s"), name);
goto erret;
@@ -2706,16 +2690,13 @@ void ex_function(exarg_T *eap)
if (fp == NULL) {
if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL) {
- int slen, plen;
- char *scriptname;
-
// Check that the autoload name matches the script name.
int j = FAIL;
if (SOURCING_NAME != NULL) {
- scriptname = autoload_name(name, strlen(name));
+ char *scriptname = autoload_name(name, strlen(name));
p = vim_strchr(scriptname, '/');
- plen = (int)strlen(p);
- slen = (int)strlen(SOURCING_NAME);
+ int plen = (int)strlen(p);
+ int slen = (int)strlen(SOURCING_NAME);
if (slen > plen && path_fnamecmp(p, SOURCING_NAME + slen - plen) == 0) {
j = OK;
}
@@ -2753,7 +2734,7 @@ void ex_function(exarg_T *eap)
// insert the new function in the function list
set_ufunc_name(fp, name);
if (overwrite) {
- hi = hash_find(&func_hashtab, name);
+ hashitem_T *hi = hash_find(&func_hashtab, name);
hi->hi_key = UF2HIKEY(fp);
} else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL) {
xfree(fp);
@@ -2862,7 +2843,6 @@ char *get_user_func_name(expand_T *xp, int idx)
static size_t done;
static int changed;
static hashitem_T *hi;
- ufunc_T *fp;
if (idx == 0) {
done = 0;
@@ -2877,7 +2857,7 @@ char *get_user_func_name(expand_T *xp, int idx)
while (HASHITEM_EMPTY(hi)) {
hi++;
}
- fp = HI2UF(hi);
+ ufunc_T *fp = HI2UF(hi);
if ((fp->uf_flags & FC_DICT)
|| strncmp(fp->uf_name, "<lambda>", 8) == 0) {
@@ -2904,12 +2884,10 @@ char *get_user_func_name(expand_T *xp, int idx)
void ex_delfunction(exarg_T *eap)
{
ufunc_T *fp = NULL;
- char *p;
- char *name;
funcdict_T fudi;
- p = eap->arg;
- name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
+ char *p = eap->arg;
+ char *name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
xfree(fudi.fd_newkey);
if (name == NULL) {
if (fudi.fd_dict != NULL && !eap->skip) {
@@ -2986,13 +2964,11 @@ void ex_delfunction(exarg_T *eap)
/// becomes zero.
void func_unref(char *name)
{
- ufunc_T *fp = NULL;
-
if (name == NULL || !func_name_refcount(name)) {
return;
}
- fp = find_func(name);
+ ufunc_T *fp = find_func(name);
if (fp == NULL && isdigit((uint8_t)(*name))) {
#ifdef EXITFREE
if (!entered_free_all_mem) {
@@ -3028,12 +3004,10 @@ void func_ptr_unref(ufunc_T *fp)
/// Count a reference to a Function.
void func_ref(char *name)
{
- ufunc_T *fp;
-
if (name == NULL || !func_name_refcount(name)) {
return;
}
- fp = find_func(name);
+ ufunc_T *fp = find_func(name);
if (fp != NULL) {
(fp->uf_refcount)++;
} else if (isdigit((uint8_t)(*name))) {
@@ -3333,10 +3307,6 @@ void invoke_all_defer(void)
void ex_call(exarg_T *eap)
{
char *arg = eap->arg;
- char *startarg;
- char *name;
- char *tofree;
- int len;
bool failed = false;
funcdict_T fudi;
partial_T *partial = NULL;
@@ -3357,7 +3327,7 @@ void ex_call(exarg_T *eap)
return;
}
- tofree = trans_function_name(&arg, false, TFN_INT, &fudi, &partial);
+ char *tofree = trans_function_name(&arg, false, TFN_INT, &fudi, &partial);
if (fudi.fd_newkey != NULL) {
// Still need to give an error message for missing key.
semsg(_(e_dictkey), fudi.fd_newkey);
@@ -3376,13 +3346,13 @@ void ex_call(exarg_T *eap)
// If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
// contents. For VAR_PARTIAL get its partial, unless we already have one
// from trans_function_name().
- len = (int)strlen(tofree);
+ int len = (int)strlen(tofree);
bool found_var = false;
- name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial, false, &found_var);
+ char *name = deref_func_name(tofree, &len, partial != NULL ? NULL : &partial, false, &found_var);
// Skip white space to allow ":call func ()". Not good, but required for
// backward compatibility.
- startarg = skipwhite(arg);
+ char *startarg = skipwhite(arg);
if (*startarg != '(') {
semsg(_(e_missingparen), eap->arg);
@@ -3435,7 +3405,6 @@ end:
/// false when the return gets pending.
int do_return(exarg_T *eap, int reanimate, int is_cmd, void *rettv)
{
- int idx;
cstack_T *const cstack = eap->cstack;
if (reanimate) {
@@ -3447,7 +3416,7 @@ int do_return(exarg_T *eap, int reanimate, int is_cmd, void *rettv)
// not in its finally clause (which then is to be executed next) is found.
// In this case, make the ":return" pending for execution at the ":endtry".
// Otherwise, return normally.
- idx = cleanup_conditionals(eap->cstack, 0, true);
+ int idx = cleanup_conditionals(eap->cstack, 0, true);
if (idx >= 0) {
cstack->cs_pending[idx] = CSTP_RETURN;
@@ -3531,7 +3500,6 @@ char *get_func_line(int c, void *cookie, int indent, bool do_concat)
funccall_T *fcp = (funccall_T *)cookie;
ufunc_T *fp = fcp->fc_func;
char *retval;
- garray_T *gap; // growarray with function lines
// If breakpoints have been added/deleted need to check for it.
if (fcp->fc_dbg_tick != debug_tick) {
@@ -3542,7 +3510,7 @@ char *get_func_line(int c, void *cookie, int indent, bool do_concat)
func_line_end(cookie);
}
- gap = &fp->uf_lines;
+ garray_T *gap = &fp->uf_lines; // growarray with function lines
if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
|| fcp->fc_returned) {
retval = NULL;
@@ -3904,15 +3872,11 @@ bool set_ref_in_call_stack(int copyID)
/// Set "copyID" in all functions available by name.
bool set_ref_in_functions(int copyID)
{
- int todo;
- hashitem_T *hi = NULL;
- ufunc_T *fp;
-
- todo = (int)func_hashtab.ht_used;
- for (hi = func_hashtab.ht_array; todo > 0 && !got_int; hi++) {
+ int todo = (int)func_hashtab.ht_used;
+ for (hashitem_T *hi = func_hashtab.ht_array; todo > 0 && !got_int; hi++) {
if (!HASHITEM_EMPTY(hi)) {
todo--;
- fp = HI2UF(hi);
+ ufunc_T *fp = HI2UF(hi);
if (!func_name_refcount(fp->uf_name)
&& set_ref_in_func(NULL, fp, copyID)) {
return true;
@@ -3942,7 +3906,6 @@ bool set_ref_in_func_args(int copyID)
bool set_ref_in_func(char *name, ufunc_T *fp_in, int copyID)
{
ufunc_T *fp = fp_in;
- funccall_T *fc;
int error = FCERR_NONE;
char fname_buf[FLEN_FIXED + 1];
char *tofree = NULL;
@@ -3956,7 +3919,7 @@ bool set_ref_in_func(char *name, ufunc_T *fp_in, int copyID)
fp = find_func(fname);
}
if (fp != NULL) {
- for (fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped) {
+ for (funccall_T *fc = fp->uf_scoped; fc != NULL; fc = fc->fc_func->uf_scoped) {
abort = abort || set_ref_in_funccal(fc, copyID);
}
}