From 5ed79e9451ace2f2214ed277500202a6e57967bb Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 16 Jun 2026 18:01:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E5=AD=97?= =?UTF-8?q?=E9=9D=A2=E9=87=8F=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=BA=E7=A9=BA?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在声明字面量字符串数组前检查数组是否为空 - 避免当没有字面量字符串时生成无效的 extern 声明 - 添加空数组条件判断以防止编译器报错 - 为函数默认值相关的字符串字面量添加相同保护 - 确保代码在各种情况下都能正确编译 --- src/Php/Translator.php | 24 ++++++++++++++++-------- tests/aot/basic/main-func.phpt | 9 +++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 tests/aot/basic/main-func.phpt diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 716d1c84..5935734f 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -507,8 +507,10 @@ class Translator extends Preprocessor $lines[] = 'extern ' . self::TYPE_VAR . ' ' . $this->escapeGlobalVar($name) . ';'; } - $literalStringsCount = count($this->literalStrings); - $lines[] = 'extern ' . self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[' . $literalStringsCount . '];' . PHP_EOL; + if ($this->literalStrings) { + $literalStringsCount = count($this->literalStrings); + $lines[] = 'extern ' . self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[' . $literalStringsCount . '];' . PHP_EOL; + } // 确保数组大小至少为 1,避免 C/C++ 编译错误 $classCount = max(1, count($this->classMap)); @@ -613,11 +615,15 @@ CODE; $code .= "\n\n"; $code .= "// literal strings \n"; - $code .= self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[] = {' . PHP_EOL; - foreach ($this->literalStrings as $str => $index) { - $code .= self::TYPE_STR . '{ZEND_STRL("' . $this->escapeString($str) . '"), true}, // [' . $index . ']' . PHP_EOL; + if ($this->literalStrings) { + $code .= self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[] = {' . PHP_EOL; + foreach ($this->literalStrings as $str => $index) { + $code .= self::TYPE_STR . '{ZEND_STRL("' . $this->escapeString($str) . '"), true}, // [' . $index . ']' . PHP_EOL; + } + $code .= '};' . PHP_EOL . PHP_EOL; + } else { + $code .= PHP_EOL; } - $code .= '};' . PHP_EOL . PHP_EOL; $code .= "// constants \n"; foreach ($this->constants as $name => $const) { @@ -1386,8 +1392,10 @@ CODE; $code = '#include ' . PHP_EOL; // 函数的默认值可能会使用字符串字面量,需要提前声明 - $literalStringsCount = count($this->literalStrings); - $code .= 'extern ' . self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[' . $literalStringsCount . '];' . PHP_EOL; + if ($this->literalStrings) { + $literalStringsCount = count($this->literalStrings); + $code .= 'extern ' . self::TYPE_STR . ' ' . self::LITERAL_STRINGS . '[' . $literalStringsCount . '];' . PHP_EOL; + } foreach ($this->functions as $name => $func) { $code .= 'extern ' . $func->returnType . ' ' . self::PREFIX . $name . '('; diff --git a/tests/aot/basic/main-func.phpt b/tests/aot/basic/main-func.phpt new file mode 100644 index 00000000..ffccba17 --- /dev/null +++ b/tests/aot/basic/main-func.phpt @@ -0,0 +1,9 @@ +--TEST-- +main function +--FILE-- + +--EXPECT--