From 9625eb1d688fdbe2f483c46e93e2896f702cb584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 18 Sep 2025 11:05:05 +0200 Subject: [PATCH 1/2] gen_stub: Fix handling of falsy preprocessor conditions --- build/gen_stub.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/gen_stub.php b/build/gen_stub.php index 8495f3612ce54..3b80b29a20a36 100755 --- a/build/gen_stub.php +++ b/build/gen_stub.php @@ -5111,7 +5111,7 @@ function generateCodeWithConditions( continue; } - if ($info->cond && $info->cond !== $parentCond) { + if ($info->cond !== null && $info->cond !== $parentCond) { if ($openCondition !== null && $info->cond !== $openCondition ) { From 9606c0cd2d78dc8c969a600f3799a9e4625415be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 18 Sep 2025 11:06:03 +0200 Subject: [PATCH 2/2] gen_stub: Add support for generation C `#include` statements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is useful to include the necessary files for a constant’s `@cvalue` from within the arginfo itself. --- build/gen_stub.php | 36 ++++++++++++++++++++++++++++++++++++ ext/zend_test/test.stub.php | 8 ++++++++ ext/zend_test/test_arginfo.h | 9 ++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/build/gen_stub.php b/build/gen_stub.php index 3b80b29a20a36..e63cd24e902dc 100755 --- a/build/gen_stub.php +++ b/build/gen_stub.php @@ -1299,6 +1299,19 @@ public function generateVersionDependentFlagCode( } } +class IncludeInfo { + public /* readonly */ ?string $cond; + public /* readonly */ string $include; + + public function __construct( + string $include, + ?string $cond, + ) { + $this->include = $include; + $this->cond = $cond; + } +} + class FuncInfo { public /* readonly */ FunctionOrMethodName $name; private /* readonly */ int $classFlags; @@ -4189,6 +4202,8 @@ class FileInfo { public array $funcInfos = []; /** @var ClassInfo[] */ public array $classInfos = []; + /** @var IncludeInfo[] */ + public array $includeInfos = []; public bool $generateFunctionEntries = false; public string $declarationPrefix = ""; public bool $generateClassEntries = false; @@ -4337,6 +4352,16 @@ private function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPri $conds = []; foreach ($stmts as $stmt) { $cond = self::handlePreprocessorConditions($conds, $stmt); + + if ($stmt instanceof Stmt\Declare_) { + foreach ($stmt->declares as $declare) { + if ($declare->key->name !== 'c_include') { + throw new Exception("Unexpected declare {$declare->key->name}"); + } + $this->includeInfos[] = new IncludeInfo((string)EvaluatedValue::createFromExpression($declare->value, null, null, [])->value, $cond); + } + continue; + } if ($stmt instanceof Stmt\Nop) { continue; @@ -5162,6 +5187,17 @@ function generateArgInfoCode( $generatedFuncInfos = []; + $argInfoCode = generateCodeWithConditions( + $fileInfo->includeInfos, "\n", + static function (IncludeInfo $includeInfo) { + return sprintf("#include %s\n", $includeInfo->include); + } + ); + + if ($argInfoCode !== "") { + $code .= "$argInfoCode\n"; + } + $argInfoCode = generateCodeWithConditions( $fileInfo->getAllFuncInfos(), "\n", static function (FuncInfo $funcInfo) use (&$generatedFuncInfos, $fileInfo) { diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index bf9a1c6b5bc8d..c2df64d83e5db 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -5,6 +5,14 @@ * @generate-legacy-arginfo 80000 * @undocumentable */ + +#if 0 +declare( + c_include='', + c_include='"zend_attributes.h"' +); +#endif + namespace { require "Zend/zend_attributes.stub.php"; diff --git a/ext/zend_test/test_arginfo.h b/ext/zend_test/test_arginfo.h index bd2240cedd637..06282c008c7a3 100644 --- a/ext/zend_test/test_arginfo.h +++ b/ext/zend_test/test_arginfo.h @@ -1,5 +1,12 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 6bccdc2444e6a68ba615fc281235a4551d0b8819 */ + * Stub hash: e62d828db8b1f30762db3b7df5e60ef4ed92f273 */ + +#if 0 +#include + +#include "zend_attributes.h" +#endif + ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_zend_trigger_bailout, 0, 0, IS_NEVER, 0) ZEND_END_ARG_INFO()