blob: 67e0c7724f26eedc313d4d1576b9573461b83fe0 [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 Gregor1e5e6682010-08-26 13:48:20 +00004#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00005#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00006#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00007#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00008#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009
Ted Kremenek0d435192009-11-17 18:13:31 +000010/******************************************************************************/
11/* Utility functions. */
12/******************************************************************************/
13
John Thompson2e06fc82009-10-27 13:42:56 +000014#ifdef _MSC_VER
15char *basename(const char* path)
16{
17 char* base1 = (char*)strrchr(path, '/');
18 char* base2 = (char*)strrchr(path, '\\');
19 if (base1 && base2)
20 return((base1 > base2) ? base1 + 1 : base2 + 1);
21 else if (base1)
22 return(base1 + 1);
23 else if (base2)
24 return(base2 + 1);
25
26 return((char*)path);
27}
28#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000029extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000030#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000031
Douglas Gregor45ba9a12010-07-25 17:39:21 +000032/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000033static unsigned getDefaultParsingOptions() {
34 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
35
36 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000037 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000038 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
39 options |= CXTranslationUnit_CacheCompletionResults;
Douglas Gregor44c181a2010-07-23 00:33:23 +000040
41 return options;
42}
43
Daniel Dunbar51b058c2010-02-14 08:32:24 +000044static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
45 unsigned end_line, unsigned end_column) {
46 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000047 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000048}
49
Ted Kremenek1c6da172009-11-17 19:37:36 +000050static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
51 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000052
Douglas Gregora88084b2010-02-18 18:08:43 +000053 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000054 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000055 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
56 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000057 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000058 return 1;
59}
60
Douglas Gregor4db64a42010-01-23 00:14:00 +000061void free_remapped_files(struct CXUnsavedFile *unsaved_files,
62 int num_unsaved_files) {
63 int i;
64 for (i = 0; i != num_unsaved_files; ++i) {
65 free((char *)unsaved_files[i].Filename);
66 free((char *)unsaved_files[i].Contents);
67 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000068 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000069}
70
71int parse_remapped_files(int argc, const char **argv, int start_arg,
72 struct CXUnsavedFile **unsaved_files,
73 int *num_unsaved_files) {
74 int i;
75 int arg;
76 int prefix_len = strlen("-remap-file=");
77 *unsaved_files = 0;
78 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000079
Douglas Gregor4db64a42010-01-23 00:14:00 +000080 /* Count the number of remapped files. */
81 for (arg = start_arg; arg < argc; ++arg) {
82 if (strncmp(argv[arg], "-remap-file=", prefix_len))
83 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000084
Douglas Gregor4db64a42010-01-23 00:14:00 +000085 ++*num_unsaved_files;
86 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000087
Douglas Gregor4db64a42010-01-23 00:14:00 +000088 if (*num_unsaved_files == 0)
89 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000090
Douglas Gregor4db64a42010-01-23 00:14:00 +000091 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000092 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
93 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000094 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
95 struct CXUnsavedFile *unsaved = *unsaved_files + i;
96 const char *arg_string = argv[arg] + prefix_len;
97 int filename_len;
98 char *filename;
99 char *contents;
100 FILE *to_file;
101 const char *semi = strchr(arg_string, ';');
102 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000103 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000104 "error: -remap-file=from;to argument is missing semicolon\n");
105 free_remapped_files(*unsaved_files, i);
106 *unsaved_files = 0;
107 *num_unsaved_files = 0;
108 return -1;
109 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000110
Douglas Gregor4db64a42010-01-23 00:14:00 +0000111 /* Open the file that we're remapping to. */
112 to_file = fopen(semi + 1, "r");
113 if (!to_file) {
114 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
115 semi + 1);
116 free_remapped_files(*unsaved_files, i);
117 *unsaved_files = 0;
118 *num_unsaved_files = 0;
119 return -1;
120 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000121
Douglas Gregor4db64a42010-01-23 00:14:00 +0000122 /* Determine the length of the file we're remapping to. */
123 fseek(to_file, 0, SEEK_END);
124 unsaved->Length = ftell(to_file);
125 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000126
Douglas Gregor4db64a42010-01-23 00:14:00 +0000127 /* Read the contents of the file we're remapping to. */
128 contents = (char *)malloc(unsaved->Length + 1);
129 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
130 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
131 (feof(to_file) ? "EOF" : "error"), semi + 1);
132 fclose(to_file);
133 free_remapped_files(*unsaved_files, i);
134 *unsaved_files = 0;
135 *num_unsaved_files = 0;
136 return -1;
137 }
138 contents[unsaved->Length] = 0;
139 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000140
Douglas Gregor4db64a42010-01-23 00:14:00 +0000141 /* Close the file. */
142 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000143
Douglas Gregor4db64a42010-01-23 00:14:00 +0000144 /* Copy the file name that we're remapping from. */
145 filename_len = semi - arg_string;
146 filename = (char *)malloc(filename_len + 1);
147 memcpy(filename, arg_string, filename_len);
148 filename[filename_len] = 0;
149 unsaved->Filename = filename;
150 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000151
Douglas Gregor4db64a42010-01-23 00:14:00 +0000152 return 0;
153}
154
Ted Kremenek0d435192009-11-17 18:13:31 +0000155/******************************************************************************/
156/* Pretty-printing. */
157/******************************************************************************/
158
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000159static void PrintCursor(CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000160 if (clang_isInvalid(Cursor.kind)) {
161 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
162 printf("Invalid Cursor => %s", clang_getCString(ks));
163 clang_disposeString(ks);
164 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000165 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000166 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000167 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000168 unsigned line, column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000169
170 ks = clang_getCursorKindSpelling(Cursor.kind);
Steve Naroffef0cef62009-11-09 17:45:52 +0000171 string = clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000172 printf("%s=%s", clang_getCString(ks),
173 clang_getCString(string));
174 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000175 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000176
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000177 Referenced = clang_getCursorReferenced(Cursor);
178 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
179 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000180 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000181 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000182 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000183
184 if (clang_isCursorDefinition(Cursor))
185 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000186
187 switch (clang_getCursorAvailability(Cursor)) {
188 case CXAvailability_Available:
189 break;
190
191 case CXAvailability_Deprecated:
192 printf(" (deprecated)");
193 break;
194
195 case CXAvailability_NotAvailable:
196 printf(" (unavailable)");
197 break;
198 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000199
200 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
201 CXType T =
202 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
203 CXString S = clang_getTypeKindSpelling(T.kind);
204 printf(" [IBOutletCollection=%s]", clang_getCString(S));
205 clang_disposeString(S);
206 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000207 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000208}
Steve Naroff89922f82009-08-31 00:59:03 +0000209
Ted Kremeneke68fff62010-02-17 00:41:32 +0000210static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000211 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000212 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000213 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000214 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000215 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000216 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000217 clang_disposeString(source);
218 return "<invalid loc>";
219 }
220 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000221 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000222 clang_disposeString(source);
223 return b;
224 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000225}
226
Ted Kremenek0d435192009-11-17 18:13:31 +0000227/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000228/* Callbacks. */
229/******************************************************************************/
230
231typedef void (*PostVisitTU)(CXTranslationUnit);
232
Douglas Gregora88084b2010-02-18 18:08:43 +0000233void PrintDiagnostic(CXDiagnostic Diagnostic) {
234 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000235 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000236 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000237 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
238 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
239 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000240
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000241 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000242 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000243
Douglas Gregor274f1902010-02-22 23:17:23 +0000244 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
245 fprintf(stderr, "%s\n", clang_getCString(Msg));
246 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000247
Douglas Gregor5352ac02010-01-28 00:27:43 +0000248 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000249 &file, 0, 0, 0);
250 if (!file)
251 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000252
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000253 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
254 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000255 CXSourceRange range;
256 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
257 CXSourceLocation start = clang_getRangeStart(range);
258 CXSourceLocation end = clang_getRangeEnd(range);
259 unsigned start_line, start_column, end_line, end_column;
260 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000261 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000262 &start_column, 0);
263 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
264 if (clang_equalLocations(start, end)) {
265 /* Insertion. */
266 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000267 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000268 clang_getCString(insertion_text), start_line, start_column);
269 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
270 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000271 if (start_file == file && end_file == file) {
272 fprintf(out, "FIX-IT: Remove ");
273 PrintExtent(out, start_line, start_column, end_line, end_column);
274 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000275 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000276 } else {
277 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000278 if (start_file == end_file) {
279 fprintf(out, "FIX-IT: Replace ");
280 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000281 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000282 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000283 break;
284 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000285 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000286 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000287}
288
Douglas Gregora88084b2010-02-18 18:08:43 +0000289void PrintDiagnostics(CXTranslationUnit TU) {
290 int i, n = clang_getNumDiagnostics(TU);
291 for (i = 0; i != n; ++i) {
292 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
293 PrintDiagnostic(Diag);
294 clang_disposeDiagnostic(Diag);
295 }
296}
297
Ted Kremenekce2ae882010-01-26 17:59:48 +0000298/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000299/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000300/******************************************************************************/
301
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000302static const char *FileCheckPrefix = "CHECK";
303
Douglas Gregora7bde202010-01-19 00:34:46 +0000304static void PrintCursorExtent(CXCursor C) {
305 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000306 CXFile begin_file, end_file;
307 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000308
Douglas Gregor1db19de2010-01-19 21:36:55 +0000309 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000310 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000311 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000312 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000313 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000314 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000315
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000316 printf(" Extent=");
317 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000318}
319
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000320/* Data used by all of the visitors. */
321typedef struct {
322 CXTranslationUnit TU;
323 enum CXCursorKind *Filter;
324} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000325
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000326
Ted Kremeneke68fff62010-02-17 00:41:32 +0000327enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000328 CXCursor Parent,
329 CXClientData ClientData) {
330 VisitorData *Data = (VisitorData *)ClientData;
331 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000332 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000333 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000334 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000335 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000336 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000337 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000338 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000339 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000340 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000341 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000342
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000343 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000344}
Steve Naroff50398192009-08-28 15:28:48 +0000345
Ted Kremeneke68fff62010-02-17 00:41:32 +0000346static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000347 CXCursor Parent,
348 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000349 const char *startBuf, *endBuf;
350 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
351 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000352 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000353
Douglas Gregorb6998662010-01-19 19:34:47 +0000354 if (Cursor.kind != CXCursor_FunctionDecl ||
355 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000356 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000357
358 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
359 &startLine, &startColumn,
360 &endLine, &endColumn);
361 /* Probe the entire body, looking for both decls and refs. */
362 curLine = startLine;
363 curColumn = startColumn;
364
365 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000366 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000367 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000368 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000369
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000370 if (*startBuf == '\n') {
371 startBuf++;
372 curLine++;
373 curColumn = 1;
374 } else if (*startBuf != '\t')
375 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000376
Douglas Gregor98258af2010-01-18 22:46:11 +0000377 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000378 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000379
Douglas Gregor1db19de2010-01-19 21:36:55 +0000380 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000381 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000382 CXSourceLocation RefLoc
383 = clang_getLocation(Data->TU, file, curLine, curColumn);
384 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000385 if (Ref.kind == CXCursor_NoDeclFound) {
386 /* Nothing found here; that's fine. */
387 } else if (Ref.kind != CXCursor_FunctionDecl) {
388 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
389 curLine, curColumn);
390 PrintCursor(Ref);
391 printf("\n");
392 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000393 }
Ted Kremenek74844072010-02-17 00:41:20 +0000394 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000395 startBuf++;
396 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000397
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000398 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000399}
400
Ted Kremenek7d405622010-01-12 23:34:26 +0000401/******************************************************************************/
402/* USR testing. */
403/******************************************************************************/
404
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000405enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
406 CXClientData ClientData) {
407 VisitorData *Data = (VisitorData *)ClientData;
408 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000409 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000410 const char *cstr = clang_getCString(USR);
411 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000412 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000413 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000414 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000415 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
416
Douglas Gregora7bde202010-01-19 00:34:46 +0000417 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000418 printf("\n");
419 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000420
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000421 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000422 }
423
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000424 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000425}
426
427/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000428/* Inclusion stack testing. */
429/******************************************************************************/
430
431void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
432 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000433
Ted Kremenek16b55a72010-01-26 19:31:51 +0000434 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000435 CXString fname;
436
437 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000438 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000439 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000440
Ted Kremenek16b55a72010-01-26 19:31:51 +0000441 for (i = 0; i < includeStackLen; ++i) {
442 CXFile includingFile;
443 unsigned line, column;
444 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
445 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000446 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000447 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000448 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000449 }
450 printf("\n");
451}
452
453void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000454 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000455}
456
457/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000458/* Linkage testing. */
459/******************************************************************************/
460
461static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
462 CXClientData d) {
463 const char *linkage = 0;
464
465 if (clang_isInvalid(clang_getCursorKind(cursor)))
466 return CXChildVisit_Recurse;
467
468 switch (clang_getCursorLinkage(cursor)) {
469 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000470 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
471 case CXLinkage_Internal: linkage = "Internal"; break;
472 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
473 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000474 }
475
476 if (linkage) {
477 PrintCursor(cursor);
478 printf("linkage=%s\n", linkage);
479 }
480
481 return CXChildVisit_Recurse;
482}
483
484/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000485/* Typekind testing. */
486/******************************************************************************/
487
488static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
489 CXClientData d) {
490
491 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
492 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000493 CXString S = clang_getTypeKindSpelling(T.kind);
494 PrintCursor(cursor);
495 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000496 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000497 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000498 {
499 CXType CT = clang_getCanonicalType(T);
500 if (!clang_equalTypes(T, CT)) {
501 CXString CS = clang_getTypeKindSpelling(CT.kind);
502 printf(" [canonical=%s]", clang_getCString(CS));
503 clang_disposeString(CS);
504 }
505 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000506 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000507 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000508 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000509 if (RT.kind != CXType_Invalid) {
510 CXString RS = clang_getTypeKindSpelling(RT.kind);
511 printf(" [result=%s]", clang_getCString(RS));
512 clang_disposeString(RS);
513 }
514 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000515 /* Print if this is a non-POD type. */
516 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000517
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000518 printf("\n");
519 }
520 return CXChildVisit_Recurse;
521}
522
523
524/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000525/* Loading ASTs/source. */
526/******************************************************************************/
527
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000528static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000529 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000530 CXCursorVisitor Visitor,
531 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000532
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000533 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000534 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000535
536 if (Visitor) {
537 enum CXCursorKind K = CXCursor_NotImplemented;
538 enum CXCursorKind *ck = &K;
539 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000540
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000541 /* Perform some simple filtering. */
542 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000543 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000544 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
545 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
546 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
547 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
548 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
549 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
550 else {
551 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
552 return 1;
553 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000554
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000555 Data.TU = TU;
556 Data.Filter = ck;
557 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000558 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000559
Ted Kremenekce2ae882010-01-26 17:59:48 +0000560 if (PV)
561 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000562
Douglas Gregora88084b2010-02-18 18:08:43 +0000563 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000564 clang_disposeTranslationUnit(TU);
565 return 0;
566}
567
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000568int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000569 const char *prefix, CXCursorVisitor Visitor,
570 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000571 CXIndex Idx;
572 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000573 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000574 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000575 !strcmp(filter, "local") ? 1 : 0,
576 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000577
Ted Kremenek020a0952010-02-11 07:41:25 +0000578 if (!CreateTranslationUnit(Idx, file, &TU)) {
579 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000580 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000581 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000582
Ted Kremenek020a0952010-02-11 07:41:25 +0000583 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
584 clang_disposeIndex(Idx);
585 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000586}
587
Ted Kremenekce2ae882010-01-26 17:59:48 +0000588int perform_test_load_source(int argc, const char **argv,
589 const char *filter, CXCursorVisitor Visitor,
590 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000591 const char *UseExternalASTs =
592 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000593 CXIndex Idx;
594 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000595 struct CXUnsavedFile *unsaved_files = 0;
596 int num_unsaved_files = 0;
597 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000598
Daniel Dunbarada487d2009-12-01 02:03:10 +0000599 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000600 !strcmp(filter, "local") ? 1 : 0,
601 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000602
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000603 if (UseExternalASTs && strlen(UseExternalASTs))
604 clang_setUseExternalASTGeneration(Idx, 1);
605
Ted Kremenek020a0952010-02-11 07:41:25 +0000606 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
607 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000608 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000609 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000610
Ted Kremeneke68fff62010-02-17 00:41:32 +0000611 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
612 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000613 argv + num_unsaved_files,
614 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000615 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000616 if (!TU) {
617 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000618 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000619 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000620 return 1;
621 }
622
Ted Kremenekce2ae882010-01-26 17:59:48 +0000623 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000624 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000625 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000626 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000627}
628
Douglas Gregorabc563f2010-07-19 21:46:24 +0000629int perform_test_reparse_source(int argc, const char **argv, int trials,
630 const char *filter, CXCursorVisitor Visitor,
631 PostVisitTU PV) {
632 const char *UseExternalASTs =
633 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
634 CXIndex Idx;
635 CXTranslationUnit TU;
636 struct CXUnsavedFile *unsaved_files = 0;
637 int num_unsaved_files = 0;
638 int result;
639 int trial;
640
641 Idx = clang_createIndex(/* excludeDeclsFromPCH */
642 !strcmp(filter, "local") ? 1 : 0,
643 /* displayDiagnosics=*/1);
644
645 if (UseExternalASTs && strlen(UseExternalASTs))
646 clang_setUseExternalASTGeneration(Idx, 1);
647
648 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
649 clang_disposeIndex(Idx);
650 return -1;
651 }
652
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000653 /* Load the initial translation unit -- we do this without honoring remapped
654 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000655 TU = clang_parseTranslationUnit(Idx, 0,
656 argv + num_unsaved_files,
657 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000658 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000659 if (!TU) {
660 fprintf(stderr, "Unable to load translation unit!\n");
661 free_remapped_files(unsaved_files, num_unsaved_files);
662 clang_disposeIndex(Idx);
663 return 1;
664 }
665
666 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000667 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
668 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000669 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000670 clang_disposeTranslationUnit(TU);
671 free_remapped_files(unsaved_files, num_unsaved_files);
672 clang_disposeIndex(Idx);
673 return -1;
674 }
675 }
676
677 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
678 free_remapped_files(unsaved_files, num_unsaved_files);
679 clang_disposeIndex(Idx);
680 return result;
681}
682
Ted Kremenek0d435192009-11-17 18:13:31 +0000683/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000684/* Logic for testing clang_getCursor(). */
685/******************************************************************************/
686
687static void print_cursor_file_scan(CXCursor cursor,
688 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000689 unsigned end_line, unsigned end_col,
690 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000691 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000692 if (prefix)
693 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000694 PrintExtent(stdout, start_line, start_col, end_line, end_col);
695 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000696 PrintCursor(cursor);
697 printf("\n");
698}
699
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000700static int perform_file_scan(const char *ast_file, const char *source_file,
701 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000702 CXIndex Idx;
703 CXTranslationUnit TU;
704 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000705 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000706 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000707 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000708 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000709
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000710 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
711 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000712 fprintf(stderr, "Could not create Index\n");
713 return 1;
714 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000715
Ted Kremenek1c6da172009-11-17 19:37:36 +0000716 if (!CreateTranslationUnit(Idx, ast_file, &TU))
717 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000718
Ted Kremenek1c6da172009-11-17 19:37:36 +0000719 if ((fp = fopen(source_file, "r")) == NULL) {
720 fprintf(stderr, "Could not open '%s'\n", source_file);
721 return 1;
722 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000723
Douglas Gregorb9790342010-01-22 21:44:22 +0000724 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000725 for (;;) {
726 CXCursor cursor;
727 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000728
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000729 if (c == '\n') {
730 ++line;
731 col = 1;
732 } else
733 ++col;
734
735 /* Check the cursor at this position, and dump the previous one if we have
736 * found something new.
737 */
738 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
739 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
740 prevCursor.kind != CXCursor_InvalidFile) {
741 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000742 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000743 start_line = line;
744 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000745 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000746 if (c == EOF)
747 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000748
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000749 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000750 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000751
Ted Kremenek1c6da172009-11-17 19:37:36 +0000752 fclose(fp);
753 return 0;
754}
755
756/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000757/* Logic for testing clang_codeComplete(). */
758/******************************************************************************/
759
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000760/* Parse file:line:column from the input string. Returns 0 on success, non-zero
761 on failure. If successful, the pointer *filename will contain newly-allocated
762 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000763int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000764 unsigned *column, unsigned *second_line,
765 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000766 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000767 const char *last_colon = strrchr(input, ':');
768 unsigned values[4], i;
769 unsigned num_values = (second_line && second_column)? 4 : 2;
770
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000771 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000772 if (!last_colon || last_colon == input) {
773 if (num_values == 4)
774 fprintf(stderr, "could not parse filename:line:column:line:column in "
775 "'%s'\n", input);
776 else
777 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000778 return 1;
779 }
780
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000781 for (i = 0; i != num_values; ++i) {
782 const char *prev_colon;
783
784 /* Parse the next line or column. */
785 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
786 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000787 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000788 (i % 2 ? "column" : "line"), input);
789 return 1;
790 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000791
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000792 if (i + 1 == num_values)
793 break;
794
795 /* Find the previous colon. */
796 prev_colon = last_colon - 1;
797 while (prev_colon != input && *prev_colon != ':')
798 --prev_colon;
799 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000800 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000801 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000802 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000803 }
804
805 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000806 }
807
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000808 *line = values[0];
809 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000810
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000811 if (second_line && second_column) {
812 *second_line = values[2];
813 *second_column = values[3];
814 }
815
Douglas Gregor88d23952009-11-09 18:19:57 +0000816 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000817 *filename = (char*)malloc(last_colon - input + 1);
818 memcpy(*filename, input, last_colon - input);
819 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000820 return 0;
821}
822
823const char *
824clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
825 switch (Kind) {
826 case CXCompletionChunk_Optional: return "Optional";
827 case CXCompletionChunk_TypedText: return "TypedText";
828 case CXCompletionChunk_Text: return "Text";
829 case CXCompletionChunk_Placeholder: return "Placeholder";
830 case CXCompletionChunk_Informative: return "Informative";
831 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
832 case CXCompletionChunk_LeftParen: return "LeftParen";
833 case CXCompletionChunk_RightParen: return "RightParen";
834 case CXCompletionChunk_LeftBracket: return "LeftBracket";
835 case CXCompletionChunk_RightBracket: return "RightBracket";
836 case CXCompletionChunk_LeftBrace: return "LeftBrace";
837 case CXCompletionChunk_RightBrace: return "RightBrace";
838 case CXCompletionChunk_LeftAngle: return "LeftAngle";
839 case CXCompletionChunk_RightAngle: return "RightAngle";
840 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000841 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000842 case CXCompletionChunk_Colon: return "Colon";
843 case CXCompletionChunk_SemiColon: return "SemiColon";
844 case CXCompletionChunk_Equal: return "Equal";
845 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
846 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000847 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000848
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000849 return "Unknown";
850}
851
Douglas Gregor3ac73852009-11-09 16:04:45 +0000852void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000853 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000854
Douglas Gregor3ac73852009-11-09 16:04:45 +0000855 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000856 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000857 CXString text;
858 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000859 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000860 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000861
Douglas Gregor3ac73852009-11-09 16:04:45 +0000862 if (Kind == CXCompletionChunk_Optional) {
863 fprintf(file, "{Optional ");
864 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000865 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000866 file);
867 fprintf(file, "}");
868 continue;
869 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000870
Douglas Gregord5a20892009-11-09 17:05:28 +0000871 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000872 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000873 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000874 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000875 cstr ? cstr : "");
876 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000877 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000878
Douglas Gregor3ac73852009-11-09 16:04:45 +0000879}
880
881void print_completion_result(CXCompletionResult *completion_result,
882 CXClientData client_data) {
883 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000884 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
885
886 fprintf(file, "%s:", clang_getCString(ks));
887 clang_disposeString(ks);
888
Douglas Gregor3ac73852009-11-09 16:04:45 +0000889 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000890 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +0000891 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +0000892 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
893 case CXAvailability_Available:
894 break;
895
896 case CXAvailability_Deprecated:
897 fprintf(file, " (deprecated)");
898 break;
899
900 case CXAvailability_NotAvailable:
901 fprintf(file, " (unavailable)");
902 break;
903 }
904 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000905}
906
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000907int my_stricmp(const char *s1, const char *s2) {
908 while (*s1 && *s2) {
909 int c1 = tolower(*s1), c2 = tolower(*s2);
910 if (c1 < c2)
911 return -1;
912 else if (c1 > c2)
913 return 1;
914
915 ++s1;
916 ++s2;
917 }
918
919 if (*s1)
920 return 1;
921 else if (*s2)
922 return -1;
923 return 0;
924}
925
Douglas Gregor1982c182010-07-12 18:38:41 +0000926int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000927 const char *input = argv[1];
928 char *filename = 0;
929 unsigned line;
930 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000931 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000932 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000933 struct CXUnsavedFile *unsaved_files = 0;
934 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000935 CXCodeCompleteResults *results = 0;
Douglas Gregordf95a132010-08-09 20:45:32 +0000936 CXTranslationUnit *TU = 0;
937
Douglas Gregor1982c182010-07-12 18:38:41 +0000938 if (timing_only)
939 input += strlen("-code-completion-timing=");
940 else
941 input += strlen("-code-completion-at=");
942
Ted Kremeneke68fff62010-02-17 00:41:32 +0000943 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000944 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000945 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000946
Douglas Gregor735df882009-12-02 09:21:34 +0000947 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
948 return -1;
949
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000950 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000951 if (getenv("CINDEXTEST_EDITING")) {
Daniel Dunbard91de2b2010-08-09 21:06:06 +0000952 unsigned I, Repeats = 5;
Douglas Gregordf95a132010-08-09 20:45:32 +0000953 TU = clang_parseTranslationUnit(CIdx, 0,
954 argv + num_unsaved_files + 2,
955 argc - num_unsaved_files - 2,
Daniel Dunbar43f331d2010-08-19 23:44:04 +0000956 0, 0, getDefaultParsingOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +0000957 if (!TU) {
958 fprintf(stderr, "Unable to load translation unit!\n");
959 return 1;
960 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000961 for (I = 0; I != Repeats; ++I) {
962 results = clang_codeCompleteAt(TU, filename, line, column,
963 unsaved_files, num_unsaved_files,
964 clang_defaultCodeCompleteOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +0000965 if (!results) {
966 fprintf(stderr, "Unable to perform code completion!\n");
967 return 1;
968 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000969 if (I != Repeats-1)
970 clang_disposeCodeCompleteResults(results);
971 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000972 } else
973 results = clang_codeComplete(CIdx,
974 argv[argc - 1], argc - num_unsaved_files - 3,
975 argv + num_unsaved_files + 2,
976 num_unsaved_files, unsaved_files,
977 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000978
Douglas Gregorec6762c2009-12-18 16:20:58 +0000979 if (results) {
980 unsigned i, n = results->NumResults;
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000981 if (!timing_only) {
982 /* Sort the code-completion results based on the typed text. */
983 clang_sortCodeCompletionResults(results->Results, results->NumResults);
984
Douglas Gregor1982c182010-07-12 18:38:41 +0000985 for (i = 0; i != n; ++i)
986 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000987 }
Douglas Gregora88084b2010-02-18 18:08:43 +0000988 n = clang_codeCompleteGetNumDiagnostics(results);
989 for (i = 0; i != n; ++i) {
990 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
991 PrintDiagnostic(diag);
992 clang_disposeDiagnostic(diag);
993 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000994 clang_disposeCodeCompleteResults(results);
995 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000996 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000997 clang_disposeIndex(CIdx);
998 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000999
Douglas Gregor735df882009-12-02 09:21:34 +00001000 free_remapped_files(unsaved_files, num_unsaved_files);
1001
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001002 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001003}
1004
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001005typedef struct {
1006 char *filename;
1007 unsigned line;
1008 unsigned column;
1009} CursorSourceLocation;
1010
1011int inspect_cursor_at(int argc, const char **argv) {
1012 CXIndex CIdx;
1013 int errorCode;
1014 struct CXUnsavedFile *unsaved_files = 0;
1015 int num_unsaved_files = 0;
1016 CXTranslationUnit TU;
1017 CXCursor Cursor;
1018 CursorSourceLocation *Locations = 0;
1019 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001020
Ted Kremeneke68fff62010-02-17 00:41:32 +00001021 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001022 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1023 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001024
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001025 /* Parse the locations. */
1026 assert(NumLocations > 0 && "Unable to count locations?");
1027 Locations = (CursorSourceLocation *)malloc(
1028 NumLocations * sizeof(CursorSourceLocation));
1029 for (Loc = 0; Loc < NumLocations; ++Loc) {
1030 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001031 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1032 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001033 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001034 return errorCode;
1035 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001036
1037 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001038 &num_unsaved_files))
1039 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001040
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001041 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001042 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1043 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001044 argv + num_unsaved_files + 1 + NumLocations,
1045 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001046 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001047 if (!TU) {
1048 fprintf(stderr, "unable to parse input\n");
1049 return -1;
1050 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001051
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001052 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +00001053 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1054 if (!file)
1055 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001056
1057 Cursor = clang_getCursor(TU,
1058 clang_getLocation(TU, file, Locations[Loc].line,
1059 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001060 PrintCursor(Cursor);
1061 printf("\n");
1062 free(Locations[Loc].filename);
1063 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001064
Douglas Gregora88084b2010-02-18 18:08:43 +00001065 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001066 clang_disposeTranslationUnit(TU);
1067 clang_disposeIndex(CIdx);
1068 free(Locations);
1069 free_remapped_files(unsaved_files, num_unsaved_files);
1070 return 0;
1071}
1072
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001073int perform_token_annotation(int argc, const char **argv) {
1074 const char *input = argv[1];
1075 char *filename = 0;
1076 unsigned line, second_line;
1077 unsigned column, second_column;
1078 CXIndex CIdx;
1079 CXTranslationUnit TU = 0;
1080 int errorCode;
1081 struct CXUnsavedFile *unsaved_files = 0;
1082 int num_unsaved_files = 0;
1083 CXToken *tokens;
1084 unsigned num_tokens;
1085 CXSourceRange range;
1086 CXSourceLocation startLoc, endLoc;
1087 CXFile file = 0;
1088 CXCursor *cursors = 0;
1089 unsigned i;
1090
1091 input += strlen("-test-annotate-tokens=");
1092 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1093 &second_line, &second_column)))
1094 return errorCode;
1095
1096 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1097 return -1;
1098
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001099 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001100 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1101 argc - num_unsaved_files - 3,
1102 argv + num_unsaved_files + 2,
1103 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001104 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001105 if (!TU) {
1106 fprintf(stderr, "unable to parse input\n");
1107 clang_disposeIndex(CIdx);
1108 free(filename);
1109 free_remapped_files(unsaved_files, num_unsaved_files);
1110 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001111 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001112 errorCode = 0;
1113
1114 file = clang_getFile(TU, filename);
1115 if (!file) {
1116 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1117 errorCode = -1;
1118 goto teardown;
1119 }
1120
1121 startLoc = clang_getLocation(TU, file, line, column);
1122 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001123 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001124 column);
1125 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001126 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001127 }
1128
1129 endLoc = clang_getLocation(TU, file, second_line, second_column);
1130 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001131 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001132 second_line, second_column);
1133 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001134 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001135 }
1136
1137 range = clang_getRange(startLoc, endLoc);
1138 clang_tokenize(TU, range, &tokens, &num_tokens);
1139 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1140 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1141 for (i = 0; i != num_tokens; ++i) {
1142 const char *kind = "<unknown>";
1143 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1144 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1145 unsigned start_line, start_column, end_line, end_column;
1146
1147 switch (clang_getTokenKind(tokens[i])) {
1148 case CXToken_Punctuation: kind = "Punctuation"; break;
1149 case CXToken_Keyword: kind = "Keyword"; break;
1150 case CXToken_Identifier: kind = "Identifier"; break;
1151 case CXToken_Literal: kind = "Literal"; break;
1152 case CXToken_Comment: kind = "Comment"; break;
1153 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001154 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001155 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001156 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001157 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001158 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1159 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001160 if (!clang_isInvalid(cursors[i].kind)) {
1161 printf(" ");
1162 PrintCursor(cursors[i]);
1163 }
1164 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001165 }
1166 free(cursors);
1167
1168 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001169 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001170 clang_disposeTranslationUnit(TU);
1171 clang_disposeIndex(CIdx);
1172 free(filename);
1173 free_remapped_files(unsaved_files, num_unsaved_files);
1174 return errorCode;
1175}
1176
Ted Kremenek0d435192009-11-17 18:13:31 +00001177/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001178/* USR printing. */
1179/******************************************************************************/
1180
1181static int insufficient_usr(const char *kind, const char *usage) {
1182 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1183 return 1;
1184}
1185
1186static unsigned isUSR(const char *s) {
1187 return s[0] == 'c' && s[1] == ':';
1188}
1189
1190static int not_usr(const char *s, const char *arg) {
1191 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1192 return 1;
1193}
1194
1195static void print_usr(CXString usr) {
1196 const char *s = clang_getCString(usr);
1197 printf("%s\n", s);
1198 clang_disposeString(usr);
1199}
1200
1201static void display_usrs() {
1202 fprintf(stderr, "-print-usrs options:\n"
1203 " ObjCCategory <class name> <category name>\n"
1204 " ObjCClass <class name>\n"
1205 " ObjCIvar <ivar name> <class USR>\n"
1206 " ObjCMethod <selector> [0=class method|1=instance method] "
1207 "<class USR>\n"
1208 " ObjCProperty <property name> <class USR>\n"
1209 " ObjCProtocol <protocol name>\n");
1210}
1211
1212int print_usrs(const char **I, const char **E) {
1213 while (I != E) {
1214 const char *kind = *I;
1215 unsigned len = strlen(kind);
1216 switch (len) {
1217 case 8:
1218 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1219 if (I + 2 >= E)
1220 return insufficient_usr(kind, "<ivar name> <class USR>");
1221 if (!isUSR(I[2]))
1222 return not_usr("<class USR>", I[2]);
1223 else {
1224 CXString x;
1225 x.Spelling = I[2];
1226 x.MustFreeString = 0;
1227 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1228 }
1229
1230 I += 3;
1231 continue;
1232 }
1233 break;
1234 case 9:
1235 if (memcmp(kind, "ObjCClass", 9) == 0) {
1236 if (I + 1 >= E)
1237 return insufficient_usr(kind, "<class name>");
1238 print_usr(clang_constructUSR_ObjCClass(I[1]));
1239 I += 2;
1240 continue;
1241 }
1242 break;
1243 case 10:
1244 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1245 if (I + 3 >= E)
1246 return insufficient_usr(kind, "<method selector> "
1247 "[0=class method|1=instance method] <class USR>");
1248 if (!isUSR(I[3]))
1249 return not_usr("<class USR>", I[3]);
1250 else {
1251 CXString x;
1252 x.Spelling = I[3];
1253 x.MustFreeString = 0;
1254 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1255 }
1256 I += 4;
1257 continue;
1258 }
1259 break;
1260 case 12:
1261 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1262 if (I + 2 >= E)
1263 return insufficient_usr(kind, "<class name> <category name>");
1264 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1265 I += 3;
1266 continue;
1267 }
1268 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1269 if (I + 1 >= E)
1270 return insufficient_usr(kind, "<protocol name>");
1271 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1272 I += 2;
1273 continue;
1274 }
1275 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1276 if (I + 2 >= E)
1277 return insufficient_usr(kind, "<property name> <class USR>");
1278 if (!isUSR(I[2]))
1279 return not_usr("<class USR>", I[2]);
1280 else {
1281 CXString x;
1282 x.Spelling = I[2];
1283 x.MustFreeString = 0;
1284 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1285 }
1286 I += 3;
1287 continue;
1288 }
1289 break;
1290 default:
1291 break;
1292 }
1293 break;
1294 }
1295
1296 if (I != E) {
1297 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1298 display_usrs();
1299 return 1;
1300 }
1301 return 0;
1302}
1303
1304int print_usrs_file(const char *file_name) {
1305 char line[2048];
1306 const char *args[128];
1307 unsigned numChars = 0;
1308
1309 FILE *fp = fopen(file_name, "r");
1310 if (!fp) {
1311 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1312 return 1;
1313 }
1314
1315 /* This code is not really all that safe, but it works fine for testing. */
1316 while (!feof(fp)) {
1317 char c = fgetc(fp);
1318 if (c == '\n') {
1319 unsigned i = 0;
1320 const char *s = 0;
1321
1322 if (numChars == 0)
1323 continue;
1324
1325 line[numChars] = '\0';
1326 numChars = 0;
1327
1328 if (line[0] == '/' && line[1] == '/')
1329 continue;
1330
1331 s = strtok(line, " ");
1332 while (s) {
1333 args[i] = s;
1334 ++i;
1335 s = strtok(0, " ");
1336 }
1337 if (print_usrs(&args[0], &args[i]))
1338 return 1;
1339 }
1340 else
1341 line[numChars++] = c;
1342 }
1343
1344 fclose(fp);
1345 return 0;
1346}
1347
1348/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001349/* Command line processing. */
1350/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001351int write_pch_file(const char *filename, int argc, const char *argv[]) {
1352 CXIndex Idx;
1353 CXTranslationUnit TU;
1354 struct CXUnsavedFile *unsaved_files = 0;
1355 int num_unsaved_files = 0;
1356
1357 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1358
1359 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1360 clang_disposeIndex(Idx);
1361 return -1;
1362 }
1363
1364 TU = clang_parseTranslationUnit(Idx, 0,
1365 argv + num_unsaved_files,
1366 argc - num_unsaved_files,
1367 unsaved_files,
1368 num_unsaved_files,
1369 CXTranslationUnit_Incomplete);
1370 if (!TU) {
1371 fprintf(stderr, "Unable to load translation unit!\n");
1372 free_remapped_files(unsaved_files, num_unsaved_files);
1373 clang_disposeIndex(Idx);
1374 return 1;
1375 }
1376
Douglas Gregor19998442010-08-13 15:35:05 +00001377 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001378 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1379 clang_disposeTranslationUnit(TU);
1380 free_remapped_files(unsaved_files, num_unsaved_files);
1381 clang_disposeIndex(Idx);
1382 return 0;
1383}
1384
1385/******************************************************************************/
1386/* Command line processing. */
1387/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001388
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001389static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001390 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001391 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001392 if (strcmp(s, "-usrs") == 0)
1393 return USRVisitor;
1394 return NULL;
1395}
1396
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001397static void print_usage(void) {
1398 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001399 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001400 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001401 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001402 " c-index-test -test-file-scan <AST file> <source file> "
1403 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001404 " c-index-test -test-load-tu <AST file> <symbol filter> "
1405 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001406 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1407 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001408 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001409 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001410 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1411 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001412 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001413 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1414 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001415 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001416 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001417 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001418 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1419 fprintf(stderr,
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001420 " c-index-test -print-usr-file <file>\n"
1421 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001422 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001423 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001424 " all - load all symbols, including those from PCH\n"
1425 " local - load all symbols except those in PCH\n"
1426 " category - only load ObjC categories (non-PCH)\n"
1427 " interface - only load ObjC interfaces (non-PCH)\n"
1428 " protocol - only load ObjC protocols (non-PCH)\n"
1429 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001430 " typedef - only load typdefs (non-PCH)\n"
1431 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001432}
1433
1434int main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001435 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001436 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001437 return perform_code_completion(argc, argv, 0);
1438 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1439 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001440 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1441 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001442 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001443 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001444 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001445 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1446 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001447 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001448 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1449 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1450 if (I) {
1451 int trials = atoi(argv[2]);
1452 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1453 NULL);
1454 }
1455 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001456 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001457 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001458 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001459 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001460 }
1461 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001462 return perform_file_scan(argv[2], argv[3],
1463 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001464 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1465 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001466 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1467 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1468 PrintInclusionStack);
1469 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1470 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1471 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001472 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1473 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1474 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001475 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1476 return perform_test_load_source(argc - 2, argv + 2, "all",
1477 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001478 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1479 if (argc > 2)
1480 return print_usrs(argv + 2, argv + argc);
1481 else {
1482 display_usrs();
1483 return 1;
1484 }
1485 }
1486 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1487 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001488 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1489 return write_pch_file(argv[2], argc - 3, argv + 3);
1490
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001491 print_usage();
1492 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001493}