summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-08-10 16:28:57 +0200
committerNikita Popov <nikita.ppv@gmail.com>2019-08-10 16:29:45 +0200
commitae721c488dc31c59a5a05b4220564ee8dbf8fbb0 (patch)
treec80997ffba76d787e34534b9e3da6511fd88bb3a /scripts
parent9c02c584819369b7e1fd1ab515a2e5c411355b11 (diff)
downloadphp-git-ae721c488dc31c59a5a05b4220564ee8dbf8fbb0.tar.gz
Handle preprocessor conditions inside classes
Also remove the dead parseClass() function.
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/dev/gen_stub.php61
1 files changed, 26 insertions, 35 deletions
diff --git a/scripts/dev/gen_stub.php b/scripts/dev/gen_stub.php
index f61893050c..80496cc013 100755
--- a/scripts/dev/gen_stub.php
+++ b/scripts/dev/gen_stub.php
@@ -194,17 +194,32 @@ function parseFunctionLike(string $name, Node\FunctionLike $func, ?string $cond)
return new FuncInfo($name, $args, $return, $numRequiredArgs, $cond);
}
-function parseClass(Stmt\Class_ $class): ClassInfo {
- $funcs = [];
- $className = $class->name->toString();
- foreach ($class as $stmt) {
- if (!$stmt instanceof Stmt\ClassMethod) {
- throw new Exception("Not implemented class statement");
+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\"");
}
-
- $funcs[] = parseFunctionLike($className . '_' . $stmt->name->toString(), $stmt);
}
- return new ClassInfo($className, $funcs);
+
+ return empty($conds) ? null : implode(' && ', $conds);
}
/** @return FuncInfo[] */
@@ -226,32 +241,7 @@ function parseStubFile(string $fileName) {
$funcInfos = [];
$conds = [];
foreach ($stmts as $stmt) {
- 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\"");
- }
- }
-
- $cond = empty($conds) ? null : implode(' && ', $conds);
-
+ $cond = handlePreprocessorConditions($conds, $stmt);
if ($stmt instanceof Stmt\Nop) {
continue;
}
@@ -264,6 +254,7 @@ function parseStubFile(string $fileName) {
if ($stmt instanceof Stmt\ClassLike) {
$className = $stmt->name->toString();
foreach ($stmt->stmts as $classStmt) {
+ $cond = handlePreprocessorConditions($conds, $classStmt);
if ($classStmt instanceof Stmt\Nop) {
continue;
}