~sircmpwn/annotatec

5554c69f84ac53668a1f01761a3fe15eda868705 — Drew DeVault 7 months ago a3b8fab
Add graph structures
3 files changed, 58 insertions(+), 16 deletions(-)

A graph.h
M main.c
M parse.h
A graph.h => graph.h +42 -0
@@ 0,0 1,42 @@
#ifndef _GRAPH_H
#define _GRAPH_H
#include <stdbool.h>

struct symbol_cache {
	struct symtab_entry *buckets[256];
};

struct symtab_entry {
	struct symtab_entry *next;
	unsigned int hash;
	struct symbol_definition *symbol;
};

struct symbol_definition {
	struct source_file *file;
	char *name;
	int lineno, colno;
	int nreferences;
	bool is_static;
};

struct symbol_reference {
	struct symbol_reference *next;
	struct symbol_definition *symbol;
	int lineno, colno;
};

struct source_file {
	struct source_file *next;
	char *path, *blob_sha;
	struct symbol_definition *symbols;
	struct symbol_reference *references;
};

struct project_graph {
	struct source_file *files;
	struct symbol_definition *orphans;
	struct symbol_cache cache;
};

#endif

M main.c => main.c +13 -16
@@ 2,8 2,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "graph.h"
#include "parse.h"

struct project_graph graph = { 0 };

static FILE *invoke_cpp(const char *cppcmd, const char *input) {
	char cmd[4096];
	if (snprintf(cmd, sizeof(cmd), "%s %s", cppcmd, input) == sizeof(cmd)) {


@@ 13,29 16,19 @@ static FILE *invoke_cpp(const char *cppcmd, const char *input) {
	return popen(cmd, "r");
}

static void postprocess(const char *file) {
	printf("\n%s:\n\nDefinitions:\n", file);
static void postprocess(struct source_file *file) {
	struct funcdef *def = parse_state.funcdefs;
	while (def) {
		// TODO: Store function definitions for later linking to references
		if (def->is_static) {
			printf("\t%s (static): %s:%d:%d\n", def->strloc.name,
					file, def->strloc.lineno, def->strloc.colno);
		} else {
			printf("\t%s: %s:%d:%d\n", def->strloc.name,
					file, def->strloc.lineno, def->strloc.colno);
		}
		/* TODO: stow away */
		struct funcdef *next = def->next;
		free(def->strloc.name);
		free(def);
		def = next;
	}

	printf("\nFunction calls:\n");
	struct funccall *call = parse_state.funccalls;
	while (call) {
		printf("\t%s: %s:%d:%d\n", call->strloc.name,
				file, call->strloc.lineno, call->strloc.colno);
		/* TODO: stow away */
		struct funccall *next = call->next;
		free(call->strloc.name);
		free(call);


@@ 44,6 37,7 @@ static void postprocess(const char *file) {

	struct typedefs *tdefs = parse_state.typedefs;
	while (tdefs) {
		/* TODO: stow away */
		struct typedefs *next = tdefs->next;
		free(tdefs->name);
		free(tdefs);


@@ 51,7 45,6 @@ static void postprocess(const char *file) {
	}

	memset(&parse_state, 0, sizeof(parse_state));
	printf("\n");
}

extern int yydebug;


@@ 76,11 69,15 @@ int main(int argc, char **argv) {
	}

	for (int i = optind; i < argc; ++i) {
		struct source_file *file = calloc(1, sizeof(struct source_file));
		file->path = argv[i];
		FILE *cpp = invoke_cpp(cppcmd, argv[i]);
		lexreset(argv[i], cpp);
		lexreset(file->path, cpp);
		yyparse();
		pclose(cpp);
		postprocess(argv[i]);
		postprocess(file);
		file->next = graph.files;
		graph.files = file;
	}

	return 0;

M parse.h => parse.h +3 -0
@@ 2,6 2,7 @@
#define _PARSE_H
#include <stdbool.h>
#include <stdio.h>
#include "graph.h"

int yyparse(void);
void lexreset(const char *filename, FILE *input);


@@ 21,12 22,14 @@ struct typedefs {
	struct typedefs *next;
};

/* TODO: replace with symbol_definition */
struct funcdef {
	struct strloc strloc;
	bool is_static;
	struct funcdef *next;
};

/* TODO: replace with symbol_reference */
struct funccall {
	struct strloc strloc;
	struct funccall *next;