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