blob: 73764950c313641d39256734909854ae3e6c2d4d [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Steve Naroff89922f82009-08-31 00:59:03 +00004#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00005#include <string.h>
6
Steve Naroffff9e18c2009-09-24 20:03:06 +00007extern char *basename(const char *);
8
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000010 if (clang_isInvalid(Cursor.kind))
11 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000012 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000013 CXDecl DeclReferenced;
Steve Naroffff9e18c2009-09-24 20:03:06 +000014 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
15 clang_getCursorSpelling(Cursor));
Eric Christopherf393c3b2009-10-05 21:33:42 +000016 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000017 if (DeclReferenced)
18 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
19 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000020 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000021}
Steve Naroff89922f82009-08-31 00:59:03 +000022
Eric Christopherf393c3b2009-10-05 21:33:42 +000023static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000024{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000025 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000026 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
27 clang_getCursorLine(Cursor),
28 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000029 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000030 printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
Daniel Dunbarbce6f622009-09-03 05:59:50 +000031 }
Steve Naroffc857ea42009-09-02 13:28:54 +000032}
33static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherf393c3b2009-10-05 21:33:42 +000034 CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000035{
36 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000037 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
38 clang_getCursorLine(Cursor),
39 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000040 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000041 printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
42
43 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000044
Steve Naroff4ade6d62009-09-23 17:52:52 +000045 if (Cursor.kind == CXCursor_FunctionDefn) {
46 const char *startBuf, *endBuf;
47 unsigned startLine, startColumn, endLine, endColumn;
48 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
49 &startLine, &startColumn,
50 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000051 {
52 /* Probe the entire body, looking for both decls and refs. */
53 unsigned curLine = startLine, curColumn = startColumn;
54 CXCursor Ref;
Eric Christopherf393c3b2009-10-05 21:33:42 +000055
Steve Narofff7469a32009-09-23 20:00:53 +000056 while (startBuf <= endBuf) {
57 if (*startBuf == '\n') {
58 startBuf++;
59 curLine++;
60 curColumn = 1;
61 } else if (*startBuf != '\t')
62 curColumn++;
Eric Christopherf393c3b2009-10-05 21:33:42 +000063
64 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
Steve Narofff7469a32009-09-23 20:00:53 +000065 curLine, curColumn);
Douglas Gregor02465752009-10-16 21:24:31 +000066 if (Ref.kind == CXCursor_NoDeclFound) {
67 // Nothing found here; that's fine.
68 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000069 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
70 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000071 PrintCursor(Ref);
Steve Naroffff9e18c2009-09-24 20:03:06 +000072 printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
Steve Narofff7469a32009-09-23 20:00:53 +000073 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000074 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +000075 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000076 }
77 }
Steve Naroff2d4d6292009-08-31 14:26:51 +000078 }
Steve Naroff89922f82009-08-31 00:59:03 +000079}
Steve Naroff50398192009-08-28 15:28:48 +000080
81/*
82 * First sign of life:-)
83 */
84int main(int argc, char **argv) {
Steve Narofff7469a32009-09-23 20:00:53 +000085 if (argc != 3) {
86 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
87 return 0;
88 }
89 {
Steve Naroff50398192009-08-28 15:28:48 +000090 CXIndex Idx = clang_createIndex();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000091 if (!strcmp(argv[2], "local"))
92 clang_wantOnlyLocalDeclarations(Idx);
Steve Naroff50398192009-08-28 15:28:48 +000093 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000094 if (!TU) {
95 fprintf(stderr, "Unable to load translation unit!\n");
96 return 1;
97 }
Steve Narofff7469a32009-09-23 20:00:53 +000098 enum CXCursorKind K = CXCursor_NotImplemented;
Eric Christopherf393c3b2009-10-05 21:33:42 +000099
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000100 if (!strcmp(argv[2], "all") || !strcmp(argv[2], "local")) {
Steve Narofff7469a32009-09-23 20:00:53 +0000101 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
Steve Naroffe19944c2009-10-15 22:23:48 +0000102 clang_disposeTranslationUnit(TU);
Steve Narofff7469a32009-09-23 20:00:53 +0000103 return 1;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000104 }
Steve Narofff7469a32009-09-23 20:00:53 +0000105 /* Perform some simple filtering. */
106 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
107 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
108 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
109 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
110 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000111
Steve Narofff7469a32009-09-23 20:00:53 +0000112 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroffe19944c2009-10-15 22:23:48 +0000113 clang_disposeTranslationUnit(TU);
Steve Naroff50398192009-08-28 15:28:48 +0000114 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +0000115 }
Steve Naroff50398192009-08-28 15:28:48 +0000116}