Skip to content

Commit d3135a2

Browse files
committed
gen_stub: Fix ce_flags generation for compatibility mode
Fixes #18506
1 parent 5825a6b commit d3135a2

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

build/gen_stub.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,7 +3283,15 @@ public function getRegistration(array $allConstInfos): string
32833283
$code .= "{\n";
32843284

32853285
$flagCodes = generateVersionDependentFlagCode("%s", $this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility);
3286-
$flags = implode("", $flagCodes);
3286+
if (count($flagCodes) > 1) {
3287+
// If we have more than one entry, there will be preprocessor conditions,
3288+
// thus we need to start with a newline.
3289+
$flags = "\n" . implode("\n", $flagCodes);
3290+
} else if (count($flagCodes) === 1) {
3291+
$flags = " " . $flagCodes[0];
3292+
} else {
3293+
$flags = "";
3294+
}
32873295

32883296
$classMethods = ($this->funcInfos === []) ? 'NULL' : "class_{$escapedName}_methods";
32893297
if ($this->type === "enum") {
@@ -3292,7 +3300,7 @@ public function getRegistration(array $allConstInfos): string
32923300
? $this->enumBackingType->toTypeCode() : "IS_UNDEF";
32933301
$code .= "\tzend_class_entry *class_entry = zend_register_internal_enum(\"$name\", $backingType, $classMethods);\n";
32943302
if ($flags !== "") {
3295-
$code .= "\tclass_entry->ce_flags |= $flags\n";
3303+
$code .= "\tclass_entry->ce_flags |=$flags\n";
32963304
}
32973305
} else {
32983306
$code .= "\tzend_class_entry ce, *class_entry;\n\n";
@@ -3310,21 +3318,21 @@ public function getRegistration(array $allConstInfos): string
33103318
$code .= "#if (PHP_VERSION_ID >= " . PHP_84_VERSION_ID . ")\n";
33113319
}
33123320

3313-
$code .= "\tclass_entry = zend_register_internal_class_with_flags(&ce, " . (isset($this->extends[0]) ? "class_entry_" . str_replace("\\", "_", $this->extends[0]->toString()) : "NULL") . ", " . ($flags ?: 0) . ");\n";
3321+
$code .= "\tclass_entry = zend_register_internal_class_with_flags(&ce, " . (isset($this->extends[0]) ? "class_entry_" . str_replace("\\", "_", $this->extends[0]->toString()) : "NULL") . "," . ($flags ?: " 0") . ");\n";
33143322

33153323
if (!$php84MinimumCompatibility) {
33163324
$code .= "#else\n";
33173325

33183326
$code .= "\tclass_entry = zend_register_internal_class_ex(&ce, " . (isset($this->extends[0]) ? "class_entry_" . str_replace("\\", "_", $this->extends[0]->toString()) : "NULL") . ");\n";
33193327
if ($flags !== "") {
3320-
$code .= "\tclass_entry->ce_flags |= $flags;\n";
3328+
$code .= "\tclass_entry->ce_flags |=$flags;\n";
33213329
}
33223330
$code .= "#endif\n";
33233331
}
33243332
} else {
33253333
$code .= "\tclass_entry = zend_register_internal_interface(&ce);\n";
33263334
if ($flags !== "") {
3327-
$code .= "\tclass_entry->ce_flags |= $flags\n";
3335+
$code .= "\tclass_entry->ce_flags |=$flags\n";
33283336
}
33293337
}
33303338
}
@@ -5391,12 +5399,16 @@ static function (array $value): bool {
53915399
$code = "";
53925400

53935401
$if = $i === 0 ? "#if" : "#elif";
5394-
$endif = $i === $flagCount - 1 ? "#endif\n" : "";
53955402

53965403
$code .= "$if (PHP_VERSION_ID >= $version)\n";
53975404

53985405
$code .= sprintf($codeTemplate, implode("|", $versionFlags));
5399-
$code .= $endif;
5406+
if ($i === $flagCount - 1) {
5407+
if (!str_ends_with($code, "\n")) {
5408+
$code .= "\n";
5409+
}
5410+
$code .= "#endif\n";
5411+
}
54005412

54015413
$result[] = $code;
54025414
$i++;

ext/zend_test/test.stub.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ class _ZendTestChildClass extends _ZendTestClass
8585
public function returnsThrowable(): Exception {}
8686
}
8787

88+
/**
89+
* @not-serializable
90+
*/
91+
final class GenStubFlagCompatibilityTest {
92+
93+
}
94+
8895
class ZendAttributeTest {
8996
/** @var int */
9097
#[ZendTestRepeatableAttribute]

ext/zend_test/test_arginfo.h

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

0 commit comments

Comments
 (0)