From 1dadf9d966c2a260586ab27b00143c77e800a04c Mon Sep 17 00:00:00 2001 From: Nicolas Sauzede Date: Sat, 16 Nov 2019 00:30:50 +0100 Subject: [PATCH] fix Windows warnings --- thirdparty/vschannel/vschannel.c | 2 -- thirdparty/vschannel/vschannel.h | 4 ++-- vlib/builtin/builtin.v | 7 +++--- vlib/builtin/cfns.v | 12 ++++++++-- vlib/builtin/utf8.v | 24 +++++++++---------- vlib/compiler/cc.v | 15 +++++++++++- vlib/compiler/cheaders.v | 3 +++ vlib/compiler/fn.v | 6 ++--- vlib/compiler/msvc.v | 2 +- vlib/os/const_windows.v | 2 +- vlib/os/os.v | 34 ++++++++++++++++----------- vlib/os/os_windows.v | 40 ++++++++++++++++---------------- vlib/time/time.v | 2 +- 13 files changed, 92 insertions(+), 61 deletions(-) diff --git a/thirdparty/vschannel/vschannel.c b/thirdparty/vschannel/vschannel.c index 74ddc94e30..ba4a7324a4 100644 --- a/thirdparty/vschannel/vschannel.c +++ b/thirdparty/vschannel/vschannel.c @@ -1092,5 +1092,3 @@ static void get_new_client_credentials(TlsContext *tls_ctx) { break; } } - - diff --git a/thirdparty/vschannel/vschannel.h b/thirdparty/vschannel/vschannel.h index 9d8749902a..0338645c58 100644 --- a/thirdparty/vschannel/vschannel.h +++ b/thirdparty/vschannel/vschannel.h @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include @@ -44,4 +44,4 @@ static DWORD verify_server_certificate(PCCERT_CONTEXT pServerCert, LPWSTR host, static SECURITY_STATUS create_credentials(TlsContext *tls_ctx); -static void get_new_client_credentials(TlsContext *tls_ctx); \ No newline at end of file +static void get_new_client_credentials(TlsContext *tls_ctx); diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index aef052fb13..8d2cad8b10 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -162,9 +162,10 @@ fn v_ptr_free(ptr voidptr) { pub fn is_atty(fd int) int { $if windows { - mut mode := 0 - C.GetConsoleMode(C._get_osfhandle(fd), &mode) - return mode + mut mode := u32(0) + osfh := voidptr(C._get_osfhandle(fd)) + C.GetConsoleMode(osfh, voidptr(&mode)) + return int(mode) } $else { return C.isatty(fd) } diff --git a/vlib/builtin/cfns.v b/vlib/builtin/cfns.v index da2c89f9cc..03348b9b5a 100644 --- a/vlib/builtin/cfns.v +++ b/vlib/builtin/cfns.v @@ -32,5 +32,13 @@ fn C.realpath(byteptr, byteptr) &char // Windows fn C._setmode(int, int) fn C._fileno(int) int - - +fn C._get_osfhandle(fd int) C.intptr_t +fn C.GetModuleFileNameW(hModule voidptr, lpFilename &u16, nSize u32) u32 +fn C.CreatePipe(hReadPipe &voidptr, hWritePipe &voidptr, lpPipeAttributes voidptr, nSize u32) bool +fn C.SetHandleInformation(hObject voidptr, dwMask u32, dwFlags u32) bool +fn C.ExpandEnvironmentStringsW(lpSrc &u16, lpDst &u16, nSize u32) u32 +fn C.CreateProcessW(lpApplicationName &u16, lpCommandLine &u16, lpProcessAttributes voidptr, lpThreadAttributes voidptr, bInheritHandles bool, dwCreationFlags u32, lpEnvironment voidptr, lpCurrentDirectory &u16, lpStartupInfo voidptr, lpProcessInformation voidptr) bool +fn C.ReadFile(hFile voidptr, lpBuffer voidptr, nNumberOfBytesToRead u32, lpNumberOfBytesRead voidptr, lpOverlapped voidptr) bool +fn C.GetFileAttributesW(lpFileName byteptr) u32 +fn C.RegQueryValueExW(hKey voidptr, lpValueName &u16, lpReserved &u32, lpType &u32, lpData byteptr, lpcbData &u32) int +fn C.RegOpenKeyExW(hKey voidptr, lpSubKey &u16, ulOptions u32, samDesired u32, phkResult voidptr) int diff --git a/vlib/builtin/utf8.v b/vlib/builtin/utf8.v index 84fbdab0ea..38c2482f31 100644 --- a/vlib/builtin/utf8.v +++ b/vlib/builtin/utf8.v @@ -96,17 +96,17 @@ const ( ) pub fn (_str string) to_wide() &u16 { - $if windows { - num_chars := int(C.MultiByteToWideChar(CP_UTF8, 0, _str.str, _str.len, 0, 0)) - mut wstr := &u16(malloc((num_chars + 1) * 2)) // sizeof(wchar_t) - if wstr > 0 { - C.MultiByteToWideChar(CP_UTF8, 0, _str.str, _str.len, wstr, num_chars) - C.memset(&byte(wstr) + num_chars * 2, 0, 2) - } - return wstr - } $else { - return 0 - } + $if windows { + num_chars := int(C.MultiByteToWideChar(CP_UTF8, 0, _str.str, _str.len, 0, 0)) + mut wstr := &u16(malloc((num_chars + 1) * 2)) // sizeof(wchar_t) + if !isnil(wstr) { + C.MultiByteToWideChar(CP_UTF8, 0, _str.str, _str.len, wstr, num_chars) + C.memset(&byte(wstr) + num_chars * 2, 0, 2) + } + return wstr + } $else { + return 0 + } } pub fn string_from_wide(_wstr &u16) string { @@ -122,7 +122,7 @@ pub fn string_from_wide2(_wstr &u16, len int) string { $if windows { num_chars := int(C.WideCharToMultiByte(CP_UTF8, 0, _wstr, len, 0, 0, 0, 0)) mut str_to := &byte(malloc(num_chars + 1)) - if str_to > 0 { + if !isnil(str_to) { C.WideCharToMultiByte(CP_UTF8, 0, _wstr, len, str_to, num_chars, 0, 0) C.memset(&byte(str_to) + num_chars, 0, 1) } diff --git a/vlib/compiler/cc.v b/vlib/compiler/cc.v index 7975d635bf..3ea0b0a21a 100644 --- a/vlib/compiler/cc.v +++ b/vlib/compiler/cc.v @@ -103,7 +103,20 @@ fn (v mut V) cc() { } //linux_host := os.user_os() == 'linux' v.log('cc() isprod=$v.pref.is_prod outname=$v.out_name') - mut a := [v.pref.cflags, '-std=gnu11', '-w'] // arguments for the C compiler + // arguments for the C compiler + mut a := [v.pref.cflags, '-std=gnu11', + '-Wall', + '-Wextra', + // TODO : activate -Werror once no warnings remain +// '-Werror', + // TODO : try and remove the below workaround options when the corresponding + // warnings are totally fixed/removed + '-Wno-unused-variable', + '-Wno-unused-but-set-variable', + '-Wno-unused-parameter', + '-Wno-unused-result', + '-Wno-missing-braces', + '-Wno-unused-label'] if v.pref.is_so { a << '-shared -fPIC '// -Wl,-z,defs' diff --git a/vlib/compiler/cheaders.v b/vlib/compiler/cheaders.v index e9da07254b..d7cd5e0ed0 100644 --- a/vlib/compiler/cheaders.v +++ b/vlib/compiler/cheaders.v @@ -89,6 +89,9 @@ c_headers = ' #ifdef _WIN32 #define WINVER 0x0600 +#ifdef _WIN32_WINNT +#undef _WIN32_WINNT +#endif #define _WIN32_WINNT 0x0600 #define WIN32_LEAN_AND_MEAN #define _UNICODE diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index 26c74d50e5..825d015d7c 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -646,9 +646,9 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type wrapper_name := '${fn_name}_thread_wrapper' mut wrapper_type := 'void*' if p.os == .windows { - wrapper_type = 'void* __stdcall' + wrapper_type = 'DWORD WINAPI' } - wrapper_text := '$wrapper_type $wrapper_name($arg_struct_name * arg) {$fn_name( /*f*/$str_args ); return NULL; }' + wrapper_text := '$wrapper_type $wrapper_name($arg_struct_name * arg) {$fn_name( /*f*/$str_args ); return 0; }' p.cgen.register_thread_fn(wrapper_name, wrapper_text, arg_struct) // Create thread object tmp_nr := p.get_tmp_counter() @@ -663,7 +663,7 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type } // Call the wrapper if p.os == .windows { - p.genln(' CreateThread(0,0, $wrapper_name, $parg, 0,0);') + p.genln(' CreateThread(0,0, (LPTHREAD_START_ROUTINE)$wrapper_name, $parg, 0,0);') } else { p.genln('int $tmp2 = pthread_create(& $thread_name, NULL, (void *)$wrapper_name, $parg);') diff --git a/vlib/compiler/msvc.v b/vlib/compiler/msvc.v index ef29b66d1d..9ef79b3e3d 100644 --- a/vlib/compiler/msvc.v +++ b/vlib/compiler/msvc.v @@ -4,7 +4,7 @@ import os #flag windows -l shell32 #flag windows -l dbghelp -// RegOpenKeyExA etc +// RegOpenKeyExW etc #flag windows -l advapi32 struct MsvcResult { diff --git a/vlib/os/const_windows.v b/vlib/os/const_windows.v index 8e72df7a75..e8f0460aac 100644 --- a/vlib/os/const_windows.v +++ b/vlib/os/const_windows.v @@ -66,7 +66,7 @@ const ( ) const( - INVALID_HANDLE_VALUE = -1 + INVALID_HANDLE_VALUE = voidptr(-1) ) // https://docs.microsoft.com/en-us/windows/console/setconsolemode diff --git a/vlib/os/os.v b/vlib/os/os.v index fe1f4ca11c..60a24cf06f 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -109,7 +109,7 @@ pub fn read_file(path string) ?string { pub fn file_size(path string) int { mut s := C.stat{} $if windows { - C._wstat(path.to_wide(), &s) + C._wstat(path.to_wide(), voidptr(&s)) } $else { C.stat(*char(path.str), &s) } @@ -583,13 +583,13 @@ pub fn get_raw_line() string { buf := &byte(malloc(max_line_chars*2)) if is_atty(0) > 0 { h_input := C.GetStdHandle(STD_INPUT_HANDLE) - mut nr_chars := 0 - C.ReadConsole(h_input, buf, max_line_chars * 2, &nr_chars, 0) - return string_from_wide2(&u16(buf), nr_chars) + mut nr_chars := u32(0) + C.ReadConsole(h_input, buf, max_line_chars * 2, voidptr(&nr_chars), 0) + return string_from_wide2(&u16(buf), int(nr_chars)) } - res := int( C.fgetws(buf, max_line_chars, C.stdin ) ) + res := C.fgetws(&u16(buf), max_line_chars, C.stdin ) len := int( C.wcslen(&u16(buf)) ) - if 0 != res { return string_from_wide2( &u16(buf), len ) } + if !isnil(res) { return string_from_wide2( &u16(buf), len ) } return '' } $else { max := size_t(256) @@ -830,7 +830,9 @@ pub fn realpath(fpath string) string { mut fullpath := calloc( MAX_PATH ) mut res := 0 $if windows { - res = int( C._fullpath( fullpath, fpath.str, MAX_PATH ) ) + // here we want an int==0 if _fullpath failed , in which case + // it would return NULL, and !isnil(NULL) would be false==0 + res = int( !isnil(C._fullpath( fullpath, fpath.str, MAX_PATH )) ) } $else{ if fpath.len != strlen(fpath.str) { @@ -900,19 +902,25 @@ fn C.fork() int fn C.wait() int pub fn fork() int { + mut pid := -1 $if !windows { - pid := C.fork() - return pid + pid = C.fork() } - panic('os.fork not supported in windows') // TODO + $if windows { + panic('os.fork not supported in windows') // TODO + } + return pid } pub fn wait() int { + mut pid := -1 $if !windows { - pid := C.wait(0) - return pid + pid = C.wait(0) } - panic('os.wait not supported in windows') // TODO + $if !windows { + panic('os.wait not supported in windows') // TODO + } + return pid } pub fn file_last_mod_unix(path string) int { diff --git a/vlib/os/os_windows.v b/vlib/os/os_windows.v index 9fb5219270..16d5641417 100644 --- a/vlib/os/os_windows.v +++ b/vlib/os/os_windows.v @@ -78,7 +78,7 @@ fn init_os_args(argc int, argv &byteptr) []string { mut args := []string mut args_list := &voidptr(0) mut args_count := 0 - args_list = C.CommandLineToArgvW(C.GetCommandLine(), &args_count) + args_list = &voidptr(C.CommandLineToArgvW(C.GetCommandLine(), &args_count)) for i := 0; i < args_count; i++ { args << string_from_wide(&u16(args_list[i])) } @@ -103,12 +103,12 @@ pub fn ls(path string) ?[]string { path_files := '$path\\*' // NOTE:TODO: once we have a way to convert utf16 wide character to utf8 // we should use FindFirstFileW and FindNextFileW - h_find_files := C.FindFirstFile(path_files.to_wide(), &find_file_data) + h_find_files := C.FindFirstFile(path_files.to_wide(), voidptr(&find_file_data)) first_filename := string_from_wide(&u16(find_file_data.cFileName)) if first_filename != '.' && first_filename != '..' { dir_files << first_filename } - for C.FindNextFile(h_find_files, &find_file_data) { + for C.FindNextFile(h_find_files, voidptr(&find_file_data)) { filename := string_from_wide(&u16(find_file_data.cFileName)) if filename != '.' && filename != '..' { dir_files << filename.clone() @@ -120,11 +120,11 @@ pub fn ls(path string) ?[]string { pub fn dir_exists(path string) bool { _path := path.replace('/', '\\') - attr := int(C.GetFileAttributes(_path.to_wide())) - if attr == C.INVALID_FILE_ATTRIBUTES { + attr := C.GetFileAttributesW(_path.to_wide()) + if int(attr) == int(C.INVALID_FILE_ATTRIBUTES) { return false } - if (attr & C.FILE_ATTRIBUTE_DIRECTORY) != 0 { + if (int(attr) & C.FILE_ATTRIBUTE_DIRECTORY) != 0 { return true } return false @@ -149,7 +149,7 @@ pub fn get_file_handle(path string) HANDLE { if _fd == 0 { return HANDLE(INVALID_HANDLE_VALUE) } - _handle := C._get_osfhandle(C._fileno(_fd)) // CreateFile? - hah, no -_- + _handle := HANDLE(C._get_osfhandle(C._fileno(_fd))) // CreateFile? - hah, no -_- return _handle } @@ -160,7 +160,7 @@ pub fn get_module_filename(handle HANDLE) ?string { mut sz := int(4096) // Optimized length mut buf := &u16(malloc(4096)) for { - status := C.GetModuleFileName(handle, &buf, sz) + status := int(C.GetModuleFileNameW(handle, voidptr(&buf), sz)) match status { SUCCESS { _filename := string_from_wide2(buf, sz) @@ -209,7 +209,7 @@ fn ptr_win_get_error_msg(code u32) voidptr { FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - 0, code, C.MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &buf, 0, 0) + 0, code, C.MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), voidptr(&buf), 0, 0) return buf } @@ -237,12 +237,12 @@ pub fn exec(cmd string) ?Result { sa.nLength = sizeof(C.SECURITY_ATTRIBUTES) sa.bInheritHandle = true - create_pipe_result := C.CreatePipe(&child_stdout_read, &child_stdout_write, &sa, 0) + create_pipe_result := int(C.CreatePipe(voidptr(&child_stdout_read), voidptr(&child_stdout_write), voidptr(&sa), 0)) if create_pipe_result == 0 { error_msg := get_error_msg(int(C.GetLastError())) return error('exec failed (CreatePipe): $error_msg') } - set_handle_info_result := C.SetHandleInformation(child_stdout_read, C.HANDLE_FLAG_INHERIT, 0) + set_handle_info_result := int(C.SetHandleInformation(child_stdout_read, C.HANDLE_FLAG_INHERIT, 0)) if set_handle_info_result == 0 { error_msg := get_error_msg(int(C.GetLastError())) panic('exec failed (SetHandleInformation): $error_msg') @@ -256,8 +256,8 @@ pub fn exec(cmd string) ?Result { start_info.hStdError = child_stdout_write start_info.dwFlags = u32(C.STARTF_USESTDHANDLES) command_line := [32768]u16 - C.ExpandEnvironmentStrings(cmd.to_wide(), &command_line, 32768) - create_process_result := C.CreateProcess(0, command_line, 0, 0, C.TRUE, 0, 0, 0, &start_info, &proc_info) + C.ExpandEnvironmentStringsW(cmd.to_wide(), voidptr(&command_line), 32768) + create_process_result := int(C.CreateProcessW(0, command_line, 0, 0, C.TRUE, 0, 0, 0, voidptr(&start_info), voidptr(&proc_info))) if create_process_result == 0 { error_msg := get_error_msg(int(C.GetLastError())) return error('exec failed (CreateProcess): $error_msg') @@ -265,23 +265,23 @@ pub fn exec(cmd string) ?Result { C.CloseHandle(child_stdin) C.CloseHandle(child_stdout_write) buf := [1000]byte - mut bytes_read := 0 + mut bytes_read := u32(0) mut read_data := '' for { - readfile_result := C.ReadFile(child_stdout_read, buf, 1000, &bytes_read, 0) - read_data += tos(buf, bytes_read) - if (readfile_result == 0 || bytes_read == 0) { + readfile_result := C.ReadFile(child_stdout_read, buf, 1000, voidptr(&bytes_read), 0) + read_data += tos(buf, int(bytes_read)) + if (readfile_result == false || int(bytes_read) == 0) { break } } read_data = read_data.trim_space() - exit_code := 0 + exit_code := u32(0) C.WaitForSingleObject(proc_info.hProcess, C.INFINITE) - C.GetExitCodeProcess(proc_info.hProcess, &exit_code) + C.GetExitCodeProcess(proc_info.hProcess, voidptr(&exit_code)) C.CloseHandle(proc_info.hProcess) C.CloseHandle(proc_info.hThread) return Result { output: read_data - exit_code: exit_code + exit_code: int(exit_code) } } diff --git a/vlib/time/time.v b/vlib/time/time.v index 4ffda7cdc0..a952ebb227 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -445,7 +445,7 @@ pub fn ticks() i64 { pub fn sleep(seconds int) { $if windows { - C._sleep(seconds * 1000) + C.Sleep(seconds * 1000) } $else { C.sleep(seconds)