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