From 460ec638df0602d0d7f27010733d6bfe473e7468 Mon Sep 17 00:00:00 2001 From: rango Date: Sat, 9 May 2026 17:27:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor(php):=20=E4=BC=98=E5=8C=96foreach?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E4=B8=AD=E7=9A=84=E8=BF=AD=E4=BB=A3=E5=99=A8?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E4=BD=BF=E7=94=A8=E5=B9=B6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=99=A8=E5=B8=B8=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用临时变量替代直接使用iteratorVar以避免命名冲突 - 为Windows平台移除clang-format格式化检查逻辑 - 添加多个新的编译器错误代码和警告信息定义 - 更新迭代器访问方式以提高代码安全性和可读性 --- src/Php/CompilerBase.php | 16 ++++++---------- src/Php/Constants.php | 7 +++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 051a14d2..781b046d 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.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); diff --git a/src/Php/Constants.php b/src/Php/Constants.php index 133a6dc6..841d60fe 100644 --- a/src/Php/Constants.php +++ b/src/Php/Constants.php @@ -180,5 +180,12 @@ class Constants '5039' => '使用未定义的函数', '4101' => '未使用的局部变量', '4102' => '未引用的标签', + '4800' => '从整数到布尔类型的隐式转换警告', + '5045' => 'Spectre 缓解警告', + '5264' => '未使用 const 变量', + '5246' => '子对象的初始化应当包装在大括号内', + '4388' => '有符号/无符号不匹配', + '4623' => '已将默认构造函数隐式定义为“已删除”', + '4611' => '_setjmp 和 C++ 对象析构之间的交互是不可移植的', ]; }