-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathscread.c
100 lines (86 loc) · 3.36 KB
/
scread.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
#include <stdio.h>
#include <stdbool.h>
#include <fcntl.h>
#include "clang-c/Index.h"
char* findingString = "Init";
int numOfArgc = 0;
bool printKindSpelling(CXCursor cursor) {
enum CXCursorKind curKind = clang_getCursorKind(cursor);
const char *curkindSpelling = clang_getCString(
clang_getCursorKindSpelling(curKind));
printf("The AST node kind spelling is:%s\n", curkindSpelling);
return true;
}
bool printSpelling(CXCursor cursor) {
const char *astSpelling = clang_getCString(clang_getCursorSpelling(cursor));
printf("The AST node spelling is:%s\n", astSpelling);
return true;
}
bool printLocation(CXCursor cursor) {
CXSourceRange range = clang_getCursorExtent(cursor);
CXSourceLocation startLocation = clang_getRangeStart(range);
CXSourceLocation endLocation = clang_getRangeEnd(range);
CXFile file;
unsigned int line, column, offset;
clang_getInstantiationLocation(startLocation, &file, &line, &column, &offset);
printf("Start: Line: %u Column: %u Offset: %u\n", line, column, offset);
clang_getInstantiationLocation(endLocation, &file, &line, &column, &offset);
printf("End: Line: %u Column: %u Offset: %u\n", line, column, offset);
return true;
}
enum CXChildVisitResult printVisitor(CXCursor cursor, CXCursor parent,
CXClientData client_data) {
char *astSpelling = clang_getCString(clang_getCursorSpelling(cursor));
if (numOfArgc == 3) {
if (strcmp(astSpelling, findingString) == 0) {
printSpelling(cursor);
printKindSpelling(cursor);
printLocation(cursor);
}
} else {
printSpelling(cursor);
printKindSpelling(cursor);
printLocation(cursor);
}
return CXChildVisit_Recurse;
}
int main(int argc, char *argv[]) {
if ((argc > 3) || (argc < 2)) {
printf("You input wrong number arguments!\n");
return -1;
}
if ((strcmp(argv[1],"-v")) == 0) {
printf("scread version:0.1\n");
printf("Author shining\n");
printf("Mail:[email protected]\n");
return 0;
}else if ((strcmp(argv[1],"-help")) == 0) {
printf("scread <filename> The scread will output all the AST nodes' information\n");
printf("scread <filename> <keyword> The scread will output the AST nodes' matched the keyword.\n");
printf("scread -v The scread will output the version information.\n");
printf("scread -help The scread will output the help information.\n");
return 0;
}else {
int result = open(argv[1], O_RDONLY);
if (result == -1) {
printf("Can't open the file: %s.\n", argv[1]);
return -1;
}
}
numOfArgc = argc;
if (numOfArgc == 3) {
findingString = argv[2];
}
CXIndex Index = clang_createIndex(0,0);
CXTranslationUnit TU = clang_parseTranslationUnit(Index, 0, argv, argc,
0, 0,
CXTranslationUnit_None);
CXCursor C = clang_getTranslationUnitCursor(TU);
printSpelling(C);
printKindSpelling(C);
printLocation(C);
clang_visitChildren(C, printVisitor, NULL);
clang_disposeTranslationUnit(TU);
clang_disposeIndex(Index);
return 0;
}