Visualize your source code as DOT flowcharts
Project description
Code2flow generates call graphs for dynamic programming language. Code2flow supports Python, JavaScript, Ruby, and PHP.
The basic algorithm is simple:
- Translate your source files into ASTs.
- Find all function definitions.
- Determine where those functions are called.
- Connect the dots.
Code2flow is useful for:
- Untangling spaghetti code.
- Identifying orphaned functions.
- Getting new developers up to speed.
Code2flow provides a pretty good estimate of your project's structure. No algorithm can generate a perfect call graph for a dynamic language – even less so if that language is duck-typed. See the known limitations in the section below.
(Below: Code2flow running against a subset of itself code2flow code2flow/engine.py code2flow/python.py --target-function=code2flow --downstream-depth=3
)
Installation
pip3 install code2flow
If you don't have it already, you will also need to install graphviz. Installation instructions can be found here.
Additionally, depending on the language you want to parse, you may need to install additional dependencies:
- JavaScript: Acorn
- Ruby: Parser
- PHP: PHP-Parser
- Python: No extra dependencies needed
Usage
To generate a DOT file, run something like:
code2flow mypythonfile.py
Or, for Javascript:
code2flow myjavascriptfile.js
You can specify multiple files or import directories:
code2flow project/directory/source_a.js project/directory/source_b.js
code2flow project/directory/*.js
code2flow project/directory --language js
To pull out a subset of the graph, try something like:
code2flow mypythonfile.py --target-function my_func --upstream-depth=1 --downstream-depth=1
There are a ton of command line options, to see them all, run:
code2flow --help
How code2flow works
Code2flow approximates the structure of projects in dynamic languages. It is not possible to generate a perfect callgraph for a dynamic language.
Detailed algorithm:
- Generate an AST of the source code
- Recursively separate groups and nodes. Groups are files, modules, or classes. More precisely, groups are namespaces where functions live. Nodes are the functions themselves.
- For all nodes, identify function calls in those nodes.
- For all nodes, identify in-scope variables. Attempt to connect those variables to specific nodes and groups. This is where there is some ambiguity in the algorithm because it is impossible to know the types of variables in dynamic languages. So, instead, heuristics must be used.
- For all calls in all nodes, attempt to find a match from the in-scope variables. This will be an edge.
- If a definitive match from in-scope variables cannot be found, attempt to find a single match from all other groups and nodes.
- Trim orphaned nodes and groups.
- Output results.
Why is it impossible to generate a perfect call graph?
Consider this toy example in Python
def func_factory(param):
if param < .5:
return func_a
else:
return func_b
func = func_factory(important_variable)
func()
We have no way of knowing whether func
will point to func_a
or func_b
until runtime. In practice, ambiguity like this is common and is present in most non-trivial applications.
Known limitations
Code2flow is internally powered by ASTs. Most limitations stem from a token not being named what code2flow expects it to be named.
- All functions without definitions are skipped. This most often happens when a file is not included.
- Functions with identical names in different namespaces are (loudly) skipped. E.g. If you have two classes with identically named methods, code2flow cannot distinguish between these and skips them.
- Imported functions from outside your project directory (including from standard libraries) which share names with your defined functions may not be handled correctly. Instead, when you call the imported function, code2flow will link to your local functions. For example, if you have a function
search()
and call,import searcher; searcher.search()
, code2flow may link (incorrectly) to your defined function. - Anonymous or generated functions are skipped. This includes lambdas and factories.
- If a function is renamed, either explicitly or by being passed around as a parameter, it will be skipped.
As an imported library
You can work with code2flow as an imported Python library in much the same way as you work with it from the CLI.
import code2flow
code2flow.code2flow(['path/to/filea', 'path/to/fileb'], 'path/to/outputfile')
The keyword arguments to code2flow.code2flow
are roughly the same as the CLI
parameters. To see all available parameters, refer to the code2flow function in engine.py.
How to contribute
- Open an issue: Code2flow is not perfect and there is a lot that can be improved. If you find a problem parsing your source that you can identify with a simplified example, please open an issue.
- Create a PR: Even better, if you have a fix for the issue you identified that passes unit tests, please open a PR.
- Add a language: While dense, each language implementation is between 250-400 lines of code including comments. If you want to implement another language, the existing implementations can be your guide.
Unit tests
Test coverage is 100%. To run:
pip install -r requirements_dev.txt
make test
License
Code2flow is licensed under the MIT license. Prior to the rewrite in April 2021, code2flow was licensed under LGPL. The last commit under that license was 24b2cb854c6a872ba6e17409fbddb6659bf64d4c. The April 2021 rewrite was substantial, so it's probably reasonable to treat code2flow as completely MIT-licensed.
Acknowledgements
- In mid 2021, Code2flow was rewritten, and two new languages were added. This was prompted and financially supported by the Sider Corporation.
- The code2flow pip name was graciously transferred to this project from Dheeraj Nair. He was using it for his own (unrelated) code2flow project.
- Many others have contributed through bug fixes, cleanups, and identifying issues. Thank you!!!
Unrelated projects
The name, "code2flow", has been used for several unrelated projects. Specifically, the domain, code2flow.com, has no association with this project. I've never heard anything from them and it doesn't appear like they use anything from here.
Feedback / Issues / Contact
If you have an issue using code2flow or a feature request, please post it in the issues tab. In general, I don't provide help over email. Answering a question publicly helps way more people. For everything else, please do email! scottmrogowski@gmail.com
Feature Requests
Email me. Usually, I'm spread thin across a lot of projects, so I will, unfortunately, turn down most requests. However, I am open to paid development for compelling features.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file code2flownew10-2.5.0.tar.gz
.
File metadata
- Download URL: code2flownew10-2.5.0.tar.gz
- Upload date:
- Size: 28.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.0 urllib3/1.26.7 tqdm/4.64.1 importlib-metadata/4.6.1 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c3295be2e5d609645548e8b8adf955b8ece33d4490f855d92b0a8fc41f9b9e9 |
|
MD5 | a951f13b2c6cc7c49ad8b76f1444f228 |
|
BLAKE2b-256 | 936964497a450f19a61df5bd56179ed2a5d8cdc2ca3ebffb54d30a1cc8b71ab9 |
File details
Details for the file code2flownew10-2.5.0-py3-none-any.whl
.
File metadata
- Download URL: code2flownew10-2.5.0-py3-none-any.whl
- Upload date:
- Size: 34.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.10.0 urllib3/1.26.7 tqdm/4.64.1 importlib-metadata/4.6.1 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1784e1b84290c62cc8d003183ef19d0b9a6d59acb308d19610dfc3c486d278ee |
|
MD5 | de6710c77a68867ab37bb89858788fa1 |
|
BLAKE2b-256 | 4bbbb736f46d6bc6b73fa5da145701db29708430e29a44261db2ed0156af9d41 |