Skip to content

Commit

Permalink
started implementing va_arg params in value_string_ex
Browse files Browse the repository at this point in the history
  • Loading branch information
aodinokov committed Jul 25, 2024
1 parent d77f0c8 commit db4500e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"name": "(lldb) Launch - Macos",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/value_string_test",
"program": "${fileDirname}/value_with_args_test",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
Expand Down
2 changes: 1 addition & 1 deletion src/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ metac_entry_t * metac_entry_final_entry(metac_entry_t *p_entry, metac_quals_t *
break;
case METAC_KND_subprogram_parameter:
if (p_entry->subprogram_parameter_info.unspecified_parameters != 0) {
return p_entry; // this is the final entry then
return p_entry; // this is the final entry then ??? maybe return NULL
}
p_entry = p_entry->subprogram_parameter_info.type;
break;
Expand Down
31 changes: 27 additions & 4 deletions src/value_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ char * metac_value_string_ex(metac_value_t * p_val, metac_value_walk_mode_t wmod
p = (metac_value_t *)metac_recursive_iterator_next(p_iter)) {
int state = metac_recursive_iterator_get_state(p_iter);

metac_kind_t final_kind = metac_value_final_kind(p, NULL);
metac_kind_t final_kind;
if (metac_value_kind(p) != METAC_KND_subprogram_parameter || metac_value_has_load_of_parameter(p) == 0) {
final_kind = metac_value_final_kind(p, NULL);
} else {
final_kind = METAC_KND_subprogram_parameter; // unspecified and va_arg;
}

switch(final_kind) {
case METAC_KND_base_type: {
char * out = metac_value_base_type_string(p);
Expand Down Expand Up @@ -460,6 +466,7 @@ char * metac_value_string_ex(metac_value_t * p_val, metac_value_walk_mode_t wmod
}
}
}
case METAC_KND_subprogram_parameter:// this is only it's unspecified param // TODO: we need also va_arg here
//case METAC_KND_subroutine_type:
case METAC_KND_subprogram: {
// TODO: support arguments
Expand Down Expand Up @@ -499,14 +506,23 @@ char * metac_value_string_ex(metac_value_t * p_val, metac_value_walk_mode_t wmod
break;
}

char * param_cdecl = metac_entry_cdecl(metac_entry_parameter_entry(metac_value_entry(p_param_val)));
char * param_cdecl = NULL;
if (metac_value_kind(p_param_val) == METAC_KND_subprogram_parameter) {
// TODO: skip this for ...
param_cdecl = metac_entry_cdecl(metac_entry_parameter_entry(metac_value_entry(p_param_val)));
} else {
param_cdecl = metac_entry_cdecl(metac_value_entry(p_param_val));
}
if (param_cdecl == NULL) {
free(param_out);
failure = 1;
break;
}

char * param_name = metac_entry_name(metac_value_entry(p_param_val));
char * param_name = NULL;
if (metac_value_kind(p_param_val) == METAC_KND_subprogram_parameter) {
param_name = metac_entry_name(metac_value_entry(p_param_val));
}
char * param = dsprintf(param_cdecl,
param_name == NULL?"":param_name);
free(param_cdecl);
Expand All @@ -517,10 +533,11 @@ char * metac_value_string_ex(metac_value_t * p_val, metac_value_walk_mode_t wmod
}

char *prev_out = out;
out = dsprintf("%s%s%s = %s",
out = dsprintf("%s%s%s%s%s",
prev_out == NULL?"":prev_out, /*must include , at the end */
prev_out == NULL?"":", ",
param,
param_name == NULL?" ":" = ",
param_out
);
if (prev_out) {
Expand All @@ -540,6 +557,12 @@ char * metac_value_string_ex(metac_value_t * p_val, metac_value_walk_mode_t wmod
metac_recursive_iterator_set_state(p_iter, 2); // failure cleanup
continue;
}

if (final_kind == METAC_KND_subprogram_parameter) {
metac_recursive_iterator_done(p_iter, out);
continue;
}

char *prev_out = out;
out = dsprintf("%s(%s)",
metac_entry_name(metac_value_entry(p)),
Expand Down
21 changes: 20 additions & 1 deletion src/value_with_args_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,23 @@ METAC_START_TEST(va_arg_to_value) {
}

metac_tag_map_delete(p_tag_map);
}
}END_TEST

METAC_START_TEST(subrouting_sanity) {
metac_value_t * p_val;
char *s, *expected_s;
metac_tag_map_t * p_tagmap = va_args_tag_map();

p_val = METAC_NEW_VALUE_WITH_ARGS(p_tagmap, test_function_with_va_args, "%s %s", "some", "test");
fail_unless(p_val != NULL);

expected_s = "test_function_with_va_args(const char * format = (const char []){'%', 's', ' ', '%', 's', 0,}, "
"va_list parameters = char [5] {'s', 'o', 'm', 'e', 0,}, char [5] {'t', 'e', 's', 't', 0,})";
s = metac_value_string_ex(p_val, METAC_WMODE_deep, p_tagmap);
fail_unless(s != NULL, "got NULL");
fail_unless(strcmp(s, expected_s) == 0, "expected %s, got %s", expected_s, s);
free(s);

metac_value_delete(p_val);
metac_tag_map_delete(p_tagmap);
}END_TEST

0 comments on commit db4500e

Please sign in to comment.