|
3 | 3 |
|
4 | 4 | [](LICENSE) |
5 | 5 | [](https://github.com/ComputationalReflection/cnerator/releases) |
| 6 | +[](https://computationalreflection.github.io/Cnerator/) |
6 | 7 | <img alt="Code size" src="https://img.shields.io/github/languages/code-size/computationalreflection/cnerator"> |
7 | 8 | <img alt="Repo size" src="https://img.shields.io/github/repo-size/computationalreflection/cnerator"> |
8 | 9 |
|
9 | 10 |
|
| 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. |
10 | 16 |
|
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 |
15 | 18 |
|
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: |
19 | 20 |
|
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: |
21 | 66 |
|
22 | 67 | ``` 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}" |
46 | 69 | ``` |
47 | 70 |
|
| 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. |
48 | 115 |
|
49 | 116 | ## License |
50 | 117 |
|
|
0 commit comments