Skip to content

Commit

Permalink
Do not promote warnings to errors in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Feb 3, 2025
1 parent e83781e commit 9988bd6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/diag.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ typedef struct _hint_rec {
void *context;
} hint_rec_t;

static unsigned n_errors = 0;
static unsigned n_diags[DIAG_FATAL + 1];
static unsigned error_limit = 0;
static file_list_t loc_files;
static nvc_lock_t diag_lock = 0;
Expand Down Expand Up @@ -1054,10 +1054,9 @@ void diag_femit(diag_t *d, FILE *f)
show_stacktrace();
}

const bool is_error = d->level >= DIAG_ERROR
|| (opt_get_int(OPT_UNIT_TEST) && d->level > DIAG_DEBUG);
const unsigned count = relaxed_add(&n_diags[d->level], 1);

if (is_error && relaxed_add(&n_errors, 1) == error_limit)
if (d->level >= DIAG_ERROR && count == error_limit)
fatal("too many errors, giving up");

cleanup:
Expand Down Expand Up @@ -1167,14 +1166,23 @@ void diag_remove_hint_fn(diag_hint_fn_t fn)
fatal_trace("hint function %p not registered", fn);
}

unsigned diag_count(diag_level_t level)
{
int sum = 0;
for (int i = level; i <= DIAG_FATAL; i++)
sum += relaxed_load(&n_diags[i]);
return sum;
}

unsigned error_count(void)
{
return n_errors;
return diag_count(DIAG_ERROR);
}

void reset_error_count(void)
{
n_errors = 0;
for (int i = 0; i <= DIAG_FATAL; i++)
relaxed_store(&n_diags[i], 0);
}

unsigned set_error_limit(unsigned limit)
Expand Down
1 change: 1 addition & 0 deletions src/diag.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void diag_femit(diag_t *d, FILE *f);
void diag_suppress(diag_t *d, bool suppress);
void diag_stacktrace(diag_t *d, bool stacktrace);
void diag_clear(diag_t *d);
unsigned diag_count(diag_level_t level);

unsigned error_count(void);
void reset_error_count(void);
Expand Down
4 changes: 2 additions & 2 deletions test/test_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void check_expected_errors(void)
ck_abort_msg("missing expected error line %d '%s'",
error_lines->line, error_lines->snippet);

ck_assert_int_eq(errors_seen, error_count());
ck_assert_int_eq(errors_seen, diag_count(DIAG_NOTE));
}

TCase *nvc_unit_test(void)
Expand Down Expand Up @@ -211,7 +211,7 @@ tree_t _parse_and_check(const tree_kind_t *array, int num, bool simp)

void fail_if_errors(void)
{
ck_assert_msg(error_count() == 0, "have errors");
ck_assert_msg(diag_count(DIAG_WARN) == 0, "have errors");
ck_assert_msg(errors_seen == 0, "have errors or diagnostics");
}

Expand Down

0 comments on commit 9988bd6

Please sign in to comment.