Skip to content

Commit 3afe586

Browse files
Merge branch 'develop' into main
# Conflicts: # .gitignore
2 parents 1379cdb + 3158aa1 commit 3afe586

File tree

260 files changed

+65028
-1507
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

260 files changed

+65028
-1507
lines changed

.gitignore

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ parts/
2020
sdist/
2121
var/
2222
wheels/
23-
<<<<<<< HEAD
24-
=======
25-
pip-wheel-metadata/
26-
>>>>>>> 892da8f00d4ce0b5f981ca1329f6b076693c0ad7
2723
share/python-wheels/
2824
*.egg-info/
2925
.installed.cfg
@@ -53,10 +49,7 @@ coverage.xml
5349
*.py,cover
5450
.hypothesis/
5551
.pytest_cache/
56-
<<<<<<< HEAD
5752
cover/
58-
=======
59-
>>>>>>> 892da8f00d4ce0b5f981ca1329f6b076693c0ad7
6053

6154
# Translations
6255
*.mo
@@ -75,14 +68,8 @@ instance/
7568
# Scrapy stuff:
7669
.scrapy
7770

78-
# Sphinx documentation
79-
docs/_build/
80-
8171
# PyBuilder
82-
<<<<<<< HEAD
8372
.pybuilder/
84-
=======
85-
>>>>>>> 892da8f00d4ce0b5f981ca1329f6b076693c0ad7
8673
target/
8774

8875
# Jupyter Notebook
@@ -93,7 +80,6 @@ profile_default/
9380
ipython_config.py
9481

9582
# pyenv
96-
<<<<<<< HEAD
9783
# For a library or package, you might want to ignore these files since the code is
9884
# intended to run in multiple environments; otherwise, check them in:
9985
# .python-version
@@ -144,7 +130,6 @@ dmypy.json
144130

145131
# Pyre type checker
146132
.pyre/
147-
<<<<<<< HEAD
148133

149134
# pytype static type analyzer
150135
.pytype/
@@ -161,3 +146,8 @@ out/
161146
# Pycharm config dir
162147
.idea/
163148

149+
# Sphinx documentation
150+
sphinx/_build/
151+
152+
scratch.py
153+
out/

.nojekyll

Whitespace-only changes.

INSTALL

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pip install singledispatch

README.md

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,115 @@
33

44
[![License](https://img.shields.io/github/license/ComputationalReflection/cnerator)](LICENSE)
55
[![Latest release](https://img.shields.io/github/v/release/computationalreflection/cnerator?include_prereleases)](https://github.com/ComputationalReflection/cnerator/releases)
6+
[![Documentation](https://img.shields.io/static/v1?label=docs&message=Sphinx&color=blue)](https://computationalreflection.github.io/Cnerator/)
67
<img alt="Code size" src="https://img.shields.io/github/languages/code-size/computationalreflection/cnerator">
78
<img alt="Repo size" src="https://img.shields.io/github/repo-size/computationalreflection/cnerator">
89

910

11+
Cnerator is a C source code generation tool. It can be used to generate large amounts of
12+
[standard ANSI/ISO C source code](https://www.iso.org/standard/74528.html), ready to be compiled
13+
by any standard language implementation.
14+
Cnerator is highly customizable to generate all the syntactic constructs of the C language, necessary to build
15+
accurate predictive models with machine learning algorithms.
1016

11-
Cnerator is a C source code generation tool. Generated programs can be compiled with any standard ANSI C compiler.
12-
The user may define different parameters such as the number of function to be generated or the probabilities of all
13-
the different syntactic constructs (e.g., the average number of statements in a function, expression types,
14-
number and types of local variables, and the kind of syntactic constructs to be generated).
17+
## Functionalities
1518

16-
Generated programs fulfill the type rules of the C programming language, so they are compiled without errors.
17-
Using the different parameters, the user can utilize Cnerator to create a huge amount of synthetic C programs,
18-
necessary in common "Big Code" scenarios.
19+
These are the main functionalities provided by Cnerator:
1920

20-
## Command line options:
21+
1. _ANSI/ISO standard C_. All the source code generated by Cnerator follows the ISO/IEC 9899:2018 (C17)
22+
standard specification.
23+
24+
2. _Probabilistic randomness_. C language constructs are randomly generated, following different probability
25+
distributions specified by the user. For example, it is possible to describe the probability of each statement
26+
and expression construct, the number of statements in a function, and the types of their arguments and return values.
27+
To this aim, the user can specify fixed probabilities of each element, or use different probability distributions,
28+
such as normal, uniform and direct and inverse proportional.
29+
30+
3. _Compilable code_. The generated code strictly follows the syntax grammar, type system and semantic
31+
rules of the C programming language. In this way, the generated code has been checked to be compilable
32+
by any standard compiler implementation.
33+
34+
4. _Highly customizable_. Many features of the programs to be generated are customizable.
35+
Some examples include the types of each language construct, array dimensions and sizes, struct fields,
36+
maximum depth of expression and statement trees, number of function parameters and statements,
37+
global and local variables, structures of control flow statements and type promotions,
38+
among others –see the detailed [documentation](user-manual.md).
39+
40+
5. _Large amounts of code_. Cnerator is designed to allow generating large amounts of C source code.
41+
A parameter indicates the number of independent compilation units to be created for the output application,
42+
so that each unit could be treated as an independently compilable module. This feature, together with the
43+
probabilistic randomness, make Cnerator an ideal tool to create predictive models of source code, because
44+
the input programs used to train such models comprise abundant and varied code patterns.
45+
46+
## Usage
47+
48+
To run Cnerator, you need to install the `singledispatch` Python package first:
49+
50+
51+
``` text
52+
pip install singledispatch
53+
```
54+
55+
Then, you may just run Cnerator with no arguments to generate a random C program:
56+
57+
58+
``` text
59+
python cnerator.py
60+
```
61+
62+
There are plenty of options to customize Cnerator. To specify the probability of a particular language
63+
construct, you can use the `-p` or `--probs` option.
64+
The following command sets to 20% the probability of
65+
generating a function invocation when a new expression is required:
2166

2267
``` text
23-
usage: cnerator.py [-h] [-w PATH] [-o NAME] [-p AMOUNT] [-r RECURSION]
24-
[-vst VISITORS] [-v] [-d]
25-
26-
Generates a compilable C program
27-
28-
optional arguments:
29-
-h, --help show this help message and exit
30-
-w PATH, --working-dir PATH
31-
Working directory (default: out)
32-
-o NAME, --output NAME
33-
C output file name, without the .c extension (default:
34-
main)
35-
-p AMOUNT, --parts AMOUNT
36-
Split the program in different C files (default: 2)
37-
-r RECURSION, --recursion RECURSION
38-
Python recursion limit (default: 50000)
39-
-vst VISITORS, --visitors VISITORS
40-
Semicolon-separated list of visitors, in order (e.g.,
41-
visitors.func_to_proc;visitors.return_instrumentation)
42-
(default: )
43-
-v, --verbose Verbose messages (default: False)
44-
-d, --debug Generate debug info (call graph and struct structure)
45-
in .dot files (default: False)
68+
python cnerator.py -p "call_prob = {True: 0.2, False: 0.8}"
4669
```
4770

71+
If more sophisticated probabilities are required, you can specify them in a JSON file and pass it as
72+
a parameter (see the [documentation](user-manual.md#probability-specification-files) to know the JSON file format).
73+
The following line passes an example JSON file in the `json/probabilities` folder where
74+
different probability distributions are specified for some syntactic constructs:
75+
76+
``` text
77+
python cnerator.py -P json/probabilities/example_probs.json
78+
```
79+
80+
Cnerator also provides allows the user to control the number and characteristics of
81+
all the functions to be generated. A JSON file is used for that purpose
82+
(see the [documentation](user-manual.md#function-generation-files)).
83+
The following command makes Cnerator generate one function for each high-level return
84+
type in the C programming language:
85+
86+
87+
``` text
88+
python cnerator.py -f json/functions/1-function-each-type.json
89+
```
90+
91+
Sometimes, the user needs the output program to fulfill some requirements not guaranteed by the
92+
stochastic generation process.
93+
To this aim, Cnerator allows the specification of an ordered collection of Python
94+
post-process specification files (see the [documentation](user-manual.md#post-processing-specification-files)).
95+
These post-processing files can modify the generated code to satisfy those requirements.
96+
The following execution generates a random program and then executes two visitors:
97+
one to rename `func` functions to `proc` (and their invocations) when they return `void`;
98+
and another one to add a `__RETURN__` label before each return statement:
99+
100+
``` text
101+
python cnerator.py -V visitors.func_to_proc:visitors.return_instrumentation
102+
```
103+
104+
To see all the options, just run the `-h` or `--help` options.
105+
For more information, please check the [documentation](user-manual.md).
106+
Developer documentation is also provided [here](https://computationalreflection.github.io/Cnerator).
107+
108+
109+
## Documentation
110+
111+
* Check out the [user manual](user-manual.md) about how to use Cnerator.
112+
113+
* A [developer manual](https://computationalreflection.github.io/Cnerator) is also available
114+
if you want to modify the source code.
48115

49116
## License
50117

cnerator.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
"""
5+
This is the entry point to the Cnerator source code generation tool.
6+
The main function runs Cnerator.
7+
To see the different arguments provided, run Cnerator with either ``-h`` or ``--help`` command line arguments.
8+
"""
9+
410
from __future__ import print_function
511

612
import os
@@ -11,26 +17,32 @@
1117
from params.parameters import parse_args, get_modules_to_import, get_probs_to_override
1218
from params.writter import write_in_files
1319
from params import json_probs
14-
import cnerator
20+
import core
1521

1622

1723
def run(args):
24+
"""
25+
This function runs Cnerator, receiving the command line arguments.
26+
27+
:param args: The command line arguments returned by ``argparse.ArgumentParser.parse_args``.
28+
:return: None.
29+
"""
1830
# Set the recursion limit
1931
sys.setrecursionlimit(args.recursion)
2032

21-
# Get the probabilites from the command line arguments and modify the default ones
22-
cnerator.probs.set_probabilites(get_probs_to_override(args.probs))
33+
# Get the probabilities from the command line arguments and modify the default ones
34+
core.probs.set_probabilities(get_probs_to_override(args.probs))
2335
if args.probsfile:
24-
from cnerator import probs
25-
probabilites = json_probs.parse_probabilites_specification_json_file(args.probsfile)
26-
probs.set_probabilites(probabilites)
36+
from core import probs
37+
probabilities = json_probs.parse_probabilities_specification_json_file(args.probsfile)
38+
probs.set_probabilities(probabilities)
2739

2840
# Process the json file to create functions, or generates a program using the probabilities defined
2941
if args.functions: # if a json file was passed, it defines the functions to be generated
3042
dictionary = parameters.parse_function_specification_json_file(args.functions)
31-
program = cnerator.generators.generate_program_with_function_distribution(dictionary, args, remove_unwanted_functions=True)
43+
program = core.generators.generate_program_with_function_distribution(dictionary, args, remove_unwanted_functions=True)
3244
else: # otherwise, a random program is generated, considering the specified probabilities
33-
program = cnerator.generators.generate_program()
45+
program = core.generators.generate_program()
3446

3547
# Load all the visitor modules and run them, in the same order as specified by the user
3648
modules = get_modules_to_import(args.visitors)
@@ -54,6 +66,9 @@ def run(args):
5466

5567

5668
def main():
69+
"""Main function.
70+
To see the different arguments provided, run Cnerator with either ``-h`` or ``--help`` command line arguments.
71+
"""
5772
exit(run(parse_args()))
5873

5974

cnerator/__init__.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

cnerator/function_subs.py

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)