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 | |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 31 | static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column, |
| 32 | unsigned end_line, unsigned end_column) { |
| 33 | fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column, |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 34 | end_line, end_column); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 37 | static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, |
| 38 | CXTranslationUnit *TU) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 40 | *TU = clang_createTranslationUnit(Idx, file); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 41 | if (!TU) { |
| 42 | fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); |
| 43 | return 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 44 | } |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 45 | return 1; |
| 46 | } |
| 47 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 48 | void free_remapped_files(struct CXUnsavedFile *unsaved_files, |
| 49 | int num_unsaved_files) { |
| 50 | int i; |
| 51 | for (i = 0; i != num_unsaved_files; ++i) { |
| 52 | free((char *)unsaved_files[i].Filename); |
| 53 | free((char *)unsaved_files[i].Contents); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | int parse_remapped_files(int argc, const char **argv, int start_arg, |
| 58 | struct CXUnsavedFile **unsaved_files, |
| 59 | int *num_unsaved_files) { |
| 60 | int i; |
| 61 | int arg; |
| 62 | int prefix_len = strlen("-remap-file="); |
| 63 | *unsaved_files = 0; |
| 64 | *num_unsaved_files = 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 65 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 66 | /* Count the number of remapped files. */ |
| 67 | for (arg = start_arg; arg < argc; ++arg) { |
| 68 | if (strncmp(argv[arg], "-remap-file=", prefix_len)) |
| 69 | break; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 71 | ++*num_unsaved_files; |
| 72 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 73 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 74 | if (*num_unsaved_files == 0) |
| 75 | return 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 76 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 77 | *unsaved_files |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 78 | = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 79 | *num_unsaved_files); |
| 80 | for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) { |
| 81 | struct CXUnsavedFile *unsaved = *unsaved_files + i; |
| 82 | const char *arg_string = argv[arg] + prefix_len; |
| 83 | int filename_len; |
| 84 | char *filename; |
| 85 | char *contents; |
| 86 | FILE *to_file; |
| 87 | const char *semi = strchr(arg_string, ';'); |
| 88 | if (!semi) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 89 | fprintf(stderr, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 90 | "error: -remap-file=from;to argument is missing semicolon\n"); |
| 91 | free_remapped_files(*unsaved_files, i); |
| 92 | *unsaved_files = 0; |
| 93 | *num_unsaved_files = 0; |
| 94 | return -1; |
| 95 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 97 | /* Open the file that we're remapping to. */ |
| 98 | to_file = fopen(semi + 1, "r"); |
| 99 | if (!to_file) { |
| 100 | fprintf(stderr, "error: cannot open file %s that we are remapping to\n", |
| 101 | semi + 1); |
| 102 | free_remapped_files(*unsaved_files, i); |
| 103 | *unsaved_files = 0; |
| 104 | *num_unsaved_files = 0; |
| 105 | return -1; |
| 106 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 107 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 108 | /* Determine the length of the file we're remapping to. */ |
| 109 | fseek(to_file, 0, SEEK_END); |
| 110 | unsaved->Length = ftell(to_file); |
| 111 | fseek(to_file, 0, SEEK_SET); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 112 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 113 | /* Read the contents of the file we're remapping to. */ |
| 114 | contents = (char *)malloc(unsaved->Length + 1); |
| 115 | if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) { |
| 116 | fprintf(stderr, "error: unexpected %s reading 'to' file %s\n", |
| 117 | (feof(to_file) ? "EOF" : "error"), semi + 1); |
| 118 | fclose(to_file); |
| 119 | free_remapped_files(*unsaved_files, i); |
| 120 | *unsaved_files = 0; |
| 121 | *num_unsaved_files = 0; |
| 122 | return -1; |
| 123 | } |
| 124 | contents[unsaved->Length] = 0; |
| 125 | unsaved->Contents = contents; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 126 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 127 | /* Close the file. */ |
| 128 | fclose(to_file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 129 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 130 | /* Copy the file name that we're remapping from. */ |
| 131 | filename_len = semi - arg_string; |
| 132 | filename = (char *)malloc(filename_len + 1); |
| 133 | memcpy(filename, arg_string, filename_len); |
| 134 | filename[filename_len] = 0; |
| 135 | unsaved->Filename = filename; |
| 136 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 137 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 141 | /******************************************************************************/ |
| 142 | /* Pretty-printing. */ |
| 143 | /******************************************************************************/ |
| 144 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 145 | static void PrintCursor(CXCursor Cursor) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 146 | if (clang_isInvalid(Cursor.kind)) { |
| 147 | CXString ks = clang_getCursorKindSpelling(Cursor.kind); |
| 148 | printf("Invalid Cursor => %s", clang_getCString(ks)); |
| 149 | clang_disposeString(ks); |
| 150 | } |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 151 | else { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 152 | CXString string, ks; |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 153 | CXCursor Referenced; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 154 | unsigned line, column; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 155 | |
| 156 | ks = clang_getCursorKindSpelling(Cursor.kind); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 157 | string = clang_getCursorSpelling(Cursor); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 158 | printf("%s=%s", clang_getCString(ks), |
| 159 | clang_getCString(string)); |
| 160 | clang_disposeString(ks); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 161 | clang_disposeString(string); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 162 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 163 | Referenced = clang_getCursorReferenced(Cursor); |
| 164 | if (!clang_equalCursors(Referenced, clang_getNullCursor())) { |
| 165 | CXSourceLocation Loc = clang_getCursorLocation(Referenced); |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 166 | clang_getInstantiationLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 167 | printf(":%d:%d", line, column); |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 168 | } |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 169 | |
| 170 | if (clang_isCursorDefinition(Cursor)) |
| 171 | printf(" (Definition)"); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 172 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 173 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 175 | static const char* GetCursorSource(CXCursor Cursor) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 176 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 177 | CXString source; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 178 | CXFile file; |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 179 | clang_getInstantiationLocation(Loc, &file, 0, 0, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 180 | source = clang_getFileName(file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 181 | if (!clang_getCString(source)) { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 182 | clang_disposeString(source); |
| 183 | return "<invalid loc>"; |
| 184 | } |
| 185 | else { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 186 | const char *b = basename(clang_getCString(source)); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 187 | clang_disposeString(source); |
| 188 | return b; |
| 189 | } |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 192 | /******************************************************************************/ |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 193 | /* Callbacks. */ |
| 194 | /******************************************************************************/ |
| 195 | |
| 196 | typedef void (*PostVisitTU)(CXTranslationUnit); |
| 197 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 198 | void PrintDiagnostic(CXDiagnostic Diagnostic) { |
| 199 | FILE *out = stderr; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 200 | CXFile file; |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 201 | CXString Msg; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 202 | unsigned display_opts = CXDiagnostic_DisplaySourceLocation |
| 203 | | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges; |
| 204 | unsigned i, num_fixits; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 205 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 206 | if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 207 | return; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 208 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 209 | Msg = clang_formatDiagnostic(Diagnostic, display_opts); |
| 210 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 211 | clang_disposeString(Msg); |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 213 | clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 214 | &file, 0, 0, 0); |
| 215 | if (!file) |
| 216 | return; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 218 | num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); |
| 219 | for (i = 0; i != num_fixits; ++i) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 220 | CXSourceRange range; |
| 221 | CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range); |
| 222 | CXSourceLocation start = clang_getRangeStart(range); |
| 223 | CXSourceLocation end = clang_getRangeEnd(range); |
| 224 | unsigned start_line, start_column, end_line, end_column; |
| 225 | CXFile start_file, end_file; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 226 | clang_getInstantiationLocation(start, &start_file, &start_line, |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 227 | &start_column, 0); |
| 228 | clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0); |
| 229 | if (clang_equalLocations(start, end)) { |
| 230 | /* Insertion. */ |
| 231 | if (start_file == file) |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 232 | fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 233 | clang_getCString(insertion_text), start_line, start_column); |
| 234 | } else if (strcmp(clang_getCString(insertion_text), "") == 0) { |
| 235 | /* Removal. */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 236 | if (start_file == file && end_file == file) { |
| 237 | fprintf(out, "FIX-IT: Remove "); |
| 238 | PrintExtent(out, start_line, start_column, end_line, end_column); |
| 239 | fprintf(out, "\n"); |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 240 | } |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 241 | } else { |
| 242 | /* Replacement. */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 243 | if (start_file == end_file) { |
| 244 | fprintf(out, "FIX-IT: Replace "); |
| 245 | PrintExtent(out, start_line, start_column, end_line, end_column); |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 246 | fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); |
Douglas Gregor | 436f3f0 | 2010-02-18 22:27:07 +0000 | [diff] [blame] | 247 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 248 | break; |
| 249 | } |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 250 | clang_disposeString(insertion_text); |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 251 | } |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 254 | void PrintDiagnostics(CXTranslationUnit TU) { |
| 255 | int i, n = clang_getNumDiagnostics(TU); |
| 256 | for (i = 0; i != n; ++i) { |
| 257 | CXDiagnostic Diag = clang_getDiagnostic(TU, i); |
| 258 | PrintDiagnostic(Diag); |
| 259 | clang_disposeDiagnostic(Diag); |
| 260 | } |
| 261 | } |
| 262 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 263 | /******************************************************************************/ |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 264 | /* Logic for testing traversal. */ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 265 | /******************************************************************************/ |
| 266 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 267 | static const char *FileCheckPrefix = "CHECK"; |
| 268 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 269 | static void PrintCursorExtent(CXCursor C) { |
| 270 | CXSourceRange extent = clang_getCursorExtent(C); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 271 | CXFile begin_file, end_file; |
| 272 | unsigned begin_line, begin_column, end_line, end_column; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 273 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 274 | clang_getInstantiationLocation(clang_getRangeStart(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 275 | &begin_file, &begin_line, &begin_column, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 276 | clang_getInstantiationLocation(clang_getRangeEnd(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 277 | &end_file, &end_line, &end_column, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 278 | if (!begin_file || !end_file) |
Ted Kremenek | 70ee542 | 2010-01-16 01:44:12 +0000 | [diff] [blame] | 279 | return; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 280 | |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 281 | printf(" Extent="); |
| 282 | PrintExtent(stdout, begin_line, begin_column, end_line, end_column); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 285 | /* Data used by all of the visitors. */ |
| 286 | typedef struct { |
| 287 | CXTranslationUnit TU; |
| 288 | enum CXCursorKind *Filter; |
| 289 | } VisitorData; |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 290 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 292 | enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 293 | CXCursor Parent, |
| 294 | CXClientData ClientData) { |
| 295 | VisitorData *Data = (VisitorData *)ClientData; |
| 296 | if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 297 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 298 | unsigned line, column; |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 299 | clang_getInstantiationLocation(Loc, 0, &line, &column, 0); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 300 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 301 | GetCursorSource(Cursor), line, column); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 302 | PrintCursor(Cursor); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 303 | PrintCursorExtent(Cursor); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 304 | printf("\n"); |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 305 | return CXChildVisit_Recurse; |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 306 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 307 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 308 | return CXChildVisit_Continue; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 309 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 311 | static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 312 | CXCursor Parent, |
| 313 | CXClientData ClientData) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 314 | const char *startBuf, *endBuf; |
| 315 | unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; |
| 316 | CXCursor Ref; |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 317 | VisitorData *Data = (VisitorData *)ClientData; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 319 | if (Cursor.kind != CXCursor_FunctionDecl || |
| 320 | !clang_isCursorDefinition(Cursor)) |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 321 | return CXChildVisit_Continue; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 322 | |
| 323 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 324 | &startLine, &startColumn, |
| 325 | &endLine, &endColumn); |
| 326 | /* Probe the entire body, looking for both decls and refs. */ |
| 327 | curLine = startLine; |
| 328 | curColumn = startColumn; |
| 329 | |
| 330 | while (startBuf < endBuf) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 331 | CXSourceLocation Loc; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 332 | CXFile file; |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 333 | CXString source; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 334 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 335 | if (*startBuf == '\n') { |
| 336 | startBuf++; |
| 337 | curLine++; |
| 338 | curColumn = 1; |
| 339 | } else if (*startBuf != '\t') |
| 340 | curColumn++; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 341 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 342 | Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 343 | clang_getInstantiationLocation(Loc, &file, 0, 0, 0); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 345 | source = clang_getFileName(file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 346 | if (clang_getCString(source)) { |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 347 | CXSourceLocation RefLoc |
| 348 | = clang_getLocation(Data->TU, file, curLine, curColumn); |
| 349 | Ref = clang_getCursor(Data->TU, RefLoc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 350 | if (Ref.kind == CXCursor_NoDeclFound) { |
| 351 | /* Nothing found here; that's fine. */ |
| 352 | } else if (Ref.kind != CXCursor_FunctionDecl) { |
| 353 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), |
| 354 | curLine, curColumn); |
| 355 | PrintCursor(Ref); |
| 356 | printf("\n"); |
| 357 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 358 | } |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 359 | clang_disposeString(source); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 360 | startBuf++; |
| 361 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 362 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 363 | return CXChildVisit_Continue; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 366 | /******************************************************************************/ |
| 367 | /* USR testing. */ |
| 368 | /******************************************************************************/ |
| 369 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 370 | enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, |
| 371 | CXClientData ClientData) { |
| 372 | VisitorData *Data = (VisitorData *)ClientData; |
| 373 | if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 374 | CXString USR = clang_getCursorUSR(C); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 375 | const char *cstr = clang_getCString(USR); |
| 376 | if (!cstr || cstr[0] == '\0') { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 377 | clang_disposeString(USR); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 378 | return CXChildVisit_Recurse; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 379 | } |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 380 | printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr); |
| 381 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 382 | PrintCursorExtent(C); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 383 | printf("\n"); |
| 384 | clang_disposeString(USR); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 385 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 386 | return CXChildVisit_Recurse; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 389 | return CXChildVisit_Continue; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /******************************************************************************/ |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 393 | /* Inclusion stack testing. */ |
| 394 | /******************************************************************************/ |
| 395 | |
| 396 | void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, |
| 397 | unsigned includeStackLen, CXClientData data) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 399 | unsigned i; |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 400 | CXString fname; |
| 401 | |
| 402 | fname = clang_getFileName(includedFile); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 403 | printf("file: %s\nincluded by:\n", clang_getCString(fname)); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 404 | clang_disposeString(fname); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 405 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 406 | for (i = 0; i < includeStackLen; ++i) { |
| 407 | CXFile includingFile; |
| 408 | unsigned line, column; |
| 409 | clang_getInstantiationLocation(includeStack[i], &includingFile, &line, |
| 410 | &column, 0); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 411 | fname = clang_getFileName(includingFile); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 412 | printf(" %s:%d:%d\n", clang_getCString(fname), line, column); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 413 | clang_disposeString(fname); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 414 | } |
| 415 | printf("\n"); |
| 416 | } |
| 417 | |
| 418 | void PrintInclusionStack(CXTranslationUnit TU) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 419 | clang_getInclusions(TU, InclusionVisitor, NULL); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /******************************************************************************/ |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 423 | /* Linkage testing. */ |
| 424 | /******************************************************************************/ |
| 425 | |
| 426 | static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, |
| 427 | CXClientData d) { |
| 428 | const char *linkage = 0; |
| 429 | |
| 430 | if (clang_isInvalid(clang_getCursorKind(cursor))) |
| 431 | return CXChildVisit_Recurse; |
| 432 | |
| 433 | switch (clang_getCursorLinkage(cursor)) { |
| 434 | case CXLinkage_Invalid: break; |
Douglas Gregor | c2a2b3c | 2010-03-04 19:36:27 +0000 | [diff] [blame] | 435 | case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; |
| 436 | case CXLinkage_Internal: linkage = "Internal"; break; |
| 437 | case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; |
| 438 | case CXLinkage_External: linkage = "External"; break; |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (linkage) { |
| 442 | PrintCursor(cursor); |
| 443 | printf("linkage=%s\n", linkage); |
| 444 | } |
| 445 | |
| 446 | return CXChildVisit_Recurse; |
| 447 | } |
| 448 | |
| 449 | /******************************************************************************/ |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 450 | /* Typekind testing. */ |
| 451 | /******************************************************************************/ |
| 452 | |
| 453 | static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p, |
| 454 | CXClientData d) { |
| 455 | |
| 456 | if (!clang_isInvalid(clang_getCursorKind(cursor))) { |
| 457 | CXType T = clang_getCursorType(cursor); |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 458 | CXString S = clang_getTypeKindSpelling(T.kind); |
| 459 | PrintCursor(cursor); |
| 460 | printf(" typekind=%s", clang_getCString(S)); |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 461 | clang_disposeString(S); |
Benjamin Kramer | e1403d2 | 2010-06-22 09:29:44 +0000 | [diff] [blame] | 462 | /* Print the canonical type if it is different. */ |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 463 | { |
| 464 | CXType CT = clang_getCanonicalType(T); |
| 465 | if (!clang_equalTypes(T, CT)) { |
| 466 | CXString CS = clang_getTypeKindSpelling(CT.kind); |
| 467 | printf(" [canonical=%s]", clang_getCString(CS)); |
| 468 | clang_disposeString(CS); |
| 469 | } |
| 470 | } |
Benjamin Kramer | e1403d2 | 2010-06-22 09:29:44 +0000 | [diff] [blame] | 471 | /* Print the return type if it exists. */ |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 472 | { |
Ted Kremenek | 9a14084 | 2010-06-21 20:48:56 +0000 | [diff] [blame] | 473 | CXType RT = clang_getCursorResultType(cursor); |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 474 | if (RT.kind != CXType_Invalid) { |
| 475 | CXString RS = clang_getTypeKindSpelling(RT.kind); |
| 476 | printf(" [result=%s]", clang_getCString(RS)); |
| 477 | clang_disposeString(RS); |
| 478 | } |
| 479 | } |
| 480 | |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 481 | printf("\n"); |
| 482 | } |
| 483 | return CXChildVisit_Recurse; |
| 484 | } |
| 485 | |
| 486 | |
| 487 | /******************************************************************************/ |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 488 | /* Loading ASTs/source. */ |
| 489 | /******************************************************************************/ |
| 490 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 491 | static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, |
Ted Kremenek | 9827156 | 2010-01-12 18:53:15 +0000 | [diff] [blame] | 492 | const char *filter, const char *prefix, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 493 | CXCursorVisitor Visitor, |
| 494 | PostVisitTU PV) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 495 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 496 | if (prefix) |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 497 | FileCheckPrefix = prefix; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 498 | |
| 499 | if (Visitor) { |
| 500 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 501 | enum CXCursorKind *ck = &K; |
| 502 | VisitorData Data; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 503 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 504 | /* Perform some simple filtering. */ |
| 505 | if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
Daniel Dunbar | b1ffee6 | 2010-02-10 20:42:40 +0000 | [diff] [blame] | 506 | else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 507 | else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| 508 | else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 509 | else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 510 | else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| 511 | else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
| 512 | else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; |
| 513 | else { |
| 514 | fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| 515 | return 1; |
| 516 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 517 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 518 | Data.TU = TU; |
| 519 | Data.Filter = ck; |
| 520 | clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 521 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 522 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 523 | if (PV) |
| 524 | PV(TU); |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 526 | PrintDiagnostics(TU); |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 527 | clang_disposeTranslationUnit(TU); |
| 528 | return 0; |
| 529 | } |
| 530 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 531 | int perform_test_load_tu(const char *file, const char *filter, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 532 | const char *prefix, CXCursorVisitor Visitor, |
| 533 | PostVisitTU PV) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 534 | CXIndex Idx; |
| 535 | CXTranslationUnit TU; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 536 | int result; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 537 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 538 | !strcmp(filter, "local") ? 1 : 0, |
| 539 | /* displayDiagnosics=*/1); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 540 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 541 | if (!CreateTranslationUnit(Idx, file, &TU)) { |
| 542 | clang_disposeIndex(Idx); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 543 | return 1; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 544 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 545 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 546 | result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV); |
| 547 | clang_disposeIndex(Idx); |
| 548 | return result; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 551 | int perform_test_load_source(int argc, const char **argv, |
| 552 | const char *filter, CXCursorVisitor Visitor, |
| 553 | PostVisitTU PV) { |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 554 | const char *UseExternalASTs = |
| 555 | getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION"); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 556 | CXIndex Idx; |
| 557 | CXTranslationUnit TU; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 558 | struct CXUnsavedFile *unsaved_files = 0; |
| 559 | int num_unsaved_files = 0; |
| 560 | int result; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 561 | |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 562 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 563 | !strcmp(filter, "local") ? 1 : 0, |
| 564 | /* displayDiagnosics=*/1); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 565 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 566 | if (UseExternalASTs && strlen(UseExternalASTs)) |
| 567 | clang_setUseExternalASTGeneration(Idx, 1); |
| 568 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 569 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 570 | clang_disposeIndex(Idx); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 571 | return -1; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 572 | } |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 573 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 574 | TU = clang_createTranslationUnitFromSourceFile(Idx, 0, |
| 575 | argc - num_unsaved_files, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 576 | argv + num_unsaved_files, |
| 577 | num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 578 | unsaved_files); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 579 | if (!TU) { |
| 580 | fprintf(stderr, "Unable to load translation unit!\n"); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 581 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 582 | clang_disposeIndex(Idx); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 583 | return 1; |
| 584 | } |
| 585 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 586 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 587 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 588 | clang_disposeIndex(Idx); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 589 | return result; |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 592 | int perform_test_reparse_source(int argc, const char **argv, int trials, |
| 593 | const char *filter, CXCursorVisitor Visitor, |
| 594 | PostVisitTU PV) { |
| 595 | const char *UseExternalASTs = |
| 596 | getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION"); |
| 597 | CXIndex Idx; |
| 598 | CXTranslationUnit TU; |
| 599 | struct CXUnsavedFile *unsaved_files = 0; |
| 600 | int num_unsaved_files = 0; |
| 601 | int result; |
| 602 | int trial; |
| 603 | |
| 604 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| 605 | !strcmp(filter, "local") ? 1 : 0, |
| 606 | /* displayDiagnosics=*/1); |
| 607 | |
| 608 | if (UseExternalASTs && strlen(UseExternalASTs)) |
| 609 | clang_setUseExternalASTGeneration(Idx, 1); |
| 610 | |
| 611 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 612 | clang_disposeIndex(Idx); |
| 613 | return -1; |
| 614 | } |
| 615 | |
| 616 | TU = clang_createTranslationUnitFromSourceFile(Idx, 0, |
| 617 | argc - num_unsaved_files, |
| 618 | argv + num_unsaved_files, |
| 619 | num_unsaved_files, |
| 620 | unsaved_files); |
| 621 | if (!TU) { |
| 622 | fprintf(stderr, "Unable to load translation unit!\n"); |
| 623 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 624 | clang_disposeIndex(Idx); |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | for (trial = 0; trial < trials; ++trial) { |
| 629 | if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files)) { |
| 630 | clang_disposeTranslationUnit(TU); |
| 631 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 632 | clang_disposeIndex(Idx); |
| 633 | return -1; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); |
| 638 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 639 | clang_disposeIndex(Idx); |
| 640 | return result; |
| 641 | } |
| 642 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 643 | /******************************************************************************/ |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 644 | /* Logic for testing clang_getCursor(). */ |
| 645 | /******************************************************************************/ |
| 646 | |
| 647 | static void print_cursor_file_scan(CXCursor cursor, |
| 648 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 649 | unsigned end_line, unsigned end_col, |
| 650 | const char *prefix) { |
Ted Kremenek | 9096a20 | 2010-01-07 01:17:12 +0000 | [diff] [blame] | 651 | printf("// %s: ", FileCheckPrefix); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 652 | if (prefix) |
| 653 | printf("-%s", prefix); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 654 | PrintExtent(stdout, start_line, start_col, end_line, end_col); |
| 655 | printf(" "); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 656 | PrintCursor(cursor); |
| 657 | printf("\n"); |
| 658 | } |
| 659 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 660 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 661 | const char *prefix) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 662 | CXIndex Idx; |
| 663 | CXTranslationUnit TU; |
| 664 | FILE *fp; |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 665 | CXCursor prevCursor = clang_getNullCursor(); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 666 | CXFile file; |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 667 | unsigned line = 1, col = 1; |
Daniel Dunbar | 8f0bf81 | 2010-02-14 08:32:51 +0000 | [diff] [blame] | 668 | unsigned start_line = 1, start_col = 1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 669 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 670 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
| 671 | /* displayDiagnosics=*/1))) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 672 | fprintf(stderr, "Could not create Index\n"); |
| 673 | return 1; |
| 674 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 676 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 677 | return 1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 678 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 679 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 680 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 681 | return 1; |
| 682 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 684 | file = clang_getFile(TU, source_file); |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 685 | for (;;) { |
| 686 | CXCursor cursor; |
| 687 | int c = fgetc(fp); |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 688 | |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 689 | if (c == '\n') { |
| 690 | ++line; |
| 691 | col = 1; |
| 692 | } else |
| 693 | ++col; |
| 694 | |
| 695 | /* Check the cursor at this position, and dump the previous one if we have |
| 696 | * found something new. |
| 697 | */ |
| 698 | cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); |
| 699 | if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && |
| 700 | prevCursor.kind != CXCursor_InvalidFile) { |
| 701 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 702 | line, col, prefix); |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 703 | start_line = line; |
| 704 | start_col = col; |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 705 | } |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 706 | if (c == EOF) |
| 707 | break; |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 708 | |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 709 | prevCursor = cursor; |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 710 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 711 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 712 | fclose(fp); |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 717 | /* Logic for testing clang_codeComplete(). */ |
| 718 | /******************************************************************************/ |
| 719 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 720 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 721 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 722 | memory (that will be owned by the caller) to store the file name. */ |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 723 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 724 | unsigned *column, unsigned *second_line, |
| 725 | unsigned *second_column) { |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 726 | /* Find the second colon. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 727 | const char *last_colon = strrchr(input, ':'); |
| 728 | unsigned values[4], i; |
| 729 | unsigned num_values = (second_line && second_column)? 4 : 2; |
| 730 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 731 | char *endptr = 0; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 732 | if (!last_colon || last_colon == input) { |
| 733 | if (num_values == 4) |
| 734 | fprintf(stderr, "could not parse filename:line:column:line:column in " |
| 735 | "'%s'\n", input); |
| 736 | else |
| 737 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 738 | return 1; |
| 739 | } |
| 740 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 741 | for (i = 0; i != num_values; ++i) { |
| 742 | const char *prev_colon; |
| 743 | |
| 744 | /* Parse the next line or column. */ |
| 745 | values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); |
| 746 | if (*endptr != 0 && *endptr != ':') { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 747 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 748 | (i % 2 ? "column" : "line"), input); |
| 749 | return 1; |
| 750 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 751 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 752 | if (i + 1 == num_values) |
| 753 | break; |
| 754 | |
| 755 | /* Find the previous colon. */ |
| 756 | prev_colon = last_colon - 1; |
| 757 | while (prev_colon != input && *prev_colon != ':') |
| 758 | --prev_colon; |
| 759 | if (prev_colon == input) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 760 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 761 | (i % 2 == 0? "column" : "line"), input); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 762 | return 1; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | last_colon = prev_colon; |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 768 | *line = values[0]; |
| 769 | *column = values[1]; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 770 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 771 | if (second_line && second_column) { |
| 772 | *second_line = values[2]; |
| 773 | *second_column = values[3]; |
| 774 | } |
| 775 | |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 776 | /* Copy the file name. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 777 | *filename = (char*)malloc(last_colon - input + 1); |
| 778 | memcpy(*filename, input, last_colon - input); |
| 779 | (*filename)[last_colon - input] = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | const char * |
| 784 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 785 | switch (Kind) { |
| 786 | case CXCompletionChunk_Optional: return "Optional"; |
| 787 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 788 | case CXCompletionChunk_Text: return "Text"; |
| 789 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 790 | case CXCompletionChunk_Informative: return "Informative"; |
| 791 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 792 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 793 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 794 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 795 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 796 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 797 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 798 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 799 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 800 | case CXCompletionChunk_Comma: return "Comma"; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 801 | case CXCompletionChunk_ResultType: return "ResultType"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 802 | case CXCompletionChunk_Colon: return "Colon"; |
| 803 | case CXCompletionChunk_SemiColon: return "SemiColon"; |
| 804 | case CXCompletionChunk_Equal: return "Equal"; |
| 805 | case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; |
| 806 | case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 807 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 808 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 809 | return "Unknown"; |
| 810 | } |
| 811 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 812 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 813 | int I, N; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 814 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 815 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 816 | for (I = 0; I != N; ++I) { |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 817 | CXString text; |
| 818 | const char *cstr; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 819 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 820 | = clang_getCompletionChunkKind(completion_string, I); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 821 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 822 | if (Kind == CXCompletionChunk_Optional) { |
| 823 | fprintf(file, "{Optional "); |
| 824 | print_completion_string( |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 825 | clang_getCompletionChunkCompletionString(completion_string, I), |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 826 | file); |
| 827 | fprintf(file, "}"); |
| 828 | continue; |
| 829 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 831 | text = clang_getCompletionChunkText(completion_string, I); |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 832 | cstr = clang_getCString(text); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 833 | fprintf(file, "{%s %s}", |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 834 | clang_getCompletionChunkKindSpelling(Kind), |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 835 | cstr ? cstr : ""); |
| 836 | clang_disposeString(text); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 837 | } |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | void print_completion_result(CXCompletionResult *completion_result, |
| 842 | CXClientData client_data) { |
| 843 | FILE *file = (FILE *)client_data; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 844 | CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); |
| 845 | |
| 846 | fprintf(file, "%s:", clang_getCString(ks)); |
| 847 | clang_disposeString(ks); |
| 848 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 849 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 850 | fprintf(file, " (%u)\n", |
| 851 | clang_getCompletionPriority(completion_result->CompletionString)); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 854 | int perform_code_completion(int argc, const char **argv, int timing_only) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 855 | const char *input = argv[1]; |
| 856 | char *filename = 0; |
| 857 | unsigned line; |
| 858 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 859 | CXIndex CIdx; |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 860 | int errorCode; |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 861 | struct CXUnsavedFile *unsaved_files = 0; |
| 862 | int num_unsaved_files = 0; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 863 | CXCodeCompleteResults *results = 0; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 864 | |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 865 | if (timing_only) |
| 866 | input += strlen("-code-completion-timing="); |
| 867 | else |
| 868 | input += strlen("-code-completion-at="); |
| 869 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 870 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 871 | 0, 0))) |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 872 | return errorCode; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 874 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 875 | return -1; |
| 876 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 877 | CIdx = clang_createIndex(0, 1); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 878 | results = clang_codeComplete(CIdx, |
| 879 | argv[argc - 1], argc - num_unsaved_files - 3, |
| 880 | argv + num_unsaved_files + 2, |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 881 | num_unsaved_files, unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 882 | filename, line, column); |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 883 | |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 884 | if (results) { |
| 885 | unsigned i, n = results->NumResults; |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 886 | if (!timing_only) |
| 887 | for (i = 0; i != n; ++i) |
| 888 | print_completion_result(results->Results + i, stdout); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 889 | n = clang_codeCompleteGetNumDiagnostics(results); |
| 890 | for (i = 0; i != n; ++i) { |
| 891 | CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); |
| 892 | PrintDiagnostic(diag); |
| 893 | clang_disposeDiagnostic(diag); |
| 894 | } |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 895 | clang_disposeCodeCompleteResults(results); |
| 896 | } |
| 897 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 898 | clang_disposeIndex(CIdx); |
| 899 | free(filename); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 900 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 901 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 902 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 903 | return 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 906 | typedef struct { |
| 907 | char *filename; |
| 908 | unsigned line; |
| 909 | unsigned column; |
| 910 | } CursorSourceLocation; |
| 911 | |
| 912 | int inspect_cursor_at(int argc, const char **argv) { |
| 913 | CXIndex CIdx; |
| 914 | int errorCode; |
| 915 | struct CXUnsavedFile *unsaved_files = 0; |
| 916 | int num_unsaved_files = 0; |
| 917 | CXTranslationUnit TU; |
| 918 | CXCursor Cursor; |
| 919 | CursorSourceLocation *Locations = 0; |
| 920 | unsigned NumLocations = 0, Loc; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 921 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 922 | /* Count the number of locations. */ |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 923 | while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) |
| 924 | ++NumLocations; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 925 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 926 | /* Parse the locations. */ |
| 927 | assert(NumLocations > 0 && "Unable to count locations?"); |
| 928 | Locations = (CursorSourceLocation *)malloc( |
| 929 | NumLocations * sizeof(CursorSourceLocation)); |
| 930 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 931 | const char *input = argv[Loc + 1] + strlen("-cursor-at="); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 932 | if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| 933 | &Locations[Loc].line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 934 | &Locations[Loc].column, 0, 0))) |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 935 | return errorCode; |
| 936 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 937 | |
| 938 | if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 939 | &num_unsaved_files)) |
| 940 | return -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 941 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 942 | CIdx = clang_createIndex(0, 1); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 943 | TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], |
| 944 | argc - num_unsaved_files - 2 - NumLocations, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 945 | argv + num_unsaved_files + 1 + NumLocations, |
| 946 | num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 947 | unsaved_files); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 948 | if (!TU) { |
| 949 | fprintf(stderr, "unable to parse input\n"); |
| 950 | return -1; |
| 951 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 952 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 953 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 954 | CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| 955 | if (!file) |
| 956 | continue; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 957 | |
| 958 | Cursor = clang_getCursor(TU, |
| 959 | clang_getLocation(TU, file, Locations[Loc].line, |
| 960 | Locations[Loc].column)); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 961 | PrintCursor(Cursor); |
| 962 | printf("\n"); |
| 963 | free(Locations[Loc].filename); |
| 964 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 965 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 966 | PrintDiagnostics(TU); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 967 | clang_disposeTranslationUnit(TU); |
| 968 | clang_disposeIndex(CIdx); |
| 969 | free(Locations); |
| 970 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 971 | return 0; |
| 972 | } |
| 973 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 974 | int perform_token_annotation(int argc, const char **argv) { |
| 975 | const char *input = argv[1]; |
| 976 | char *filename = 0; |
| 977 | unsigned line, second_line; |
| 978 | unsigned column, second_column; |
| 979 | CXIndex CIdx; |
| 980 | CXTranslationUnit TU = 0; |
| 981 | int errorCode; |
| 982 | struct CXUnsavedFile *unsaved_files = 0; |
| 983 | int num_unsaved_files = 0; |
| 984 | CXToken *tokens; |
| 985 | unsigned num_tokens; |
| 986 | CXSourceRange range; |
| 987 | CXSourceLocation startLoc, endLoc; |
| 988 | CXFile file = 0; |
| 989 | CXCursor *cursors = 0; |
| 990 | unsigned i; |
| 991 | |
| 992 | input += strlen("-test-annotate-tokens="); |
| 993 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
| 994 | &second_line, &second_column))) |
| 995 | return errorCode; |
| 996 | |
| 997 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 998 | return -1; |
| 999 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1000 | CIdx = clang_createIndex(0, 1); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1001 | TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], |
| 1002 | argc - num_unsaved_files - 3, |
| 1003 | argv + num_unsaved_files + 2, |
| 1004 | num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1005 | unsaved_files); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1006 | if (!TU) { |
| 1007 | fprintf(stderr, "unable to parse input\n"); |
| 1008 | clang_disposeIndex(CIdx); |
| 1009 | free(filename); |
| 1010 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1011 | return -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1012 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1013 | errorCode = 0; |
| 1014 | |
| 1015 | file = clang_getFile(TU, filename); |
| 1016 | if (!file) { |
| 1017 | fprintf(stderr, "file %s is not in this translation unit\n", filename); |
| 1018 | errorCode = -1; |
| 1019 | goto teardown; |
| 1020 | } |
| 1021 | |
| 1022 | startLoc = clang_getLocation(TU, file, line, column); |
| 1023 | if (clang_equalLocations(clang_getNullLocation(), startLoc)) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1024 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1025 | column); |
| 1026 | errorCode = -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1027 | goto teardown; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | endLoc = clang_getLocation(TU, file, second_line, second_column); |
| 1031 | if (clang_equalLocations(clang_getNullLocation(), endLoc)) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1032 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1033 | second_line, second_column); |
| 1034 | errorCode = -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1035 | goto teardown; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | range = clang_getRange(startLoc, endLoc); |
| 1039 | clang_tokenize(TU, range, &tokens, &num_tokens); |
| 1040 | cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); |
| 1041 | clang_annotateTokens(TU, tokens, num_tokens, cursors); |
| 1042 | for (i = 0; i != num_tokens; ++i) { |
| 1043 | const char *kind = "<unknown>"; |
| 1044 | CXString spelling = clang_getTokenSpelling(TU, tokens[i]); |
| 1045 | CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); |
| 1046 | unsigned start_line, start_column, end_line, end_column; |
| 1047 | |
| 1048 | switch (clang_getTokenKind(tokens[i])) { |
| 1049 | case CXToken_Punctuation: kind = "Punctuation"; break; |
| 1050 | case CXToken_Keyword: kind = "Keyword"; break; |
| 1051 | case CXToken_Identifier: kind = "Identifier"; break; |
| 1052 | case CXToken_Literal: kind = "Literal"; break; |
| 1053 | case CXToken_Comment: kind = "Comment"; break; |
| 1054 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1055 | clang_getInstantiationLocation(clang_getRangeStart(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 1056 | 0, &start_line, &start_column, 0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1057 | clang_getInstantiationLocation(clang_getRangeEnd(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 1058 | 0, &end_line, &end_column, 0); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 1059 | printf("%s: \"%s\" ", kind, clang_getCString(spelling)); |
| 1060 | PrintExtent(stdout, start_line, start_column, end_line, end_column); |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 1061 | if (!clang_isInvalid(cursors[i].kind)) { |
| 1062 | printf(" "); |
| 1063 | PrintCursor(cursors[i]); |
| 1064 | } |
| 1065 | printf("\n"); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1066 | } |
| 1067 | free(cursors); |
| 1068 | |
| 1069 | teardown: |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1070 | PrintDiagnostics(TU); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1071 | clang_disposeTranslationUnit(TU); |
| 1072 | clang_disposeIndex(CIdx); |
| 1073 | free(filename); |
| 1074 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1075 | return errorCode; |
| 1076 | } |
| 1077 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1078 | /******************************************************************************/ |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 1079 | /* USR printing. */ |
| 1080 | /******************************************************************************/ |
| 1081 | |
| 1082 | static int insufficient_usr(const char *kind, const char *usage) { |
| 1083 | fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage); |
| 1084 | return 1; |
| 1085 | } |
| 1086 | |
| 1087 | static unsigned isUSR(const char *s) { |
| 1088 | return s[0] == 'c' && s[1] == ':'; |
| 1089 | } |
| 1090 | |
| 1091 | static int not_usr(const char *s, const char *arg) { |
| 1092 | fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg); |
| 1093 | return 1; |
| 1094 | } |
| 1095 | |
| 1096 | static void print_usr(CXString usr) { |
| 1097 | const char *s = clang_getCString(usr); |
| 1098 | printf("%s\n", s); |
| 1099 | clang_disposeString(usr); |
| 1100 | } |
| 1101 | |
| 1102 | static void display_usrs() { |
| 1103 | fprintf(stderr, "-print-usrs options:\n" |
| 1104 | " ObjCCategory <class name> <category name>\n" |
| 1105 | " ObjCClass <class name>\n" |
| 1106 | " ObjCIvar <ivar name> <class USR>\n" |
| 1107 | " ObjCMethod <selector> [0=class method|1=instance method] " |
| 1108 | "<class USR>\n" |
| 1109 | " ObjCProperty <property name> <class USR>\n" |
| 1110 | " ObjCProtocol <protocol name>\n"); |
| 1111 | } |
| 1112 | |
| 1113 | int print_usrs(const char **I, const char **E) { |
| 1114 | while (I != E) { |
| 1115 | const char *kind = *I; |
| 1116 | unsigned len = strlen(kind); |
| 1117 | switch (len) { |
| 1118 | case 8: |
| 1119 | if (memcmp(kind, "ObjCIvar", 8) == 0) { |
| 1120 | if (I + 2 >= E) |
| 1121 | return insufficient_usr(kind, "<ivar name> <class USR>"); |
| 1122 | if (!isUSR(I[2])) |
| 1123 | return not_usr("<class USR>", I[2]); |
| 1124 | else { |
| 1125 | CXString x; |
| 1126 | x.Spelling = I[2]; |
| 1127 | x.MustFreeString = 0; |
| 1128 | print_usr(clang_constructUSR_ObjCIvar(I[1], x)); |
| 1129 | } |
| 1130 | |
| 1131 | I += 3; |
| 1132 | continue; |
| 1133 | } |
| 1134 | break; |
| 1135 | case 9: |
| 1136 | if (memcmp(kind, "ObjCClass", 9) == 0) { |
| 1137 | if (I + 1 >= E) |
| 1138 | return insufficient_usr(kind, "<class name>"); |
| 1139 | print_usr(clang_constructUSR_ObjCClass(I[1])); |
| 1140 | I += 2; |
| 1141 | continue; |
| 1142 | } |
| 1143 | break; |
| 1144 | case 10: |
| 1145 | if (memcmp(kind, "ObjCMethod", 10) == 0) { |
| 1146 | if (I + 3 >= E) |
| 1147 | return insufficient_usr(kind, "<method selector> " |
| 1148 | "[0=class method|1=instance method] <class USR>"); |
| 1149 | if (!isUSR(I[3])) |
| 1150 | return not_usr("<class USR>", I[3]); |
| 1151 | else { |
| 1152 | CXString x; |
| 1153 | x.Spelling = I[3]; |
| 1154 | x.MustFreeString = 0; |
| 1155 | print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x)); |
| 1156 | } |
| 1157 | I += 4; |
| 1158 | continue; |
| 1159 | } |
| 1160 | break; |
| 1161 | case 12: |
| 1162 | if (memcmp(kind, "ObjCCategory", 12) == 0) { |
| 1163 | if (I + 2 >= E) |
| 1164 | return insufficient_usr(kind, "<class name> <category name>"); |
| 1165 | print_usr(clang_constructUSR_ObjCCategory(I[1], I[2])); |
| 1166 | I += 3; |
| 1167 | continue; |
| 1168 | } |
| 1169 | if (memcmp(kind, "ObjCProtocol", 12) == 0) { |
| 1170 | if (I + 1 >= E) |
| 1171 | return insufficient_usr(kind, "<protocol name>"); |
| 1172 | print_usr(clang_constructUSR_ObjCProtocol(I[1])); |
| 1173 | I += 2; |
| 1174 | continue; |
| 1175 | } |
| 1176 | if (memcmp(kind, "ObjCProperty", 12) == 0) { |
| 1177 | if (I + 2 >= E) |
| 1178 | return insufficient_usr(kind, "<property name> <class USR>"); |
| 1179 | if (!isUSR(I[2])) |
| 1180 | return not_usr("<class USR>", I[2]); |
| 1181 | else { |
| 1182 | CXString x; |
| 1183 | x.Spelling = I[2]; |
| 1184 | x.MustFreeString = 0; |
| 1185 | print_usr(clang_constructUSR_ObjCProperty(I[1], x)); |
| 1186 | } |
| 1187 | I += 3; |
| 1188 | continue; |
| 1189 | } |
| 1190 | break; |
| 1191 | default: |
| 1192 | break; |
| 1193 | } |
| 1194 | break; |
| 1195 | } |
| 1196 | |
| 1197 | if (I != E) { |
| 1198 | fprintf(stderr, "Invalid USR kind: %s\n", *I); |
| 1199 | display_usrs(); |
| 1200 | return 1; |
| 1201 | } |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | int print_usrs_file(const char *file_name) { |
| 1206 | char line[2048]; |
| 1207 | const char *args[128]; |
| 1208 | unsigned numChars = 0; |
| 1209 | |
| 1210 | FILE *fp = fopen(file_name, "r"); |
| 1211 | if (!fp) { |
| 1212 | fprintf(stderr, "error: cannot open '%s'\n", file_name); |
| 1213 | return 1; |
| 1214 | } |
| 1215 | |
| 1216 | /* This code is not really all that safe, but it works fine for testing. */ |
| 1217 | while (!feof(fp)) { |
| 1218 | char c = fgetc(fp); |
| 1219 | if (c == '\n') { |
| 1220 | unsigned i = 0; |
| 1221 | const char *s = 0; |
| 1222 | |
| 1223 | if (numChars == 0) |
| 1224 | continue; |
| 1225 | |
| 1226 | line[numChars] = '\0'; |
| 1227 | numChars = 0; |
| 1228 | |
| 1229 | if (line[0] == '/' && line[1] == '/') |
| 1230 | continue; |
| 1231 | |
| 1232 | s = strtok(line, " "); |
| 1233 | while (s) { |
| 1234 | args[i] = s; |
| 1235 | ++i; |
| 1236 | s = strtok(0, " "); |
| 1237 | } |
| 1238 | if (print_usrs(&args[0], &args[i])) |
| 1239 | return 1; |
| 1240 | } |
| 1241 | else |
| 1242 | line[numChars++] = c; |
| 1243 | } |
| 1244 | |
| 1245 | fclose(fp); |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1250 | /* Command line processing. */ |
| 1251 | /******************************************************************************/ |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1253 | static CXCursorVisitor GetVisitor(const char *s) { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1254 | if (s[0] == '\0') |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1255 | return FilteredPrintingVisitor; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1256 | if (strcmp(s, "-usrs") == 0) |
| 1257 | return USRVisitor; |
| 1258 | return NULL; |
| 1259 | } |
| 1260 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1261 | static void print_usage(void) { |
| 1262 | fprintf(stderr, |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1263 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1264 | " c-index-test -code-completion-timing=<site> <compiler arguments>\n" |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1265 | " c-index-test -cursor-at=<site> <compiler arguments>\n" |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1266 | " c-index-test -test-file-scan <AST file> <source file> " |
| 1267 | "[FileCheck prefix]\n" |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1268 | " c-index-test -test-load-tu <AST file> <symbol filter> " |
| 1269 | "[FileCheck prefix]\n" |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1270 | " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " |
| 1271 | "[FileCheck prefix]\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1272 | " c-index-test -test-load-source <symbol filter> {<args>}*\n"); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1273 | fprintf(stderr, |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1274 | " c-index-test -test-load-source-reparse <trials> <symbol filter> " |
| 1275 | " {<args>}*\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1276 | " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n" |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1277 | " c-index-test -test-annotate-tokens=<range> {<args>}*\n" |
| 1278 | " c-index-test -test-inclusion-stack-source {<args>}*\n" |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1279 | " c-index-test -test-inclusion-stack-tu <AST file>\n" |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 1280 | " c-index-test -test-print-linkage-source {<args>}*\n" |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1281 | " c-index-test -test-print-typekind {<args>}*\n" |
Chandler Carruth | 53513d2 | 2010-07-22 06:29:13 +0000 | [diff] [blame^] | 1282 | " c-index-test -print-usr [<CursorKind> {<args>}]*\n"); |
| 1283 | fprintf(stderr, |
Douglas Gregor | caf4bd3 | 2010-07-20 14:34:35 +0000 | [diff] [blame] | 1284 | " c-index-test -print-usr-file <file>\n\n"); |
| 1285 | fprintf(stderr, |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1286 | " <symbol filter> values:\n%s", |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1287 | " all - load all symbols, including those from PCH\n" |
| 1288 | " local - load all symbols except those in PCH\n" |
| 1289 | " category - only load ObjC categories (non-PCH)\n" |
| 1290 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 1291 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 1292 | " function - only load functions (non-PCH)\n" |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1293 | " typedef - only load typdefs (non-PCH)\n" |
| 1294 | " scan-function - scan function bodies (non-PCH)\n\n"); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | int main(int argc, const char **argv) { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1298 | clang_enableStackTraces(); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1299 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1300 | return perform_code_completion(argc, argv, 0); |
| 1301 | if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1]) |
| 1302 | return perform_code_completion(argc, argv, 1); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1303 | if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) |
| 1304 | return inspect_cursor_at(argc, argv); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1305 | else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1306 | CXCursorVisitor I = GetVisitor(argv[1] + 13); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1307 | if (I) |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1308 | return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, |
| 1309 | NULL); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1310 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1311 | else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){ |
| 1312 | CXCursorVisitor I = GetVisitor(argv[1] + 25); |
| 1313 | if (I) { |
| 1314 | int trials = atoi(argv[2]); |
| 1315 | return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I, |
| 1316 | NULL); |
| 1317 | } |
| 1318 | } |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1319 | else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1320 | CXCursorVisitor I = GetVisitor(argv[1] + 17); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1321 | if (I) |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1322 | 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] | 1323 | } |
| 1324 | else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1325 | return perform_file_scan(argv[2], argv[3], |
| 1326 | argc >= 5 ? argv[4] : 0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1327 | else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) |
| 1328 | return perform_token_annotation(argc, argv); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1329 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) |
| 1330 | return perform_test_load_source(argc - 2, argv + 2, "all", NULL, |
| 1331 | PrintInclusionStack); |
| 1332 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) |
| 1333 | return perform_test_load_tu(argv[2], "all", NULL, NULL, |
| 1334 | PrintInclusionStack); |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1335 | else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0) |
| 1336 | return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage, |
| 1337 | NULL); |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1338 | else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0) |
| 1339 | return perform_test_load_source(argc - 2, argv + 2, "all", |
| 1340 | PrintTypeKind, 0); |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 1341 | else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { |
| 1342 | if (argc > 2) |
| 1343 | return print_usrs(argv + 2, argv + argc); |
| 1344 | else { |
| 1345 | display_usrs(); |
| 1346 | return 1; |
| 1347 | } |
| 1348 | } |
| 1349 | else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0) |
| 1350 | return print_usrs_file(argv[2]); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1351 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1352 | print_usage(); |
| 1353 | return 1; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 1354 | } |