Skip to content

Commit

Permalink
Merged pull request xdebug#919
Browse files Browse the repository at this point in the history
  • Loading branch information
derickr committed Nov 8, 2023
2 parents f83a47f + c9e39a2 commit ea6e154
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/coverage/code_coverage.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ static void add_cc_function(void *ret, xdebug_hash_element *e)
xdebug_coverage_function *function = (xdebug_coverage_function*) e->ptr;
zval *retval = (zval*) ret;
zval *function_info;
zend_string *trait_scope = NULL;

XDEBUG_MAKE_STD_ZVAL(function_info);
array_init(function_info);
Expand All @@ -851,7 +852,14 @@ static void add_cc_function(void *ret, xdebug_hash_element *e)
add_paths(function_info, function->branch_info);
}

add_assoc_zval_ex(retval, function->name, HASH_KEY_STRLEN(function->name), function_info);
if ((trait_scope = xdebug_get_trait_scope(function->name)) != NULL) {
char *with_scope = xdebug_sprintf("%s->%s", ZSTR_VAL(trait_scope), function->name);

add_assoc_zval_ex(retval, with_scope, strlen(with_scope), function_info);
} else {
add_assoc_zval_ex(retval, function->name, HASH_KEY_STRLEN(function->name), function_info);
}


efree(function_info);
}
Expand Down
30 changes: 28 additions & 2 deletions src/lib/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ void xdebug_library_rinit(void)

XG_LIB(dumped) = 0;
XG_LIB(do_collect_errors) = 0;

XG_LIB(trait_location_map) = xdebug_hash_alloc(256, (xdebug_hash_dtor_t) zend_string_release);
}

void xdebug_library_post_deactivate(void)
Expand All @@ -101,6 +103,7 @@ void xdebug_library_post_deactivate(void)
xdebug_llist_destroy(XG_LIB(headers), NULL);
XG_LIB(headers) = NULL;

xdebug_hash_destroy(XG_LIB(trait_location_map));

xdebug_close_log();
xdebug_str_free(XG_LIB(diagnosis_buffer));
Expand Down Expand Up @@ -703,9 +706,26 @@ void xdebug_llist_string_dtor(void *dummy, void *elem)
}
}

zend_string* xdebug_wrap_location_around_function_name(const char *prefix, zend_op_array *opa, zend_string *fname)
zend_string *xdebug_get_trait_scope(const char *function)
{
zend_string *trait_scope;

if (
function[0] != '{' &&
function[strlen(function)-1] == '}' &&
xdebug_hash_find(XG_LIB(trait_location_map), function, strlen(function), (void *) &trait_scope)
) {
return trait_scope;
}

return NULL;
}

zend_string *xdebug_wrap_location_around_function_name(const char *prefix, zend_op_array *opa, zend_string *fname)
{
return zend_strpprintf(
void *dummy;

zend_string *wrapped = zend_strpprintf(
0,
"%s{%s:%s:%d-%d}",
ZSTR_VAL(fname),
Expand All @@ -714,6 +734,12 @@ zend_string* xdebug_wrap_location_around_function_name(const char *prefix, zend_
opa->line_start,
opa->line_end
);

if (!xdebug_hash_find(XG_LIB(trait_location_map), ZSTR_VAL(wrapped), ZSTR_LEN(wrapped), &dummy)) {
xdebug_hash_add(XG_LIB(trait_location_map), ZSTR_VAL(wrapped), ZSTR_LEN(wrapped), zend_string_copy(opa->scope->name));
}

return wrapped;
}

zend_string* xdebug_wrap_closure_location_around_function_name(zend_op_array *opa, zend_string *fname)
Expand Down
6 changes: 6 additions & 0 deletions src/lib/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ typedef struct _xdebug_library_globals_t {
char *log_open_timestring;
xdebug_str *diagnosis_buffer;

/* Trait location map */
xdebug_hash *trait_location_map;

/* Opcode overrite handlers */
user_opcode_handler_t original_opcode_handlers[256];
xdebug_multi_opcode_handler_t *opcode_multi_handlers[256];
xdebug_set *opcode_handlers_set;
Expand Down Expand Up @@ -335,6 +339,8 @@ int xdebug_call_original_opcode_handler_if_set(int opcode, XDEBUG_OPCODE_HANDLER
char *xdebug_lib_get_output_dir(void);

void xdebug_llist_string_dtor(void *dummy, void *elem);

zend_string *xdebug_get_trait_scope(const char *function);
zend_string* xdebug_wrap_location_around_function_name(const char *prefix, zend_op_array *opa, zend_string *fname);
zend_string* xdebug_wrap_closure_location_around_function_name(zend_op_array *opa, zend_string *fname);

Expand Down
2 changes: 1 addition & 1 deletion tests/coverage/bug01938.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ App\Bar->useTrait
- paths
- 0: HIT

returnsTrue{trait-method:%sbug01938-FooTrait.inc:9-12}
App\FooTrait->returnsTrue{trait-method:%sbug01938-FooTrait.inc:9-12}
- branches
- 00; OP: 00-%d; line: %d-%d HIT%S
- paths
Expand Down

0 comments on commit ea6e154

Please sign in to comment.