diff options
author | Lewis Russell <lewis6991@gmail.com> | 2022-09-08 14:43:22 +0100 |
---|---|---|
committer | Lewis Russell <lewis6991@gmail.com> | 2022-09-13 10:35:52 +0100 |
commit | 8eabbacf221da314ddeac9847476453d1c8eb2b1 (patch) | |
tree | 62a4afed3e6bdd49cb963c9529b1b9f892704e1e /src | |
parent | 907fc8ac373226556b84c2fdc4fe26525bbdb2c4 (diff) | |
download | rneovim-8eabbacf221da314ddeac9847476453d1c8eb2b1.tar.gz rneovim-8eabbacf221da314ddeac9847476453d1c8eb2b1.tar.bz2 rneovim-8eabbacf221da314ddeac9847476453d1c8eb2b1.zip |
refactor(memline): resolve clint errors
Co-authored-by: zeertzjq <zeertzjq@outlook.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/memline.c | 352 |
1 files changed, 130 insertions, 222 deletions
diff --git a/src/nvim/memline.c b/src/nvim/memline.c index b374010f80..13fc2b43ec 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -227,9 +227,6 @@ typedef enum { int ml_open(buf_T *buf) { bhdr_T *hp = NULL; - ZERO_BL *b0p; - PTR_BL *pp; - DATA_BL *dp; // init fields in memline struct buf->b_ml.ml_stack_size = 0; // no stack yet @@ -269,7 +266,7 @@ int ml_open(buf_T *buf) iemsg(_("E298: Didn't get block nr 0?")); goto error; } - b0p = hp->bh_data; + ZERO_BL *b0p = hp->bh_data; b0p->b0_id[0] = BLOCK0_ID0; b0p->b0_id[1] = BLOCK0_ID1; @@ -309,7 +306,7 @@ int ml_open(buf_T *buf) iemsg(_("E298: Didn't get block nr 1?")); goto error; } - pp = hp->bh_data; + PTR_BL *pp = hp->bh_data; pp->pb_count = 1; pp->pb_pointer[0].pe_bnum = 2; pp->pb_pointer[0].pe_page_count = 1; @@ -324,7 +321,7 @@ int ml_open(buf_T *buf) goto error; } - dp = hp->bh_data; + DATA_BL *dp = hp->bh_data; dp->db_index[0] = --dp->db_txt_start; // at end of block dp->db_free -= 1 + (unsigned)INDEX_SIZE; dp->db_line_count = 1; @@ -348,11 +345,8 @@ error: void ml_setname(buf_T *buf) { bool success = false; - memfile_T *mfp; - char *fname; - char *dirp; - mfp = buf->b_ml.ml_mfp; + memfile_T *mfp = buf->b_ml.ml_mfp; if (mfp->mf_fd < 0) { // there is no swap file yet // When 'updatecount' is 0 and 'noswapfile' there is no swap file. // For help files we will make a swap file now. @@ -363,13 +357,13 @@ void ml_setname(buf_T *buf) } // Try all directories in the 'directory' option. - dirp = p_dir; + char *dirp = p_dir; bool found_existing_dir = false; for (;;) { if (*dirp == NUL) { // tried all directories, fail break; } - fname = findswapname(buf, &dirp, mfp->mf_fname, &found_existing_dir); + char *fname = findswapname(buf, &dirp, mfp->mf_fname, &found_existing_dir); // alloc's fname if (dirp == NULL) { // out of memory break; @@ -432,11 +426,7 @@ void ml_open_files(void) /// and the memfile will be in memory only (no recovery possible). void ml_open_file(buf_T *buf) { - memfile_T *mfp; - char_u *fname; - char *dirp; - - mfp = buf->b_ml.ml_mfp; + memfile_T *mfp = buf->b_ml.ml_mfp; if (mfp == NULL || mfp->mf_fd >= 0 || !buf->b_p_swf || (cmdmod.cmod_flags & CMOD_NOSWAPFILE) || buf->terminal) { @@ -445,7 +435,7 @@ void ml_open_file(buf_T *buf) // For a spell buffer use a temp file name. if (buf->b_spell) { - fname = (char_u *)vim_tempname(); + char *fname = vim_tempname(); if (fname != NULL) { (void)mf_open_file(mfp, (char *)fname); // consumes fname! } @@ -454,7 +444,7 @@ void ml_open_file(buf_T *buf) } // Try all directories in 'directory' option. - dirp = p_dir; + char *dirp = p_dir; bool found_existing_dir = false; for (;;) { if (*dirp == NUL) { @@ -463,14 +453,14 @@ void ml_open_file(buf_T *buf) // There is a small chance that between choosing the swap file name // and creating it, another Vim creates the file. In that case the // creation will fail and we will use another directory. - fname = (char_u *)findswapname(buf, &dirp, NULL, &found_existing_dir); + char *fname = findswapname(buf, &dirp, NULL, &found_existing_dir); if (dirp == NULL) { break; // out of memory } if (fname == NULL) { continue; } - if (mf_open_file(mfp, (char *)fname) == OK) { // consumes fname! + if (mf_open_file(mfp, fname) == OK) { // consumes fname! ml_upd_block0(buf, UB_SAME_DIR); // Flush block zero, so others can read it @@ -586,15 +576,13 @@ static bool ml_check_b0_strings(ZERO_BL *b0p) /// Update the timestamp or the B0_SAME_DIR flag of the .swp file. static void ml_upd_block0(buf_T *buf, upd_block0_T what) { - memfile_T *mfp; bhdr_T *hp; - ZERO_BL *b0p; - mfp = buf->b_ml.ml_mfp; + memfile_T *mfp = buf->b_ml.ml_mfp; if (mfp == NULL || (hp = mf_get(mfp, 0, 1)) == NULL) { return; } - b0p = hp->bh_data; + ZERO_BL *b0p = hp->bh_data; if (ml_check_b0_id(b0p) == FAIL) { iemsg(_("E304: ml_upd_block0(): Didn't get block 0??")); } else { @@ -675,10 +663,9 @@ static void set_b0_dir_flag(ZERO_BL *b0p, buf_T *buf) /// When there is room, add the 'fileencoding' to block zero. static void add_b0_fenc(ZERO_BL *b0p, buf_T *buf) { - int n; - int size = B0_FNAME_SIZE_NOCRYPT; + const int size = B0_FNAME_SIZE_NOCRYPT; - n = (int)strlen(buf->b_p_fenc); + int n = (int)strlen(buf->b_p_fenc); if ((int)STRLEN(b0p->b0_fname) + n + 1 > size) { b0p->b0_flags &= (uint8_t) ~B0_HAS_FENC; } else { @@ -697,7 +684,6 @@ void ml_recover(bool checkext) { buf_T *buf = NULL; memfile_T *mfp = NULL; - char *fname; char *fname_used = NULL; bhdr_T *hp = NULL; ZERO_BL *b0p; @@ -706,37 +692,22 @@ void ml_recover(bool checkext) PTR_BL *pp; DATA_BL *dp; infoptr_T *ip; - blocknr_T bnum; - int len; bool directly; - linenr_T lnum; char *p; - int i; - long error; - bool cannot_open; - linenr_T line_count; - bool has_error; - int idx; - int top; - int txt_start; - off_T size; - int called_from_main; bool serious_error = true; - long mtime; - int attr; int orig_file_status = NOTDONE; recoverymode = true; - called_from_main = (curbuf->b_ml.ml_mfp == NULL); - attr = HL_ATTR(HLF_E); + int called_from_main = (curbuf->b_ml.ml_mfp == NULL); + int attr = HL_ATTR(HLF_E); // If the file name ends in ".s[a-w][a-z]" we assume this is the swap file. // Otherwise a search is done to find the swap file(s). - fname = curbuf->b_fname; + char *fname = curbuf->b_fname; if (fname == NULL) { // When there is no file name fname = ""; } - len = (int)strlen(fname); + int len = (int)strlen(fname); if (checkext && len >= 4 && STRNICMP(fname + len - 4, ".s", 2) == 0 && vim_strchr("abcdefghijklmnopqrstuvw", TOLOWER_ASC(fname[len - 2])) != NULL @@ -752,6 +723,7 @@ void ml_recover(bool checkext) semsg(_("E305: No swap file found for %s"), fname); goto theend; } + int i; if (len == 1) { // one swap file found, use it i = 1; } else { // several swap files found, choose @@ -857,6 +829,7 @@ void ml_recover(bool checkext) msg_end(); goto theend; } + off_T size; if ((size = vim_lseek(mfp->mf_fd, (off_T)0L, SEEK_END)) <= 0) { mfp->mf_blocknr_max = 0; // no file or empty file } else { @@ -894,7 +867,7 @@ void ml_recover(bool checkext) // check date of swap file and original file FileInfo org_file_info; FileInfo swp_file_info; - mtime = char_to_long(b0p->b0_mtime); + long mtime = char_to_long(b0p->b0_mtime); if (curbuf->b_ffname != NULL && os_fileinfo(curbuf->b_ffname, &org_file_info) && ((os_fileinfo(mfp->mf_fname, &swp_file_info) @@ -940,21 +913,17 @@ void ml_recover(bool checkext) } unchanged(curbuf, true, true); - bnum = 1; // start with block 1 + blocknr_T bnum = 1; // start with block 1 unsigned page_count = 1; // which is 1 page - lnum = 0; // append after line 0 in curbuf - line_count = 0; - idx = 0; // start with first index in block 1 - error = 0; + linenr_T lnum = 0; // append after line 0 in curbuf + linenr_T line_count = 0; + int idx = 0; // start with first index in block 1 + long error = 0; buf->b_ml.ml_stack_top = 0; // -V1048 buf->b_ml.ml_stack = NULL; buf->b_ml.ml_stack_size = 0; // -V1048 - if (curbuf->b_ffname == NULL) { - cannot_open = true; - } else { - cannot_open = false; - } + bool cannot_open = (curbuf->b_ffname == NULL); serious_error = false; for (; !got_int; line_breakcheck()) { @@ -975,7 +944,7 @@ void ml_recover(bool checkext) if (pp->pb_id == PTR_ID) { // it is a pointer block // check line count when using pointer block first time if (idx == 0 && line_count != 0) { - for (i = 0; i < (int)pp->pb_count; i++) { + for (int i = 0; i < (int)pp->pb_count; i++) { line_count -= pp->pb_pointer[i].pe_line_count; } if (line_count != 0) { @@ -1014,7 +983,7 @@ void ml_recover(bool checkext) } // going one block deeper in the tree - top = ml_add_stack(buf); // new entry in stack + int top = ml_add_stack(buf); // new entry in stack ip = &(buf->b_ml.ml_stack[top]); ip->ip_bnum = bnum; ip->ip_index = idx; @@ -1039,7 +1008,7 @@ void ml_recover(bool checkext) } else { // it is a data block // Append all the lines in this block - has_error = false; + bool has_error = false; // check length of block // if wrong, use length in pointer block if (page_count * mfp->mf_page_size != dp->db_txt_end) { @@ -1065,8 +1034,8 @@ void ml_recover(bool checkext) has_error = true; } - for (i = 0; i < dp->db_line_count; i++) { - txt_start = (dp->db_index[i] & DB_INDEX_MASK); + for (int i = 0; i < dp->db_line_count; i++) { + int txt_start = (dp->db_index[i] & DB_INDEX_MASK); if (txt_start <= (int)HEADER_SIZE || txt_start >= (int)dp->db_txt_end) { p = "???"; @@ -1110,7 +1079,7 @@ void ml_recover(bool checkext) for (idx = 1; idx <= lnum; idx++) { // Need to copy one line, fetching the other one may flush it. p = xstrdup(ml_get(idx)); - i = strcmp(p, ml_get(idx + lnum)); + int i = strcmp(p, ml_get(idx + lnum)); xfree(p); if (i != 0) { changed_internal(); @@ -1191,12 +1160,9 @@ int recover_names(char_u *fname, int list, int nr, char **fname_out) int num_names; char *(names[6]); char_u *tail; - char_u *p; - int num_files; + char *p; int file_count = 0; char **files; - char *dirp; - char_u *dir_name; char_u *fname_res = NULL; #ifdef HAVE_READLINK char_u fname_buf[MAXPATHL]; @@ -1221,8 +1187,8 @@ int recover_names(char_u *fname, int list, int nr, char **fname_out) // Do the loop for every directory in 'directory'. // First allocate some memory to put the directory name in. - dir_name = xmalloc(strlen(p_dir) + 1); - dirp = p_dir; + char *dir_name = xmalloc(strlen(p_dir) + 1); + char *dirp = p_dir; while (*dirp) { // Isolate a directory name from *dirp and put it in dir_name (we know // it is large enough, so use 31000 for length). @@ -1266,6 +1232,7 @@ int recover_names(char_u *fname, int list, int nr, char **fname_out) } } + int num_files; if (num_names == 0) { num_files = 0; } else if (expand_wildcards(num_names, names, &num_files, &files, @@ -1291,7 +1258,7 @@ int recover_names(char_u *fname, int list, int nr, char **fname_out) // remove swapfile name of the current buffer, it must be ignored if (curbuf->b_ml.ml_mfp != NULL - && (p = (char_u *)curbuf->b_ml.ml_mfp->mf_fname) != NULL) { + && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL) { for (int i = 0; i < num_files; i++) { // Do not expand wildcards, on Windows would try to expand // "%tmp%" in "%tmp%file" @@ -1440,9 +1407,12 @@ static time_t swapfile_info(char_u *fname) msg_puts(_(" owned by: ")); msg_outtrans(uname); msg_puts(_(" dated: ")); - } else -#endif + } else { + msg_puts(_(" dated: ")); + } +#else msg_puts(_(" dated: ")); +#endif x = file_info.stat.st_mtim.tv_sec; char ctime_buf[50]; msg_puts(os_ctime_r(&x, ctime_buf, sizeof(ctime_buf))); @@ -1513,7 +1483,6 @@ static time_t swapfile_info(char_u *fname) static bool swapfile_unchanged(char *fname) { struct block0 b0; - int ret = true; // Swap file must exist. if (!os_path_exists(fname)) { @@ -1530,6 +1499,8 @@ static bool swapfile_unchanged(char *fname) return false; } + bool ret = true; + // the ID and magic number must be correct if (ml_check_b0_id(&b0) == FAIL || b0_magic_wrong(&b0)) { ret = false; @@ -1637,10 +1608,7 @@ void ml_sync_all(int check_file, int check_char, bool do_fsync) /// @param message if true, the success of preserving is reported. void ml_preserve(buf_T *buf, int message, bool do_fsync) { - bhdr_T *hp; - linenr_T lnum; memfile_T *mfp = buf->b_ml.ml_mfp; - int status; int got_int_save = got_int; if (mfp == NULL || mfp->mf_fname == NULL) { @@ -1656,7 +1624,7 @@ void ml_preserve(buf_T *buf, int message, bool do_fsync) ml_flush_line(buf); // flush buffered line (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); // flush locked block - status = mf_sync(mfp, MFS_ALL | (do_fsync ? MFS_FLUSH : 0)); + int status = mf_sync(mfp, MFS_ALL | (do_fsync ? MFS_FLUSH : 0)); // stack is invalid after mf_sync(.., MFS_ALL) buf->b_ml.ml_stack_top = 0; @@ -1672,9 +1640,9 @@ void ml_preserve(buf_T *buf, int message, bool do_fsync) // ml_find_line() does the work by translating the negative block numbers // when getting the first line of each data block. if (mf_need_trans(mfp) && !got_int) { - lnum = 1; + linenr_T lnum = 1; while (mf_need_trans(mfp) && lnum <= buf->b_ml.ml_line_count) { - hp = ml_find_line(buf, lnum, ML_FIND); + bhdr_T *hp = ml_find_line(buf, lnum, ML_FIND); if (hp == NULL) { status = FAIL; goto theend; @@ -1740,9 +1708,6 @@ int gchar_pos(pos_T *pos) char *ml_get_buf(buf_T *buf, linenr_T lnum, bool will_change) FUNC_ATTR_NONNULL_ALL { - bhdr_T *hp; - DATA_BL *dp; - char *ptr; static int recursive = 0; static char questions[4]; @@ -1779,6 +1744,7 @@ errorret: // Find the data block containing the line. // This also fills the stack with the blocks from the root to the data // block and releases any locked block. + bhdr_T *hp; if ((hp = ml_find_line(buf, lnum, ML_FIND)) == NULL) { if (recursive == 0) { // Avoid giving this message for a recursive call, may happen @@ -1793,10 +1759,9 @@ errorret: goto errorret; } - dp = hp->bh_data; + DATA_BL *dp = hp->bh_data; - ptr = (char *)dp + - ((dp->db_index[lnum - buf->b_ml.ml_locked_low]) & DB_INDEX_MASK); + char *ptr = (char *)dp + (dp->db_index[lnum - buf->b_ml.ml_locked_low] & DB_INDEX_MASK); buf->b_ml.ml_line_ptr = (char_u *)ptr; buf->b_ml.ml_line_lnum = lnum; buf->b_ml.ml_flags &= ~ML_LINE_DIRTY; @@ -1872,17 +1837,6 @@ int ml_append_buf(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, bool new static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, bool newfile, int mark) { - int i; - int line_count; // number of indexes in current block - int offset; - int from, to; - int page_count; - int db_idx; // index for lnum in data block - bhdr_T *hp; - DATA_BL *dp; - PTR_BL *pp; - infoptr_T *ip; - // lnum out of range if (lnum > buf->b_ml.ml_line_count || buf->b_ml.ml_mfp == NULL) { return FAIL; @@ -1903,6 +1857,7 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b // find the data block containing the previous line // This also fills the stack with the blocks from the root to the data block // This also releases any locked block. + bhdr_T *hp; if ((hp = ml_find_line(buf, lnum == 0 ? (linenr_T)1 : lnum, ML_INSERT)) == NULL) { return FAIL; @@ -1910,15 +1865,16 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b buf->b_ml.ml_flags &= ~ML_EMPTY; + int db_idx; // index for lnum in data block if (lnum == 0) { // got line one instead, correct db_idx db_idx = -1; // careful, it is negative! } else { db_idx = lnum - buf->b_ml.ml_locked_low; } - // get line count before the insertion - line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low; + // get line count (number of indexes in current block) before the insertion + int line_count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low; - dp = hp->bh_data; + DATA_BL *dp = hp->bh_data; // If // - there is not enough room in the current block @@ -1957,6 +1913,7 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b if (line_count > db_idx + 1) { // if there are following lines // Offset is the start of the previous line. // This will become the character just after the new line. + int offset; if (db_idx < 0) { offset = (int)dp->db_txt_end; } else { @@ -1965,7 +1922,7 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b memmove((char *)dp + dp->db_txt_start, (char *)dp + dp->db_txt_start + len, (size_t)offset - (dp->db_txt_start + (size_t)len)); - for (i = line_count - 1; i > db_idx; i--) { + for (int i = line_count - 1; i > db_idx; i--) { dp->db_index[i + 1] = dp->db_index[i] - (unsigned)len; } dp->db_index[db_idx + 1] = (unsigned)(offset - len); @@ -2038,7 +1995,7 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b } } - page_count = ((space_needed + (int)HEADER_SIZE) + page_size - 1) / page_size; + int page_count = ((space_needed + (int)HEADER_SIZE) + page_size - 1) / page_size; hp_new = ml_new_data(mfp, newfile, page_count); if (db_idx < 0) { // left block is new hp_left = hp_new; @@ -2078,12 +2035,12 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b memmove((char *)dp_right + dp_right->db_txt_start, (char *)dp_left + dp_left->db_txt_start, (size_t)data_moved); - offset = (int)(dp_right->db_txt_start - dp_left->db_txt_start); + int offset = (int)(dp_right->db_txt_start - dp_left->db_txt_start); dp_left->db_txt_start += (unsigned)data_moved; dp_left->db_free += (unsigned)total_moved; // update indexes in the new block - for (to = line_count_right, from = db_idx + 1; + for (int to = line_count_right, from = db_idx + 1; from < line_count_left; from++, to++) { dp_right->db_index[to] = dp->db_index[from] + (unsigned)offset; } @@ -2138,14 +2095,13 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b (void)ml_find_line(buf, (linenr_T)0, ML_FLUSH); // flush data block // update pointer blocks for the new data block - for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; - --stack_idx) { - ip = &(buf->b_ml.ml_stack[stack_idx]); + for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; stack_idx--) { + infoptr_T *ip = &(buf->b_ml.ml_stack[stack_idx]); pb_idx = ip->ip_index; if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) { return FAIL; } - pp = hp->bh_data; // must be pointer block + PTR_BL *pp = hp->bh_data; // must be pointer block if (pp->pb_id != PTR_ID) { iemsg(_("E317: pointer block id wrong 3")); mf_put(mfp, hp, false, false); @@ -2258,11 +2214,11 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char_u *line, colnr_T len, b // recompute line counts line_count_right = 0; - for (i = 0; i < (int)pp_new->pb_count; i++) { + for (int i = 0; i < (int)pp_new->pb_count; i++) { line_count_right += pp_new->pb_pointer[i].pe_line_count; } line_count_left = 0; - for (i = 0; i < (int)pp->pb_count; i++) { + for (int i = 0; i < (int)pp->pb_count; i++) { line_count_left += pp->pb_pointer[i].pe_line_count; } @@ -2378,18 +2334,6 @@ int ml_delete(linenr_T lnum, bool message) static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) { - bhdr_T *hp; - memfile_T *mfp; - DATA_BL *dp; - PTR_BL *pp; - infoptr_T *ip; - int count; // number of entries in block - int idx; - int stack_idx; - int line_start; - long line_size; - int i; - if (lnum < 1 || lnum > buf->b_ml.ml_line_count) { return FAIL; } @@ -2404,7 +2348,7 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) set_keep_msg(_(no_lines_msg), 0); } - i = ml_replace((linenr_T)1, "", true); + int i = ml_replace((linenr_T)1, "", true); buf->b_ml.ml_flags |= ML_EMPTY; return i; @@ -2413,23 +2357,25 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) // find the data block containing the line // This also fills the stack with the blocks from the root to the data block // This also releases any locked block. - mfp = buf->b_ml.ml_mfp; + memfile_T *mfp = buf->b_ml.ml_mfp; if (mfp == NULL) { return FAIL; } + bhdr_T *hp; if ((hp = ml_find_line(buf, lnum, ML_DELETE)) == NULL) { return FAIL; } - dp = hp->bh_data; - // compute line count before the delete - count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 2; - idx = lnum - buf->b_ml.ml_locked_low; + DATA_BL *dp = hp->bh_data; + // compute line count (number of entries in block) before the delete + int count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 2; + int idx = lnum - buf->b_ml.ml_locked_low; buf->b_ml.ml_line_count--; - line_start = ((dp->db_index[idx]) & DB_INDEX_MASK); + int line_start = ((dp->db_index[idx]) & DB_INDEX_MASK); + long line_size; if (idx == 0) { // first line in block, text at the end line_size = dp->db_txt_end - (unsigned)line_start; } else { @@ -2451,15 +2397,14 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) mf_free(mfp, hp); // free the data block buf->b_ml.ml_locked = NULL; - for (stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; - --stack_idx) { + for (int stack_idx = buf->b_ml.ml_stack_top - 1; stack_idx >= 0; stack_idx--) { buf->b_ml.ml_stack_top = 0; // stack is invalid when failing - ip = &(buf->b_ml.ml_stack[stack_idx]); + infoptr_T *ip = &(buf->b_ml.ml_stack[stack_idx]); idx = ip->ip_index; if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) { return FAIL; } - pp = hp->bh_data; // must be pointer block + PTR_BL *pp = hp->bh_data; // must be pointer block if (pp->pb_id != PTR_ID) { iemsg(_("E317: pointer block id wrong 4")); mf_put(mfp, hp, false, false); @@ -2496,7 +2441,7 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) // delete the index by moving the next indexes backwards // Adjust the indexes for the text movement. - for (i = idx; i < count - 1; i++) { + for (int i = idx; i < count - 1; i++) { dp->db_index[i] = dp->db_index[i + 1] + (unsigned)line_size; } @@ -2515,8 +2460,6 @@ static int ml_delete_int(buf_T *buf, linenr_T lnum, bool message) /// set the B_MARKED flag for line 'lnum' void ml_setmarked(linenr_T lnum) { - bhdr_T *hp; - DATA_BL *dp; // invalid line number if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count || curbuf->b_ml.ml_mfp == NULL) { @@ -2529,10 +2472,11 @@ void ml_setmarked(linenr_T lnum) // find the data block containing the line // This also fills the stack with the blocks from the root to the data block // This also releases any locked block. + bhdr_T *hp; if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) { return; // give error message? } - dp = hp->bh_data; + DATA_BL *dp = hp->bh_data; dp->db_index[lnum - curbuf->b_ml.ml_locked_low] |= DB_MARKED; curbuf->b_ml.ml_flags |= ML_LOCKED_DIRTY; } @@ -2540,27 +2484,23 @@ void ml_setmarked(linenr_T lnum) /// find the first line with its B_MARKED flag set linenr_T ml_firstmarked(void) { - bhdr_T *hp; - DATA_BL *dp; - linenr_T lnum; - int i; - if (curbuf->b_ml.ml_mfp == NULL) { return (linenr_T)0; } // The search starts with lowest_marked line. This is the last line where // a mark was found, adjusted by inserting/deleting lines. - for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count;) { + for (linenr_T lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count;) { // Find the data block containing the line. // This also fills the stack with the blocks from the root to the data // block This also releases any locked block. + bhdr_T *hp; if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) { return (linenr_T)0; // give error message? } - dp = hp->bh_data; + DATA_BL *dp = hp->bh_data; - for (i = lnum - curbuf->b_ml.ml_locked_low; + for (int i = lnum - curbuf->b_ml.ml_locked_low; lnum <= curbuf->b_ml.ml_locked_high; i++, lnum++) { if ((dp->db_index[i]) & DB_MARKED) { (dp->db_index[i]) &= DB_INDEX_MASK; @@ -2577,26 +2517,22 @@ linenr_T ml_firstmarked(void) /// clear all DB_MARKED flags void ml_clearmarked(void) { - bhdr_T *hp; - DATA_BL *dp; - linenr_T lnum; - int i; - if (curbuf->b_ml.ml_mfp == NULL) { // nothing to do return; } // The search starts with line lowest_marked. - for (lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count;) { + for (linenr_T lnum = lowest_marked; lnum <= curbuf->b_ml.ml_line_count;) { // Find the data block containing the line. // This also fills the stack with the blocks from the root to the data // block and releases any locked block. + bhdr_T *hp; if ((hp = ml_find_line(curbuf, lnum, ML_FIND)) == NULL) { return; // give error message? } - dp = hp->bh_data; + DATA_BL *dp = hp->bh_data; - for (i = lnum - curbuf->b_ml.ml_locked_low; + for (int i = lnum - curbuf->b_ml.ml_locked_low; lnum <= curbuf->b_ml.ml_locked_high; i++, lnum++) { if ((dp->db_index[i]) & DB_MARKED) { (dp->db_index[i]) &= DB_INDEX_MASK; @@ -2622,18 +2558,6 @@ size_t ml_flush_deleted_bytes(buf_T *buf, size_t *codepoints, size_t *codeunits) /// flush ml_line if necessary static void ml_flush_line(buf_T *buf) { - bhdr_T *hp; - DATA_BL *dp; - linenr_T lnum; - char_u *new_line; - char_u *old_line; - colnr_T new_len; - int old_len; - int extra; - int idx; - int start; - int count; - int i; static bool entered = false; if (buf->b_ml.ml_line_lnum == 0 || buf->b_ml.ml_mfp == NULL) { @@ -2648,29 +2572,30 @@ static void ml_flush_line(buf_T *buf) buf->flush_count++; - lnum = buf->b_ml.ml_line_lnum; - new_line = buf->b_ml.ml_line_ptr; + linenr_T lnum = buf->b_ml.ml_line_lnum; + char_u *new_line = buf->b_ml.ml_line_ptr; - hp = ml_find_line(buf, lnum, ML_FIND); + bhdr_T *hp = ml_find_line(buf, lnum, ML_FIND); if (hp == NULL) { siemsg(_("E320: Cannot find line %" PRId64), (int64_t)lnum); } else { - dp = hp->bh_data; - idx = lnum - buf->b_ml.ml_locked_low; - start = ((dp->db_index[idx]) & DB_INDEX_MASK); - old_line = (char_u *)dp + start; + DATA_BL *dp = hp->bh_data; + int idx = lnum - buf->b_ml.ml_locked_low; + int start = ((dp->db_index[idx]) & DB_INDEX_MASK); + char_u *old_line = (char_u *)dp + start; + int old_len; if (idx == 0) { // line is last in block old_len = (int)dp->db_txt_end - start; } else { // text of previous line follows old_len = (int)(dp->db_index[idx - 1] & DB_INDEX_MASK) - start; } - new_len = (colnr_T)STRLEN(new_line) + 1; - extra = new_len - old_len; // negative if lines gets smaller + colnr_T new_len = (colnr_T)STRLEN(new_line) + 1; + int extra = new_len - old_len; // negative if lines gets smaller // if new line fits in data block, replace directly if ((int)dp->db_free >= extra) { // if the length changes and there are following lines - count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1; + int count = buf->b_ml.ml_locked_high - buf->b_ml.ml_locked_low + 1; if (extra != 0 && idx < count - 1) { // move text of following lines memmove((char *)dp + dp->db_txt_start - extra, @@ -2678,7 +2603,7 @@ static void ml_flush_line(buf_T *buf) (size_t)(start - (int)dp->db_txt_start)); // adjust pointers of this and following lines - for (i = idx + 1; i < count; i++) { + for (int i = idx + 1; i < count; i++) { dp->db_index[i] -= (unsigned)extra; } } @@ -2755,20 +2680,11 @@ static bhdr_T *ml_new_ptr(memfile_T *mfp) /// @return NULL for failure, pointer to block header otherwise static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) { - DATA_BL *dp; PTR_BL *pp; - infoptr_T *ip; bhdr_T *hp; - memfile_T *mfp; - linenr_T t; - blocknr_T bnum, bnum2; - int dirty; - linenr_T low, high; int top; - int page_count; - int idx; - mfp = buf->b_ml.ml_mfp; + memfile_T *mfp = buf->b_ml.ml_mfp; // If there is a locked block check if the wanted line is in it. // If not, flush and release the locked block. @@ -2805,14 +2721,15 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) return NULL; } - bnum = 1; // start at the root of the tree - page_count = 1; - low = 1; - high = buf->b_ml.ml_line_count; + blocknr_T bnum = 1; // start at the root of the tree + blocknr_T bnum2; + int page_count = 1; + linenr_T low = 1; + linenr_T high = buf->b_ml.ml_line_count; if (action == ML_FIND) { // first try stack entries for (top = buf->b_ml.ml_stack_top - 1; top >= 0; top--) { - ip = &(buf->b_ml.ml_stack[top]); + infoptr_T *ip = &(buf->b_ml.ml_stack[top]); if (ip->ip_low <= lnum && ip->ip_high >= lnum) { bnum = ip->ip_bnum; low = ip->ip_low; @@ -2840,7 +2757,7 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) high--; } - dp = hp->bh_data; + DATA_BL *dp = hp->bh_data; if (dp->db_id == DATA_ID) { // data block buf->b_ml.ml_locked = hp; buf->b_ml.ml_locked_low = low; @@ -2857,15 +2774,16 @@ static bhdr_T *ml_find_line(buf_T *buf, linenr_T lnum, int action) } top = ml_add_stack(buf); // add new entry to stack - ip = &(buf->b_ml.ml_stack[top]); + infoptr_T *ip = &(buf->b_ml.ml_stack[top]); ip->ip_bnum = bnum; ip->ip_low = low; ip->ip_high = high; ip->ip_index = -1; // index not known yet - dirty = false; + bool dirty = false; + int idx; for (idx = 0; idx < (int)pp->pb_count; idx++) { - t = pp->pb_pointer[idx].pe_line_count; + linenr_T t = pp->pb_pointer[idx].pe_line_count; CHECK(t == 0, _("pe_line_count is zero")); if ((low += t) > lnum) { ip->ip_index = idx; @@ -2951,18 +2869,15 @@ static int ml_add_stack(buf_T *buf) /// Count is the number of lines added, negative if lines have been deleted. static void ml_lineadd(buf_T *buf, int count) { - int idx; - infoptr_T *ip; - PTR_BL *pp; memfile_T *mfp = buf->b_ml.ml_mfp; - bhdr_T *hp; - for (idx = buf->b_ml.ml_stack_top - 1; idx >= 0; idx--) { - ip = &(buf->b_ml.ml_stack[idx]); + for (int idx = buf->b_ml.ml_stack_top - 1; idx >= 0; idx--) { + infoptr_T *ip = &(buf->b_ml.ml_stack[idx]); + bhdr_T *hp; if ((hp = mf_get(mfp, ip->ip_bnum, 1)) == NULL) { break; } - pp = hp->bh_data; // must be pointer block + PTR_BL *pp = hp->bh_data; // must be pointer block if (pp->pb_id != PTR_ID) { mf_put(mfp, hp, false, false); iemsg(_("E317: pointer block id wrong 2")); @@ -2985,7 +2900,6 @@ static void ml_lineadd(buf_T *buf, int count) int resolve_symlink(const char *fname, char *buf) { char tmp[MAXPATHL]; - int ret; int depth = 0; if (fname == NULL) { @@ -3002,7 +2916,7 @@ int resolve_symlink(const char *fname, char *buf) return FAIL; } - ret = (int)readlink(tmp, buf, MAXPATHL - 1); + int ret = (int)readlink(tmp, buf, MAXPATHL - 1); if (ret <= 0) { if (errno == EINVAL || errno == ENOENT) { // Found non-symlink or not existing file, stop here. @@ -3048,7 +2962,6 @@ int resolve_symlink(const char *fname, char *buf) /// @return pointer to allocated memory or NULL. char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name) { - char_u *r, *s; char_u *fname_res = fname; #ifdef HAVE_READLINK char_u fname_buf[MAXPATHL]; @@ -3061,11 +2974,11 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name #endif int len = (int)STRLEN(dir_name); - s = dir_name + len; + char_u *s = dir_name + len; if (after_pathsep((char *)dir_name, (char *)s) && len > 1 && s[-1] == s[-2]) { // Ends with '//', Use Full path - r = NULL; + char_u *r = NULL; s = (char_u *)make_percent_swname((char *)dir_name, (char *)fname_res); if (s != NULL) { r = (char_u *)modname((char *)s, ".swp", false); @@ -3075,8 +2988,8 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name } // Prepend a '.' to the swap file name for the current directory. - r = (char_u *)modname((char *)fname_res, ".swp", - dir_name[0] == '.' && dir_name[1] == NUL); + char_u *r = (char_u *)modname((char *)fname_res, ".swp", + dir_name[0] == '.' && dir_name[1] == NUL); if (r == NULL) { // out of memory return NULL; } @@ -3100,12 +3013,9 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name /// @param dname don't use "dirname", it is a global for Alpha char *get_file_in_dir(char *fname, char *dname) { - char *t; - char *tail; char *retval; - int save_char; - tail = path_tail(fname); + char *tail = path_tail(fname); if (dname[0] == '.' && dname[1] == NUL) { retval = xstrdup(fname); @@ -3113,10 +3023,10 @@ char *get_file_in_dir(char *fname, char *dname) if (tail == fname) { // no path before file name retval = concat_fnames(dname + 2, tail, true); } else { - save_char = (uint8_t)(*tail); + char save_char = *tail; *tail = NUL; - t = concat_fnames(fname, dname + 2, true); - *tail = (char)save_char; + char *t = concat_fnames(fname, dname + 2, true); + *tail = save_char; retval = concat_fnames(t, tail, true); xfree(t); } @@ -3240,20 +3150,18 @@ static int do_swapexists(buf_T *buf, char_u *fname) static char *findswapname(buf_T *buf, char **dirp, char *old_fname, bool *found_existing_dir) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1, 2, 4) { - char *fname; size_t n; - char *dir_name; char *buf_fname = buf->b_fname; // Isolate a directory name from *dirp and put it in dir_name. // First allocate some memory to put the directory name in. const size_t dir_len = strlen(*dirp) + 1; - dir_name = xmalloc(dir_len); + char *dir_name = xmalloc(dir_len); (void)copy_option_part(dirp, dir_name, dir_len, ","); // we try different names until we find one that does not exist yet - fname = (char *)makeswapname((char_u *)buf_fname, (char_u *)buf->b_ffname, buf, - (char_u *)dir_name); + char *fname = (char *)makeswapname((char_u *)buf_fname, (char_u *)buf->b_ffname, buf, + (char_u *)dir_name); for (;;) { if (fname == NULL) { // must be out of memory |