~sircmpwn/annotatec

annotatec/graph.c -rw-r--r-- 4.7 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "graph.h"

static unsigned int djb2(const char *str) {
	unsigned int hash = 5381;
	char c;
	while ((c = *str++)) {
		hash = ((hash << 5) + hash) + c;
	}
	return hash;
}

static struct symbol_definition *get_or_cache_symbol(
		struct project_graph *graph, struct source_file *file,
		char *name, bool is_static, int lineno, int colno) {
	struct symbol_definition *symbol;
	/* Index symbol */
	unsigned int hash = djb2(name);
	size_t len = (sizeof(graph->file_cache.buckets) /
			sizeof(graph->file_cache.buckets[0]));
	size_t index = hash % len;
	struct scache_entry *previous = NULL, *bucket =
		graph->symbol_cache.buckets[index];
	while (bucket) {
		symbol = bucket->symbol;
		if (bucket->hash == hash && strcmp(symbol->name, name) == 0) {
			if (((symbol->is_static || is_static) && file == symbol->file) ||
					(!(symbol->is_static || is_static) && symbol->file && file)) {
				/* Duplicate symbols */
				fprintf(stderr, "\nWarning: duplicate symbol %s "
						"(in both %s and %s; using former as canonical)", name,
						symbol->file->path, file->path);
				return symbol;
			} else if (!symbol->file && !file) {
				/* Repeated orphan reference */
				return symbol;
			} else if (!symbol->file && file && !is_static) {
				/* Adopt orphaned symbol */
				symbol->file = file;
				symbol->lineno = lineno;
				symbol->colno = colno;
				symbol->is_static = is_static;
				if (symbol->next) {
					symbol->next->prev = symbol->prev;
				}
				if (symbol->prev) {
					symbol->prev->next = symbol->next;
				}
				++file->nsyms;
				if (file->symbols) {
					file->symbols->prev = symbol;
				}
				symbol->next = file->symbols;
				file->symbols = symbol;
				return symbol;
			} else if (symbol->file && !file) {
				return symbol;
			}
		}
		previous = bucket;
		bucket = bucket->next;
	}

	symbol = calloc(sizeof(struct symbol_definition), 1);
	symbol->name = name;
	symbol->lineno = lineno;
	symbol->colno = colno;
	symbol->is_static = is_static;
	symbol->file = file;
	if (file) {
		++file->nsyms;
		if (file->symbols) {
			file->symbols->prev = symbol;
		}
		symbol->next = file->symbols;
		file->symbols = symbol;
	} else {
		++graph->norphans;
		if (graph->orphans) {
			graph->orphans->prev = symbol;
		}
		symbol->next = graph->orphans;
		graph->orphans = symbol;
	}

	bucket = calloc(1, sizeof(struct scache_entry));
	bucket->hash = hash;
	bucket->symbol = symbol;
	if (previous == NULL) {
		graph->symbol_cache.buckets[index] = bucket;
	} else {
		previous->next = bucket;
	}
	return symbol;
}

struct symbol_definition *define_symbol(
		struct project_graph *graph, struct source_file *file,
		char *name, bool is_static, int lineno, int colno) {
	return get_or_cache_symbol(graph, file, name, is_static, lineno, colno);
}

struct symbol_reference *reference_symbol(
		struct project_graph *graph, struct source_file *file,
		char *name, int lineno, int colno) {
	assert(file);
	struct symbol_reference *reference =
		calloc(sizeof(struct symbol_reference), 1);
	reference->lineno = lineno;
	reference->colno = colno;

	struct symbol_definition *symbol =
		get_or_cache_symbol(graph, NULL, name, false, 0, 0);

	reference->symbol = symbol;
	reference->snext = symbol->references;
	symbol->references = reference;
	++symbol->nrefs;

	reference->file = file;
	reference->fnext = file->references;
	file->references = reference;

	++file->nrefs;
	return reference;
}

struct source_file *get_source_file(
		struct project_graph *graph, const char *path) {
	char want[PATH_MAX + 1], have[PATH_MAX + 1];
	realpath(path, want);
	unsigned int hash = djb2(want);
	size_t len = (sizeof(graph->file_cache.buckets) /
			sizeof(graph->file_cache.buckets[0]));
	size_t index = hash % len;
	struct fcache_entry *bucket = graph->file_cache.buckets[index];
	while (bucket) {
		if (bucket->hash == hash) {
			realpath(bucket->file->path, have);
			if (strcmp(want, have) == 0) {
				return bucket->file;
			}
		}
		bucket = bucket->next;
	}
	return NULL;
}

void cache_source_file(struct project_graph *graph, struct source_file *file) {
	/* Note: doesn't address repeated filenames */
	char real[PATH_MAX + 1];
	realpath(file->path, real);
	unsigned int hash = djb2(real);
	size_t len = (sizeof(graph->file_cache.buckets) /
			sizeof(graph->file_cache.buckets[0]));
	size_t index = hash % len;
	struct fcache_entry *previous = NULL, *bucket =
		graph->file_cache.buckets[index];
	while (bucket) {
		previous = bucket;
		bucket = bucket->next;
	}
	bucket = calloc(1, sizeof(struct fcache_entry));
	bucket->hash = hash;
	bucket->file = file;
	if (previous == NULL) {
		graph->file_cache.buckets[index] = bucket;
	} else {
		previous->next = bucket;
	}
}