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