From 182bbc350ed8b3c547133a9a44a4f30a0ba3b77e Mon Sep 17 00:00:00 2001 From: Snild Dolkow Date: Mon, 29 Jan 2024 16:43:32 +0100 Subject: [PATCH] tests: Make it clear to clang-tidy that assert_true may not return The key is to have __attribute__((noreturn)) somewhere that clang-tidy can see it. In this case, this is the _fail() function, which is conditionally called from the assert_true() macro. This will ensure that clang-tidy doesn't complain about NULL values that we've asserted against in tests. --- expat/tests/basic_tests.c | 2 +- expat/tests/common.c | 6 +++--- expat/tests/handlers.c | 4 +++- expat/tests/minicheck.c | 5 +---- expat/tests/minicheck.h | 14 +++++++++++--- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/expat/tests/basic_tests.c b/expat/tests/basic_tests.c index ed7d1a08..1e9b7782 100644 --- a/expat/tests/basic_tests.c +++ b/expat/tests/basic_tests.c @@ -3163,7 +3163,7 @@ static int XMLCALL external_bom_checker(XML_Parser parser, const XML_Char *context, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId) { - const char *text = ""; + const char *text; UNUSED_P(base); UNUSED_P(systemId); UNUSED_P(publicId); diff --git a/expat/tests/common.c b/expat/tests/common.c index 4a552e60..ccdc5973 100644 --- a/expat/tests/common.c +++ b/expat/tests/common.c @@ -185,7 +185,7 @@ _xml_failure(XML_Parser parser, const char *file, int line) { "u, offset %" XML_FMT_INT_MOD "u)\n reported from %s, line %d\n", err, XML_ErrorString(err), XML_GetCurrentLineNumber(parser), XML_GetCurrentColumnNumber(parser), file, line); - _assert_true(0, file, line, buffer); + _fail(file, line, buffer); } enum XML_Status @@ -214,9 +214,9 @@ _expect_failure(const char *text, enum XML_Error errorCode, const char *errorMessage, const char *file, int lineno) { if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) == XML_STATUS_OK) - /* Hackish use of _assert_true() macro, but let's us report + /* Hackish use of _fail() macro, but lets us report the right filename and line number. */ - _assert_true(0, file, lineno, errorMessage); + _fail(file, lineno, errorMessage); if (XML_GetErrorCode(g_parser) != errorCode) _xml_failure(g_parser, file, lineno); } diff --git a/expat/tests/handlers.c b/expat/tests/handlers.c index 0fb93eed..d707b914 100644 --- a/expat/tests/handlers.c +++ b/expat/tests/handlers.c @@ -1717,7 +1717,9 @@ record_element_end_handler(void *userData, const XML_Char *name) { const struct handler_record_entry * _handler_record_get(const struct handler_record_list *storage, int index, const char *file, int line) { - _assert_true(storage->count > index, file, line, "too few handler calls"); + if (storage->count <= index) { + _fail(file, line, "too few handler calls"); + } return &storage->entries[index]; } diff --git a/expat/tests/minicheck.c b/expat/tests/minicheck.c index fe6949a3..44d10daa 100644 --- a/expat/tests/minicheck.c +++ b/expat/tests/minicheck.c @@ -244,14 +244,11 @@ srunner_summarize(SRunner *runner, int verbosity) { } void -_assert_true(int condition, const char *file, int line, const char *msg) { +_fail(const char *file, int line, const char *msg) { /* Always print the error message so it isn't lost. In this case, we have a failure, so there's no reason to be quiet about what it is. */ - if (condition) { - return; - } _check_current_filename = file; _check_current_lineno = line; if (msg != NULL) { diff --git a/expat/tests/minicheck.h b/expat/tests/minicheck.h index 86a24068..60612e18 100644 --- a/expat/tests/minicheck.h +++ b/expat/tests/minicheck.h @@ -83,9 +83,13 @@ extern "C" { void PRINTF_LIKE(1, 2) set_subtest(char const *fmt, ...); -# define fail(msg) _assert_true(0, __FILE__, __LINE__, msg) +# define fail(msg) _fail(__FILE__, __LINE__, msg) # define assert_true(cond) \ - _assert_true((cond), __FILE__, __LINE__, "check failed: " #cond) + do { \ + if (! (cond)) { \ + _fail(__FILE__, __LINE__, "check failed: " #cond); \ + } \ + } while (0) typedef void (*tcase_setup_function)(void); typedef void (*tcase_teardown_function)(void); @@ -124,7 +128,11 @@ void _check_set_test_info(char const *function, char const *filename, * Prototypes for the actual implementation. */ -void _assert_true(int condition, const char *file, int line, const char *msg); +# if defined(__GNUC__) +__attribute__((noreturn)) +# endif +void +_fail(const char *file, int line, const char *msg); Suite *suite_create(const char *name); TCase *tcase_create(const char *name); void suite_add_tcase(Suite *suite, TCase *tc);