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