From fe34c72c8755d7e9a4f6863e7ae43f3fbd4d63ef Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 4 Feb 2026 19:19:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=B7=BB=E5=8A=A0=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E4=BF=A1=E6=81=AF=E7=94=9F=E6=88=90=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在编译器选项配置中新增 debug-info 参数支持 - 在编译器基类中实现 genDebugInfo 方法用于生成调试信息 - 修改 parseAssignPropertyArrayDim 和 parseParentMethodCall 方法为受保护访问级别 - 在翻译器中集成 debug-info 命令行参数处理逻辑 - 更新属性赋值方式从 getPropertyIndirect 改为 attr 方法 - 在 C++ 主程序中添加异常处理时的未清理关闭标记 --- src/Php/CompilerBase.php | 22 +++++++++++++++++++--- src/Php/Translator.php | 4 +++- src/config/compiler_options.php | 5 +++++ src/cpp/main.cc | 1 + 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 1883c545..2e6949f7 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -708,6 +708,7 @@ class CompilerBase extends \PhpAot\Core\Translator } $code .= "\n"; $this->indentLevel--; + $code .= $this->genDebugInfo(); $code .= $stmts; $code .= "}\n"; @@ -841,7 +842,7 @@ class CompilerBase extends \PhpAot\Core\Translator $this->afterStmtLines = []; $result = ''; $this->writeLog('Line ' . $this->getLine($v) . ': ' . $class); - + $lines[] = $this->genDebugInfo($v); $lines[] = $this->getComment($v, $class); switch ($class) { case 'Stmt_Expression': @@ -3285,7 +3286,7 @@ class CompilerBase extends \PhpAot\Core\Translator return self::PREFIX . $nativeFunc . '(' . $object . ', ' . $this->parseNativeCallArgs($args, $nativeFunc) . ')'; } - private function parseAssignPropertyArrayDim(Node $left, Node $right): string + protected function parseAssignPropertyArrayDim(Node $left, Node $right): string { $obj = $this->parseIdentifier($left->var->var); $propName = $this->identifierToStr($left->var->name); @@ -3299,7 +3300,7 @@ class CompilerBase extends \PhpAot\Core\Translator return $code . "{$obj}.updateArrayProperty({$propName}, {$dim}, {$value})"; } - private function parseParentMethodCall(Node\Expr\StaticCall $expr): string + protected function parseParentMethodCall(Node\Expr\StaticCall $expr): string { $method = $this->identifierToStr($expr->name); if (empty($expr->args)) { @@ -3307,4 +3308,19 @@ class CompilerBase extends \PhpAot\Core\Translator } return 'this_.callParentMethod(' . $method . ', {' . $this->parseCallArgs($expr->args) . '})'; } + + protected function genDebugInfo(?NodeAbstract $stmt = null): string + { + $code = ''; + if ($this->debugInfo) { + if ($stmt) { + $code .= 'php::debug_info.php_line = ' . $stmt->getLine() . ';' . PHP_EOL; + $code .= 'php::debug_info.cpp_line = __LINE__;' . PHP_EOL; + } else { + $code .= 'php::debug_info.enable = true;' . PHP_EOL; + $code .= 'php::debug_info.php_file = "' . $this->escapeString($this->file) . '";' . PHP_EOL; + } + } + return $code; + } } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 23cb5958..ed178d2d 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -20,6 +20,7 @@ use PhpAot\Php\Exception\Redo; use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\Stmt\Foreach_; +use PhpParser\NodeAbstract; use PhpParser\NodeTraverser; use Symfony\Component\Yaml\Yaml; @@ -50,6 +51,7 @@ class Translator extends Preprocessor $this->optimizeLevel = $this->climate->arguments->get('optimize'); $this->buildMode = $this->climate->arguments->get('mode'); $this->debugLine = intval($this->climate->arguments->get('debug-line')); + $this->debugInfo = $this->climate->arguments->defined('debug-info'); $this->noLiteralStrings = $this->climate->arguments->get('noLiteralStrings'); $this->enableProfiler = $this->climate->arguments->defined('profile'); $this->internalFunctions = array_flip(get_defined_functions()['internal']); @@ -751,7 +753,7 @@ class Translator extends Preprocessor foreach ($classDef->properties as $property) { if ($property->type === self::TYPE_ARRAY and $property->default and $property->default !== self::TYPE_ARRAY . '{}') { $propOffset = self::PREFIX . $this->getPropertyOffset($property->name, $classDef->name, $classDef->namespace); - $cppCode .= $this->getIndent() . 'this_.getPropertyIndirect(' . $propOffset . ') = ' . $property->default . ';' . PHP_EOL; + $cppCode .= $this->getIndent() . 'this_.attr(' . $propOffset . ') = ' . $property->default . ';' . PHP_EOL; } } diff --git a/src/config/compiler_options.php b/src/config/compiler_options.php index 267da990..2518d1fa 100644 --- a/src/config/compiler_options.php +++ b/src/config/compiler_options.php @@ -51,4 +51,9 @@ return [ 'required' => false, 'defaultValue' => 0, ], + 'debug-info' => [ + 'longPrefix' => 'debug-info', + 'description' => 'Enable debug info', + 'required' => false, + ], ]; \ No newline at end of file diff --git a/src/cpp/main.cc b/src/cpp/main.cc index 61a28720..70c6aa54 100644 --- a/src/cpp/main.cc +++ b/src/cpp/main.cc @@ -45,6 +45,7 @@ int main(int cpp_argc, char **cpp_argv) { php::eval("main();"); } catch (zend_object *e) { rc = EG(exit_status); + CG(unclean_shutdown) = 1; zend_exception_error(e, E_ERROR); } #if PPROF_ON