compilation: Add -Wconversion to more files and validate CONV_SOURCES

All files under the os, api and msgpack_rpc directories have -Wconversion
automatically applied. CONV_SOURCES is also checked for missing files(when
renaming, for example)
This commit is contained in:
Thiago de Arruda
2014-10-21 08:53:55 -03:00
parent cf9571b7b1
commit 79b7263f79
6 changed files with 61 additions and 60 deletions

View File

@@ -38,8 +38,7 @@ endforeach()
list(REMOVE_ITEM NEOVIM_SOURCES ${to_remove}) list(REMOVE_ITEM NEOVIM_SOURCES ${to_remove})
set(CONV_SRCS set(CONV_SOURCES
api.c
arabic.c arabic.c
cursor.c cursor.c
garray.c garray.c
@@ -48,31 +47,24 @@ set(CONV_SRCS
map.c map.c
memory.c memory.c
misc2.c misc2.c
map.c
profile.c profile.c
os/env.c
os/event.c
os/job.c
os/mem.c
os/rstream.c
os/signal.c
os/users.c
os/provider.c
os/uv_helpers.c
os/wstream.c
os/msgpack_rpc.c
tempfile.c tempfile.c
api/buffer.c
api/private/helpers.c
api/private/handle.c
api/tabpage.c
api/window.c
api/vim.h
api/vim.c
) )
foreach(sfile ${CONV_SOURCES})
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/src/nvim/${sfile}")
message(FATAL_ERROR "${sfile} doesn't exist(it was added to CONV_SOURCES)")
endif()
endforeach()
file(GLOB_RECURSE EXTRA_CONV_SOURCES os/*.c api/*.c msgpack_rpc/*.c)
foreach(sfile ${EXTRA_CONV_SOURCES})
file(RELATIVE_PATH f "${PROJECT_SOURCE_DIR}/src/nvim" "${sfile}")
list(APPEND CONV_SOURCES ${f})
endforeach()
set_source_files_properties( set_source_files_properties(
${CONV_SRCS} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS} -Wconversion") ${CONV_SOURCES} PROPERTIES COMPILE_FLAGS "${COMPILE_FLAGS} -Wconversion")
if(CMAKE_C_COMPILER_ID MATCHES "Clang") if(CMAKE_C_COMPILER_ID MATCHES "Clang")
if(DEFINED ENV{SANITIZE}) if(DEFINED ENV{SANITIZE})

View File

@@ -331,7 +331,7 @@ static void parse_msgpack(RStream *rstream, void *data, bool eof)
goto end; goto end;
} }
uint32_t count = rstream_pending(rstream); size_t count = rstream_pending(rstream);
DLOG("Feeding the msgpack parser with %u bytes of data from RStream(%p)", DLOG("Feeding the msgpack parser with %u bytes of data from RStream(%p)",
count, count,
rstream); rstream);

View File

@@ -119,7 +119,8 @@ int server_start(const char *endpoint)
ip_end = strchr(addr, NUL); ip_end = strchr(addr, NUL);
} }
uint32_t addr_len = ip_end - addr; // (ip_end - addr) is always > 0, so convert to size_t
size_t addr_len = (size_t)(ip_end - addr);
if (addr_len > sizeof(ip) - 1) { if (addr_len > sizeof(ip) - 1) {
// Maximum length of an IP address buffer is 15(eg: 255.255.255.255) // Maximum length of an IP address buffer is 15(eg: 255.255.255.255)

View File

@@ -72,7 +72,7 @@ void input_stop(void)
} }
// Low level input function. // Low level input function.
int os_inchar(uint8_t *buf, int maxlen, int32_t ms, int tb_change_cnt) int os_inchar(uint8_t *buf, int maxlen, int ms, int tb_change_cnt)
{ {
InbufPollResult result; InbufPollResult result;
@@ -86,7 +86,7 @@ int os_inchar(uint8_t *buf, int maxlen, int32_t ms, int tb_change_cnt)
return 0; return 0;
} }
} else { } else {
if ((result = inbuf_poll(p_ut)) == kInputNone) { if ((result = inbuf_poll((int)p_ut)) == kInputNone) {
if (trigger_cursorhold() && maxlen >= 3 if (trigger_cursorhold() && maxlen >= 3
&& !typebuf_changed(tb_change_cnt)) { && !typebuf_changed(tb_change_cnt)) {
buf[0] = K_SPECIAL; buf[0] = K_SPECIAL;
@@ -116,7 +116,9 @@ int os_inchar(uint8_t *buf, int maxlen, int32_t ms, int tb_change_cnt)
} }
convert_input(); convert_input();
return rbuffer_read(input_buffer, (char *)buf, maxlen); // Safe to convert rbuffer_read to int, it will never overflow since
// we use relatively small buffers.
return (int)rbuffer_read(input_buffer, (char *)buf, (size_t)maxlen);
} }
// Check if a character is available for reading // Check if a character is available for reading
@@ -170,7 +172,7 @@ static bool input_poll(int ms)
} }
// This is a replacement for the old `WaitForChar` function in os_unix.c // This is a replacement for the old `WaitForChar` function in os_unix.c
static InbufPollResult inbuf_poll(int32_t ms) static InbufPollResult inbuf_poll(int ms)
{ {
if (typebuf_was_filled || rbuffer_pending(input_buffer)) { if (typebuf_was_filled || rbuffer_pending(input_buffer)) {
return kInputAvail; return kInputAvail;
@@ -260,9 +262,9 @@ static void convert_input(void)
char *inbuf = rbuffer_read_ptr(input_buffer); char *inbuf = rbuffer_read_ptr(input_buffer);
size_t count = rbuffer_pending(input_buffer), consume_count = 0; size_t count = rbuffer_pending(input_buffer), consume_count = 0;
for (int i = count - 1; i >= 0; i--) { for (int i = (int)count - 1; i >= 0; i--) {
if (inbuf[i] == 3) { if (inbuf[i] == 3) {
consume_count = i + 1; consume_count = (size_t)(i + 1);
break; break;
} }
} }

View File

@@ -1,4 +1,5 @@
#include <string.h> #include <string.h>
#include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
@@ -58,11 +59,11 @@ typedef struct {
/// `shell_free_argv` when no longer needed. /// `shell_free_argv` when no longer needed.
char **shell_build_argv(const char_u *cmd, const char_u *extra_shell_opt) char **shell_build_argv(const char_u *cmd, const char_u *extra_shell_opt)
{ {
int argc = tokenize(p_sh, NULL) + tokenize(p_shcf, NULL); size_t argc = tokenize(p_sh, NULL) + tokenize(p_shcf, NULL);
char **rv = xmalloc((unsigned)((argc + 4) * sizeof(char *))); char **rv = xmalloc((unsigned)((argc + 4) * sizeof(char *)));
// Split 'shell' // Split 'shell'
int i = tokenize(p_sh, rv); size_t i = tokenize(p_sh, rv);
if (extra_shell_opt != NULL) { if (extra_shell_opt != NULL) {
// Push a copy of `extra_shell_opt` // Push a copy of `extra_shell_opt`
@@ -356,9 +357,9 @@ static void system_data_cb(RStream *rstream, void *data, bool eof)
/// @param argv The vector that will be filled with copies of the parsed /// @param argv The vector that will be filled with copies of the parsed
/// words. It can be NULL if the caller only needs to count words. /// words. It can be NULL if the caller only needs to count words.
/// @return The number of words parsed. /// @return The number of words parsed.
static int tokenize(const char_u *str, char **argv) static size_t tokenize(const char_u *str, char **argv)
{ {
int argc = 0, len; size_t argc = 0, len;
char_u *p = (char_u *) str; char_u *p = (char_u *) str;
while (*p != NUL) { while (*p != NUL) {
@@ -383,11 +384,11 @@ static int tokenize(const char_u *str, char **argv)
/// ///
/// @param str A pointer to the first character of the word /// @param str A pointer to the first character of the word
/// @return The offset from `str` at which the word ends. /// @return The offset from `str` at which the word ends.
static int word_length(const char_u *str) static size_t word_length(const char_u *str)
{ {
const char_u *p = str; const char_u *p = str;
bool inquote = false; bool inquote = false;
int length = 0; size_t length = 0;
// Move `p` to the end of shell word by advancing the pointer while it's // Move `p` to the end of shell word by advancing the pointer while it's
// inside a quote or it's a non-whitespace character // inside a quote or it's a non-whitespace character
@@ -418,15 +419,15 @@ static void write_selection(uv_write_t *req)
// TODO(tarruda): use a static buffer for up to a limit(BUFFER_LENGTH) and // TODO(tarruda): use a static buffer for up to a limit(BUFFER_LENGTH) and
// only after filled we should start allocating memory(skip unnecessary // only after filled we should start allocating memory(skip unnecessary
// allocations for small writes) // allocations for small writes)
int buflen = BUFFER_LENGTH; size_t buflen = BUFFER_LENGTH;
pdata->wbuffer = (char *)xmalloc(buflen); pdata->wbuffer = (char *)xmalloc(buflen);
uv_buf_t uvbuf; uv_buf_t uvbuf;
linenr_T lnum = curbuf->b_op_start.lnum; linenr_T lnum = curbuf->b_op_start.lnum;
int off = 0; size_t off = 0;
int written = 0; size_t written = 0;
char_u *lp = ml_get(lnum); char_u *lp = ml_get(lnum);
int l; size_t l;
int len; size_t len;
for (;;) { for (;;) {
l = strlen((char *)lp + written); l = strlen((char *)lp + written);
@@ -443,7 +444,7 @@ static void write_selection(uv_write_t *req)
pdata->wbuffer[off++] = NUL; pdata->wbuffer[off++] = NUL;
} else { } else {
char_u *s = vim_strchr(lp + written, NL); char_u *s = vim_strchr(lp + written, NL);
len = s == NULL ? l : s - (lp + written); len = s == NULL ? l : (size_t)(s - (lp + written));
while (off + len >= buflen) { while (off + len >= buflen) {
// Resize the buffer // Resize the buffer
buflen *= 2; buflen *= 2;
@@ -584,6 +585,7 @@ static void exit_cb(uv_process_t *proc, int64_t status, int term_signal)
{ {
ProcessData *data = (ProcessData *)proc->data; ProcessData *data = (ProcessData *)proc->data;
data->exited++; data->exited++;
data->exit_status = status; assert(status <= INT_MAX);
data->exit_status = (int)status;
uv_close((uv_handle_t *)proc, NULL); uv_close((uv_handle_t *)proc, NULL);
} }

View File

@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
@@ -64,23 +65,6 @@ void os_microdelay(uint64_t microseconds, bool ignoreinput)
} }
} }
static void microdelay(uint64_t microseconds)
{
uint64_t hrtime;
int64_t ns = microseconds * 1000; // convert to nanoseconds
uv_mutex_lock(&delay_mutex);
while (ns > 0) {
hrtime = uv_hrtime();
if (uv_cond_timedwait(&delay_cond, &delay_mutex, ns) == UV_ETIMEDOUT)
break;
ns -= uv_hrtime() - hrtime;
}
uv_mutex_unlock(&delay_mutex);
}
/// Portable version of POSIX localtime_r() /// Portable version of POSIX localtime_r()
/// ///
/// @return NULL in case of error /// @return NULL in case of error
@@ -112,3 +96,23 @@ struct tm *os_get_localtime(struct tm *result) FUNC_ATTR_NONNULL_ALL
time_t rawtime = time(NULL); time_t rawtime = time(NULL);
return os_localtime_r(&rawtime, result); return os_localtime_r(&rawtime, result);
} }
static void microdelay(uint64_t microseconds)
{
uint64_t elapsed = 0;
uint64_t ns = microseconds * 1000; // convert to nanoseconds
uint64_t base = uv_hrtime();
uv_mutex_lock(&delay_mutex);
while (elapsed < ns) {
if (uv_cond_timedwait(&delay_cond, &delay_mutex, ns - elapsed)
== UV_ETIMEDOUT)
break;
uint64_t now = uv_hrtime();
elapsed += now - base;
base = now;
}
uv_mutex_unlock(&delay_mutex);
}