blob: 34332d3e4088163f14a961d647650f620e885449 [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;
Steve Naroffef0cef62009-11-09 17:45:52 +000052 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);
Douglas Gregorc5d1e932010-01-19 01:20:04 +000056
57 Referenced = clang_getCursorReferenced(Cursor);
58 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
59 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
60 printf(":%d:%d", Loc.line, Loc.column);
61 }
Steve Naroff699a07d2009-09-25 21:32:34 +000062 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000063}
Steve Naroff89922f82009-08-31 00:59:03 +000064
Ted Kremenek9298cfc2009-11-17 05:31:58 +000065static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor98258af2010-01-18 22:46:11 +000066 const char *source = clang_getFileName(clang_getCursorLocation(Cursor).file);
Ted Kremenek9298cfc2009-11-17 05:31:58 +000067 if (!source)
68 return "<invalid loc>";
69 return basename(source);
70}
71
Ted Kremenek0d435192009-11-17 18:13:31 +000072/******************************************************************************/
73/* Logic for testing clang_loadTranslationUnit(). */
74/******************************************************************************/
75
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000076static const char *FileCheckPrefix = "CHECK";
77
Douglas Gregora7bde202010-01-19 00:34:46 +000078static void PrintCursorExtent(CXCursor C) {
79 CXSourceRange extent = clang_getCursorExtent(C);
80 /* FIXME: Better way to check for empty extents? */
81 if (!extent.begin.file)
Ted Kremenek70ee5422010-01-16 01:44:12 +000082 return;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000083 printf(" [Extent=%d:%d:%d:%d]", extent.begin.line, extent.begin.column,
84 extent.end.line, extent.end.column);
85}
86
Daniel Dunbar625e4ef2009-12-01 02:35:37 +000087static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
Daniel Dunbarbce6f622009-09-03 05:59:50 +000088 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +000089 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
90 const char *source = clang_getFileName(Loc.file);
91 if (!source)
92 source = "<invalid loc>";
93 printf("// %s: %s:%d:%d: ", FileCheckPrefix, source, Loc.line, Loc.column);
Douglas Gregora7bde202010-01-19 00:34:46 +000094 PrintCursor(Cursor);
95 PrintCursorExtent(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000096
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)) {
Ted Kremenek70ee5422010-01-16 01:44:12 +0000104 CXDecl D;
Douglas Gregor98258af2010-01-18 22:46:11 +0000105 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000106 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor98258af2010-01-18 22:46:11 +0000107 GetCursorSource(Cursor), Loc.line, Loc.column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000108 PrintCursor(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000109
Ted Kremenek70ee5422010-01-16 01:44:12 +0000110 D = clang_getCursorDecl(Cursor);
111 if (!D) {
112 printf("\n");
113 return;
114 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000115
Douglas Gregora7bde202010-01-19 00:34:46 +0000116 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000117 printf("\n");
118 clang_loadDeclaration(D, 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) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000139 CXSourceLocation Loc;
140 const char *source = 0;
141
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000142 if (*startBuf == '\n') {
143 startBuf++;
144 curLine++;
145 curColumn = 1;
146 } else if (*startBuf != '\t')
147 curColumn++;
148
Douglas Gregor98258af2010-01-18 22:46:11 +0000149 Loc = clang_getCursorLocation(Cursor);
150 source = clang_getFileName(Loc.file);
151 if (source) {
152 Ref = clang_getCursor(Unit, source, curLine, curColumn);
153 if (Ref.kind == CXCursor_NoDeclFound) {
154 /* Nothing found here; that's fine. */
155 } else if (Ref.kind != CXCursor_FunctionDecl) {
156 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
157 curLine, curColumn);
158 PrintCursor(Ref);
159 printf("\n");
160 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000161 }
162 startBuf++;
163 }
164}
165
Ted Kremenek7d405622010-01-12 23:34:26 +0000166/******************************************************************************/
167/* USR testing. */
168/******************************************************************************/
169
170static void USRDeclVisitor(CXDecl D, CXCursor C, CXClientData Filter) {
171 if (!Filter || (C.kind == *(enum CXCursorKind *)Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000172 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000173 if (!USR.Spelling) {
174 clang_disposeString(USR);
175 return;
176 }
177 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000178 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000179 printf("\n");
180 clang_disposeString(USR);
181 }
182}
183
184static void USRVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Ted Kremenek70ee5422010-01-16 01:44:12 +0000185 CXClientData Filter) {
186 CXDecl D = clang_getCursorDecl(Cursor);
187 if (D) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000188 /* USRDeclVisitor(Unit, Cursor.decl, Cursor, Filter);*/
Ted Kremenek70ee5422010-01-16 01:44:12 +0000189 clang_loadDeclaration(D, USRDeclVisitor, 0);
Ted Kremenek7d405622010-01-12 23:34:26 +0000190 }
191}
192
193/******************************************************************************/
194/* Loading ASTs/source. */
195/******************************************************************************/
196
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000197static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000198 const char *filter, const char *prefix,
199 CXTranslationUnitIterator Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000200 enum CXCursorKind K = CXCursor_NotImplemented;
201 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000202
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000203 if (prefix)
204 FileCheckPrefix = prefix;
205
Ted Kremenek0d435192009-11-17 18:13:31 +0000206 /* Perform some simple filtering. */
207 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
208 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
209 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
210 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
211 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
212 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000213 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000214 else {
215 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
216 return 1;
217 }
218
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000219 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000220 clang_disposeTranslationUnit(TU);
221 return 0;
222}
223
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000224int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000225 const char *prefix,
226 CXTranslationUnitIterator Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000227 CXIndex Idx;
228 CXTranslationUnit TU;
229 Idx = clang_createIndex(/* excludeDeclsFromPCH */
230 !strcmp(filter, "local") ? 1 : 0,
231 /* displayDiagnostics */ 1);
232
233 if (!CreateTranslationUnit(Idx, file, &TU))
234 return 1;
235
Ted Kremenek98271562010-01-12 18:53:15 +0000236 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000237}
238
Ted Kremenek98271562010-01-12 18:53:15 +0000239int perform_test_load_source(int argc, const char **argv, const char *filter,
240 CXTranslationUnitIterator Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000241 const char *UseExternalASTs =
242 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000243 CXIndex Idx;
244 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000245 Idx = clang_createIndex(/* excludeDeclsFromPCH */
246 !strcmp(filter, "local") ? 1 : 0,
247 /* displayDiagnostics */ 1);
248
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000249 if (UseExternalASTs && strlen(UseExternalASTs))
250 clang_setUseExternalASTGeneration(Idx, 1);
251
Daniel Dunbarada487d2009-12-01 02:03:10 +0000252 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
253 if (!TU) {
254 fprintf(stderr, "Unable to load translation unit!\n");
255 return 1;
256 }
257
Ted Kremenek98271562010-01-12 18:53:15 +0000258 return perform_test_load(Idx, TU, filter, NULL, Visitor);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000259}
260
Ted Kremenek0d435192009-11-17 18:13:31 +0000261/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000262/* Logic for testing clang_getCursor(). */
263/******************************************************************************/
264
265static void print_cursor_file_scan(CXCursor cursor,
266 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000267 unsigned end_line, unsigned end_col,
268 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000269 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000270 if (prefix)
271 printf("-%s", prefix);
272 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
273 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000274 PrintCursor(cursor);
275 printf("\n");
276}
277
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000278static int perform_file_scan(const char *ast_file, const char *source_file,
279 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000280 CXIndex Idx;
281 CXTranslationUnit TU;
282 FILE *fp;
283 unsigned line;
284 CXCursor prevCursor;
285 unsigned printed;
286 unsigned start_line, start_col, last_line, last_col;
287 size_t i;
288
289 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
290 /* displayDiagnostics */ 1))) {
291 fprintf(stderr, "Could not create Index\n");
292 return 1;
293 }
294
295 if (!CreateTranslationUnit(Idx, ast_file, &TU))
296 return 1;
297
298 if ((fp = fopen(source_file, "r")) == NULL) {
299 fprintf(stderr, "Could not open '%s'\n", source_file);
300 return 1;
301 }
302
303 line = 0;
304 prevCursor = clang_getNullCursor();
305 printed = 0;
306 start_line = last_line = 1;
307 start_col = last_col = 1;
308
309 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000310 size_t len = 0;
311 int c;
312
313 while ((c = fgetc(fp)) != EOF) {
314 len++;
315 if (c == '\n')
316 break;
317 }
318
Ted Kremenek1c6da172009-11-17 19:37:36 +0000319 ++line;
320
321 for (i = 0; i < len ; ++i) {
322 CXCursor cursor;
323 cursor = clang_getCursor(TU, source_file, line, i+1);
324
325 if (!clang_equalCursors(cursor, prevCursor) &&
326 prevCursor.kind != CXCursor_InvalidFile) {
327 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000328 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000329 printed = 1;
330 start_line = line;
331 start_col = (unsigned) i+1;
332 }
333 else {
334 printed = 0;
335 }
336
337 prevCursor = cursor;
338 last_line = line;
339 last_col = (unsigned) i+1;
340 }
341 }
342
343 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
344 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000345 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000346 }
347
348 fclose(fp);
349 return 0;
350}
351
352/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000353/* Logic for testing clang_codeComplete(). */
354/******************************************************************************/
355
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000356/* Parse file:line:column from the input string. Returns 0 on success, non-zero
357 on failure. If successful, the pointer *filename will contain newly-allocated
358 memory (that will be owned by the caller) to store the file name. */
359int parse_file_line_column(const char *input, char **filename, unsigned *line,
360 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000361 /* Find the second colon. */
362 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000363 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000364 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000365 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
366 return 1;
367 }
368
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000369 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000370 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000371 if (*endptr != 0) {
372 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000373 return 1;
374 }
375
376 /* Find the first colon. */
377 first_colon = second_colon - 1;
378 while (first_colon != input && *first_colon != ':')
379 --first_colon;
380 if (first_colon == input) {
381 fprintf(stderr, "could not parse line in '%s'\n", input);
382 return 1;
383 }
384
385 /* Parse the line number. */
386 *line = strtol(first_colon + 1, &endptr, 10);
387 if (*endptr != ':') {
388 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000389 return 1;
390 }
391
Douglas Gregor88d23952009-11-09 18:19:57 +0000392 /* Copy the file name. */
393 *filename = (char*)malloc(first_colon - input + 1);
394 memcpy(*filename, input, first_colon - input);
395 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000396 return 0;
397}
398
399const char *
400clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
401 switch (Kind) {
402 case CXCompletionChunk_Optional: return "Optional";
403 case CXCompletionChunk_TypedText: return "TypedText";
404 case CXCompletionChunk_Text: return "Text";
405 case CXCompletionChunk_Placeholder: return "Placeholder";
406 case CXCompletionChunk_Informative: return "Informative";
407 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
408 case CXCompletionChunk_LeftParen: return "LeftParen";
409 case CXCompletionChunk_RightParen: return "RightParen";
410 case CXCompletionChunk_LeftBracket: return "LeftBracket";
411 case CXCompletionChunk_RightBracket: return "RightBracket";
412 case CXCompletionChunk_LeftBrace: return "LeftBrace";
413 case CXCompletionChunk_RightBrace: return "RightBrace";
414 case CXCompletionChunk_LeftAngle: return "LeftAngle";
415 case CXCompletionChunk_RightAngle: return "RightAngle";
416 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000417 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000418 case CXCompletionChunk_Colon: return "Colon";
419 case CXCompletionChunk_SemiColon: return "SemiColon";
420 case CXCompletionChunk_Equal: return "Equal";
421 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
422 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000423 }
424
425 return "Unknown";
426}
427
Douglas Gregor3ac73852009-11-09 16:04:45 +0000428void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000429 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000430
431 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000432 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000433 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000434 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000435 = clang_getCompletionChunkKind(completion_string, I);
436
437 if (Kind == CXCompletionChunk_Optional) {
438 fprintf(file, "{Optional ");
439 print_completion_string(
440 clang_getCompletionChunkCompletionString(completion_string, I),
441 file);
442 fprintf(file, "}");
443 continue;
444 }
445
Douglas Gregord5a20892009-11-09 17:05:28 +0000446 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000447 fprintf(file, "{%s %s}",
448 clang_getCompletionChunkKindSpelling(Kind),
449 text? text : "");
450 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000451}
452
453void print_completion_result(CXCompletionResult *completion_result,
454 CXClientData client_data) {
455 FILE *file = (FILE *)client_data;
456 fprintf(file, "%s:",
457 clang_getCursorKindSpelling(completion_result->CursorKind));
458 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000459 fprintf(file, "\n");
460}
461
Douglas Gregor735df882009-12-02 09:21:34 +0000462void free_remapped_files(struct CXUnsavedFile *unsaved_files,
463 int num_unsaved_files) {
464 int i;
465 for (i = 0; i != num_unsaved_files; ++i) {
466 free((char *)unsaved_files[i].Filename);
467 free((char *)unsaved_files[i].Contents);
468 }
469}
470
471int parse_remapped_files(int argc, const char **argv, int start_arg,
472 struct CXUnsavedFile **unsaved_files,
473 int *num_unsaved_files) {
474 int i;
475 int arg;
476 int prefix_len = strlen("-remap-file=");
477 *unsaved_files = 0;
478 *num_unsaved_files = 0;
479
480 /* Count the number of remapped files. */
481 for (arg = start_arg; arg < argc; ++arg) {
482 if (strncmp(argv[arg], "-remap-file=", prefix_len))
483 break;
484
485 ++*num_unsaved_files;
486 }
487
488 if (*num_unsaved_files == 0)
489 return 0;
490
491 *unsaved_files
492 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
493 *num_unsaved_files);
494 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
495 struct CXUnsavedFile *unsaved = *unsaved_files + i;
496 const char *arg_string = argv[arg] + prefix_len;
497 int filename_len;
498 char *filename;
499 char *contents;
500 FILE *to_file;
501 const char *semi = strchr(arg_string, ';');
502 if (!semi) {
503 fprintf(stderr,
504 "error: -remap-file=from;to argument is missing semicolon\n");
505 free_remapped_files(*unsaved_files, i);
506 *unsaved_files = 0;
507 *num_unsaved_files = 0;
508 return -1;
509 }
510
511 /* Open the file that we're remapping to. */
512 to_file = fopen(semi + 1, "r");
513 if (!to_file) {
514 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
515 semi + 1);
516 free_remapped_files(*unsaved_files, i);
517 *unsaved_files = 0;
518 *num_unsaved_files = 0;
519 return -1;
520 }
521
522 /* Determine the length of the file we're remapping to. */
523 fseek(to_file, 0, SEEK_END);
524 unsaved->Length = ftell(to_file);
525 fseek(to_file, 0, SEEK_SET);
526
527 /* Read the contents of the file we're remapping to. */
528 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000529 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
530 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
531 (feof(to_file) ? "EOF" : "error"), semi + 1);
532 fclose(to_file);
533 free_remapped_files(*unsaved_files, i);
534 *unsaved_files = 0;
535 *num_unsaved_files = 0;
536 return -1;
537 }
Douglas Gregor735df882009-12-02 09:21:34 +0000538 contents[unsaved->Length] = 0;
539 unsaved->Contents = contents;
540
541 /* Close the file. */
542 fclose(to_file);
543
544 /* Copy the file name that we're remapping from. */
545 filename_len = semi - arg_string;
546 filename = (char *)malloc(filename_len + 1);
547 memcpy(filename, arg_string, filename_len);
548 filename[filename_len] = 0;
549 unsaved->Filename = filename;
550 }
551
552 return 0;
553}
554
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000555int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000556 const char *input = argv[1];
557 char *filename = 0;
558 unsigned line;
559 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000560 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000561 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000562 struct CXUnsavedFile *unsaved_files = 0;
563 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000564 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000565
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000566 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000567 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
568 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000569
Douglas Gregor735df882009-12-02 09:21:34 +0000570 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
571 return -1;
572
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000573 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000574 results = clang_codeComplete(CIdx,
575 argv[argc - 1], argc - num_unsaved_files - 3,
576 argv + num_unsaved_files + 2,
577 num_unsaved_files, unsaved_files,
578 filename, line, column);
579 if (results) {
580 unsigned i, n = results->NumResults;
581 for (i = 0; i != n; ++i)
582 print_completion_result(results->Results + i, stdout);
583 clang_disposeCodeCompleteResults(results);
584 }
585
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000586 clang_disposeIndex(CIdx);
587 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000588
Douglas Gregor735df882009-12-02 09:21:34 +0000589 free_remapped_files(unsaved_files, num_unsaved_files);
590
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000591 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000592}
593
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000594typedef struct {
595 char *filename;
596 unsigned line;
597 unsigned column;
598} CursorSourceLocation;
599
600int inspect_cursor_at(int argc, const char **argv) {
601 CXIndex CIdx;
602 int errorCode;
603 struct CXUnsavedFile *unsaved_files = 0;
604 int num_unsaved_files = 0;
605 CXTranslationUnit TU;
606 CXCursor Cursor;
607 CursorSourceLocation *Locations = 0;
608 unsigned NumLocations = 0, Loc;
609
610 /* Count the number of locations. */
611 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
612 ++NumLocations;
613
614 /* Parse the locations. */
615 assert(NumLocations > 0 && "Unable to count locations?");
616 Locations = (CursorSourceLocation *)malloc(
617 NumLocations * sizeof(CursorSourceLocation));
618 for (Loc = 0; Loc < NumLocations; ++Loc) {
619 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
620 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
621 &Locations[Loc].line,
622 &Locations[Loc].column)))
623 return errorCode;
624 }
625
626 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
627 &num_unsaved_files))
628 return -1;
629
630 if (num_unsaved_files > 0) {
631 fprintf(stderr, "cannot remap files when looking for a cursor\n");
632 return -1;
633 }
634
635 CIdx = clang_createIndex(0, 1);
636 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
637 argc - num_unsaved_files - 2 - NumLocations,
638 argv + num_unsaved_files + 1 + NumLocations);
639 if (!TU) {
640 fprintf(stderr, "unable to parse input\n");
641 return -1;
642 }
643
644 for (Loc = 0; Loc < NumLocations; ++Loc) {
645 Cursor = clang_getCursor(TU, Locations[Loc].filename,
646 Locations[Loc].line, Locations[Loc].column);
647 PrintCursor(Cursor);
648 printf("\n");
649 free(Locations[Loc].filename);
650 }
651
652 clang_disposeTranslationUnit(TU);
653 clang_disposeIndex(CIdx);
654 free(Locations);
655 free_remapped_files(unsaved_files, num_unsaved_files);
656 return 0;
657}
658
Ted Kremenek0d435192009-11-17 18:13:31 +0000659/******************************************************************************/
660/* Command line processing. */
661/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000662
Ted Kremenek7d405622010-01-12 23:34:26 +0000663static CXTranslationUnitIterator GetVisitor(const char *s) {
664 if (s[0] == '\0')
665 return TranslationUnitVisitor;
666 if (strcmp(s, "-usrs") == 0)
667 return USRVisitor;
668 return NULL;
669}
670
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000671static void print_usage(void) {
672 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000673 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000674 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000675 " c-index-test -test-file-scan <AST file> <source file> "
676 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000677 " c-index-test -test-load-tu <AST file> <symbol filter> "
678 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000679 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
680 "[FileCheck prefix]\n"
681 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000682 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n");
683 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +0000684 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000685 " all - load all symbols, including those from PCH\n"
686 " local - load all symbols except those in PCH\n"
687 " category - only load ObjC categories (non-PCH)\n"
688 " interface - only load ObjC interfaces (non-PCH)\n"
689 " protocol - only load ObjC protocols (non-PCH)\n"
690 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000691 " typedef - only load typdefs (non-PCH)\n"
692 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000693}
694
695int main(int argc, const char **argv) {
696 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
697 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000698 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
699 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000700 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
701 CXTranslationUnitIterator I = GetVisitor(argv[1] + 13);
702 if (I)
703 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
704 }
705 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
706 CXTranslationUnitIterator I = GetVisitor(argv[1] + 17);
707 if (I)
708 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
709 }
710 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000711 return perform_file_scan(argv[2], argv[3],
712 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000713
714 print_usage();
715 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000716}