onezibo / py2flows

A control flow generator for Python that is is able to generate control flow graphs and flows.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

py2flows

A control flow generator for Python that is able to generate control flow graphs and corresponding flows. The motivation behind this project is to generate flows suitable for data flow analysis for Python. In my plan, an instance of a dynamic monotone framework is going to be implemented upon it.

PLEASE NOTE

I'm doing my master's thesis now. In order to save some time, now the latest py2flows code lies in dmf. You can find it in module dmf.flows.

After I finish my thesis, I will update this project.

Supported language versions

Python version

  • Python 3.7
  • [] Python 3.8(Untested)
  • [] Python 3.9(Untested)
  • [] Python 3.10(Untested)

Abstract Syntax Tree

Modules

  • ast.Module

Statements

  • ast.FunctionDef
  • ast.AsyncFunctionDef(Poor support for now)
  • ast.ClassDef
  • ast.Return
  • ast.Delete
  • ast.Assign
  • ast.AugAssign
  • ast.AnnAssign
  • ast.For
  • ast.AsyncFor(Poor support for now)
  • ast.While
  • ast.If
  • ast.With(Poor support for now)
  • ast.AsyncWith(Poor support for now)
  • ast.Raise(Poor support for now)
  • ast.Try(Relatively poor support for now)
  • ast.Assert
  • ast.Import
  • ast.ImportFrom
  • ast.Global
  • ast.Nonlocal
  • ast.Expr
  • ast.Pass
  • ast.Break
  • ast.Continue

Expressions

  • ast.BoolOp
  • ast.BinOp
  • ast.UnaryOp
  • ast.Lambda
  • ast.IfExp
  • ast.Dict
  • ast.Set
  • ast.ListComp
  • ast.SetComp
  • ast.DictComp
  • ast.GeneratorExp
  • ast.Await
  • ast.Yield
  • ast.YieldFrom
  • ast.Compare
  • ast.Call
  • ast.Num
  • ast.Str
  • ast.FormattedValue
  • ast.JoinedStr
  • ast.Bytes
  • ast.NameConstant
  • ast.Ellipsis
  • ast.Constant
  • ast.Attribute
  • ast.Subscript
  • ast.Starred
  • ast.Name
  • ast.List
  • ast.Tuple

Support for other statements and expressions will be added gradually.

Additional features

  • Removal of comments and docstrings
  • Decomposition of complex statements
  • Isolated entries and exits
  • [] Support for Modules and packages. Motivation: a project may contain multiple packages and modules. So we would like to maintain a data structure to represent this relationship. It would be helpful for static analysis.
  • Refactor exception handling
  • [] Count characteristics of each file. Motivation: Emm. It is related to my thesis topic. I wanna count the tendency of each source file. I bet this feature would be implemented in 2 weeks.
  • Prune temporal variables
  • Use two labels to denote call and return of each function call
  • Understandable CFGs generated
  • Use one return to denote all returns in functions

What can be better

  • [] The data structure representing classes. Motivation: As we know, classes consist of fields and methods. So it's better to have a good data structure to store and retrieve fields and methods elegantly. I think it also holds for functions(In modern languages, functions can be shadowed).

How to use it

Install

  1. Install all dependencies listed in requirements.txt
  2. Open a terminal and run python setup.py install
  3. If step 2 succeeds, an executable file py2flows will be available.
  4. py2flows --help

Example 1

# 12_listcomp.py
z = [[1, 2, 3], [4, 5, 6]]
a = [x for x in [y for y in z]]
b = [2 * x for y in z if len(y) > 1 for x in y if x > 2 if x < 4]

Example

Example 2

def test():
    while i <= n:
        sum = sum + i
        i = i + 1


print("no isolated entries and exits")

No isolated entries and exits

No isolated entries and exits

Isolated entries and exits

Isolated entries and exits

About

A control flow generator for Python that is is able to generate control flow graphs and flows.

License:Apache License 2.0


Languages

Language:Python 100.0%