Skip to content

Commit 1cb0688

Browse files
committed
update readme's, more links mostly
1 parent 935a04d commit 1cb0688

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

pyt/analysis/README.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,33 @@ How does a definition reach?
3838
============================
3939

4040
After we know that a definition reaches a use that we are interested in,
41-
we make what are called `definition-use chains`_ figure out how the definition
41+
we make what use called `definition-use chains`_ to figure out how the definition
4242
reaches the use. This is necessary because there may be more than one path from
43-
the definition to the use.
43+
the definition to the use. Here is the code from `definition_chains.py`_:
4444

45+
.. code-block:: python
46+
47+
def build_def_use_chain(
48+
cfg_nodes,
49+
lattice
50+
):
51+
def_use = defaultdict(list)
52+
# For every node
53+
for node in cfg_nodes:
54+
# That's a definition
55+
if isinstance(node, AssignmentNode):
56+
# Get the uses
57+
for variable in node.right_hand_side_variables:
58+
# Loop through most of the nodes before it
59+
for earlier_node in get_constraint_nodes(node, lattice):
60+
# and add them to the 'uses list' of each earlier node, when applicable
61+
# 'earlier node' here being a simplification
62+
if variable in earlier_node.left_hand_side:
63+
def_use[earlier_node].append(node)
64+
return def_use
4565
4666
.. _definition-use chains: https://en.wikipedia.org/wiki/Use-define_chain
67+
.. _definition_chains.py: https://github.com/python-security/pyt/blob/re_organize_code/pyt/analysis/definition_chains.py#L16-L33
4768

4869

4970
Additional details

pyt/cfg/README.rst

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
make_cfg is what __main__.py calls, it takes the Abstract Syntax Tree, creates an ExprVisitor and returns a Control Flow Graph.
1+
`make_cfg`_ is what `__main__.py`_ calls, it takes the Abstract Syntax Tree, creates an ExprVisitor and returns a Control Flow Graph.
2+
3+
.. _make_cfg: https://github.com/python-security/pyt/blob/re_organize_code/pyt/cfg/make_cfg.py#L22-L38
4+
.. _\_\_main\_\_.py: https://github.com/python-security/pyt/blob/re_organize_code/pyt/__main__.py#L33-L106
25

36
stmt_visitor.py and expr_visitor.py mirror the `abstract grammar`_ of Python. Statements can contain expressions, but not the other way around. This is why ExprVisitor inherits from StmtVisitor, (which inherits from `ast.NodeVisitor`_ from the standard library.)
47

@@ -13,9 +16,11 @@ This is how ast.NodeVisitor works:
1316
return visitor(node)
1417
1518
16-
So as you'll see, there is a `visit\_` function for almost every AST node type. We keep track of all the nodes while we visit by adding them to self.nodes, connecting them via `ingoing` and `outgoing` node attributes.
19+
So as you'll see, there is a `visit\_` function for almost every AST node type. We keep track of all the nodes while we visit by adding them to self.nodes, connecting them via `\`ingoing\` and \`outgoing\` node attributes`_.
20+
21+
.. _\`ingoing\` and \`outgoing\` node attributes_: https://github.com/python-security/pyt/blob/re_organize_code/pyt/core/node_types.py#L27-L48
1722

18-
The two most illustrative functions are stmt_star_handler and expr_star_handler. expr_star_handler has not been merged to master so let's talk about stmt_star_handler.
23+
The two most illustrative functions are `stmt_star_handler`_ and expr_star_handler. expr_star_handler has not been merged to master so let's talk about `stmt_star_handler`_.
1924

2025

2126
Handling an if: statement
@@ -36,12 +41,14 @@ This is the relevant part of the `abstract grammar`_
3641
# Note: stmt* means any number of statements.
3742
3843
39-
Upon visiting an if: statement we will enter visit_If in stmt_visitor.py. Since we know that the test is just one expression, we can just call self.visit() on it. The body could be an infinite number of statements, so we use the stmt_star_handler function.
44+
Upon visiting an if: statement we will enter visit_If in stmt_visitor.py. Since we know that the test is just one expression, we can just call self.visit() on it. The body could be an infinite number of statements, so we use the `stmt_star_handler`_ function.
4045

41-
stmt_star_handler returns a namedtuple (ConnectStatements) with the first statement, last_statements and break_statements of all of the statements that were in the body of the node. stmt_star_handler takes care of connecting each statement in the body to the next one.
46+
`stmt_star_handler`_ returns a namedtuple (ConnectStatements) with the first statement, last_statements and break_statements of all of the statements that were in the body of the node. `stmt_star_handler`_ takes care of connecting each statement in the body to the next one.
4247

4348
We then connect the test node to the first node in the body (if some_condition -> x = 5) and return a namedtuple (ControlFlowNode) with the test, last_statements and break_statements.
4449

50+
.. _stmt_star_handler: https://github.com/python-security/pyt/blob/re_organize_code/pyt/cfg/stmt_visitor.py#L60-L121
51+
4552

4653
.. code-block:: python
4754

0 commit comments

Comments
 (0)