blob: 185c0597379f4713de736beadcf7d0fdc19e1c4d [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 {
Steve Naroffef0cef62009-11-09 17:45:52 +000050 CXString string;
Douglas Gregorc5d1e932010-01-19 01:20:04 +000051 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +000052 unsigned line, column;
Steve Naroffef0cef62009-11-09 17:45:52 +000053 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000054 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000055 clang_getCString(string));
56 clang_disposeString(string);
Douglas Gregorc5d1e932010-01-19 01:20:04 +000057
58 Referenced = clang_getCursorReferenced(Cursor);
59 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
60 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor1db19de2010-01-19 21:36:55 +000061 clang_getInstantiationLocation(Loc, 0, &line, &column);
62 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +000063 }
Douglas Gregorb6998662010-01-19 19:34:47 +000064
65 if (clang_isCursorDefinition(Cursor))
66 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +000067 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000068}
Steve Naroff89922f82009-08-31 00:59:03 +000069
Ted Kremenek9298cfc2009-11-17 05:31:58 +000070static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +000071 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
72 const char *source;
73 CXFile file;
74 clang_getInstantiationLocation(Loc, &file, 0, 0);
75 source = clang_getFileName(file);
Ted Kremenek9298cfc2009-11-17 05:31:58 +000076 if (!source)
77 return "<invalid loc>";
78 return basename(source);
79}
80
Ted Kremenek0d435192009-11-17 18:13:31 +000081/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +000082/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +000083/******************************************************************************/
84
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000085static const char *FileCheckPrefix = "CHECK";
86
Douglas Gregora7bde202010-01-19 00:34:46 +000087static void PrintCursorExtent(CXCursor C) {
88 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +000089 CXFile begin_file, end_file;
90 unsigned begin_line, begin_column, end_line, end_column;
91
92 clang_getInstantiationLocation(clang_getRangeStart(extent),
93 &begin_file, &begin_line, &begin_column);
94 clang_getInstantiationLocation(clang_getRangeEnd(extent),
95 &end_file, &end_line, &end_column);
96 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +000097 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +000098
99 printf(" [Extent=%d:%d:%d:%d]", begin_line, begin_column,
100 end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000101}
102
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000103/* Data used by all of the visitors. */
104typedef struct {
105 CXTranslationUnit TU;
106 enum CXCursorKind *Filter;
107} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000108
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000109
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000110enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
111 CXCursor Parent,
112 CXClientData ClientData) {
113 VisitorData *Data = (VisitorData *)ClientData;
114 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000115 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000116 unsigned line, column;
117 clang_getInstantiationLocation(Loc, 0, &line, &column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000118 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000119 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000120 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000121 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000122 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000123 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000124 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000125
126 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000127}
Steve Naroff50398192009-08-28 15:28:48 +0000128
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000129static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
130 CXCursor Parent,
131 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000132 const char *startBuf, *endBuf;
133 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
134 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000135 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000136
Douglas Gregorb6998662010-01-19 19:34:47 +0000137 if (Cursor.kind != CXCursor_FunctionDecl ||
138 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000139 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000140
141 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
142 &startLine, &startColumn,
143 &endLine, &endColumn);
144 /* Probe the entire body, looking for both decls and refs. */
145 curLine = startLine;
146 curColumn = startColumn;
147
148 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000149 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000150 CXFile file;
Douglas Gregor98258af2010-01-18 22:46:11 +0000151 const char *source = 0;
152
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000153 if (*startBuf == '\n') {
154 startBuf++;
155 curLine++;
156 curColumn = 1;
157 } else if (*startBuf != '\t')
158 curColumn++;
159
Douglas Gregor98258af2010-01-18 22:46:11 +0000160 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000161 clang_getInstantiationLocation(Loc, &file, 0, 0);
162 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000163 if (source) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000164 Ref = clang_getCursor(Data->TU, source, curLine, curColumn);
Douglas Gregor98258af2010-01-18 22:46:11 +0000165 if (Ref.kind == CXCursor_NoDeclFound) {
166 /* Nothing found here; that's fine. */
167 } else if (Ref.kind != CXCursor_FunctionDecl) {
168 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
169 curLine, curColumn);
170 PrintCursor(Ref);
171 printf("\n");
172 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000173 }
174 startBuf++;
175 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000176
177 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000178}
179
Ted Kremenek7d405622010-01-12 23:34:26 +0000180/******************************************************************************/
181/* USR testing. */
182/******************************************************************************/
183
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000184enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
185 CXClientData ClientData) {
186 VisitorData *Data = (VisitorData *)ClientData;
187 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000188 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000189 if (!USR.Spelling) {
190 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000191 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000192 }
193 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000194 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000195 printf("\n");
196 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000197
198 return CXChildVisit_Recurse;
199 }
200
201 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000202}
203
204/******************************************************************************/
205/* Loading ASTs/source. */
206/******************************************************************************/
207
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000208static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000209 const char *filter, const char *prefix,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000210 CXCursorVisitor Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000211 enum CXCursorKind K = CXCursor_NotImplemented;
212 enum CXCursorKind *ck = &K;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000213 VisitorData Data;
214
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000215 if (prefix)
216 FileCheckPrefix = prefix;
217
Ted Kremenek0d435192009-11-17 18:13:31 +0000218 /* Perform some simple filtering. */
219 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
220 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
221 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
222 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
223 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
224 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000225 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000226 else {
227 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
228 return 1;
229 }
230
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000231 Data.TU = TU;
232 Data.Filter = ck;
233 clang_visitChildren(TU, clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000234 clang_disposeTranslationUnit(TU);
235 return 0;
236}
237
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000238int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000239 const char *prefix,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000240 CXCursorVisitor Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000241 CXIndex Idx;
242 CXTranslationUnit TU;
243 Idx = clang_createIndex(/* excludeDeclsFromPCH */
244 !strcmp(filter, "local") ? 1 : 0,
245 /* displayDiagnostics */ 1);
246
247 if (!CreateTranslationUnit(Idx, file, &TU))
248 return 1;
249
Ted Kremenek98271562010-01-12 18:53:15 +0000250 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000251}
252
Ted Kremenek98271562010-01-12 18:53:15 +0000253int perform_test_load_source(int argc, const char **argv, const char *filter,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000254 CXCursorVisitor Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000255 const char *UseExternalASTs =
256 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000257 CXIndex Idx;
258 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000259 Idx = clang_createIndex(/* excludeDeclsFromPCH */
260 !strcmp(filter, "local") ? 1 : 0,
261 /* displayDiagnostics */ 1);
262
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000263 if (UseExternalASTs && strlen(UseExternalASTs))
264 clang_setUseExternalASTGeneration(Idx, 1);
265
Daniel Dunbarada487d2009-12-01 02:03:10 +0000266 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
267 if (!TU) {
268 fprintf(stderr, "Unable to load translation unit!\n");
269 return 1;
270 }
271
Ted Kremenek98271562010-01-12 18:53:15 +0000272 return perform_test_load(Idx, TU, filter, NULL, Visitor);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000273}
274
Ted Kremenek0d435192009-11-17 18:13:31 +0000275/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000276/* Logic for testing clang_getCursor(). */
277/******************************************************************************/
278
279static void print_cursor_file_scan(CXCursor cursor,
280 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000281 unsigned end_line, unsigned end_col,
282 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000283 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000284 if (prefix)
285 printf("-%s", prefix);
286 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
287 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000288 PrintCursor(cursor);
289 printf("\n");
290}
291
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000292static int perform_file_scan(const char *ast_file, const char *source_file,
293 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000294 CXIndex Idx;
295 CXTranslationUnit TU;
296 FILE *fp;
297 unsigned line;
298 CXCursor prevCursor;
299 unsigned printed;
300 unsigned start_line, start_col, last_line, last_col;
301 size_t i;
302
303 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
304 /* displayDiagnostics */ 1))) {
305 fprintf(stderr, "Could not create Index\n");
306 return 1;
307 }
308
309 if (!CreateTranslationUnit(Idx, ast_file, &TU))
310 return 1;
311
312 if ((fp = fopen(source_file, "r")) == NULL) {
313 fprintf(stderr, "Could not open '%s'\n", source_file);
314 return 1;
315 }
316
317 line = 0;
318 prevCursor = clang_getNullCursor();
319 printed = 0;
320 start_line = last_line = 1;
321 start_col = last_col = 1;
322
323 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000324 size_t len = 0;
325 int c;
326
327 while ((c = fgetc(fp)) != EOF) {
328 len++;
329 if (c == '\n')
330 break;
331 }
332
Ted Kremenek1c6da172009-11-17 19:37:36 +0000333 ++line;
334
335 for (i = 0; i < len ; ++i) {
336 CXCursor cursor;
337 cursor = clang_getCursor(TU, source_file, line, i+1);
338
339 if (!clang_equalCursors(cursor, prevCursor) &&
340 prevCursor.kind != CXCursor_InvalidFile) {
341 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000342 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000343 printed = 1;
344 start_line = line;
345 start_col = (unsigned) i+1;
346 }
347 else {
348 printed = 0;
349 }
350
351 prevCursor = cursor;
352 last_line = line;
353 last_col = (unsigned) i+1;
354 }
355 }
356
357 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
358 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000359 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000360 }
361
362 fclose(fp);
363 return 0;
364}
365
366/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000367/* Logic for testing clang_codeComplete(). */
368/******************************************************************************/
369
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000370/* Parse file:line:column from the input string. Returns 0 on success, non-zero
371 on failure. If successful, the pointer *filename will contain newly-allocated
372 memory (that will be owned by the caller) to store the file name. */
373int parse_file_line_column(const char *input, char **filename, unsigned *line,
374 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000375 /* Find the second colon. */
376 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000377 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000378 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000379 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
380 return 1;
381 }
382
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000383 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000384 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000385 if (*endptr != 0) {
386 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000387 return 1;
388 }
389
390 /* Find the first colon. */
391 first_colon = second_colon - 1;
392 while (first_colon != input && *first_colon != ':')
393 --first_colon;
394 if (first_colon == input) {
395 fprintf(stderr, "could not parse line in '%s'\n", input);
396 return 1;
397 }
398
399 /* Parse the line number. */
400 *line = strtol(first_colon + 1, &endptr, 10);
401 if (*endptr != ':') {
402 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000403 return 1;
404 }
405
Douglas Gregor88d23952009-11-09 18:19:57 +0000406 /* Copy the file name. */
407 *filename = (char*)malloc(first_colon - input + 1);
408 memcpy(*filename, input, first_colon - input);
409 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000410 return 0;
411}
412
413const char *
414clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
415 switch (Kind) {
416 case CXCompletionChunk_Optional: return "Optional";
417 case CXCompletionChunk_TypedText: return "TypedText";
418 case CXCompletionChunk_Text: return "Text";
419 case CXCompletionChunk_Placeholder: return "Placeholder";
420 case CXCompletionChunk_Informative: return "Informative";
421 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
422 case CXCompletionChunk_LeftParen: return "LeftParen";
423 case CXCompletionChunk_RightParen: return "RightParen";
424 case CXCompletionChunk_LeftBracket: return "LeftBracket";
425 case CXCompletionChunk_RightBracket: return "RightBracket";
426 case CXCompletionChunk_LeftBrace: return "LeftBrace";
427 case CXCompletionChunk_RightBrace: return "RightBrace";
428 case CXCompletionChunk_LeftAngle: return "LeftAngle";
429 case CXCompletionChunk_RightAngle: return "RightAngle";
430 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000431 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000432 case CXCompletionChunk_Colon: return "Colon";
433 case CXCompletionChunk_SemiColon: return "SemiColon";
434 case CXCompletionChunk_Equal: return "Equal";
435 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
436 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000437 }
438
439 return "Unknown";
440}
441
Douglas Gregor3ac73852009-11-09 16:04:45 +0000442void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000443 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000444
445 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000446 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000447 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000448 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000449 = clang_getCompletionChunkKind(completion_string, I);
450
451 if (Kind == CXCompletionChunk_Optional) {
452 fprintf(file, "{Optional ");
453 print_completion_string(
454 clang_getCompletionChunkCompletionString(completion_string, I),
455 file);
456 fprintf(file, "}");
457 continue;
458 }
459
Douglas Gregord5a20892009-11-09 17:05:28 +0000460 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000461 fprintf(file, "{%s %s}",
462 clang_getCompletionChunkKindSpelling(Kind),
463 text? text : "");
464 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000465}
466
467void print_completion_result(CXCompletionResult *completion_result,
468 CXClientData client_data) {
469 FILE *file = (FILE *)client_data;
470 fprintf(file, "%s:",
471 clang_getCursorKindSpelling(completion_result->CursorKind));
472 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000473 fprintf(file, "\n");
474}
475
Douglas Gregor735df882009-12-02 09:21:34 +0000476void free_remapped_files(struct CXUnsavedFile *unsaved_files,
477 int num_unsaved_files) {
478 int i;
479 for (i = 0; i != num_unsaved_files; ++i) {
480 free((char *)unsaved_files[i].Filename);
481 free((char *)unsaved_files[i].Contents);
482 }
483}
484
485int parse_remapped_files(int argc, const char **argv, int start_arg,
486 struct CXUnsavedFile **unsaved_files,
487 int *num_unsaved_files) {
488 int i;
489 int arg;
490 int prefix_len = strlen("-remap-file=");
491 *unsaved_files = 0;
492 *num_unsaved_files = 0;
493
494 /* Count the number of remapped files. */
495 for (arg = start_arg; arg < argc; ++arg) {
496 if (strncmp(argv[arg], "-remap-file=", prefix_len))
497 break;
498
499 ++*num_unsaved_files;
500 }
501
502 if (*num_unsaved_files == 0)
503 return 0;
504
505 *unsaved_files
506 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
507 *num_unsaved_files);
508 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
509 struct CXUnsavedFile *unsaved = *unsaved_files + i;
510 const char *arg_string = argv[arg] + prefix_len;
511 int filename_len;
512 char *filename;
513 char *contents;
514 FILE *to_file;
515 const char *semi = strchr(arg_string, ';');
516 if (!semi) {
517 fprintf(stderr,
518 "error: -remap-file=from;to argument is missing semicolon\n");
519 free_remapped_files(*unsaved_files, i);
520 *unsaved_files = 0;
521 *num_unsaved_files = 0;
522 return -1;
523 }
524
525 /* Open the file that we're remapping to. */
526 to_file = fopen(semi + 1, "r");
527 if (!to_file) {
528 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
529 semi + 1);
530 free_remapped_files(*unsaved_files, i);
531 *unsaved_files = 0;
532 *num_unsaved_files = 0;
533 return -1;
534 }
535
536 /* Determine the length of the file we're remapping to. */
537 fseek(to_file, 0, SEEK_END);
538 unsaved->Length = ftell(to_file);
539 fseek(to_file, 0, SEEK_SET);
540
541 /* Read the contents of the file we're remapping to. */
542 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000543 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
544 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
545 (feof(to_file) ? "EOF" : "error"), semi + 1);
546 fclose(to_file);
547 free_remapped_files(*unsaved_files, i);
548 *unsaved_files = 0;
549 *num_unsaved_files = 0;
550 return -1;
551 }
Douglas Gregor735df882009-12-02 09:21:34 +0000552 contents[unsaved->Length] = 0;
553 unsaved->Contents = contents;
554
555 /* Close the file. */
556 fclose(to_file);
557
558 /* Copy the file name that we're remapping from. */
559 filename_len = semi - arg_string;
560 filename = (char *)malloc(filename_len + 1);
561 memcpy(filename, arg_string, filename_len);
562 filename[filename_len] = 0;
563 unsaved->Filename = filename;
564 }
565
566 return 0;
567}
568
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000569int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000570 const char *input = argv[1];
571 char *filename = 0;
572 unsigned line;
573 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000574 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000575 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000576 struct CXUnsavedFile *unsaved_files = 0;
577 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000578 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000579
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000580 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000581 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
582 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000583
Douglas Gregor735df882009-12-02 09:21:34 +0000584 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
585 return -1;
586
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000587 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000588 results = clang_codeComplete(CIdx,
589 argv[argc - 1], argc - num_unsaved_files - 3,
590 argv + num_unsaved_files + 2,
591 num_unsaved_files, unsaved_files,
592 filename, line, column);
593 if (results) {
594 unsigned i, n = results->NumResults;
595 for (i = 0; i != n; ++i)
596 print_completion_result(results->Results + i, stdout);
597 clang_disposeCodeCompleteResults(results);
598 }
599
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000600 clang_disposeIndex(CIdx);
601 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000602
Douglas Gregor735df882009-12-02 09:21:34 +0000603 free_remapped_files(unsaved_files, num_unsaved_files);
604
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000605 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000606}
607
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000608typedef struct {
609 char *filename;
610 unsigned line;
611 unsigned column;
612} CursorSourceLocation;
613
614int inspect_cursor_at(int argc, const char **argv) {
615 CXIndex CIdx;
616 int errorCode;
617 struct CXUnsavedFile *unsaved_files = 0;
618 int num_unsaved_files = 0;
619 CXTranslationUnit TU;
620 CXCursor Cursor;
621 CursorSourceLocation *Locations = 0;
622 unsigned NumLocations = 0, Loc;
623
624 /* Count the number of locations. */
625 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
626 ++NumLocations;
627
628 /* Parse the locations. */
629 assert(NumLocations > 0 && "Unable to count locations?");
630 Locations = (CursorSourceLocation *)malloc(
631 NumLocations * sizeof(CursorSourceLocation));
632 for (Loc = 0; Loc < NumLocations; ++Loc) {
633 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
634 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
635 &Locations[Loc].line,
636 &Locations[Loc].column)))
637 return errorCode;
638 }
639
640 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
641 &num_unsaved_files))
642 return -1;
643
644 if (num_unsaved_files > 0) {
645 fprintf(stderr, "cannot remap files when looking for a cursor\n");
646 return -1;
647 }
648
649 CIdx = clang_createIndex(0, 1);
650 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
651 argc - num_unsaved_files - 2 - NumLocations,
652 argv + num_unsaved_files + 1 + NumLocations);
653 if (!TU) {
654 fprintf(stderr, "unable to parse input\n");
655 return -1;
656 }
657
658 for (Loc = 0; Loc < NumLocations; ++Loc) {
659 Cursor = clang_getCursor(TU, Locations[Loc].filename,
660 Locations[Loc].line, Locations[Loc].column);
661 PrintCursor(Cursor);
662 printf("\n");
663 free(Locations[Loc].filename);
664 }
665
666 clang_disposeTranslationUnit(TU);
667 clang_disposeIndex(CIdx);
668 free(Locations);
669 free_remapped_files(unsaved_files, num_unsaved_files);
670 return 0;
671}
672
Ted Kremenek0d435192009-11-17 18:13:31 +0000673/******************************************************************************/
674/* Command line processing. */
675/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000676
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000677static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000678 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000679 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000680 if (strcmp(s, "-usrs") == 0)
681 return USRVisitor;
682 return NULL;
683}
684
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000685static void print_usage(void) {
686 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000687 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000688 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000689 " c-index-test -test-file-scan <AST file> <source file> "
690 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000691 " c-index-test -test-load-tu <AST file> <symbol filter> "
692 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000693 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
694 "[FileCheck prefix]\n"
695 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000696 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n");
697 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +0000698 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000699 " all - load all symbols, including those from PCH\n"
700 " local - load all symbols except those in PCH\n"
701 " category - only load ObjC categories (non-PCH)\n"
702 " interface - only load ObjC interfaces (non-PCH)\n"
703 " protocol - only load ObjC protocols (non-PCH)\n"
704 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000705 " typedef - only load typdefs (non-PCH)\n"
706 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000707}
708
709int main(int argc, const char **argv) {
710 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
711 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000712 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
713 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000714 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000715 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +0000716 if (I)
717 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
718 }
719 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000720 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +0000721 if (I)
722 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
723 }
724 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000725 return perform_file_scan(argv[2], argv[3],
726 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000727
728 print_usage();
729 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000730}