Skip to content

Commit 34c84a1

Browse files
committed
gen_stub: Improve output for ce_flags compatibility
1 parent 7777939 commit 34c84a1

File tree

2 files changed

+16
-38
lines changed

2 files changed

+16
-38
lines changed

build/gen_stub.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3282,26 +3282,13 @@ public function getRegistration(array $allConstInfos): string
32823282

32833283
$code .= "{\n";
32843284

3285-
$flagCodes = generateVersionDependentFlagCode("%s", $this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility);
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-
}
3295-
32963285
$classMethods = ($this->funcInfos === []) ? 'NULL' : "class_{$escapedName}_methods";
32973286
if ($this->type === "enum") {
32983287
$name = addslashes((string) $this->name);
32993288
$backingType = $this->enumBackingType
33003289
? $this->enumBackingType->toTypeCode() : "IS_UNDEF";
33013290
$code .= "\tzend_class_entry *class_entry = zend_register_internal_enum(\"$name\", $backingType, $classMethods);\n";
3302-
if ($flags !== "") {
3303-
$code .= "\tclass_entry->ce_flags |=$flags\n";
3304-
}
3291+
$code .= implode("", generateVersionDependentFlagCode("\tclass_entry->ce_flags = %s;\n", $this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility));
33053292
} else {
33063293
$code .= "\tzend_class_entry ce, *class_entry;\n\n";
33073294
if (count($this->name->getParts()) > 1) {
@@ -3318,22 +3305,25 @@ public function getRegistration(array $allConstInfos): string
33183305
$code .= "#if (PHP_VERSION_ID >= " . PHP_84_VERSION_ID . ")\n";
33193306
}
33203307

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";
3308+
$template = "\tclass_entry = zend_register_internal_class_with_flags(&ce, " . (isset($this->extends[0]) ? "class_entry_" . str_replace("\\", "_", $this->extends[0]->toString()) : "NULL") . ", %s);\n";
3309+
$entries = generateVersionDependentFlagCode($template, $this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility ? max($this->phpVersionIdMinimumCompatibility, PHP_84_VERSION_ID) : null);
3310+
if ($entries !== []) {
3311+
$code .= implode("", $entries);
3312+
} else {
3313+
$code .= sprintf($template, "0");
3314+
}
33223315

33233316
if (!$php84MinimumCompatibility) {
33243317
$code .= "#else\n";
33253318

33263319
$code .= "\tclass_entry = zend_register_internal_class_ex(&ce, " . (isset($this->extends[0]) ? "class_entry_" . str_replace("\\", "_", $this->extends[0]->toString()) : "NULL") . ");\n";
3327-
if ($flags !== "") {
3328-
$code .= "\tclass_entry->ce_flags |=$flags;\n";
3329-
}
3320+
$code .= implode("", generateVersionDependentFlagCode("\tclass_entry->ce_flags |= %s;\n", $this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility));
33303321
$code .= "#endif\n";
33313322
}
33323323
} else {
33333324
$code .= "\tclass_entry = zend_register_internal_interface(&ce);\n";
3334-
if ($flags !== "") {
3335-
$code .= "\tclass_entry->ce_flags |=$flags\n";
3336-
}
3325+
$code .= implode("", generateVersionDependentFlagCode("\tclass_entry->ce_flags |= %s;\n", $this->getFlagsByPhpVersion(), $this->phpVersionIdMinimumCompatibility));
3326+
33373327
}
33383328
}
33393329

@@ -5399,16 +5389,12 @@ static function (array $value): bool {
53995389
$code = "";
54005390

54015391
$if = $i === 0 ? "#if" : "#elif";
5392+
$endif = $i === $flagCount - 1 ? "#endif\n" : "";
54025393

54035394
$code .= "$if (PHP_VERSION_ID >= $version)\n";
54045395

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

54135399
$result[] = $code;
54145400
$i++;

ext/zend_test/test_arginfo.h

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

0 commit comments

Comments
 (0)