Skip to content

Commit 653ee2c

Browse files
committed
reduce Windows warnings
1 parent 5ed69e2 commit 653ee2c

File tree

13 files changed

+2993
-90
lines changed

13 files changed

+2993
-90
lines changed

1qqq

Lines changed: 2869 additions & 0 deletions
Large diffs are not rendered by default.

cli/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,12 @@ static inline void print_version(void)
180180

181181
static inline FILE *open_file(const char *filename, const char *mode)
182182
{
183-
FILE *stream = fopen(filename, mode);
183+
FILE *stream = NULL;
184+
#ifdef _WIN32
185+
(void) fopen_s(&stream, filename, mode);
186+
#else
187+
stream = fopen(filename, mode);
188+
#endif
184189
if (!stream)
185190
fatal_error("unable to open file `%s`", filename);
186191
return stream;

core/io.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#ifdef _WIN32
1010
#include <windows.h>
11+
#include <io.h>
1112
#endif
1213

1314
#ifndef _WIN32
@@ -66,7 +67,12 @@ static void open_call(HkState *state, HkValue *args)
6667
hk_return_if_not_ok(state);
6768
HkString *filename = hk_as_string(args[1]);
6869
HkString *mode = hk_as_string(args[2]);
69-
FILE *stream = fopen(filename->chars, mode->chars);
70+
FILE *stream = NULL;
71+
#ifdef _WIN32
72+
(void) fopen_s(&stream, filename->chars, mode->chars);
73+
#else
74+
stream = fopen(filename->chars, mode->chars);
75+
#endif
7076
if (!stream)
7177
{
7278
hk_state_push_nil(state);
@@ -133,11 +139,13 @@ static void sync_call(HkState *state, HkValue *args)
133139
hk_state_check_argument_userdata(state, args, 1);
134140
hk_return_if_not_ok(state);
135141
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
136-
int fd = fileno(stream);
137142
bool result;
138143
#ifdef _WIN32
139-
result = FlushFileBuffers(fd);
144+
int fd = _fileno(stream);
145+
HANDLE handle = (HANDLE) _get_osfhandle(fd);
146+
result = FlushFileBuffers(handle);
140147
#else
148+
int fd = fileno(stream);
141149
result = !fsync(fd);
142150
#endif
143151
hk_state_push_bool(state, result);
@@ -169,7 +177,7 @@ static void seek_call(HkState *state, HkValue *args)
169177
hk_state_check_argument_int(state, args, 3);
170178
hk_return_if_not_ok(state);
171179
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
172-
int64_t offset = (int64_t) hk_as_number(args[2]);
180+
long offset = (long) hk_as_number(args[2]);
173181
int whence = (int) hk_as_number(args[3]);
174182
hk_state_push_number(state, fseek(stream, offset, whence));
175183
}
@@ -181,7 +189,7 @@ static void read_call(HkState *state, HkValue *args)
181189
hk_state_check_argument_int(state, args, 2);
182190
hk_return_if_not_ok(state);
183191
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
184-
int64_t size = (int64_t) hk_as_number(args[2]);
192+
int size = (int) hk_as_number(args[2]);
185193
HkString *str = hk_string_new_with_capacity(size);
186194
int length = (int) fread(str->chars, 1, size, stream);
187195
if (length < size && !feof(stream))
@@ -205,8 +213,8 @@ static void write_call(HkState *state, HkValue *args)
205213
hk_return_if_not_ok(state);
206214
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
207215
HkString *str = hk_as_string(args[2]);
208-
size_t size = str->length;
209-
if (fwrite(str->chars, 1, size, stream) < size)
216+
int size = str->length;
217+
if ((int) fwrite(str->chars, 1, size, stream) < size)
210218
{
211219
hk_state_push_nil(state);
212220
return;
@@ -230,8 +238,9 @@ static void writeln_call(HkState *state, HkValue *args)
230238
hk_return_if_not_ok(state);
231239
FILE *stream = ((File *) hk_as_userdata(args[1]))->stream;
232240
HkString *str = hk_as_string(args[2]);
233-
size_t size = str->length;
234-
if (fwrite(str->chars, 1, size, stream) < size || fwrite("\n", 1, 1, stream) < 1)
241+
int size = str->length;
242+
if ((int) fwrite(str->chars, 1, size, stream) < size
243+
|| fwrite("\n", 1, 1, stream) < 1)
235244
{
236245
hk_state_push_nil(state);
237246
return;

core/json.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ static inline cJSON *value_to_json(HkValue val)
5757
for (int i = 0; i < ztruct->length; ++i)
5858
{
5959
HkField field = fields[i];
60-
HkValue val = inst->values[i];
61-
cJSON *json_val = value_to_json(val);
62-
hk_assert(cJSON_AddItemToObject(json, field.name->chars, json_val), "Failed to add item to object.");
60+
cJSON *json_val = value_to_json(inst->values[i]);
61+
hk_assert(cJSON_AddItemToObject(json, field.name->chars, json_val),
62+
"Failed to add item to object.");
6363
}
6464
}
6565
break;

core/selectors.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static inline bool poll_selector_register(PollSelector *selector,
108108
if (selector->count == MAX_FDS) return false;
109109
PollFd fd = {
110110
.fd = (int) udata->sock,
111-
.events = events,
111+
.events = (short) events,
112112
.revents = 0
113113
};
114114
int index = selector->count;
@@ -149,7 +149,7 @@ static inline bool poll_selector_modify(PollSelector *selector,
149149
if (fd->fd == (int) udata->sock) break;
150150
}
151151
if (i == n) return false;
152-
selector->fds[i].events = events;
152+
selector->fds[i].events = (short) events;
153153
return true;
154154
}
155155

core/socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static void bind_call(HkState *state, HkValue *args)
267267
}
268268
struct sockaddr_in sock_addr;
269269
memset(&sock_addr, 0, sizeof(sock_addr));
270-
sock_addr.sin_family = udata->domain;
270+
sock_addr.sin_family = (unsigned short) udata->domain;
271271
sock_addr.sin_port = htons((uint16_t) port);
272272
if (inet_pton(AF_INET, address, &sock_addr.sin_addr) < 1)
273273
{

src/builtin.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,12 @@ static inline HkArray *split(HkString *str, HkString *sep)
136136
// TODO: Do not use strtok_r and do not copy the string
137137
HkString *_str = hk_string_copy(str);
138138
char *cur = _str->chars;
139-
char *tk;
140-
while ((tk = strtok_r(cur, sep->chars, &cur)))
139+
char *tk = strtok_r(cur, sep->chars, &cur);
140+
while (tk)
141141
{
142142
HkValue elem = hk_string_value(hk_string_from_chars(-1, tk));
143143
hk_array_inplace_add_element(arr, elem);
144+
tk = strtok_r(cur, sep->chars, &cur);
144145
}
145146
hk_string_free(_str);
146147
return arr;
@@ -267,13 +268,13 @@ static void to_int_call(HkState *state, HkValue *args)
267268
HkValue val = args[1];
268269
if (hk_is_number(val))
269270
{
270-
hk_state_push_number(state, (int64_t) hk_as_number(val));
271+
hk_state_push_number(state, (double) ((int64_t) hk_as_number(val)));
271272
return;
272273
}
273274
double result;
274275
string_to_double(state, hk_as_string(val), &result);
275276
hk_return_if_not_ok(state);
276-
hk_state_push_number(state, (int64_t) result);
277+
hk_state_push_number(state, (double) ((int64_t) result));
277278
}
278279

279280
static void to_number_call(HkState *state, HkValue *args)
@@ -405,7 +406,11 @@ static void bin_call(HkState *state, HkValue *args)
405406
char *chars = str->chars;
406407
for (int i = 0; i < length; ++i)
407408
{
409+
#ifdef _WIN32
410+
sscanf_s(chars, "%2hhx", (unsigned char *) &result->chars[i]);
411+
#else
408412
sscanf(chars, "%2hhx", (unsigned char *) &result->chars[i]);
413+
#endif
409414
chars += 2;
410415
}
411416
hk_state_push_string(state, result);
@@ -466,14 +471,14 @@ static void len_call(HkState *state, HkValue *args)
466471
HkRange *range = hk_as_range(val);
467472
if (range->start < range->end)
468473
{
469-
int result = (int) range->end - range->start + 1;
470-
hk_state_push_number(state, result);
474+
int64_t result = range->end - range->start + 1;
475+
hk_state_push_number(state, (double) result);
471476
return;
472477
}
473478
if (range->start > range->end)
474479
{
475-
int result = (int) range->start - range->end + 1;
476-
hk_state_push_number(state, result);
480+
int64_t result = range->start - range->end + 1;
481+
hk_state_push_number(state, (double) result);
477482
return;
478483
}
479484
hk_state_push_number(state, 1);

src/callable.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,10 @@ HkFunction *hk_function_deserialize(FILE *stream)
122122
HkFunction **functions = (HkFunction **) hk_allocate(sizeof(*functions) * fn->functionsCapacity);
123123
for (int i = 0; i < fn->functionsLength; ++i)
124124
{
125-
HkFunction *fn = hk_function_deserialize(stream);
126-
if (!fn)
127-
return NULL;
128-
hk_incr_ref(fn);
129-
functions[i] = fn;
125+
HkFunction *nestedFn = hk_function_deserialize(stream);
126+
if (!nestedFn) return NULL;
127+
hk_incr_ref(nestedFn);
128+
functions[i] = nestedFn;
130129
}
131130
fn->functions = functions;
132131
if (fread(&fn->numNonlocals, sizeof(fn->numNonlocals), 1, stream) != 1)

src/compiler.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ typedef enum
3232

3333
#define consume(c, k) do \
3434
{ \
35-
Scanner *scan = (c)->scan; \
36-
if (!match(scan, k)) \
35+
if (!match((c)->scan, k)) \
3736
syntax_error_unexpected(c); \
38-
scanner_next_token(scan); \
37+
scanner_next_token((c)->scan); \
3938
} while(0)
4039

4140
#define add_placeholder(c) do \
@@ -604,10 +603,10 @@ static void compile_import_statement(Compiler *comp)
604603
scanner_next_token(scan);
605604
if (!match(scan, TOKEN_KIND_NAME))
606605
syntax_error_unexpected(comp);
607-
Token tk = scan->token;
606+
tk = scan->token;
608607
scanner_next_token(scan);
609608
define_local(comp, &tk, false);
610-
uint8_t index = add_string_constant(comp, &tk);
609+
index = add_string_constant(comp, &tk);
611610
hk_chunk_emit_opcode(chunk, HK_OP_CONSTANT);
612611
hk_chunk_emit_byte(chunk, index);
613612
++n;
@@ -697,11 +696,11 @@ static void compile_constant_declaration(Compiler *comp)
697696
scanner_next_token(scan);
698697
if (!match(scan, TOKEN_KIND_NAME))
699698
syntax_error_unexpected(comp);
700-
Token tk = scan->token;
699+
tk = scan->token;
701700
scanner_next_token(scan);
702701
// FIXME: This is a bug, we should not define the local here
703702
define_local(comp, &tk, false);
704-
uint8_t index = add_string_constant(comp, &tk);
703+
index = add_string_constant(comp, &tk);
705704
hk_chunk_emit_opcode(chunk, HK_OP_CONSTANT);
706705
hk_chunk_emit_byte(chunk, index);
707706
++n;
@@ -790,11 +789,11 @@ static void compile_variable_declaration(Compiler *comp)
790789
scanner_next_token(scan);
791790
if (!match(scan, TOKEN_KIND_NAME))
792791
syntax_error_unexpected(comp);
793-
Token tk = scan->token;
792+
tk = scan->token;
794793
scanner_next_token(scan);
795794
// FIXME: This is a bug, we should not define the local here
796795
define_local(comp, &tk, true);
797-
uint8_t index = add_string_constant(comp, &tk);
796+
index = add_string_constant(comp, &tk);
798797
hk_chunk_emit_opcode(chunk, HK_OP_CONSTANT);
799798
hk_chunk_emit_byte(chunk, index);
800799
++n;
@@ -1478,7 +1477,7 @@ static void compile_for_statement(Compiler *comp)
14781477
}
14791478
uint16_t jump1 = (uint16_t) chunk->codeLength;
14801479
bool missing = match(scan, TOKEN_KIND_SEMICOLON);
1481-
int offset1;
1480+
int offset1 = 0;
14821481
if (missing)
14831482
scanner_next_token(scan);
14841483
else
@@ -1509,7 +1508,10 @@ static void compile_for_statement(Compiler *comp)
15091508
hk_chunk_emit_opcode(chunk, HK_OP_JUMP);
15101509
hk_chunk_emit_word(chunk, jump2);
15111510
if (!missing)
1511+
{
1512+
hk_assert(offset1, "offset1 is zero");
15121513
patch_jump(comp, offset1);
1514+
}
15131515
end_loop(comp);
15141516
pop_scope(comp);
15151517
}
@@ -2254,7 +2256,7 @@ static Variable compile_variable(Compiler *comp, Token *tk, bool emit)
22542256
return (Variable) {
22552257
.isLocal = false,
22562258
.depth = -1,
2257-
.index = index,
2259+
.index = (uint8_t) index,
22582260
.length = tk->length,
22592261
.start = tk->start,
22602262
.isMutable = false

src/dump.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,6 @@ void hk_dump(HkFunction *fn, FILE *stream)
289289
}
290290
}
291291
fprintf(stream, "; %d instruction(s)\n\n", n);
292-
for (int i = 0; i < fn->functionsLength; ++i)
293-
hk_dump(fn->functions[i], stream);
292+
for (int j = 0; j < fn->functionsLength; ++j)
293+
hk_dump(fn->functions[j], stream);
294294
}

0 commit comments

Comments
 (0)