修复 while do...while和if中的判断是赋值导致的C++编译警告的问题

pull/3/head
NathanFreeman 2 months ago
parent 7eeb416f6a
commit c8d8d1c9ba
  1. 12
      src/Php/CompilerBase.php

@ -3346,6 +3346,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseIf(Node\Stmt\If_ $v): string
{
$cond = $this->parseExpr($v->cond);
$cond = $this->parseConditionAssign($v->cond, $cond);
$code = $this->parseBeforeStmtLines() . PHP_EOL;
$code .= 'if (' . $cond . ') {' . PHP_EOL;
@ -3381,6 +3382,7 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseWhile(Node\Stmt\While_ $v): string
{
$cond = $this->parseExpr($v->cond);
$cond = $this->parseConditionAssign($v->cond, $cond);
$stmts = $v->stmts;
$code = $this->parseBeforeStmtLines() . PHP_EOL;
@ -3401,6 +3403,7 @@ class CompilerBase extends \PhpAot\Core\Translator
{
$stmts = $v->stmts;
$cond = $this->parseExpr($v->cond);
$cond = $this->parseConditionAssign($v->cond, $cond);
$code = $this->parseBeforeStmtLines() . PHP_EOL;
$code .= 'do {' . PHP_EOL;
$code .= $this->parseBlockStmts($stmts);
@ -3437,6 +3440,15 @@ class CompilerBase extends \PhpAot\Core\Translator
return $tmpVar;
}
/**
* while, do while 判断中赋值,在C++编译中会提示
* suggest parentheses around assignment used as truth value
*/
protected function parseConditionAssign(NodeAbstract $cond, string $code): string
{
return $cond instanceof Expr\Assign ? '(' . $code . ')' : $code;
}
protected function packData(string $bytes): string
{
$out = '';

Loading…
Cancel
Save