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