blob: 727ad66d57406b62ac694ee2af9b79ddbf63f21d [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>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00007#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00008
Ted Kremenek0d435192009-11-17 18:13:31 +00009/******************************************************************************/
10/* Utility functions. */
11/******************************************************************************/
12
John Thompson2e06fc82009-10-27 13:42:56 +000013#ifdef _MSC_VER
14char *basename(const char* path)
15{
16 char* base1 = (char*)strrchr(path, '/');
17 char* base2 = (char*)strrchr(path, '\\');
18 if (base1 && base2)
19 return((base1 > base2) ? base1 + 1 : base2 + 1);
20 else if (base1)
21 return(base1 + 1);
22 else if (base2)
23 return(base2 + 1);
24
25 return((char*)path);
26}
27#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000028extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000029#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000030
Ted Kremenek1c6da172009-11-17 19:37:36 +000031static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
32 CXTranslationUnit *TU) {
33
34 *TU = clang_createTranslationUnit(Idx, file);
35 if (!TU) {
36 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
37 return 0;
38 }
39 return 1;
40}
41
Ted Kremenek0d435192009-11-17 18:13:31 +000042/******************************************************************************/
43/* Pretty-printing. */
44/******************************************************************************/
45
Steve Naroffaf08ddc2009-09-03 15:49:00 +000046static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000047 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +000048 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000049 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000050 CXDecl DeclReferenced;
Steve Naroffef0cef62009-11-09 17:45:52 +000051 CXString string;
52 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000053 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000054 clang_getCString(string));
55 clang_disposeString(string);
Eric Christopherf393c3b2009-10-05 21:33:42 +000056 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000057 if (DeclReferenced)
58 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
59 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000060 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000061}
Steve Naroff89922f82009-08-31 00:59:03 +000062
Ted Kremenek9298cfc2009-11-17 05:31:58 +000063static const char* GetCursorSource(CXCursor Cursor) {
64 const char *source = clang_getCursorSource(Cursor);
65 if (!source)
66 return "<invalid loc>";
67 return basename(source);
68}
69
Ted Kremenek0d435192009-11-17 18:13:31 +000070/******************************************************************************/
71/* Logic for testing clang_loadTranslationUnit(). */
72/******************************************************************************/
73
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000074static const char *FileCheckPrefix = "CHECK";
75
76static void PrintDeclExtent(CXDecl Dcl) {
77 CXSourceExtent extent = clang_getDeclExtent(Dcl);
78 printf(" [Extent=%d:%d:%d:%d]", extent.begin.line, extent.begin.column,
79 extent.end.line, extent.end.column);
80}
81
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000082static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
Daniel Dunbarbce6f622009-09-03 05:59:50 +000083 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000084 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000085 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
86 GetCursorSource(Cursor),
87 clang_getCursorLine(Cursor),
88 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000089 PrintCursor(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000090
Steve Naroffef0cef62009-11-09 17:45:52 +000091 string = clang_getDeclSpelling(Dcl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000092 printf(" [Context=%s]", clang_getCString(string));
Steve Naroffef0cef62009-11-09 17:45:52 +000093 clang_disposeString(string);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000094
95 PrintDeclExtent(clang_getCursorDecl(Cursor));
96
97 printf("\n");
Daniel Dunbarbce6f622009-09-03 05:59:50 +000098 }
Steve Naroffc857ea42009-09-02 13:28:54 +000099}
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000100
Steve Naroffc857ea42009-09-02 13:28:54 +0000101static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000102 CXClientData Filter) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000103 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +0000104 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000105 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
106 GetCursorSource(Cursor), clang_getCursorLine(Cursor),
107 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000108 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +0000109 string = clang_getTranslationUnitSpelling(Unit);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000110 printf(" [Context=%s]",
Steve Naroffef0cef62009-11-09 17:45:52 +0000111 basename(clang_getCString(string)));
112 clang_disposeString(string);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000113
114 PrintDeclExtent(Cursor.decl);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000115
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000116 printf("\n");
117
Steve Naroffff9e18c2009-09-24 20:03:06 +0000118 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000119 }
Steve Naroff89922f82009-08-31 00:59:03 +0000120}
Steve Naroff50398192009-08-28 15:28:48 +0000121
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000122static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
123 CXClientData Filter) {
124 const char *startBuf, *endBuf;
125 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
126 CXCursor Ref;
127
128 if (Cursor.kind != CXCursor_FunctionDefn)
129 return;
130
131 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
132 &startLine, &startColumn,
133 &endLine, &endColumn);
134 /* Probe the entire body, looking for both decls and refs. */
135 curLine = startLine;
136 curColumn = startColumn;
137
138 while (startBuf < endBuf) {
139 if (*startBuf == '\n') {
140 startBuf++;
141 curLine++;
142 curColumn = 1;
143 } else if (*startBuf != '\t')
144 curColumn++;
145
146 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
147 curLine, curColumn);
148 if (Ref.kind == CXCursor_NoDeclFound) {
149 /* Nothing found here; that's fine. */
150 } else if (Ref.kind != CXCursor_FunctionDecl) {
151 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000152 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000153 curLine, curColumn);
154 PrintCursor(Ref);
155 string = clang_getDeclSpelling(Ref.decl);
156 printf(" [Context:%s]\n", clang_getCString(string));
157 clang_disposeString(string);
158 }
159 startBuf++;
160 }
161}
162
Ted Kremenek7d405622010-01-12 23:34:26 +0000163/******************************************************************************/
164/* USR testing. */
165/******************************************************************************/
166
167static void USRDeclVisitor(CXDecl D, CXCursor C, CXClientData Filter) {
168 if (!Filter || (C.kind == *(enum CXCursorKind *)Filter)) {
169 CXString USR = clang_getDeclUSR(C.decl);
170 if (!USR.Spelling) {
171 clang_disposeString(USR);
172 return;
173 }
174 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
175 PrintDeclExtent(C.decl);
176 printf("\n");
177 clang_disposeString(USR);
178 }
179}
180
181static void USRVisitor(CXTranslationUnit Unit, CXCursor Cursor,
182 CXClientData Filter) {
183 if (Cursor.decl) {
184 /* USRDeclVisitor(Unit, Cursor.decl, Cursor, Filter);*/
185 clang_loadDeclaration(Cursor.decl, USRDeclVisitor, 0);
186 }
187}
188
189/******************************************************************************/
190/* Loading ASTs/source. */
191/******************************************************************************/
192
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000193static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000194 const char *filter, const char *prefix,
195 CXTranslationUnitIterator Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000196 enum CXCursorKind K = CXCursor_NotImplemented;
197 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000198
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000199 if (prefix)
200 FileCheckPrefix = prefix;
201
Ted Kremenek0d435192009-11-17 18:13:31 +0000202 /* Perform some simple filtering. */
203 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
204 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
205 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
206 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
207 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
208 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000209 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000210 else {
211 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
212 return 1;
213 }
214
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000215 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000216 clang_disposeTranslationUnit(TU);
217 return 0;
218}
219
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000220int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000221 const char *prefix,
222 CXTranslationUnitIterator Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000223 CXIndex Idx;
224 CXTranslationUnit TU;
225 Idx = clang_createIndex(/* excludeDeclsFromPCH */
226 !strcmp(filter, "local") ? 1 : 0,
227 /* displayDiagnostics */ 1);
228
229 if (!CreateTranslationUnit(Idx, file, &TU))
230 return 1;
231
Ted Kremenek98271562010-01-12 18:53:15 +0000232 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000233}
234
Ted Kremenek98271562010-01-12 18:53:15 +0000235int perform_test_load_source(int argc, const char **argv, const char *filter,
236 CXTranslationUnitIterator Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000237 const char *UseExternalASTs =
238 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000239 CXIndex Idx;
240 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000241 Idx = clang_createIndex(/* excludeDeclsFromPCH */
242 !strcmp(filter, "local") ? 1 : 0,
243 /* displayDiagnostics */ 1);
244
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000245 if (UseExternalASTs && strlen(UseExternalASTs))
246 clang_setUseExternalASTGeneration(Idx, 1);
247
Daniel Dunbarada487d2009-12-01 02:03:10 +0000248 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
249 if (!TU) {
250 fprintf(stderr, "Unable to load translation unit!\n");
251 return 1;
252 }
253
Ted Kremenek98271562010-01-12 18:53:15 +0000254 return perform_test_load(Idx, TU, filter, NULL, Visitor);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000255}
256
Ted Kremenek0d435192009-11-17 18:13:31 +0000257/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000258/* Logic for testing clang_getCursor(). */
259/******************************************************************************/
260
261static void print_cursor_file_scan(CXCursor cursor,
262 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000263 unsigned end_line, unsigned end_col,
264 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000265 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000266 if (prefix)
267 printf("-%s", prefix);
268 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
269 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000270 PrintCursor(cursor);
271 printf("\n");
272}
273
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000274static int perform_file_scan(const char *ast_file, const char *source_file,
275 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000276 CXIndex Idx;
277 CXTranslationUnit TU;
278 FILE *fp;
279 unsigned line;
280 CXCursor prevCursor;
281 unsigned printed;
282 unsigned start_line, start_col, last_line, last_col;
283 size_t i;
284
285 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
286 /* displayDiagnostics */ 1))) {
287 fprintf(stderr, "Could not create Index\n");
288 return 1;
289 }
290
291 if (!CreateTranslationUnit(Idx, ast_file, &TU))
292 return 1;
293
294 if ((fp = fopen(source_file, "r")) == NULL) {
295 fprintf(stderr, "Could not open '%s'\n", source_file);
296 return 1;
297 }
298
299 line = 0;
300 prevCursor = clang_getNullCursor();
301 printed = 0;
302 start_line = last_line = 1;
303 start_col = last_col = 1;
304
305 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000306 size_t len = 0;
307 int c;
308
309 while ((c = fgetc(fp)) != EOF) {
310 len++;
311 if (c == '\n')
312 break;
313 }
314
Ted Kremenek1c6da172009-11-17 19:37:36 +0000315 ++line;
316
317 for (i = 0; i < len ; ++i) {
318 CXCursor cursor;
319 cursor = clang_getCursor(TU, source_file, line, i+1);
320
321 if (!clang_equalCursors(cursor, prevCursor) &&
322 prevCursor.kind != CXCursor_InvalidFile) {
323 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000324 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000325 printed = 1;
326 start_line = line;
327 start_col = (unsigned) i+1;
328 }
329 else {
330 printed = 0;
331 }
332
333 prevCursor = cursor;
334 last_line = line;
335 last_col = (unsigned) i+1;
336 }
337 }
338
339 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
340 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000341 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000342 }
343
344 fclose(fp);
345 return 0;
346}
347
348/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000349/* Logic for testing clang_codeComplete(). */
350/******************************************************************************/
351
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000352/* Parse file:line:column from the input string. Returns 0 on success, non-zero
353 on failure. If successful, the pointer *filename will contain newly-allocated
354 memory (that will be owned by the caller) to store the file name. */
355int parse_file_line_column(const char *input, char **filename, unsigned *line,
356 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000357 /* Find the second colon. */
358 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000359 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000360 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000361 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
362 return 1;
363 }
364
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000365 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000366 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000367 if (*endptr != 0) {
368 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000369 return 1;
370 }
371
372 /* Find the first colon. */
373 first_colon = second_colon - 1;
374 while (first_colon != input && *first_colon != ':')
375 --first_colon;
376 if (first_colon == input) {
377 fprintf(stderr, "could not parse line in '%s'\n", input);
378 return 1;
379 }
380
381 /* Parse the line number. */
382 *line = strtol(first_colon + 1, &endptr, 10);
383 if (*endptr != ':') {
384 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000385 return 1;
386 }
387
Douglas Gregor88d23952009-11-09 18:19:57 +0000388 /* Copy the file name. */
389 *filename = (char*)malloc(first_colon - input + 1);
390 memcpy(*filename, input, first_colon - input);
391 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000392 return 0;
393}
394
395const char *
396clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
397 switch (Kind) {
398 case CXCompletionChunk_Optional: return "Optional";
399 case CXCompletionChunk_TypedText: return "TypedText";
400 case CXCompletionChunk_Text: return "Text";
401 case CXCompletionChunk_Placeholder: return "Placeholder";
402 case CXCompletionChunk_Informative: return "Informative";
403 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
404 case CXCompletionChunk_LeftParen: return "LeftParen";
405 case CXCompletionChunk_RightParen: return "RightParen";
406 case CXCompletionChunk_LeftBracket: return "LeftBracket";
407 case CXCompletionChunk_RightBracket: return "RightBracket";
408 case CXCompletionChunk_LeftBrace: return "LeftBrace";
409 case CXCompletionChunk_RightBrace: return "RightBrace";
410 case CXCompletionChunk_LeftAngle: return "LeftAngle";
411 case CXCompletionChunk_RightAngle: return "RightAngle";
412 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000413 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000414 case CXCompletionChunk_Colon: return "Colon";
415 case CXCompletionChunk_SemiColon: return "SemiColon";
416 case CXCompletionChunk_Equal: return "Equal";
417 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
418 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000419 }
420
421 return "Unknown";
422}
423
Douglas Gregor3ac73852009-11-09 16:04:45 +0000424void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000425 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000426
427 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000428 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000429 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000430 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000431 = clang_getCompletionChunkKind(completion_string, I);
432
433 if (Kind == CXCompletionChunk_Optional) {
434 fprintf(file, "{Optional ");
435 print_completion_string(
436 clang_getCompletionChunkCompletionString(completion_string, I),
437 file);
438 fprintf(file, "}");
439 continue;
440 }
441
Douglas Gregord5a20892009-11-09 17:05:28 +0000442 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000443 fprintf(file, "{%s %s}",
444 clang_getCompletionChunkKindSpelling(Kind),
445 text? text : "");
446 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000447}
448
449void print_completion_result(CXCompletionResult *completion_result,
450 CXClientData client_data) {
451 FILE *file = (FILE *)client_data;
452 fprintf(file, "%s:",
453 clang_getCursorKindSpelling(completion_result->CursorKind));
454 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000455 fprintf(file, "\n");
456}
457
Douglas Gregor735df882009-12-02 09:21:34 +0000458void free_remapped_files(struct CXUnsavedFile *unsaved_files,
459 int num_unsaved_files) {
460 int i;
461 for (i = 0; i != num_unsaved_files; ++i) {
462 free((char *)unsaved_files[i].Filename);
463 free((char *)unsaved_files[i].Contents);
464 }
465}
466
467int parse_remapped_files(int argc, const char **argv, int start_arg,
468 struct CXUnsavedFile **unsaved_files,
469 int *num_unsaved_files) {
470 int i;
471 int arg;
472 int prefix_len = strlen("-remap-file=");
473 *unsaved_files = 0;
474 *num_unsaved_files = 0;
475
476 /* Count the number of remapped files. */
477 for (arg = start_arg; arg < argc; ++arg) {
478 if (strncmp(argv[arg], "-remap-file=", prefix_len))
479 break;
480
481 ++*num_unsaved_files;
482 }
483
484 if (*num_unsaved_files == 0)
485 return 0;
486
487 *unsaved_files
488 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
489 *num_unsaved_files);
490 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
491 struct CXUnsavedFile *unsaved = *unsaved_files + i;
492 const char *arg_string = argv[arg] + prefix_len;
493 int filename_len;
494 char *filename;
495 char *contents;
496 FILE *to_file;
497 const char *semi = strchr(arg_string, ';');
498 if (!semi) {
499 fprintf(stderr,
500 "error: -remap-file=from;to argument is missing semicolon\n");
501 free_remapped_files(*unsaved_files, i);
502 *unsaved_files = 0;
503 *num_unsaved_files = 0;
504 return -1;
505 }
506
507 /* Open the file that we're remapping to. */
508 to_file = fopen(semi + 1, "r");
509 if (!to_file) {
510 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
511 semi + 1);
512 free_remapped_files(*unsaved_files, i);
513 *unsaved_files = 0;
514 *num_unsaved_files = 0;
515 return -1;
516 }
517
518 /* Determine the length of the file we're remapping to. */
519 fseek(to_file, 0, SEEK_END);
520 unsaved->Length = ftell(to_file);
521 fseek(to_file, 0, SEEK_SET);
522
523 /* Read the contents of the file we're remapping to. */
524 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000525 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
526 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
527 (feof(to_file) ? "EOF" : "error"), semi + 1);
528 fclose(to_file);
529 free_remapped_files(*unsaved_files, i);
530 *unsaved_files = 0;
531 *num_unsaved_files = 0;
532 return -1;
533 }
Douglas Gregor735df882009-12-02 09:21:34 +0000534 contents[unsaved->Length] = 0;
535 unsaved->Contents = contents;
536
537 /* Close the file. */
538 fclose(to_file);
539
540 /* Copy the file name that we're remapping from. */
541 filename_len = semi - arg_string;
542 filename = (char *)malloc(filename_len + 1);
543 memcpy(filename, arg_string, filename_len);
544 filename[filename_len] = 0;
545 unsaved->Filename = filename;
546 }
547
548 return 0;
549}
550
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000551int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000552 const char *input = argv[1];
553 char *filename = 0;
554 unsigned line;
555 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000556 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000557 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000558 struct CXUnsavedFile *unsaved_files = 0;
559 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000560 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000561
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000562 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000563 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
564 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000565
Douglas Gregor735df882009-12-02 09:21:34 +0000566 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
567 return -1;
568
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000569 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000570 results = clang_codeComplete(CIdx,
571 argv[argc - 1], argc - num_unsaved_files - 3,
572 argv + num_unsaved_files + 2,
573 num_unsaved_files, unsaved_files,
574 filename, line, column);
575 if (results) {
576 unsigned i, n = results->NumResults;
577 for (i = 0; i != n; ++i)
578 print_completion_result(results->Results + i, stdout);
579 clang_disposeCodeCompleteResults(results);
580 }
581
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000582 clang_disposeIndex(CIdx);
583 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000584
Douglas Gregor735df882009-12-02 09:21:34 +0000585 free_remapped_files(unsaved_files, num_unsaved_files);
586
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000587 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000588}
589
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000590typedef struct {
591 char *filename;
592 unsigned line;
593 unsigned column;
594} CursorSourceLocation;
595
596int inspect_cursor_at(int argc, const char **argv) {
597 CXIndex CIdx;
598 int errorCode;
599 struct CXUnsavedFile *unsaved_files = 0;
600 int num_unsaved_files = 0;
601 CXTranslationUnit TU;
602 CXCursor Cursor;
603 CursorSourceLocation *Locations = 0;
604 unsigned NumLocations = 0, Loc;
605
606 /* Count the number of locations. */
607 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
608 ++NumLocations;
609
610 /* Parse the locations. */
611 assert(NumLocations > 0 && "Unable to count locations?");
612 Locations = (CursorSourceLocation *)malloc(
613 NumLocations * sizeof(CursorSourceLocation));
614 for (Loc = 0; Loc < NumLocations; ++Loc) {
615 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
616 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
617 &Locations[Loc].line,
618 &Locations[Loc].column)))
619 return errorCode;
620 }
621
622 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
623 &num_unsaved_files))
624 return -1;
625
626 if (num_unsaved_files > 0) {
627 fprintf(stderr, "cannot remap files when looking for a cursor\n");
628 return -1;
629 }
630
631 CIdx = clang_createIndex(0, 1);
632 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
633 argc - num_unsaved_files - 2 - NumLocations,
634 argv + num_unsaved_files + 1 + NumLocations);
635 if (!TU) {
636 fprintf(stderr, "unable to parse input\n");
637 return -1;
638 }
639
640 for (Loc = 0; Loc < NumLocations; ++Loc) {
641 Cursor = clang_getCursor(TU, Locations[Loc].filename,
642 Locations[Loc].line, Locations[Loc].column);
643 PrintCursor(Cursor);
644 printf("\n");
645 free(Locations[Loc].filename);
646 }
647
648 clang_disposeTranslationUnit(TU);
649 clang_disposeIndex(CIdx);
650 free(Locations);
651 free_remapped_files(unsaved_files, num_unsaved_files);
652 return 0;
653}
654
Ted Kremenek0d435192009-11-17 18:13:31 +0000655/******************************************************************************/
656/* Command line processing. */
657/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000658
Ted Kremenek7d405622010-01-12 23:34:26 +0000659static CXTranslationUnitIterator GetVisitor(const char *s) {
660 if (s[0] == '\0')
661 return TranslationUnitVisitor;
662 if (strcmp(s, "-usrs") == 0)
663 return USRVisitor;
664 return NULL;
665}
666
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000667static void print_usage(void) {
668 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000669 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000670 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000671 " c-index-test -test-file-scan <AST file> <source file> "
672 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000673 " c-index-test -test-load-tu <AST file> <symbol filter> "
674 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000675 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
676 "[FileCheck prefix]\n"
677 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000678 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n");
679 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +0000680 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000681 " all - load all symbols, including those from PCH\n"
682 " local - load all symbols except those in PCH\n"
683 " category - only load ObjC categories (non-PCH)\n"
684 " interface - only load ObjC interfaces (non-PCH)\n"
685 " protocol - only load ObjC protocols (non-PCH)\n"
686 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000687 " typedef - only load typdefs (non-PCH)\n"
688 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000689}
690
691int main(int argc, const char **argv) {
692 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
693 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000694 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
695 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000696 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
697 CXTranslationUnitIterator I = GetVisitor(argv[1] + 13);
698 if (I)
699 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
700 }
701 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
702 CXTranslationUnitIterator I = GetVisitor(argv[1] + 17);
703 if (I)
704 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
705 }
706 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000707 return perform_file_scan(argv[2], argv[3],
708 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000709
710 print_usage();
711 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000712}