Skip to content

Commit

Permalink
Merge pull request #2215 from ruby/inttypes
Browse files Browse the repository at this point in the history
Use inttypes for more accurate printf formatting
  • Loading branch information
kddnewton authored Jan 19, 2024
2 parents 3d67f57 + 2a22b9b commit f1fa486
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
8 changes: 8 additions & 0 deletions include/prism/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
#include <stdio.h>
#include <string.h>

/**
* We want to be able to use the PRI* macros for printing out integers, but on
* some platforms they aren't included unless this is already defined.
*/
#define __STDC_FORMAT_MACROS

#include <inttypes.h>

/**
* By default, we compile with -fvisibility=hidden. When this is enabled, we
* need to mark certain functions as being publically-visible. This macro does
Expand Down
54 changes: 29 additions & 25 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ static inline void *
pm_alloc_node(PRISM_ATTRIBUTE_UNUSED pm_parser_t *parser, size_t size) {
void *memory = calloc(1, size);
if (memory == NULL) {
fprintf(stderr, "Failed to allocate %zu bytes\n", size);
fprintf(stderr, "Failed to allocate %d bytes\n", (int) size);
abort();
}
return memory;
Expand Down Expand Up @@ -17800,13 +17800,13 @@ typedef struct {
pm_diagnostic_t *error;

/** The start line of the diagnostic message. */
size_t line;
uint32_t line;

/** The column start of the diagnostic message. */
size_t column_start;
uint32_t column_start;

/** The column end of the diagnostic message. */
size_t column_end;
uint32_t column_end;
} pm_error_t;

/** The format that will be used to format the errors into the output. */
Expand Down Expand Up @@ -17848,8 +17848,8 @@ pm_parser_errors_format_sort(const pm_list_t *error_list, const pm_newline_list_
(index < error_list->size) &&
(errors[index].error != NULL) &&
(
(errors[index].line < start.line) ||
(errors[index].line == start.line && errors[index].column_start < start.column)
(errors[index].line < ((uint32_t) start.line)) ||
(errors[index].line == ((uint32_t) start.line) && errors[index].column_start < ((uint32_t) start.column))
)
) index++;

Expand All @@ -17858,20 +17858,20 @@ pm_parser_errors_format_sort(const pm_list_t *error_list, const pm_newline_list_
memcpy(&errors[index + 1], &errors[index], sizeof(pm_error_t) * (error_list->size - index - 1));

// Finally, we'll insert the error into the array.
size_t column_end;
uint32_t column_end;
if (start.line == end.line) {
column_end = end.column;
column_end = (uint32_t) end.column;
} else {
column_end = newline_list->offsets[start.line + 1] - newline_list->offsets[start.line] - 1;
column_end = (uint32_t) (newline_list->offsets[start.line + 1] - newline_list->offsets[start.line] - 1);
}

// Ensure we have at least one column of error.
if (start.column == column_end) column_end++;
if (((uint32_t) start.column) == column_end) column_end++;

errors[index] = (pm_error_t) {
.error = error,
.line = start.line,
.column_start = start.column,
.line = (uint32_t) start.line,
.column_start = (uint32_t) start.column,
.column_end = column_end
};
}
Expand All @@ -17884,14 +17884,18 @@ pm_parser_errors_format_line(const pm_parser_t *parser, const pm_newline_list_t
const uint8_t *start = &parser->start[newline_list->offsets[line]];
const uint8_t *end;

if (line + 1 > newline_list->size) {
if (line + 1 >= newline_list->size) {
end = parser->end;
} else {
end = &parser->start[newline_list->offsets[line + 1]];
}

pm_buffer_append_format(buffer, number_prefix, line + 1);
pm_buffer_append_format(buffer, number_prefix, (uint32_t) (line + 1));
pm_buffer_append_string(buffer, (const char *) start, (size_t) (end - start));

if (end == parser->end && end[-1] != '\n') {
pm_buffer_append_string(buffer, "\n", 1);
}
}

/**
Expand All @@ -17916,69 +17920,69 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
if (max_line_number < 10) {
if (colorize) {
error_format = (pm_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%1zu | " PM_COLOR_RESET,
.number_prefix = PM_COLOR_GRAY "%1" PRIu32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_error_format_t) {
.number_prefix = "%1zu | ",
.number_prefix = "%1" PRIu32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~\n"
};
}
} else if (max_line_number < 100) {
if (colorize) {
error_format = (pm_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%2zu | " PM_COLOR_RESET,
.number_prefix = PM_COLOR_GRAY "%2" PRIu32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_error_format_t) {
.number_prefix = "%2zu | ",
.number_prefix = "%2" PRIu32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~~\n"
};
}
} else if (max_line_number < 1000) {
if (colorize) {
error_format = (pm_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%3zu | " PM_COLOR_RESET,
.number_prefix = PM_COLOR_GRAY "%3" PRIu32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_error_format_t) {
.number_prefix = "%3zu | ",
.number_prefix = "%3" PRIu32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~~~\n"
};
}
} else if (max_line_number < 10000) {
if (colorize) {
error_format = (pm_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%4zu | " PM_COLOR_RESET,
.number_prefix = PM_COLOR_GRAY "%4" PRIu32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_error_format_t) {
.number_prefix = "%4zu | ",
.number_prefix = "%4" PRIu32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~~~~\n"
};
}
} else {
if (colorize) {
error_format = (pm_error_format_t) {
.number_prefix = PM_COLOR_GRAY "%5zu | " PM_COLOR_RESET,
.number_prefix = PM_COLOR_GRAY "%5" PRIu32 " | " PM_COLOR_RESET,
.blank_prefix = PM_COLOR_GRAY " | " PM_COLOR_RESET,
.divider = PM_COLOR_GRAY " ~~~~~~~~" PM_COLOR_RESET "\n"
};
} else {
error_format = (pm_error_format_t) {
.number_prefix = "%5zu | ",
.number_prefix = "%5" PRIu32 " | ",
.blank_prefix = " | ",
.divider = " ~~~~~~~~\n"
};
Expand All @@ -17993,7 +17997,7 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
// the source before the error to give some context. We'll be careful not to
// display the same line twice in case the errors are close enough in the
// source.
size_t last_line = (size_t) -1;
uint32_t last_line = (uint32_t) -1;
const pm_encoding_t *encoding = parser->encoding;

for (size_t index = 0; index < error_list->size; index++) {
Expand Down
6 changes: 4 additions & 2 deletions templates/src/prettyprint.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ prettyprint_node(pm_buffer_t *output_buffer, const pm_parser_t *parser, const pm
prettyprint_source(output_buffer, location->start, (size_t) (location->end - location->start));
pm_buffer_append_string(output_buffer, "\"\n", 2);
}
<%- when Prism::UInt8Field, Prism::UInt32Field -%>
pm_buffer_append_format(output_buffer, " %d\n", cast-><%= field.name %>);
<%- when Prism::UInt8Field -%>
pm_buffer_append_format(output_buffer, " %" PRIu8 "\n", cast-><%= field.name %>);
<%- when Prism::UInt32Field -%>
pm_buffer_append_format(output_buffer, " %" PRIu32 "\n", cast-><%= field.name %>);
<%- when Prism::FlagsField -%>
bool found = false;
<%- found = flags.find { |flag| flag.name == field.kind }.tap { |found| raise "Expected to find #{field.kind}" unless found } -%>
Expand Down

0 comments on commit f1fa486

Please sign in to comment.