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