annotatec/main.c -rw-r--r-- 4.2 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#define _POSIX_C_SOURCE 200809L
#include <getopt.h>
#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)) {
		fprintf(stderr, "\nFatal: cpp command larger than internal buffer\n");
		exit(1);
	}
	return popen(cmd, "r");
}

static void scan_git_tree(struct project_graph *graph) {
	FILE *git = popen("git ls-tree -zr HEAD", "r");
	char *line = NULL;
	size_t n = 0;
	/* TODO: Add incremental mode (filtered by modified in last commit) */
	while (getdelim(&line, &n, '\0', git) != -1) {
		const char *delims = " \t";
		/* const char *mode = */
		strtok(line, delims);
		/* const char *type = */
		strtok(NULL, delims);
		const char *oid = strtok(NULL, delims);
		const char *path = strtok(NULL, delims);
		struct source_file *file = get_source_file(graph, path);
		if (file != NULL) {
			file->oid = strdup(oid);
		}
	}
	free(line);
	pclose(git);
}

static void print_jstring(const char *str) {
	while (*str) {
		if (*str < 0x20) {
			printf("\\u00%2x", *str);
			continue;
		}
		switch (*str) {
		case '\\':
			printf("\\\\");
			break;
		case '\n':
			printf("\\n");
		}
		printf("%c", *str);
		str++;
	}
}

static void emit_annotations(struct project_graph *graph, bool git) {
	struct source_file *file = graph->files;
	printf("{\n");
	while (file) {
		if (git && !file->oid) {
			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);
		printf("\": [\n");

		struct symbol_reference *ref;
		struct symbol_definition *symbol;

		bool first = true;
		symbol = file->symbols;
		while (symbol) {
			if (symbol->references) {
				if (!first) {
					printf(",\n");
				} else {
					first = false;
				}
				printf("\t\t{\"type\":\"markdown\",\"lineno\":%d,"
						"\"title\": \"%d reference%s\",\n\t\t\"content\":\"",
						symbol->lineno, symbol->nrefs,
						symbol->nrefs > 1 ? "s" : "");
			}
			ref = symbol->references;
			while (ref) {
				// TODO: need to escape html as well
				printf("- [");
				print_jstring(ref->file->path);
				printf(":%d](", ref->lineno);
				print_jstring(ref->file->path);
				printf("#L%d)\\n", ref->lineno);
				ref = ref->snext;
			}
			if (symbol->references) {
				printf("\"}");
			}
			symbol = symbol->next;
		}

		ref = file->references;
		while (ref) {
			symbol = ref->symbol;
			if (symbol->file) {
				if (!first) {
					printf(",\n");
				} else {
					first = false;
				}
				printf("\t\t{\"type\":\"link\","
						"\"lineno\": %d, \"colno\": %d, \"len\": %zd,"
						"\"to\": \"",
						ref->lineno, ref->colno, strlen(symbol->name));
				print_jstring(symbol->file->path);
				printf("#L%d", symbol->lineno);
				printf("\"}");
			}
			ref = ref->fnext;
		}
		printf("\n");

		if (file->next) {
			printf("\t],\n");
		} else {
			printf("\t]\n");
		}

		file = file->next;
	}
	printf("}\n");
}

extern int yydebug;
extern bool preprocessed_linenos;

int main(int argc, char **argv) {
	char *cppcmd = "cpp -std=c11";

	bool git = false;
	int c;
	while ((c = getopt(argc, argv, "dDC:g")) != -1) {
		switch (c) {
		case 'C':
			cppcmd = optarg;
			break;
		case 'd':
			yydebug = 1;
			break;
		case 'D':
			preprocessed_linenos = true;
			break;
		case 'g':
			git = true;
			break;
		}
	}

	int nsyms = 0, nrefs = 0;
	for (int i = optind; i < argc; ++i) {
		const char *path = argv[i];
		FILE *cpp = invoke_cpp(cppcmd, path);
		struct source_file *file = lexreset(path, cpp);
		fprintf(stderr, "Scanning %3d/%d %-25s ",
				i - optind + 1, argc - optind, path);
		yyparse();
		pclose(cpp);
		fprintf(stderr, "%3d symbols %5d references\n",
				file->nsyms, file->nrefs);
		nsyms += file->nsyms, nrefs += file->nrefs;
		file->next = graph.files;
		graph.files = file;
		cache_source_file(&graph, file);
	}
	fprintf(stderr, "Scan complete: "
			"%d files, %d symbols, %d references, %d orphan refs\n",
			argc - optind, nsyms, nrefs, graph.norphans);

	if (git) {
		scan_git_tree(&graph);
	}

	emit_annotations(&graph, git);

	// TODO: free state

	return 0;
}