Skip to content

Commit

Permalink
Fixed issue #1192: Dead code analysis does not work for generators wi…
Browse files Browse the repository at this point in the history
…th 'return;'
  • Loading branch information
derickr committed Oct 26, 2015
1 parent 5241948 commit 19decf4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
73 changes: 73 additions & 0 deletions tests/bug01192.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
--TEST--
Test for bug #1092: Dead code analysis does not work for generators with 'return;'
--SKIPIF--
<?php if (!version_compare(phpversion(), "5.5", '>=')) echo "skip >= PHP 5.5 needed\n"; ?>
--FILE--
<?php
function gen(&$output, $branch = false)
{
yield;

if($branch) {
$output = 'branched';
return;
} // This line is never covered.
$output = 'did not branch';

}

function testGen()
{
$output = '';
$gen = gen($output, true);

while($gen->valid()) {
$gen->next();
}
}

xdebug_start_code_coverage (XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);

testGen();

$c = xdebug_get_code_coverage();
ksort($c);
var_dump($c);
xdebug_stop_code_coverage();
?>
--EXPECTF--
array(1) {
["%sbug01192.php"]=>
array(15) {
[4]=>
int(1)
[6]=>
int(1)
[7]=>
int(1)
[8]=>
int(1)
[9]=>
int(-2)
[10]=>
int(-1)
[12]=>
int(-1)
[16]=>
int(1)
[17]=>
int(1)
[19]=>
int(1)
[20]=>
int(1)
[21]=>
int(1)
[22]=>
int(1)
[26]=>
int(1)
[28]=>
int(1)
}
}
9 changes: 8 additions & 1 deletion xdebug_code_coverage.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,14 @@ static int xdebug_find_jump(zend_op_array *opa, unsigned int position, long *jmp
} else if (opcode.opcode == ZEND_GOTO) {
*jmp1 = ((long) opcode.op1.jmp_addr - (long) base_address) / sizeof(zend_op);
return 1;
} else if (opcode.opcode == ZEND_EXIT || opcode.opcode == ZEND_THROW || opcode.opcode == ZEND_RETURN) {
} else if (
#if PHP_VERSION_ID >= 50500
opcode.opcode == ZEND_GENERATOR_RETURN ||
#endif
opcode.opcode == ZEND_EXIT ||
opcode.opcode == ZEND_THROW ||
opcode.opcode == ZEND_RETURN
) {
*jmp1 = XDEBUG_JMP_EXIT;
return 1;
}
Expand Down

0 comments on commit 19decf4

Please sign in to comment.