unit tests: extract "private" prototypes at build time

In order to do unit tests for private functions, functions that are
marked UNITTEST but without a global scope in the library, functions
that do not have prototypes in their corresponding header file, unit
tests previously brought their own private prototype *copy* into the
unit test.

This was error-prone when the internal function changes but the change
might be missed in the unit test which then uses an outdated prototype
copy for testing.

This change removes the private prototypes from unit tests and instead
introduces a C file parser that parses the specific C files and extracts
the necessary unit test prototypes into a generated header file for unit
tests to use. This geneated lib/unitprotos.h header is then included by
unit tests that need private prototypes.

Assisted-by: Viktor Szakats
Closes #17750
This commit is contained in:
Daniel Stenberg 2025-06-29 14:33:13 +02:00
parent 48d3407d7c
commit c9bb9cd165
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
10 changed files with 125 additions and 13 deletions

View file

@ -35,10 +35,11 @@ add_custom_command(OUTPUT "${BUNDLE}.c"
VERBATIM)
add_executable(${BUNDLE} EXCLUDE_FROM_ALL "${BUNDLE}.c")
add_dependencies(${BUNDLE} curlu-unitprotos)
add_dependencies(testdeps ${BUNDLE})
target_link_libraries(${BUNDLE} curlu)
target_include_directories(${BUNDLE} PRIVATE
"${PROJECT_BINARY_DIR}/lib" # for "curl_config.h"
"${PROJECT_BINARY_DIR}/lib" # for "curl_config.h", "unitprotos.h"
"${PROJECT_SOURCE_DIR}/lib" # for "curl_setup.h", curlx
"${PROJECT_SOURCE_DIR}/tests/libtest" # for "first.h"
"${CMAKE_CURRENT_SOURCE_DIR}" # for the generated bundle source to find included test sources

View file

@ -24,8 +24,7 @@
#include "curlcheck.h"
#include "llist.h"
UNITTEST void Curl_node_uremove(struct Curl_llist_node *, void *);
#include "unitprotos.h"
static void test_Curl_llist_dtor(void *key, void *value)
{

View file

@ -22,11 +22,8 @@
*
***************************************************************************/
#include "curlcheck.h"
/* copied from urlapi.c */
extern int dedotdotify(const char *input, size_t clen, char **out);
#include "memdebug.h"
#include "unitprotos.h"
static CURLcode test_unit1395(char *arg)
{

View file

@ -26,8 +26,7 @@
#include "urldata.h"
#include "uint-table.h"
#include "curl_trc.h"
UNITTEST void Curl_uint_tbl_clear(struct uint_tbl *tbl);
#include "unitprotos.h"
#define TBL_SIZE 100

View file

@ -26,8 +26,7 @@
#include "urldata.h"
#include "uint-spbset.h"
#include "curl_trc.h"
UNITTEST void Curl_uint_spbset_clear(struct uint_spbset *bset);
#include "unitprotos.h"
static void check_spbset(const char *name, const unsigned int *s, size_t slen)
{