-
Notifications
You must be signed in to change notification settings - Fork 0
/
huntc.c
223 lines (182 loc) · 6.03 KB
/
huntc.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
#include "huntc.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <clang-c/Index.h>
bool
huntc_normalize_spelling(HuntcConstString spelling, HuntcString* result) {
CXIndex index = clang_createIndex(FALSE, FALSE);
struct CXUnsavedFile main_c_file = {
.Filename = "main.c",
.Contents = spelling.data,
.Length = spelling.length,
};
CXTranslationUnit translation_unit =
clang_parseTranslationUnit(index, "main.c", NULL, 0, &main_c_file, 1, CXTranslationUnit_None);
if (translation_unit == NULL) {
clang_disposeIndex(index);
return false;
}
CXCursor cursor = clang_getTranslationUnitCursor(translation_unit);
CXSourceRange range = clang_getCursorExtent(cursor);
CXToken* tokens;
unsigned token_count;
clang_tokenize(translation_unit, range, &tokens, &token_count);
{
GString* builder = g_string_new(NULL);
if (token_count > 0) {
for (unsigned i = 0; i < token_count - 1; ++i) {
CXString spelling = clang_getTokenSpelling(translation_unit, tokens[i]);
g_string_append_printf(builder, "%s ", clang_getCString(spelling));
clang_disposeString(spelling);
}
CXString spelling = clang_getTokenSpelling(translation_unit, tokens[token_count - 1]);
g_string_append(builder, clang_getCString(spelling));
clang_disposeString(spelling);
}
result->length = builder->len;
result->data = g_string_free(builder, FALSE);
}
clang_disposeTokens(translation_unit, tokens, token_count);
clang_disposeTranslationUnit(translation_unit);
clang_disposeIndex(index);
return true;
}
static enum CXChildVisitResult
compute_associations_visitor(CXCursor cursor, CXCursor parent, CXClientData _associations) {
(void)parent;
GArray* associations = (GArray*)_associations;
if (clang_getCursorKind(cursor) != CXCursor_FunctionDecl)
return CXChildVisit_Continue;
HuntcAssociation a;
cursor = clang_getCanonicalCursor(cursor);
{
CXFile file;
clang_getFileLocation(clang_getCursorLocation(cursor), &file, &a.line, &a.column, NULL);
a.file_name = clang_getFileName(file);
const char* file_name = clang_getCString(a.file_name);
for (unsigned i = 0; i < associations->len; ++i) {
const HuntcAssociation* b = &g_array_index(associations, HuntcAssociation, i);
if (b->line == a.line && b->column == a.column
&& strcmp(clang_getCString(b->file_name), file_name) == 0) {
clang_disposeString(a.file_name);
return CXChildVisit_Continue;
}
}
}
{
CXString type_spelling = clang_getTypeSpelling(clang_getCursorType(cursor));
const char* type_spelling_data = clang_getCString(type_spelling);
size_t type_spelling_length = strlen(type_spelling_data);
if (!huntc_normalize_spelling((HuntcConstString){type_spelling_data, type_spelling_length},
&a.normalized_type_spelling)) {
a.normalized_type_spelling.data = strndup(type_spelling_data, type_spelling_length);
a.normalized_type_spelling.length = type_spelling_length;
}
clang_disposeString(type_spelling);
}
{
CXPrintingPolicy printing_policy = clang_getCursorPrintingPolicy(cursor);
clang_PrintingPolicy_setProperty(printing_policy, CXPrintingPolicy_PolishForDeclaration, TRUE);
a.signature_spelling = clang_getCursorPrettyPrinted(cursor, printing_policy);
clang_PrintingPolicy_dispose(printing_policy);
}
g_array_append_val(associations, a);
return CXChildVisit_Continue;
}
void
huntc_compute_associations(CXTranslationUnit translation_unit, GArray* associations) {
CXCursor cursor = clang_getTranslationUnitCursor(translation_unit);
clang_visitChildren(cursor, compute_associations_visitor, associations);
}
size_t
huntc_distance(HuntcString a, HuntcString b, bool fuzzy) {
if (b.length < a.length) {
HuntcString t = a;
a = b;
b = t;
}
size_t* rows = g_new(size_t, (b.length + 1) * 2);
size_t* row0 = rows + (b.length + 1) * 0;
size_t* row1 = rows + (b.length + 1) * 1;
if (fuzzy) {
for (size_t ci = 0; ci <= b.length; ++ci) {
row0[ci] = 0;
}
} else {
for (size_t ci = 0; ci <= b.length; ++ci) {
row0[ci] = ci;
}
}
for (size_t ri = 1; ri <= a.length; ++ri) {
row1[0] = ri;
for (size_t ci = 1; ci <= b.length; ++ci) {
size_t deletion_cost = row0[ci] + 1;
size_t insertion_cost = row1[ci - 1] + 1;
size_t substitution_cost = a.data[ri - 1] == b.data[ci - 1] ? row0[ci - 1] : row0[ci - 1] + 1;
row1[ci] = MIN(MIN(deletion_cost, insertion_cost), substitution_cost);
}
size_t* t = row0;
row0 = row1;
row1 = t;
}
size_t result = row0[b.length];
g_free(rows);
return result;
}
void
huntc_parse_arguments(
int* argc, char*** argv, char** query, bool* libc, bool* fuzzy, char** format, GError** error) {
gboolean c = *libc;
gboolean f = *fuzzy;
const GOptionEntry entries[] = {
{
.long_name = "query",
.short_name = 'q',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_STRING,
.arg_data = query,
.description = NULL,
.arg_description = "QUERY",
},
{
.long_name = "libc",
.short_name = 'c',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_NONE,
.arg_data = &c,
.description = NULL,
.arg_description = NULL,
},
{
.long_name = "fuzzy",
.short_name = 'f',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_NONE,
.arg_data = &f,
.description = NULL,
.arg_description = NULL,
},
{
.long_name = "format",
.short_name = 'F',
.flags = G_OPTION_FLAG_NONE,
.arg = G_OPTION_ARG_STRING,
.arg_data = format,
.description = NULL,
.arg_description = NULL,
},
G_OPTION_ENTRY_NULL,
};
GOptionContext* context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, entries, NULL);
GError* e = NULL;
g_option_context_parse(context, argc, argv, &e);
*libc = c;
*fuzzy = f;
if (e != NULL) {
g_propagate_error(error, e);
}
g_option_context_free(context);
}