From 610815c0cecf6af23b778a76da1104b8f4d9ba3c Mon Sep 17 00:00:00 2001 From: =?utf8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Sat, 4 Apr 2020 21:26:46 +0200 Subject: [PATCH] Improve gen_stub.php Closes GH-5350 Add support for generating deprecated function entries, as well as forward declaration of function aliases. --- build/gen_stub.php | 30 +++-- ext/bz2/bz2.c | 38 ++----- ext/bz2/bz2.stub.php | 2 + ext/bz2/bz2_arginfo.h | 27 +++++ ext/ldap/ldap.c | 108 +----------------- ext/ldap/ldap.stub.php | 8 +- ext/ldap/ldap_arginfo.h | 210 +++++++++++++++++++++++++++++++++++ ext/zend_test/test.c | 14 +-- ext/zend_test/test.stub.php | 3 + ext/zend_test/test_arginfo.h | 23 ++++ 10 files changed, 303 insertions(+), 160 deletions(-) diff --git a/build/gen_stub.php b/build/gen_stub.php index 2207a1348b..ecad6fd81d 100755 --- a/build/gen_stub.php +++ b/build/gen_stub.php @@ -304,6 +304,8 @@ class FuncInfo { public $className; /** @var ?string */ public $alias; + /** @var bool */ + public $isDeprecated; /** @var ArgInfo[] */ public $args; /** @var ReturnInfo */ @@ -314,12 +316,13 @@ class FuncInfo { public $cond; public function __construct( - string $name, ?string $className, ?string $alias, array $args, ReturnInfo $return, + string $name, ?string $className, ?string $alias, bool $isDeprecated, array $args, ReturnInfo $return, int $numRequiredArgs, ?string $cond ) { $this->name = $name; $this->className = $className; $this->alias = $alias; + $this->isDeprecated = $isDeprecated; $this->args = $args; $this->return = $return; $this->numRequiredArgs = $numRequiredArgs; @@ -419,7 +422,7 @@ function parseDocComment(DocComment $comment): array { $commentText = substr($comment->getText(), 2, -2); $tags = []; foreach (explode("\n", $commentText) as $commentLine) { - $regex = '/^\*\s*@([a-z-]+)(?:\s+(.+))$/'; + $regex = '/^\*\s*@([a-z-]+)(?:\s+(.+))?$/'; if (preg_match($regex, trim($commentLine), $matches, PREG_UNMATCHED_AS_NULL)) { $tags[] = new DocCommentTag($matches[1], $matches[2]); } @@ -434,6 +437,7 @@ function parseFunctionLike( $comment = $func->getDocComment(); $paramMeta = []; $alias = null; + $isDeprecated = false; $haveDocReturnType = false; if ($comment) { @@ -447,6 +451,8 @@ function parseFunctionLike( $paramMeta[$varName]['preferRef'] = true; } else if ($tag->name === 'alias') { $alias = $tag->getValue(); + } else if ($tag->name === 'deprecated') { + $isDeprecated = true; } else if ($tag->name === 'return') { $haveDocReturnType = true; } @@ -507,7 +513,7 @@ function parseFunctionLike( $return = new ReturnInfo( $func->returnsByRef(), $returnType ? Type::fromNode($returnType) : null); - return new FuncInfo($name, $className, $alias, $args, $return, $numRequiredArgs, $cond); + return new FuncInfo($name, $className, $alias, $isDeprecated, $args, $return, $numRequiredArgs, $cond); } function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string { @@ -737,6 +743,7 @@ function generateCodeWithConditions( } function generateArgInfoCode(FileInfo $fileInfo): string { + $generatedDeclarations = []; $funcInfos = $fileInfo->funcInfos; $code = "/* This is a generated file, edit the .stub.php file instead. */\n"; @@ -761,12 +768,15 @@ function generateArgInfoCode(FileInfo $fileInfo): string { if ($fileInfo->generateFunctionEntries) { $code .= "\n\n"; - $code .= generateCodeWithConditions($fileInfo->funcInfos, "", function(FuncInfo $funcInfo) { - if ($funcInfo->alias) { + $code .= generateCodeWithConditions($funcInfos, "", function(FuncInfo $funcInfo) use (&$generatedDeclarations) { + $name = $funcInfo->alias ?? $funcInfo->name; + $key = "$name|$funcInfo->cond"; + if (isset($generatedDeclarations[$key])) { return null; } - return "ZEND_FUNCTION($funcInfo->name);\n"; + $generatedDeclarations[$key] = true; + return "ZEND_FUNCTION($name);\n"; }); $code .= "\n\nstatic const zend_function_entry ext_functions[] = {\n"; @@ -776,9 +786,13 @@ function generateArgInfoCode(FileInfo $fileInfo): string { "\tZEND_FALIAS(%s, %s, %s)\n", $funcInfo->name, $funcInfo->alias, $funcInfo->getArgInfoName() ); - } else { - return sprintf("\tZEND_FE(%s, %s)\n", $funcInfo->name, $funcInfo->getArgInfoName()); } + + if ($funcInfo->isDeprecated) { + return sprintf("\tZEND_DEP_FE(%s, %s)\n", $funcInfo->name, $funcInfo->getArgInfoName()); + } + + return sprintf("\tZEND_FE(%s, %s)\n", $funcInfo->name, $funcInfo->getArgInfoName()); }); $code .= "\tZEND_FE_END\n"; $code .= "};\n"; diff --git a/ext/bz2/bz2.c b/ext/bz2/bz2.c index 005520c2ad..29046e7122 100644 --- a/ext/bz2/bz2.c +++ b/ext/bz2/bz2.c @@ -25,7 +25,6 @@ #if HAVE_BZ2 /* PHP Includes */ -#include "ext/standard/file.h" #include "ext/standard/info.h" #include "ext/standard/php_string.h" #include "main/php_network.h" @@ -41,32 +40,11 @@ static PHP_MINIT_FUNCTION(bz2); static PHP_MSHUTDOWN_FUNCTION(bz2); static PHP_MINFO_FUNCTION(bz2); -static PHP_FUNCTION(bzopen); -static PHP_FUNCTION(bzread); -static PHP_FUNCTION(bzerrno); -static PHP_FUNCTION(bzerrstr); -static PHP_FUNCTION(bzerror); -static PHP_FUNCTION(bzcompress); -static PHP_FUNCTION(bzdecompress); - -static const zend_function_entry bz2_functions[] = { - PHP_FE(bzopen, arginfo_bzopen) - PHP_FE(bzread, arginfo_bzread) - PHP_FALIAS(bzwrite, fwrite, arginfo_bzwrite) - PHP_FALIAS(bzflush, fflush, arginfo_bzflush) - PHP_FALIAS(bzclose, fclose, arginfo_bzclose) - PHP_FE(bzerrno, arginfo_bzerrno) - PHP_FE(bzerrstr, arginfo_bzerrstr) - PHP_FE(bzerror, arginfo_bzerror) - PHP_FE(bzcompress, arginfo_bzcompress) - PHP_FE(bzdecompress, arginfo_bzdecompress) - PHP_FE_END -}; zend_module_entry bz2_module_entry = { STANDARD_MODULE_HEADER, "bz2", - bz2_functions, + ext_functions, PHP_MINIT(bz2), PHP_MSHUTDOWN(bz2), NULL, @@ -325,7 +303,7 @@ static PHP_MINFO_FUNCTION(bz2) /* {{{ proto string bzread(resource bz[, int length]) Reads up to length bytes from a BZip2 stream, or 1024 bytes if length is not specified */ -static PHP_FUNCTION(bzread) +PHP_FUNCTION(bzread) { zval *bz; zend_long len = 1024; @@ -353,7 +331,7 @@ static PHP_FUNCTION(bzread) /* {{{ proto resource bzopen(string|int file|fp, string mode) Opens a new BZip2 stream */ -static PHP_FUNCTION(bzopen) +PHP_FUNCTION(bzopen) { zval *file; /* The file to open */ char *mode; /* The mode to open the stream with */ @@ -444,7 +422,7 @@ static PHP_FUNCTION(bzopen) /* {{{ proto int bzerrno(resource bz) Returns the error number */ -static PHP_FUNCTION(bzerrno) +PHP_FUNCTION(bzerrno) { php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRNO); } @@ -452,7 +430,7 @@ static PHP_FUNCTION(bzerrno) /* {{{ proto string bzerrstr(resource bz) Returns the error string */ -static PHP_FUNCTION(bzerrstr) +PHP_FUNCTION(bzerrstr) { php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRSTR); } @@ -460,7 +438,7 @@ static PHP_FUNCTION(bzerrstr) /* {{{ proto array bzerror(resource bz) Returns the error number and error string in an associative array */ -static PHP_FUNCTION(bzerror) +PHP_FUNCTION(bzerror) { php_bz2_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_BZ_ERRBOTH); } @@ -468,7 +446,7 @@ static PHP_FUNCTION(bzerror) /* {{{ proto string bzcompress(string source [, int blocksize100k [, int workfactor]]) Compresses a string into BZip2 encoded data */ -static PHP_FUNCTION(bzcompress) +PHP_FUNCTION(bzcompress) { char *source; /* Source data to compress */ zend_long zblock_size = 0; /* Optional block size to use */ @@ -519,7 +497,7 @@ static PHP_FUNCTION(bzcompress) /* {{{ proto string bzdecompress(string source [, int small]) Decompresses BZip2 compressed data */ -static PHP_FUNCTION(bzdecompress) +PHP_FUNCTION(bzdecompress) { char *source; zend_string *dest; diff --git a/ext/bz2/bz2.stub.php b/ext/bz2/bz2.stub.php index 6c871e3990..2d47c04501 100644 --- a/ext/bz2/bz2.stub.php +++ b/ext/bz2/bz2.stub.php @@ -1,5 +1,7 @@ 2000) || HAVE_ORALDAP - PHP_FE(ldap_rename, arginfo_ldap_rename) - PHP_FE(ldap_rename_ext, arginfo_ldap_rename_ext) - PHP_FE(ldap_get_option, arginfo_ldap_get_option) - PHP_FE(ldap_set_option, arginfo_ldap_set_option) - PHP_FE(ldap_first_reference, arginfo_ldap_first_reference) - PHP_FE(ldap_next_reference, arginfo_ldap_next_reference) -#ifdef HAVE_LDAP_PARSE_REFERENCE - PHP_FE(ldap_parse_reference, arginfo_ldap_parse_reference) -#endif -#ifdef HAVE_LDAP_PARSE_RESULT - PHP_FE(ldap_parse_result, arginfo_ldap_parse_result) -#endif -#ifdef HAVE_LDAP_START_TLS_S - PHP_FE(ldap_start_tls, arginfo_ldap_start_tls) -#endif -#ifdef HAVE_LDAP_EXTENDED_OPERATION_S - PHP_FE(ldap_exop, arginfo_ldap_exop) -#endif -#ifdef HAVE_LDAP_PASSWD - PHP_FE(ldap_exop_passwd, arginfo_ldap_exop_passwd) -#endif -#ifdef HAVE_LDAP_WHOAMI_S - PHP_FE(ldap_exop_whoami, arginfo_ldap_exop_whoami) -#endif -#ifdef HAVE_LDAP_REFRESH_S - PHP_FE(ldap_exop_refresh, arginfo_ldap_exop_refresh) -#endif -#ifdef HAVE_LDAP_PARSE_EXTENDED_RESULT - PHP_FE(ldap_parse_exop, arginfo_ldap_parse_exop) -#endif -#endif - -#if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC) - PHP_FE(ldap_set_rebind_proc, arginfo_ldap_set_rebind_proc) -#endif - - PHP_FE(ldap_escape, arginfo_ldap_escape) - -#ifdef STR_TRANSLATION - PHP_FE(ldap_t61_to_8859, arginfo_ldap_t61_to_8859) - PHP_FE(ldap_8859_to_t61, arginfo_ldap_8859_to_t61) -#endif - -#ifdef LDAP_CONTROL_PAGEDRESULTS - PHP_DEP_FE(ldap_control_paged_result, arginfo_ldap_control_paged_result) - PHP_DEP_FE(ldap_control_paged_result_response, arginfo_ldap_control_paged_result_response) -#endif - PHP_FE_END -}; -/* }}} */ - zend_module_entry ldap_module_entry = { /* {{{ */ STANDARD_MODULE_HEADER, "ldap", - ldap_functions, + ext_functions, PHP_MINIT(ldap), PHP_MSHUTDOWN(ldap), NULL, diff --git a/ext/ldap/ldap.stub.php b/ext/ldap/ldap.stub.php index 8224bc1583..afe6b7e918 100644 --- a/ext/ldap/ldap.stub.php +++ b/ext/ldap/ldap.stub.php @@ -1,6 +1,6 @@ 2000) || HAVE_NSLDAP || HAVE_ORALDAP +ZEND_FUNCTION(ldap_rename); +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP +ZEND_FUNCTION(ldap_rename_ext); +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP +ZEND_FUNCTION(ldap_get_option); +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP +ZEND_FUNCTION(ldap_set_option); +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP +ZEND_FUNCTION(ldap_first_reference); +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP +ZEND_FUNCTION(ldap_next_reference); +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP && defined(HAVE_LDAP_PARSE_REFERENCE) +ZEND_FUNCTION(ldap_parse_reference); +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP && defined(HAVE_LDAP_PARSE_RESULT) +ZEND_FUNCTION(ldap_parse_result); +#endif +#if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC) +ZEND_FUNCTION(ldap_set_rebind_proc); +#endif +#if defined(HAVE_LDAP_START_TLS_S) +ZEND_FUNCTION(ldap_start_tls); +#endif +ZEND_FUNCTION(ldap_escape); +#if defined(STR_TRANSLATION) +ZEND_FUNCTION(ldap_t61_to_8859); +#endif +#if defined(STR_TRANSLATION) +ZEND_FUNCTION(ldap_8859_to_t61); +#endif +#if defined(HAVE_LDAP_EXTENDED_OPERATION_S) +ZEND_FUNCTION(ldap_exop); +#endif +#if defined(HAVE_LDAP_PASSWD) +ZEND_FUNCTION(ldap_exop_passwd); +#endif +#if defined(HAVE_LDAP_WHOAMI_S) +ZEND_FUNCTION(ldap_exop_whoami); +#endif +#if defined(HAVE_LDAP_REFRESH_S) +ZEND_FUNCTION(ldap_exop_refresh); +#endif +#if defined(HAVE_LDAP_PARSE_EXTENDED_RESULT) +ZEND_FUNCTION(ldap_parse_exop); +#endif + + +static const zend_function_entry ext_functions[] = { +#if defined(HAVE_ORALDAP) + ZEND_FE(ldap_connect, arginfo_ldap_connect) +#endif +#if !(defined(HAVE_ORALDAP)) + ZEND_FE(ldap_connect, arginfo_ldap_connect) +#endif + ZEND_FE(ldap_unbind, arginfo_ldap_unbind) + ZEND_FALIAS(ldap_close, ldap_unbind, arginfo_ldap_close) + ZEND_FE(ldap_bind, arginfo_ldap_bind) + ZEND_FE(ldap_bind_ext, arginfo_ldap_bind_ext) +#if defined(HAVE_LDAP_SASL) + ZEND_FE(ldap_sasl_bind, arginfo_ldap_sasl_bind) +#endif + ZEND_FE(ldap_read, arginfo_ldap_read) + ZEND_FE(ldap_list, arginfo_ldap_list) + ZEND_FE(ldap_search, arginfo_ldap_search) + ZEND_FE(ldap_free_result, arginfo_ldap_free_result) + ZEND_FE(ldap_count_entries, arginfo_ldap_count_entries) + ZEND_FE(ldap_first_entry, arginfo_ldap_first_entry) + ZEND_FE(ldap_next_entry, arginfo_ldap_next_entry) + ZEND_FE(ldap_get_entries, arginfo_ldap_get_entries) + ZEND_FE(ldap_first_attribute, arginfo_ldap_first_attribute) + ZEND_FE(ldap_next_attribute, arginfo_ldap_next_attribute) + ZEND_FE(ldap_get_attributes, arginfo_ldap_get_attributes) + ZEND_FE(ldap_get_values_len, arginfo_ldap_get_values_len) + ZEND_FALIAS(ldap_get_values, ldap_get_values_len, arginfo_ldap_get_values) + ZEND_FE(ldap_get_dn, arginfo_ldap_get_dn) + ZEND_FE(ldap_explode_dn, arginfo_ldap_explode_dn) + ZEND_FE(ldap_dn2ufn, arginfo_ldap_dn2ufn) + ZEND_FE(ldap_add, arginfo_ldap_add) + ZEND_FE(ldap_add_ext, arginfo_ldap_add_ext) + ZEND_FE(ldap_delete, arginfo_ldap_delete) + ZEND_FE(ldap_delete_ext, arginfo_ldap_delete_ext) + ZEND_FE(ldap_modify_batch, arginfo_ldap_modify_batch) + ZEND_FE(ldap_mod_add, arginfo_ldap_mod_add) + ZEND_FE(ldap_mod_add_ext, arginfo_ldap_mod_add_ext) + ZEND_FE(ldap_mod_replace, arginfo_ldap_mod_replace) + ZEND_FALIAS(ldap_modify, ldap_mod_replace, arginfo_ldap_modify) + ZEND_FE(ldap_mod_replace_ext, arginfo_ldap_mod_replace_ext) + ZEND_FE(ldap_mod_del, arginfo_ldap_mod_del) + ZEND_FE(ldap_mod_del_ext, arginfo_ldap_mod_del_ext) + ZEND_FE(ldap_errno, arginfo_ldap_errno) + ZEND_FE(ldap_error, arginfo_ldap_error) + ZEND_FE(ldap_err2str, arginfo_ldap_err2str) + ZEND_FE(ldap_compare, arginfo_ldap_compare) +#if defined(LDAP_CONTROL_PAGEDRESULTS) + ZEND_DEP_FE(ldap_control_paged_result, arginfo_ldap_control_paged_result) +#endif +#if defined(LDAP_CONTROL_PAGEDRESULTS) + ZEND_DEP_FE(ldap_control_paged_result_response, arginfo_ldap_control_paged_result_response) +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP + ZEND_FE(ldap_rename, arginfo_ldap_rename) +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP + ZEND_FE(ldap_rename_ext, arginfo_ldap_rename_ext) +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP + ZEND_FE(ldap_get_option, arginfo_ldap_get_option) +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP + ZEND_FE(ldap_set_option, arginfo_ldap_set_option) +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP + ZEND_FE(ldap_first_reference, arginfo_ldap_first_reference) +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP + ZEND_FE(ldap_next_reference, arginfo_ldap_next_reference) +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP && defined(HAVE_LDAP_PARSE_REFERENCE) + ZEND_FE(ldap_parse_reference, arginfo_ldap_parse_reference) +#endif +#if (LDAP_API_VERSION > 2000) || HAVE_NSLDAP || HAVE_ORALDAP && defined(HAVE_LDAP_PARSE_RESULT) + ZEND_FE(ldap_parse_result, arginfo_ldap_parse_result) +#endif +#if defined(LDAP_API_FEATURE_X_OPENLDAP) && defined(HAVE_3ARG_SETREBINDPROC) + ZEND_FE(ldap_set_rebind_proc, arginfo_ldap_set_rebind_proc) +#endif +#if defined(HAVE_LDAP_START_TLS_S) + ZEND_FE(ldap_start_tls, arginfo_ldap_start_tls) +#endif + ZEND_FE(ldap_escape, arginfo_ldap_escape) +#if defined(STR_TRANSLATION) + ZEND_FE(ldap_t61_to_8859, arginfo_ldap_t61_to_8859) +#endif +#if defined(STR_TRANSLATION) + ZEND_FE(ldap_8859_to_t61, arginfo_ldap_8859_to_t61) +#endif +#if defined(HAVE_LDAP_EXTENDED_OPERATION_S) + ZEND_FE(ldap_exop, arginfo_ldap_exop) +#endif +#if defined(HAVE_LDAP_PASSWD) + ZEND_FE(ldap_exop_passwd, arginfo_ldap_exop_passwd) +#endif +#if defined(HAVE_LDAP_WHOAMI_S) + ZEND_FE(ldap_exop_whoami, arginfo_ldap_exop_whoami) +#endif +#if defined(HAVE_LDAP_REFRESH_S) + ZEND_FE(ldap_exop_refresh, arginfo_ldap_exop_refresh) +#endif +#if defined(HAVE_LDAP_PARSE_EXTENDED_RESULT) + ZEND_FE(ldap_parse_exop, arginfo_ldap_parse_exop) +#endif + ZEND_FE_END +}; diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 1851202348..f7c2913fac 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -311,22 +311,10 @@ PHP_MINFO_FUNCTION(zend_test) php_info_print_table_end(); } -static const zend_function_entry zend_test_functions[] = { - ZEND_FE(zend_test_array_return, arginfo_zend_test_array_return) - ZEND_FE(zend_test_nullable_array_return, arginfo_zend_test_nullable_array_return) - ZEND_FE(zend_test_void_return, arginfo_zend_test_void_return) - ZEND_DEP_FE(zend_test_deprecated, arginfo_zend_test_deprecated) - ZEND_FE(zend_create_unterminated_string, arginfo_zend_create_unterminated_string) - ZEND_FE(zend_terminate_string, arginfo_zend_terminate_string) - ZEND_FE(zend_leak_bytes, arginfo_zend_leak_bytes) - ZEND_FE(zend_leak_variable, arginfo_zend_leak_variable) - ZEND_FE_END -}; - zend_module_entry zend_test_module_entry = { STANDARD_MODULE_HEADER, "zend-test", - zend_test_functions, + ext_functions, PHP_MINIT(zend_test), PHP_MSHUTDOWN(zend_test), PHP_RINIT(zend_test), diff --git a/ext/zend_test/test.stub.php b/ext/zend_test/test.stub.php index b75dfea9c0..84724fd255 100644 --- a/ext/zend_test/test.stub.php +++ b/ext/zend_test/test.stub.php @@ -1,5 +1,7 @@