From bf58f316c2101e5f7dfe0f91de063990d3e6b66e Mon Sep 17 00:00:00 2001 From: xKhorasan Date: Thu, 29 Jun 2017 12:00:12 -0700 Subject: [PATCH] fix xdebug coverage (part1): `xdebug_get_code_coverage` return value format for executed line Summary: This pull request is related to #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 https://github.com/facebook/hhvm/pull/7888 Differential Revision: D5294692 Pulled By: mofarrell fbshipit-source-id: c394e7b7b7c352edf66b2d6fa4895413b2005a35 --- hphp/runtime/base/code-coverage.h | 2 ++ hphp/runtime/ext/xdebug/ext_xdebug.cpp | 20 ++++++++++++++++--- .../code_coverage_started.php.expectf | 16 +++++++-------- .../ext_xdebug/get_code_coverage.php.expectf | 16 +++++++-------- .../ext_xdebug/xdebug/coverage.php.expectf | 18 ++++++++--------- 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/hphp/runtime/base/code-coverage.h b/hphp/runtime/base/code-coverage.h index 1775ef6e0f3e7..5da9e55f8628c 100644 --- a/hphp/runtime/base/code-coverage.h +++ b/hphp/runtime/base/code-coverage.h @@ -28,6 +28,8 @@ namespace HPHP { struct Array; struct CodeCoverage { + static constexpr int kLineExecuted = 1; + void Record(const char* filename, int line0, int line1); /* diff --git a/hphp/runtime/ext/xdebug/ext_xdebug.cpp b/hphp/runtime/ext/xdebug/ext_xdebug.cpp index 8fbe6a2bbe0a8..61f24409f81f8 100644 --- a/hphp/runtime/ext/xdebug/ext_xdebug.cpp +++ b/hphp/runtime/ext/xdebug/ext_xdebug.cpp @@ -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() diff --git a/hphp/test/slow/ext_xdebug/code_coverage_started.php.expectf b/hphp/test/slow/ext_xdebug/code_coverage_started.php.expectf index a8b28b17e6511..7a09b0bd9a06c 100644 --- a/hphp/test/slow/ext_xdebug/code_coverage_started.php.expectf +++ b/hphp/test/slow/ext_xdebug/code_coverage_started.php.expectf @@ -6,7 +6,7 @@ array(1) { [5]=> int(1) [6]=> - int(3) + int(1) [8]=> int(1) } @@ -19,9 +19,9 @@ array(1) { [11]=> int(1) [12]=> - int(3) + int(1) [14]=> - int(2) + int(1) [15]=> int(1) } @@ -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) } diff --git a/hphp/test/slow/ext_xdebug/get_code_coverage.php.expectf b/hphp/test/slow/ext_xdebug/get_code_coverage.php.expectf index afe40ced85046..95bec1ab33391 100644 --- a/hphp/test/slow/ext_xdebug/get_code_coverage.php.expectf +++ b/hphp/test/slow/ext_xdebug/get_code_coverage.php.expectf @@ -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) } diff --git a/hphp/test/slow/ext_xdebug/xdebug/coverage.php.expectf b/hphp/test/slow/ext_xdebug/xdebug/coverage.php.expectf index 3ccdf7cb77bde..1c86fcdf236e8 100644 --- a/hphp/test/slow/ext_xdebug/xdebug/coverage.php.expectf +++ b/hphp/test/slow/ext_xdebug/xdebug/coverage.php.expectf @@ -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) }