Skip to content

Commit ae721c4

Browse files
committed
Handle preprocessor conditions inside classes
Also remove the dead parseClass() function.
1 parent 9c02c58 commit ae721c4

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

scripts/dev/gen_stub.php

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,32 @@ function parseFunctionLike(string $name, Node\FunctionLike $func, ?string $cond)
194194
return new FuncInfo($name, $args, $return, $numRequiredArgs, $cond);
195195
}
196196

197-
function parseClass(Stmt\Class_ $class): ClassInfo {
198-
$funcs = [];
199-
$className = $class->name->toString();
200-
foreach ($class as $stmt) {
201-
if (!$stmt instanceof Stmt\ClassMethod) {
202-
throw new Exception("Not implemented class statement");
197+
function handlePreprocessorConditions(array &$conds, Stmt $stmt): ?string {
198+
foreach ($stmt->getComments() as $comment) {
199+
$text = trim($comment->getText());
200+
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
201+
$conds[] = $matches[1];
202+
} else if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches)) {
203+
$conds[] = "defined($matches[1])";
204+
} else if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches)) {
205+
$conds[] = "!defined($matches[1])";
206+
} else if (preg_match('/^#\s*else$/', $text)) {
207+
if (empty($conds)) {
208+
throw new Exception("Encountered else without corresponding #if");
209+
}
210+
$cond = array_pop($conds);
211+
$conds[] = "!($cond)";
212+
} else if (preg_match('/^#\s*endif$/', $text)) {
213+
if (empty($conds)) {
214+
throw new Exception("Encountered #endif without corresponding #if");
215+
}
216+
array_pop($conds);
217+
} else if ($text[0] === '#') {
218+
throw new Exception("Unrecognized preprocessor directive \"$text\"");
203219
}
204-
205-
$funcs[] = parseFunctionLike($className . '_' . $stmt->name->toString(), $stmt);
206220
}
207-
return new ClassInfo($className, $funcs);
221+
222+
return empty($conds) ? null : implode(' && ', $conds);
208223
}
209224

210225
/** @return FuncInfo[] */
@@ -226,32 +241,7 @@ function parseStubFile(string $fileName) {
226241
$funcInfos = [];
227242
$conds = [];
228243
foreach ($stmts as $stmt) {
229-
foreach ($stmt->getComments() as $comment) {
230-
$text = trim($comment->getText());
231-
if (preg_match('/^#\s*if\s+(.+)$/', $text, $matches)) {
232-
$conds[] = $matches[1];
233-
} else if (preg_match('/^#\s*ifdef\s+(.+)$/', $text, $matches)) {
234-
$conds[] = "defined($matches[1])";
235-
} else if (preg_match('/^#\s*ifndef\s+(.+)$/', $text, $matches)) {
236-
$conds[] = "!defined($matches[1])";
237-
} else if (preg_match('/^#\s*else$/', $text)) {
238-
if (empty($conds)) {
239-
throw new Exception("Encountered else without corresponding #if");
240-
}
241-
$cond = array_pop($conds);
242-
$conds[] = "!($cond)";
243-
} else if (preg_match('/^#\s*endif$/', $text)) {
244-
if (empty($conds)) {
245-
throw new Exception("Encountered #endif without corresponding #if");
246-
}
247-
array_pop($conds);
248-
} else if ($text[0] === '#') {
249-
throw new Exception("Unrecognized preprocessor directive \"$text\"");
250-
}
251-
}
252-
253-
$cond = empty($conds) ? null : implode(' && ', $conds);
254-
244+
$cond = handlePreprocessorConditions($conds, $stmt);
255245
if ($stmt instanceof Stmt\Nop) {
256246
continue;
257247
}
@@ -264,6 +254,7 @@ function parseStubFile(string $fileName) {
264254
if ($stmt instanceof Stmt\ClassLike) {
265255
$className = $stmt->name->toString();
266256
foreach ($stmt->stmts as $classStmt) {
257+
$cond = handlePreprocessorConditions($conds, $classStmt);
267258
if ($classStmt instanceof Stmt\Nop) {
268259
continue;
269260
}

0 commit comments

Comments
 (0)