Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup structure #1

Open
wants to merge 152 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
152 commits
Select commit Hold shift + click to select a range
aecb7f8
Store RawCLang and Clang for history purposes
PaulN2112CGE Oct 17, 2024
faf8290
Store dumps before removal
PaulN2112CGE Oct 17, 2024
947ac4f
Initial very draft version
PaulN2112CGE Oct 18, 2024
33d69a7
add init
PaulN2112CGE Oct 18, 2024
8fbc4cc
Merge branch 'setup-structure' of github.com:TNO/Renaissance-Experiments
PaulN2112CGE Oct 18, 2024
ee483e3
Add installation procedure
PaulN2112CGE Oct 18, 2024
50abd76
Add a main readme
PaulN2112CGE Oct 18, 2024
cc2091c
Add first simple patterns
PaulN2112CGE Oct 22, 2024
d9dac60
Keep first version of match_finder
PaulN2112CGE Oct 24, 2024
b1d3ddd
Consolidate current efforts. Far from finished yet.
PaulN2112CGE Oct 24, 2024
fe3157c
Merge branch 'setup-structure' of github.com:TNO/Renaissance-Experime…
PaulN2112CGE Oct 24, 2024
d86edb8
Add is_statement
PaulN2112CGE Oct 29, 2024
a161869
move test_utils to it's own file
PaulN2112CGE Oct 29, 2024
823fac2
Document match finder
PaulN2112CGE Oct 29, 2024
69dd6c3
add c_cpp utils and factories
PaulN2112CGE Oct 29, 2024
35cadf9
add tests for finder and c_pattern_factory
PaulN2112CGE Oct 29, 2024
d9e6825
use is_unexposed() method
PaulN2112CGE Oct 29, 2024
24be2a8
Add Unary operator behavior
PaulN2112CGE Oct 29, 2024
9a27653
Remove hardwired dependencies to clang
PaulN2112CGE Oct 29, 2024
028ce29
Reorganize
PaulN2112CGE Oct 29, 2024
85daa3c
Do not return name for call expressions
PaulN2112CGE Oct 31, 2024
85c8be4
Add clang json
PaulN2112CGE Oct 31, 2024
a4b2e78
Support both clang and clang json
PaulN2112CGE Oct 31, 2024
973fc3f
Correctly handle duplicate keys
PaulN2112CGE Oct 31, 2024
133974e
Generalize for clang json
PaulN2112CGE Oct 31, 2024
79aa13c
Generalize and add unary operators
PaulN2112CGE Oct 31, 2024
98d6e3c
Stored unfinished gcc attempt for possible later use
PaulN2112CGE Nov 5, 2024
90593d0
Delete gcc attempt
PaulN2112CGE Nov 5, 2024
2095aad
Add rewriter and stream
PaulN2112CGE Nov 8, 2024
47e4aef
set scope for clang and clang json
PaulN2112CGE Nov 8, 2024
2f66d56
Add rewriter
PaulN2112CGE Nov 8, 2024
b3c1762
Add test for rewriter
PaulN2112CGE Nov 8, 2024
a5b6a8f
Conform to python conventions
PaulN2112CGE Nov 8, 2024
4538f2b
Add test for AstRewriter
PaulN2112CGE Nov 8, 2024
c239c12
Add examples
PaulN2112CGE Nov 8, 2024
9a985df
Add a simple cpp class for test
PaulN2112CGE Nov 8, 2024
43bea3b
Update readme with todo's
PaulN2112CGE Nov 8, 2024
9fc6361
Cleanup unused files
PaulN2112CGE Nov 8, 2024
5e00b71
make sure that ast_node has no public abstract methods
PaulN2112CGE Nov 12, 2024
51a96c1
intergration tests
PaulN2112CGE Nov 12, 2024
957c740
made compilable
PaulN2112CGE Nov 12, 2024
ac459c8
add coverage
PaulN2112CGE Nov 12, 2024
d9d74f2
Add references and _ protected overrides
PaulN2112CGE Nov 12, 2024
8b41bde
matches_kind public
PaulN2112CGE Nov 12, 2024
711cef0
add name and optionally include properties
PaulN2112CGE Nov 12, 2024
b6dffeb
deal with empty lines in remove
PaulN2112CGE Nov 12, 2024
7d1ef80
use atu for resolving types
PaulN2112CGE Nov 12, 2024
345ec15
add TestUseAtuToCreatePatterns
PaulN2112CGE Nov 12, 2024
5353f3f
add an example to remove unused variables
PaulN2112CGE Nov 12, 2024
bdd8d36
test use factory to create patterns
PaulN2112CGE Nov 12, 2024
a0666ff
Don't use List use list
PaulN2112CGE Nov 13, 2024
c294f21
store kind of reference add testcase for call,baseclass,typeref refer…
PaulN2112CGE Nov 13, 2024
9c47ba2
use Sequence wherever possible
PaulN2112CGE Nov 14, 2024
127784b
Improve types
PaulN2112CGE Nov 14, 2024
2a4037f
add Stream tests
PaulN2112CGE Nov 14, 2024
01c4e3e
use ASTNode iso 'ASTNode'
PaulN2112CGE Nov 14, 2024
5d9697c
Clarify the reason for public methods not abstract
PaulN2112CGE Nov 14, 2024
4e5373e
Remove print
PaulN2112CGE Nov 14, 2024
994c9c2
Better names
PaulN2112CGE Nov 15, 2024
8fff2d5
Generate with preprocessing data, add macro_expansion to get_properties
PaulN2112CGE Nov 15, 2024
e630b5a
add macro_expansion to properties
PaulN2112CGE Nov 15, 2024
0269268
Use Sequence iso list
PaulN2112CGE Nov 15, 2024
4a53f2f
Skip Macro and inclusion directive to determine offset
PaulN2112CGE Nov 15, 2024
d3672a7
get_names return Sequence
PaulN2112CGE Nov 15, 2024
8d19d00
Add TestUseAtuToCreatePattern
PaulN2112CGE Nov 15, 2024
99c164a
add navigation methods, add stripped get_text() and get_extended_off…
PaulN2112CGE Nov 19, 2024
e49849b
include ';' for statements (to ease refactoring)
PaulN2112CGE Nov 19, 2024
22adf9f
use new get_text()
PaulN2112CGE Nov 19, 2024
d540add
include function declarations
PaulN2112CGE Nov 19, 2024
6f595ac
use Sequence iso list
PaulN2112CGE Nov 19, 2024
b14d1d9
use new get_text
PaulN2112CGE Nov 19, 2024
64e3363
log if only properties do not match. used new get_text
PaulN2112CGE Nov 19, 2024
961e3d5
introduce TextUtils
PaulN2112CGE Nov 19, 2024
57cf314
introduces TextUtils
PaulN2112CGE Nov 19, 2024
5c9e301
Allow for next compositions
PaulN2112CGE Nov 19, 2024
c1a7e4e
add example for nested compositions, also using atu in pattern factory
PaulN2112CGE Nov 19, 2024
553773a
peek agnostic of result
PaulN2112CGE Nov 20, 2024
1896b2b
use root for reference determination
PaulN2112CGE Nov 20, 2024
45566d1
reuse pattern
PaulN2112CGE Nov 20, 2024
2c0f837
add has_changed
PaulN2112CGE Nov 20, 2024
29165c5
create an umbrella class for easy refactoring
PaulN2112CGE Nov 20, 2024
815dbca
publish ASTRefactor
PaulN2112CGE Nov 20, 2024
e9ea8cf
add an example for a refactor method
PaulN2112CGE Nov 20, 2024
60bb5ff
Showcase remove unused variable as a refactor action
PaulN2112CGE Nov 20, 2024
ebbc1d2
reuse AstNodeType
PaulN2112CGE Nov 20, 2024
bb8916b
Parse compilation database and pass args and dir to loaders
PaulN2112CGE Nov 20, 2024
521a041
add working_dir
PaulN2112CGE Nov 20, 2024
7f3a285
publish CompilationDatabase
PaulN2112CGE Nov 20, 2024
6e12b31
Add an example compilation database
PaulN2112CGE Nov 20, 2024
9bec427
Example for walking compilation database
PaulN2112CGE Nov 20, 2024
bab3395
Check comment of preceding node as starting point
PaulN2112CGE Nov 21, 2024
112a1d6
interface and encoding changes
PaulN2112CGE Nov 21, 2024
d4777c2
add get_ancestor and encoding
PaulN2112CGE Nov 21, 2024
7715fc5
add batch processing
PaulN2112CGE Nov 21, 2024
6991670
avoid write text to disk
PaulN2112CGE Nov 21, 2024
84d76a9
use system encoding
PaulN2112CGE Nov 21, 2024
1cc8164
change test to correctly point predecessor comment
PaulN2112CGE Nov 21, 2024
5064009
no return value
PaulN2112CGE Nov 21, 2024
b4489e1
add a batch process example
PaulN2112CGE Nov 21, 2024
a99f828
if include_whitespace==False leave the handling to the inserter
PaulN2112CGE Nov 25, 2024
b53647c
add HasFinalAction
PaulN2112CGE Nov 25, 2024
e781c3b
add parallel processing and remove HasFinalAction ifo recipes
PaulN2112CGE Nov 26, 2024
665cccf
add repeat_step and remove user_objects ifo recipe
PaulN2112CGE Nov 26, 2024
27ef1dc
publish AST_FACTORY_AND ATU and Action
PaulN2112CGE Nov 26, 2024
79f2be6
add recipe_ast_processor to handle recipes
PaulN2112CGE Nov 26, 2024
8d20f84
show_case recipe handling in examples
PaulN2112CGE Nov 26, 2024
683de12
Remove user object
PaulN2112CGE Nov 28, 2024
abfa015
Map action on peek
PaulN2112CGE Dec 2, 2024
0336a14
Add store_node
PaulN2112CGE Dec 2, 2024
f41eb45
Enable concurrency
PaulN2112CGE Dec 2, 2024
838e853
Add to clipboard
PaulN2112CGE Dec 2, 2024
89c09aa
Add matches_kind, get_frozen_properties
PaulN2112CGE Dec 2, 2024
f48186a
Improve usages, add Constrained Pattern
PaulN2112CGE Dec 2, 2024
2e3c8ad
Only check ';' if is_mutlip_placeholder
PaulN2112CGE Dec 2, 2024
1b5c710
strip surrounding whitespace
PaulN2112CGE Dec 2, 2024
786c6a7
publish ConstrainedPattern and CPPUtils
PaulN2112CGE Dec 2, 2024
3a88a0f
fullmatch ignore case
PaulN2112CGE Dec 2, 2024
7ed17a0
Improve useability after test
PaulN2112CGE Dec 2, 2024
260965d
Add CPPUtils
PaulN2112CGE Dec 2, 2024
8c8b52d
Move ComposeReplacement to test_ast_rewriter
PaulN2112CGE Dec 2, 2024
6a66619
check on references for using
PaulN2112CGE Dec 2, 2024
a7b8e88
add some utilities and ConstrainedPattern
PaulN2112CGE Dec 2, 2024
168c7b8
Insert a Type Ref node for built in types, add matches_kind
PaulN2112CGE Dec 2, 2024
98258ea
Remove magic behavior from ast_rewriter
PaulN2112CGE Dec 2, 2024
13c8f76
Add Testcase for nested refactorings
PaulN2112CGE Dec 2, 2024
2a11381
Remove user_object
PaulN2112CGE Dec 2, 2024
7f7d7d2
Remove print statements
PaulN2112CGE Dec 2, 2024
85d0e84
Add DeclLoc node
PaulN2112CGE Dec 2, 2024
d148b98
do not add inserted node to references
PaulN2112CGE Dec 2, 2024
4965595
remove print
PaulN2112CGE Dec 2, 2024
108d9d0
remove exception for CallExpr, improve reference resolving
PaulN2112CGE Dec 10, 2024
367c1b5
Fix compile errors
PaulN2112CGE Dec 10, 2024
3ea04a1
create a more complex recipe example
PaulN2112CGE Dec 10, 2024
d371da4
Split patterns
PaulN2112CGE Dec 10, 2024
ca59af4
Fix compiler errors, repeat until finished
PaulN2112CGE Dec 10, 2024
c783cab
Remove unused in_memory
PaulN2112CGE Dec 10, 2024
820c750
or_else may return anything
PaulN2112CGE Dec 10, 2024
d9d89ef
improve reference handling, store results in temp file
PaulN2112CGE Dec 10, 2024
a4d68fb
publish Recipe handling
PaulN2112CGE Dec 10, 2024
1860216
Improve kind match
PaulN2112CGE Dec 10, 2024
a3315f9
Support nested pattern matches
PaulN2112CGE Dec 10, 2024
bf84aae
Common refactor action (experimental)
PaulN2112CGE Dec 10, 2024
0314671
Fix offset handling, support Sequence of patterns
PaulN2112CGE Dec 10, 2024
e24a370
better guessing, add constructor_call
PaulN2112CGE Dec 10, 2024
a596601
better handling of *
PaulN2112CGE Dec 10, 2024
0e13e75
REmove {?i) add decl/def refs
PaulN2112CGE Dec 10, 2024
0090230
Fix compilation errors
PaulN2112CGE Dec 10, 2024
76b3baa
Change criteria for adding a TypeRef
PaulN2112CGE Dec 12, 2024
032a9f5
test more examples
PaulN2112CGE Dec 12, 2024
dad06e6
Small-fixes-in-clang_json_ast_node.py
PaulN2112CGE Dec 20, 2024
8c537ea
Filter-out-source-filename-when-present-as-command
PaulN2112CGE Dec 20, 2024
43f16ce
VERBOSE = False
PaulN2112CGE Dec 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
**/.venv
**/.modules
**/*.pyc
**/__pycache__
**/*.log
**/*.swp
**/*.swo
**/*.sqlite3
**/*.db
**/*.db-journal
**/*.pyo
**/bin
**/*.exe
**/*.dll
**/.*.so
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Renaissance Experiments

This project is experimental in nature and aims to explore various concepts and techniques to apply renaissance pattern matching in a generic way using multiple abract syntax trees.

The code for the experiments is located in the [python](./python) folder.
18 changes: 18 additions & 0 deletions c/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Most usefull commands:

## gcc
gcc -fdump-tree-all-raw-lineno -fdump-rtl-all-raw-lineno -o main.exe main.c


## clang

### ast dump

`clang -Xclang -ast-dump -fsyntax-only main.c > ast-dump.ast`
or
`clang -Xclang -ast-dump -fsyntax-only main.c > ast-dump.ast`
### preprocessing dump

`pp-trace main.c > pptrace.ast`

contains all preprocessing directives and all usages.
16 changes: 16 additions & 0 deletions c/src/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"directory": "Z:\\testproject\\c\\src",
"file": "test.cpp",
"output": "C:\\Users\\PNELIS~1\\AppData\\Local\\Temp\\1\\test-9e2a00.o",
"arguments": [
"C:\\Users\\pnelissen\\scoop\\apps\\llvm\\current\\bin\\clang++.exe",
"-xc++",
"test.cpp",
"-o",
"C:\\Users\\PNELIS~1\\AppData\\Local\\Temp\\1\\test-9e2a00.o",
"--driver-mode=g++",
"--target=x86_64-pc-windows-msvc19.39.33521"
]
}
]
19 changes: 19 additions & 0 deletions c/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>

static int static_int = 2;

#define A_DEFINE (4 + static_int)
#define B_DEFINE (A_DEFINE + static_int)

#define FC_MACRO(arg)\
do{\
arg += A_DEFINE;\
} while(0)

int main() {
int qwerty = 3 + A_DEFINE;
FC_MACRO(qwerty);
printf("QWERTY %d", qwerty+static_int);
FC_MACRO(qwerty);
return 0;
}
58 changes: 58 additions & 0 deletions c/src/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//hËllo utf-8 2 byte character
static int static_int = 2;

#define A_DEFINE (4 + static_int)
#define B_DEFINE (A_DEFINE + static_int)

#define FC_MACRO(arg)\
do{\
arg += A_DEFINE;\
} while(0)

void printf(char*);
void printf(const char*, const char*, int);
class A {
public:
A() {
printf("A constructor\n");
}
~A() {
printf("A destructor\n");
}
protected:
int a;
virtual void testA() {
printf("A test\n");
}
};

class B: public A {
public:
B() {
printf("B constructor\n");
}
~B() {
printf("B destructor\n");
}
public:
int b;
virtual int testB(int x, const char *y) {
this->testA();
printf("B *s test %d\n", y+A_DEFINE, x);
return x;
}
void testA() {
A::testA();
}
};

static void test() {
static A a;
B b;
b.testB(1, "test");
b.testA();
}
int main() {
test ();
return 0;
}
2 changes: 2 additions & 0 deletions python/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#PATH=.venv\\Lib\\site-packages\\clang\\native;%PATH%
PYTHONPATH=${workspaceFolder}/src
24 changes: 24 additions & 0 deletions python/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"test*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
"python.testing.pytestArgs": [
"test"
],
"python.envFile": "${workspaceFolder}/.env",
"terminal.integrated.env.linux": {
"PATH": ".venv/Lib/site-packages/clang/native:${env:PATH}"
},
"terminal.integrated.env.osx": {
"PATH": ".venv/Lib/site-packages/clang/native:${env:PATH}"
},
"terminal.integrated.env.windows": {
"Path": ".venv\\Lib\\site-packages\\clang\\native;${env:Path}"
}
}
53 changes: 53 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Description
This project is a generic approach to refactor code bases with a generic AST structure.
It uses `TNO Renaissance` pattern matching.
Currently clang native and clang python bindings are supported.

# How to add a different binding
You'll need to implement a concrete class for syntax_tree.ASTNode.
Follow the implementations of `ClangASTNode` and `ClangJsonASTNode` as an example.
If the concrete AST has a different language then also a `PatternFactory` must be added. See `CPatternFactory` for inspiration.

## Installation Procedure
To install the necessary dependencies, follow these steps:

1. **Run the Installation Script**
- Navigate to the project directory.
- Execute the `install.bat` script by double-clicking it or running the following command in the terminal:
```sh
./install.bat
```

## Configuration and Verification

1. **Configure the Environment**
- Open Visual Studio Code (VSCode).
- Ensure that the Python extension is installed.
- Open the project folder in VSCode.
- alternatively in shell goto <root>/python folder and
```sh
code .
```

2. **Verify the Installation**
- Open the integrated terminal in VSCode.
- Run the following command to execute the tests:
```sh
python -m unittest discover
```
- Check the output to ensure all tests pass successfully.

By following these steps, you will have installed and verified the setup for the project.


## TODO

An incomplete list of todo's:

* The get_properties methods of both `ClangASTNode` and `ClangJsonASTNode` are not complete yet. This might cause mismatches in the `Match_Finder`
* C++ constructs have not been tested yet
* An example of how to use includes in a `Pattern` must be added
* Tests need to be added for macro handling
* The methods `get_references` and `referred_by` must be added to `ASTNode` and implemented in the concrete classes
* Test cases for multiple match patterns need to be added. Currently, there is only one working case in the examples
* Comments in Clang appear incorrectly in the `ASTShower`. This seems to be a Clang issue, which is surprising
149 changes: 149 additions & 0 deletions python/examples/batch_process_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#use clang to load and walk a compilation database

from dataclasses import dataclass
from typing import Callable
from syntax_tree.recipe_ast_processor import RecipeASTProcessor, after_step, recipe_step, final_action
from typing_extensions import Iterable, override
from impl import ClangASTNode, ClangJsonASTNode
from refactoring import CleanupRefactoring
from syntax_tree import ASTProcessor, ASTNode, ASTNodeType, TextUtils, ASTFactory, BatchASTProcessor

example_1 = TextUtils.strip_indent("""
void x(int a) {}
void x1(int a) {}
void x2(int a) {}

void f1(int a){
int unused = 0;
int unused2 = 0; //must be removed
if (a==1) {
int unused = 0;
int unused2 = 0; //should be kept
int c = unused2;
x1(c);
}
}
""")

example_2 = TextUtils.strip_indent("""
void x(int a) {}
void x1(int a) {}
void x2(int a) {}
void f2(int a){
int unused = 0;
if (a==1) {
int unused = 0;
int another_unused = 0;
int used2 = 0; //should be kept
int c = used2;
x2(c);
}
}
""")

# generate a simple code base provider in real life use a compilation database
def simple_codebase_provider() -> Iterable[tuple[ASTFactory[ASTNodeType], ASTNodeType]]:
for impl_type in [ClangASTNode, ClangJsonASTNode]:
factory = ASTFactory(impl_type)
atu1 = factory.create_from_text(example_1, impl_type.__name__+'1.c')
yield factory, atu1
atu2 = factory.create_from_text(example_2, impl_type.__name__+'2.c')
yield factory, atu2

def print_results(title, batch_processor):
print(title +':')
for file, code in batch_processor.in_memory_files.items():
print(TextUtils.shift_right(file, 4)+'\n')
print(TextUtils.shift_right(code, 8)+'\n')


def batch_remove_unused_variable_once_example():
"""
This function demonstrates a batch processing example using different AST node implementations.
It iterates over a list of AST node implementations (`ClangASTNode` and `ClangJsonASTNode`),
and for each implementation, it generates a codebase provider that yields tuples of
`ASTFactory` and `ASTNode` created from example source texts (`example_1` and `example_2`).
The function then creates a `BatchASTProcessor` with in-memory storage enabled and processes
the codebase using the `CleanupRefactoring.remove_unused_variables` refactoring operation.
Finally, it prints the rewritten code stored in memory.
"""
#generate a batch processor for testing purposes we store into memory
batch_processor = BatchASTProcessor(in_memory=True)
batch_processor.once(simple_codebase_provider, CleanupRefactoring.remove_unused_variables)
#print the rewritten code normally you would write to a file
print_results('example batch remove unused variable once', batch_processor)


def batch_repeat_example():
"""
Demonstrates the use of a batch processor to perform multiple refactoring operations on a codebase.
This example creates an in-memory batch processor and applies two refactoring operations:
1. CleanupRefactoring.remove_unused_variables: Removes unused variables from the codebase.
2. remove_function: Removes all function calls from the codebase.
The results of the refactoring operations are printed to the console.

Repeat is in action here:
the first time the codebase is processed, the unused variables are removed.
and the function calls are removed.
the second time the codebase is processed, the new unused variables are removed again.
Note:
In a real-world scenario, the rewritten code would typically be written to a file instead of being printed.
"""
#generate a batch processor for testing purposes we store into memory
batch_processor = BatchASTProcessor(in_memory=True)
#remove a function to create more unused variables
def remove_function(ast_processor: ASTProcessor[ASTNodeType]):
ast_processor.find_kind('(?i)Call_?Expr').\
for_each(lambda node: ast_processor.insert_before( '// ', node, False, False ))

# batch_processor.repeat(simple_codebase_provider, [remove_function])
batch_processor.repeat(simple_codebase_provider, [CleanupRefactoring.remove_unused_variables, remove_function])
#print the rewritten code normally you would write to a file
print_results('example batch repeat', batch_processor)

@dataclass
class Call:
callee: str
calls: str

class AnalysisRecipe:
def __init__(self):
self._calls = []

@recipe_step(order=0)
def store_function_call(self, ast_processor: ASTProcessor[ASTNodeType]) -> Callable[[], None]|None:
# find all function calls and store them, this routing is invoked in parallel!
calls = []
ast_processor.find_kind('(?i)Call_?Expr').\
for_each(lambda node: AnalysisRecipe._add_function_call(node, calls))
# the resulting lambda is invoked single threaded
# this kind of mechanism is mainly used to store results from multiple processors
# for refactoring operations this is not needed as a refactoring operation is single threaded
if calls:
return lambda: self._calls.extend(calls)

@after_step('store_function_call')
def just_show_the_method(self):
print('called after store_function_call')

@final_action()
def final_action(self):
print('Calls:')
for call in self._calls:
print(' '+call.callee + ' -- calls --> ' + call.calls)
@staticmethod
def _add_function_call(call: ASTNode, calls: list[Call]):
callee = call.get_ancestor('(?i)Function_?Decl')
if callee:
calls.append(Call(callee.get_name(), call.get_children()[0].get_name()))

def batch_recipe_example():
print('example batch analysis using recipe:\n')
recipeAstProcessor = RecipeASTProcessor(AnalysisRecipe(), simple_codebase_provider, r'.*', in_memory=True)
recipeAstProcessor.run()

if __name__ == "__main__":
# a list of example to show batch processing of a code base
batch_remove_unused_variable_once_example()
batch_repeat_example()
batch_recipe_example()
Loading