2009-09-01 23:55:40 +08:00
|
|
|
/* c-index-test.c */
|
2009-08-28 23:28:48 +08:00
|
|
|
|
|
|
|
#include "clang-c/Index.h"
|
2009-08-31 08:59:03 +08:00
|
|
|
#include <stdio.h>
|
2009-09-03 23:49:00 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2009-09-25 04:03:06 +08:00
|
|
|
extern char *basename(const char *);
|
|
|
|
|
2009-09-03 23:49:00 +08:00
|
|
|
static void PrintCursor(CXCursor Cursor) {
|
2009-09-16 04:25:34 +08:00
|
|
|
if (clang_isInvalid(Cursor.kind))
|
|
|
|
printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
|
2009-09-26 05:32:34 +08:00
|
|
|
else {
|
2009-10-06 05:33:42 +08:00
|
|
|
CXDecl DeclReferenced;
|
2009-09-25 04:03:06 +08:00
|
|
|
printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
|
|
|
|
clang_getCursorSpelling(Cursor));
|
2009-10-06 05:33:42 +08:00
|
|
|
DeclReferenced = clang_getCursorDecl(Cursor);
|
2009-10-01 08:31:07 +08:00
|
|
|
if (DeclReferenced)
|
|
|
|
printf(":%d:%d", clang_getDeclLine(DeclReferenced),
|
|
|
|
clang_getDeclColumn(DeclReferenced));
|
2009-09-26 05:32:34 +08:00
|
|
|
}
|
2009-09-03 23:49:00 +08:00
|
|
|
}
|
2009-08-31 08:59:03 +08:00
|
|
|
|
2009-10-06 05:33:42 +08:00
|
|
|
static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
|
2009-09-02 21:28:54 +08:00
|
|
|
{
|
2009-09-03 13:59:50 +08:00
|
|
|
if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
|
2009-09-25 04:03:06 +08:00
|
|
|
printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
|
|
|
|
clang_getCursorLine(Cursor),
|
|
|
|
clang_getCursorColumn(Cursor));
|
2009-09-03 23:49:00 +08:00
|
|
|
PrintCursor(Cursor);
|
2009-09-25 04:03:06 +08:00
|
|
|
printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
|
2009-09-03 13:59:50 +08:00
|
|
|
}
|
2009-09-02 21:28:54 +08:00
|
|
|
}
|
|
|
|
static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
|
2009-10-06 05:33:42 +08:00
|
|
|
CXClientData Filter)
|
2009-09-02 21:28:54 +08:00
|
|
|
{
|
|
|
|
if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
|
2009-09-25 04:03:06 +08:00
|
|
|
printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
|
|
|
|
clang_getCursorLine(Cursor),
|
|
|
|
clang_getCursorColumn(Cursor));
|
2009-09-03 23:49:00 +08:00
|
|
|
PrintCursor(Cursor);
|
2009-09-25 04:03:06 +08:00
|
|
|
printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
|
|
|
|
|
|
|
|
clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
|
2009-09-02 21:28:54 +08:00
|
|
|
|
2009-09-24 01:52:52 +08:00
|
|
|
if (Cursor.kind == CXCursor_FunctionDefn) {
|
|
|
|
const char *startBuf, *endBuf;
|
|
|
|
unsigned startLine, startColumn, endLine, endColumn;
|
|
|
|
clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
|
|
|
|
&startLine, &startColumn,
|
|
|
|
&endLine, &endColumn);
|
2009-09-24 04:00:53 +08:00
|
|
|
{
|
|
|
|
/* Probe the entire body, looking for both decls and refs. */
|
|
|
|
unsigned curLine = startLine, curColumn = startColumn;
|
|
|
|
CXCursor Ref;
|
2009-10-06 05:33:42 +08:00
|
|
|
|
2009-09-24 04:00:53 +08:00
|
|
|
while (startBuf <= endBuf) {
|
|
|
|
if (*startBuf == '\n') {
|
|
|
|
startBuf++;
|
|
|
|
curLine++;
|
|
|
|
curColumn = 1;
|
|
|
|
} else if (*startBuf != '\t')
|
|
|
|
curColumn++;
|
2009-10-06 05:33:42 +08:00
|
|
|
|
|
|
|
Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
|
2009-09-24 04:00:53 +08:00
|
|
|
curLine, curColumn);
|
|
|
|
if (Ref.kind != CXCursor_FunctionDecl) {
|
2009-09-25 04:03:06 +08:00
|
|
|
printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
|
|
|
|
curLine, curColumn);
|
2009-09-24 04:00:53 +08:00
|
|
|
PrintCursor(Ref);
|
2009-09-25 04:03:06 +08:00
|
|
|
printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
|
2009-09-24 04:00:53 +08:00
|
|
|
}
|
2009-09-24 01:52:52 +08:00
|
|
|
startBuf++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-31 22:26:51 +08:00
|
|
|
}
|
2009-08-31 08:59:03 +08:00
|
|
|
}
|
2009-08-28 23:28:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First sign of life:-)
|
|
|
|
*/
|
|
|
|
int main(int argc, char **argv) {
|
2009-09-24 04:00:53 +08:00
|
|
|
if (argc != 3) {
|
|
|
|
printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
{
|
2009-08-28 23:28:48 +08:00
|
|
|
CXIndex Idx = clang_createIndex();
|
|
|
|
CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
|
2009-09-24 04:00:53 +08:00
|
|
|
enum CXCursorKind K = CXCursor_NotImplemented;
|
2009-10-06 05:33:42 +08:00
|
|
|
|
2009-09-24 04:00:53 +08:00
|
|
|
if (!strcmp(argv[2], "all")) {
|
|
|
|
clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
|
2009-10-16 06:23:48 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
2009-09-24 04:00:53 +08:00
|
|
|
return 1;
|
2009-10-06 05:33:42 +08:00
|
|
|
}
|
2009-09-24 04:00:53 +08:00
|
|
|
/* Perform some simple filtering. */
|
|
|
|
if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
|
|
|
|
else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
|
|
|
|
else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
|
|
|
|
else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
|
|
|
|
else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
|
2009-10-06 05:33:42 +08:00
|
|
|
|
2009-09-24 04:00:53 +08:00
|
|
|
clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
|
2009-10-16 06:23:48 +08:00
|
|
|
clang_disposeTranslationUnit(TU);
|
2009-08-28 23:28:48 +08:00
|
|
|
return 1;
|
2009-09-24 04:00:53 +08:00
|
|
|
}
|
2009-08-28 23:28:48 +08:00
|
|
|
}
|