Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1430302
GotoAnalyzer.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
GotoAnalyzer.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
Phan\Analysis
;
use
ast
;
use
ast\Node
;
/**
* Analyzes uses of goto
*/
class
GotoAnalyzer
{
/**
* Finds the label set for the scope of a node with parent node list $parent_node_list
* @param list<Node> $parent_node_list
* @return array<string,true>
*/
public
static
function
getLabelSet
(
array
$parent_node_list
):
array
{
// Find the AST_STMT_LIST that is the root of the function-like
$prev_node
=
null
;
for
(
$i
=
\count
(
$parent_node_list
)
-
1
;
$i
>=
0
;
$i
--)
{
$node
=
$parent_node_list
[
$i
];
if
(
\in_array
(
$node
->
kind
,
[
ast\AST_FUNC_DECL
,
ast\AST_CLOSURE
,
ast\AST_METHOD
],
true
))
{
break
;
}
$prev_node
=
$node
;
}
if
(!
$prev_node
)
{
return
[];
}
// $prev_node is the AST_STMT_LIST in the global scope or the nearest function-like scope.
// @phan-suppress-next-line PhanUndeclaredProperty deliberately adding this to a dynamic property to avoid recomputing it for large function bodies.
return
$prev_node
->
used_label_set
??
(
$prev_node
->
used_label_set
=
self
::
computeLabelSet
(
$prev_node
));
}
/**
* @return array<string,true> the set of labels that are used by "goto label" in this function-like scope or global scope.
*/
private
static
function
computeLabelSet
(
Node
$node
):
array
{
$result
=
[];
$nodes
=
[];
while
(
true
)
{
$kind
=
$node
->
kind
;
// fprintf(STDERR, "Processing node of kind %s\n", ast\get_kind_name($kind));
switch
(
$kind
)
{
case
ast\AST_FUNC_DECL
:
case
ast\AST_CLOSURE
:
case
ast\AST_METHOD
:
case
ast\AST_CLASS
:
break
;
case
ast\AST_GOTO
:
$result
[(
string
)
$node
->
children
[
'label'
]]
=
true
;
break
;
default
:
foreach
(
$node
->
children
as
$child_node
)
{
if
(
$child_node
instanceof
Node
)
{
$nodes
[]
=
$child_node
;
}
}
}
if
(
\count
(
$nodes
)
===
0
)
{
return
$result
;
}
$node
=
\array_pop
(
$nodes
);
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 18:36 (6 h, 16 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
00/5b/c2baf6f9717b3e7c7abcedbf550a
Default Alt Text
GotoAnalyzer.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment