From 60d1a627aadd994b7aa1b506c42b6ba10bd78d7a 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 acee1e6ca0981..963237f6d5978 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 3a289ce1a59e94f2cf69f0718e3fd30de24d7047 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 963237f6d5978..ee201dd6afe2b 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 d0c0c64b8b1d0..33f6ca71e9ff7 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 683b3b38648b6..7ea6dea47298c 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: a8dae89983ccbcd5dd36d1cdee736d40af4fd33c */ + * Stub hash: 12fb82ded803c8358ccd26399caf000b01d57b26 */ + +#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()