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

made changes to parser.py for if else conditionals #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 25 additions & 20 deletions UserCode.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@

int fibonacci(int n) {

if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}

int main() {
int random = 5;
int num = 10;
int *n = (int *)malloc(sizeof(int));
int *x = (int *)malloc(sizeof(int));

*x = random;
*n = num;

printf("Fibonacci sequence up to %d:\n", *n);
for (int i = 0; i < *n; i++) {
printf("%d\n", fibonacci(i));
printf("----\n");
}
int random = 5;
int num = 10;
int *n = (int *)malloc(sizeof(int));
int *x = (int *)malloc(sizeof(int));

*x = random;
*n = num;

printf("Fibonacci sequence up to %d:\n", *n);
for (int i = 0; i < *n; i++) {
if (i % 2 == 0) {
int *leak1 = (int *)malloc(sizeof(int));
*leak1 = i;
} else {
int *leak2 = (int *)malloc(sizeof(int));
*leak2 = i * 2;
}
printf("%d\n", fibonacci(i));
printf("----\n");
}

printf("\n");
return 0;
printf("\n");
return 0;
}
42 changes: 27 additions & 15 deletions parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,58 @@

deallocations = []
references = {}
conditional_allocs = {}


def traverse_ast(node, start_line=None,end_line = None):
def traverse_ast(node, start_line=None, end_line=None, inside_conditional=False):
if start_line is None:
start_line = node.location.line

if node.kind == CursorKind.FOR_STMT or node.kind == CursorKind.WHILE_STMT:
start_line = node.location.line
end_line = node.extent.end.line

if node.kind == CursorKind.IF_STMT:
inside_conditional = True
start_line = node.location.line
end_line = node.extent.end.line

if node.kind == CursorKind.DECL_REF_EXPR:
if node.referenced and node.referenced.kind == CursorKind.VAR_DECL:
var_name = node.spelling
references[var_name] = node.location.line
if not inside_conditional:
references[var_name] = node.location.line
else:
conditional_allocs[var_name] = node.location.line

for child in node.get_children():
traverse_ast(child, start_line, end_line)
traverse_ast(child, start_line, end_line, inside_conditional)

if node.kind == CursorKind.FOR_STMT or node.kind == CursorKind.WHILE_STMT:
if (
node.kind == CursorKind.FOR_STMT
or node.kind == CursorKind.WHILE_STMT
or node.kind == CursorKind.IF_STMT
):
for iterator in references.keys():
line_number = references[iterator]
if start_line <= line_number <= end_line:
references[iterator] = end_line


def check_alloc_term(filename, line_number, var_name):
with open(filename, 'r') as f:
with open(filename, "r") as f:
lines = f.readlines()
for i, line in enumerate(lines):
if ("alloc" in line):
if (("*" + var_name) in line):
if "alloc" in line:
if ("*" + var_name) in line:
return True
return False


def main(input_file, json_file):
index = Index.create()
tu = index.parse(input_file, args=['-std=c11'])
print('Translation unit:', tu.spelling)
tu = index.parse(input_file, args=["-std=c11"])
print("Translation unit:", tu.spelling)

for node in tu.cursor.get_children():
if node.kind == CursorKind.FUNCTION_DECL:
Expand All @@ -51,15 +64,14 @@ def main(input_file, json_file):
references[iterator] = int(references[iterator])
line_number = references[iterator]
if check_alloc_term(input_file, line_number, iterator):
deallocations.append({"line_number": line_number, "variable_name": iterator})
deallocations.append(
{"line_number": line_number, "variable_name": iterator}
)

output = {
"deallocations": deallocations
}
#print(deallocations)
output = {"deallocations": deallocations}

if deallocations:
with open(json_file, 'w') as f:
with open(json_file, "w") as f:
json.dump(output, f, indent=4)
print("Data written to references.json")
else:
Expand Down