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