Skip to content

Commit c5b34e0

Browse files
committed
separate this clang rep from llvm_misc
0 parents  commit c5b34e0

31 files changed

+2299
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.o
2+
*.out
3+
*.pyc
4+
*.so
5+
*.ll
6+
*.bc

c/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- mode: makefile-gmake; -*-
2+
LLVM_COMPONENTS := \
3+
jit interpreter irreader nativecodegen codegen \
4+
bitreader bitwriter ipo linker instrumentation core mc
5+
6+
CLANG_LIBS := \
7+
-lclang -lclangFrontend -lclangFrontendTool -lclangDriver -lclangSerialization \
8+
-lclangCodeGen -lclangParse -lclangSema -lclangEdit \
9+
-lclangAnalysis -lclangAST -lclangLex -lclangBasic\
10+
-lclangRewriteCore -lclangRewriteFrontend -lclangStaticAnalyzerFrontend \
11+
-lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangARCMigrate
12+
13+
CFLAGS += $(shell llvm-config-3.4 --cflags) -std=c99
14+
LDFLAGS += $(shell llvm-config-3.4 --ldflags --libs $(LLVM_COMPONENTS))
15+
16+
SRCS := $(wildcard *.c)
17+
OBJS := $(SRCS:.c=.o)
18+
EXES := $(SRCS:.c=.out)
19+
20+
all: $(EXES)
21+
22+
.c.o:
23+
$(CC) $(CFLAGS) -c $< -o $@
24+
25+
.o.out:
26+
$(CC) $< $(CLANG_LIBS) $(LDFLAGS) -o $@
27+
28+
clean:
29+
$(RM) *.out *.o

c/SymbolScanner.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <clang-c/Index.h>
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
enum CXChildVisitResult foundChild(CXCursor cursor, CXCursor parent,
8+
CXClientData client_data) {
9+
if (clang_getCursorKind(cursor) == CXCursor_CallExpr) {
10+
printf("-%s\n", clang_getCString(clang_getCursorSpelling(cursor)));
11+
}
12+
13+
if (clang_isDeclaration(clang_getCursorKind(cursor)) &&
14+
(clang_getCursorLinkage(cursor) == CXLinkage_External ||
15+
clang_getCursorLinkage(cursor) == CXLinkage_Internal)) {
16+
CXFile file;
17+
const char *filename, *wantFilename;
18+
CXSourceLocation cloc;
19+
20+
cloc = clang_getCursorLocation(cursor);
21+
clang_getInstantiationLocation(cloc, &file, NULL, NULL, NULL);
22+
filename = clang_getCString(clang_getFileName(file));
23+
wantFilename = (const char *)client_data;
24+
25+
if (!filename || strcmp(wantFilename, filename))
26+
return CXChildVisit_Recurse;
27+
28+
if (clang_getCursorLinkage(cursor) == CXLinkage_External)
29+
printf("+%s\n", clang_getCString(clang_getCursorSpelling(cursor)));
30+
else
31+
printf("?%s\n", clang_getCString(clang_getCursorSpelling(cursor)));
32+
}
33+
34+
return CXChildVisit_Recurse;
35+
}
36+
37+
int main(int argc, char **argv) {
38+
CXTranslationUnit tu;
39+
CXIndex idx;
40+
CXCursor cur;
41+
char *filename;
42+
43+
if (argc != 2) {
44+
printf("Usage: %s source_filename.c\n", argv[0]);
45+
return EXIT_FAILURE;
46+
}
47+
48+
filename = argv[1];
49+
idx = clang_createIndex(1, 1);
50+
// FIXME, obvious
51+
const char *args[] = {"-I/usr/lib/llvm-3.4/include"};
52+
tu = clang_createTranslationUnitFromSourceFile(idx, filename, 1, args, 0,
53+
NULL);
54+
cur = clang_getTranslationUnitCursor(tu);
55+
56+
clang_visitChildren(cur, foundChild, strdup(filename));
57+
58+
return EXIT_SUCCESS;
59+
}

c/clang_visit.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <string.h>
4+
#include <clang-c/Index.h>
5+
6+
#define MAX_VAR_LENGTH 128
7+
8+
enum MATCHING_TYPE {
9+
MATCH_DEFINE,
10+
MATCH_DECLARE
11+
};
12+
13+
struct MatchPatternStruct {
14+
int type;
15+
char name[MAX_VAR_LENGTH];
16+
CXCursor result_cursor;
17+
};
18+
19+
typedef struct MatchPatternStruct MatchPattern;
20+
21+
void init_match_pattern(MatchPattern* m, enum MATCHING_TYPE type,
22+
const char* name) {
23+
m->type = type;
24+
strncpy(m->name, name, MAX_VAR_LENGTH);
25+
m->result_cursor = clang_getNullCursor();
26+
}
27+
28+
int compare_match_pattern(MatchPattern* a, MatchPattern* b) {
29+
return ((a->type == b->type) && (strcmp(a->name, b->name) == 0));
30+
}
31+
32+
enum CXChildVisitResult visitor_f(CXCursor cursor, CXCursor parent,
33+
CXClientData clientData) {
34+
CXFile file;
35+
unsigned int line, column, offset;
36+
MatchPattern* mp = (MatchPattern*)clientData;
37+
38+
// if corresponding file is invalid, then visit sibling
39+
clang_getInstantiationLocation(clang_getCursorLocation(cursor), &file, &line,
40+
&column, &offset);
41+
42+
if (!clang_getFileName(file).data) {
43+
//printf("visitor(): no file data, visit sibling\n");
44+
return CXChildVisit_Continue;
45+
}
46+
47+
// print if the cursor matches the clientData
48+
const char* info = clang_getCString(clang_getCursorSpelling(cursor));
49+
if ((strlen(info) > 0) && (strcmp(mp->name, info) == 0)) {
50+
const char* kind = clang_getCString(
51+
clang_getCursorKindSpelling(clang_getCursorKind(cursor)));
52+
const char* fname = clang_getCString(clang_getFileName(file));
53+
printf("%s: (%d, %d) %s\t: %s\n", fname, line, column, kind, info);
54+
}
55+
return CXChildVisit_Recurse;
56+
}
57+
58+
int main(int argc, char* argv[]) {
59+
if (argc < 2) {
60+
fprintf(stderr, "Usage:\n\t <-s symbol> <-f file1>[,file2]...\n");
61+
return 1;
62+
}
63+
64+
int c;
65+
char* ast_files[128];
66+
char* p;
67+
char* tmp_files;
68+
CXTranslationUnit tu_array[64];
69+
char* symbol;
70+
71+
while ((c = getopt(argc, argv, "s:f:")) != -1) {
72+
switch (c) {
73+
case 's':
74+
symbol = optarg;
75+
break;
76+
case 'f':
77+
tmp_files = optarg;
78+
break;
79+
default:
80+
printf("unknow option -%c.\n", optopt);
81+
}
82+
}
83+
84+
if (symbol == NULL) {
85+
fprintf(stderr, "No symbol specified, exit.\n");
86+
return 1;
87+
}
88+
89+
p = strtok(tmp_files, ",");
90+
int fcnt = 0;
91+
while (p != NULL) {
92+
ast_files[fcnt++] = p;
93+
p = strtok(NULL, ",");
94+
}
95+
96+
// debug print
97+
printf("query symbol '%s'\n", symbol);
98+
printf("total %d ast files:\n", fcnt);
99+
for (int i = 0; i < fcnt; i++) printf("\t%s\n", ast_files[i]);
100+
101+
CXIndex cidx = clang_createIndex(0, 0);
102+
MatchPattern m;
103+
init_match_pattern(&m, MATCH_DEFINE, symbol);
104+
105+
// TODO hard code
106+
const char* command_line_args[] = {
107+
"-I/usr/include/x86_64-linux-gnu", "-I/usr/lib/gcc/x86_64-linux-gnu/4.4.7/include", "-I/usr/include/c++/4.4", "-I/usr/include/c++/4.4/x86_64-linux-gnu", "-I/usr/lib/llvm-3.3/include"
108+
};
109+
110+
for (int i = 0; i < fcnt; i++) {
111+
tu_array[i] = clang_parseTranslationUnit(
112+
cidx, ast_files[i], command_line_args, 3, 0, 0,
113+
CXTranslationUnit_DetailedPreprocessingRecord);
114+
115+
CXCursor tu_cur = clang_getTranslationUnitCursor(tu_array[i]);
116+
clang_visitChildren(tu_cur, visitor_f, &m);
117+
}
118+
}

c/diagnose.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <clang-c/Index.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <assert.h>
5+
6+
int main(int argc, char* argv[]) {
7+
CXIndex Index = clang_createIndex(0, 0);
8+
CXTranslationUnit TU = clang_parseTranslationUnit(
9+
Index, 0, (const char**)argv, argc, 0, 0, CXTranslationUnit_None);
10+
unsigned I = 0, J;
11+
unsigned N = clang_getNumDiagnostics(TU);
12+
13+
for (I = 1; I != N; ++I) {
14+
CXDiagnostic diag = clang_getDiagnostic(TU, I);
15+
enum CXDiagnosticSeverity sev = clang_getDiagnosticSeverity(diag);
16+
switch (sev) {
17+
case 0:
18+
assert(0 && "impossible");
19+
break;
20+
case 1:
21+
fputs("[note] ", stderr);
22+
break;
23+
case 2:
24+
fputs("[warning] ", stderr);
25+
break;
26+
case 3:
27+
fputs("[error] ", stderr);
28+
break;
29+
case 4:
30+
fputs("[fatal error] ", stderr);
31+
break;
32+
}
33+
fputs(clang_getCString(clang_getDiagnosticSpelling(diag)), stderr);
34+
fputs("\n", stderr);
35+
unsigned line, column, offset;
36+
CXFile error_file;
37+
unsigned ranges;
38+
FILE* source;
39+
const char* err_filename = NULL;
40+
char* abs_filename = NULL;
41+
if ((ranges = clang_getDiagnosticNumRanges(diag)) >= 1) {
42+
CXSourceRange rangeset = clang_getDiagnosticRange(diag, 0);
43+
clang_getSpellingLocation(clang_getRangeStart(rangeset), &error_file,
44+
&line, &column, &offset);
45+
err_filename = clang_getCString(clang_getFileName(error_file));
46+
fprintf(stderr, "[BEGIN] %s:%d:%d\n", err_filename, line, column);
47+
clang_getSpellingLocation(clang_getRangeEnd(rangeset), &error_file, &line,
48+
&column, &offset);
49+
err_filename = clang_getCString(clang_getFileName(error_file));
50+
fprintf(stderr, "[END] %s:%d:%d\n", err_filename, line, column);
51+
} else {
52+
clang_getSpellingLocation(clang_getDiagnosticLocation(diag), &error_file,
53+
&line, &column, &offset);
54+
err_filename = clang_getCString(clang_getFileName(error_file));
55+
abs_filename = realpath(err_filename, NULL);
56+
fprintf(stderr, "%s:%d:%d\n", abs_filename, line, column);
57+
free(abs_filename);
58+
source = fopen(err_filename, "r");
59+
int hold;
60+
fseek(source, offset - column + 1, SEEK_SET);
61+
while ((hold = getc(source)) != '\n') {
62+
putchar(hold);
63+
}
64+
printf("\n");
65+
for (J = 1; J < column; J++) {
66+
printf(" ");
67+
}
68+
printf("^\n\n");
69+
fclose(source);
70+
}
71+
}
72+
clang_disposeTranslationUnit(TU);
73+
clang_disposeIndex(Index);
74+
return 0;
75+
}

c/test_childvisitor.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <clang-c/Index.h>
2+
3+
enum CXChildVisitResult visitor(CXCursor cursor, CXCursor parent, CXClientData data)
4+
{
5+
return CXChildVisit_Continue;
6+
}
7+
8+
int main(int argc, char * argv[])
9+
{
10+
CXIndex index = clang_createIndex(0, 0);
11+
const char* const* args = (const char* const*)argv;
12+
CXTranslationUnit txUnit = clang_parseTranslationUnit(index, 0, args, argc, 0, 0, CXTranslationUnit_None);
13+
14+
CXCursor cur = clang_getTranslationUnitCursor(txUnit);
15+
clang_visitChildren(cur, visitor, NULL);
16+
17+
clang_disposeTranslationUnit(txUnit);
18+
clang_disposeIndex(index);
19+
return 0;
20+
}

c/test_inclusionVisitor.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <clang-c/Index.h>
2+
3+
void inclusionVisitor(CXFile file, CXSourceLocation* srcLocs,
4+
unsigned numSrcLocs, CXClientData data) {
5+
CXString fname = clang_getFileName(file);
6+
printf("Included:%s\n", clang_getCString(fname));
7+
clang_disposeString(fname);
8+
}
9+
10+
int main(int argc, char* argv[]) {
11+
CXIndex index = clang_createIndex(0, 0);
12+
const char* const* args = (const char * const*)argv;
13+
CXTranslationUnit txUnit = clang_parseTranslationUnit(
14+
index, 0, args, argc, 0, 0, CXTranslationUnit_None);
15+
16+
clang_getInclusions(txUnit, inclusionVisitor, NULL);
17+
18+
clang_disposeTranslationUnit(txUnit);
19+
clang_disposeIndex(index);
20+
return 0;
21+
}

0 commit comments

Comments
 (0)