~sircmpwn/annotatec

4dde209d96836fab3971e6ec7f7200911d2fd7c4 — Drew DeVault 7 months ago 6493f7c
Use realpath for file lookup
2 files changed, 16 insertions(+), 6 deletions(-)

M graph.c
M main.c
M graph.c => graph.c +13 -4
@@ 1,4 1,6 @@
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


@@ 124,14 126,19 @@ struct symbol_reference *reference_symbol(

struct source_file *get_source_file(
		struct project_graph *graph, const char *path) {
	unsigned int hash = djb2(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 && strcmp(path, bucket->file->path) == 0) {
			return bucket->file;
		if (bucket->hash == hash) {
			realpath(bucket->file->path, have);
			if (strcmp(want, have) == 0) {
				return bucket->file;
			}
		}
		bucket = bucket->next;
	}


@@ 140,7 147,9 @@ struct source_file *get_source_file(

void cache_source_file(struct project_graph *graph, struct source_file *file) {
	/* Note: doesn't address repeated filenames */
	unsigned int hash = djb2(file->path);
	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;

M main.c => main.c +3 -2
@@ 62,8 62,9 @@ static void emit_annotations(struct project_graph *graph, bool git) {
	printf("{\n");
	while (file) {
		if (git && !file->oid) {
			fprintf(stderr, "Warning: no git tree entry for %s", file->path);
			return;
			fprintf(stderr, "Warning: no git tree entry for %s\n", file->path);
			file = file->next;
			continue;
		}
		printf("\t\"");
		print_jstring(file->oid ? file->oid : file->path);