From 8f2ac8a7311f69d5860a436faf5809277ba62ce2 Mon Sep 17 00:00:00 2001 From: sach1t Date: Tue, 3 May 2016 21:25:38 -0400 Subject: [PATCH 1/2] Enable -Wconversion for getchar.c --- src/nvim/CMakeLists.txt | 1 - src/nvim/getchar.c | 108 +++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 52 deletions(-) diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index a473f99278..0711642868 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -87,7 +87,6 @@ set(CONV_SOURCES ex_docmd.c ex_getln.c fileio.c - getchar.c mbyte.c memline.c message.c diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 050862d811..6550ad5b29 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -144,7 +144,7 @@ static int KeyNoremap = 0; /* remapping flags */ static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */ static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */ -static int last_recorded_len = 0; /* number of last recorded chars */ +static unsigned int last_recorded_len = 0; // number of last recorded chars static const uint8_t ui_toggle[] = { K_SPECIAL, KS_EXTRA, KE_PASTE, 0 }; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -211,7 +211,7 @@ char_u *get_recorded(void) * (possibly mapped) characters that stopped the recording. */ len = STRLEN(p); - if ((int)len >= last_recorded_len) { + if (len >= last_recorded_len) { len -= last_recorded_len; p[len] = NUL; } @@ -243,13 +243,15 @@ static void add_buff ( buffheader_T *buf, char_u *s, - long slen /* length of "s" or -1 */ + ssize_t slen // length of "s" or -1 ) { - if (slen < 0) - slen = (long)STRLEN(s); - if (slen == 0) /* don't add empty strings */ + if (slen < 0) { + slen = (ssize_t)STRLEN(s); + } + if (slen == 0) { // don't add empty strings return; + } if (buf->bh_first.b_next == NULL) { /* first add to list */ buf->bh_space = 0; @@ -264,16 +266,16 @@ add_buff ( buf->bh_index = 0; ssize_t len; - if (buf->bh_space >= (int)slen) { - len = STRLEN(buf->bh_curr->b_str); + if (buf->bh_space >= slen) { + len = (ssize_t)STRLEN(buf->bh_curr->b_str); STRLCPY(buf->bh_curr->b_str + len, s, slen + 1); - buf->bh_space -= slen; + buf->bh_space -= (int)slen; } else { if (slen < MINIMAL_SIZE) len = MINIMAL_SIZE; else len = slen; - buffblock_T *p = xmalloc(sizeof(buffblock_T) + len); + buffblock_T *p = xmalloc(sizeof(buffblock_T) + (size_t)len); buf->bh_space = (int)(len - slen); STRLCPY(p->b_str, s, slen + 1); @@ -317,11 +319,11 @@ static void add_char_buff(buffheader_T *buf, int c) if (IS_SPECIAL(c) || c == K_SPECIAL || c == NUL) { /* translate special key code into three byte sequence */ temp[0] = K_SPECIAL; - temp[1] = K_SECOND(c); - temp[2] = K_THIRD(c); + temp[1] = (char_u)K_SECOND(c); + temp[2] = (char_u)K_THIRD(c); temp[3] = NUL; } else { - temp[0] = c; + temp[0] = (char_u)c; temp[1] = NUL; } add_buff(buf, temp, -1L); @@ -694,10 +696,11 @@ static int read_redo(int init, int old_redo) bp = bp->b_next; p = bp->b_str; } - buf[i] = c; - if (i == n - 1) { /* last byte of a character */ - if (n != 1) + buf[i] = (char_u)c; + if (i == n - 1) { // last byte of a character + if (n != 1) { c = (*mb_ptr2char)(buf); + } break; } c = *p; @@ -882,8 +885,8 @@ int ins_typebuf(char_u *str, int noremap, int offset, int nottyped, bool silent) setcursor(); return FAIL; } - s1 = xmalloc(newlen); - s2 = xmalloc(newlen); + s1 = xmalloc((size_t)newlen); + s2 = xmalloc((size_t)newlen); typebuf.tb_buflen = newlen; /* copy the old chars, before the insertion point */ @@ -937,7 +940,7 @@ int ins_typebuf(char_u *str, int noremap, int offset, int nottyped, bool silent) nrm = noremap; for (i = 0; i < addlen; ++i) typebuf.tb_noremap[typebuf.tb_off + i + offset] = - (--nrm >= 0) ? val : RM_YES; + (char_u)((--nrm >= 0) ? val : RM_YES); /* tb_maplen and tb_silent only remember the length of mapped and/or * silent mappings at the start of the buffer, assuming that a mapped @@ -965,8 +968,8 @@ void ins_char_typebuf(int c) char_u buf[MB_MAXBYTES + 1]; if (IS_SPECIAL(c)) { buf[0] = K_SPECIAL; - buf[1] = K_SECOND(c); - buf[2] = K_THIRD(c); + buf[1] = (char_u)K_SECOND(c); + buf[2] = (char_u)K_THIRD(c); buf[3] = NUL; } else { buf[(*mb_char2bytes)(c, buf)] = NUL; @@ -1090,9 +1093,11 @@ static void gotchars(char_u *chars, int len) char_u buf[2]; int todo = len; - /* remember how many chars were last recorded */ - if (Recording) - last_recorded_len += len; + // remember how many chars were last recorded + if (Recording) { + assert(len >= 0); + last_recorded_len += (unsigned int)len; + } buf[1] = NUL; while (todo--) { @@ -1101,7 +1106,7 @@ static void gotchars(char_u *chars, int len) updatescript(c); if (Recording) { - buf[0] = c; + buf[0] = (char_u)c; add_buff(&recordbuff, buf, 1L); } } @@ -1465,10 +1470,10 @@ int vgetc(void) * Note: This will loop until enough bytes are received! */ if (has_mbyte && (n = MB_BYTE2LEN_CHECK(c)) > 1) { - ++no_mapping; - buf[0] = c; - for (i = 1; i < n; ++i) { - buf[i] = vgetorpeek(TRUE); + no_mapping++; + buf[0] = (char_u)c; + for (i = 1; i < n; i++) { + buf[i] = (char_u)vgetorpeek(true); if (buf[i] == K_SPECIAL ) { /* Must be a K_SPECIAL - KS_SPECIAL - KE_FILLER sequence, @@ -1711,7 +1716,7 @@ static int vgetorpeek(int advance) if (advance) { /* Also record this character, it might be needed to * get out of Insert mode. */ - *typebuf.tb_buf = c; + *typebuf.tb_buf = (char_u)c; gotchars(typebuf.tb_buf, 1); } cmd_silent = FALSE; @@ -1888,8 +1893,8 @@ static int vgetorpeek(int advance) (long)!p_paste, NULL, 0); if (!(State & INSERT)) { msg_col = 0; - msg_row = Rows - 1; - msg_clr_eos(); /* clear ruler */ + msg_row = (int)Rows - 1; + msg_clr_eos(); // clear ruler } status_redraw_all(); redraw_statuslines(); @@ -2417,7 +2422,7 @@ inchar ( else return -1; } else { - buf[0] = script_char; + buf[0] = (char_u)script_char; len = 1; } } @@ -2453,7 +2458,7 @@ inchar ( * Fill up to a third of the buffer, because each character may be * tripled below. */ - len = os_inchar(buf, maxlen / 3, wait_time, tb_change_cnt); + len = os_inchar(buf, maxlen / 3, (int)wait_time, tb_change_cnt); } if (typebuf_changed(tb_change_cnt)) @@ -2496,8 +2501,8 @@ fix_input_buffer ( && !script && (i < 2 || p[1] != KS_EXTRA))) { memmove(p + 3, p + 1, (size_t)i); - p[2] = K_THIRD(p[0]); - p[1] = K_SECOND(p[0]); + p[2] = (char_u)K_THIRD(p[0]); + p[1] = (char_u)K_SECOND(p[0]); p[0] = K_SPECIAL; p += 2; len += 2; @@ -2573,11 +2578,11 @@ do_map ( int new_hash; mapblock_T **abbr_table; mapblock_T **map_table; - int unique = FALSE; - int nowait = FALSE; - int silent = FALSE; - int special = FALSE; - int expr = FALSE; + bool unique = false; + bool nowait = false; + bool silent = false; + bool special = false; + bool expr = false; int noremap; char_u *orig_rhs; @@ -2609,7 +2614,7 @@ do_map ( */ if (STRNCMP(keys, "", 8) == 0) { keys = skipwhite(keys + 8); - nowait = TRUE; + nowait = true; continue; } @@ -2618,7 +2623,7 @@ do_map ( */ if (STRNCMP(keys, "", 8) == 0) { keys = skipwhite(keys + 8); - silent = TRUE; + silent = true; continue; } @@ -2627,7 +2632,7 @@ do_map ( */ if (STRNCMP(keys, "", 9) == 0) { keys = skipwhite(keys + 9); - special = TRUE; + special = true; continue; } @@ -2645,7 +2650,7 @@ do_map ( */ if (STRNCMP(keys, "", 6) == 0) { keys = skipwhite(keys + 6); - expr = TRUE; + expr = true; continue; } /* @@ -2653,7 +2658,7 @@ do_map ( */ if (STRNCMP(keys, "", 8) == 0) { keys = skipwhite(keys + 8); - unique = TRUE; + unique = true; continue; } break; @@ -3487,7 +3492,7 @@ int ExpandMappings(regmatch_T *regmatch, int *num_file, char_u ***file) break; /* for (round) */ if (round == 1) { - *file = (char_u **)xmalloc(count * sizeof(char_u *)); + *file = (char_u **)xmalloc((size_t)count * sizeof(char_u *)); } } /* for (round) */ @@ -3651,8 +3656,8 @@ int check_abbr(int c, char_u *ptr, int col, int mincol) /* special key code, split up */ if (IS_SPECIAL(c) || c == K_SPECIAL) { tb[j++] = K_SPECIAL; - tb[j++] = K_SECOND(c); - tb[j++] = K_THIRD(c); + tb[j++] = (char_u)K_SECOND(c); + tb[j++] = (char_u)K_THIRD(c); } else { if (c < ABBR_OFF && (c < ' ' || c > '~')) tb[j++] = Ctrl_V; /* special char needs CTRL-V */ @@ -3661,8 +3666,9 @@ int check_abbr(int c, char_u *ptr, int col, int mincol) if (c >= ABBR_OFF) c -= ABBR_OFF; j += (*mb_char2bytes)(c, tb + j); - } else - tb[j++] = c; + } else { + tb[j++] = (char_u)c; + } } tb[j] = NUL; /* insert the last typed char */ From 16217b285404389e8ba0ddb41055c9f086e2016a Mon Sep 17 00:00:00 2001 From: sach1t Date: Fri, 6 May 2016 01:02:18 -0400 Subject: [PATCH 2/2] getchar.c wconversion: Change types --- src/nvim/buffer_defs.h | 4 ++-- src/nvim/getchar.c | 51 +++++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index c0cd801cd4..0324f6b88a 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -141,8 +141,8 @@ struct buffblock { struct buffheader { buffblock_T bh_first; // first (dummy) block of list buffblock_T *bh_curr; // buffblock for appending - int bh_index; // index for reading - int bh_space; // space in bh_curr for appending + size_t bh_index; // index for reading + size_t bh_space; // space in bh_curr for appending }; /* diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 6550ad5b29..dbf0322d78 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -144,7 +144,7 @@ static int KeyNoremap = 0; /* remapping flags */ static char_u typebuf_init[TYPELEN_INIT]; /* initial typebuf.tb_buf */ static char_u noremapbuf_init[TYPELEN_INIT]; /* initial typebuf.tb_noremap */ -static unsigned int last_recorded_len = 0; // number of last recorded chars +static size_t last_recorded_len = 0; // number of last recorded chars static const uint8_t ui_toggle[] = { K_SPECIAL, KS_EXTRA, KE_PASTE, 0 }; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -265,18 +265,19 @@ add_buff ( STRLEN(buf->bh_first.b_next->b_str + buf->bh_index) + 1); buf->bh_index = 0; - ssize_t len; - if (buf->bh_space >= slen) { - len = (ssize_t)STRLEN(buf->bh_curr->b_str); + size_t len; + if (buf->bh_space >= (size_t)slen) { + len = STRLEN(buf->bh_curr->b_str); STRLCPY(buf->bh_curr->b_str + len, s, slen + 1); - buf->bh_space -= (int)slen; + buf->bh_space -= (size_t)slen; } else { - if (slen < MINIMAL_SIZE) + if (slen < MINIMAL_SIZE) { len = MINIMAL_SIZE; - else - len = slen; - buffblock_T *p = xmalloc(sizeof(buffblock_T) + (size_t)len); - buf->bh_space = (int)(len - slen); + } else { + len = (size_t)slen; + } + buffblock_T *p = xmalloc(sizeof(buffblock_T) + len); + buf->bh_space = len - (size_t)slen; STRLCPY(p->b_str, s, slen + 1); p->b_next = buf->bh_curr->b_next; @@ -1086,22 +1087,20 @@ void del_typebuf(int len, int offset) * Write typed characters to script file. * If recording is on put the character in the recordbuffer. */ -static void gotchars(char_u *chars, int len) +static void gotchars(char_u *chars, size_t len) { char_u *s = chars; int c; char_u buf[2]; - int todo = len; // remember how many chars were last recorded if (Recording) { - assert(len >= 0); - last_recorded_len += (unsigned int)len; + last_recorded_len += len; } buf[1] = NUL; - while (todo--) { - /* Handle one byte at a time; no translation to be done. */ + while (len--) { + // Handle one byte at a time; no translation to be done. c = *s++; updatescript(c); @@ -1882,11 +1881,11 @@ static int vgetorpeek(int advance) match = typebuf_match_len(p_pt, &mlen); } if (match) { - /* write chars to script file(s) */ - if (mlen > typebuf.tb_maplen) - gotchars(typebuf.tb_buf + typebuf.tb_off - + typebuf.tb_maplen, - mlen - typebuf.tb_maplen); + // write chars to script file(s) + if (mlen > typebuf.tb_maplen) { + gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen, + (size_t)(mlen - typebuf.tb_maplen)); + } del_typebuf(mlen, 0); /* remove the chars */ set_option_value((char_u *)"paste", @@ -1980,11 +1979,11 @@ static int vgetorpeek(int advance) char_u *save_m_keys; char_u *save_m_str; - /* write chars to script file(s) */ - if (keylen > typebuf.tb_maplen) - gotchars(typebuf.tb_buf + typebuf.tb_off - + typebuf.tb_maplen, - keylen - typebuf.tb_maplen); + // write chars to script file(s) + if (keylen > typebuf.tb_maplen) { + gotchars(typebuf.tb_buf + typebuf.tb_off + typebuf.tb_maplen, + (size_t)(keylen - typebuf.tb_maplen)); + } cmd_silent = (typebuf.tb_silent > 0); del_typebuf(keylen, 0); /* remove the mapped keys */