c-hdr-parser: handle function pointers better

This commit is contained in:
icst 2024-06-29 21:59:11 -04:00
parent 87b023b9be
commit dbcbfeeab5

View file

@ -321,7 +321,7 @@ char ** parse_c_header(size_t *ndecl, FILE *f);
/// Parse sub expression from buffer
char * parse_c_header_sub(char *out, size_t *out_len, const char *sub, size_t sub_len) {
FILE *fsub = fmemopen(sub, sub_len, "r");
FILE *fsub = fmemopen((void*)sub, sub_len, "r");
size_t n_sub_decl = 0;
char ** sub_decl = parse_c_header(&n_sub_decl, fsub);
@ -401,6 +401,8 @@ char ** parse_c_header(size_t *ndecl, FILE *f) {
int64_t array_count = 0, peak_array_count = 0;
char *func_name = NULL;
int c;
while ( (c = fgetc(f)) != EOF ) {
@ -480,6 +482,37 @@ char ** parse_c_header(size_t *ndecl, FILE *f) {
if ( c == '(' ) {
size_t args_len = 0;
char *args = parse_c_header_decl_args(&c, &args_len, f);
int is_func_ptr = 0;
for (size_t n=0; n < args_len; n++) {
if ( args[n] == '*' ) is_func_ptr = 1;
if ( !isspace(args[n]) ) break;
}
if ( is_func_ptr ) {
while ( (c = fgetc(f)) != EOF && c != '(' );
if ( c == EOF ) {
free( args );
break;
}
ungetc(c, f);
// if we are not already in a func pointer decl, set the name
if ( func_name == NULL ) {
size_t lwlen;
char *lastword = parse_c_last_word(&lwlen, args);
if ( lastword ) {
func_name = malloc( lwlen + 1 );
memcpy(func_name, lastword, lwlen);
func_name[lwlen] = 0;
}
free( args );
}
continue;
}
// remove any space between the function identifier and '('
if ( slen > 0 && s[slen-1] == ' ' ) slen--;
@ -490,15 +523,29 @@ char ** parse_c_header(size_t *ndecl, FILE *f) {
s[slen] = '(';
slen++;
size_t args_len = 0;
char *args = parse_c_header_decl_args(&c, &args_len, f);
s = parse_c_header_sub(s, &slen, args, args_len);
free( args );
s = realloc(s, slen + 1);
s[slen] = ')';
slen++;
if ( func_name ) {
size_t func_name_len = strlen(func_name);
s = realloc(s, slen + 1 + func_name_len);
s[slen] = ' ';
slen++;
memcpy(s + slen, func_name, func_name_len);
slen += func_name_len;
free( func_name );
func_name = NULL;
}
// consume remainder of function prototype definition
while ( (c = fgetc(f)) != EOF && c != ';' );
if ( c == EOF ) break;