-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
246 lines (218 loc) · 7.22 KB
/
main.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2023, Nikita Romaniuk
*/
#include "hash.h"
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DUB_VERSION "0.1.0"
struct callmap_callee {
int count;
const char* whom;
};
struct callmap_caller {
struct callmap_callee* list;
int capacity;
int size;
};
static CXCursor current_function;
static struct hashmap name2sigmap;
static struct hashmap callmap;
static enum CXChildVisitResult visitor(CXCursor cursor, CXCursor parent, CXClientData client_data)
{
CXString spelling = clang_getCursorSpelling(cursor);
CXString name = clang_getCursorDisplayName(cursor);
const char* spelling_cstr = clang_getCString(spelling);
const char* name_cstr = clang_getCString(name);
enum CXCursorKind kind = clang_getCursorKind(cursor);
switch (kind) {
case CXCursor_FunctionDecl: {
current_function = cursor;
hashmap_insert(&name2sigmap, spelling_cstr, strdup(name_cstr));
} break;
case CXCursor_CallExpr: {
if (!clang_Cursor_isNull(current_function)) {
CXString current_function_name = clang_getCursorSpelling(current_function);
const char* current_function_name_cstr = clang_getCString(current_function_name);
struct callmap_caller* caller_entry = hashmap_get(&callmap, current_function_name_cstr);
if (caller_entry == NULL) {
struct callmap_caller* new_caller_entry = malloc(sizeof(*new_caller_entry));
new_caller_entry->capacity = 16;
new_caller_entry->size = 0;
new_caller_entry->list = calloc(new_caller_entry->capacity, sizeof(struct callmap_callee));
hashmap_insert(&callmap, current_function_name_cstr, new_caller_entry);
caller_entry = new_caller_entry;
}
bool fn_was_present = false;
for (int i = 0; i < caller_entry->size; ++i) {
struct callmap_callee* callee = caller_entry->list + i;
if (strcmp(callee->whom, spelling_cstr) == 0) {
++callee->count;
fn_was_present = true;
break;
}
}
if (!fn_was_present) {
if (caller_entry->capacity == caller_entry->size) {
int new_capacity = caller_entry->capacity * 2;
caller_entry->list = realloc(caller_entry->list, sizeof(struct callmap_callee) * new_capacity);
caller_entry->capacity = new_capacity;
}
struct callmap_callee* callee_entry = caller_entry->list + caller_entry->size;
callee_entry->count = 1;
callee_entry->whom = strdup(spelling_cstr);
++caller_entry->size;
}
clang_disposeString(current_function_name);
}
} break;
default: break;
}
clang_disposeString(name);
clang_disposeString(spelling);
return CXChildVisit_Recurse;
}
int main(int argc, char** argv)
{
if (argc != 2) {
fprintf(stderr, "usage: %s <compile_commands.json>", argv[0]);
return 1;
}
const char* compile_commands_path = argv[1];
hashmap_create(&callmap);
hashmap_create(&name2sigmap);
CXIndex index = clang_createIndex(1, 0);
CXCompilationDatabase compilation_db = clang_CompilationDatabase_fromDirectory(compile_commands_path, NULL);
CXCompileCommands compile_commands = clang_CompilationDatabase_getAllCompileCommands(compilation_db);
unsigned int commands_count = clang_CompileCommands_getSize(compile_commands);
for (unsigned int i = 0; i < commands_count; ++i) {
CXCompileCommand command = clang_CompileCommands_getCommand(compile_commands, i);
CXString filename = clang_CompileCommand_getFilename(command);
const char* filename_cstr = clang_getCString(filename);
int args_n = clang_CompileCommand_getNumArgs(command);
const char** args = malloc(sizeof(const char*) * args_n);
for (int j = 0; j < args_n; ++j) {
CXString str = clang_CompileCommand_getArg(command, j);
args[j] = strdup(clang_getCString(str));
clang_disposeString(str);
}
CXTranslationUnit unit;
enum CXErrorCode error = clang_parseTranslationUnit2(index, NULL, args, args_n, NULL, 0, CXTranslationUnit_None, &unit);
if (error != CXError_Success) {
fprintf(stderr, "failed to parse %s: %d\n", filename_cstr, error);
for (int j = 0; j < args_n; ++j)
free((void*) args[j]);
free(args);
continue;
}
fprintf(stderr, "%s\n", filename_cstr);
CXCursor cursor = clang_getTranslationUnitCursor(unit);
clang_visitChildren(cursor, visitor, NULL);
clang_disposeString(filename);
clang_disposeTranslationUnit(unit);
for (int j = 0; j < args_n; ++j)
free((void*) args[j]);
free(args);
}
clang_CompileCommands_dispose(compile_commands);
clang_CompilationDatabase_dispose(compilation_db);
clang_disposeIndex(index);
printf(
"<!DOCTYPE html>"
"<html lang=\"en\">"
"<head>"
"<meta charset=\"UTF-8\">"
"<title>dub "DUB_VERSION" callmap</title>"
"<style>"
"/* https://stackoverflow.com/a/36297446 */"
"ul.tree li {"
"list-style-type: none;"
"position: relative;"
"}"
"ul.tree li ul {"
"display: none;"
"}"
"ul.tree li.open > ul {"
"display: block;"
"}"
"ul.tree li a {"
"text-decoration: none;"
"}"
"ul.tree li > a:not(:last-child):before {"
"content: '+';"
"}"
"ul.tree li.open > a:not(:last-child):before {"
"content: '-';"
"}"
"</style>"
"</head>"
"<body>"
"This file was generated by <i>dub "DUB_VERSION"</i>.<br>"
"<ul class=\"tree\">"
);
for (int i = 0; i < callmap.capacity; ++i) {
struct hashmap_entry* entry = callmap.table + i;
if (entry->exists) {
const struct callmap_caller* caller = entry->value;
const char* entry_sig = hashmap_get(&name2sigmap, entry->key);
if (entry_sig == NULL) {
fprintf(stderr, "something weird happened: we don't know the definition of %s\n", entry->key);
entry_sig = entry->key;
}
printf("<li><a id=\"%s\" href=\"#\">%s: %d</a>", entry_sig, entry_sig, caller->size);
printf("<ul>");
for (int j = 0; j < caller->size; ++j) {
const struct callmap_callee* callee = caller->list + j;
const char* whom_sig = hashmap_get(&name2sigmap, callee->whom);
if (whom_sig == NULL) {
fprintf(stderr, "something weird happened: we don't know the definition of %s\n", callee->whom);
whom_sig = callee->whom;
}
printf("<li><a href=\"#%s\">%s (%d)</a></li>", whom_sig, whom_sig, callee->count);
free((void*) callee->whom);
}
printf("</ul>");
printf("</li>");
free((void*) caller->list);
free((void*) caller);
}
}
printf(
"</ul>"
"<script type=\"text/javascript\">"
"/* https://stackoverflow.com/a/36297446 */"
"let tree = document.querySelectorAll(\"ul.tree a:not(:last-child)\");"
"for (let i = 0; i < tree.length; ++i) {"
"tree[i].addEventListener(\"click\", function(e) {"
"let parent = e.target.parentElement;"
"let classList = parent.classList;"
"if (classList.contains(\"open\")) {"
"classList.remove(\"open\");"
"let opensubs = parent.querySelectorAll(\":scope .open\");"
"for (let j = 0; j < opensubs.length; ++j) {"
"opensubs[j].classList.remove(\"open\");"
"}"
"} else {"
"classList.add(\"open\");"
"}"
"e.preventDefault();"
"});"
"}"
"</script>"
"</body>"
"</html>"
);
// meeeh ?
for (int i = 0; i < name2sigmap.capacity; ++i) {
const struct hashmap_entry* entry = name2sigmap.table + i;
if (entry->exists)
free(entry->value);
}
hashmap_free(&name2sigmap);
hashmap_free(&callmap);
return 0;
}