-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfunction.c
149 lines (118 loc) · 3.71 KB
/
function.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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>
#include <clang-c/Index.h>
#include <clang-c/Platform.h>
#include "function.h"
enum CXChildVisitResult cursorVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data);
typedef struct CData {
functions_array* fa;
const char* fname;
} CData;
functions_array* fa_new(size_t cap)
{
functions_array* fa = malloc(sizeof(functions_array));
fa->len = 0;
fa->cap = cap;
fa->data = malloc(fa->cap * sizeof(void*));
return fa;
}
void fa_add(functions_array* fa, function* f)
{
if (fa->len >= fa->cap) {
fa->cap <<= 1; // grow exponentially
fa->data = realloc(fa->data, fa->cap * sizeof(void*));
}
fa->data[fa->len] = f;
fa->len++;
}
void fa_free(functions_array* fa) {
size_t i;
for (i = 0; i < fa->len; i++) {
free(fa->data[i]);
}
free(fa->data);
free(fa);
}
function* fa_at(functions_array* fa, size_t i) {
return fa->data[i];
}
functions_array* get_functions(const char* fname, const char* contents,
unsigned long contents_len)
{
/*printf("DEBUG:\nfname: %s\ncontents: %s\nlen: %lu\n", fname, contents, contents_len);*/
struct CXUnsavedFile *unsaved = NULL;
functions_array* fa;
int unsaved_cnt = (contents_len == 0) ? 0 : 1;
CXIndex index = clang_createIndex(0, 0);
CData cdata;
if(index == 0) {
errno = EIO;
return NULL;
}
/* build unsaved file */
if (contents_len != 0) {
unsaved = malloc(sizeof(struct CXUnsavedFile));
unsaved->Filename = fname;
unsaved->Contents = contents;
unsaved->Length = contents_len;
}
CXTranslationUnit translationUnit = clang_parseTranslationUnit(index,
fname, 0, 0, unsaved, unsaved_cnt,
CXTranslationUnit_Incomplete | CXTranslationUnit_DetailedPreprocessingRecord);
if (translationUnit == 0) {
errno = ENOENT;
return NULL;
}
/* get diagnostics */
/*
CXDiagnosticSet diags = clang_getDiagnosticSetFromTU(translationUnit);
for (unsigned i = 0; i < clang_getNumDiagnosticsInSet(diags); i++) {
CXDiagnostic d = clang_getDiagnosticInSet(diags, i);
CXString str = clang_formatDiagnostic(d, clang_defaultDiagnosticDisplayOptions());
puts(clang_getCString(str));
clang_disposeString(str);
clang_disposeDiagnostic(d);
}
clang_disposeDiagnosticSet(diags);
*/
CXCursor rootCursor = clang_getTranslationUnitCursor(translationUnit);
fa = fa_new(10);
cdata.fa = fa;
cdata.fname = fname;
clang_visitChildren(rootCursor, *cursorVisitor, (CXClientData)&cdata);
clang_disposeTranslationUnit(translationUnit);
clang_disposeIndex(index);
if (unsaved != NULL) {
free(unsaved);
}
return fa;
}
enum CXChildVisitResult cursorVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data)
{
enum CXCursorKind kind = clang_getCursorKind(cursor);
CXString name = clang_getCursorSpelling(cursor);
if (kind == CXCursor_FunctionDecl || kind == CXCursor_ObjCInstanceMethodDecl) {
CXSourceRange extent = clang_getCursorExtent(cursor);
CXSourceLocation start = clang_getRangeStart(extent);
CXSourceLocation end = clang_getRangeEnd(extent);
CXFile loc_file;
CData* cdata = (CData*)client_data;
clang_getExpansionLocation(start, &loc_file, NULL, NULL, NULL);
CXString cx_loc_fname = clang_getFileName(loc_file);
/*printf("file %s\nfunc %s\n", cdata->fname, clang_getCString(cx_loc_fname));*/
if (strcmp(cdata->fname, clang_getCString(cx_loc_fname)) == 0) { // not a local function
function* f = malloc(sizeof(function));
f->name = clang_getCString(name);
clang_getExpansionLocation(start, NULL, &(f->start_line), NULL, NULL);
clang_getExpansionLocation(end, NULL, &(f->end_line), NULL, NULL);
fa_add(cdata->fa, f);
}
clang_disposeString(cx_loc_fname);
return CXChildVisit_Continue;
}
return CXChildVisit_Recurse;
}