Skip to content

gen_stub: Conditionally include zend_attributes.h to arginfo based on usage - #21570

Merged
kocsismate merged 3 commits into
php:masterfrom
dkulyk:gen_stub
Sep 6, 2026
Merged

gen_stub: Conditionally include zend_attributes.h to arginfo based on usage#21570
kocsismate merged 3 commits into
php:masterfrom
dkulyk:gen_stub

Conversation

@dkulyk

@dkulyk dkulyk commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

Conditionally include zend_attributes.h only when attributes are actually used.

Currently, zend_attributes.h must be explicitly included in C source files even when only common attributes (e.g. #[\Deprecated]) are needed. This introduces unnecessary coupling between extensions and attribute internals.

This change removes the requirement to manually include the header for common attributes, reducing boilerplate and avoiding unnecessary dependencies.

It does not affect behavior or runtime — only improves developer ergonomics and compilation dependencies.

@kocsismate

Copy link
Copy Markdown
Member

Another complication: some functions are declared conditionally:

function ldap_connect(?string $uri = null, int $port = 389, string $wallet = UNKNOWN, #[\SensitiveParameter] string $password = UNKNOWN, int $auth_mode = GSLC_SSL_NO_AUTH): LDAP\Connection|false {}

And the related attributes are declared with the same conditions: e.g.

#if defined(HAVE_ORALDAP)

So ideally, any includes should be wrapped within the necessary conditions.

There are already a few parameters which are passed to all generating methods (e.g. allConstInfos or $declaredStrings). I don't mind adding yet another one. Alternatively, you could create a DTO containing these classes.

And there's already a Context class which stores global state, but I wouldn't put a file based info into this one.

@dkulyk

dkulyk commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

Reworked. Header dependencies are collected during generation and passed down as a parameter, so the includes come from the code that emits the declaration rather than from scanning the output.

All four points covered: collection in a HeaderDependencies instance owned by FileInfo, zend_constants.h and zend_enum.h alongside zend_attributes.h, and includes carrying the same version and preprocessor guards as the code needing them. Several conditions for one header merge into #if (A) || (B); a header also needed unconditionally is emitted without a guard.

The third commit drops manual includes that became redundant.

Comment thread ext/uri/php_uri.c Outdated
Comment thread build/gen_stub.php Outdated
Comment thread build/gen_stub.php Outdated
Comment thread build/gen_stub.php Outdated
Headers required by the generated code (zend_attributes.h, zend_constants.h,
zend_enum.h) are collected during generation and emitted as includes at the
top of the arginfo file, with the same preprocessor conditions and PHP
version guards as the code that needs them.
Adds the zend_attributes.h, zend_constants.h and zend_enum.h includes now
emitted by gen_stub.php.
Removes the manual zend_attributes.h and zend_enum.h includes from sources
that only needed them for the arginfo file they include.
@kocsismate

Copy link
Copy Markdown
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants