gen_stub: move handlePreprocessorConditions() into FileInfo()

Reduce the number of global functions by moving it to static method
`FileInfo::handlePreprocessorConditions()`. Since it is only used by
`FileInfo::handleStatements()`, also make it private.
This commit is contained in:
Daniel Scherzer 2025-03-18 12:58:27 -07:00 committed by DanielEScherzer
parent 1c9b6b84df
commit ce3990c1d3

View file

@ -4302,7 +4302,7 @@ class FileInfo {
public function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrinter): void { public function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrinter): void {
$conds = []; $conds = [];
foreach ($stmts as $stmt) { foreach ($stmts as $stmt) {
$cond = handlePreprocessorConditions($conds, $stmt); $cond = self::handlePreprocessorConditions($conds, $stmt);
if ($stmt instanceof Stmt\Nop) { if ($stmt instanceof Stmt\Nop) {
continue; continue;
@ -4352,7 +4352,7 @@ class FileInfo {
$methodInfos = []; $methodInfos = [];
$enumCaseInfos = []; $enumCaseInfos = [];
foreach ($stmt->stmts as $classStmt) { foreach ($stmt->stmts as $classStmt) {
$cond = handlePreprocessorConditions($conds, $classStmt); $cond = self::handlePreprocessorConditions($conds, $classStmt);
if ($classStmt instanceof Stmt\Nop) { if ($classStmt instanceof Stmt\Nop) {
continue; continue;
} }
@ -4442,6 +4442,34 @@ class FileInfo {
throw new Exception("Unterminated preprocessor conditions"); throw new Exception("Unterminated preprocessor conditions");
} }
} }
private static function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string {
foreach ($stmt->getComments() as $comment) {
$text = trim($comment->getText());
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
$conds[] = $matches[1];
} else if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches)) {
$conds[] = "defined($matches[1])";
} else if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches)) {
$conds[] = "!defined($matches[1])";
} else if (preg_match('/^#\s*else$/', $text)) {
if (empty($conds)) {
throw new Exception("Encountered else without corresponding #if");
}
$cond = array_pop($conds);
$conds[] = "!($cond)";
} else if (preg_match('/^#\s*endif$/', $text)) {
if (empty($conds)) {
throw new Exception("Encountered #endif without corresponding #if");
}
array_pop($conds);
} else if ($text[0] === '#') {
throw new Exception("Unrecognized preprocessor directive \"$text\"");
}
}
return empty($conds) ? null : implode(' && ', $conds);
}
} }
class DocCommentTag { class DocCommentTag {
@ -5014,34 +5042,6 @@ function parseClass(
); );
} }
function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string {
foreach ($stmt->getComments() as $comment) {
$text = trim($comment->getText());
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
$conds[] = $matches[1];
} else if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches)) {
$conds[] = "defined($matches[1])";
} else if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches)) {
$conds[] = "!defined($matches[1])";
} else if (preg_match('/^#\s*else$/', $text)) {
if (empty($conds)) {
throw new Exception("Encountered else without corresponding #if");
}
$cond = array_pop($conds);
$conds[] = "!($cond)";
} else if (preg_match('/^#\s*endif$/', $text)) {
if (empty($conds)) {
throw new Exception("Encountered #endif without corresponding #if");
}
array_pop($conds);
} else if ($text[0] === '#') {
throw new Exception("Unrecognized preprocessor directive \"$text\"");
}
}
return empty($conds) ? null : implode(' && ', $conds);
}
/** @return DocComment[] */ /** @return DocComment[] */
function getFileDocComments(array $stmts): array { function getFileDocComments(array $stmts): array {
if (empty($stmts)) { if (empty($stmts)) {