11#include "prism/diagnostic.h"
2+ #include "prism/util/pm_arena.h"
23
34#define PM_DIAGNOSTIC_ID_MAX <%= errors.length + warnings.length %>
45
@@ -451,29 +452,26 @@ pm_diagnostic_level(pm_diagnostic_id_t diag_id) {
451452/**
452453 * Append an error to the given list of diagnostic.
453454 */
454- bool
455- pm_diagnostic_list_append(pm_list_t *list, uint32_t start, uint32_t length, pm_diagnostic_id_t diag_id) {
456- pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) xcalloc(1, sizeof(pm_diagnostic_t));
457- if (diagnostic == NULL) return false;
455+ void
456+ pm_diagnostic_list_append(pm_arena_t *arena, pm_list_t *list, uint32_t start, uint32_t length, pm_diagnostic_id_t diag_id) {
457+ pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) pm_arena_zalloc(arena, sizeof(pm_diagnostic_t), PRISM_ALIGNOF(pm_diagnostic_t));
458458
459459 *diagnostic = (pm_diagnostic_t) {
460460 .location = { .start = start, .length = length },
461461 .diag_id = diag_id,
462462 .message = pm_diagnostic_message(diag_id),
463- .owned = false,
464463 .level = pm_diagnostic_level(diag_id)
465464 };
466465
467466 pm_list_append(list, (pm_list_node_t *) diagnostic);
468- return true;
469467}
470468
471469/**
472470 * Append a diagnostic to the given list of diagnostics that is using a format
473471 * string for its message.
474472 */
475- bool
476- pm_diagnostic_list_append_format(pm_list_t *list, uint32_t start, uint32_t length, pm_diagnostic_id_t diag_id, ...) {
473+ void
474+ pm_diagnostic_list_append_format(pm_arena_t *arena, pm_list_t *list, uint32_t start, uint32_t length, pm_diagnostic_id_t diag_id, ...) {
477475 va_list arguments;
478476 va_start(arguments, diag_id);
479477
@@ -482,20 +480,13 @@ pm_diagnostic_list_append_format(pm_list_t *list, uint32_t start, uint32_t lengt
482480 va_end(arguments);
483481
484482 if (result < 0) {
485- return false ;
483+ return;
486484 }
487485
488- pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) xcalloc(1, sizeof(pm_diagnostic_t));
489- if (diagnostic == NULL) {
490- return false;
491- }
486+ pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) pm_arena_zalloc(arena, sizeof(pm_diagnostic_t), PRISM_ALIGNOF(pm_diagnostic_t));
492487
493488 size_t message_length = (size_t) (result + 1);
494- char *message = (char *) xmalloc(message_length);
495- if (message == NULL) {
496- xfree_sized(diagnostic, sizeof(pm_diagnostic_t));
497- return false;
498- }
489+ char *message = (char *) pm_arena_alloc(arena, message_length, 1);
499490
500491 va_start(arguments, diag_id);
501492 vsnprintf(message, message_length, format, arguments);
@@ -505,27 +496,9 @@ pm_diagnostic_list_append_format(pm_list_t *list, uint32_t start, uint32_t lengt
505496 .location = { .start = start, .length = length },
506497 .diag_id = diag_id,
507498 .message = message,
508- .owned = true,
509499 .level = pm_diagnostic_level(diag_id)
510500 };
511501
512502 pm_list_append(list, (pm_list_node_t *) diagnostic);
513- return true;
514503}
515504
516- /**
517- * Deallocate the internal state of the given diagnostic list.
518- */
519- void
520- pm_diagnostic_list_free(pm_list_t *list) {
521- pm_diagnostic_t *node = (pm_diagnostic_t *) list-> head;
522-
523- while (node != NULL) {
524- pm_diagnostic_t *next = (pm_diagnostic_t *) node-> node.next;
525-
526- if (node-> owned) xfree((void *) node-> message);
527- xfree_sized(node, sizeof(pm_diagnostic_t));
528-
529- node = next;
530- }
531- }
0 commit comments