blob: afceafab2052138b920cc5059c61de3f48a87d38 [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) {
Ted Kremenek70ee5422010-01-16 01:44:12 +000077 CXSourceExtent extent;
78 if (!Dcl)
79 return;
80 extent = clang_getDeclExtent(Dcl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000081 printf(" [Extent=%d:%d:%d:%d]", extent.begin.line, extent.begin.column,
82 extent.end.line, extent.end.column);
83}
84
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000085static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
Daniel Dunbarbce6f622009-09-03 05:59:50 +000086 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000087 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000088 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
89 GetCursorSource(Cursor),
90 clang_getCursorLine(Cursor),
91 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000092 PrintCursor(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000093
Steve Naroffef0cef62009-11-09 17:45:52 +000094 string = clang_getDeclSpelling(Dcl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000095 printf(" [Context=%s]", clang_getCString(string));
Steve Naroffef0cef62009-11-09 17:45:52 +000096 clang_disposeString(string);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000097
98 PrintDeclExtent(clang_getCursorDecl(Cursor));
99
100 printf("\n");
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000101 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000102}
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000103
Steve Naroffc857ea42009-09-02 13:28:54 +0000104static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000105 CXClientData Filter) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000106 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Ted Kremenek70ee5422010-01-16 01:44:12 +0000107 CXDecl D;
Steve Naroffef0cef62009-11-09 17:45:52 +0000108 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000109 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
110 GetCursorSource(Cursor), clang_getCursorLine(Cursor),
111 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000112 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +0000113 string = clang_getTranslationUnitSpelling(Unit);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000114 printf(" [Context=%s]",
Steve Naroffef0cef62009-11-09 17:45:52 +0000115 basename(clang_getCString(string)));
116 clang_disposeString(string);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000117
Ted Kremenek70ee5422010-01-16 01:44:12 +0000118 D = clang_getCursorDecl(Cursor);
119 if (!D) {
120 printf("\n");
121 return;
122 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000123
Ted Kremenek70ee5422010-01-16 01:44:12 +0000124 PrintDeclExtent(D);
125 printf("\n");
126 clang_loadDeclaration(D, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000127 }
Steve Naroff89922f82009-08-31 00:59:03 +0000128}
Steve Naroff50398192009-08-28 15:28:48 +0000129
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000130static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
131 CXClientData Filter) {
132 const char *startBuf, *endBuf;
133 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
134 CXCursor Ref;
135
136 if (Cursor.kind != CXCursor_FunctionDefn)
137 return;
138
139 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
140 &startLine, &startColumn,
141 &endLine, &endColumn);
142 /* Probe the entire body, looking for both decls and refs. */
143 curLine = startLine;
144 curColumn = startColumn;
145
146 while (startBuf < endBuf) {
147 if (*startBuf == '\n') {
148 startBuf++;
149 curLine++;
150 curColumn = 1;
151 } else if (*startBuf != '\t')
152 curColumn++;
153
154 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
155 curLine, curColumn);
156 if (Ref.kind == CXCursor_NoDeclFound) {
157 /* Nothing found here; that's fine. */
158 } else if (Ref.kind != CXCursor_FunctionDecl) {
159 CXString string;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000160 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000161 curLine, curColumn);
162 PrintCursor(Ref);
Douglas Gregor283cae32010-01-15 21:56:13 +0000163 string = clang_getDeclSpelling(Ref.data[0]);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000164 printf(" [Context:%s]\n", clang_getCString(string));
165 clang_disposeString(string);
166 }
167 startBuf++;
168 }
169}
170
Ted Kremenek7d405622010-01-12 23:34:26 +0000171/******************************************************************************/
172/* USR testing. */
173/******************************************************************************/
174
175static void USRDeclVisitor(CXDecl D, CXCursor C, CXClientData Filter) {
176 if (!Filter || (C.kind == *(enum CXCursorKind *)Filter)) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000177 CXString USR = clang_getDeclUSR(C.data[0]);
Ted Kremenek7d405622010-01-12 23:34:26 +0000178 if (!USR.Spelling) {
179 clang_disposeString(USR);
180 return;
181 }
182 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregor283cae32010-01-15 21:56:13 +0000183 PrintDeclExtent(C.data[0]);
Ted Kremenek7d405622010-01-12 23:34:26 +0000184 printf("\n");
185 clang_disposeString(USR);
186 }
187}
188
189static void USRVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Ted Kremenek70ee5422010-01-16 01:44:12 +0000190 CXClientData Filter) {
191 CXDecl D = clang_getCursorDecl(Cursor);
192 if (D) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000193 /* USRDeclVisitor(Unit, Cursor.decl, Cursor, Filter);*/
Ted Kremenek70ee5422010-01-16 01:44:12 +0000194 clang_loadDeclaration(D, USRDeclVisitor, 0);
Ted Kremenek7d405622010-01-12 23:34:26 +0000195 }
196}
197
198/******************************************************************************/
199/* Loading ASTs/source. */
200/******************************************************************************/
201
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000202static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000203 const char *filter, const char *prefix,
204 CXTranslationUnitIterator Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000205 enum CXCursorKind K = CXCursor_NotImplemented;
206 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000207
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000208 if (prefix)
209 FileCheckPrefix = prefix;
210
Ted Kremenek0d435192009-11-17 18:13:31 +0000211 /* Perform some simple filtering. */
212 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
213 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
214 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
215 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
216 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
217 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000218 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000219 else {
220 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
221 return 1;
222 }
223
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000224 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000225 clang_disposeTranslationUnit(TU);
226 return 0;
227}
228
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000229int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000230 const char *prefix,
231 CXTranslationUnitIterator Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000232 CXIndex Idx;
233 CXTranslationUnit TU;
234 Idx = clang_createIndex(/* excludeDeclsFromPCH */
235 !strcmp(filter, "local") ? 1 : 0,
236 /* displayDiagnostics */ 1);
237
238 if (!CreateTranslationUnit(Idx, file, &TU))
239 return 1;
240
Ted Kremenek98271562010-01-12 18:53:15 +0000241 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000242}
243
Ted Kremenek98271562010-01-12 18:53:15 +0000244int perform_test_load_source(int argc, const char **argv, const char *filter,
245 CXTranslationUnitIterator Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000246 const char *UseExternalASTs =
247 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000248 CXIndex Idx;
249 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000250 Idx = clang_createIndex(/* excludeDeclsFromPCH */
251 !strcmp(filter, "local") ? 1 : 0,
252 /* displayDiagnostics */ 1);
253
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000254 if (UseExternalASTs && strlen(UseExternalASTs))
255 clang_setUseExternalASTGeneration(Idx, 1);
256
Daniel Dunbarada487d2009-12-01 02:03:10 +0000257 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
258 if (!TU) {
259 fprintf(stderr, "Unable to load translation unit!\n");
260 return 1;
261 }
262
Ted Kremenek98271562010-01-12 18:53:15 +0000263 return perform_test_load(Idx, TU, filter, NULL, Visitor);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000264}
265
Ted Kremenek0d435192009-11-17 18:13:31 +0000266/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000267/* Logic for testing clang_getCursor(). */
268/******************************************************************************/
269
270static void print_cursor_file_scan(CXCursor cursor,
271 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000272 unsigned end_line, unsigned end_col,
273 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000274 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000275 if (prefix)
276 printf("-%s", prefix);
277 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
278 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000279 PrintCursor(cursor);
280 printf("\n");
281}
282
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000283static int perform_file_scan(const char *ast_file, const char *source_file,
284 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000285 CXIndex Idx;
286 CXTranslationUnit TU;
287 FILE *fp;
288 unsigned line;
289 CXCursor prevCursor;
290 unsigned printed;
291 unsigned start_line, start_col, last_line, last_col;
292 size_t i;
293
294 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
295 /* displayDiagnostics */ 1))) {
296 fprintf(stderr, "Could not create Index\n");
297 return 1;
298 }
299
300 if (!CreateTranslationUnit(Idx, ast_file, &TU))
301 return 1;
302
303 if ((fp = fopen(source_file, "r")) == NULL) {
304 fprintf(stderr, "Could not open '%s'\n", source_file);
305 return 1;
306 }
307
308 line = 0;
309 prevCursor = clang_getNullCursor();
310 printed = 0;
311 start_line = last_line = 1;
312 start_col = last_col = 1;
313
314 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000315 size_t len = 0;
316 int c;
317
318 while ((c = fgetc(fp)) != EOF) {
319 len++;
320 if (c == '\n')
321 break;
322 }
323
Ted Kremenek1c6da172009-11-17 19:37:36 +0000324 ++line;
325
326 for (i = 0; i < len ; ++i) {
327 CXCursor cursor;
328 cursor = clang_getCursor(TU, source_file, line, i+1);
329
330 if (!clang_equalCursors(cursor, prevCursor) &&
331 prevCursor.kind != CXCursor_InvalidFile) {
332 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000333 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000334 printed = 1;
335 start_line = line;
336 start_col = (unsigned) i+1;
337 }
338 else {
339 printed = 0;
340 }
341
342 prevCursor = cursor;
343 last_line = line;
344 last_col = (unsigned) i+1;
345 }
346 }
347
348 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
349 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000350 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000351 }
352
353 fclose(fp);
354 return 0;
355}
356
357/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000358/* Logic for testing clang_codeComplete(). */
359/******************************************************************************/
360
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000361/* Parse file:line:column from the input string. Returns 0 on success, non-zero
362 on failure. If successful, the pointer *filename will contain newly-allocated
363 memory (that will be owned by the caller) to store the file name. */
364int parse_file_line_column(const char *input, char **filename, unsigned *line,
365 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000366 /* Find the second colon. */
367 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000368 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000369 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000370 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
371 return 1;
372 }
373
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000374 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000375 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000376 if (*endptr != 0) {
377 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000378 return 1;
379 }
380
381 /* Find the first colon. */
382 first_colon = second_colon - 1;
383 while (first_colon != input && *first_colon != ':')
384 --first_colon;
385 if (first_colon == input) {
386 fprintf(stderr, "could not parse line in '%s'\n", input);
387 return 1;
388 }
389
390 /* Parse the line number. */
391 *line = strtol(first_colon + 1, &endptr, 10);
392 if (*endptr != ':') {
393 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000394 return 1;
395 }
396
Douglas Gregor88d23952009-11-09 18:19:57 +0000397 /* Copy the file name. */
398 *filename = (char*)malloc(first_colon - input + 1);
399 memcpy(*filename, input, first_colon - input);
400 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000401 return 0;
402}
403
404const char *
405clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
406 switch (Kind) {
407 case CXCompletionChunk_Optional: return "Optional";
408 case CXCompletionChunk_TypedText: return "TypedText";
409 case CXCompletionChunk_Text: return "Text";
410 case CXCompletionChunk_Placeholder: return "Placeholder";
411 case CXCompletionChunk_Informative: return "Informative";
412 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
413 case CXCompletionChunk_LeftParen: return "LeftParen";
414 case CXCompletionChunk_RightParen: return "RightParen";
415 case CXCompletionChunk_LeftBracket: return "LeftBracket";
416 case CXCompletionChunk_RightBracket: return "RightBracket";
417 case CXCompletionChunk_LeftBrace: return "LeftBrace";
418 case CXCompletionChunk_RightBrace: return "RightBrace";
419 case CXCompletionChunk_LeftAngle: return "LeftAngle";
420 case CXCompletionChunk_RightAngle: return "RightAngle";
421 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000422 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000423 case CXCompletionChunk_Colon: return "Colon";
424 case CXCompletionChunk_SemiColon: return "SemiColon";
425 case CXCompletionChunk_Equal: return "Equal";
426 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
427 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000428 }
429
430 return "Unknown";
431}
432
Douglas Gregor3ac73852009-11-09 16:04:45 +0000433void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000434 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000435
436 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000437 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000438 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000439 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000440 = clang_getCompletionChunkKind(completion_string, I);
441
442 if (Kind == CXCompletionChunk_Optional) {
443 fprintf(file, "{Optional ");
444 print_completion_string(
445 clang_getCompletionChunkCompletionString(completion_string, I),
446 file);
447 fprintf(file, "}");
448 continue;
449 }
450
Douglas Gregord5a20892009-11-09 17:05:28 +0000451 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000452 fprintf(file, "{%s %s}",
453 clang_getCompletionChunkKindSpelling(Kind),
454 text? text : "");
455 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000456}
457
458void print_completion_result(CXCompletionResult *completion_result,
459 CXClientData client_data) {
460 FILE *file = (FILE *)client_data;
461 fprintf(file, "%s:",
462 clang_getCursorKindSpelling(completion_result->CursorKind));
463 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000464 fprintf(file, "\n");
465}
466
Douglas Gregor735df882009-12-02 09:21:34 +0000467void free_remapped_files(struct CXUnsavedFile *unsaved_files,
468 int num_unsaved_files) {
469 int i;
470 for (i = 0; i != num_unsaved_files; ++i) {
471 free((char *)unsaved_files[i].Filename);
472 free((char *)unsaved_files[i].Contents);
473 }
474}
475
476int parse_remapped_files(int argc, const char **argv, int start_arg,
477 struct CXUnsavedFile **unsaved_files,
478 int *num_unsaved_files) {
479 int i;
480 int arg;
481 int prefix_len = strlen("-remap-file=");
482 *unsaved_files = 0;
483 *num_unsaved_files = 0;
484
485 /* Count the number of remapped files. */
486 for (arg = start_arg; arg < argc; ++arg) {
487 if (strncmp(argv[arg], "-remap-file=", prefix_len))
488 break;
489
490 ++*num_unsaved_files;
491 }
492
493 if (*num_unsaved_files == 0)
494 return 0;
495
496 *unsaved_files
497 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
498 *num_unsaved_files);
499 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
500 struct CXUnsavedFile *unsaved = *unsaved_files + i;
501 const char *arg_string = argv[arg] + prefix_len;
502 int filename_len;
503 char *filename;
504 char *contents;
505 FILE *to_file;
506 const char *semi = strchr(arg_string, ';');
507 if (!semi) {
508 fprintf(stderr,
509 "error: -remap-file=from;to argument is missing semicolon\n");
510 free_remapped_files(*unsaved_files, i);
511 *unsaved_files = 0;
512 *num_unsaved_files = 0;
513 return -1;
514 }
515
516 /* Open the file that we're remapping to. */
517 to_file = fopen(semi + 1, "r");
518 if (!to_file) {
519 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
520 semi + 1);
521 free_remapped_files(*unsaved_files, i);
522 *unsaved_files = 0;
523 *num_unsaved_files = 0;
524 return -1;
525 }
526
527 /* Determine the length of the file we're remapping to. */
528 fseek(to_file, 0, SEEK_END);
529 unsaved->Length = ftell(to_file);
530 fseek(to_file, 0, SEEK_SET);
531
532 /* Read the contents of the file we're remapping to. */
533 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000534 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
535 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
536 (feof(to_file) ? "EOF" : "error"), semi + 1);
537 fclose(to_file);
538 free_remapped_files(*unsaved_files, i);
539 *unsaved_files = 0;
540 *num_unsaved_files = 0;
541 return -1;
542 }
Douglas Gregor735df882009-12-02 09:21:34 +0000543 contents[unsaved->Length] = 0;
544 unsaved->Contents = contents;
545
546 /* Close the file. */
547 fclose(to_file);
548
549 /* Copy the file name that we're remapping from. */
550 filename_len = semi - arg_string;
551 filename = (char *)malloc(filename_len + 1);
552 memcpy(filename, arg_string, filename_len);
553 filename[filename_len] = 0;
554 unsaved->Filename = filename;
555 }
556
557 return 0;
558}
559
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000560int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000561 const char *input = argv[1];
562 char *filename = 0;
563 unsigned line;
564 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000565 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000566 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000567 struct CXUnsavedFile *unsaved_files = 0;
568 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000569 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000570
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000571 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000572 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
573 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000574
Douglas Gregor735df882009-12-02 09:21:34 +0000575 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
576 return -1;
577
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000578 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000579 results = clang_codeComplete(CIdx,
580 argv[argc - 1], argc - num_unsaved_files - 3,
581 argv + num_unsaved_files + 2,
582 num_unsaved_files, unsaved_files,
583 filename, line, column);
584 if (results) {
585 unsigned i, n = results->NumResults;
586 for (i = 0; i != n; ++i)
587 print_completion_result(results->Results + i, stdout);
588 clang_disposeCodeCompleteResults(results);
589 }
590
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000591 clang_disposeIndex(CIdx);
592 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000593
Douglas Gregor735df882009-12-02 09:21:34 +0000594 free_remapped_files(unsaved_files, num_unsaved_files);
595
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000596 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000597}
598
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000599typedef struct {
600 char *filename;
601 unsigned line;
602 unsigned column;
603} CursorSourceLocation;
604
605int inspect_cursor_at(int argc, const char **argv) {
606 CXIndex CIdx;
607 int errorCode;
608 struct CXUnsavedFile *unsaved_files = 0;
609 int num_unsaved_files = 0;
610 CXTranslationUnit TU;
611 CXCursor Cursor;
612 CursorSourceLocation *Locations = 0;
613 unsigned NumLocations = 0, Loc;
614
615 /* Count the number of locations. */
616 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
617 ++NumLocations;
618
619 /* Parse the locations. */
620 assert(NumLocations > 0 && "Unable to count locations?");
621 Locations = (CursorSourceLocation *)malloc(
622 NumLocations * sizeof(CursorSourceLocation));
623 for (Loc = 0; Loc < NumLocations; ++Loc) {
624 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
625 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
626 &Locations[Loc].line,
627 &Locations[Loc].column)))
628 return errorCode;
629 }
630
631 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
632 &num_unsaved_files))
633 return -1;
634
635 if (num_unsaved_files > 0) {
636 fprintf(stderr, "cannot remap files when looking for a cursor\n");
637 return -1;
638 }
639
640 CIdx = clang_createIndex(0, 1);
641 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
642 argc - num_unsaved_files - 2 - NumLocations,
643 argv + num_unsaved_files + 1 + NumLocations);
644 if (!TU) {
645 fprintf(stderr, "unable to parse input\n");
646 return -1;
647 }
648
649 for (Loc = 0; Loc < NumLocations; ++Loc) {
650 Cursor = clang_getCursor(TU, Locations[Loc].filename,
651 Locations[Loc].line, Locations[Loc].column);
652 PrintCursor(Cursor);
653 printf("\n");
654 free(Locations[Loc].filename);
655 }
656
657 clang_disposeTranslationUnit(TU);
658 clang_disposeIndex(CIdx);
659 free(Locations);
660 free_remapped_files(unsaved_files, num_unsaved_files);
661 return 0;
662}
663
Ted Kremenek0d435192009-11-17 18:13:31 +0000664/******************************************************************************/
665/* Command line processing. */
666/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000667
Ted Kremenek7d405622010-01-12 23:34:26 +0000668static CXTranslationUnitIterator GetVisitor(const char *s) {
669 if (s[0] == '\0')
670 return TranslationUnitVisitor;
671 if (strcmp(s, "-usrs") == 0)
672 return USRVisitor;
673 return NULL;
674}
675
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000676static void print_usage(void) {
677 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000678 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000679 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000680 " c-index-test -test-file-scan <AST file> <source file> "
681 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000682 " c-index-test -test-load-tu <AST file> <symbol filter> "
683 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000684 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
685 "[FileCheck prefix]\n"
686 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000687 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n");
688 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +0000689 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000690 " all - load all symbols, including those from PCH\n"
691 " local - load all symbols except those in PCH\n"
692 " category - only load ObjC categories (non-PCH)\n"
693 " interface - only load ObjC interfaces (non-PCH)\n"
694 " protocol - only load ObjC protocols (non-PCH)\n"
695 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000696 " typedef - only load typdefs (non-PCH)\n"
697 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000698}
699
700int main(int argc, const char **argv) {
701 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
702 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000703 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
704 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000705 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
706 CXTranslationUnitIterator I = GetVisitor(argv[1] + 13);
707 if (I)
708 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
709 }
710 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
711 CXTranslationUnitIterator I = GetVisitor(argv[1] + 17);
712 if (I)
713 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
714 }
715 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000716 return perform_file_scan(argv[2], argv[3],
717 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000718
719 print_usage();
720 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000721}