backup
This commit is contained in:
parent
1b3e3680f9
commit
e075ac2da3
2
Makefile
2
Makefile
@ -1,4 +1,4 @@
|
|||||||
CC = clang
|
CC = gcc
|
||||||
CFLAGS = -Wall -Wextra -Werror -O3
|
CFLAGS = -Wall -Wextra -Werror -O3
|
||||||
# GCCVERSIONGTE10 := $(shell expr `gcc -dumpversion | cut -f1 -d.` \>= 10)
|
# GCCVERSIONGTE10 := $(shell expr `gcc -dumpversion | cut -f1 -d.` \>= 10)
|
||||||
#
|
#
|
||||||
|
48
src/cparse.c
48
src/cparse.c
@ -1,4 +1,3 @@
|
|||||||
#include "vec.h"
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
@ -6,6 +5,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "cparse.h"
|
||||||
|
|
||||||
graph_vec find_funcs(const char *path, graph_vec *g)
|
graph_vec find_funcs(const char *path, graph_vec *g)
|
||||||
{
|
{
|
||||||
@ -49,14 +49,14 @@ graph_vec find_funcs(const char *path, graph_vec *g)
|
|||||||
// printf("Line: <<%.*s>>\n", (int)(rm[0].rm_eo - rm[0].rm_so), line + rm[0].rm_so);
|
// 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);
|
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);
|
// 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 ((index = vis_in(*g, boum)) > -1) {
|
||||||
if (g->node[cur_node]->nbs == g->node[cur_node]->cap)
|
if (g->node[cur_node]->nbs == g->node[cur_node]->cap)
|
||||||
g->node[cur_node]->succ =
|
g->node[cur_node]->succ =
|
||||||
reallocarray(g->node[cur_node]->succ, (g->node[cur_node]->cap += 5), sizeof(nodept));
|
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]->succ[g->node[cur_node]->nbs] = g->node[index];
|
||||||
g->node[cur_node]->nbs++;
|
g->node[cur_node]->nbs++;
|
||||||
} else {
|
} else {
|
||||||
insert_node(g, boum);
|
vinsert_node(g, boum);
|
||||||
if (g->node[cur_node]->nbs == g->node[cur_node]->cap)
|
if (g->node[cur_node]->nbs == g->node[cur_node]->cap)
|
||||||
g->node[cur_node]->succ =
|
g->node[cur_node]->succ =
|
||||||
reallocarray(g->node[cur_node]->succ, (g->node[cur_node]->cap += 5), sizeof(nodept));
|
reallocarray(g->node[cur_node]->succ, (g->node[cur_node]->cap += 5), sizeof(nodept));
|
||||||
@ -66,25 +66,15 @@ graph_vec find_funcs(const char *path, graph_vec *g)
|
|||||||
}
|
}
|
||||||
} else if (regexec(&fdre, line, 2, rm, 0) == 0) { // function definition
|
} 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);
|
sprintf(boum, "%.*s", (int)(rm[1].rm_eo - rm[1].rm_so), line + rm[1].rm_so);
|
||||||
if ((index = is_in(*g, boum)) > -1) {
|
if ((index = vis_in(*g, boum)) > -1) {
|
||||||
cur_node = index;
|
cur_node = index;
|
||||||
} else {
|
} else {
|
||||||
insert_node(g, boum);
|
vinsert_node(g, boum);
|
||||||
cur_node = g->n;
|
cur_node = g->n;
|
||||||
}
|
}
|
||||||
in = true;
|
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(&fdre);
|
||||||
regfree(&fcre);
|
regfree(&fcre);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
@ -105,7 +95,7 @@ void findAndParse(const char *path, graph_vec *g)
|
|||||||
if (strncmp(dp->d_name, ".", 1) == 0)
|
if (strncmp(dp->d_name, ".", 1) == 0)
|
||||||
continue;
|
continue;
|
||||||
snprintf(p, sizeof(p), "%s/%s", path, dp->d_name);
|
snprintf(p, sizeof(p), "%s/%s", path, dp->d_name);
|
||||||
// findAndParse(p, g);
|
findAndParse(p, g);
|
||||||
} else {
|
} else {
|
||||||
char *f = strrchr(dp->d_name, '.');
|
char *f = strrchr(dp->d_name, '.');
|
||||||
if (f) {
|
if (f) {
|
||||||
@ -119,29 +109,3 @@ void findAndParse(const char *path, graph_vec *g)
|
|||||||
}
|
}
|
||||||
closedir(dir);
|
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;
|
|
||||||
}
|
|
||||||
|
4
src/cparse.h
Normal file
4
src/cparse.h
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
#include "vec.h"
|
||||||
|
|
||||||
|
void findAndParse(const char *path, graph_vec *g);
|
||||||
|
graph_vec find_funcs(const char *path, graph_vec *g);
|
BIN
src/cparse.o
BIN
src/cparse.o
Binary file not shown.
38
src/main.c
Normal file
38
src/main.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// code-analyzer creates graph from a C source code.
|
||||||
|
// Copyright © 2021 Volodymyr Patuta
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#include "cparse.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
graph_vec g = vnew_graph();
|
||||||
|
// find_funcs("/home/user/dev/l2/algo2/pattern-search/src/rgxp.c", &g);
|
||||||
|
findAndParse("/tmp/neovim/src/nvim", &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
|
||||||
|
vdot_graph(g);
|
||||||
|
vdelete_graph(&g);
|
||||||
|
return 0;
|
||||||
|
}
|
17
src/matcom.h
17
src/matcom.h
@ -1,4 +1,5 @@
|
|||||||
typedef char fix_str[256];
|
typedef char fix_str[256];
|
||||||
|
typedef struct graph_mco graph_mco;
|
||||||
|
|
||||||
struct edge {
|
struct edge {
|
||||||
int i; // noeud de depart
|
int i; // noeud de depart
|
||||||
@ -11,3 +12,19 @@ struct graph_mco {
|
|||||||
fix_str *func; // noms de fonction
|
fix_str *func; // noms de fonction
|
||||||
struct edge *vec; // vecteur d'aretes
|
struct edge *vec; // vecteur d'aretes
|
||||||
};
|
};
|
||||||
|
|
||||||
|
graph_mco mcgragh(void)
|
||||||
|
{
|
||||||
|
graph_mco g;
|
||||||
|
g.nbs = 0;
|
||||||
|
g.nba = -1;
|
||||||
|
g.func = malloc(10 * sizeof(fix_str));
|
||||||
|
assert(g.func);
|
||||||
|
g.vec = malloc(10 * sizeof(struct edge));
|
||||||
|
assert(g.vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mcinsert_edge(graph_mco *g, fix_str i, int dep, int arr)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
59
src/vec.c
59
src/vec.c
@ -4,49 +4,18 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.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)
|
graph_vec vnew_graph(void)
|
||||||
// {
|
|
||||||
// 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;
|
graph_vec g;
|
||||||
g.n = -1;
|
g.n = -1;
|
||||||
g.cap = cap;
|
g.cap = 10;
|
||||||
g.node = malloc(cap * sizeof(nodept));
|
g.node = malloc(10 * sizeof(nodept));
|
||||||
assert(g.node);
|
assert(g.node);
|
||||||
return g;
|
return g;
|
||||||
}
|
}
|
||||||
|
|
||||||
void insert_node(graph_vec *g, fix_str str)
|
void vinsert_node(graph_vec *g, fix_str str)
|
||||||
{
|
{
|
||||||
++g->n;
|
++g->n;
|
||||||
if (g->n == g->cap)
|
if (g->n == g->cap)
|
||||||
@ -61,7 +30,7 @@ void insert_node(graph_vec *g, fix_str str)
|
|||||||
g->node[g->n] = n;
|
g->node[g->n] = n;
|
||||||
}
|
}
|
||||||
|
|
||||||
void delete_graph(graph_vec *g)
|
void vdelete_graph(graph_vec *g)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i <= g->n; ++i) {
|
for (i = 0; i <= g->n; ++i) {
|
||||||
@ -77,7 +46,7 @@ void delete_graph(graph_vec *g)
|
|||||||
free(g->node);
|
free(g->node);
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_in(graph_vec g, fix_str func)
|
int vis_in(graph_vec g, fix_str func)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
if (g.n < 0) {
|
if (g.n < 0) {
|
||||||
@ -91,7 +60,7 @@ int is_in(graph_vec g, fix_str func)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_node(nodept node)
|
void vprint_node(nodept node)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
printf("NODE: %s\n", node->func);
|
printf("NODE: %s\n", node->func);
|
||||||
@ -100,12 +69,11 @@ void print_node(nodept node)
|
|||||||
}
|
}
|
||||||
for (i = 0; i < node->nbs; ++i) {
|
for (i = 0; i < node->nbs; ++i) {
|
||||||
if (node->succ[i]->num != node->num && node->succ[i]->nbs != 0)
|
if (node->succ[i]->num != node->num && node->succ[i]->nbs != 0)
|
||||||
print_node(node->succ[i]);
|
vprint_node(node->succ[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fprintf(fp, "\t%s -> %s;\n", node->func, node->succ[i]->func);
|
void vwrite_node_dot(FILE *fp, graph_vec g)
|
||||||
void write_node_dot(FILE *fp, graph_vec g)
|
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
for (i = 0; i <= g.n; i++) {
|
for (i = 0; i <= g.n; i++) {
|
||||||
@ -114,14 +82,9 @@ void write_node_dot(FILE *fp, graph_vec g)
|
|||||||
fprintf(fp, "\t%s -> %s;\n", g.node[i]->func, g.node[i]->succ[j]->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)
|
void vdot_graph(graph_vec g)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
@ -129,7 +92,7 @@ void dot_graph(graph_vec g)
|
|||||||
|
|
||||||
fprintf(fp, "digraph main {\n");
|
fprintf(fp, "digraph main {\n");
|
||||||
|
|
||||||
write_node_dot(fp, g);
|
vwrite_node_dot(fp, g);
|
||||||
// write_node_dot(fp, node->succ[2]->succ[1]->succ[0]);
|
// write_node_dot(fp, node->succ[2]->succ[1]->succ[0]);
|
||||||
|
|
||||||
fprintf(fp, "}");
|
fprintf(fp, "}");
|
||||||
|
15
src/vec.h
15
src/vec.h
@ -18,12 +18,9 @@ struct node {
|
|||||||
nodept *succ; // les successeurs
|
nodept *succ; // les successeurs
|
||||||
};
|
};
|
||||||
|
|
||||||
struct node *new_node(int num);
|
graph_vec vnew_graph(void);
|
||||||
void delete_node(struct node *n);
|
void vinsert_node(graph_vec *g, fix_str str);
|
||||||
void insert_succ(graph_vec *g, int i, int i1, fix_str str);
|
void vdelete_graph(graph_vec *g);
|
||||||
graph_vec new_graph(int cap);
|
int vis_in(graph_vec g, fix_str func);
|
||||||
void insert_node(graph_vec *g, fix_str str);
|
void vprint_node(nodept node);
|
||||||
void delete_graph(graph_vec *g);
|
void vdot_graph(graph_vec g);
|
||||||
int is_in(graph_vec g, fix_str func);
|
|
||||||
void print_node(nodept node);
|
|
||||||
void dot_graph(graph_vec g);
|
|
||||||
|
Loading…
Reference in New Issue
Block a user