Skip to content

Commit 3f62f35

Browse files
committed
Support arbitrary parameter attributes in stubs
1 parent 7075706 commit 3f62f35

File tree

13 files changed

+615
-179
lines changed

13 files changed

+615
-179
lines changed

build/gen_stub.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -815,33 +815,35 @@ class ArgInfo {
815815
public $phpDocType;
816816
/** @var string|null */
817817
public $defaultValue;
818-
/** @var bool */
819-
public $isSensitive;
818+
/** @var AttributeInfo[] */
819+
public $attributes;
820820

821+
/**
822+
* @param AttributeInfo[] $attributes
823+
*/
821824
public function __construct(
822825
string $name,
823826
int $sendBy,
824827
bool $isVariadic,
825828
?Type $type,
826829
?Type $phpDocType,
827830
?string $defaultValue,
828-
bool $isSensitive
831+
array $attributes
829832
) {
830833
$this->name = $name;
831834
$this->sendBy = $sendBy;
832835
$this->isVariadic = $isVariadic;
833836
$this->setTypes($type, $phpDocType);
834837
$this->defaultValue = $defaultValue;
835-
$this->isSensitive = $isSensitive;
838+
$this->attributes = $attributes;
836839
}
837840

838841
public function equals(ArgInfo $other): bool {
839842
return $this->name === $other->name
840843
&& $this->sendBy === $other->sendBy
841844
&& $this->isVariadic === $other->isVariadic
842845
&& Type::equals($this->type, $other->type)
843-
&& $this->defaultValue === $other->defaultValue
844-
&& $this->isSensitive === $other->isSensitive;
846+
&& $this->defaultValue === $other->defaultValue;
845847
}
846848

847849
public function getSendByString(): string {
@@ -2570,7 +2572,7 @@ function (Name $item) {
25702572
}
25712573
}
25722574

2573-
if ($attributeInitializationCode = generateAttributeInitialization($this->funcInfos, $this->cond)) {
2575+
if ($attributeInitializationCode = generateAttributeInitialization($this->funcInfos, $this->cond, $allConstInfos)) {
25742576
if (!$php82MinimumCompatibility) {
25752577
$code .= "#if (PHP_VERSION_ID >= " . PHP_82_VERSION_ID . ")\n";
25762578
}
@@ -3259,16 +3261,10 @@ function parseFunctionLike(
32593261
foreach ($func->getParams() as $i => $param) {
32603262
$varName = $param->var->name;
32613263
$preferRef = !empty($paramMeta[$varName]['prefer-ref']);
3262-
$isSensitive = false;
3264+
$attributes = [];
32633265
foreach ($param->attrGroups as $attrGroup) {
32643266
foreach ($attrGroup->attrs as $attr) {
3265-
switch ($attr->name->toCodeString()) {
3266-
case '\\SensitiveParameter':
3267-
$isSensitive = true;
3268-
break;
3269-
default:
3270-
throw new Exception("Unhandled attribute {$attr->name->toCodeString()}.");
3271-
}
3267+
$attributes[] = new AttributeInfo($attr->name->toString(), $attr->args);
32723268
}
32733269
}
32743270
unset($paramMeta[$varName]);
@@ -3318,7 +3314,7 @@ function parseFunctionLike(
33183314
$type,
33193315
isset($docParamTypes[$varName]) ? Type::fromString($docParamTypes[$varName]) : null,
33203316
$param->default ? $prettyPrinter->prettyPrintExpr($param->default) : null,
3321-
$isSensitive
3317+
$attributes
33223318
);
33233319
if (!$param->default && !$param->variadic) {
33243320
$numRequiredArgs = $i + 1;
@@ -3972,7 +3968,7 @@ static function (FuncInfo $funcInfo) use ($fileInfo, &$generatedFunctionDeclarat
39723968
}
39733969

39743970
if ($fileInfo->generateClassEntries) {
3975-
$attributeInitializationCode = generateAttributeInitialization($fileInfo->funcInfos);
3971+
$attributeInitializationCode = generateAttributeInitialization($fileInfo->funcInfos, null, $allConstInfos);
39763972

39773973
if ($attributeInitializationCode !== "" || !empty($fileInfo->constInfos)) {
39783974
$code .= "\nstatic void register_{$stubFilenameWithoutExtension}_symbols(int module_number)\n";
@@ -4039,25 +4035,23 @@ function generateFunctionEntries(?Name $className, array $funcInfos, ?string $co
40394035
/**
40404036
* @param iterable<FuncInfo> $funcInfos
40414037
*/
4042-
function generateAttributeInitialization(iterable $funcInfos, ?string $parentCond = null): string {
4038+
function generateAttributeInitialization(iterable $funcInfos, ?string $parentCond = null, iterable $allConstInfos): string {
40434039
return generateCodeWithConditions(
40444040
$funcInfos,
40454041
"",
4046-
static function (FuncInfo $funcInfo) {
4042+
static function (FuncInfo $funcInfo) use ($allConstInfos) {
40474043
$code = null;
40484044

40494045
foreach ($funcInfo->args as $index => $arg) {
4050-
if (!$arg->isSensitive) {
4051-
continue;
4052-
}
4053-
40544046
if ($funcInfo->name instanceof MethodName) {
40554047
$functionTable = "&class_entry->function_table";
40564048
} else {
40574049
$functionTable = "CG(function_table)";
40584050
}
40594051

4060-
$code .= "\tzend_mark_function_parameter_as_sensitive($functionTable, \"" . $funcInfo->name->getNameForAttributes() . "\", $index);\n";
4052+
foreach ($arg->attributes as $attribute) {
4053+
$code .= $attribute->generateCode("zend_add_parameter_attribute(zend_hash_str_find_ptr($functionTable, \"" . $funcInfo->name->getNameForAttributes() . "\", sizeof(\"" . $funcInfo->name->getNameForAttributes() . "\") - 1), $index", "{$funcInfo->getArgInfoName()}_arg{$index}", $allConstInfos);
4054+
}
40614055
}
40624056

40634057
return $code;

ext/ftp/ftp_arginfo.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/hash/hash_arginfo.h

Lines changed: 28 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/imap/php_imap_arginfo.h

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/ldap/ldap_arginfo.h

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/mysqli/mysqli_arginfo.h

Lines changed: 28 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)