parcours de dossier et creation de graphe vec de succ

This commit is contained in:
fiplox 2021-12-19 23:47:07 +01:00
parent 8d9e2416f2
commit 8a22d221dc
9 changed files with 29526 additions and 57 deletions

59
Makefile Normal file
View File

@ -0,0 +1,59 @@
CC = clang
CFLAGS = -Wall -Wextra -Werror -O3
# GCCVERSIONGTE10 := $(shell expr `gcc -dumpversion | cut -f1 -d.` \>= 10)
#
# ifeq "$(GCCVERSIONGTE10)" "1"
# CFLAGS += -fanalyzer
# endif
NAME = code-analyzer
AUTHOR = Volodymyr_Patuta
VERSION = 0.1
SRCDIR = ./src
SRCS = $(shell find $(SRCDIR) -name '*.c')
HEADERS = $(shell find $(SRCDIR) -name '*.h')
OBJS = $(SRCS:.c=.o)
RM = rm -f
TAR = tar
MKDIR = mkdir
CHMOD = chmod
CP = rsync -Rr
DOT = dot -Tpdf -Grankdir=LR
EXTRAFILES = LICENSE README.md
DISTFILES = $(SRCDIR) $(TESTDIR) Makefile $(EXTRAFILES)
distdir = $(AUTHOR)-$(NAME)-$(VERSION)
all: $(NAME)
graph:
$(DOT) graph.dot > out.pdf
debug: CFLAGS += -DDEBUG -g
debug: $(NAME)
$(SRC_DIR)/%.o: %.c
$(CC) $(CFLAGS) $< -c
$(NAME): $(OBJS)
$(CC) $(CFLAGS) $^ -o $(NAME)
dist: distdir
$(CHMOD) -R a+r $(distdir)
$(TAR) zcvf $(distdir).tgz $(distdir)
$(RM) -r $(distdir)
distdir: $(DISTFILES)
$(RM) -r $(distdir)
$(MKDIR) $(distdir)
$(CHMOD) 777 $(distdir)
$(CP) $(DISTFILES) $(distdir)
clean:
$(RM) $(OBJS) out.pdf
fclean: clean
$(RM) $(NAME)
.PHONY: all clean fclean re dist distdir debug

29197
graph.dot

File diff suppressed because it is too large Load Diff

BIN
out.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 747 KiB

147
src/cparse.c Normal file
View File

@ -0,0 +1,147 @@
#include "vec.h"
#include <dirent.h>
#include <errno.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
graph_vec find_funcs(const char *path, graph_vec *g)
{
FILE *fp;
regex_t fdre, fcre;
int ret;
char line[1024];
char boum[1024];
regmatch_t rm[2];
// TODO: rework regex to ignore static char *(spo_name_tab[SPO_COUNT]) =
// { "ms=", "me=", "hs=", "he=", "rs=", "re=", "lc=" };
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));
exit(1);
}
ret = regcomp(&fdre, re_func_decl, REG_EXTENDED);
if (ret != 0) {
fprintf(stderr, "Failed to compile regex '%s'\n", re_func_decl);
fclose(fp);
exit(2);
}
ret = regcomp(&fcre, re_func_call, REG_EXTENDED);
if (ret != 0) {
fprintf(stderr, "Failed to compile regex '%s'\n", re_func_call);
fclose(fp);
exit(2);
}
bool in = false;
int index;
int cur_node;
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)) > -1) {
if (g->node[cur_node]->nbs == g->node[cur_node]->cap)
g->node[cur_node]->succ =
reallocarray(g->node[cur_node]->succ, (g->node[cur_node]->cap += 5), sizeof(nodept));
g->node[cur_node]->succ[g->node[cur_node]->nbs] = g->node[index];
g->node[cur_node]->nbs++;
} else {
insert_node(g, boum);
if (g->node[cur_node]->nbs == g->node[cur_node]->cap)
g->node[cur_node]->succ =
reallocarray(g->node[cur_node]->succ, (g->node[cur_node]->cap += 5), sizeof(nodept));
g->node[cur_node]->succ[g->node[cur_node]->nbs] = g->node[g->n];
g->node[cur_node]->nbs++;
}
}
} else if (regexec(&fdre, line, 2, rm, 0) == 0) { // function definition
sprintf(boum, "%.*s", (int)(rm[1].rm_eo - rm[1].rm_so), line + rm[1].rm_so);
if ((index = is_in(*g, boum)) > -1) {
cur_node = index;
} else {
insert_node(g, boum);
cur_node = g->n;
}
in = true;
}
}
// for (int j = 0; j < g.n; j++) {
// printf("NODE %d: %s\n", j, g.node[j]->func);
// }
// fix_str m = "main";
// index = is_in(*g, m);
// print_node(g->node[index]);
// dot_graph(g->node[index]);
// int test = is_in(g, "ml_new_data");
// printf("%s\n", g->node[index]->func);
// printf("%d\n", g->node[index]->nbs);
regfree(&fdre);
regfree(&fcre);
fclose(fp);
return *g;
}
void findAndParse(const char *path, graph_vec *g)
{
char p[1024];
struct dirent *dp;
DIR *dir = opendir(path);
if (!dir)
return;
while ((dp = readdir(dir)) != NULL) {
if (dp->d_type == DT_DIR) {
if (strncmp(dp->d_name, ".", 1) == 0)
continue;
snprintf(p, sizeof(p), "%s/%s", path, dp->d_name);
// findAndParse(p, g);
} else {
char *f = strrchr(dp->d_name, '.');
if (f) {
if (strncmp(f, ".c", 2) == 0) {
char pa[255];
sprintf(pa, "%s/%s", path, dp->d_name);
find_funcs(pa, g);
}
}
}
}
closedir(dir);
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
graph_vec g = new_graph(10);
// find_funcs("/home/user/dev/l2/algo2/pattern-search/src/rgxp.c", &g);
findAndParse("/tmp/neovim/src/nvim", &g);
// find_funcs("/tmp/neovim/src/nvim/eval/decode.c", &g);
// find_funcs("/tmp/neovim/src/nvim/eval/encode.c", &g);
// find_funcs("/tmp/neovim/src/nvim/eval/executor.c", &g);
// find_funcs("/tmp/neovim/src/nvim/eval/funcs.c", &g);
// find_funcs("/tmp/neovim/src/nvim/eval/gc.c", &g);
// for (int j = 0; j <= g.n; j++) {
// printf("NODE %d: '%s'\n", j, g.node[j]->func);
// }
// int i = is_in(g, "nvim_buf_get_offset");
// if (i > -1) {
// puts("yes");
// print_node(g.node[i]);
// }
// } else
dot_graph(g);
delete_graph(&g);
return 0;
}

BIN
src/cparse.o Normal file

Binary file not shown.

13
src/matcom.h Normal file
View File

@ -0,0 +1,13 @@
typedef char fix_str[256];
struct edge {
int i; // noeud de depart
int j; // noeud d'arrivé
};
struct graph_mco {
int nbs; // nombre de sommets
int nba; // nombre d'aretes
fix_str *func; // noms de fonction
struct edge *vec; // vecteur d'aretes
};

138
src/vec.c Normal file
View File

@ -0,0 +1,138 @@
#include "vec.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
nodept new_node(int num)
{
nodept n = malloc(sizeof(struct node));
assert(n);
// n->succ = malloc((n->cap = 8) * sizeof(nodept));
// assert(n->succ);
n->num = num;
n->nbs = 0;
return n;
}
// void delete_node(struct node *n)
// {
// int i;
// // for (i = 0; i < n->nbs; i++) {
// // free(n->succ[i]);
// // }
// // free(n->succ);
// }
// void insert_succ(graph_vec *g, int i, int i1, fix_str str)
// {
// if (g->node[i]->nbs == g->node[i]->cap)
// g->node[i]->succ = reallocarray(g->node[i]->succ, (g->node[i]->cap += 5), sizeof(nodept));
// struct node *n = malloc(sizeof(struct node));
// n->num = i;
// n->nbs = 0;
// strcpy(n->func, str);
// g->node[i]->succ[i1] = n;
// g->node[i]->nbs++;
// }
graph_vec new_graph(int cap)
{
graph_vec g;
g.n = -1;
g.cap = cap;
g.node = malloc(cap * sizeof(nodept));
assert(g.node);
return g;
}
void insert_node(graph_vec *g, fix_str str)
{
++g->n;
if (g->n == g->cap)
g->node = reallocarray(g->node, (g->cap += 128), sizeof(nodept));
struct node *n = malloc(sizeof(struct node));
assert(n);
n->num = g->n;
n->nbs = 0;
n->cap = 5;
n->succ = malloc(5 * sizeof(nodept));
strcpy(n->func, str);
g->node[g->n] = n;
}
void delete_graph(graph_vec *g)
{
int i;
for (i = 0; i <= g->n; ++i) {
if (g->node[i]) {
if (g->node[i]->succ) {
free(g->node[i]->succ);
g->node[i]->succ = NULL;
}
free(g->node[i]);
g->node[i] = NULL;
}
}
free(g->node);
}
int is_in(graph_vec g, fix_str func)
{
int i;
if (g.n < 0) {
return -1;
}
for (i = 0; i <= g.n; i++) {
if (strcmp(g.node[i]->func, func) == 0) {
return i;
}
}
return -1;
}
void print_node(nodept node)
{
int i;
printf("NODE: %s\n", node->func);
for (i = 0; i < node->nbs; ++i) {
printf("SUCC %d: %s\n", i, node->succ[i]->func);
}
for (i = 0; i < node->nbs; ++i) {
if (node->succ[i]->num != node->num && node->succ[i]->nbs != 0)
print_node(node->succ[i]);
}
}
// fprintf(fp, "\t%s -> %s;\n", node->func, node->succ[i]->func);
void write_node_dot(FILE *fp, graph_vec g)
{
int i, j;
for (i = 0; i <= g.n; i++) {
for (j = 0; j < g.node[i]->nbs; j++) {
// printf("\t%s -> %s;\n", node->func, node->succ[i]->func);
fprintf(fp, "\t%s -> %s;\n", g.node[i]->func, g.node[i]->succ[j]->func);
}
}
// for (i = 0; i < node->nbs; i++) {
// if (node->succ[i]->nbs != 0 && node->succ[i]->num != node->num) {
// write_node_dot(fp, node->succ[i]);
// }
// }
}
void dot_graph(graph_vec g)
{
FILE *fp;
fp = fopen("graph.dot", "w");
fprintf(fp, "digraph main {\n");
write_node_dot(fp, g);
// write_node_dot(fp, node->succ[2]->succ[1]->succ[0]);
fprintf(fp, "}");
fclose(fp);
}

29
src/vec.h Normal file
View File

@ -0,0 +1,29 @@
#include <stdbool.h>
typedef char fix_str[256];
typedef struct node *nodept;
typedef struct nodes graph_vec;
struct nodes {
int n;
int cap;
nodept *node;
};
struct node {
int num; // le numero de noeud
int nbs; // nombre de successeurs
fix_str func; // noms de fonctions
int cap;
nodept *succ; // les successeurs
};
struct node *new_node(int num);
void delete_node(struct node *n);
void insert_succ(graph_vec *g, int i, int i1, fix_str str);
graph_vec new_graph(int cap);
void insert_node(graph_vec *g, fix_str str);
void delete_graph(graph_vec *g);
int is_in(graph_vec g, fix_str func);
void print_node(nodept node);
void dot_graph(graph_vec g);

BIN
src/vec.o Normal file

Binary file not shown.