Steve Naroff | 2b8ee6c | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 1 | /* c-index-test.c */ |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 2 | |
| 3 | #include "clang-c/Index.h" |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 4 | #include <stdlib.h> |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 6 | #include <string.h> |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 7 | #include <assert.h> |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 8 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 9 | /******************************************************************************/ |
| 10 | /* Utility functions. */ |
| 11 | /******************************************************************************/ |
| 12 | |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 13 | #ifdef _MSC_VER |
| 14 | char *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 Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 28 | extern char *basename(const char *); |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 29 | #endif |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 31 | static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, |
| 32 | CXClientData ClientData); |
| 33 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 34 | static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, |
| 35 | CXTranslationUnit *TU) { |
| 36 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 37 | *TU = clang_createTranslationUnit(Idx, file, PrintDiagnosticCallback, 0); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 38 | if (!TU) { |
| 39 | fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); |
| 40 | return 0; |
| 41 | } |
| 42 | return 1; |
| 43 | } |
| 44 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 45 | void free_remapped_files(struct CXUnsavedFile *unsaved_files, |
| 46 | int num_unsaved_files) { |
| 47 | int i; |
| 48 | for (i = 0; i != num_unsaved_files; ++i) { |
| 49 | free((char *)unsaved_files[i].Filename); |
| 50 | free((char *)unsaved_files[i].Contents); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | int parse_remapped_files(int argc, const char **argv, int start_arg, |
| 55 | struct CXUnsavedFile **unsaved_files, |
| 56 | int *num_unsaved_files) { |
| 57 | int i; |
| 58 | int arg; |
| 59 | int prefix_len = strlen("-remap-file="); |
| 60 | *unsaved_files = 0; |
| 61 | *num_unsaved_files = 0; |
| 62 | |
| 63 | /* Count the number of remapped files. */ |
| 64 | for (arg = start_arg; arg < argc; ++arg) { |
| 65 | if (strncmp(argv[arg], "-remap-file=", prefix_len)) |
| 66 | break; |
| 67 | |
| 68 | ++*num_unsaved_files; |
| 69 | } |
| 70 | |
| 71 | if (*num_unsaved_files == 0) |
| 72 | return 0; |
| 73 | |
| 74 | *unsaved_files |
| 75 | = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * |
| 76 | *num_unsaved_files); |
| 77 | for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) { |
| 78 | struct CXUnsavedFile *unsaved = *unsaved_files + i; |
| 79 | const char *arg_string = argv[arg] + prefix_len; |
| 80 | int filename_len; |
| 81 | char *filename; |
| 82 | char *contents; |
| 83 | FILE *to_file; |
| 84 | const char *semi = strchr(arg_string, ';'); |
| 85 | if (!semi) { |
| 86 | fprintf(stderr, |
| 87 | "error: -remap-file=from;to argument is missing semicolon\n"); |
| 88 | free_remapped_files(*unsaved_files, i); |
| 89 | *unsaved_files = 0; |
| 90 | *num_unsaved_files = 0; |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | /* Open the file that we're remapping to. */ |
| 95 | to_file = fopen(semi + 1, "r"); |
| 96 | if (!to_file) { |
| 97 | fprintf(stderr, "error: cannot open file %s that we are remapping to\n", |
| 98 | semi + 1); |
| 99 | free_remapped_files(*unsaved_files, i); |
| 100 | *unsaved_files = 0; |
| 101 | *num_unsaved_files = 0; |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | /* Determine the length of the file we're remapping to. */ |
| 106 | fseek(to_file, 0, SEEK_END); |
| 107 | unsaved->Length = ftell(to_file); |
| 108 | fseek(to_file, 0, SEEK_SET); |
| 109 | |
| 110 | /* Read the contents of the file we're remapping to. */ |
| 111 | contents = (char *)malloc(unsaved->Length + 1); |
| 112 | if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) { |
| 113 | fprintf(stderr, "error: unexpected %s reading 'to' file %s\n", |
| 114 | (feof(to_file) ? "EOF" : "error"), semi + 1); |
| 115 | fclose(to_file); |
| 116 | free_remapped_files(*unsaved_files, i); |
| 117 | *unsaved_files = 0; |
| 118 | *num_unsaved_files = 0; |
| 119 | return -1; |
| 120 | } |
| 121 | contents[unsaved->Length] = 0; |
| 122 | unsaved->Contents = contents; |
| 123 | |
| 124 | /* Close the file. */ |
| 125 | fclose(to_file); |
| 126 | |
| 127 | /* Copy the file name that we're remapping from. */ |
| 128 | filename_len = semi - arg_string; |
| 129 | filename = (char *)malloc(filename_len + 1); |
| 130 | memcpy(filename, arg_string, filename_len); |
| 131 | filename[filename_len] = 0; |
| 132 | unsaved->Filename = filename; |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 138 | /******************************************************************************/ |
| 139 | /* Pretty-printing. */ |
| 140 | /******************************************************************************/ |
| 141 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 142 | static void PrintCursor(CXCursor Cursor) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 143 | if (clang_isInvalid(Cursor.kind)) |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 144 | printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 145 | else { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 146 | CXString string; |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 147 | CXCursor Referenced; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 148 | unsigned line, column; |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 149 | string = clang_getCursorSpelling(Cursor); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 150 | printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind), |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 151 | clang_getCString(string)); |
| 152 | clang_disposeString(string); |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 153 | |
| 154 | Referenced = clang_getCursorReferenced(Cursor); |
| 155 | if (!clang_equalCursors(Referenced, clang_getNullCursor())) { |
| 156 | CXSourceLocation Loc = clang_getCursorLocation(Referenced); |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 157 | clang_getInstantiationLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 158 | printf(":%d:%d", line, column); |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 159 | } |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 160 | |
| 161 | if (clang_isCursorDefinition(Cursor)) |
| 162 | printf(" (Definition)"); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 163 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 164 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 166 | static const char* GetCursorSource(CXCursor Cursor) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 167 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
| 168 | const char *source; |
| 169 | CXFile file; |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 170 | clang_getInstantiationLocation(Loc, &file, 0, 0, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 171 | source = clang_getFileName(file); |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 172 | if (!source) |
| 173 | return "<invalid loc>"; |
| 174 | return basename(source); |
| 175 | } |
| 176 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 177 | /******************************************************************************/ |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 178 | /* Callbacks. */ |
| 179 | /******************************************************************************/ |
| 180 | |
| 181 | typedef void (*PostVisitTU)(CXTranslationUnit); |
| 182 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 183 | static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, |
| 184 | CXClientData ClientData) { |
| 185 | FILE *out = (FILE *)ClientData; |
| 186 | CXFile file; |
| 187 | unsigned line, column; |
| 188 | CXString text; |
| 189 | enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic); |
| 190 | |
| 191 | /* Ignore diagnostics that should be ignored. */ |
| 192 | if (severity == CXDiagnostic_Ignored) |
| 193 | return; |
| 194 | |
| 195 | /* Print file:line:column. */ |
| 196 | clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), |
| 197 | &file, &line, &column, 0); |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 198 | if (file) { |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 199 | unsigned i, n; |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 200 | unsigned printed_any_ranges = 0; |
| 201 | |
| 202 | fprintf(out, "%s:%d:%d:", clang_getFileName(file), line, column); |
| 203 | |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 204 | n = clang_getDiagnosticNumRanges(Diagnostic); |
| 205 | for (i = 0; i != n; ++i) { |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 206 | CXFile start_file, end_file; |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 207 | CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i); |
| 208 | |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 209 | unsigned start_line, start_column, end_line, end_column; |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 210 | clang_getInstantiationLocation(clang_getRangeStart(range), |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 211 | &start_file, &start_line, &start_column,0); |
Douglas Gregor | a3890ba | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 212 | clang_getInstantiationLocation(clang_getRangeEnd(range), |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 213 | &end_file, &end_line, &end_column, 0); |
| 214 | |
| 215 | if (start_file != end_file || start_file != file) |
| 216 | continue; |
| 217 | |
| 218 | fprintf(out, "{%d:%d-%d:%d}", start_line, start_column, end_line, |
| 219 | end_column+1); |
| 220 | printed_any_ranges = 1; |
| 221 | } |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 222 | if (printed_any_ranges) |
| 223 | fprintf(out, ":"); |
| 224 | |
| 225 | fprintf(out, " "); |
| 226 | } |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 227 | |
| 228 | /* Print warning/error/etc. */ |
| 229 | switch (severity) { |
| 230 | case CXDiagnostic_Ignored: assert(0 && "impossible"); break; |
| 231 | case CXDiagnostic_Note: fprintf(out, "note: "); break; |
| 232 | case CXDiagnostic_Warning: fprintf(out, "warning: "); break; |
| 233 | case CXDiagnostic_Error: fprintf(out, "error: "); break; |
| 234 | case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break; |
| 235 | } |
| 236 | |
| 237 | text = clang_getDiagnosticSpelling(Diagnostic); |
| 238 | if (clang_getCString(text)) |
| 239 | fprintf(out, "%s\n", clang_getCString(text)); |
| 240 | else |
| 241 | fprintf(out, "<no diagnostic text>\n"); |
| 242 | clang_disposeString(text); |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 243 | |
| 244 | if (file) { |
| 245 | unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); |
| 246 | for (i = 0; i != num_fixits; ++i) { |
| 247 | switch (clang_getDiagnosticFixItKind(Diagnostic, i)) { |
| 248 | case CXFixIt_Insertion: { |
| 249 | CXSourceLocation insertion_loc; |
| 250 | CXFile insertion_file; |
| 251 | unsigned insertion_line, insertion_column; |
| 252 | text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc); |
| 253 | clang_getInstantiationLocation(insertion_loc, &insertion_file, |
| 254 | &insertion_line, &insertion_column, 0); |
| 255 | if (insertion_file == file) |
| 256 | fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", |
| 257 | clang_getCString(text), insertion_line, insertion_column); |
| 258 | clang_disposeString(text); |
| 259 | break; |
| 260 | } |
| 261 | |
| 262 | case CXFixIt_Removal: { |
| 263 | CXFile start_file, end_file; |
| 264 | unsigned start_line, start_column, end_line, end_column; |
| 265 | CXSourceRange remove_range |
| 266 | = clang_getDiagnosticFixItRemoval(Diagnostic, i); |
| 267 | clang_getInstantiationLocation(clang_getRangeStart(remove_range), |
| 268 | &start_file, &start_line, &start_column, |
| 269 | 0); |
| 270 | clang_getInstantiationLocation(clang_getRangeEnd(remove_range), |
| 271 | &end_file, &end_line, &end_column, 0); |
| 272 | if (start_file == file && end_file == file) |
| 273 | fprintf(out, "FIX-IT: Remove %d:%d-%d:%d\n", |
| 274 | start_line, start_column, end_line, end_column+1); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | case CXFixIt_Replacement: { |
| 279 | CXFile start_file, end_file; |
| 280 | unsigned start_line, start_column, end_line, end_column; |
| 281 | CXSourceRange remove_range; |
| 282 | text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range); |
| 283 | clang_getInstantiationLocation(clang_getRangeStart(remove_range), |
| 284 | &start_file, &start_line, &start_column, |
| 285 | 0); |
| 286 | clang_getInstantiationLocation(clang_getRangeEnd(remove_range), |
| 287 | &end_file, &end_line, &end_column, 0); |
| 288 | if (start_file == end_file) |
| 289 | fprintf(out, "FIX-IT: Replace %d:%d-%d:%d with \"%s\"\n", |
| 290 | start_line, start_column, end_line, end_column+1, |
| 291 | clang_getCString(text)); |
| 292 | clang_disposeString(text); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 300 | /******************************************************************************/ |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 301 | /* Logic for testing traversal. */ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 302 | /******************************************************************************/ |
| 303 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 304 | static const char *FileCheckPrefix = "CHECK"; |
| 305 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 306 | static void PrintCursorExtent(CXCursor C) { |
| 307 | CXSourceRange extent = clang_getCursorExtent(C); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 308 | CXFile begin_file, end_file; |
| 309 | unsigned begin_line, begin_column, end_line, end_column; |
| 310 | |
| 311 | clang_getInstantiationLocation(clang_getRangeStart(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 312 | &begin_file, &begin_line, &begin_column, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 313 | clang_getInstantiationLocation(clang_getRangeEnd(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 314 | &end_file, &end_line, &end_column, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 315 | if (!begin_file || !end_file) |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 316 | return; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 317 | |
| 318 | printf(" [Extent=%d:%d:%d:%d]", begin_line, begin_column, |
| 319 | end_line, end_column); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 322 | /* Data used by all of the visitors. */ |
| 323 | typedef struct { |
| 324 | CXTranslationUnit TU; |
| 325 | enum CXCursorKind *Filter; |
| 326 | } VisitorData; |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 327 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 329 | enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, |
| 330 | CXCursor Parent, |
| 331 | CXClientData ClientData) { |
| 332 | VisitorData *Data = (VisitorData *)ClientData; |
| 333 | if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 334 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 335 | unsigned line, column; |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 336 | clang_getInstantiationLocation(Loc, 0, &line, &column, 0); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 337 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 338 | GetCursorSource(Cursor), line, column); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 339 | PrintCursor(Cursor); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 340 | PrintCursorExtent(Cursor); |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 341 | printf("\n"); |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 342 | return CXChildVisit_Recurse; |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 343 | } |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 344 | |
| 345 | return CXChildVisit_Continue; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 346 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 347 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 348 | static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, |
| 349 | CXCursor Parent, |
| 350 | CXClientData ClientData) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 351 | const char *startBuf, *endBuf; |
| 352 | unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; |
| 353 | CXCursor Ref; |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 354 | VisitorData *Data = (VisitorData *)ClientData; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 356 | if (Cursor.kind != CXCursor_FunctionDecl || |
| 357 | !clang_isCursorDefinition(Cursor)) |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 358 | return CXChildVisit_Continue; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 359 | |
| 360 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 361 | &startLine, &startColumn, |
| 362 | &endLine, &endColumn); |
| 363 | /* Probe the entire body, looking for both decls and refs. */ |
| 364 | curLine = startLine; |
| 365 | curColumn = startColumn; |
| 366 | |
| 367 | while (startBuf < endBuf) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 368 | CXSourceLocation Loc; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 369 | CXFile file; |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 370 | const char *source = 0; |
| 371 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 372 | if (*startBuf == '\n') { |
| 373 | startBuf++; |
| 374 | curLine++; |
| 375 | curColumn = 1; |
| 376 | } else if (*startBuf != '\t') |
| 377 | curColumn++; |
| 378 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 379 | Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 380 | clang_getInstantiationLocation(Loc, &file, 0, 0, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 381 | source = clang_getFileName(file); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 382 | if (source) { |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 383 | CXSourceLocation RefLoc |
| 384 | = clang_getLocation(Data->TU, file, curLine, curColumn); |
| 385 | Ref = clang_getCursor(Data->TU, RefLoc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 386 | if (Ref.kind == CXCursor_NoDeclFound) { |
| 387 | /* Nothing found here; that's fine. */ |
| 388 | } else if (Ref.kind != CXCursor_FunctionDecl) { |
| 389 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), |
| 390 | curLine, curColumn); |
| 391 | PrintCursor(Ref); |
| 392 | printf("\n"); |
| 393 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 394 | } |
| 395 | startBuf++; |
| 396 | } |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 397 | |
| 398 | return CXChildVisit_Continue; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 401 | /******************************************************************************/ |
| 402 | /* USR testing. */ |
| 403 | /******************************************************************************/ |
| 404 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 405 | enum 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 Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 409 | CXString USR = clang_getCursorUSR(C); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 410 | if (!USR.Spelling) { |
| 411 | clang_disposeString(USR); |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 412 | return CXChildVisit_Continue; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 413 | } |
| 414 | printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 415 | PrintCursorExtent(C); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 416 | printf("\n"); |
| 417 | clang_disposeString(USR); |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 418 | |
| 419 | return CXChildVisit_Recurse; |
| 420 | } |
| 421 | |
| 422 | return CXChildVisit_Continue; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /******************************************************************************/ |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 426 | /* Inclusion stack testing. */ |
| 427 | /******************************************************************************/ |
| 428 | |
| 429 | void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, |
| 430 | unsigned includeStackLen, CXClientData data) { |
| 431 | |
| 432 | unsigned i; |
| 433 | printf("file: %s\nincluded by:\n", clang_getFileName(includedFile)); |
| 434 | for (i = 0; i < includeStackLen; ++i) { |
| 435 | CXFile includingFile; |
| 436 | unsigned line, column; |
| 437 | clang_getInstantiationLocation(includeStack[i], &includingFile, &line, |
| 438 | &column, 0); |
| 439 | printf(" %s:%d:%d\n", clang_getFileName(includingFile), line, column); |
| 440 | } |
| 441 | printf("\n"); |
| 442 | } |
| 443 | |
| 444 | void PrintInclusionStack(CXTranslationUnit TU) { |
| 445 | clang_getInclusions(TU, InclusionVisitor, NULL); |
| 446 | } |
| 447 | |
| 448 | /******************************************************************************/ |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 449 | /* Loading ASTs/source. */ |
| 450 | /******************************************************************************/ |
| 451 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 452 | static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, |
Ted Kremenek | 9827156 | 2010-01-12 18:53:15 +0000 | [diff] [blame] | 453 | const char *filter, const char *prefix, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 454 | CXCursorVisitor Visitor, |
| 455 | PostVisitTU PV) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 456 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 457 | if (prefix) |
| 458 | FileCheckPrefix = prefix; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 459 | |
| 460 | if (Visitor) { |
| 461 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 462 | enum CXCursorKind *ck = &K; |
| 463 | VisitorData Data; |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 465 | /* Perform some simple filtering. */ |
| 466 | if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
Daniel Dunbar | b1ffee6 | 2010-02-10 20:42:40 +0000 | [diff] [blame] | 467 | else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 468 | else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| 469 | else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 470 | else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 471 | else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| 472 | else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
| 473 | else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; |
| 474 | else { |
| 475 | fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | Data.TU = TU; |
| 480 | Data.Filter = ck; |
| 481 | clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 482 | } |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 483 | |
| 484 | if (PV) |
| 485 | PV(TU); |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 487 | clang_disposeTranslationUnit(TU); |
| 488 | return 0; |
| 489 | } |
| 490 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 491 | int perform_test_load_tu(const char *file, const char *filter, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 492 | const char *prefix, CXCursorVisitor Visitor, |
| 493 | PostVisitTU PV) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 494 | CXIndex Idx; |
| 495 | CXTranslationUnit TU; |
| 496 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 497 | !strcmp(filter, "local") ? 1 : 0); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 498 | |
| 499 | if (!CreateTranslationUnit(Idx, file, &TU)) |
| 500 | return 1; |
| 501 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 502 | return perform_test_load(Idx, TU, filter, prefix, Visitor, PV); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 505 | int perform_test_load_source(int argc, const char **argv, |
| 506 | const char *filter, CXCursorVisitor Visitor, |
| 507 | PostVisitTU PV) { |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 508 | const char *UseExternalASTs = |
| 509 | getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION"); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 510 | CXIndex Idx; |
| 511 | CXTranslationUnit TU; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 512 | struct CXUnsavedFile *unsaved_files = 0; |
| 513 | int num_unsaved_files = 0; |
| 514 | int result; |
| 515 | |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 516 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 517 | !strcmp(filter, "local") ? 1 : 0); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 518 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 519 | if (UseExternalASTs && strlen(UseExternalASTs)) |
| 520 | clang_setUseExternalASTGeneration(Idx, 1); |
| 521 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 522 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) |
| 523 | return -1; |
| 524 | |
| 525 | TU = clang_createTranslationUnitFromSourceFile(Idx, 0, |
| 526 | argc - num_unsaved_files, |
| 527 | argv + num_unsaved_files, |
| 528 | num_unsaved_files, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 529 | unsaved_files, |
| 530 | PrintDiagnosticCallback, |
| 531 | stderr); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 532 | if (!TU) { |
| 533 | fprintf(stderr, "Unable to load translation unit!\n"); |
| 534 | return 1; |
| 535 | } |
| 536 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 537 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 538 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 539 | return result; |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 542 | /******************************************************************************/ |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 543 | /* Logic for testing clang_getCursor(). */ |
| 544 | /******************************************************************************/ |
| 545 | |
| 546 | static void print_cursor_file_scan(CXCursor cursor, |
| 547 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 548 | unsigned end_line, unsigned end_col, |
| 549 | const char *prefix) { |
Ted Kremenek | 9096a20 | 2010-01-07 01:17:12 +0000 | [diff] [blame] | 550 | printf("// %s: ", FileCheckPrefix); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 551 | if (prefix) |
| 552 | printf("-%s", prefix); |
| 553 | printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ", |
| 554 | start_line, start_col, end_line, end_col); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 555 | PrintCursor(cursor); |
| 556 | printf("\n"); |
| 557 | } |
| 558 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 559 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 560 | const char *prefix) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 561 | CXIndex Idx; |
| 562 | CXTranslationUnit TU; |
| 563 | FILE *fp; |
| 564 | unsigned line; |
| 565 | CXCursor prevCursor; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 566 | CXFile file; |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 567 | unsigned printed; |
| 568 | unsigned start_line, start_col, last_line, last_col; |
| 569 | size_t i; |
| 570 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 571 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 572 | fprintf(stderr, "Could not create Index\n"); |
| 573 | return 1; |
| 574 | } |
| 575 | |
| 576 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 577 | return 1; |
| 578 | |
| 579 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 580 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 581 | return 1; |
| 582 | } |
| 583 | |
| 584 | line = 0; |
| 585 | prevCursor = clang_getNullCursor(); |
| 586 | printed = 0; |
| 587 | start_line = last_line = 1; |
| 588 | start_col = last_col = 1; |
| 589 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 590 | file = clang_getFile(TU, source_file); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 591 | while (!feof(fp)) { |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 592 | size_t len = 0; |
| 593 | int c; |
| 594 | |
| 595 | while ((c = fgetc(fp)) != EOF) { |
| 596 | len++; |
| 597 | if (c == '\n') |
| 598 | break; |
| 599 | } |
| 600 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 601 | ++line; |
| 602 | |
| 603 | for (i = 0; i < len ; ++i) { |
| 604 | CXCursor cursor; |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 605 | cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, i+1)); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 606 | |
| 607 | if (!clang_equalCursors(cursor, prevCursor) && |
| 608 | prevCursor.kind != CXCursor_InvalidFile) { |
| 609 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 610 | last_line, last_col, prefix); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 611 | printed = 1; |
| 612 | start_line = line; |
| 613 | start_col = (unsigned) i+1; |
| 614 | } |
| 615 | else { |
| 616 | printed = 0; |
| 617 | } |
| 618 | |
| 619 | prevCursor = cursor; |
| 620 | last_line = line; |
| 621 | last_col = (unsigned) i+1; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | if (!printed && prevCursor.kind != CXCursor_InvalidFile) { |
| 626 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 627 | last_line, last_col, prefix); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | fclose(fp); |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 635 | /* Logic for testing clang_codeComplete(). */ |
| 636 | /******************************************************************************/ |
| 637 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 638 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 639 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 640 | memory (that will be owned by the caller) to store the file name. */ |
| 641 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 642 | unsigned *column, unsigned *second_line, |
| 643 | unsigned *second_column) { |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 644 | /* Find the second colon. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 645 | const char *last_colon = strrchr(input, ':'); |
| 646 | unsigned values[4], i; |
| 647 | unsigned num_values = (second_line && second_column)? 4 : 2; |
| 648 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 649 | char *endptr = 0; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 650 | if (!last_colon || last_colon == input) { |
| 651 | if (num_values == 4) |
| 652 | fprintf(stderr, "could not parse filename:line:column:line:column in " |
| 653 | "'%s'\n", input); |
| 654 | else |
| 655 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 656 | return 1; |
| 657 | } |
| 658 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 659 | for (i = 0; i != num_values; ++i) { |
| 660 | const char *prev_colon; |
| 661 | |
| 662 | /* Parse the next line or column. */ |
| 663 | values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); |
| 664 | if (*endptr != 0 && *endptr != ':') { |
| 665 | fprintf(stderr, "could not parse %s in '%s'\n", |
| 666 | (i % 2 ? "column" : "line"), input); |
| 667 | return 1; |
| 668 | } |
| 669 | |
| 670 | if (i + 1 == num_values) |
| 671 | break; |
| 672 | |
| 673 | /* Find the previous colon. */ |
| 674 | prev_colon = last_colon - 1; |
| 675 | while (prev_colon != input && *prev_colon != ':') |
| 676 | --prev_colon; |
| 677 | if (prev_colon == input) { |
| 678 | fprintf(stderr, "could not parse %s in '%s'\n", |
| 679 | (i % 2 == 0? "column" : "line"), input); |
| 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | last_colon = prev_colon; |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 686 | *line = values[0]; |
| 687 | *column = values[1]; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 689 | if (second_line && second_column) { |
| 690 | *second_line = values[2]; |
| 691 | *second_column = values[3]; |
| 692 | } |
| 693 | |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 694 | /* Copy the file name. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 695 | *filename = (char*)malloc(last_colon - input + 1); |
| 696 | memcpy(*filename, input, last_colon - input); |
| 697 | (*filename)[last_colon - input] = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | const char * |
| 702 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 703 | switch (Kind) { |
| 704 | case CXCompletionChunk_Optional: return "Optional"; |
| 705 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 706 | case CXCompletionChunk_Text: return "Text"; |
| 707 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 708 | case CXCompletionChunk_Informative: return "Informative"; |
| 709 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 710 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 711 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 712 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 713 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 714 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 715 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 716 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 717 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 718 | case CXCompletionChunk_Comma: return "Comma"; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 719 | case CXCompletionChunk_ResultType: return "ResultType"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 720 | case CXCompletionChunk_Colon: return "Colon"; |
| 721 | case CXCompletionChunk_SemiColon: return "SemiColon"; |
| 722 | case CXCompletionChunk_Equal: return "Equal"; |
| 723 | case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; |
| 724 | case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | return "Unknown"; |
| 728 | } |
| 729 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 730 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 731 | int I, N; |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 732 | |
| 733 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 734 | for (I = 0; I != N; ++I) { |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 735 | const char *text = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 736 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 737 | = clang_getCompletionChunkKind(completion_string, I); |
| 738 | |
| 739 | if (Kind == CXCompletionChunk_Optional) { |
| 740 | fprintf(file, "{Optional "); |
| 741 | print_completion_string( |
| 742 | clang_getCompletionChunkCompletionString(completion_string, I), |
| 743 | file); |
| 744 | fprintf(file, "}"); |
| 745 | continue; |
| 746 | } |
| 747 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 748 | text = clang_getCompletionChunkText(completion_string, I); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 749 | fprintf(file, "{%s %s}", |
| 750 | clang_getCompletionChunkKindSpelling(Kind), |
| 751 | text? text : ""); |
| 752 | } |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | void print_completion_result(CXCompletionResult *completion_result, |
| 756 | CXClientData client_data) { |
| 757 | FILE *file = (FILE *)client_data; |
| 758 | fprintf(file, "%s:", |
| 759 | clang_getCursorKindSpelling(completion_result->CursorKind)); |
| 760 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 761 | fprintf(file, "\n"); |
| 762 | } |
| 763 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 764 | int perform_code_completion(int argc, const char **argv) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 765 | const char *input = argv[1]; |
| 766 | char *filename = 0; |
| 767 | unsigned line; |
| 768 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 769 | CXIndex CIdx; |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 770 | int errorCode; |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 771 | struct CXUnsavedFile *unsaved_files = 0; |
| 772 | int num_unsaved_files = 0; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 773 | CXCodeCompleteResults *results = 0; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 775 | input += strlen("-code-completion-at="); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 776 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
| 777 | 0, 0))) |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 778 | return errorCode; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 780 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 781 | return -1; |
| 782 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 783 | CIdx = clang_createIndex(0); |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 784 | results = clang_codeComplete(CIdx, |
| 785 | argv[argc - 1], argc - num_unsaved_files - 3, |
| 786 | argv + num_unsaved_files + 2, |
| 787 | num_unsaved_files, unsaved_files, |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 788 | filename, line, column, |
| 789 | PrintDiagnosticCallback, stderr); |
| 790 | |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 791 | if (results) { |
| 792 | unsigned i, n = results->NumResults; |
| 793 | for (i = 0; i != n; ++i) |
| 794 | print_completion_result(results->Results + i, stdout); |
| 795 | clang_disposeCodeCompleteResults(results); |
| 796 | } |
| 797 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 798 | clang_disposeIndex(CIdx); |
| 799 | free(filename); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 801 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 802 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 803 | return 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 806 | typedef struct { |
| 807 | char *filename; |
| 808 | unsigned line; |
| 809 | unsigned column; |
| 810 | } CursorSourceLocation; |
| 811 | |
| 812 | int inspect_cursor_at(int argc, const char **argv) { |
| 813 | CXIndex CIdx; |
| 814 | int errorCode; |
| 815 | struct CXUnsavedFile *unsaved_files = 0; |
| 816 | int num_unsaved_files = 0; |
| 817 | CXTranslationUnit TU; |
| 818 | CXCursor Cursor; |
| 819 | CursorSourceLocation *Locations = 0; |
| 820 | unsigned NumLocations = 0, Loc; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 821 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 822 | /* Count the number of locations. */ |
| 823 | while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) |
| 824 | ++NumLocations; |
| 825 | |
| 826 | /* Parse the locations. */ |
| 827 | assert(NumLocations > 0 && "Unable to count locations?"); |
| 828 | Locations = (CursorSourceLocation *)malloc( |
| 829 | NumLocations * sizeof(CursorSourceLocation)); |
| 830 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 831 | const char *input = argv[Loc + 1] + strlen("-cursor-at="); |
| 832 | if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| 833 | &Locations[Loc].line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 834 | &Locations[Loc].column, 0, 0))) |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 835 | return errorCode; |
| 836 | } |
| 837 | |
| 838 | if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
| 839 | &num_unsaved_files)) |
| 840 | return -1; |
| 841 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 842 | CIdx = clang_createIndex(0); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 843 | TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], |
| 844 | argc - num_unsaved_files - 2 - NumLocations, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 845 | argv + num_unsaved_files + 1 + NumLocations, |
| 846 | num_unsaved_files, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 847 | unsaved_files, |
| 848 | PrintDiagnosticCallback, |
| 849 | stderr); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 850 | if (!TU) { |
| 851 | fprintf(stderr, "unable to parse input\n"); |
| 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 856 | CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| 857 | if (!file) |
| 858 | continue; |
| 859 | |
| 860 | Cursor = clang_getCursor(TU, |
| 861 | clang_getLocation(TU, file, Locations[Loc].line, |
| 862 | Locations[Loc].column)); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 863 | PrintCursor(Cursor); |
| 864 | printf("\n"); |
| 865 | free(Locations[Loc].filename); |
| 866 | } |
| 867 | |
| 868 | clang_disposeTranslationUnit(TU); |
| 869 | clang_disposeIndex(CIdx); |
| 870 | free(Locations); |
| 871 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 872 | return 0; |
| 873 | } |
| 874 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 875 | int perform_token_annotation(int argc, const char **argv) { |
| 876 | const char *input = argv[1]; |
| 877 | char *filename = 0; |
| 878 | unsigned line, second_line; |
| 879 | unsigned column, second_column; |
| 880 | CXIndex CIdx; |
| 881 | CXTranslationUnit TU = 0; |
| 882 | int errorCode; |
| 883 | struct CXUnsavedFile *unsaved_files = 0; |
| 884 | int num_unsaved_files = 0; |
| 885 | CXToken *tokens; |
| 886 | unsigned num_tokens; |
| 887 | CXSourceRange range; |
| 888 | CXSourceLocation startLoc, endLoc; |
| 889 | CXFile file = 0; |
| 890 | CXCursor *cursors = 0; |
| 891 | unsigned i; |
| 892 | |
| 893 | input += strlen("-test-annotate-tokens="); |
| 894 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
| 895 | &second_line, &second_column))) |
| 896 | return errorCode; |
| 897 | |
| 898 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 899 | return -1; |
| 900 | |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 901 | CIdx = clang_createIndex(0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 902 | TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], |
| 903 | argc - num_unsaved_files - 3, |
| 904 | argv + num_unsaved_files + 2, |
| 905 | num_unsaved_files, |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 906 | unsaved_files, |
| 907 | PrintDiagnosticCallback, |
| 908 | stderr); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 909 | if (!TU) { |
| 910 | fprintf(stderr, "unable to parse input\n"); |
| 911 | clang_disposeIndex(CIdx); |
| 912 | free(filename); |
| 913 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 914 | return -1; |
| 915 | } |
| 916 | errorCode = 0; |
| 917 | |
| 918 | file = clang_getFile(TU, filename); |
| 919 | if (!file) { |
| 920 | fprintf(stderr, "file %s is not in this translation unit\n", filename); |
| 921 | errorCode = -1; |
| 922 | goto teardown; |
| 923 | } |
| 924 | |
| 925 | startLoc = clang_getLocation(TU, file, line, column); |
| 926 | if (clang_equalLocations(clang_getNullLocation(), startLoc)) { |
| 927 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, |
| 928 | column); |
| 929 | errorCode = -1; |
| 930 | goto teardown; |
| 931 | } |
| 932 | |
| 933 | endLoc = clang_getLocation(TU, file, second_line, second_column); |
| 934 | if (clang_equalLocations(clang_getNullLocation(), endLoc)) { |
| 935 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, |
| 936 | second_line, second_column); |
| 937 | errorCode = -1; |
| 938 | goto teardown; |
| 939 | } |
| 940 | |
| 941 | range = clang_getRange(startLoc, endLoc); |
| 942 | clang_tokenize(TU, range, &tokens, &num_tokens); |
| 943 | cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); |
| 944 | clang_annotateTokens(TU, tokens, num_tokens, cursors); |
| 945 | for (i = 0; i != num_tokens; ++i) { |
| 946 | const char *kind = "<unknown>"; |
| 947 | CXString spelling = clang_getTokenSpelling(TU, tokens[i]); |
| 948 | CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); |
| 949 | unsigned start_line, start_column, end_line, end_column; |
| 950 | |
| 951 | switch (clang_getTokenKind(tokens[i])) { |
| 952 | case CXToken_Punctuation: kind = "Punctuation"; break; |
| 953 | case CXToken_Keyword: kind = "Keyword"; break; |
| 954 | case CXToken_Identifier: kind = "Identifier"; break; |
| 955 | case CXToken_Literal: kind = "Literal"; break; |
| 956 | case CXToken_Comment: kind = "Comment"; break; |
| 957 | } |
| 958 | clang_getInstantiationLocation(clang_getRangeStart(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 959 | 0, &start_line, &start_column, 0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 960 | clang_getInstantiationLocation(clang_getRangeEnd(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 961 | 0, &end_line, &end_column, 0); |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 962 | printf("%s: \"%s\" [%d:%d - %d:%d]", kind, clang_getCString(spelling), |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 963 | start_line, start_column, end_line, end_column); |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 964 | if (!clang_isInvalid(cursors[i].kind)) { |
| 965 | printf(" "); |
| 966 | PrintCursor(cursors[i]); |
| 967 | } |
| 968 | printf("\n"); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 969 | } |
| 970 | free(cursors); |
| 971 | |
| 972 | teardown: |
| 973 | clang_disposeTranslationUnit(TU); |
| 974 | clang_disposeIndex(CIdx); |
| 975 | free(filename); |
| 976 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 977 | return errorCode; |
| 978 | } |
| 979 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 980 | /******************************************************************************/ |
| 981 | /* Command line processing. */ |
| 982 | /******************************************************************************/ |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 983 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 984 | static CXCursorVisitor GetVisitor(const char *s) { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 985 | if (s[0] == '\0') |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 986 | return FilteredPrintingVisitor; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 987 | if (strcmp(s, "-usrs") == 0) |
| 988 | return USRVisitor; |
| 989 | return NULL; |
| 990 | } |
| 991 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 992 | static void print_usage(void) { |
| 993 | fprintf(stderr, |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 994 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 995 | " c-index-test -cursor-at=<site> <compiler arguments>\n" |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 996 | " c-index-test -test-file-scan <AST file> <source file> " |
| 997 | "[FileCheck prefix]\n" |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 998 | " c-index-test -test-load-tu <AST file> <symbol filter> " |
| 999 | "[FileCheck prefix]\n" |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1000 | " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " |
| 1001 | "[FileCheck prefix]\n" |
| 1002 | " c-index-test -test-load-source <symbol filter> {<args>}*\n" |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1003 | " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1004 | fprintf(stderr, |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1005 | " c-index-test -test-annotate-tokens=<range> {<args>}*\n" |
| 1006 | " c-index-test -test-inclusion-stack-source {<args>}*\n" |
| 1007 | " c-index-test -test-inclusion-stack-tu <AST file>\n\n" |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1008 | " <symbol filter> values:\n%s", |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1009 | " all - load all symbols, including those from PCH\n" |
| 1010 | " local - load all symbols except those in PCH\n" |
| 1011 | " category - only load ObjC categories (non-PCH)\n" |
| 1012 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 1013 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 1014 | " function - only load functions (non-PCH)\n" |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1015 | " typedef - only load typdefs (non-PCH)\n" |
| 1016 | " scan-function - scan function bodies (non-PCH)\n\n"); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | int main(int argc, const char **argv) { |
| 1020 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
| 1021 | return perform_code_completion(argc, argv); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1022 | if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) |
| 1023 | return inspect_cursor_at(argc, argv); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1024 | else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1025 | CXCursorVisitor I = GetVisitor(argv[1] + 13); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1026 | if (I) |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1027 | return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, |
| 1028 | NULL); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1029 | } |
| 1030 | else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1031 | CXCursorVisitor I = GetVisitor(argv[1] + 17); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1032 | if (I) |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1033 | return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1034 | } |
| 1035 | else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1036 | return perform_file_scan(argv[2], argv[3], |
| 1037 | argc >= 5 ? argv[4] : 0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1038 | else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) |
| 1039 | return perform_token_annotation(argc, argv); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1040 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) |
| 1041 | return perform_test_load_source(argc - 2, argv + 2, "all", NULL, |
| 1042 | PrintInclusionStack); |
| 1043 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) |
| 1044 | return perform_test_load_tu(argv[2], "all", NULL, NULL, |
| 1045 | PrintInclusionStack); |
| 1046 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1047 | print_usage(); |
| 1048 | return 1; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 1049 | } |