better print and cc other than main

This commit is contained in:
fiplox 2022-01-03 18:06:50 +01:00
parent 0ac868fb25
commit b890edbbe6
6 changed files with 32 additions and 32 deletions

View File

@ -29,6 +29,7 @@ all: $(NAME)
graph:
$(DOT) graph.dot > out.pdf
$(DOT) CC.dot > cc.pdf
debug: CFLAGS += -DDEBUG -g
debug: $(NAME)

View File

@ -28,7 +28,7 @@ void myfunc3(void)
free(strings);
}
static void myfunc2(void) /* "static" means don't export the symbol... */
static void myfunc2(void)
{
myfunc3();
}

View File

@ -24,26 +24,13 @@ int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
// fix_str m = "main";
graph_vec gv = vnew_graph();
graph_mco gmc = mcgragh();
// find_funcs("/home/user/files/dot_files/st/x.c", &gv, &gmc);
// find_funcs("test.c", &gv, &gmc);
findAndParse("/home/user/files/dot_files/st/", &gv, &gmc);
// int n = mcis_in(gmc, m);
// printf("%d\n", n);
// mcprint_edges(gmc, n);
// for (int i = 0; i <= gmc.nbs; i++) {
// printf("Node %d: %s\n", i, gmc.func[i]);
// for (int j = 0; j <= gmc.nba; j++) {
// if (strcmp(gmc.func[i], gmc.func[gmc.vec[j].i]) == 0) {
// printf(" Succ %d: %s\n", gmc.vec[j].j, gmc.func[gmc.vec[j].j]);
// }
// }
// }
// printf("TEST: %s %d\n", gmc.func[2], gmc.vec[2].j);
//
// vprint_nodes(gv.node[0]);
find_funcs("test.c", &gv, &gmc);
// findAndParse("/home/user/files/dot_files/st/", &gv, &gmc);
// vprint_nodes(gv.node[8]);
mcprint_edges(gmc, 8);
vfind_cycles(&gv);
mcdelete_graph(&gmc);
vdot_graph(gv, true);

View File

@ -37,6 +37,7 @@ void mcinsert_edge(graph_mco *g, int dep, int arr)
assert(g->vec);
}
e.i = dep;
e.vu = 0;
if (arr == -1)
e.j = g->nbs;
else
@ -76,6 +77,9 @@ static void mcprint_succ(graph_mco g, int succ)
void mcprint_edges(graph_mco g, int n)
{
int i;
if (g.vec[n].vu)
return;
g.vec[n].vu = 1;
printf("NODE %d: %s\n", n, g.func[n]);
for (i = 0; i <= g.nba; ++i) {
if (g.vec[i].i == n) {

View File

@ -8,6 +8,7 @@ typedef struct graph_mco graph_mco;
struct edge {
int i; // noeud de depart
int j; // noeud d'arrivé
int vu;
};
struct graph_mco {

View File

@ -5,6 +5,8 @@
#include <stdlib.h>
#include <string.h>
enum { NOT_VISITED = 0, IN_STACK = 1, DONE = 2 };
graph_vec vnew_graph(void)
{
graph_vec g;
@ -65,17 +67,13 @@ int vis_in(graph_vec g, fix_str func)
static void vprint_succ(nodept node)
{
// int i;
printf(" Succ: %s\n", node->func);
// for (i = 0; i < node->nbs; ++i) {
// printf("SUCC %d: %s\n", i, node->succ[i]->func);
// }
}
void vprint_nodes(nodept node)
{
int i;
if (node->vu == 1)
if (node->vu)
return;
node->vu = 1;
printf("NODE: %s\n", node->func);
@ -83,11 +81,12 @@ void vprint_nodes(nodept node)
vprint_succ(node->succ[i]);
}
for (i = 0; i < node->nbs; ++i) {
vprint_nodes(node->succ[i]);
if (node->succ[i]->nbs != 0)
vprint_nodes(node->succ[i]);
}
}
void vwrite_node_dot(FILE *fp, graph_vec g)
static void vwrite_node_dot(FILE *fp, graph_vec g)
{
int i, j;
for (i = 0; i <= g.n; i++) {
@ -98,7 +97,7 @@ void vwrite_node_dot(FILE *fp, graph_vec g)
}
}
void main_graph(FILE *fp, nodept node)
static void main_graph(FILE *fp, nodept node)
{
int i;
if (node->vu == 1)
@ -115,11 +114,14 @@ void main_graph(FILE *fp, nodept node)
void vdot_graph(graph_vec g, bool main)
{
FILE *fp;
FILE *fp, *fp1;
int i;
fp = fopen("graph.dot", "w");
fp1 = fopen("CC.dot", "w");
fprintf(fp, "digraph main {\n");
fprintf(fp, "digraph CCmain {\n");
fprintf(fp1, "digraph CC {\n");
if (main) {
fix_str m = "main";
@ -130,20 +132,25 @@ void vdot_graph(graph_vec g, bool main)
fprintf(stderr, "main not found.\n");
exit(1);
}
for (i = 0; i <= g.n; ++i) {
if (!g.node[i]->vu) {
main_graph(fp1, g.node[i]);
}
}
} else {
vwrite_node_dot(fp, g);
}
fprintf(fp, "}");
fprintf(fp1, "}");
fclose(fp);
fclose(fp1);
}
enum { NOT_VISITED = 0, IN_STACK = 1, DONE = 2 };
// NOTE: https://www.baeldung.com/cs/detecting-cycles-in-directed-graph
void print_cycle(graph_vec g, stack_t s, int v)
static void print_cycle(graph_vec g, stack_t s, int v)
{
stack_t s2 = init_stack();
push(&s2, pop(&s));
@ -157,7 +164,7 @@ void print_cycle(graph_vec g, stack_t s, int v)
free_stack(&s2);
}
void processDFS(nodept n, stack_t *s, graph_vec g)
static void processDFS(nodept n, stack_t *s, graph_vec g)
{
int i;
for (i = 0; i < n->nbs; ++i) {