feat(compiler): 添加对孤立代码的检测和错误处理

- 在 CompilerBase 中添加 foundStrayCode 方法用于报告孤立代码错误
- 在 Preprocessor 中为 Stmt_Expression 类型添加孤立代码检测
- 防止在扩展模式下生成名为 main 的函数
- 添加 empty-2.phpt 测试用例验证空值合并操作符功能
pull/1/head
韩天峰 7 months ago
parent 4b42419be4
commit 288021d16c
  1. 2
      examples/autoload_class.php
  2. 5
      src/Php/CompilerBase.php
  3. 7
      src/Php/Preprocessor.php
  4. 3
      src/template/extension.cc.php
  5. 15
      tests/aot/empty-2.phpt

@ -11,3 +11,5 @@ function main()
$o = new CpuCoreCounter();
var_dump($o->getCount());
}
test();

@ -1799,6 +1799,11 @@ class CompilerBase extends \PhpAot\Core\Translator
return false;
}
protected function foundStrayCode(): never
{
$this->error("All execution code must be within a function; there is no allowance for stray code.");
}
protected function parseFuncCall(Node\Expr\FuncCall $expr, bool $silent = false): string
{
$call = '';

@ -105,9 +105,10 @@ class Preprocessor extends CompilerBase
case 'Stmt_Const':
$this->parseConstDef($v);
break;
case 'Stmt_Expression':
$this->foundStrayCode();
default:
$this->fatalError($v, 'Unsupported statement: ' . $type);
break;
}
}
@ -142,6 +143,8 @@ class Preprocessor extends CompilerBase
case 'Stmt_Use':
case 'Stmt_Const':
break;
case 'Stmt_Expression':
$this->foundStrayCode();
default:
abort($v2);
}
@ -174,6 +177,8 @@ class Preprocessor extends CompilerBase
case 'Stmt_ClassMethod':
$code .= $this->prepareFunction($v) . PHP_EOL;
break;
case 'Stmt_Expression':
$this->foundStrayCode();
default:
abort($v);
}

@ -72,6 +72,9 @@ endforeach;
static const zend_function_entry ext_functions[] = {
<?php
foreach ($this->functions as $functionDef):
if ($this->buildMode === 'ext' and $functionDef->name === 'main') {
continue;
}
?>
ZEND_FE(<?=$functionDef->name?>, arginfo_<?=$functionDef->name?>)
<?php endforeach;?>

@ -0,0 +1,15 @@
--TEST--
empty 2
--FILE--
<?php
$a = 0;
$b = $a ?: 1;
var_dump($b);
$c = 3;
$d = $c ?: 5;
var_dump($d);
?>
--EXPECT--
int(1)
int(3)
Loading…
Cancel
Save