code_analyzer/cparse.c

101 lines
2.8 KiB
C
Raw Normal View History

2021-12-13 10:44:21 +01:00
#include "vec.h"
#include <errno.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int find_funcs(const char *path)
{
FILE *fp;
regex_t fdre, fcre;
int ret;
char line[1024];
char boum[1024];
regmatch_t rm[2];
const char *re_func_decl = "^(\\w+(\\s+)?){2,}\\([^!@#$+%^]+?\\)\\s+";
const char *re_func_call = "([a-zA-Z_0-9]+)\\(.*\\)";
fp = fopen(path, "r");
if (fp == 0) {
fprintf(stderr, "Failed to open file %s (%d: %s)\n", path, errno, strerror(errno));
return EXIT_FAILURE;
}
ret = regcomp(&fdre, re_func_decl, REG_EXTENDED);
if (ret != 0) {
fprintf(stderr, "Failed to compile regex '%s'\n", re_func_decl);
return EXIT_FAILURE;
}
ret = regcomp(&fcre, re_func_call, REG_EXTENDED);
if (ret != 0) {
fprintf(stderr, "Failed to compile regex '%s'\n", re_func_call);
return EXIT_FAILURE;
}
int i = 0;
bool in = false;
graph_vec g = new_graph(10);
int index;
int i1, i2 = -1;
while ((fgets(line, 1024, fp)) != NULL) {
if (in && line[0] == '}') {
in = false;
}
if (in) {
if (regexec(&fcre, line, 2, rm, 0) == 0) {
// printf("Line: <<%.*s>>\n", (int)(rm[0].rm_eo - rm[0].rm_so), line + rm[0].rm_so);
sprintf(boum, "%.*s", (int)(rm[1].rm_eo - rm[1].rm_so), line + rm[1].rm_so);
// printf("%.*s\n", (int)(rm[1].rm_eo - rm[1].rm_so), line + rm[1].rm_so);
if ((index = is_in(g, boum))) {
if (g.node[i2]->nbs == g.node[i2]->cap)
g.node[i2]->succ = reallocarray(g.node[i2]->succ, (g.node[i2]->cap += 5), sizeof(nodept));
g.node[i2]->succ[i1] = g.node[index];
g.node[i2]->nbs++;
i1++;
} else {
insert_node(&g, i, boum, false);
if (g.node[i2]->nbs == g.node[i2]->cap)
g.node[i2]->succ = reallocarray(g.node[i2]->succ, (g.node[i2]->cap += 5), sizeof(nodept));
g.node[i2]->succ[i1] = g.node[i];
g.node[i2]->nbs++;
i1++;
i++;
}
}
} else if (regexec(&fdre, line, 2, rm, 0) == 0) {
sprintf(boum, "%.*s", (int)(rm[1].rm_eo - rm[1].rm_so), line + rm[1].rm_so);
// printf("%.*s\n", (int)(rm[1].rm_eo - rm[1].rm_so), line + rm[1].rm_so);
insert_node(&g, i, boum, true);
i++;
i2++;
in = true;
i1 = 0;
}
}
// for (int j = 0; j < i; j++) {
// printf("NODE %d: %s\n", j, g.node[j]->func);
// for (int n = 0; n < g.node[j]->nbs; n++) {
// printf("SUCC %d: %s\n", j, g.node[j]->succ[n]->func);
// }
// }
print_node(g.node[0]);
regfree(&fdre);
regfree(&fcre);
fclose(fp);
delete_graph(&g);
return 0;
}
int main(int argc, char *argv[])
{
find_funcs("./memline.c");
// graph_vec g = new_graph(3);
// insert_node(&g, new_node(0), "main");
// insert_node(&g, new_node(1), "main1");
// insert_node(&g, new_node(2), "main2");
// printf("%d %d %d\n", g.node[0]->num,g.node[1]->num,g.node[2]->num);
// printf("%s %s %s\n", g.func[0],g.func[1],g.func[2]);
// delete_graph(&g);
return 0;
}