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 | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 375 | if (!clang_getCString(USR)) { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 376 | clang_disposeString(USR); |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 377 | return CXChildVisit_Continue; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 378 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 379 | printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), |
| 380 | clang_getCString(USR)); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 381 | PrintCursorExtent(C); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 382 | printf("\n"); |
| 383 | clang_disposeString(USR); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 384 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 385 | return CXChildVisit_Recurse; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 388 | return CXChildVisit_Continue; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /******************************************************************************/ |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 392 | /* Inclusion stack testing. */ |
| 393 | /******************************************************************************/ |
| 394 | |
| 395 | void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, |
| 396 | unsigned includeStackLen, CXClientData data) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 397 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 398 | unsigned i; |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 399 | CXString fname; |
| 400 | |
| 401 | fname = clang_getFileName(includedFile); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 402 | printf("file: %s\nincluded by:\n", clang_getCString(fname)); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 403 | clang_disposeString(fname); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 405 | for (i = 0; i < includeStackLen; ++i) { |
| 406 | CXFile includingFile; |
| 407 | unsigned line, column; |
| 408 | clang_getInstantiationLocation(includeStack[i], &includingFile, &line, |
| 409 | &column, 0); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 410 | fname = clang_getFileName(includingFile); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 411 | printf(" %s:%d:%d\n", clang_getCString(fname), line, column); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 412 | clang_disposeString(fname); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 413 | } |
| 414 | printf("\n"); |
| 415 | } |
| 416 | |
| 417 | void PrintInclusionStack(CXTranslationUnit TU) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 418 | clang_getInclusions(TU, InclusionVisitor, NULL); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /******************************************************************************/ |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 422 | /* Linkage testing. */ |
| 423 | /******************************************************************************/ |
| 424 | |
| 425 | static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, |
| 426 | CXClientData d) { |
| 427 | const char *linkage = 0; |
| 428 | |
| 429 | if (clang_isInvalid(clang_getCursorKind(cursor))) |
| 430 | return CXChildVisit_Recurse; |
| 431 | |
| 432 | switch (clang_getCursorLinkage(cursor)) { |
| 433 | case CXLinkage_Invalid: break; |
Douglas Gregor | c2a2b3c | 2010-03-04 19:36:27 +0000 | [diff] [blame] | 434 | case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; |
| 435 | case CXLinkage_Internal: linkage = "Internal"; break; |
| 436 | case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; |
| 437 | case CXLinkage_External: linkage = "External"; break; |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | if (linkage) { |
| 441 | PrintCursor(cursor); |
| 442 | printf("linkage=%s\n", linkage); |
| 443 | } |
| 444 | |
| 445 | return CXChildVisit_Recurse; |
| 446 | } |
| 447 | |
| 448 | /******************************************************************************/ |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 449 | /* Loading ASTs/source. */ |
| 450 | /******************************************************************************/ |
| 451 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 452 | static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, |
Ted Kremenek | 9827156 | 2010-01-12 18:53:15 +0000 | [diff] [blame] | 453 | const char *filter, const char *prefix, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 454 | CXCursorVisitor Visitor, |
| 455 | PostVisitTU PV) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 456 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 457 | if (prefix) |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 458 | FileCheckPrefix = prefix; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 459 | |
| 460 | if (Visitor) { |
| 461 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 462 | enum CXCursorKind *ck = &K; |
| 463 | VisitorData Data; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 465 | /* Perform some simple filtering. */ |
| 466 | if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
Daniel Dunbar | b1ffee6 | 2010-02-10 20:42:40 +0000 | [diff] [blame] | 467 | else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 468 | else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| 469 | else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 470 | else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 471 | else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| 472 | else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
| 473 | else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; |
| 474 | else { |
| 475 | fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| 476 | return 1; |
| 477 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 478 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 479 | Data.TU = TU; |
| 480 | Data.Filter = ck; |
| 481 | clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 482 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 484 | if (PV) |
| 485 | PV(TU); |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 487 | PrintDiagnostics(TU); |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 488 | clang_disposeTranslationUnit(TU); |
| 489 | return 0; |
| 490 | } |
| 491 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 492 | int perform_test_load_tu(const char *file, const char *filter, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 493 | const char *prefix, CXCursorVisitor Visitor, |
| 494 | PostVisitTU PV) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 495 | CXIndex Idx; |
| 496 | CXTranslationUnit TU; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 497 | int result; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 498 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 499 | !strcmp(filter, "local") ? 1 : 0, |
| 500 | /* displayDiagnosics=*/1); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 501 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 502 | if (!CreateTranslationUnit(Idx, file, &TU)) { |
| 503 | clang_disposeIndex(Idx); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 504 | return 1; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 505 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 506 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 507 | result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV); |
| 508 | clang_disposeIndex(Idx); |
| 509 | return result; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 512 | int perform_test_load_source(int argc, const char **argv, |
| 513 | const char *filter, CXCursorVisitor Visitor, |
| 514 | PostVisitTU PV) { |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 515 | const char *UseExternalASTs = |
| 516 | getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION"); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 517 | CXIndex Idx; |
| 518 | CXTranslationUnit TU; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 519 | struct CXUnsavedFile *unsaved_files = 0; |
| 520 | int num_unsaved_files = 0; |
| 521 | int result; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 522 | |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 523 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 524 | !strcmp(filter, "local") ? 1 : 0, |
| 525 | /* displayDiagnosics=*/1); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 526 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 527 | if (UseExternalASTs && strlen(UseExternalASTs)) |
| 528 | clang_setUseExternalASTGeneration(Idx, 1); |
| 529 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 530 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 531 | clang_disposeIndex(Idx); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 532 | return -1; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 533 | } |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 534 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 535 | TU = clang_createTranslationUnitFromSourceFile(Idx, 0, |
| 536 | argc - num_unsaved_files, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 537 | argv + num_unsaved_files, |
| 538 | num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 539 | unsaved_files); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 540 | if (!TU) { |
| 541 | fprintf(stderr, "Unable to load translation unit!\n"); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 542 | clang_disposeIndex(Idx); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 543 | return 1; |
| 544 | } |
| 545 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 546 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 547 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 548 | clang_disposeIndex(Idx); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 549 | return result; |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 552 | /******************************************************************************/ |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 553 | /* Logic for testing clang_getCursor(). */ |
| 554 | /******************************************************************************/ |
| 555 | |
| 556 | static void print_cursor_file_scan(CXCursor cursor, |
| 557 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 558 | unsigned end_line, unsigned end_col, |
| 559 | const char *prefix) { |
Ted Kremenek | 9096a20 | 2010-01-07 01:17:12 +0000 | [diff] [blame] | 560 | printf("// %s: ", FileCheckPrefix); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 561 | if (prefix) |
| 562 | printf("-%s", prefix); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 563 | PrintExtent(stdout, start_line, start_col, end_line, end_col); |
| 564 | printf(" "); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 565 | PrintCursor(cursor); |
| 566 | printf("\n"); |
| 567 | } |
| 568 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 569 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 570 | const char *prefix) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 571 | CXIndex Idx; |
| 572 | CXTranslationUnit TU; |
| 573 | FILE *fp; |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 574 | CXCursor prevCursor = clang_getNullCursor(); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 575 | CXFile file; |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 576 | unsigned line = 1, col = 1; |
Daniel Dunbar | 8f0bf81 | 2010-02-14 08:32:51 +0000 | [diff] [blame] | 577 | unsigned start_line = 1, start_col = 1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 578 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 579 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
| 580 | /* displayDiagnosics=*/1))) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 581 | fprintf(stderr, "Could not create Index\n"); |
| 582 | return 1; |
| 583 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 584 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 585 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 586 | return 1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 587 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 588 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 589 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 590 | return 1; |
| 591 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 593 | file = clang_getFile(TU, source_file); |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 594 | for (;;) { |
| 595 | CXCursor cursor; |
| 596 | int c = fgetc(fp); |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 597 | |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 598 | if (c == '\n') { |
| 599 | ++line; |
| 600 | col = 1; |
| 601 | } else |
| 602 | ++col; |
| 603 | |
| 604 | /* Check the cursor at this position, and dump the previous one if we have |
| 605 | * found something new. |
| 606 | */ |
| 607 | cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); |
| 608 | if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && |
| 609 | prevCursor.kind != CXCursor_InvalidFile) { |
| 610 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 611 | line, col, prefix); |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 612 | start_line = line; |
| 613 | start_col = col; |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 614 | } |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 615 | if (c == EOF) |
| 616 | break; |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 617 | |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 618 | prevCursor = cursor; |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 619 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 620 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 621 | fclose(fp); |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 626 | /* Logic for testing clang_codeComplete(). */ |
| 627 | /******************************************************************************/ |
| 628 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 629 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 630 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 631 | 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] | 632 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 633 | unsigned *column, unsigned *second_line, |
| 634 | unsigned *second_column) { |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 635 | /* Find the second colon. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 636 | const char *last_colon = strrchr(input, ':'); |
| 637 | unsigned values[4], i; |
| 638 | unsigned num_values = (second_line && second_column)? 4 : 2; |
| 639 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 640 | char *endptr = 0; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 641 | if (!last_colon || last_colon == input) { |
| 642 | if (num_values == 4) |
| 643 | fprintf(stderr, "could not parse filename:line:column:line:column in " |
| 644 | "'%s'\n", input); |
| 645 | else |
| 646 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 647 | return 1; |
| 648 | } |
| 649 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 650 | for (i = 0; i != num_values; ++i) { |
| 651 | const char *prev_colon; |
| 652 | |
| 653 | /* Parse the next line or column. */ |
| 654 | values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); |
| 655 | if (*endptr != 0 && *endptr != ':') { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 656 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 657 | (i % 2 ? "column" : "line"), input); |
| 658 | return 1; |
| 659 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 661 | if (i + 1 == num_values) |
| 662 | break; |
| 663 | |
| 664 | /* Find the previous colon. */ |
| 665 | prev_colon = last_colon - 1; |
| 666 | while (prev_colon != input && *prev_colon != ':') |
| 667 | --prev_colon; |
| 668 | if (prev_colon == input) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 669 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 670 | (i % 2 == 0? "column" : "line"), input); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 671 | return 1; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | last_colon = prev_colon; |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 677 | *line = values[0]; |
| 678 | *column = values[1]; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 679 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 680 | if (second_line && second_column) { |
| 681 | *second_line = values[2]; |
| 682 | *second_column = values[3]; |
| 683 | } |
| 684 | |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 685 | /* Copy the file name. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 686 | *filename = (char*)malloc(last_colon - input + 1); |
| 687 | memcpy(*filename, input, last_colon - input); |
| 688 | (*filename)[last_colon - input] = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | const char * |
| 693 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 694 | switch (Kind) { |
| 695 | case CXCompletionChunk_Optional: return "Optional"; |
| 696 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 697 | case CXCompletionChunk_Text: return "Text"; |
| 698 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 699 | case CXCompletionChunk_Informative: return "Informative"; |
| 700 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 701 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 702 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 703 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 704 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 705 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 706 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 707 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 708 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 709 | case CXCompletionChunk_Comma: return "Comma"; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 710 | case CXCompletionChunk_ResultType: return "ResultType"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 711 | case CXCompletionChunk_Colon: return "Colon"; |
| 712 | case CXCompletionChunk_SemiColon: return "SemiColon"; |
| 713 | case CXCompletionChunk_Equal: return "Equal"; |
| 714 | case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; |
| 715 | case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 716 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 717 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 718 | return "Unknown"; |
| 719 | } |
| 720 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 721 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 722 | int I, N; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 723 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 724 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 725 | for (I = 0; I != N; ++I) { |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 726 | CXString text; |
| 727 | const char *cstr; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 728 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 729 | = clang_getCompletionChunkKind(completion_string, I); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 730 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 731 | if (Kind == CXCompletionChunk_Optional) { |
| 732 | fprintf(file, "{Optional "); |
| 733 | print_completion_string( |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 734 | clang_getCompletionChunkCompletionString(completion_string, I), |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 735 | file); |
| 736 | fprintf(file, "}"); |
| 737 | continue; |
| 738 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 739 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 740 | text = clang_getCompletionChunkText(completion_string, I); |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 741 | cstr = clang_getCString(text); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 742 | fprintf(file, "{%s %s}", |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 743 | clang_getCompletionChunkKindSpelling(Kind), |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 744 | cstr ? cstr : ""); |
| 745 | clang_disposeString(text); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 746 | } |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | void print_completion_result(CXCompletionResult *completion_result, |
| 751 | CXClientData client_data) { |
| 752 | FILE *file = (FILE *)client_data; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 753 | CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); |
| 754 | |
| 755 | fprintf(file, "%s:", clang_getCString(ks)); |
| 756 | clang_disposeString(ks); |
| 757 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 758 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 759 | fprintf(file, "\n"); |
| 760 | } |
| 761 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 762 | int perform_code_completion(int argc, const char **argv) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 763 | const char *input = argv[1]; |
| 764 | char *filename = 0; |
| 765 | unsigned line; |
| 766 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 767 | CXIndex CIdx; |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 768 | int errorCode; |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 769 | struct CXUnsavedFile *unsaved_files = 0; |
| 770 | int num_unsaved_files = 0; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 771 | CXCodeCompleteResults *results = 0; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 772 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 773 | input += strlen("-code-completion-at="); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 774 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 775 | 0, 0))) |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 776 | return errorCode; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 777 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 778 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 779 | return -1; |
| 780 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 781 | CIdx = clang_createIndex(0, 1); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 782 | results = clang_codeComplete(CIdx, |
| 783 | argv[argc - 1], argc - num_unsaved_files - 3, |
| 784 | argv + num_unsaved_files + 2, |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 785 | num_unsaved_files, unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 786 | filename, line, column); |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 787 | |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 788 | if (results) { |
| 789 | unsigned i, n = results->NumResults; |
| 790 | for (i = 0; i != n; ++i) |
| 791 | print_completion_result(results->Results + i, stdout); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 792 | n = clang_codeCompleteGetNumDiagnostics(results); |
| 793 | for (i = 0; i != n; ++i) { |
| 794 | CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); |
| 795 | PrintDiagnostic(diag); |
| 796 | clang_disposeDiagnostic(diag); |
| 797 | } |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 798 | clang_disposeCodeCompleteResults(results); |
| 799 | } |
| 800 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 801 | clang_disposeIndex(CIdx); |
| 802 | free(filename); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 803 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 804 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 805 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 806 | return 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 809 | typedef struct { |
| 810 | char *filename; |
| 811 | unsigned line; |
| 812 | unsigned column; |
| 813 | } CursorSourceLocation; |
| 814 | |
| 815 | int inspect_cursor_at(int argc, const char **argv) { |
| 816 | CXIndex CIdx; |
| 817 | int errorCode; |
| 818 | struct CXUnsavedFile *unsaved_files = 0; |
| 819 | int num_unsaved_files = 0; |
| 820 | CXTranslationUnit TU; |
| 821 | CXCursor Cursor; |
| 822 | CursorSourceLocation *Locations = 0; |
| 823 | unsigned NumLocations = 0, Loc; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 824 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 825 | /* Count the number of locations. */ |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 826 | while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) |
| 827 | ++NumLocations; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 828 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 829 | /* Parse the locations. */ |
| 830 | assert(NumLocations > 0 && "Unable to count locations?"); |
| 831 | Locations = (CursorSourceLocation *)malloc( |
| 832 | NumLocations * sizeof(CursorSourceLocation)); |
| 833 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 834 | const char *input = argv[Loc + 1] + strlen("-cursor-at="); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 835 | if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| 836 | &Locations[Loc].line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 837 | &Locations[Loc].column, 0, 0))) |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 838 | return errorCode; |
| 839 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 840 | |
| 841 | if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 842 | &num_unsaved_files)) |
| 843 | return -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 845 | CIdx = clang_createIndex(0, 1); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 846 | TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], |
| 847 | argc - num_unsaved_files - 2 - NumLocations, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 848 | argv + num_unsaved_files + 1 + NumLocations, |
| 849 | num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 850 | unsaved_files); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 851 | if (!TU) { |
| 852 | fprintf(stderr, "unable to parse input\n"); |
| 853 | return -1; |
| 854 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 856 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 857 | CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| 858 | if (!file) |
| 859 | continue; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 860 | |
| 861 | Cursor = clang_getCursor(TU, |
| 862 | clang_getLocation(TU, file, Locations[Loc].line, |
| 863 | Locations[Loc].column)); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 864 | PrintCursor(Cursor); |
| 865 | printf("\n"); |
| 866 | free(Locations[Loc].filename); |
| 867 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 868 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 869 | PrintDiagnostics(TU); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 870 | clang_disposeTranslationUnit(TU); |
| 871 | clang_disposeIndex(CIdx); |
| 872 | free(Locations); |
| 873 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 874 | return 0; |
| 875 | } |
| 876 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 877 | int perform_token_annotation(int argc, const char **argv) { |
| 878 | const char *input = argv[1]; |
| 879 | char *filename = 0; |
| 880 | unsigned line, second_line; |
| 881 | unsigned column, second_column; |
| 882 | CXIndex CIdx; |
| 883 | CXTranslationUnit TU = 0; |
| 884 | int errorCode; |
| 885 | struct CXUnsavedFile *unsaved_files = 0; |
| 886 | int num_unsaved_files = 0; |
| 887 | CXToken *tokens; |
| 888 | unsigned num_tokens; |
| 889 | CXSourceRange range; |
| 890 | CXSourceLocation startLoc, endLoc; |
| 891 | CXFile file = 0; |
| 892 | CXCursor *cursors = 0; |
| 893 | unsigned i; |
| 894 | |
| 895 | input += strlen("-test-annotate-tokens="); |
| 896 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
| 897 | &second_line, &second_column))) |
| 898 | return errorCode; |
| 899 | |
| 900 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 901 | return -1; |
| 902 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 903 | CIdx = clang_createIndex(0, 1); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 904 | TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], |
| 905 | argc - num_unsaved_files - 3, |
| 906 | argv + num_unsaved_files + 2, |
| 907 | num_unsaved_files, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 908 | unsaved_files); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 909 | if (!TU) { |
| 910 | fprintf(stderr, "unable to parse input\n"); |
| 911 | clang_disposeIndex(CIdx); |
| 912 | free(filename); |
| 913 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 914 | return -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 915 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 916 | errorCode = 0; |
| 917 | |
| 918 | file = clang_getFile(TU, filename); |
| 919 | if (!file) { |
| 920 | fprintf(stderr, "file %s is not in this translation unit\n", filename); |
| 921 | errorCode = -1; |
| 922 | goto teardown; |
| 923 | } |
| 924 | |
| 925 | startLoc = clang_getLocation(TU, file, line, column); |
| 926 | if (clang_equalLocations(clang_getNullLocation(), startLoc)) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 927 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 928 | column); |
| 929 | errorCode = -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 930 | goto teardown; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | endLoc = clang_getLocation(TU, file, second_line, second_column); |
| 934 | if (clang_equalLocations(clang_getNullLocation(), endLoc)) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 935 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 936 | second_line, second_column); |
| 937 | errorCode = -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 938 | goto teardown; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | range = clang_getRange(startLoc, endLoc); |
| 942 | clang_tokenize(TU, range, &tokens, &num_tokens); |
| 943 | cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); |
| 944 | clang_annotateTokens(TU, tokens, num_tokens, cursors); |
| 945 | for (i = 0; i != num_tokens; ++i) { |
| 946 | const char *kind = "<unknown>"; |
| 947 | CXString spelling = clang_getTokenSpelling(TU, tokens[i]); |
| 948 | CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); |
| 949 | unsigned start_line, start_column, end_line, end_column; |
| 950 | |
| 951 | switch (clang_getTokenKind(tokens[i])) { |
| 952 | case CXToken_Punctuation: kind = "Punctuation"; break; |
| 953 | case CXToken_Keyword: kind = "Keyword"; break; |
| 954 | case CXToken_Identifier: kind = "Identifier"; break; |
| 955 | case CXToken_Literal: kind = "Literal"; break; |
| 956 | case CXToken_Comment: kind = "Comment"; break; |
| 957 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 958 | clang_getInstantiationLocation(clang_getRangeStart(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 959 | 0, &start_line, &start_column, 0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 960 | clang_getInstantiationLocation(clang_getRangeEnd(extent), |
Douglas Gregor | 46766dc | 2010-01-26 19:19:08 +0000 | [diff] [blame] | 961 | 0, &end_line, &end_column, 0); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 962 | printf("%s: \"%s\" ", kind, clang_getCString(spelling)); |
| 963 | PrintExtent(stdout, start_line, start_column, end_line, end_column); |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 964 | if (!clang_isInvalid(cursors[i].kind)) { |
| 965 | printf(" "); |
| 966 | PrintCursor(cursors[i]); |
| 967 | } |
| 968 | printf("\n"); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 969 | } |
| 970 | free(cursors); |
| 971 | |
| 972 | teardown: |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 973 | PrintDiagnostics(TU); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 974 | clang_disposeTranslationUnit(TU); |
| 975 | clang_disposeIndex(CIdx); |
| 976 | free(filename); |
| 977 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 978 | return errorCode; |
| 979 | } |
| 980 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 981 | /******************************************************************************/ |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame^] | 982 | /* USR printing. */ |
| 983 | /******************************************************************************/ |
| 984 | |
| 985 | static int insufficient_usr(const char *kind, const char *usage) { |
| 986 | fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage); |
| 987 | return 1; |
| 988 | } |
| 989 | |
| 990 | static unsigned isUSR(const char *s) { |
| 991 | return s[0] == 'c' && s[1] == ':'; |
| 992 | } |
| 993 | |
| 994 | static int not_usr(const char *s, const char *arg) { |
| 995 | fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg); |
| 996 | return 1; |
| 997 | } |
| 998 | |
| 999 | static void print_usr(CXString usr) { |
| 1000 | const char *s = clang_getCString(usr); |
| 1001 | printf("%s\n", s); |
| 1002 | clang_disposeString(usr); |
| 1003 | } |
| 1004 | |
| 1005 | static void display_usrs() { |
| 1006 | fprintf(stderr, "-print-usrs options:\n" |
| 1007 | " ObjCCategory <class name> <category name>\n" |
| 1008 | " ObjCClass <class name>\n" |
| 1009 | " ObjCIvar <ivar name> <class USR>\n" |
| 1010 | " ObjCMethod <selector> [0=class method|1=instance method] " |
| 1011 | "<class USR>\n" |
| 1012 | " ObjCProperty <property name> <class USR>\n" |
| 1013 | " ObjCProtocol <protocol name>\n"); |
| 1014 | } |
| 1015 | |
| 1016 | int print_usrs(const char **I, const char **E) { |
| 1017 | while (I != E) { |
| 1018 | const char *kind = *I; |
| 1019 | unsigned len = strlen(kind); |
| 1020 | switch (len) { |
| 1021 | case 8: |
| 1022 | if (memcmp(kind, "ObjCIvar", 8) == 0) { |
| 1023 | if (I + 2 >= E) |
| 1024 | return insufficient_usr(kind, "<ivar name> <class USR>"); |
| 1025 | if (!isUSR(I[2])) |
| 1026 | return not_usr("<class USR>", I[2]); |
| 1027 | else { |
| 1028 | CXString x; |
| 1029 | x.Spelling = I[2]; |
| 1030 | x.MustFreeString = 0; |
| 1031 | print_usr(clang_constructUSR_ObjCIvar(I[1], x)); |
| 1032 | } |
| 1033 | |
| 1034 | I += 3; |
| 1035 | continue; |
| 1036 | } |
| 1037 | break; |
| 1038 | case 9: |
| 1039 | if (memcmp(kind, "ObjCClass", 9) == 0) { |
| 1040 | if (I + 1 >= E) |
| 1041 | return insufficient_usr(kind, "<class name>"); |
| 1042 | print_usr(clang_constructUSR_ObjCClass(I[1])); |
| 1043 | I += 2; |
| 1044 | continue; |
| 1045 | } |
| 1046 | break; |
| 1047 | case 10: |
| 1048 | if (memcmp(kind, "ObjCMethod", 10) == 0) { |
| 1049 | if (I + 3 >= E) |
| 1050 | return insufficient_usr(kind, "<method selector> " |
| 1051 | "[0=class method|1=instance method] <class USR>"); |
| 1052 | if (!isUSR(I[3])) |
| 1053 | return not_usr("<class USR>", I[3]); |
| 1054 | else { |
| 1055 | CXString x; |
| 1056 | x.Spelling = I[3]; |
| 1057 | x.MustFreeString = 0; |
| 1058 | print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x)); |
| 1059 | } |
| 1060 | I += 4; |
| 1061 | continue; |
| 1062 | } |
| 1063 | break; |
| 1064 | case 12: |
| 1065 | if (memcmp(kind, "ObjCCategory", 12) == 0) { |
| 1066 | if (I + 2 >= E) |
| 1067 | return insufficient_usr(kind, "<class name> <category name>"); |
| 1068 | print_usr(clang_constructUSR_ObjCCategory(I[1], I[2])); |
| 1069 | I += 3; |
| 1070 | continue; |
| 1071 | } |
| 1072 | if (memcmp(kind, "ObjCProtocol", 12) == 0) { |
| 1073 | if (I + 1 >= E) |
| 1074 | return insufficient_usr(kind, "<protocol name>"); |
| 1075 | print_usr(clang_constructUSR_ObjCProtocol(I[1])); |
| 1076 | I += 2; |
| 1077 | continue; |
| 1078 | } |
| 1079 | if (memcmp(kind, "ObjCProperty", 12) == 0) { |
| 1080 | if (I + 2 >= E) |
| 1081 | return insufficient_usr(kind, "<property name> <class USR>"); |
| 1082 | if (!isUSR(I[2])) |
| 1083 | return not_usr("<class USR>", I[2]); |
| 1084 | else { |
| 1085 | CXString x; |
| 1086 | x.Spelling = I[2]; |
| 1087 | x.MustFreeString = 0; |
| 1088 | print_usr(clang_constructUSR_ObjCProperty(I[1], x)); |
| 1089 | } |
| 1090 | I += 3; |
| 1091 | continue; |
| 1092 | } |
| 1093 | break; |
| 1094 | default: |
| 1095 | break; |
| 1096 | } |
| 1097 | break; |
| 1098 | } |
| 1099 | |
| 1100 | if (I != E) { |
| 1101 | fprintf(stderr, "Invalid USR kind: %s\n", *I); |
| 1102 | display_usrs(); |
| 1103 | return 1; |
| 1104 | } |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | int print_usrs_file(const char *file_name) { |
| 1109 | char line[2048]; |
| 1110 | const char *args[128]; |
| 1111 | unsigned numChars = 0; |
| 1112 | |
| 1113 | FILE *fp = fopen(file_name, "r"); |
| 1114 | if (!fp) { |
| 1115 | fprintf(stderr, "error: cannot open '%s'\n", file_name); |
| 1116 | return 1; |
| 1117 | } |
| 1118 | |
| 1119 | /* This code is not really all that safe, but it works fine for testing. */ |
| 1120 | while (!feof(fp)) { |
| 1121 | char c = fgetc(fp); |
| 1122 | if (c == '\n') { |
| 1123 | unsigned i = 0; |
| 1124 | const char *s = 0; |
| 1125 | |
| 1126 | if (numChars == 0) |
| 1127 | continue; |
| 1128 | |
| 1129 | line[numChars] = '\0'; |
| 1130 | numChars = 0; |
| 1131 | |
| 1132 | if (line[0] == '/' && line[1] == '/') |
| 1133 | continue; |
| 1134 | |
| 1135 | s = strtok(line, " "); |
| 1136 | while (s) { |
| 1137 | args[i] = s; |
| 1138 | ++i; |
| 1139 | s = strtok(0, " "); |
| 1140 | } |
| 1141 | if (print_usrs(&args[0], &args[i])) |
| 1142 | return 1; |
| 1143 | } |
| 1144 | else |
| 1145 | line[numChars++] = c; |
| 1146 | } |
| 1147 | |
| 1148 | fclose(fp); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1153 | /* Command line processing. */ |
| 1154 | /******************************************************************************/ |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1155 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1156 | static CXCursorVisitor GetVisitor(const char *s) { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1157 | if (s[0] == '\0') |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1158 | return FilteredPrintingVisitor; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1159 | if (strcmp(s, "-usrs") == 0) |
| 1160 | return USRVisitor; |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1164 | static void print_usage(void) { |
| 1165 | fprintf(stderr, |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1166 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1167 | " c-index-test -cursor-at=<site> <compiler arguments>\n" |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1168 | " c-index-test -test-file-scan <AST file> <source file> " |
| 1169 | "[FileCheck prefix]\n" |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1170 | " c-index-test -test-load-tu <AST file> <symbol filter> " |
| 1171 | "[FileCheck prefix]\n" |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1172 | " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " |
| 1173 | "[FileCheck prefix]\n" |
| 1174 | " c-index-test -test-load-source <symbol filter> {<args>}*\n" |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1175 | " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1176 | fprintf(stderr, |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1177 | " c-index-test -test-annotate-tokens=<range> {<args>}*\n" |
| 1178 | " c-index-test -test-inclusion-stack-source {<args>}*\n" |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1179 | " c-index-test -test-inclusion-stack-tu <AST file>\n" |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame^] | 1180 | " c-index-test -test-print-linkage-source {<args>}*\n" |
| 1181 | " c-index-test -print-usr [<CursorKind> {<args>}]*\n" |
| 1182 | " c-index-test -print-usr-file <file>\n\n" |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1183 | " <symbol filter> values:\n%s", |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1184 | " all - load all symbols, including those from PCH\n" |
| 1185 | " local - load all symbols except those in PCH\n" |
| 1186 | " category - only load ObjC categories (non-PCH)\n" |
| 1187 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 1188 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 1189 | " function - only load functions (non-PCH)\n" |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1190 | " typedef - only load typdefs (non-PCH)\n" |
| 1191 | " scan-function - scan function bodies (non-PCH)\n\n"); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | int main(int argc, const char **argv) { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1195 | clang_enableStackTraces(); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1196 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
| 1197 | return perform_code_completion(argc, argv); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1198 | if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) |
| 1199 | return inspect_cursor_at(argc, argv); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1200 | else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1201 | CXCursorVisitor I = GetVisitor(argv[1] + 13); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1202 | if (I) |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1203 | return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, |
| 1204 | NULL); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1205 | } |
| 1206 | else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1207 | CXCursorVisitor I = GetVisitor(argv[1] + 17); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1208 | if (I) |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1209 | 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] | 1210 | } |
| 1211 | else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1212 | return perform_file_scan(argv[2], argv[3], |
| 1213 | argc >= 5 ? argv[4] : 0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1214 | else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) |
| 1215 | return perform_token_annotation(argc, argv); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1216 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) |
| 1217 | return perform_test_load_source(argc - 2, argv + 2, "all", NULL, |
| 1218 | PrintInclusionStack); |
| 1219 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) |
| 1220 | return perform_test_load_tu(argv[2], "all", NULL, NULL, |
| 1221 | PrintInclusionStack); |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1222 | else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0) |
| 1223 | return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage, |
| 1224 | NULL); |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame^] | 1225 | else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { |
| 1226 | if (argc > 2) |
| 1227 | return print_usrs(argv + 2, argv + argc); |
| 1228 | else { |
| 1229 | display_usrs(); |
| 1230 | return 1; |
| 1231 | } |
| 1232 | } |
| 1233 | else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0) |
| 1234 | return print_usrs_file(argv[2]); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1235 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1236 | print_usage(); |
| 1237 | return 1; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 1238 | } |