Skip to content

Commit

Permalink
Fix: Making exit a terminal node in CFG (#59)
Browse files Browse the repository at this point in the history
Fixes #57.
  • Loading branch information
silverfoxy authored and nikic committed Aug 8, 2019
1 parent 30728ef commit 4ee28d0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
14 changes: 6 additions & 8 deletions lib/PHPCfg/Op/Expr/Exit_.php → lib/PHPCfg/Op/Terminal/Exit_.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@
* @license MIT See LICENSE at the root of the project for more info
*/

namespace PHPCfg\Op\Expr;
namespace PHPCfg\Op\Terminal;

use PHPCfg\Op\Expr;
use PHPCfg\Op\Terminal;
use PhpCfg\Operand;

class Exit_ extends Expr
class Exit_ extends Terminal
{
public ?Operand $expr;
public ?Operand $expr = null;

public function __construct(Operand $expr = null, array $attributes = [])
{
parent::__construct($attributes);
if ($expr) {
if (!is_null($expr)) {
$this->expr = $this->addReadRef($expr);
} else {
$this->expr = null;
}
}

public function getVariableNames(): array
{
return ['expr', 'result'];
return ['expr'];
}
}
8 changes: 7 additions & 1 deletion lib/PHPCfg/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ protected function parseExprNode($expr)

return $op->result;
}

$method = 'parse'.$expr->getType();
if (method_exists($this, $method)) {
$op = $this->{$method}($expr);
Expand Down Expand Up @@ -1028,7 +1029,12 @@ protected function parseExpr_Exit(Expr\Exit_ $expr)
$e = $this->readVariable($this->parseExprNode($expr->expr));
}

return new Op\Expr\Exit_($e, $this->mapAttributes($expr));
$this->block->children[] = new Op\Terminal\Exit_($e, $this->mapAttributes($expr));
// Dump everything after the exit
$this->block = new Block();
$this->block->dead = true;

return new Literal(1);
}

protected function parseExpr_FuncCall(Expr\FuncCall $expr)
Expand Down
11 changes: 11 additions & 0 deletions test/code/exit.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
$a = 1;
exit;
echo $a;
-----
Block#1
Expr_Assign
var: Var#1<$a>
expr: LITERAL(1)
result: Var#2
Terminal_Exit
16 changes: 16 additions & 0 deletions test/code/exit_message.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
$a = 1;
exit('Quitting with message: ' . $a);
echo $a;
-----
Block#1
Expr_Assign
var: Var#1<$a>
expr: LITERAL(1)
result: Var#2
Expr_BinaryOp_Concat
left: LITERAL('Quitting with message: ')
right: Var#1<$a>
result: Var#3
Terminal_Exit
expr: Var#3

0 comments on commit 4ee28d0

Please # to comment.