Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 135 lines (105 sloc) 3.882 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002, 2003 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.0 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://xdebug.derickrethans.nl/license.php |
| If you did not receive a copy of the Xdebug license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| xdebug@derickrethans.nl so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Derick Rethans <derick@xdebug.org> |
+----------------------------------------------------------------------+
*/

#include "php_xdebug.h"
#include "xdebug_var.h"
#include "xdebug_code_coverage.h"

extern ZEND_DECLARE_MODULE_GLOBALS(xdebug);

void xdebug_coverage_line_dtor(void *data)
{
xdebug_coverage_line *line = (xdebug_coverage_line *) data;

xdfree(line);
}

void xdebug_coverage_file_dtor(void *data)
{
xdebug_coverage_file *file = (xdebug_coverage_file *) data;

xdebug_hash_destroy(file->lines);
xdfree(file->name);
xdfree(file);
}

void xdebug_count_line(char *filename, int lineno TSRMLS_DC)
{
xdebug_coverage_file *file;
xdebug_coverage_line *line;
char *sline;

sline = xdebug_sprintf("%d", lineno);

/* Check if the file already exists in the hash */
if (!xdebug_hash_find(XG(code_coverage), filename, strlen(filename), (void *) &file)) {
/* The file does not exist, so we add it to the hash, and
* add a line element to the file */
file = xdmalloc(sizeof(xdebug_coverage_file));
file->name = xdstrdup(filename);
file->lines = xdebug_hash_alloc(128, xdebug_coverage_line_dtor);

xdebug_hash_add(XG(code_coverage), filename, strlen(filename), file);
}

/* Check if the line already exists in the hash */
if (!xdebug_hash_find(file->lines, sline, strlen(sline), (void *) &line)) {
line = xdmalloc(sizeof(xdebug_coverage_line));
line->lineno = lineno;
line->count = 0;

xdebug_hash_add(file->lines, sline, strlen(sline), line);
}

line->count++;

xdfree(sline);
}

PHP_FUNCTION(xdebug_start_code_coverage)
{
XG(do_code_coverage) = 1;
}

PHP_FUNCTION(xdebug_stop_code_coverage)
{
XG(do_code_coverage) = 0;
}


static int xdebug_lineno_cmp(const void *a, const void *b TSRMLS_DC)
{
Bucket *f = *((Bucket **) a);
Bucket *s = *((Bucket **) b);

if (f->h < s->h) {
return -1;
} else if (f->h > s->h) {
return 1;
} else {
return 0;
}
}


static void add_line(void *ret, xdebug_hash_element *e)
{
xdebug_coverage_line *line = (xdebug_coverage_line*) e->ptr;
zval *retval = (zval*) ret;

add_index_long(retval, line->lineno, line->count);
}

static void add_file(void *ret, xdebug_hash_element *e)
{
xdebug_coverage_file *file = (xdebug_coverage_file*) e->ptr;
zval *retval = (zval*) ret;
zval *lines;
HashTable *target_hash;
TSRMLS_FETCH();

MAKE_STD_ZVAL(lines);
array_init(lines);

/* Add all the lines */
xdebug_hash_apply(file->lines, (void *) lines, add_line);

/* Sort on linenumber */
target_hash = HASH_OF(lines);
zend_hash_sort(target_hash, zend_qsort, xdebug_lineno_cmp, 0 TSRMLS_CC);

add_assoc_zval_ex(retval, file->name, strlen(file->name) + 1, lines);
}

PHP_FUNCTION(xdebug_get_code_coverage)
{
array_init(return_value);
xdebug_hash_apply(XG(code_coverage), (void *) return_value, add_file);
}

Something went wrong with that request. Please try again.