Skip to content

Commit

Permalink
Merge pull request #1179 from ruby/conversion
Browse files Browse the repository at this point in the history
Enable all of -wconversion
  • Loading branch information
kddnewton authored Jul 31, 2023
2 parents ac43a40 + 638163f commit e615c26
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ SOEXT := $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]')

DEFS := @DEFS@
CPPFLAGS := @DEFS@ -Iinclude
CFLAGS := @CFLAGS@ -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wsign-conversion -fPIC -fvisibility=hidden
CFLAGS := @CFLAGS@ -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -fPIC -fvisibility=hidden
CC := @CC@

HEADERS := $(shell find include -name '*.h')
Expand Down
2 changes: 1 addition & 1 deletion include/yarp/util/yp_char.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ size_t yp_strspn_whitespace(const char *string, ptrdiff_t length);
// whitespace while also tracking the location of each newline. Disallows
// searching past the given maximum number of characters.
size_t
yp_strspn_whitespace_newlines(const char *string, long length, yp_newline_list_t *newline_list, bool);
yp_strspn_whitespace_newlines(const char *string, ptrdiff_t length, yp_newline_list_t *newline_list, bool);

// Returns the number of characters at the start of the string that are inline
// whitespace. Disallows searching past the given maximum number of characters.
Expand Down
8 changes: 5 additions & 3 deletions src/regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ typedef enum {
#define YP_REGEXP_OPTION_STATE_SLOTS (YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM + 1)

// This is the set of options that are configurable on the regular expression.
typedef struct { unsigned char values[YP_REGEXP_OPTION_STATE_SLOTS]; } yp_regexp_options_t;
typedef struct {
unsigned char values[YP_REGEXP_OPTION_STATE_SLOTS];
} yp_regexp_options_t;

// Initialize a new set of options to their default values.
static void
Expand All @@ -300,7 +302,7 @@ yp_regexp_options_init(yp_regexp_options_t *options) {
static bool
yp_regexp_options_add(yp_regexp_options_t *options, unsigned char key) {
if (key >= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM && key <= YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM) {
key -= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM;
key = (unsigned char) (key - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM);

switch (options->values[key]) {
case YP_REGEXP_OPTION_STATE_INVALID:
Expand All @@ -323,7 +325,7 @@ yp_regexp_options_add(yp_regexp_options_t *options, unsigned char key) {
static bool
yp_regexp_options_remove(yp_regexp_options_t *options, unsigned char key) {
if (key >= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM && key <= YP_REGEXP_OPTION_STATE_SLOT_MAXIMUM) {
key -= YP_REGEXP_OPTION_STATE_SLOT_MINIMUM;
key = (unsigned char) (key - YP_REGEXP_OPTION_STATE_SLOT_MINIMUM);

switch (options->values[key]) {
case YP_REGEXP_OPTION_STATE_INVALID:
Expand Down
28 changes: 14 additions & 14 deletions src/unescape.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ unescape_octal(const char *backslash, unsigned char *value) {
return 2;
}

*value = (*value << 3) | (backslash[2] - '0');
*value = (unsigned char) ((*value << 3) | (backslash[2] - '0'));
if (!yp_char_is_octal_digit(backslash[3])) {
return 3;
}

*value = (*value << 3) | (backslash[3] - '0');
*value = (unsigned char) ((*value << 3) | (backslash[3] - '0'));
return 4;
}

// Convert a hexadecimal digit into its equivalent value.
static inline unsigned char
unescape_hexadecimal_digit(const char value) {
return (value <= '9') ? (unsigned char) (value - '0') : (value & 0x7) + 9;
return (unsigned char) ((value <= '9') ? (value - '0') : (value & 0x7) + 9);
}

// Scan the 1-2 digits of hexadecimal into the value. Returns the number of
Expand All @@ -88,7 +88,7 @@ unescape_hexadecimal(const char *backslash, unsigned char *value) {
return 3;
}

*value = (*value << 4) | unescape_hexadecimal_digit(backslash[3]);
*value = (unsigned char) ((*value << 4) | unescape_hexadecimal_digit(backslash[3]));
return 4;
}

Expand All @@ -113,33 +113,33 @@ unescape_unicode_write(char *dest, uint32_t value, const char *start, const char

if (value <= 0x7F) {
// 0xxxxxxx
bytes[0] = value;
bytes[0] = (unsigned char) value;
return 1;
}

if (value <= 0x7FF) {
// 110xxxxx 10xxxxxx
bytes[0] = 0xC0 | (value >> 6);
bytes[1] = 0x80 | (value & 0x3F);
bytes[0] = (unsigned char) (0xC0 | (value >> 6));
bytes[1] = (unsigned char) (0x80 | (value & 0x3F));
return 2;
}

if (value <= 0xFFFF) {
// 1110xxxx 10xxxxxx 10xxxxxx
bytes[0] = 0xE0 | (value >> 12);
bytes[1] = 0x80 | ((value >> 6) & 0x3F);
bytes[2] = 0x80 | (value & 0x3F);
bytes[0] = (unsigned char) (0xE0 | (value >> 12));
bytes[1] = (unsigned char) (0x80 | ((value >> 6) & 0x3F));
bytes[2] = (unsigned char) (0x80 | (value & 0x3F));
return 3;
}

// At this point it must be a 4 digit UTF-8 representation. If it's not, then
// the input is invalid.
if (value <= 0x10FFFF) {
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
bytes[0] = 0xF0 | (value >> 18);
bytes[1] = 0x80 | ((value >> 12) & 0x3F);
bytes[2] = 0x80 | ((value >> 6) & 0x3F);
bytes[3] = 0x80 | (value & 0x3F);
bytes[0] = (unsigned char) (0xF0 | (value >> 18));
bytes[1] = (unsigned char) (0x80 | ((value >> 12) & 0x3F));
bytes[2] = (unsigned char) (0x80 | ((value >> 6) & 0x3F));
bytes[3] = (unsigned char) (0x80 | (value & 0x3F));
return 4;
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/yp_char.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ yp_strspn_whitespace(const char *string, ptrdiff_t length) {
// whitespace while also tracking the location of each newline. Disallows
// searching past the given maximum number of characters.
size_t
yp_strspn_whitespace_newlines(const char *string, long length, yp_newline_list_t *newline_list, bool stop_at_newline) {
yp_strspn_whitespace_newlines(const char *string, ptrdiff_t length, yp_newline_list_t *newline_list, bool stop_at_newline) {
if (length <= 0) return 0;

size_t size = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/util/yp_constant_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ yp_constant_pool_init(yp_constant_pool_t *pool, size_t capacity) {
// if any potential calls to resize fail.
yp_constant_id_t
yp_constant_pool_insert(yp_constant_pool_t *pool, const char *start, size_t length) {
if (pool->size >= pool->capacity * 0.75) {
if (pool->size >= (pool->capacity / 4 * 3)) {
if (!yp_constant_pool_resize(pool)) return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/yarp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5956,7 +5956,7 @@ parser_lex(yp_parser_t *parser) {
}

size_t ident_length = (size_t) (parser->current.end - ident_start);
if (quote != YP_HEREDOC_QUOTE_NONE && !match(parser, quote)) {
if (quote != YP_HEREDOC_QUOTE_NONE && !match(parser, (char) quote)) {
// TODO: handle unterminated heredoc
}

Expand Down
36 changes: 18 additions & 18 deletions templates/src/serialize.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <stdio.h>

static inline uint32_t
yp_long_to_u32(long value) {
assert(value >= 0 && (unsigned long)value < UINT32_MAX);
yp_ptrdifft_to_u32(ptrdiff_t value) {
assert(value >= 0 && ((unsigned long) value) < UINT32_MAX);
return (uint32_t) value;
}

static inline uint32_t
yp_ulong_to_u32(unsigned long value) {
yp_sizet_to_u32(size_t value) {
assert(value < UINT32_MAX);
return (uint32_t) value;
}
Expand All @@ -22,13 +22,13 @@ serialize_location(yp_parser_t *parser, yp_location_t *location, yp_buffer_t *bu
assert(location->end);
assert(location->start <= location->end);

yp_buffer_append_u32(buffer, yp_long_to_u32(location->start - parser->start));
yp_buffer_append_u32(buffer, yp_long_to_u32(location->end - location->start));
yp_buffer_append_u32(buffer, yp_ptrdifft_to_u32(location->start - parser->start));
yp_buffer_append_u32(buffer, yp_ptrdifft_to_u32(location->end - location->start));
}

void
yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
yp_buffer_append_u8(buffer, YP_NODE_TYPE(node));
yp_buffer_append_u8(buffer, (uint8_t) YP_NODE_TYPE(node));

size_t offset = buffer->length;

Expand All @@ -54,28 +54,28 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
yp_serialize_node(parser, (yp_node_t *)((yp_<%= node.human %>_t *)node)-><%= param.name %>, buffer);
}
<%- when StringParam -%>
uint32_t <%= param.name %>_length = yp_ulong_to_u32(yp_string_length(&((yp_<%= node.human %>_t *)node)-><%= param.name %>));
uint32_t <%= param.name %>_length = yp_sizet_to_u32(yp_string_length(&((yp_<%= node.human %>_t *)node)-><%= param.name %>));
yp_buffer_append_u32(buffer, <%= param.name %>_length);
yp_buffer_append_str(buffer, yp_string_source(&((yp_<%= node.human %>_t *)node)-><%= param.name %>), <%= param.name %>_length);
<%- when NodeListParam -%>
uint32_t <%= param.name %>_size = yp_ulong_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
uint32_t <%= param.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
yp_buffer_append_u32(buffer, <%= param.name %>_size);
for (uint32_t index = 0; index < <%= param.name %>_size; index++) {
yp_serialize_node(parser, (yp_node_t *) ((yp_<%= node.human %>_t *)node)-><%= param.name %>.nodes[index], buffer);
}
<%- when LocationListParam -%>
uint32_t <%= param.name %>_size = yp_ulong_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
uint32_t <%= param.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
yp_buffer_append_u32(buffer, <%= param.name %>_size);
for (uint32_t index = 0; index < <%= param.name %>_size; index++) {
serialize_location(parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>.locations[index], buffer);
}
<%- when ConstantParam -%>
yp_buffer_append_u32(buffer, yp_ulong_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>));
yp_buffer_append_u32(buffer, yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>));
<%- when ConstantListParam -%>
uint32_t <%= param.name %>_size = yp_ulong_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
uint32_t <%= param.name %>_size = yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.size);
yp_buffer_append_u32(buffer, <%= param.name %>_size);
for (uint32_t index = 0; index < <%= param.name %>_size; index++) {
yp_buffer_append_u32(buffer, yp_ulong_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.ids[index]));
yp_buffer_append_u32(buffer, yp_sizet_to_u32(((yp_<%= node.human %>_t *)node)-><%= param.name %>.ids[index]));
}
<%- when LocationParam -%>
serialize_location(parser, &((yp_<%= node.human %>_t *)node)-><%= param.name %>, buffer);
Expand All @@ -94,7 +94,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
<%- end -%>
<%- if node.needs_serialized_length? -%>
// serialize length
uint32_t length = yp_ulong_to_u32(buffer->length - offset - sizeof(uint32_t));
uint32_t length = yp_sizet_to_u32(buffer->length - offset - sizeof(uint32_t));
memcpy(buffer->value + length_offset, &length, sizeof(uint32_t));
<%- end -%>
break;
Expand All @@ -107,7 +107,7 @@ void
yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
// First, serialize the encoding of the parser.
size_t encoding_length = strlen(parser->encoding.name);
yp_buffer_append_u32(buffer, yp_ulong_to_u32(encoding_length));
yp_buffer_append_u32(buffer, yp_sizet_to_u32(encoding_length));
yp_buffer_append_str(buffer, parser->encoding.name, encoding_length);

// Here we're going to leave space for the offset of the constant pool in
Expand All @@ -116,14 +116,14 @@ yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer)
yp_buffer_append_zeroes(buffer, 4);

// Next, encode the length of the constant pool.
yp_buffer_append_u32(buffer, yp_ulong_to_u32(parser->constant_pool.size));
yp_buffer_append_u32(buffer, yp_sizet_to_u32(parser->constant_pool.size));

// Now we're going to serialize the content of the node.
yp_serialize_node(parser, node, buffer);

// Now we're going to serialize the offset of the constant pool back where
// we left space for it.
uint32_t length = yp_ulong_to_u32(buffer->length);
uint32_t length = yp_sizet_to_u32(buffer->length);
memcpy(buffer->value + offset, &length, sizeof(uint32_t));

// Now we're going to serialize the constant pool.
Expand All @@ -139,8 +139,8 @@ yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer)
if (constant->id != 0) {
size_t buffer_offset = offset + ((constant->id - 1) * 8);

uint32_t source_offset = yp_long_to_u32(constant->start - parser->start);
uint32_t constant_length = yp_ulong_to_u32(constant->length);
uint32_t source_offset = yp_ptrdifft_to_u32(constant->start - parser->start);
uint32_t constant_length = yp_sizet_to_u32(constant->length);

memcpy(buffer->value + buffer_offset, &source_offset, 4);
memcpy(buffer->value + buffer_offset + 4, &constant_length, 4);
Expand Down

0 comments on commit e615c26

Please sign in to comment.