blob: 36deb2e6d94667d7151d59d7a7015a08dc24159d [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"
Douglas Gregor0c8296d2009-11-07 00:00:49 +00004#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00005#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00006#include <string.h>
7
John Thompson2e06fc82009-10-27 13:42:56 +00008#ifdef _MSC_VER
9char *basename(const char* path)
10{
11 char* base1 = (char*)strrchr(path, '/');
12 char* base2 = (char*)strrchr(path, '\\');
13 if (base1 && base2)
14 return((base1 > base2) ? base1 + 1 : base2 + 1);
15 else if (base1)
16 return(base1 + 1);
17 else if (base2)
18 return(base2 + 1);
19
20 return((char*)path);
21}
22#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000023extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000024#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000025
Steve Naroffaf08ddc2009-09-03 15:49:00 +000026static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000027 if (clang_isInvalid(Cursor.kind))
28 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000029 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000030 CXDecl DeclReferenced;
Steve Naroffef0cef62009-11-09 17:45:52 +000031 CXString string;
32 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000033 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000034 clang_getCString(string));
35 clang_disposeString(string);
Eric Christopherf393c3b2009-10-05 21:33:42 +000036 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000037 if (DeclReferenced)
38 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
39 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000040 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000041}
Steve Naroff89922f82009-08-31 00:59:03 +000042
Eric Christopherf393c3b2009-10-05 21:33:42 +000043static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000044{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000045 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000046 CXString string;
Steve Naroffff9e18c2009-09-24 20:03:06 +000047 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
48 clang_getCursorLine(Cursor),
49 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000050 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000051 string = clang_getDeclSpelling(Dcl);
52 printf(" [Context=%s]\n", clang_getCString(string));
53 clang_disposeString(string);
Daniel Dunbarbce6f622009-09-03 05:59:50 +000054 }
Steve Naroffc857ea42009-09-02 13:28:54 +000055}
56static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherf393c3b2009-10-05 21:33:42 +000057 CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000058{
59 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000060 CXString string;
Steve Naroffff9e18c2009-09-24 20:03:06 +000061 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
62 clang_getCursorLine(Cursor),
63 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000064 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000065 string = clang_getTranslationUnitSpelling(Unit);
66 printf(" [Context=%s]\n",
67 basename(clang_getCString(string)));
68 clang_disposeString(string);
Steve Naroffff9e18c2009-09-24 20:03:06 +000069
70 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000071
Steve Naroff4ade6d62009-09-23 17:52:52 +000072 if (Cursor.kind == CXCursor_FunctionDefn) {
73 const char *startBuf, *endBuf;
74 unsigned startLine, startColumn, endLine, endColumn;
75 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
76 &startLine, &startColumn,
77 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000078 {
79 /* Probe the entire body, looking for both decls and refs. */
80 unsigned curLine = startLine, curColumn = startColumn;
81 CXCursor Ref;
Eric Christopherf393c3b2009-10-05 21:33:42 +000082
Steve Naroff6a6de8b2009-10-21 13:56:23 +000083 while (startBuf < endBuf) {
Steve Narofff7469a32009-09-23 20:00:53 +000084 if (*startBuf == '\n') {
85 startBuf++;
86 curLine++;
87 curColumn = 1;
88 } else if (*startBuf != '\t')
89 curColumn++;
Ted Kremenekfbcb2b72009-10-22 17:22:53 +000090
Steve Narofff96b5242009-10-28 20:44:47 +000091 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
92 curLine, curColumn);
Douglas Gregor02465752009-10-16 21:24:31 +000093 if (Ref.kind == CXCursor_NoDeclFound) {
Ted Kremenek8ce88a42009-10-17 06:37:16 +000094 /* Nothing found here; that's fine. */
Douglas Gregor02465752009-10-16 21:24:31 +000095 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffef0cef62009-11-09 17:45:52 +000096 CXString string;
Steve Naroffff9e18c2009-09-24 20:03:06 +000097 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
98 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000099 PrintCursor(Ref);
Steve Naroffef0cef62009-11-09 17:45:52 +0000100 string = clang_getDeclSpelling(Ref.decl);
101 printf(" [Context:%s]\n", clang_getCString(string));
102 clang_disposeString(string);
Steve Narofff7469a32009-09-23 20:00:53 +0000103 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000104 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +0000105 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000106 }
107 }
Steve Naroff2d4d6292009-08-31 14:26:51 +0000108 }
Steve Naroff89922f82009-08-31 00:59:03 +0000109}
Steve Naroff50398192009-08-28 15:28:48 +0000110
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000111/* Parse file:line:column from the input string. Returns 0 on success, non-zero
112 on failure. If successful, the pointer *filename will contain newly-allocated
113 memory (that will be owned by the caller) to store the file name. */
114int parse_file_line_column(const char *input, char **filename, unsigned *line,
115 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000116 /* Find the second colon. */
117 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000118 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000119 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000120 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
121 return 1;
122 }
123
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000124 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000125 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000126 if (*endptr != 0) {
127 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000128 return 1;
129 }
130
131 /* Find the first colon. */
132 first_colon = second_colon - 1;
133 while (first_colon != input && *first_colon != ':')
134 --first_colon;
135 if (first_colon == input) {
136 fprintf(stderr, "could not parse line in '%s'\n", input);
137 return 1;
138 }
139
140 /* Parse the line number. */
141 *line = strtol(first_colon + 1, &endptr, 10);
142 if (*endptr != ':') {
143 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000144 return 1;
145 }
146
Douglas Gregor88d23952009-11-09 18:19:57 +0000147 /* Copy the file name. */
148 *filename = (char*)malloc(first_colon - input + 1);
149 memcpy(*filename, input, first_colon - input);
150 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000151 return 0;
152}
153
154const char *
155clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
156 switch (Kind) {
157 case CXCompletionChunk_Optional: return "Optional";
158 case CXCompletionChunk_TypedText: return "TypedText";
159 case CXCompletionChunk_Text: return "Text";
160 case CXCompletionChunk_Placeholder: return "Placeholder";
161 case CXCompletionChunk_Informative: return "Informative";
162 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
163 case CXCompletionChunk_LeftParen: return "LeftParen";
164 case CXCompletionChunk_RightParen: return "RightParen";
165 case CXCompletionChunk_LeftBracket: return "LeftBracket";
166 case CXCompletionChunk_RightBracket: return "RightBracket";
167 case CXCompletionChunk_LeftBrace: return "LeftBrace";
168 case CXCompletionChunk_RightBrace: return "RightBrace";
169 case CXCompletionChunk_LeftAngle: return "LeftAngle";
170 case CXCompletionChunk_RightAngle: return "RightAngle";
171 case CXCompletionChunk_Comma: return "Comma";
172 }
173
174 return "Unknown";
175}
176
Douglas Gregor3ac73852009-11-09 16:04:45 +0000177void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000178 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000179
180 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000181 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000182 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000183 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000184 = clang_getCompletionChunkKind(completion_string, I);
185
186 if (Kind == CXCompletionChunk_Optional) {
187 fprintf(file, "{Optional ");
188 print_completion_string(
189 clang_getCompletionChunkCompletionString(completion_string, I),
190 file);
191 fprintf(file, "}");
192 continue;
193 }
194
Douglas Gregord5a20892009-11-09 17:05:28 +0000195 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000196 fprintf(file, "{%s %s}",
197 clang_getCompletionChunkKindSpelling(Kind),
198 text? text : "");
199 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000200}
201
202void print_completion_result(CXCompletionResult *completion_result,
203 CXClientData client_data) {
204 FILE *file = (FILE *)client_data;
205 fprintf(file, "%s:",
206 clang_getCursorKindSpelling(completion_result->CursorKind));
207 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000208 fprintf(file, "\n");
209}
210
211void perform_code_completion(int argc, const char **argv) {
212 const char *input = argv[1];
213 char *filename = 0;
214 unsigned line;
215 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000216 CXIndex CIdx;
217
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000218 input += strlen("-code-completion-at=");
219 if (parse_file_line_column(input, &filename, &line, &column))
220 return;
221
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000222 CIdx = clang_createIndex(0, 0);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000223 clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2,
224 filename, line, column, &print_completion_result, stdout);
225 clang_disposeIndex(CIdx);
226 free(filename);
227}
228
Steve Naroff50398192009-08-28 15:28:48 +0000229/*
230 * First sign of life:-)
231 */
232int main(int argc, char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000233 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) {
234 perform_code_completion(argc, (const char **)argv);
235 return 0;
236 }
237
238
Steve Narofff7469a32009-09-23 20:00:53 +0000239 if (argc != 3) {
240 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
241 return 0;
242 }
243 {
Ted Kremenek85f54682009-10-17 06:42:15 +0000244 CXIndex Idx;
245 CXTranslationUnit TU;
246 enum CXCursorKind K = CXCursor_NotImplemented;
247
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000248 Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(argv[2], "local") ? 1 : 0,
249 /* displayDiagnostics */ 1);
Ted Kremenek85f54682009-10-17 06:42:15 +0000250
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000251 TU = clang_createTranslationUnit(Idx, argv[1]);
Ted Kremenek85f54682009-10-17 06:42:15 +0000252
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000253 if (!TU) {
254 fprintf(stderr, "Unable to load translation unit!\n");
255 return 1;
256 }
Eric Christopherf393c3b2009-10-05 21:33:42 +0000257
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000258 if (!strcmp(argv[2], "all") || !strcmp(argv[2], "local")) {
Steve Narofff7469a32009-09-23 20:00:53 +0000259 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
Steve Naroffe19944c2009-10-15 22:23:48 +0000260 clang_disposeTranslationUnit(TU);
Steve Narofff7469a32009-09-23 20:00:53 +0000261 return 1;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000262 }
Steve Narofff7469a32009-09-23 20:00:53 +0000263 /* Perform some simple filtering. */
264 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
265 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
266 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
267 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
268 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000269
Steve Narofff7469a32009-09-23 20:00:53 +0000270 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroffe19944c2009-10-15 22:23:48 +0000271 clang_disposeTranslationUnit(TU);
Steve Naroff50398192009-08-28 15:28:48 +0000272 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +0000273 }
Steve Naroff50398192009-08-28 15:28:48 +0000274}