refactor(php): 优化foreach循环中的迭代器变量使用并更新编译器常量

- 使用临时变量替代直接使用iteratorVar以避免命名冲突
- 为Windows平台移除clang-format格式化检查逻辑
- 添加多个新的编译器错误代码和警告信息定义
- 更新迭代器访问方式以提高代码安全性和可读性
pull/1/head
韩天峰 4 months ago
parent eaa1b4cd9a
commit 460ec638df
  1. 16
      src/Php/CompilerBase.php
  2. 7
      src/Php/Constants.php

@ -4007,12 +4007,13 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function parseForeachArray(Foreach_ $node, string $iteratorVar): string
{
$code = 'for (auto iter = ' . $iteratorVar . '.begin(); iter != ' . $iteratorVar . '.end(); ++iter) {' . PHP_EOL;
$tmpVar = $this->genTmpVarName();
$code = "for (auto $tmpVar = $iteratorVar.begin(); $tmpVar != $iteratorVar.end(); ++$tmpVar) {" . PHP_EOL;
$this->indentLevel++;
if ($node->keyVar) {
$keyVar = $this->parseIdentifier($node->keyVar);
$this->checkVar($node, $keyVar);
$code .= $this->getIndent() . ' ' . $keyVar . ' = iter.key();' . PHP_EOL;
$code .= $this->getIndent() . ' ' . $keyVar . ' = ' . $tmpVar . '.key();' . PHP_EOL;
}
if ($node->byRef and !$this->isVarExpr($node->valueVar)) {
@ -4025,7 +4026,7 @@ class CompilerBase extends \PhpAot\Core\Translator
abort($node->valueVar);
}
$dim = $this->parseIdentifier($node->valueVar->dim);
$code .= $this->getIndent() . "{$array}.offsetSet({$dim}, iter.value());";
$code .= $this->getIndent() . "{$array}.offsetSet({$dim}, {$tmpVar}.value());";
} else {
$valueVar = $this->parseIdentifier($node->valueVar);
if ($node->byRef) {
@ -4034,10 +4035,10 @@ class CompilerBase extends \PhpAot\Core\Translator
} elseif ($this->getVarType($valueVar) !== self::TYPE_REF) {
$this->fatalError($node, 'Cannot assign value to reference of type');
}
$code .= $this->getIndent() . ' ' . $valueVar . ' = iter.valueRef();' . PHP_EOL;
$code .= $this->getIndent() . ' ' . $valueVar . ' = ' . $tmpVar . '.valueRef();' . PHP_EOL;
} else {
$this->checkVar($node, $valueVar);
$code .= $this->getIndent() . ' ' . $valueVar . ' = iter.value();' . PHP_EOL;
$code .= $this->getIndent() . ' ' . $valueVar . ' = ' . $tmpVar . '.value();' . PHP_EOL;
}
}
@ -4084,11 +4085,6 @@ class CompilerBase extends \PhpAot\Core\Translator
return;
}
// Windows 下可能没有 clang-format,跳过格式化
if ($this->getPlatform() instanceof Windows) {
return;
}
$cmd = 'cd ' . $this->rootPath . ' && clang-format -i ' . $file;
$this->climate->info('format: ' . $this->getRelativePath($file));
$this->climate->comment($cmd);

@ -180,5 +180,12 @@ class Constants
'5039' => '使用未定义的函数',
'4101' => '未使用的局部变量',
'4102' => '未引用的标签',
'4800' => '从整数到布尔类型的隐式转换警告',
'5045' => 'Spectre 缓解警告',
'5264' => '未使用 const 变量',
'5246' => '子对象的初始化应当包装在大括号内',
'4388' => '有符号/无符号不匹配',
'4623' => '已将默认构造函数隐式定义为“已删除”',
'4611' => '_setjmp 和 C++ 对象析构之间的交互是不可移植的',
];
}

Loading…
Cancel
Save