Skip to content

Commit

Permalink
fix xdebug coverage (part1): xdebug_get_code_coverage return value …
Browse files Browse the repository at this point in the history
…format for executed line

Summary:
This pull request is related to facebook#1589 .
(I will submit another pull request for supporting `XDEBUG_CC_UNUSED` and `XDEBUG_CC_DEAD_CODE` later)

`xdebug_get_code_coverage()` return value format is described in https://xdebug.org/docs/code_coverage :

> The returned values for each line are:
> * 1: this line was executed
> * -1: this line was not executed
> * -2: this line did not have executable code on it

According to this description, `xdebug_get_code_coverage()` should return value like:

```
[
    "/path/to/foo.php" => [
          3 => 1,  // executed line 3
          5 => 1,  // executed line 5
    ]
]
```

However, hhvm's `xdebug_get_code_coverage()` currently returns value like:

```
[
    "/path/to/foo.php" => [
          3 => 2,  // executed line 3 (2 times)
          5 => 3,  // executed line 5 (3 times)
    ]
]
```

In this pull request, hhvm's `xdebug_get_code_coverage()` return value format for executed line is changed to follow description in https://xdebug.org/docs/code_coverage .
Closes facebook#7888

Differential Revision: D5294692

Pulled By: mofarrell

fbshipit-source-id: c394e7b7b7c352edf66b2d6fa4895413b2005a35
  • Loading branch information
xKerman authored and hhvm-bot committed Jun 29, 2017
1 parent 035aed7 commit bf58f31
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
2 changes: 2 additions & 0 deletions hphp/runtime/base/code-coverage.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace HPHP {
struct Array;

struct CodeCoverage {
static constexpr int kLineExecuted = 1;

void Record(const char* filename, int line0, int line1);

/*
Expand Down
20 changes: 17 additions & 3 deletions hphp/runtime/ext/xdebug/ext_xdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,24 @@ static void HHVM_FUNCTION(xdebug_enable) {

static Array HHVM_FUNCTION(xdebug_get_code_coverage) {
auto ti = ThreadInfo::s_threadInfo.getNoCheck();
if (ti->m_reqInjectionData.getCoverage()) {
return ti->m_coverage->Report(false);
if (!ti->m_reqInjectionData.getCoverage()) {
return Array::Create();
}

auto ret = Array::Create();
auto const reports = ti->m_coverage->Report(false);
for (ArrayIter report(reports); report; ++report) {
auto tmp = Array::Create();
auto const lines = report.second().toArray();
for (ArrayIter line(lines); line; ++line) {
auto const count = line.second().toInt64();
if (count > 0) {
tmp.set(line.first(), Variant(CodeCoverage::kLineExecuted));
}
}
ret.set(report.first(), tmp);
}
return Array::Create();
return ret;
}

// TODO(#3704) see xdebug_start_error_collection()
Expand Down
16 changes: 8 additions & 8 deletions hphp/test/slow/ext_xdebug/code_coverage_started.php.expectf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ array(1) {
[5]=>
int(1)
[6]=>
int(3)
int(1)
[8]=>
int(1)
}
Expand All @@ -19,9 +19,9 @@ array(1) {
[11]=>
int(1)
[12]=>
int(3)
int(1)
[14]=>
int(2)
int(1)
[15]=>
int(1)
}
Expand All @@ -32,15 +32,15 @@ array(1) {
[11]=>
int(1)
[12]=>
int(3)
int(1)
[14]=>
int(2)
int(1)
[15]=>
int(3)
int(1)
[16]=>
int(2)
int(1)
[17]=>
int(2)
int(1)
[18]=>
int(1)
}
Expand Down
16 changes: 8 additions & 8 deletions hphp/test/slow/ext_xdebug/get_code_coverage.php.expectf
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ array(1) {
[2]=>
int(1)
[5]=>
int(32)
int(1)
[6]=>
int(16)
int(1)
[9]=>
int(36)
int(1)
[10]=>
int(32)
int(1)
[11]=>
int(18)
int(1)
[12]=>
int(2)
int(1)
[14]=>
int(2)
int(1)
[15]=>
int(2)
int(1)
[17]=>
int(1)
}
Expand Down
18 changes: 9 additions & 9 deletions hphp/test/slow/ext_xdebug/xdebug/coverage.php.expectf
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@ array(2) {
["%s/test/slow/ext_xdebug/xdebug/coverage.inc"]=>
array(11) {
[4]=>
int(%d)
int(1)
[7]=>
int(%d)
int(1)
[8]=>
int(1)
[10]=>
int(%d)
int(1)
[11]=>
int(1)
[15]=>
int(2)
int(1)
[17]=>
int(2)
int(1)
[18]=>
int(2)
int(1)
[20]=>
int(1)
[22]=>
int(4)
int(1)
[23]=>
int(3)
int(1)
}
["%s/test/slow/ext_xdebug/xdebug/coverage.php"]=>
array(3) {
[2]=>
int(1)
[3]=>
int(2)
int(1)
[4]=>
int(1)
}
Expand Down

0 comments on commit bf58f31

Please sign in to comment.