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