forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clangtest.c
97 lines (91 loc) · 3.58 KB
/
clangtest.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
// cc clangtest.c `llvm-config-3.6 --cflags` -lclang `llvm-config-3.6 --ldflags` -o clangtest
#include <clang-c/Index.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void printString(const char *name, CXString string)
{
const char *cstr = clang_getCString(string);
if (cstr && *cstr) {
printf("%s: %s ", name, cstr);
}
clang_disposeString(string);
}
static void printCursor(CXCursor cursor)
{
CXFile file;
unsigned int off, line, col;
CXSourceLocation location = clang_getCursorLocation(cursor);
clang_getSpellingLocation(location, &file, &line, &col, &off);
CXString fileName = clang_getFileName(file);
const char *fileNameCStr = clang_getCString(fileName);
if (fileNameCStr) {
CXSourceRange range = clang_getCursorExtent(cursor);
unsigned int start, end;
clang_getSpellingLocation(clang_getRangeStart(range), 0, 0, 0, &start);
clang_getSpellingLocation(clang_getRangeEnd(range), 0, 0, 0, &end);
printf("%s:%d:%d (%d, %d-%d) ", fileNameCStr, line, col, off, start, end);
}
clang_disposeString(fileName);
printString("kind", clang_getCursorKindSpelling(clang_getCursorKind(cursor)));
printString("display name", clang_getCursorDisplayName(cursor));
printString("usr", clang_getCursorUSR(cursor));
if (clang_isCursorDefinition(cursor))
printf("definition ");
printf("\n");
}
static enum CXChildVisitResult visit(CXCursor cursor, CXCursor parent, CXClientData userData)
{
(void)parent;
int indent = *(int*)userData;
int i;
for (i=0; i<indent; ++i) {
printf(" ");
}
printCursor(cursor);
CXCursor ref = clang_getCursorReferenced(cursor);
if (!clang_isInvalid(clang_getCursorKind(ref)) && !clang_equalCursors(ref, cursor)) {
for (i=0; i<indent; ++i) {
printf(" ");
}
printf("-> ");
printCursor(ref);
}
++indent;
clang_visitChildren(cursor, visit, &indent);
return CXChildVisit_Continue;
}
int main(int argc, char **argv)
{
if (argc < 2)
return 1;
CXIndex index = clang_createIndex(1, 1);
const char * const *args = 0;
if (argc > 2)
args = (const char *const *)&argv[2];
CXTranslationUnit unit = clang_parseTranslationUnit(index, argv[1], args, argc - 2,
0, 0, clang_defaultEditingTranslationUnitOptions());
if (unit) {
int indent = 0;
clang_visitChildren(clang_getTranslationUnitCursor(unit), visit, &indent);
const unsigned int diagnosticCount = clang_getNumDiagnostics(unit);
unsigned int i;
for (i=0; i<diagnosticCount; ++i) {
CXDiagnostic diagnostic = clang_getDiagnostic(unit, i);
const unsigned int diagnosticOptions = (CXDiagnostic_DisplaySourceLocation|
CXDiagnostic_DisplayColumn|
CXDiagnostic_DisplaySourceRanges|
CXDiagnostic_DisplayOption|
CXDiagnostic_DisplayCategoryId|
CXDiagnostic_DisplayCategoryName);
CXString diagnosticText = clang_formatDiagnostic(diagnostic, diagnosticOptions);
const char *cstr = clang_getCString(diagnosticText);
if (cstr)
printf("%s\n", cstr);
clang_disposeString(diagnosticText);
}
clang_disposeTranslationUnit(unit);
}
clang_disposeIndex(index);
return 0;
}