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 | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 40 | if (getenv("CINDEXTEST_NESTED_MACROS")) |
Chandler Carruth | ba7537f | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 41 | options |= CXTranslationUnit_NestedMacroExpansions; |
Argyrios Kyrtzidis | dcaca01 | 2011-11-03 02:20:25 +0000 | [diff] [blame] | 42 | if (getenv("CINDEXTEST_COMPLETION_NO_CACHING")) |
| 43 | options &= ~CXTranslationUnit_CacheCompletionResults; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 44 | |
| 45 | return options; |
| 46 | } |
| 47 | |
Argyrios Kyrtzidis | bda536d | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 48 | static int checkForErrors(CXTranslationUnit TU); |
| 49 | |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 50 | static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column, |
| 51 | unsigned end_line, unsigned end_column) { |
| 52 | fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column, |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 53 | end_line, end_column); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 56 | static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, |
| 57 | CXTranslationUnit *TU) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 59 | *TU = clang_createTranslationUnit(Idx, file); |
Dan Gohman | 6be2a22 | 2010-07-26 21:44:15 +0000 | [diff] [blame] | 60 | if (!*TU) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 61 | fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); |
| 62 | return 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 63 | } |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 64 | return 1; |
| 65 | } |
| 66 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 67 | void free_remapped_files(struct CXUnsavedFile *unsaved_files, |
| 68 | int num_unsaved_files) { |
| 69 | int i; |
| 70 | for (i = 0; i != num_unsaved_files; ++i) { |
| 71 | free((char *)unsaved_files[i].Filename); |
| 72 | free((char *)unsaved_files[i].Contents); |
| 73 | } |
Douglas Gregor | 653a55f | 2010-08-19 20:50:29 +0000 | [diff] [blame] | 74 | free(unsaved_files); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int parse_remapped_files(int argc, const char **argv, int start_arg, |
| 78 | struct CXUnsavedFile **unsaved_files, |
| 79 | int *num_unsaved_files) { |
| 80 | int i; |
| 81 | int arg; |
| 82 | int prefix_len = strlen("-remap-file="); |
| 83 | *unsaved_files = 0; |
| 84 | *num_unsaved_files = 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 85 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 86 | /* Count the number of remapped files. */ |
| 87 | for (arg = start_arg; arg < argc; ++arg) { |
| 88 | if (strncmp(argv[arg], "-remap-file=", prefix_len)) |
| 89 | break; |
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 | ++*num_unsaved_files; |
| 92 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 94 | if (*num_unsaved_files == 0) |
| 95 | return 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 97 | *unsaved_files |
Douglas Gregor | 653a55f | 2010-08-19 20:50:29 +0000 | [diff] [blame] | 98 | = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * |
| 99 | *num_unsaved_files); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 100 | for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) { |
| 101 | struct CXUnsavedFile *unsaved = *unsaved_files + i; |
| 102 | const char *arg_string = argv[arg] + prefix_len; |
| 103 | int filename_len; |
| 104 | char *filename; |
| 105 | char *contents; |
| 106 | FILE *to_file; |
| 107 | const char *semi = strchr(arg_string, ';'); |
| 108 | if (!semi) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 109 | fprintf(stderr, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 110 | "error: -remap-file=from;to argument is missing semicolon\n"); |
| 111 | free_remapped_files(*unsaved_files, i); |
| 112 | *unsaved_files = 0; |
| 113 | *num_unsaved_files = 0; |
| 114 | return -1; |
| 115 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 116 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 117 | /* Open the file that we're remapping to. */ |
Francois Pichet | c44fe4b | 2010-10-12 01:01:43 +0000 | [diff] [blame] | 118 | to_file = fopen(semi + 1, "rb"); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 119 | if (!to_file) { |
| 120 | fprintf(stderr, "error: cannot open file %s that we are remapping to\n", |
| 121 | semi + 1); |
| 122 | free_remapped_files(*unsaved_files, i); |
| 123 | *unsaved_files = 0; |
| 124 | *num_unsaved_files = 0; |
| 125 | return -1; |
| 126 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 127 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 128 | /* Determine the length of the file we're remapping to. */ |
| 129 | fseek(to_file, 0, SEEK_END); |
| 130 | unsaved->Length = ftell(to_file); |
| 131 | fseek(to_file, 0, SEEK_SET); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 133 | /* Read the contents of the file we're remapping to. */ |
| 134 | contents = (char *)malloc(unsaved->Length + 1); |
| 135 | if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) { |
| 136 | fprintf(stderr, "error: unexpected %s reading 'to' file %s\n", |
| 137 | (feof(to_file) ? "EOF" : "error"), semi + 1); |
| 138 | fclose(to_file); |
| 139 | free_remapped_files(*unsaved_files, i); |
| 140 | *unsaved_files = 0; |
| 141 | *num_unsaved_files = 0; |
| 142 | return -1; |
| 143 | } |
| 144 | contents[unsaved->Length] = 0; |
| 145 | unsaved->Contents = contents; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 147 | /* Close the file. */ |
| 148 | fclose(to_file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 150 | /* Copy the file name that we're remapping from. */ |
| 151 | filename_len = semi - arg_string; |
| 152 | filename = (char *)malloc(filename_len + 1); |
| 153 | memcpy(filename, arg_string, filename_len); |
| 154 | filename[filename_len] = 0; |
| 155 | unsaved->Filename = filename; |
| 156 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 157 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 161 | /******************************************************************************/ |
| 162 | /* Pretty-printing. */ |
| 163 | /******************************************************************************/ |
| 164 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 165 | static void PrintRange(CXSourceRange R, const char *str) { |
| 166 | CXFile begin_file, end_file; |
| 167 | unsigned begin_line, begin_column, end_line, end_column; |
| 168 | |
| 169 | clang_getSpellingLocation(clang_getRangeStart(R), |
| 170 | &begin_file, &begin_line, &begin_column, 0); |
| 171 | clang_getSpellingLocation(clang_getRangeEnd(R), |
| 172 | &end_file, &end_line, &end_column, 0); |
| 173 | if (!begin_file || !end_file) |
| 174 | return; |
| 175 | |
| 176 | printf(" %s=", str); |
| 177 | PrintExtent(stdout, begin_line, begin_column, end_line, end_column); |
| 178 | } |
| 179 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 180 | int want_display_name = 0; |
| 181 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 182 | static void PrintCursor(CXCursor Cursor) { |
| 183 | CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 184 | if (clang_isInvalid(Cursor.kind)) { |
| 185 | CXString ks = clang_getCursorKindSpelling(Cursor.kind); |
| 186 | printf("Invalid Cursor => %s", clang_getCString(ks)); |
| 187 | clang_disposeString(ks); |
| 188 | } |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 189 | else { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 190 | CXString string, ks; |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 191 | CXCursor Referenced; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 192 | unsigned line, column; |
Douglas Gregor | e0329ac | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 193 | CXCursor SpecializationOf; |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 194 | CXCursor *overridden; |
| 195 | unsigned num_overridden; |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 196 | unsigned RefNameRangeNr; |
| 197 | CXSourceRange CursorExtent; |
| 198 | CXSourceRange RefNameRange; |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 200 | ks = clang_getCursorKindSpelling(Cursor.kind); |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 201 | string = want_display_name? clang_getCursorDisplayName(Cursor) |
| 202 | : clang_getCursorSpelling(Cursor); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 203 | printf("%s=%s", clang_getCString(ks), |
| 204 | clang_getCString(string)); |
| 205 | clang_disposeString(ks); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 206 | clang_disposeString(string); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 207 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 208 | Referenced = clang_getCursorReferenced(Cursor); |
| 209 | if (!clang_equalCursors(Referenced, clang_getNullCursor())) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 210 | if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) { |
| 211 | unsigned I, N = clang_getNumOverloadedDecls(Referenced); |
| 212 | printf("["); |
| 213 | for (I = 0; I != N; ++I) { |
| 214 | CXCursor Ovl = clang_getOverloadedDecl(Referenced, I); |
Douglas Gregor | 1f6206e | 2010-09-14 00:20:32 +0000 | [diff] [blame] | 215 | CXSourceLocation Loc; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 216 | if (I) |
| 217 | printf(", "); |
| 218 | |
Douglas Gregor | 1f6206e | 2010-09-14 00:20:32 +0000 | [diff] [blame] | 219 | Loc = clang_getCursorLocation(Ovl); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 220 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 221 | printf("%d:%d", line, column); |
| 222 | } |
| 223 | printf("]"); |
| 224 | } else { |
| 225 | CXSourceLocation Loc = clang_getCursorLocation(Referenced); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 226 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 227 | printf(":%d:%d", line, column); |
| 228 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 229 | } |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 230 | |
| 231 | if (clang_isCursorDefinition(Cursor)) |
| 232 | printf(" (Definition)"); |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 233 | |
| 234 | switch (clang_getCursorAvailability(Cursor)) { |
| 235 | case CXAvailability_Available: |
| 236 | break; |
| 237 | |
| 238 | case CXAvailability_Deprecated: |
| 239 | printf(" (deprecated)"); |
| 240 | break; |
| 241 | |
| 242 | case CXAvailability_NotAvailable: |
| 243 | printf(" (unavailable)"); |
| 244 | break; |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 245 | |
| 246 | case CXAvailability_NotAccessible: |
| 247 | printf(" (inaccessible)"); |
| 248 | break; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 249 | } |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | b83d4d7 | 2011-05-13 15:54:42 +0000 | [diff] [blame] | 251 | if (clang_CXXMethod_isStatic(Cursor)) |
| 252 | printf(" (static)"); |
| 253 | if (clang_CXXMethod_isVirtual(Cursor)) |
| 254 | printf(" (virtual)"); |
| 255 | |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 256 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
| 257 | CXType T = |
| 258 | clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor)); |
| 259 | CXString S = clang_getTypeKindSpelling(T.kind); |
| 260 | printf(" [IBOutletCollection=%s]", clang_getCString(S)); |
| 261 | clang_disposeString(S); |
| 262 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 263 | |
| 264 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 265 | enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor); |
| 266 | unsigned isVirtual = clang_isVirtualBase(Cursor); |
| 267 | const char *accessStr = 0; |
| 268 | |
| 269 | switch (access) { |
| 270 | case CX_CXXInvalidAccessSpecifier: |
| 271 | accessStr = "invalid"; break; |
| 272 | case CX_CXXPublic: |
| 273 | accessStr = "public"; break; |
| 274 | case CX_CXXProtected: |
| 275 | accessStr = "protected"; break; |
| 276 | case CX_CXXPrivate: |
| 277 | accessStr = "private"; break; |
| 278 | } |
| 279 | |
| 280 | printf(" [access=%s isVirtual=%s]", accessStr, |
| 281 | isVirtual ? "true" : "false"); |
| 282 | } |
Douglas Gregor | e0329ac | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 283 | |
| 284 | SpecializationOf = clang_getSpecializedCursorTemplate(Cursor); |
| 285 | if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) { |
| 286 | CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf); |
| 287 | CXString Name = clang_getCursorSpelling(SpecializationOf); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 288 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | e0329ac | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 289 | printf(" [Specialization of %s:%d:%d]", |
| 290 | clang_getCString(Name), line, column); |
| 291 | clang_disposeString(Name); |
| 292 | } |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 293 | |
| 294 | clang_getOverriddenCursors(Cursor, &overridden, &num_overridden); |
| 295 | if (num_overridden) { |
| 296 | unsigned I; |
| 297 | printf(" [Overrides "); |
| 298 | for (I = 0; I != num_overridden; ++I) { |
| 299 | CXSourceLocation Loc = clang_getCursorLocation(overridden[I]); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 300 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 301 | if (I) |
| 302 | printf(", "); |
| 303 | printf("@%d:%d", line, column); |
| 304 | } |
| 305 | printf("]"); |
| 306 | clang_disposeOverriddenCursors(overridden); |
| 307 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 308 | |
| 309 | if (Cursor.kind == CXCursor_InclusionDirective) { |
| 310 | CXFile File = clang_getIncludedFile(Cursor); |
| 311 | CXString Included = clang_getFileName(File); |
| 312 | printf(" (%s)", clang_getCString(Included)); |
| 313 | clang_disposeString(Included); |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 314 | |
| 315 | if (clang_isFileMultipleIncludeGuarded(TU, File)) |
| 316 | printf(" [multi-include guarded]"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 317 | } |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 318 | |
| 319 | CursorExtent = clang_getCursorExtent(Cursor); |
| 320 | RefNameRange = clang_getCursorReferenceNameRange(Cursor, |
| 321 | CXNameRange_WantQualifier |
| 322 | | CXNameRange_WantSinglePiece |
| 323 | | CXNameRange_WantTemplateArgs, |
| 324 | 0); |
| 325 | if (!clang_equalRanges(CursorExtent, RefNameRange)) |
| 326 | PrintRange(RefNameRange, "SingleRefName"); |
| 327 | |
| 328 | for (RefNameRangeNr = 0; 1; RefNameRangeNr++) { |
| 329 | RefNameRange = clang_getCursorReferenceNameRange(Cursor, |
| 330 | CXNameRange_WantQualifier |
| 331 | | CXNameRange_WantTemplateArgs, |
| 332 | RefNameRangeNr); |
| 333 | if (clang_equalRanges(clang_getNullRange(), RefNameRange)) |
| 334 | break; |
| 335 | if (!clang_equalRanges(CursorExtent, RefNameRange)) |
| 336 | PrintRange(RefNameRange, "RefName"); |
| 337 | } |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 338 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 339 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 341 | static const char* GetCursorSource(CXCursor Cursor) { |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 342 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 343 | CXString source; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 344 | CXFile file; |
Argyrios Kyrtzidis | b4efaa0 | 2011-11-03 02:20:36 +0000 | [diff] [blame] | 345 | clang_getExpansionLocation(Loc, &file, 0, 0, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 346 | source = clang_getFileName(file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 347 | if (!clang_getCString(source)) { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 348 | clang_disposeString(source); |
| 349 | return "<invalid loc>"; |
| 350 | } |
| 351 | else { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 352 | const char *b = basename(clang_getCString(source)); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 353 | clang_disposeString(source); |
| 354 | return b; |
| 355 | } |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 358 | /******************************************************************************/ |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 359 | /* Callbacks. */ |
| 360 | /******************************************************************************/ |
| 361 | |
| 362 | typedef void (*PostVisitTU)(CXTranslationUnit); |
| 363 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 364 | void PrintDiagnostic(CXDiagnostic Diagnostic) { |
| 365 | FILE *out = stderr; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 366 | CXFile file; |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 367 | CXString Msg; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 368 | unsigned display_opts = CXDiagnostic_DisplaySourceLocation |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 369 | | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges |
| 370 | | CXDiagnostic_DisplayOption; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 371 | unsigned i, num_fixits; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 373 | if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 374 | return; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 375 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 376 | Msg = clang_formatDiagnostic(Diagnostic, display_opts); |
| 377 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 378 | clang_disposeString(Msg); |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 380 | clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| 381 | &file, 0, 0, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 382 | if (!file) |
| 383 | return; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 384 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 385 | num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); |
| 386 | for (i = 0; i != num_fixits; ++i) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 387 | CXSourceRange range; |
| 388 | CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range); |
| 389 | CXSourceLocation start = clang_getRangeStart(range); |
| 390 | CXSourceLocation end = clang_getRangeEnd(range); |
| 391 | unsigned start_line, start_column, end_line, end_column; |
| 392 | CXFile start_file, end_file; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 393 | clang_getSpellingLocation(start, &start_file, &start_line, |
| 394 | &start_column, 0); |
| 395 | clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0); |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 396 | if (clang_equalLocations(start, end)) { |
| 397 | /* Insertion. */ |
| 398 | if (start_file == file) |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 399 | fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 400 | clang_getCString(insertion_text), start_line, start_column); |
| 401 | } else if (strcmp(clang_getCString(insertion_text), "") == 0) { |
| 402 | /* Removal. */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 403 | if (start_file == file && end_file == file) { |
| 404 | fprintf(out, "FIX-IT: Remove "); |
| 405 | PrintExtent(out, start_line, start_column, end_line, end_column); |
| 406 | fprintf(out, "\n"); |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 407 | } |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 408 | } else { |
| 409 | /* Replacement. */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 410 | if (start_file == end_file) { |
| 411 | fprintf(out, "FIX-IT: Replace "); |
| 412 | PrintExtent(out, start_line, start_column, end_line, end_column); |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 413 | fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); |
Douglas Gregor | 436f3f0 | 2010-02-18 22:27:07 +0000 | [diff] [blame] | 414 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 415 | break; |
| 416 | } |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 417 | clang_disposeString(insertion_text); |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 418 | } |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 421 | void PrintDiagnostics(CXTranslationUnit TU) { |
| 422 | int i, n = clang_getNumDiagnostics(TU); |
| 423 | for (i = 0; i != n; ++i) { |
| 424 | CXDiagnostic Diag = clang_getDiagnostic(TU, i); |
| 425 | PrintDiagnostic(Diag); |
| 426 | clang_disposeDiagnostic(Diag); |
| 427 | } |
| 428 | } |
| 429 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 430 | void PrintMemoryUsage(CXTranslationUnit TU) { |
Matt Beaumont-Gay | b227323 | 2011-08-29 16:37:29 +0000 | [diff] [blame] | 431 | unsigned long total = 0; |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 432 | unsigned i = 0; |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 433 | CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU); |
Francois Pichet | 3c68336 | 2011-04-18 23:33:22 +0000 | [diff] [blame] | 434 | fprintf(stderr, "Memory usage:\n"); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 435 | for (i = 0 ; i != usage.numEntries; ++i) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 436 | const char *name = clang_getTUResourceUsageName(usage.entries[i].kind); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 437 | unsigned long amount = usage.entries[i].amount; |
| 438 | total += amount; |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 439 | fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 440 | ((double) amount)/(1024*1024)); |
| 441 | } |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 442 | fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 443 | ((double) total)/(1024*1024)); |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 444 | clang_disposeCXTUResourceUsage(usage); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 447 | /******************************************************************************/ |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 448 | /* Logic for testing traversal. */ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 449 | /******************************************************************************/ |
| 450 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 451 | static const char *FileCheckPrefix = "CHECK"; |
| 452 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 453 | static void PrintCursorExtent(CXCursor C) { |
| 454 | CXSourceRange extent = clang_getCursorExtent(C); |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 455 | PrintRange(extent, "Extent"); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 458 | /* Data used by all of the visitors. */ |
| 459 | typedef struct { |
| 460 | CXTranslationUnit TU; |
| 461 | enum CXCursorKind *Filter; |
| 462 | } VisitorData; |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 463 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 464 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 465 | enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 466 | CXCursor Parent, |
| 467 | CXClientData ClientData) { |
| 468 | VisitorData *Data = (VisitorData *)ClientData; |
| 469 | if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 470 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 471 | unsigned line, column; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 472 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 473 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 474 | GetCursorSource(Cursor), line, column); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 475 | PrintCursor(Cursor); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 476 | PrintCursorExtent(Cursor); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 477 | printf("\n"); |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 478 | return CXChildVisit_Recurse; |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 479 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 480 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 481 | return CXChildVisit_Continue; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 482 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 484 | static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 485 | CXCursor Parent, |
| 486 | CXClientData ClientData) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 487 | const char *startBuf, *endBuf; |
| 488 | unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; |
| 489 | CXCursor Ref; |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 490 | VisitorData *Data = (VisitorData *)ClientData; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 492 | if (Cursor.kind != CXCursor_FunctionDecl || |
| 493 | !clang_isCursorDefinition(Cursor)) |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 494 | return CXChildVisit_Continue; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 495 | |
| 496 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 497 | &startLine, &startColumn, |
| 498 | &endLine, &endColumn); |
| 499 | /* Probe the entire body, looking for both decls and refs. */ |
| 500 | curLine = startLine; |
| 501 | curColumn = startColumn; |
| 502 | |
| 503 | while (startBuf < endBuf) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 504 | CXSourceLocation Loc; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 505 | CXFile file; |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 506 | CXString source; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 507 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 508 | if (*startBuf == '\n') { |
| 509 | startBuf++; |
| 510 | curLine++; |
| 511 | curColumn = 1; |
| 512 | } else if (*startBuf != '\t') |
| 513 | curColumn++; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 514 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 515 | Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 516 | clang_getSpellingLocation(Loc, &file, 0, 0, 0); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 517 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 518 | source = clang_getFileName(file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 519 | if (clang_getCString(source)) { |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 520 | CXSourceLocation RefLoc |
| 521 | = clang_getLocation(Data->TU, file, curLine, curColumn); |
| 522 | Ref = clang_getCursor(Data->TU, RefLoc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 523 | if (Ref.kind == CXCursor_NoDeclFound) { |
| 524 | /* Nothing found here; that's fine. */ |
| 525 | } else if (Ref.kind != CXCursor_FunctionDecl) { |
| 526 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), |
| 527 | curLine, curColumn); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 528 | PrintCursor(Ref); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 529 | printf("\n"); |
| 530 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 531 | } |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 532 | clang_disposeString(source); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 533 | startBuf++; |
| 534 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 535 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 536 | return CXChildVisit_Continue; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 539 | /******************************************************************************/ |
| 540 | /* USR testing. */ |
| 541 | /******************************************************************************/ |
| 542 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 543 | enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, |
| 544 | CXClientData ClientData) { |
| 545 | VisitorData *Data = (VisitorData *)ClientData; |
| 546 | if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { |
Ted Kremenek | cf84aa4 | 2010-01-18 20:23:29 +0000 | [diff] [blame] | 547 | CXString USR = clang_getCursorUSR(C); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 548 | const char *cstr = clang_getCString(USR); |
| 549 | if (!cstr || cstr[0] == '\0') { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 550 | clang_disposeString(USR); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 551 | return CXChildVisit_Recurse; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 552 | } |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 553 | printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr); |
| 554 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 555 | PrintCursorExtent(C); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 556 | printf("\n"); |
| 557 | clang_disposeString(USR); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 558 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 559 | return CXChildVisit_Recurse; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 562 | return CXChildVisit_Continue; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /******************************************************************************/ |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 566 | /* Inclusion stack testing. */ |
| 567 | /******************************************************************************/ |
| 568 | |
| 569 | void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, |
| 570 | unsigned includeStackLen, CXClientData data) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 571 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 572 | unsigned i; |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 573 | CXString fname; |
| 574 | |
| 575 | fname = clang_getFileName(includedFile); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 576 | printf("file: %s\nincluded by:\n", clang_getCString(fname)); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 577 | clang_disposeString(fname); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 578 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 579 | for (i = 0; i < includeStackLen; ++i) { |
| 580 | CXFile includingFile; |
| 581 | unsigned line, column; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 582 | clang_getSpellingLocation(includeStack[i], &includingFile, &line, |
| 583 | &column, 0); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 584 | fname = clang_getFileName(includingFile); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 585 | printf(" %s:%d:%d\n", clang_getCString(fname), line, column); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 586 | clang_disposeString(fname); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 587 | } |
| 588 | printf("\n"); |
| 589 | } |
| 590 | |
| 591 | void PrintInclusionStack(CXTranslationUnit TU) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 592 | clang_getInclusions(TU, InclusionVisitor, NULL); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /******************************************************************************/ |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 596 | /* Linkage testing. */ |
| 597 | /******************************************************************************/ |
| 598 | |
| 599 | static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, |
| 600 | CXClientData d) { |
| 601 | const char *linkage = 0; |
| 602 | |
| 603 | if (clang_isInvalid(clang_getCursorKind(cursor))) |
| 604 | return CXChildVisit_Recurse; |
| 605 | |
| 606 | switch (clang_getCursorLinkage(cursor)) { |
| 607 | case CXLinkage_Invalid: break; |
Douglas Gregor | c2a2b3c | 2010-03-04 19:36:27 +0000 | [diff] [blame] | 608 | case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; |
| 609 | case CXLinkage_Internal: linkage = "Internal"; break; |
| 610 | case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; |
| 611 | case CXLinkage_External: linkage = "External"; break; |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | if (linkage) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 615 | PrintCursor(cursor); |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 616 | printf("linkage=%s\n", linkage); |
| 617 | } |
| 618 | |
| 619 | return CXChildVisit_Recurse; |
| 620 | } |
| 621 | |
| 622 | /******************************************************************************/ |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 623 | /* Typekind testing. */ |
| 624 | /******************************************************************************/ |
| 625 | |
| 626 | static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p, |
| 627 | CXClientData d) { |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 628 | if (!clang_isInvalid(clang_getCursorKind(cursor))) { |
| 629 | CXType T = clang_getCursorType(cursor); |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 630 | CXString S = clang_getTypeKindSpelling(T.kind); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 631 | PrintCursor(cursor); |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 632 | printf(" typekind=%s", clang_getCString(S)); |
Douglas Gregor | e72fb6f | 2011-01-27 16:27:11 +0000 | [diff] [blame] | 633 | if (clang_isConstQualifiedType(T)) |
| 634 | printf(" const"); |
| 635 | if (clang_isVolatileQualifiedType(T)) |
| 636 | printf(" volatile"); |
| 637 | if (clang_isRestrictQualifiedType(T)) |
| 638 | printf(" restrict"); |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 639 | clang_disposeString(S); |
Benjamin Kramer | e1403d2 | 2010-06-22 09:29:44 +0000 | [diff] [blame] | 640 | /* Print the canonical type if it is different. */ |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 641 | { |
| 642 | CXType CT = clang_getCanonicalType(T); |
| 643 | if (!clang_equalTypes(T, CT)) { |
| 644 | CXString CS = clang_getTypeKindSpelling(CT.kind); |
| 645 | printf(" [canonical=%s]", clang_getCString(CS)); |
| 646 | clang_disposeString(CS); |
| 647 | } |
| 648 | } |
Benjamin Kramer | e1403d2 | 2010-06-22 09:29:44 +0000 | [diff] [blame] | 649 | /* Print the return type if it exists. */ |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 650 | { |
Ted Kremenek | 9a14084 | 2010-06-21 20:48:56 +0000 | [diff] [blame] | 651 | CXType RT = clang_getCursorResultType(cursor); |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 652 | if (RT.kind != CXType_Invalid) { |
| 653 | CXString RS = clang_getTypeKindSpelling(RT.kind); |
| 654 | printf(" [result=%s]", clang_getCString(RS)); |
| 655 | clang_disposeString(RS); |
| 656 | } |
| 657 | } |
Ted Kremenek | 3ce9e7d | 2010-07-30 00:14:11 +0000 | [diff] [blame] | 658 | /* Print if this is a non-POD type. */ |
| 659 | printf(" [isPOD=%d]", clang_isPODType(T)); |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 660 | |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 661 | printf("\n"); |
| 662 | } |
| 663 | return CXChildVisit_Recurse; |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /******************************************************************************/ |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 668 | /* Loading ASTs/source. */ |
| 669 | /******************************************************************************/ |
| 670 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 671 | static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, |
Ted Kremenek | 9827156 | 2010-01-12 18:53:15 +0000 | [diff] [blame] | 672 | const char *filter, const char *prefix, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 673 | CXCursorVisitor Visitor, |
| 674 | PostVisitTU PV) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 676 | if (prefix) |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 677 | FileCheckPrefix = prefix; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 678 | |
| 679 | if (Visitor) { |
| 680 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 681 | enum CXCursorKind *ck = &K; |
| 682 | VisitorData Data; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 683 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 684 | /* Perform some simple filtering. */ |
| 685 | if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 686 | else if (!strcmp(filter, "all-display") || |
| 687 | !strcmp(filter, "local-display")) { |
| 688 | ck = NULL; |
| 689 | want_display_name = 1; |
| 690 | } |
Daniel Dunbar | b1ffee6 | 2010-02-10 20:42:40 +0000 | [diff] [blame] | 691 | else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 692 | else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| 693 | else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 694 | else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 695 | else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| 696 | else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
| 697 | else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; |
| 698 | else { |
| 699 | fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| 700 | return 1; |
| 701 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 702 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 703 | Data.TU = TU; |
| 704 | Data.Filter = ck; |
| 705 | clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 706 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 707 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 708 | if (PV) |
| 709 | PV(TU); |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 710 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 711 | PrintDiagnostics(TU); |
Argyrios Kyrtzidis | 16ac8be | 2011-11-13 23:39:14 +0000 | [diff] [blame] | 712 | if (checkForErrors(TU) != 0) { |
| 713 | clang_disposeTranslationUnit(TU); |
| 714 | return -1; |
| 715 | } |
| 716 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 717 | clang_disposeTranslationUnit(TU); |
| 718 | return 0; |
| 719 | } |
| 720 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 721 | int perform_test_load_tu(const char *file, const char *filter, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 722 | const char *prefix, CXCursorVisitor Visitor, |
| 723 | PostVisitTU PV) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 724 | CXIndex Idx; |
| 725 | CXTranslationUnit TU; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 726 | int result; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 727 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 728 | !strcmp(filter, "local") ? 1 : 0, |
| 729 | /* displayDiagnosics=*/1); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 730 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 731 | if (!CreateTranslationUnit(Idx, file, &TU)) { |
| 732 | clang_disposeIndex(Idx); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 733 | return 1; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 734 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 735 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 736 | result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV); |
| 737 | clang_disposeIndex(Idx); |
| 738 | return result; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 741 | int perform_test_load_source(int argc, const char **argv, |
| 742 | const char *filter, CXCursorVisitor Visitor, |
| 743 | PostVisitTU PV) { |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 744 | CXIndex Idx; |
| 745 | CXTranslationUnit TU; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 746 | struct CXUnsavedFile *unsaved_files = 0; |
| 747 | int num_unsaved_files = 0; |
| 748 | int result; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 749 | |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 750 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 751 | (!strcmp(filter, "local") || |
| 752 | !strcmp(filter, "local-display"))? 1 : 0, |
Douglas Gregor | 4814fb5 | 2011-02-03 23:41:12 +0000 | [diff] [blame] | 753 | /* displayDiagnosics=*/0); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 754 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 755 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 756 | clang_disposeIndex(Idx); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 757 | return -1; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 758 | } |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 759 | |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 760 | TU = clang_parseTranslationUnit(Idx, 0, |
| 761 | argv + num_unsaved_files, |
| 762 | argc - num_unsaved_files, |
| 763 | unsaved_files, num_unsaved_files, |
| 764 | getDefaultParsingOptions()); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 765 | if (!TU) { |
| 766 | fprintf(stderr, "Unable to load translation unit!\n"); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 767 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 768 | clang_disposeIndex(Idx); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 769 | return 1; |
| 770 | } |
| 771 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 772 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 773 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 774 | clang_disposeIndex(Idx); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 775 | return result; |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 778 | int perform_test_reparse_source(int argc, const char **argv, int trials, |
| 779 | const char *filter, CXCursorVisitor Visitor, |
| 780 | PostVisitTU PV) { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 781 | CXIndex Idx; |
| 782 | CXTranslationUnit TU; |
| 783 | struct CXUnsavedFile *unsaved_files = 0; |
| 784 | int num_unsaved_files = 0; |
| 785 | int result; |
| 786 | int trial; |
Argyrios Kyrtzidis | 40098e8 | 2011-09-12 18:09:31 +0000 | [diff] [blame] | 787 | int remap_after_trial = 0; |
| 788 | char *endptr = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 789 | |
| 790 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| 791 | !strcmp(filter, "local") ? 1 : 0, |
Douglas Gregor | 1aa2730 | 2011-01-27 18:02:58 +0000 | [diff] [blame] | 792 | /* displayDiagnosics=*/0); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 794 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 795 | clang_disposeIndex(Idx); |
| 796 | return -1; |
| 797 | } |
| 798 | |
Daniel Dunbar | c8a6180 | 2010-08-18 23:09:16 +0000 | [diff] [blame] | 799 | /* Load the initial translation unit -- we do this without honoring remapped |
| 800 | * 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] | 801 | TU = clang_parseTranslationUnit(Idx, 0, |
| 802 | argv + num_unsaved_files, |
| 803 | argc - num_unsaved_files, |
Daniel Dunbar | c8a6180 | 2010-08-18 23:09:16 +0000 | [diff] [blame] | 804 | 0, 0, getDefaultParsingOptions()); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 805 | if (!TU) { |
| 806 | fprintf(stderr, "Unable to load translation unit!\n"); |
| 807 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 808 | clang_disposeIndex(Idx); |
| 809 | return 1; |
| 810 | } |
| 811 | |
Argyrios Kyrtzidis | bda536d | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 812 | if (checkForErrors(TU) != 0) |
| 813 | return -1; |
| 814 | |
Argyrios Kyrtzidis | 40098e8 | 2011-09-12 18:09:31 +0000 | [diff] [blame] | 815 | if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) { |
| 816 | remap_after_trial = |
| 817 | strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10); |
| 818 | } |
| 819 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 820 | for (trial = 0; trial < trials; ++trial) { |
Argyrios Kyrtzidis | 40098e8 | 2011-09-12 18:09:31 +0000 | [diff] [blame] | 821 | if (clang_reparseTranslationUnit(TU, |
| 822 | trial >= remap_after_trial ? num_unsaved_files : 0, |
| 823 | trial >= remap_after_trial ? unsaved_files : 0, |
Douglas Gregor | e1e13bf | 2010-08-11 15:58:42 +0000 | [diff] [blame] | 824 | clang_defaultReparseOptions(TU))) { |
Daniel Dunbar | c8a6180 | 2010-08-18 23:09:16 +0000 | [diff] [blame] | 825 | fprintf(stderr, "Unable to reparse translation unit!\n"); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 826 | clang_disposeTranslationUnit(TU); |
| 827 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 828 | clang_disposeIndex(Idx); |
| 829 | return -1; |
| 830 | } |
Argyrios Kyrtzidis | bda536d | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 831 | |
| 832 | if (checkForErrors(TU) != 0) |
| 833 | return -1; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); |
Argyrios Kyrtzidis | bda536d | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 838 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 839 | clang_disposeIndex(Idx); |
| 840 | return result; |
| 841 | } |
| 842 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 843 | /******************************************************************************/ |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 844 | /* Logic for testing clang_getCursor(). */ |
| 845 | /******************************************************************************/ |
| 846 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 847 | static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor, |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 848 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 849 | unsigned end_line, unsigned end_col, |
| 850 | const char *prefix) { |
Ted Kremenek | 9096a20 | 2010-01-07 01:17:12 +0000 | [diff] [blame] | 851 | printf("// %s: ", FileCheckPrefix); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 852 | if (prefix) |
| 853 | printf("-%s", prefix); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 854 | PrintExtent(stdout, start_line, start_col, end_line, end_col); |
| 855 | printf(" "); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 856 | PrintCursor(cursor); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 857 | printf("\n"); |
| 858 | } |
| 859 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 860 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 861 | const char *prefix) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 862 | CXIndex Idx; |
| 863 | CXTranslationUnit TU; |
| 864 | FILE *fp; |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 865 | CXCursor prevCursor = clang_getNullCursor(); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 866 | CXFile file; |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 867 | unsigned line = 1, col = 1; |
Daniel Dunbar | 8f0bf81 | 2010-02-14 08:32:51 +0000 | [diff] [blame] | 868 | unsigned start_line = 1, start_col = 1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 869 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 870 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
| 871 | /* displayDiagnosics=*/1))) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 872 | fprintf(stderr, "Could not create Index\n"); |
| 873 | return 1; |
| 874 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 875 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 876 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 877 | return 1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 878 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 879 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 880 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 881 | return 1; |
| 882 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 883 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 884 | file = clang_getFile(TU, source_file); |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 885 | for (;;) { |
| 886 | CXCursor cursor; |
| 887 | int c = fgetc(fp); |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 888 | |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 889 | if (c == '\n') { |
| 890 | ++line; |
| 891 | col = 1; |
| 892 | } else |
| 893 | ++col; |
| 894 | |
| 895 | /* Check the cursor at this position, and dump the previous one if we have |
| 896 | * found something new. |
| 897 | */ |
| 898 | cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); |
| 899 | if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && |
| 900 | prevCursor.kind != CXCursor_InvalidFile) { |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 901 | print_cursor_file_scan(TU, prevCursor, start_line, start_col, |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 902 | line, col, prefix); |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 903 | start_line = line; |
| 904 | start_col = col; |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 905 | } |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 906 | if (c == EOF) |
| 907 | break; |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 908 | |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 909 | prevCursor = cursor; |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 910 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 911 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 912 | fclose(fp); |
Douglas Gregor | 4f5e21e | 2011-01-31 22:04:05 +0000 | [diff] [blame] | 913 | clang_disposeTranslationUnit(TU); |
| 914 | clang_disposeIndex(Idx); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | /******************************************************************************/ |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 919 | /* Logic for testing clang code completion. */ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 920 | /******************************************************************************/ |
| 921 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 922 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 923 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 924 | 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] | 925 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 926 | unsigned *column, unsigned *second_line, |
| 927 | unsigned *second_column) { |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 928 | /* Find the second colon. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 929 | const char *last_colon = strrchr(input, ':'); |
| 930 | unsigned values[4], i; |
| 931 | unsigned num_values = (second_line && second_column)? 4 : 2; |
| 932 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 933 | char *endptr = 0; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 934 | if (!last_colon || last_colon == input) { |
| 935 | if (num_values == 4) |
| 936 | fprintf(stderr, "could not parse filename:line:column:line:column in " |
| 937 | "'%s'\n", input); |
| 938 | else |
| 939 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 940 | return 1; |
| 941 | } |
| 942 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 943 | for (i = 0; i != num_values; ++i) { |
| 944 | const char *prev_colon; |
| 945 | |
| 946 | /* Parse the next line or column. */ |
| 947 | values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); |
| 948 | if (*endptr != 0 && *endptr != ':') { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 949 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 950 | (i % 2 ? "column" : "line"), input); |
| 951 | return 1; |
| 952 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 953 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 954 | if (i + 1 == num_values) |
| 955 | break; |
| 956 | |
| 957 | /* Find the previous colon. */ |
| 958 | prev_colon = last_colon - 1; |
| 959 | while (prev_colon != input && *prev_colon != ':') |
| 960 | --prev_colon; |
| 961 | if (prev_colon == input) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 962 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 963 | (i % 2 == 0? "column" : "line"), input); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 964 | return 1; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | last_colon = prev_colon; |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 970 | *line = values[0]; |
| 971 | *column = values[1]; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 972 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 973 | if (second_line && second_column) { |
| 974 | *second_line = values[2]; |
| 975 | *second_column = values[3]; |
| 976 | } |
| 977 | |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 978 | /* Copy the file name. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 979 | *filename = (char*)malloc(last_colon - input + 1); |
| 980 | memcpy(*filename, input, last_colon - input); |
| 981 | (*filename)[last_colon - input] = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | const char * |
| 986 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 987 | switch (Kind) { |
| 988 | case CXCompletionChunk_Optional: return "Optional"; |
| 989 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 990 | case CXCompletionChunk_Text: return "Text"; |
| 991 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 992 | case CXCompletionChunk_Informative: return "Informative"; |
| 993 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 994 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 995 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 996 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 997 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 998 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 999 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 1000 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 1001 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 1002 | case CXCompletionChunk_Comma: return "Comma"; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1003 | case CXCompletionChunk_ResultType: return "ResultType"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1004 | case CXCompletionChunk_Colon: return "Colon"; |
| 1005 | case CXCompletionChunk_SemiColon: return "SemiColon"; |
| 1006 | case CXCompletionChunk_Equal: return "Equal"; |
| 1007 | case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; |
| 1008 | case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1009 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1010 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1011 | return "Unknown"; |
| 1012 | } |
| 1013 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1014 | static int checkForErrors(CXTranslationUnit TU) { |
| 1015 | unsigned Num, i; |
| 1016 | CXDiagnostic Diag; |
| 1017 | CXString DiagStr; |
| 1018 | |
| 1019 | if (!getenv("CINDEXTEST_FAILONERROR")) |
| 1020 | return 0; |
| 1021 | |
| 1022 | Num = clang_getNumDiagnostics(TU); |
| 1023 | for (i = 0; i != Num; ++i) { |
| 1024 | Diag = clang_getDiagnostic(TU, i); |
| 1025 | if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) { |
| 1026 | DiagStr = clang_formatDiagnostic(Diag, |
| 1027 | clang_defaultDiagnosticDisplayOptions()); |
| 1028 | fprintf(stderr, "%s\n", clang_getCString(DiagStr)); |
| 1029 | clang_disposeString(DiagStr); |
| 1030 | clang_disposeDiagnostic(Diag); |
| 1031 | return -1; |
| 1032 | } |
| 1033 | clang_disposeDiagnostic(Diag); |
| 1034 | } |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1039 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 1040 | int I, N; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1041 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1042 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1043 | for (I = 0; I != N; ++I) { |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1044 | CXString text; |
| 1045 | const char *cstr; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1046 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1047 | = clang_getCompletionChunkKind(completion_string, I); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1049 | if (Kind == CXCompletionChunk_Optional) { |
| 1050 | fprintf(file, "{Optional "); |
| 1051 | print_completion_string( |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1052 | clang_getCompletionChunkCompletionString(completion_string, I), |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1053 | file); |
| 1054 | fprintf(file, "}"); |
| 1055 | continue; |
Douglas Gregor | 5a9c0bc | 2010-10-08 20:39:29 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | if (Kind == CXCompletionChunk_VerticalSpace) { |
| 1059 | fprintf(file, "{VerticalSpace }"); |
| 1060 | continue; |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1061 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1062 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 1063 | text = clang_getCompletionChunkText(completion_string, I); |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1064 | cstr = clang_getCString(text); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1065 | fprintf(file, "{%s %s}", |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1066 | clang_getCompletionChunkKindSpelling(Kind), |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1067 | cstr ? cstr : ""); |
| 1068 | clang_disposeString(text); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1069 | } |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | void print_completion_result(CXCompletionResult *completion_result, |
| 1074 | CXClientData client_data) { |
| 1075 | FILE *file = (FILE *)client_data; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1076 | CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 1077 | unsigned annotationCount; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1078 | |
| 1079 | fprintf(file, "%s:", clang_getCString(ks)); |
| 1080 | clang_disposeString(ks); |
| 1081 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1082 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1083 | fprintf(file, " (%u)", |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1084 | clang_getCompletionPriority(completion_result->CompletionString)); |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1085 | switch (clang_getCompletionAvailability(completion_result->CompletionString)){ |
| 1086 | case CXAvailability_Available: |
| 1087 | break; |
| 1088 | |
| 1089 | case CXAvailability_Deprecated: |
| 1090 | fprintf(file, " (deprecated)"); |
| 1091 | break; |
| 1092 | |
| 1093 | case CXAvailability_NotAvailable: |
| 1094 | fprintf(file, " (unavailable)"); |
| 1095 | break; |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1096 | |
| 1097 | case CXAvailability_NotAccessible: |
| 1098 | fprintf(file, " (inaccessible)"); |
| 1099 | break; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1100 | } |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 1101 | |
| 1102 | annotationCount = clang_getCompletionNumAnnotations( |
| 1103 | completion_result->CompletionString); |
| 1104 | if (annotationCount) { |
| 1105 | unsigned i; |
| 1106 | fprintf(file, " ("); |
| 1107 | for (i = 0; i < annotationCount; ++i) { |
| 1108 | if (i != 0) |
| 1109 | fprintf(file, ", "); |
| 1110 | fprintf(file, "\"%s\"", |
| 1111 | clang_getCString(clang_getCompletionAnnotation( |
| 1112 | completion_result->CompletionString, i))); |
| 1113 | } |
| 1114 | fprintf(file, ")"); |
| 1115 | } |
| 1116 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1117 | fprintf(file, "\n"); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1120 | void print_completion_contexts(unsigned long long contexts, FILE *file) { |
| 1121 | fprintf(file, "Completion contexts:\n"); |
| 1122 | if (contexts == CXCompletionContext_Unknown) { |
| 1123 | fprintf(file, "Unknown\n"); |
| 1124 | } |
| 1125 | if (contexts & CXCompletionContext_AnyType) { |
| 1126 | fprintf(file, "Any type\n"); |
| 1127 | } |
| 1128 | if (contexts & CXCompletionContext_AnyValue) { |
| 1129 | fprintf(file, "Any value\n"); |
| 1130 | } |
| 1131 | if (contexts & CXCompletionContext_ObjCObjectValue) { |
| 1132 | fprintf(file, "Objective-C object value\n"); |
| 1133 | } |
| 1134 | if (contexts & CXCompletionContext_ObjCSelectorValue) { |
| 1135 | fprintf(file, "Objective-C selector value\n"); |
| 1136 | } |
| 1137 | if (contexts & CXCompletionContext_CXXClassTypeValue) { |
| 1138 | fprintf(file, "C++ class type value\n"); |
| 1139 | } |
| 1140 | if (contexts & CXCompletionContext_DotMemberAccess) { |
| 1141 | fprintf(file, "Dot member access\n"); |
| 1142 | } |
| 1143 | if (contexts & CXCompletionContext_ArrowMemberAccess) { |
| 1144 | fprintf(file, "Arrow member access\n"); |
| 1145 | } |
| 1146 | if (contexts & CXCompletionContext_ObjCPropertyAccess) { |
| 1147 | fprintf(file, "Objective-C property access\n"); |
| 1148 | } |
| 1149 | if (contexts & CXCompletionContext_EnumTag) { |
| 1150 | fprintf(file, "Enum tag\n"); |
| 1151 | } |
| 1152 | if (contexts & CXCompletionContext_UnionTag) { |
| 1153 | fprintf(file, "Union tag\n"); |
| 1154 | } |
| 1155 | if (contexts & CXCompletionContext_StructTag) { |
| 1156 | fprintf(file, "Struct tag\n"); |
| 1157 | } |
| 1158 | if (contexts & CXCompletionContext_ClassTag) { |
| 1159 | fprintf(file, "Class name\n"); |
| 1160 | } |
| 1161 | if (contexts & CXCompletionContext_Namespace) { |
| 1162 | fprintf(file, "Namespace or namespace alias\n"); |
| 1163 | } |
| 1164 | if (contexts & CXCompletionContext_NestedNameSpecifier) { |
| 1165 | fprintf(file, "Nested name specifier\n"); |
| 1166 | } |
| 1167 | if (contexts & CXCompletionContext_ObjCInterface) { |
| 1168 | fprintf(file, "Objective-C interface\n"); |
| 1169 | } |
| 1170 | if (contexts & CXCompletionContext_ObjCProtocol) { |
| 1171 | fprintf(file, "Objective-C protocol\n"); |
| 1172 | } |
| 1173 | if (contexts & CXCompletionContext_ObjCCategory) { |
| 1174 | fprintf(file, "Objective-C category\n"); |
| 1175 | } |
| 1176 | if (contexts & CXCompletionContext_ObjCInstanceMessage) { |
| 1177 | fprintf(file, "Objective-C instance method\n"); |
| 1178 | } |
| 1179 | if (contexts & CXCompletionContext_ObjCClassMessage) { |
| 1180 | fprintf(file, "Objective-C class method\n"); |
| 1181 | } |
| 1182 | if (contexts & CXCompletionContext_ObjCSelectorName) { |
| 1183 | fprintf(file, "Objective-C selector name\n"); |
| 1184 | } |
| 1185 | if (contexts & CXCompletionContext_MacroName) { |
| 1186 | fprintf(file, "Macro name\n"); |
| 1187 | } |
| 1188 | if (contexts & CXCompletionContext_NaturalLanguage) { |
| 1189 | fprintf(file, "Natural language\n"); |
| 1190 | } |
| 1191 | } |
| 1192 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1193 | int my_stricmp(const char *s1, const char *s2) { |
| 1194 | while (*s1 && *s2) { |
NAKAMURA Takumi | 6d55521 | 2011-03-09 03:02:28 +0000 | [diff] [blame] | 1195 | int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1196 | if (c1 < c2) |
| 1197 | return -1; |
| 1198 | else if (c1 > c2) |
| 1199 | return 1; |
| 1200 | |
| 1201 | ++s1; |
| 1202 | ++s2; |
| 1203 | } |
| 1204 | |
| 1205 | if (*s1) |
| 1206 | return 1; |
| 1207 | else if (*s2) |
| 1208 | return -1; |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1212 | int perform_code_completion(int argc, const char **argv, int timing_only) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1213 | const char *input = argv[1]; |
| 1214 | char *filename = 0; |
| 1215 | unsigned line; |
| 1216 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 1217 | CXIndex CIdx; |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1218 | int errorCode; |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1219 | struct CXUnsavedFile *unsaved_files = 0; |
| 1220 | int num_unsaved_files = 0; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1221 | CXCodeCompleteResults *results = 0; |
Dawn Perchik | 25d9b00 | 2010-09-30 22:26:05 +0000 | [diff] [blame] | 1222 | CXTranslationUnit TU = 0; |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1223 | unsigned I, Repeats = 1; |
| 1224 | unsigned completionOptions = clang_defaultCodeCompleteOptions(); |
| 1225 | |
| 1226 | if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS")) |
| 1227 | completionOptions |= CXCodeComplete_IncludeCodePatterns; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1228 | |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1229 | if (timing_only) |
| 1230 | input += strlen("-code-completion-timing="); |
| 1231 | else |
| 1232 | input += strlen("-code-completion-at="); |
| 1233 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1234 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1235 | 0, 0))) |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1236 | return errorCode; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1238 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 1239 | return -1; |
| 1240 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1241 | CIdx = clang_createIndex(0, 0); |
| 1242 | |
| 1243 | if (getenv("CINDEXTEST_EDITING")) |
| 1244 | Repeats = 5; |
| 1245 | |
| 1246 | TU = clang_parseTranslationUnit(CIdx, 0, |
| 1247 | argv + num_unsaved_files + 2, |
| 1248 | argc - num_unsaved_files - 2, |
| 1249 | 0, 0, getDefaultParsingOptions()); |
| 1250 | if (!TU) { |
| 1251 | fprintf(stderr, "Unable to load translation unit!\n"); |
| 1252 | return 1; |
| 1253 | } |
Douglas Gregor | 08bb4c6 | 2010-11-15 23:00:34 +0000 | [diff] [blame] | 1254 | |
| 1255 | if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) { |
| 1256 | fprintf(stderr, "Unable to reparse translation init!\n"); |
| 1257 | return 1; |
| 1258 | } |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1259 | |
| 1260 | for (I = 0; I != Repeats; ++I) { |
| 1261 | results = clang_codeCompleteAt(TU, filename, line, column, |
| 1262 | unsaved_files, num_unsaved_files, |
| 1263 | completionOptions); |
| 1264 | if (!results) { |
| 1265 | fprintf(stderr, "Unable to perform code completion!\n"); |
Daniel Dunbar | 2de41c9 | 2010-08-19 23:44:06 +0000 | [diff] [blame] | 1266 | return 1; |
| 1267 | } |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1268 | if (I != Repeats-1) |
| 1269 | clang_disposeCodeCompleteResults(results); |
| 1270 | } |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 1271 | |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1272 | if (results) { |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 1273 | unsigned i, n = results->NumResults, containerIsIncomplete = 0; |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1274 | unsigned long long contexts; |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 1275 | enum CXCursorKind containerKind; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 1276 | CXString objCSelector; |
| 1277 | const char *selectorString; |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1278 | if (!timing_only) { |
| 1279 | /* Sort the code-completion results based on the typed text. */ |
| 1280 | clang_sortCodeCompletionResults(results->Results, results->NumResults); |
| 1281 | |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1282 | for (i = 0; i != n; ++i) |
| 1283 | print_completion_result(results->Results + i, stdout); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1284 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1285 | n = clang_codeCompleteGetNumDiagnostics(results); |
| 1286 | for (i = 0; i != n; ++i) { |
| 1287 | CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); |
| 1288 | PrintDiagnostic(diag); |
| 1289 | clang_disposeDiagnostic(diag); |
| 1290 | } |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1291 | |
| 1292 | contexts = clang_codeCompleteGetContexts(results); |
| 1293 | print_completion_contexts(contexts, stdout); |
| 1294 | |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 1295 | containerKind = clang_codeCompleteGetContainerKind(results, |
| 1296 | &containerIsIncomplete); |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 1297 | |
| 1298 | if (containerKind != CXCursor_InvalidCode) { |
| 1299 | /* We have found a container */ |
| 1300 | CXString containerUSR, containerKindSpelling; |
| 1301 | containerKindSpelling = clang_getCursorKindSpelling(containerKind); |
| 1302 | printf("Container Kind: %s\n", clang_getCString(containerKindSpelling)); |
| 1303 | clang_disposeString(containerKindSpelling); |
| 1304 | |
| 1305 | if (containerIsIncomplete) { |
| 1306 | printf("Container is incomplete\n"); |
| 1307 | } |
| 1308 | else { |
| 1309 | printf("Container is complete\n"); |
| 1310 | } |
| 1311 | |
| 1312 | containerUSR = clang_codeCompleteGetContainerUSR(results); |
| 1313 | printf("Container USR: %s\n", clang_getCString(containerUSR)); |
| 1314 | clang_disposeString(containerUSR); |
| 1315 | } |
| 1316 | |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 1317 | objCSelector = clang_codeCompleteGetObjCSelector(results); |
| 1318 | selectorString = clang_getCString(objCSelector); |
| 1319 | if (selectorString && strlen(selectorString) > 0) { |
| 1320 | printf("Objective-C selector: %s\n", selectorString); |
| 1321 | } |
| 1322 | clang_disposeString(objCSelector); |
| 1323 | |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1324 | clang_disposeCodeCompleteResults(results); |
| 1325 | } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1326 | clang_disposeTranslationUnit(TU); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1327 | clang_disposeIndex(CIdx); |
| 1328 | free(filename); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1329 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1330 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1331 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1332 | return 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1335 | typedef struct { |
| 1336 | char *filename; |
| 1337 | unsigned line; |
| 1338 | unsigned column; |
| 1339 | } CursorSourceLocation; |
| 1340 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1341 | static int inspect_cursor_at(int argc, const char **argv) { |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1342 | CXIndex CIdx; |
| 1343 | int errorCode; |
| 1344 | struct CXUnsavedFile *unsaved_files = 0; |
| 1345 | int num_unsaved_files = 0; |
| 1346 | CXTranslationUnit TU; |
| 1347 | CXCursor Cursor; |
| 1348 | CursorSourceLocation *Locations = 0; |
| 1349 | unsigned NumLocations = 0, Loc; |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1350 | unsigned Repeats = 1; |
Douglas Gregor | bdc4b36 | 2010-11-30 06:04:54 +0000 | [diff] [blame] | 1351 | unsigned I; |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1352 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1353 | /* Count the number of locations. */ |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1354 | while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) |
| 1355 | ++NumLocations; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1356 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1357 | /* Parse the locations. */ |
| 1358 | assert(NumLocations > 0 && "Unable to count locations?"); |
| 1359 | Locations = (CursorSourceLocation *)malloc( |
| 1360 | NumLocations * sizeof(CursorSourceLocation)); |
| 1361 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 1362 | const char *input = argv[Loc + 1] + strlen("-cursor-at="); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1363 | if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| 1364 | &Locations[Loc].line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1365 | &Locations[Loc].column, 0, 0))) |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1366 | return errorCode; |
| 1367 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1368 | |
| 1369 | if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1370 | &num_unsaved_files)) |
| 1371 | return -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1372 | |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1373 | if (getenv("CINDEXTEST_EDITING")) |
| 1374 | Repeats = 5; |
| 1375 | |
| 1376 | /* Parse the translation unit. When we're testing clang_getCursor() after |
| 1377 | reparsing, don't remap unsaved files until the second parse. */ |
| 1378 | CIdx = clang_createIndex(1, 1); |
| 1379 | TU = clang_parseTranslationUnit(CIdx, argv[argc - 1], |
| 1380 | argv + num_unsaved_files + 1 + NumLocations, |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1381 | argc - num_unsaved_files - 2 - NumLocations, |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1382 | unsaved_files, |
| 1383 | Repeats > 1? 0 : num_unsaved_files, |
| 1384 | getDefaultParsingOptions()); |
| 1385 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1386 | if (!TU) { |
| 1387 | fprintf(stderr, "unable to parse input\n"); |
| 1388 | return -1; |
| 1389 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1390 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1391 | if (checkForErrors(TU) != 0) |
| 1392 | return -1; |
| 1393 | |
Douglas Gregor | bdc4b36 | 2010-11-30 06:04:54 +0000 | [diff] [blame] | 1394 | for (I = 0; I != Repeats; ++I) { |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1395 | if (Repeats > 1 && |
| 1396 | clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 1397 | clang_defaultReparseOptions(TU))) { |
| 1398 | clang_disposeTranslationUnit(TU); |
| 1399 | return 1; |
| 1400 | } |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1401 | |
| 1402 | if (checkForErrors(TU) != 0) |
| 1403 | return -1; |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1404 | |
| 1405 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 1406 | CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| 1407 | if (!file) |
| 1408 | continue; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1409 | |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1410 | Cursor = clang_getCursor(TU, |
| 1411 | clang_getLocation(TU, file, Locations[Loc].line, |
| 1412 | Locations[Loc].column)); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (checkForErrors(TU) != 0) |
| 1415 | return -1; |
| 1416 | |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1417 | if (I + 1 == Repeats) { |
Douglas Gregor | 8fa0a80 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1418 | CXCompletionString completionString = clang_getCursorCompletionString( |
| 1419 | Cursor); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1420 | PrintCursor(Cursor); |
Douglas Gregor | 8fa0a80 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1421 | if (completionString != NULL) { |
| 1422 | printf("\nCompletion string: "); |
| 1423 | print_completion_string(completionString, stdout); |
| 1424 | } |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1425 | printf("\n"); |
| 1426 | free(Locations[Loc].filename); |
| 1427 | } |
| 1428 | } |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1429 | } |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 1430 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1431 | PrintDiagnostics(TU); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 1432 | clang_disposeTranslationUnit(TU); |
| 1433 | clang_disposeIndex(CIdx); |
| 1434 | free(Locations); |
| 1435 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1436 | return 0; |
| 1437 | } |
| 1438 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1439 | static enum CXVisitorResult findFileRefsVisit(void *context, |
| 1440 | CXCursor cursor, CXSourceRange range) { |
| 1441 | if (clang_Range_isNull(range)) |
| 1442 | return CXVisit_Continue; |
| 1443 | |
| 1444 | PrintCursor(cursor); |
| 1445 | PrintRange(range, ""); |
| 1446 | printf("\n"); |
| 1447 | return CXVisit_Continue; |
| 1448 | } |
| 1449 | |
| 1450 | static int find_file_refs_at(int argc, const char **argv) { |
| 1451 | CXIndex CIdx; |
| 1452 | int errorCode; |
| 1453 | struct CXUnsavedFile *unsaved_files = 0; |
| 1454 | int num_unsaved_files = 0; |
| 1455 | CXTranslationUnit TU; |
| 1456 | CXCursor Cursor; |
| 1457 | CursorSourceLocation *Locations = 0; |
| 1458 | unsigned NumLocations = 0, Loc; |
| 1459 | unsigned Repeats = 1; |
| 1460 | unsigned I; |
| 1461 | |
| 1462 | /* Count the number of locations. */ |
| 1463 | while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1]) |
| 1464 | ++NumLocations; |
| 1465 | |
| 1466 | /* Parse the locations. */ |
| 1467 | assert(NumLocations > 0 && "Unable to count locations?"); |
| 1468 | Locations = (CursorSourceLocation *)malloc( |
| 1469 | NumLocations * sizeof(CursorSourceLocation)); |
| 1470 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 1471 | const char *input = argv[Loc + 1] + strlen("-file-refs-at="); |
| 1472 | if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| 1473 | &Locations[Loc].line, |
| 1474 | &Locations[Loc].column, 0, 0))) |
| 1475 | return errorCode; |
| 1476 | } |
| 1477 | |
| 1478 | if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
| 1479 | &num_unsaved_files)) |
| 1480 | return -1; |
| 1481 | |
| 1482 | if (getenv("CINDEXTEST_EDITING")) |
| 1483 | Repeats = 5; |
| 1484 | |
| 1485 | /* Parse the translation unit. When we're testing clang_getCursor() after |
| 1486 | reparsing, don't remap unsaved files until the second parse. */ |
| 1487 | CIdx = clang_createIndex(1, 1); |
| 1488 | TU = clang_parseTranslationUnit(CIdx, argv[argc - 1], |
| 1489 | argv + num_unsaved_files + 1 + NumLocations, |
| 1490 | argc - num_unsaved_files - 2 - NumLocations, |
| 1491 | unsaved_files, |
| 1492 | Repeats > 1? 0 : num_unsaved_files, |
| 1493 | getDefaultParsingOptions()); |
| 1494 | |
| 1495 | if (!TU) { |
| 1496 | fprintf(stderr, "unable to parse input\n"); |
| 1497 | return -1; |
| 1498 | } |
| 1499 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1500 | if (checkForErrors(TU) != 0) |
| 1501 | return -1; |
| 1502 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1503 | for (I = 0; I != Repeats; ++I) { |
| 1504 | if (Repeats > 1 && |
| 1505 | clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 1506 | clang_defaultReparseOptions(TU))) { |
| 1507 | clang_disposeTranslationUnit(TU); |
| 1508 | return 1; |
| 1509 | } |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1510 | |
| 1511 | if (checkForErrors(TU) != 0) |
| 1512 | return -1; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1513 | |
| 1514 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 1515 | CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| 1516 | if (!file) |
| 1517 | continue; |
| 1518 | |
| 1519 | Cursor = clang_getCursor(TU, |
| 1520 | clang_getLocation(TU, file, Locations[Loc].line, |
| 1521 | Locations[Loc].column)); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1522 | |
| 1523 | if (checkForErrors(TU) != 0) |
| 1524 | return -1; |
| 1525 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1526 | if (I + 1 == Repeats) { |
Erik Verbruggen | 26fc0f9 | 2011-10-06 11:38:08 +0000 | [diff] [blame] | 1527 | CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit }; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1528 | PrintCursor(Cursor); |
| 1529 | printf("\n"); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1530 | clang_findReferencesInFile(Cursor, file, visitor); |
| 1531 | free(Locations[Loc].filename); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1532 | |
| 1533 | if (checkForErrors(TU) != 0) |
| 1534 | return -1; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | PrintDiagnostics(TU); |
| 1540 | clang_disposeTranslationUnit(TU); |
| 1541 | clang_disposeIndex(CIdx); |
| 1542 | free(Locations); |
| 1543 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1547 | typedef struct { |
| 1548 | const char *check_prefix; |
| 1549 | int first_check_printed; |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1550 | int fail_for_error; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1551 | } IndexData; |
| 1552 | |
| 1553 | static void printCheck(IndexData *data) { |
| 1554 | if (data->check_prefix) { |
| 1555 | if (data->first_check_printed) { |
| 1556 | printf("// %s-NEXT: ", data->check_prefix); |
| 1557 | } else { |
| 1558 | printf("// %s : ", data->check_prefix); |
| 1559 | data->first_check_printed = 1; |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1564 | static void printCXIndexFile(CXIdxClientFile file) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1565 | CXString filename = clang_getFileName((CXFile)file); |
| 1566 | printf("%s", clang_getCString(filename)); |
| 1567 | clang_disposeString(filename); |
| 1568 | } |
| 1569 | |
| 1570 | static void printCXIndexLoc(CXIdxLoc loc) { |
| 1571 | CXString filename; |
| 1572 | const char *cname, *end; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1573 | CXIdxClientFile file; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1574 | unsigned line, column; |
Argyrios Kyrtzidis | 36180f3 | 2011-10-17 22:12:24 +0000 | [diff] [blame] | 1575 | int isHeader; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1576 | |
| 1577 | clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); |
| 1578 | if (line == 0) { |
| 1579 | printf("<null loc>"); |
| 1580 | return; |
| 1581 | } |
| 1582 | filename = clang_getFileName((CXFile)file); |
| 1583 | cname = clang_getCString(filename); |
| 1584 | end = cname + strlen(cname); |
Argyrios Kyrtzidis | 36180f3 | 2011-10-17 22:12:24 +0000 | [diff] [blame] | 1585 | isHeader = (end[-2] == '.' && end[-1] == 'h'); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1586 | |
| 1587 | if (isHeader) { |
| 1588 | printCXIndexFile(file); |
| 1589 | printf(":"); |
| 1590 | } |
| 1591 | printf("%d:%d", line, column); |
| 1592 | } |
| 1593 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1594 | static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info, |
| 1595 | CXIdxLoc loc) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1596 | const char *name; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1597 | char *newStr; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1598 | CXIdxClientFile file; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1599 | unsigned line, column; |
| 1600 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1601 | name = info->name; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1602 | if (!name) |
| 1603 | name = "<anon-tag>"; |
| 1604 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1605 | clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); |
Argyrios Kyrtzidis | f89bc05 | 2011-10-20 17:21:46 +0000 | [diff] [blame] | 1606 | /* FIXME: free these.*/ |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1607 | newStr = (char *)malloc(strlen(name) + 10); |
| 1608 | sprintf(newStr, "%s:%d:%d", name, line, column); |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1609 | return (CXIdxClientContainer)newStr; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1612 | static void printCXIndexContainer(CXIdxClientContainer container) { |
Argyrios Kyrtzidis | 3e340a6 | 2011-11-16 02:35:05 +0000 | [diff] [blame^] | 1613 | if (!container) |
| 1614 | printf("[<<NULL>>]"); |
| 1615 | else |
| 1616 | printf("[%s]", (const char *)container); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1619 | static const char *getEntityKindString(CXIdxEntityKind kind) { |
| 1620 | switch (kind) { |
| 1621 | case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>"; |
| 1622 | case CXIdxEntity_Typedef: return "typedef"; |
| 1623 | case CXIdxEntity_Function: return "function"; |
| 1624 | case CXIdxEntity_Variable: return "variable"; |
| 1625 | case CXIdxEntity_Field: return "field"; |
| 1626 | case CXIdxEntity_EnumConstant: return "enumerator"; |
| 1627 | case CXIdxEntity_ObjCClass: return "objc-class"; |
| 1628 | case CXIdxEntity_ObjCProtocol: return "objc-protocol"; |
| 1629 | case CXIdxEntity_ObjCCategory: return "objc-category"; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 1630 | case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method"; |
| 1631 | case CXIdxEntity_ObjCClassMethod: return "objc-class-method"; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1632 | case CXIdxEntity_ObjCProperty: return "objc-property"; |
| 1633 | case CXIdxEntity_ObjCIvar: return "objc-ivar"; |
| 1634 | case CXIdxEntity_Enum: return "enum"; |
| 1635 | case CXIdxEntity_Struct: return "struct"; |
| 1636 | case CXIdxEntity_Union: return "union"; |
| 1637 | case CXIdxEntity_CXXClass: return "c++-class"; |
| 1638 | } |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1639 | assert(0 && "Garbage entity kind"); |
| 1640 | return 0; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1643 | static void printEntityInfo(const char *cb, |
| 1644 | CXClientData client_data, |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1645 | const CXIdxEntityInfo *info) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1646 | const char *name; |
| 1647 | IndexData *index_data; |
| 1648 | index_data = (IndexData *)client_data; |
| 1649 | printCheck(index_data); |
| 1650 | |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 1651 | if (!info) { |
| 1652 | printf("%s: <<NULL>>", cb); |
| 1653 | return; |
| 1654 | } |
| 1655 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1656 | name = info->name; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1657 | if (!name) |
| 1658 | name = "<anon-tag>"; |
| 1659 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1660 | printf("%s: kind: %s", cb, getEntityKindString(info->kind)); |
| 1661 | printf(" | name: %s", name); |
| 1662 | printf(" | USR: %s", info->USR); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 1665 | static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo, |
| 1666 | CXClientData client_data) { |
| 1667 | unsigned i; |
| 1668 | for (i = 0; i < ProtoInfo->numProtocols; ++i) { |
| 1669 | printEntityInfo(" <protocol>", client_data, |
| 1670 | ProtoInfo->protocols[i]->protocol); |
| 1671 | printf(" | cursor: "); |
| 1672 | PrintCursor(ProtoInfo->protocols[i]->cursor); |
| 1673 | printf(" | loc: "); |
| 1674 | printCXIndexLoc(ProtoInfo->protocols[i]->loc); |
| 1675 | printf("\n"); |
| 1676 | } |
| 1677 | } |
| 1678 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1679 | static void index_diagnostic(CXClientData client_data, |
| 1680 | CXDiagnostic diag, void *reserved) { |
| 1681 | CXString str; |
| 1682 | const char *cstr; |
| 1683 | IndexData *index_data; |
| 1684 | index_data = (IndexData *)client_data; |
| 1685 | printCheck(index_data); |
| 1686 | |
| 1687 | str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); |
| 1688 | cstr = clang_getCString(str); |
Argyrios Kyrtzidis | 66042b3 | 2011-11-05 04:03:35 +0000 | [diff] [blame] | 1689 | printf("[diagnostic]: %s\n", cstr); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1690 | clang_disposeString(str); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1691 | |
| 1692 | if (getenv("CINDEXTEST_FAILONERROR") && |
| 1693 | clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) { |
| 1694 | index_data->fail_for_error = 1; |
| 1695 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1698 | static CXIdxClientFile index_enteredMainFile(CXClientData client_data, |
| 1699 | CXFile file, void *reserved) { |
| 1700 | IndexData *index_data; |
| 1701 | index_data = (IndexData *)client_data; |
| 1702 | printCheck(index_data); |
| 1703 | |
| 1704 | printf("[enteredMainFile]: "); |
| 1705 | printCXIndexFile((CXIdxClientFile)file); |
| 1706 | printf("\n"); |
| 1707 | |
| 1708 | return (CXIdxClientFile)file; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1711 | static CXIdxClientFile index_ppIncludedFile(CXClientData client_data, |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1712 | const CXIdxIncludedFileInfo *info) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1713 | IndexData *index_data; |
| 1714 | index_data = (IndexData *)client_data; |
| 1715 | printCheck(index_data); |
| 1716 | |
Argyrios Kyrtzidis | 66042b3 | 2011-11-05 04:03:35 +0000 | [diff] [blame] | 1717 | printf("[ppIncludedFile]: "); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1718 | printCXIndexFile((CXIdxClientFile)info->file); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1719 | printf(" | name: \"%s\"", info->filename); |
| 1720 | printf(" | hash loc: "); |
| 1721 | printCXIndexLoc(info->hashLoc); |
| 1722 | printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1723 | |
| 1724 | return (CXIdxClientFile)info->file; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1727 | static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1728 | void *reserved) { |
| 1729 | IndexData *index_data; |
| 1730 | index_data = (IndexData *)client_data; |
| 1731 | printCheck(index_data); |
| 1732 | |
Argyrios Kyrtzidis | 66042b3 | 2011-11-05 04:03:35 +0000 | [diff] [blame] | 1733 | printf("[startedTranslationUnit]\n"); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1734 | return (CXIdxClientContainer)"TU"; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1737 | static void index_indexDeclaration(CXClientData client_data, |
| 1738 | const CXIdxDeclInfo *info, |
| 1739 | const CXIdxDeclOut *outData) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1740 | IndexData *index_data; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1741 | const CXIdxObjCCategoryDeclInfo *CatInfo; |
| 1742 | const CXIdxObjCInterfaceDeclInfo *InterInfo; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 1743 | const CXIdxObjCProtocolRefListInfo *ProtoInfo; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1744 | index_data = (IndexData *)client_data; |
| 1745 | |
| 1746 | printEntityInfo("[indexDeclaration]", client_data, info->entityInfo); |
| 1747 | printf(" | cursor: "); |
| 1748 | PrintCursor(info->cursor); |
| 1749 | printf(" | loc: "); |
| 1750 | printCXIndexLoc(info->loc); |
| 1751 | printf(" | container: "); |
| 1752 | printCXIndexContainer(info->container); |
| 1753 | printf(" | isRedecl: %d", info->isRedeclaration); |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 1754 | printf(" | isDef: %d", info->isDefinition); |
| 1755 | printf(" | isContainer: %d", info->isContainer); |
| 1756 | printf(" | isImplicit: %d\n", info->isImplicit); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1757 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1758 | if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) { |
| 1759 | const char *kindName = 0; |
| 1760 | CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind; |
| 1761 | switch (K) { |
| 1762 | case CXIdxObjCContainer_ForwardRef: |
| 1763 | kindName = "forward-ref"; break; |
| 1764 | case CXIdxObjCContainer_Interface: |
| 1765 | kindName = "interface"; break; |
| 1766 | case CXIdxObjCContainer_Implementation: |
| 1767 | kindName = "implementation"; break; |
| 1768 | } |
| 1769 | printCheck(index_data); |
| 1770 | printf(" <ObjCContainerInfo>: kind: %s\n", kindName); |
| 1771 | } |
| 1772 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1773 | if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1774 | printEntityInfo(" <ObjCCategoryInfo>: class", client_data, |
| 1775 | CatInfo->objcClass); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 1776 | printf(" | cursor: "); |
| 1777 | PrintCursor(CatInfo->classCursor); |
| 1778 | printf(" | loc: "); |
| 1779 | printCXIndexLoc(CatInfo->classLoc); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1780 | printf("\n"); |
| 1781 | } |
| 1782 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1783 | if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) { |
| 1784 | if (InterInfo->superInfo) { |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 1785 | printEntityInfo(" <base>", client_data, |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1786 | InterInfo->superInfo->base); |
| 1787 | printf(" | cursor: "); |
| 1788 | PrintCursor(InterInfo->superInfo->cursor); |
| 1789 | printf(" | loc: "); |
| 1790 | printCXIndexLoc(InterInfo->superInfo->loc); |
| 1791 | printf("\n"); |
| 1792 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 1795 | if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) { |
| 1796 | printProtocolList(ProtoInfo, client_data); |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1797 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1798 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1799 | if (outData->outContainer) |
| 1800 | *outData->outContainer = makeClientContainer(info->entityInfo, info->loc); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | static void index_indexEntityReference(CXClientData client_data, |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1804 | const CXIdxEntityRefInfo *info) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1805 | printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1806 | printf(" | cursor: "); |
| 1807 | PrintCursor(info->cursor); |
| 1808 | printf(" | loc: "); |
| 1809 | printCXIndexLoc(info->loc); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1810 | printEntityInfo(" | <parent>:", client_data, info->parentEntity); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1811 | printf(" | container: "); |
| 1812 | printCXIndexContainer(info->container); |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 1813 | printf(" | refkind: "); |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 1814 | switch (info->kind) { |
| 1815 | case CXIdxEntityRef_Direct: printf("direct"); break; |
| 1816 | case CXIdxEntityRef_ImplicitProperty: printf("implicit prop"); break; |
| 1817 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1818 | printf("\n"); |
| 1819 | } |
| 1820 | |
| 1821 | static IndexerCallbacks IndexCB = { |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 1822 | 0, /*abortQuery*/ |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1823 | index_diagnostic, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1824 | index_enteredMainFile, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1825 | index_ppIncludedFile, |
Argyrios Kyrtzidis | f89bc05 | 2011-10-20 17:21:46 +0000 | [diff] [blame] | 1826 | 0, /*importedASTFile*/ |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1827 | index_startedTranslationUnit, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 1828 | index_indexDeclaration, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1829 | index_indexEntityReference |
| 1830 | }; |
| 1831 | |
| 1832 | static int index_file(int argc, const char **argv) { |
| 1833 | const char *check_prefix; |
| 1834 | CXIndex CIdx; |
| 1835 | IndexData index_data; |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1836 | int result; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1837 | |
| 1838 | check_prefix = 0; |
| 1839 | if (argc > 0) { |
| 1840 | if (strstr(argv[0], "-check-prefix=") == argv[0]) { |
| 1841 | check_prefix = argv[0] + strlen("-check-prefix="); |
| 1842 | ++argv; |
| 1843 | --argc; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | if (argc == 0) { |
| 1848 | fprintf(stderr, "no compiler arguments\n"); |
| 1849 | return -1; |
| 1850 | } |
| 1851 | |
| 1852 | CIdx = clang_createIndex(0, 1); |
| 1853 | index_data.check_prefix = check_prefix; |
| 1854 | index_data.first_check_printed = 0; |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1855 | index_data.fail_for_error = 0; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1856 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 1857 | result = clang_indexSourceFile(CIdx, &index_data, |
| 1858 | &IndexCB,sizeof(IndexCB), |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 1859 | CXIndexOpt_OneRefPerFile, |
| 1860 | 0, argv, argc, 0, 0, 0, 0); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 1861 | if (index_data.fail_for_error) |
| 1862 | return -1; |
| 1863 | |
| 1864 | return result; |
| 1865 | } |
| 1866 | |
| 1867 | static int index_tu(int argc, const char **argv) { |
| 1868 | CXIndex Idx; |
| 1869 | CXTranslationUnit TU; |
| 1870 | const char *check_prefix; |
| 1871 | IndexData index_data; |
| 1872 | int result; |
| 1873 | |
| 1874 | check_prefix = 0; |
| 1875 | if (argc > 0) { |
| 1876 | if (strstr(argv[0], "-check-prefix=") == argv[0]) { |
| 1877 | check_prefix = argv[0] + strlen("-check-prefix="); |
| 1878 | ++argv; |
| 1879 | --argc; |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | if (argc == 0) { |
| 1884 | fprintf(stderr, "no ast file\n"); |
| 1885 | return -1; |
| 1886 | } |
| 1887 | |
| 1888 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
| 1889 | /* displayDiagnosics=*/1))) { |
| 1890 | fprintf(stderr, "Could not create Index\n"); |
| 1891 | return 1; |
| 1892 | } |
| 1893 | |
| 1894 | if (!CreateTranslationUnit(Idx, argv[0], &TU)) |
| 1895 | return 1; |
| 1896 | |
| 1897 | index_data.check_prefix = check_prefix; |
| 1898 | index_data.first_check_printed = 0; |
| 1899 | index_data.fail_for_error = 0; |
| 1900 | |
| 1901 | result = clang_indexTranslationUnit(TU, &index_data, |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 1902 | &IndexCB,sizeof(IndexCB), |
| 1903 | CXIndexOpt_OneRefPerFile); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1904 | if (index_data.fail_for_error) |
| 1905 | return -1; |
| 1906 | |
| 1907 | return result; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1910 | int perform_token_annotation(int argc, const char **argv) { |
| 1911 | const char *input = argv[1]; |
| 1912 | char *filename = 0; |
| 1913 | unsigned line, second_line; |
| 1914 | unsigned column, second_column; |
| 1915 | CXIndex CIdx; |
| 1916 | CXTranslationUnit TU = 0; |
| 1917 | int errorCode; |
| 1918 | struct CXUnsavedFile *unsaved_files = 0; |
| 1919 | int num_unsaved_files = 0; |
| 1920 | CXToken *tokens; |
| 1921 | unsigned num_tokens; |
| 1922 | CXSourceRange range; |
| 1923 | CXSourceLocation startLoc, endLoc; |
| 1924 | CXFile file = 0; |
| 1925 | CXCursor *cursors = 0; |
| 1926 | unsigned i; |
| 1927 | |
| 1928 | input += strlen("-test-annotate-tokens="); |
| 1929 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
| 1930 | &second_line, &second_column))) |
| 1931 | return errorCode; |
| 1932 | |
| 1933 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 1934 | return -1; |
| 1935 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1936 | CIdx = clang_createIndex(0, 1); |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 1937 | TU = clang_parseTranslationUnit(CIdx, argv[argc - 1], |
| 1938 | argv + num_unsaved_files + 2, |
| 1939 | argc - num_unsaved_files - 3, |
| 1940 | unsaved_files, |
| 1941 | num_unsaved_files, |
| 1942 | getDefaultParsingOptions()); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1943 | if (!TU) { |
| 1944 | fprintf(stderr, "unable to parse input\n"); |
| 1945 | clang_disposeIndex(CIdx); |
| 1946 | free(filename); |
| 1947 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1948 | return -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1949 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1950 | errorCode = 0; |
| 1951 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1952 | if (checkForErrors(TU) != 0) |
| 1953 | return -1; |
| 1954 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 1955 | if (getenv("CINDEXTEST_EDITING")) { |
| 1956 | for (i = 0; i < 5; ++i) { |
| 1957 | if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 1958 | clang_defaultReparseOptions(TU))) { |
| 1959 | fprintf(stderr, "Unable to reparse translation unit!\n"); |
| 1960 | errorCode = -1; |
| 1961 | goto teardown; |
| 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1966 | if (checkForErrors(TU) != 0) { |
| 1967 | errorCode = -1; |
| 1968 | goto teardown; |
| 1969 | } |
| 1970 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1971 | file = clang_getFile(TU, filename); |
| 1972 | if (!file) { |
| 1973 | fprintf(stderr, "file %s is not in this translation unit\n", filename); |
| 1974 | errorCode = -1; |
| 1975 | goto teardown; |
| 1976 | } |
| 1977 | |
| 1978 | startLoc = clang_getLocation(TU, file, line, column); |
| 1979 | if (clang_equalLocations(clang_getNullLocation(), startLoc)) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1980 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1981 | column); |
| 1982 | errorCode = -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1983 | goto teardown; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | endLoc = clang_getLocation(TU, file, second_line, second_column); |
| 1987 | if (clang_equalLocations(clang_getNullLocation(), endLoc)) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1988 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1989 | second_line, second_column); |
| 1990 | errorCode = -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1991 | goto teardown; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | range = clang_getRange(startLoc, endLoc); |
| 1995 | clang_tokenize(TU, range, &tokens, &num_tokens); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1996 | |
| 1997 | if (checkForErrors(TU) != 0) { |
| 1998 | errorCode = -1; |
| 1999 | goto teardown; |
| 2000 | } |
| 2001 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2002 | cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); |
| 2003 | clang_annotateTokens(TU, tokens, num_tokens, cursors); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2004 | |
| 2005 | if (checkForErrors(TU) != 0) { |
| 2006 | errorCode = -1; |
| 2007 | goto teardown; |
| 2008 | } |
| 2009 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2010 | for (i = 0; i != num_tokens; ++i) { |
| 2011 | const char *kind = "<unknown>"; |
| 2012 | CXString spelling = clang_getTokenSpelling(TU, tokens[i]); |
| 2013 | CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); |
| 2014 | unsigned start_line, start_column, end_line, end_column; |
| 2015 | |
| 2016 | switch (clang_getTokenKind(tokens[i])) { |
| 2017 | case CXToken_Punctuation: kind = "Punctuation"; break; |
| 2018 | case CXToken_Keyword: kind = "Keyword"; break; |
| 2019 | case CXToken_Identifier: kind = "Identifier"; break; |
| 2020 | case CXToken_Literal: kind = "Literal"; break; |
| 2021 | case CXToken_Comment: kind = "Comment"; break; |
| 2022 | } |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 2023 | clang_getSpellingLocation(clang_getRangeStart(extent), |
| 2024 | 0, &start_line, &start_column, 0); |
| 2025 | clang_getSpellingLocation(clang_getRangeEnd(extent), |
| 2026 | 0, &end_line, &end_column, 0); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 2027 | printf("%s: \"%s\" ", kind, clang_getCString(spelling)); |
| 2028 | PrintExtent(stdout, start_line, start_column, end_line, end_column); |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 2029 | if (!clang_isInvalid(cursors[i].kind)) { |
| 2030 | printf(" "); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2031 | PrintCursor(cursors[i]); |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 2032 | } |
| 2033 | printf("\n"); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2034 | } |
| 2035 | free(cursors); |
Ted Kremenek | 93f5e6a | 2010-10-20 21:22:15 +0000 | [diff] [blame] | 2036 | clang_disposeTokens(TU, tokens, num_tokens); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2037 | |
| 2038 | teardown: |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2039 | PrintDiagnostics(TU); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2040 | clang_disposeTranslationUnit(TU); |
| 2041 | clang_disposeIndex(CIdx); |
| 2042 | free(filename); |
| 2043 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 2044 | return errorCode; |
| 2045 | } |
| 2046 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 2047 | /******************************************************************************/ |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 2048 | /* USR printing. */ |
| 2049 | /******************************************************************************/ |
| 2050 | |
| 2051 | static int insufficient_usr(const char *kind, const char *usage) { |
| 2052 | fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage); |
| 2053 | return 1; |
| 2054 | } |
| 2055 | |
| 2056 | static unsigned isUSR(const char *s) { |
| 2057 | return s[0] == 'c' && s[1] == ':'; |
| 2058 | } |
| 2059 | |
| 2060 | static int not_usr(const char *s, const char *arg) { |
| 2061 | fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg); |
| 2062 | return 1; |
| 2063 | } |
| 2064 | |
| 2065 | static void print_usr(CXString usr) { |
| 2066 | const char *s = clang_getCString(usr); |
| 2067 | printf("%s\n", s); |
| 2068 | clang_disposeString(usr); |
| 2069 | } |
| 2070 | |
| 2071 | static void display_usrs() { |
| 2072 | fprintf(stderr, "-print-usrs options:\n" |
| 2073 | " ObjCCategory <class name> <category name>\n" |
| 2074 | " ObjCClass <class name>\n" |
| 2075 | " ObjCIvar <ivar name> <class USR>\n" |
| 2076 | " ObjCMethod <selector> [0=class method|1=instance method] " |
| 2077 | "<class USR>\n" |
| 2078 | " ObjCProperty <property name> <class USR>\n" |
| 2079 | " ObjCProtocol <protocol name>\n"); |
| 2080 | } |
| 2081 | |
| 2082 | int print_usrs(const char **I, const char **E) { |
| 2083 | while (I != E) { |
| 2084 | const char *kind = *I; |
| 2085 | unsigned len = strlen(kind); |
| 2086 | switch (len) { |
| 2087 | case 8: |
| 2088 | if (memcmp(kind, "ObjCIvar", 8) == 0) { |
| 2089 | if (I + 2 >= E) |
| 2090 | return insufficient_usr(kind, "<ivar name> <class USR>"); |
| 2091 | if (!isUSR(I[2])) |
| 2092 | return not_usr("<class USR>", I[2]); |
| 2093 | else { |
| 2094 | CXString x; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2095 | x.data = (void*) I[2]; |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 2096 | x.private_flags = 0; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 2097 | print_usr(clang_constructUSR_ObjCIvar(I[1], x)); |
| 2098 | } |
| 2099 | |
| 2100 | I += 3; |
| 2101 | continue; |
| 2102 | } |
| 2103 | break; |
| 2104 | case 9: |
| 2105 | if (memcmp(kind, "ObjCClass", 9) == 0) { |
| 2106 | if (I + 1 >= E) |
| 2107 | return insufficient_usr(kind, "<class name>"); |
| 2108 | print_usr(clang_constructUSR_ObjCClass(I[1])); |
| 2109 | I += 2; |
| 2110 | continue; |
| 2111 | } |
| 2112 | break; |
| 2113 | case 10: |
| 2114 | if (memcmp(kind, "ObjCMethod", 10) == 0) { |
| 2115 | if (I + 3 >= E) |
| 2116 | return insufficient_usr(kind, "<method selector> " |
| 2117 | "[0=class method|1=instance method] <class USR>"); |
| 2118 | if (!isUSR(I[3])) |
| 2119 | return not_usr("<class USR>", I[3]); |
| 2120 | else { |
| 2121 | CXString x; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2122 | x.data = (void*) I[3]; |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 2123 | x.private_flags = 0; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 2124 | print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x)); |
| 2125 | } |
| 2126 | I += 4; |
| 2127 | continue; |
| 2128 | } |
| 2129 | break; |
| 2130 | case 12: |
| 2131 | if (memcmp(kind, "ObjCCategory", 12) == 0) { |
| 2132 | if (I + 2 >= E) |
| 2133 | return insufficient_usr(kind, "<class name> <category name>"); |
| 2134 | print_usr(clang_constructUSR_ObjCCategory(I[1], I[2])); |
| 2135 | I += 3; |
| 2136 | continue; |
| 2137 | } |
| 2138 | if (memcmp(kind, "ObjCProtocol", 12) == 0) { |
| 2139 | if (I + 1 >= E) |
| 2140 | return insufficient_usr(kind, "<protocol name>"); |
| 2141 | print_usr(clang_constructUSR_ObjCProtocol(I[1])); |
| 2142 | I += 2; |
| 2143 | continue; |
| 2144 | } |
| 2145 | if (memcmp(kind, "ObjCProperty", 12) == 0) { |
| 2146 | if (I + 2 >= E) |
| 2147 | return insufficient_usr(kind, "<property name> <class USR>"); |
| 2148 | if (!isUSR(I[2])) |
| 2149 | return not_usr("<class USR>", I[2]); |
| 2150 | else { |
| 2151 | CXString x; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 2152 | x.data = (void*) I[2]; |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 2153 | x.private_flags = 0; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 2154 | print_usr(clang_constructUSR_ObjCProperty(I[1], x)); |
| 2155 | } |
| 2156 | I += 3; |
| 2157 | continue; |
| 2158 | } |
| 2159 | break; |
| 2160 | default: |
| 2161 | break; |
| 2162 | } |
| 2163 | break; |
| 2164 | } |
| 2165 | |
| 2166 | if (I != E) { |
| 2167 | fprintf(stderr, "Invalid USR kind: %s\n", *I); |
| 2168 | display_usrs(); |
| 2169 | return 1; |
| 2170 | } |
| 2171 | return 0; |
| 2172 | } |
| 2173 | |
| 2174 | int print_usrs_file(const char *file_name) { |
| 2175 | char line[2048]; |
| 2176 | const char *args[128]; |
| 2177 | unsigned numChars = 0; |
| 2178 | |
| 2179 | FILE *fp = fopen(file_name, "r"); |
| 2180 | if (!fp) { |
| 2181 | fprintf(stderr, "error: cannot open '%s'\n", file_name); |
| 2182 | return 1; |
| 2183 | } |
| 2184 | |
| 2185 | /* This code is not really all that safe, but it works fine for testing. */ |
| 2186 | while (!feof(fp)) { |
| 2187 | char c = fgetc(fp); |
| 2188 | if (c == '\n') { |
| 2189 | unsigned i = 0; |
| 2190 | const char *s = 0; |
| 2191 | |
| 2192 | if (numChars == 0) |
| 2193 | continue; |
| 2194 | |
| 2195 | line[numChars] = '\0'; |
| 2196 | numChars = 0; |
| 2197 | |
| 2198 | if (line[0] == '/' && line[1] == '/') |
| 2199 | continue; |
| 2200 | |
| 2201 | s = strtok(line, " "); |
| 2202 | while (s) { |
| 2203 | args[i] = s; |
| 2204 | ++i; |
| 2205 | s = strtok(0, " "); |
| 2206 | } |
| 2207 | if (print_usrs(&args[0], &args[i])) |
| 2208 | return 1; |
| 2209 | } |
| 2210 | else |
| 2211 | line[numChars++] = c; |
| 2212 | } |
| 2213 | |
| 2214 | fclose(fp); |
| 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 2219 | /* Command line processing. */ |
| 2220 | /******************************************************************************/ |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2221 | int write_pch_file(const char *filename, int argc, const char *argv[]) { |
| 2222 | CXIndex Idx; |
| 2223 | CXTranslationUnit TU; |
| 2224 | struct CXUnsavedFile *unsaved_files = 0; |
| 2225 | int num_unsaved_files = 0; |
Francois Pichet | 08aa622 | 2011-07-06 22:09:44 +0000 | [diff] [blame] | 2226 | int result = 0; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2227 | |
| 2228 | Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1); |
| 2229 | |
| 2230 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 2231 | clang_disposeIndex(Idx); |
| 2232 | return -1; |
| 2233 | } |
| 2234 | |
| 2235 | TU = clang_parseTranslationUnit(Idx, 0, |
| 2236 | argv + num_unsaved_files, |
| 2237 | argc - num_unsaved_files, |
| 2238 | unsaved_files, |
| 2239 | num_unsaved_files, |
| 2240 | CXTranslationUnit_Incomplete); |
| 2241 | if (!TU) { |
| 2242 | fprintf(stderr, "Unable to load translation unit!\n"); |
| 2243 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 2244 | clang_disposeIndex(Idx); |
| 2245 | return 1; |
| 2246 | } |
| 2247 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2248 | switch (clang_saveTranslationUnit(TU, filename, |
| 2249 | clang_defaultSaveOptions(TU))) { |
| 2250 | case CXSaveError_None: |
| 2251 | break; |
| 2252 | |
| 2253 | case CXSaveError_TranslationErrors: |
| 2254 | fprintf(stderr, "Unable to write PCH file %s: translation errors\n", |
| 2255 | filename); |
| 2256 | result = 2; |
| 2257 | break; |
| 2258 | |
| 2259 | case CXSaveError_InvalidTU: |
| 2260 | fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n", |
| 2261 | filename); |
| 2262 | result = 3; |
| 2263 | break; |
| 2264 | |
| 2265 | case CXSaveError_Unknown: |
| 2266 | default: |
| 2267 | fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename); |
| 2268 | result = 1; |
| 2269 | break; |
| 2270 | } |
| 2271 | |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2272 | clang_disposeTranslationUnit(TU); |
| 2273 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 2274 | clang_disposeIndex(Idx); |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2275 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | /******************************************************************************/ |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2279 | /* Serialized diagnostics. */ |
| 2280 | /******************************************************************************/ |
| 2281 | |
| 2282 | static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) { |
| 2283 | switch (error) { |
| 2284 | case CXLoadDiag_CannotLoad: return "Cannot Load File"; |
| 2285 | case CXLoadDiag_None: break; |
| 2286 | case CXLoadDiag_Unknown: return "Unknown"; |
| 2287 | case CXLoadDiag_InvalidFile: return "Invalid File"; |
| 2288 | } |
| 2289 | return "None"; |
| 2290 | } |
| 2291 | |
| 2292 | static const char *getSeverityString(enum CXDiagnosticSeverity severity) { |
| 2293 | switch (severity) { |
| 2294 | case CXDiagnostic_Note: return "note"; |
| 2295 | case CXDiagnostic_Error: return "error"; |
| 2296 | case CXDiagnostic_Fatal: return "fatal"; |
| 2297 | case CXDiagnostic_Ignored: return "ignored"; |
| 2298 | case CXDiagnostic_Warning: return "warning"; |
| 2299 | } |
| 2300 | return "unknown"; |
| 2301 | } |
| 2302 | |
| 2303 | static void printIndent(unsigned indent) { |
Ted Kremenek | a7e8a83 | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 2304 | if (indent == 0) |
| 2305 | return; |
| 2306 | fprintf(stderr, "+"); |
| 2307 | --indent; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2308 | while (indent > 0) { |
Ted Kremenek | a7e8a83 | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 2309 | fprintf(stderr, "-"); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2310 | --indent; |
| 2311 | } |
| 2312 | } |
| 2313 | |
| 2314 | static void printLocation(CXSourceLocation L) { |
| 2315 | CXFile File; |
| 2316 | CXString FileName; |
| 2317 | unsigned line, column, offset; |
| 2318 | |
| 2319 | clang_getExpansionLocation(L, &File, &line, &column, &offset); |
| 2320 | FileName = clang_getFileName(File); |
| 2321 | |
| 2322 | fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column); |
| 2323 | clang_disposeString(FileName); |
| 2324 | } |
| 2325 | |
| 2326 | static void printRanges(CXDiagnostic D, unsigned indent) { |
| 2327 | unsigned i, n = clang_getDiagnosticNumRanges(D); |
| 2328 | |
| 2329 | for (i = 0; i < n; ++i) { |
| 2330 | CXSourceLocation Start, End; |
| 2331 | CXSourceRange SR = clang_getDiagnosticRange(D, i); |
| 2332 | Start = clang_getRangeStart(SR); |
| 2333 | End = clang_getRangeEnd(SR); |
| 2334 | |
| 2335 | printIndent(indent); |
| 2336 | fprintf(stderr, "Range: "); |
| 2337 | printLocation(Start); |
| 2338 | fprintf(stderr, " "); |
| 2339 | printLocation(End); |
| 2340 | fprintf(stderr, "\n"); |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | static void printFixIts(CXDiagnostic D, unsigned indent) { |
| 2345 | unsigned i, n = clang_getDiagnosticNumFixIts(D); |
| 2346 | for (i = 0 ; i < n; ++i) { |
| 2347 | CXSourceRange ReplacementRange; |
| 2348 | CXString text; |
| 2349 | text = clang_getDiagnosticFixIt(D, i, &ReplacementRange); |
| 2350 | |
| 2351 | printIndent(indent); |
| 2352 | fprintf(stderr, "FIXIT: ("); |
| 2353 | printLocation(clang_getRangeStart(ReplacementRange)); |
| 2354 | fprintf(stderr, " - "); |
| 2355 | printLocation(clang_getRangeEnd(ReplacementRange)); |
| 2356 | fprintf(stderr, "): \"%s\"\n", clang_getCString(text)); |
| 2357 | clang_disposeString(text); |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) { |
NAKAMURA Takumi | 9190943 | 2011-11-10 09:30:15 +0000 | [diff] [blame] | 2362 | unsigned i, n; |
| 2363 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2364 | if (!Diags) |
| 2365 | return; |
| 2366 | |
NAKAMURA Takumi | 9190943 | 2011-11-10 09:30:15 +0000 | [diff] [blame] | 2367 | n = clang_getNumDiagnosticsInSet(Diags); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2368 | for (i = 0; i < n; ++i) { |
| 2369 | CXSourceLocation DiagLoc; |
| 2370 | CXDiagnostic D; |
| 2371 | CXFile File; |
| 2372 | CXString FileName, DiagSpelling, DiagOption; |
| 2373 | unsigned line, column, offset; |
| 2374 | const char *DiagOptionStr = 0; |
| 2375 | |
| 2376 | D = clang_getDiagnosticInSet(Diags, i); |
| 2377 | DiagLoc = clang_getDiagnosticLocation(D); |
| 2378 | clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset); |
| 2379 | FileName = clang_getFileName(File); |
| 2380 | DiagSpelling = clang_getDiagnosticSpelling(D); |
| 2381 | |
| 2382 | printIndent(indent); |
| 2383 | |
| 2384 | fprintf(stderr, "%s:%d:%d: %s: %s", |
| 2385 | clang_getCString(FileName), |
| 2386 | line, |
| 2387 | column, |
| 2388 | getSeverityString(clang_getDiagnosticSeverity(D)), |
| 2389 | clang_getCString(DiagSpelling)); |
| 2390 | |
| 2391 | DiagOption = clang_getDiagnosticOption(D, 0); |
| 2392 | DiagOptionStr = clang_getCString(DiagOption); |
| 2393 | if (DiagOptionStr) { |
| 2394 | fprintf(stderr, " [%s]", DiagOptionStr); |
| 2395 | } |
| 2396 | |
| 2397 | fprintf(stderr, "\n"); |
| 2398 | |
| 2399 | printRanges(D, indent); |
| 2400 | printFixIts(D, indent); |
| 2401 | |
NAKAMURA Takumi | a4ca95a | 2011-11-10 10:07:57 +0000 | [diff] [blame] | 2402 | /* Print subdiagnostics. */ |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2403 | printDiagnosticSet(clang_getChildDiagnostics(D), indent+2); |
| 2404 | |
| 2405 | clang_disposeString(FileName); |
| 2406 | clang_disposeString(DiagSpelling); |
| 2407 | clang_disposeString(DiagOption); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | static int read_diagnostics(const char *filename) { |
| 2412 | enum CXLoadDiag_Error error; |
| 2413 | CXString errorString; |
| 2414 | CXDiagnosticSet Diags = 0; |
| 2415 | |
| 2416 | Diags = clang_loadDiagnostics(filename, &error, &errorString); |
| 2417 | if (!Diags) { |
| 2418 | fprintf(stderr, "Trouble deserializing file (%s): %s\n", |
| 2419 | getDiagnosticCodeStr(error), |
| 2420 | clang_getCString(errorString)); |
| 2421 | clang_disposeString(errorString); |
| 2422 | return 1; |
| 2423 | } |
| 2424 | |
| 2425 | printDiagnosticSet(Diags, 0); |
Ted Kremenek | a7e8a83 | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 2426 | fprintf(stderr, "Number of diagnostics: %d\n", |
| 2427 | clang_getNumDiagnosticsInSet(Diags)); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2428 | clang_disposeDiagnosticSet(Diags); |
| 2429 | return 0; |
| 2430 | } |
| 2431 | |
| 2432 | /******************************************************************************/ |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2433 | /* Command line processing. */ |
| 2434 | /******************************************************************************/ |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2435 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 2436 | static CXCursorVisitor GetVisitor(const char *s) { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2437 | if (s[0] == '\0') |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 2438 | return FilteredPrintingVisitor; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2439 | if (strcmp(s, "-usrs") == 0) |
| 2440 | return USRVisitor; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 2441 | if (strncmp(s, "-memory-usage", 13) == 0) |
| 2442 | return GetVisitor(s + 13); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2443 | return NULL; |
| 2444 | } |
| 2445 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2446 | static void print_usage(void) { |
| 2447 | fprintf(stderr, |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 2448 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 2449 | " c-index-test -code-completion-timing=<site> <compiler arguments>\n" |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2450 | " c-index-test -cursor-at=<site> <compiler arguments>\n" |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2451 | " c-index-test -file-refs-at=<site> <compiler arguments>\n" |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2452 | " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n" |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 2453 | " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n" |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 2454 | " c-index-test -test-file-scan <AST file> <source file> " |
Erik Verbruggen | 26fc0f9 | 2011-10-06 11:38:08 +0000 | [diff] [blame] | 2455 | "[FileCheck prefix]\n"); |
| 2456 | fprintf(stderr, |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 2457 | " c-index-test -test-load-tu <AST file> <symbol filter> " |
| 2458 | "[FileCheck prefix]\n" |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2459 | " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " |
| 2460 | "[FileCheck prefix]\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 2461 | " c-index-test -test-load-source <symbol filter> {<args>}*\n"); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2462 | fprintf(stderr, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 2463 | " c-index-test -test-load-source-memory-usage " |
| 2464 | "<symbol filter> {<args>}*\n" |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2465 | " c-index-test -test-load-source-reparse <trials> <symbol filter> " |
| 2466 | " {<args>}*\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 2467 | " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 2468 | " c-index-test -test-load-source-usrs-memory-usage " |
| 2469 | "<symbol filter> {<args>}*\n" |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 2470 | " c-index-test -test-annotate-tokens=<range> {<args>}*\n" |
| 2471 | " c-index-test -test-inclusion-stack-source {<args>}*\n" |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 2472 | " c-index-test -test-inclusion-stack-tu <AST file>\n"); |
Chandler Carruth | 53513d2 | 2010-07-22 06:29:13 +0000 | [diff] [blame] | 2473 | fprintf(stderr, |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 2474 | " c-index-test -test-print-linkage-source {<args>}*\n" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 2475 | " c-index-test -test-print-typekind {<args>}*\n" |
| 2476 | " c-index-test -print-usr [<CursorKind> {<args>}]*\n" |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2477 | " c-index-test -print-usr-file <file>\n" |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2478 | " c-index-test -write-pch <file> <compiler arguments>\n"); |
| 2479 | fprintf(stderr, |
| 2480 | " c-index-test -read-diagnostics <file>\n\n"); |
Douglas Gregor | caf4bd3 | 2010-07-20 14:34:35 +0000 | [diff] [blame] | 2481 | fprintf(stderr, |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2482 | " <symbol filter> values:\n%s", |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 2483 | " all - load all symbols, including those from PCH\n" |
| 2484 | " local - load all symbols except those in PCH\n" |
| 2485 | " category - only load ObjC categories (non-PCH)\n" |
| 2486 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 2487 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 2488 | " function - only load functions (non-PCH)\n" |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 2489 | " typedef - only load typdefs (non-PCH)\n" |
| 2490 | " scan-function - scan function bodies (non-PCH)\n\n"); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 2493 | /***/ |
| 2494 | |
| 2495 | int cindextest_main(int argc, const char **argv) { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 2496 | clang_enableStackTraces(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 2497 | if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0) |
| 2498 | return read_diagnostics(argv[2]); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2499 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 2500 | return perform_code_completion(argc, argv, 0); |
| 2501 | if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1]) |
| 2502 | return perform_code_completion(argc, argv, 1); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2503 | if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) |
| 2504 | return inspect_cursor_at(argc, argv); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2505 | if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1]) |
| 2506 | return find_file_refs_at(argc, argv); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2507 | if (argc > 2 && strcmp(argv[1], "-index-file") == 0) |
| 2508 | return index_file(argc - 2, argv + 2); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 2509 | if (argc > 2 && strcmp(argv[1], "-index-tu") == 0) |
| 2510 | return index_tu(argc - 2, argv + 2); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2511 | else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 2512 | CXCursorVisitor I = GetVisitor(argv[1] + 13); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2513 | if (I) |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 2514 | return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, |
| 2515 | NULL); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2516 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2517 | else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){ |
| 2518 | CXCursorVisitor I = GetVisitor(argv[1] + 25); |
| 2519 | if (I) { |
| 2520 | int trials = atoi(argv[2]); |
| 2521 | return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I, |
| 2522 | NULL); |
| 2523 | } |
| 2524 | } |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2525 | else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 2526 | CXCursorVisitor I = GetVisitor(argv[1] + 17); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 2527 | |
| 2528 | PostVisitTU postVisit = 0; |
| 2529 | if (strstr(argv[1], "-memory-usage")) |
| 2530 | postVisit = PrintMemoryUsage; |
| 2531 | |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2532 | if (I) |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 2533 | return perform_test_load_source(argc - 3, argv + 3, argv[2], I, |
| 2534 | postVisit); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 2535 | } |
| 2536 | else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 2537 | return perform_file_scan(argv[2], argv[3], |
| 2538 | argc >= 5 ? argv[4] : 0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2539 | else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) |
| 2540 | return perform_token_annotation(argc, argv); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 2541 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) |
| 2542 | return perform_test_load_source(argc - 2, argv + 2, "all", NULL, |
| 2543 | PrintInclusionStack); |
| 2544 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) |
| 2545 | return perform_test_load_tu(argv[2], "all", NULL, NULL, |
| 2546 | PrintInclusionStack); |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 2547 | else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0) |
| 2548 | return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage, |
| 2549 | NULL); |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 2550 | else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0) |
| 2551 | return perform_test_load_source(argc - 2, argv + 2, "all", |
| 2552 | PrintTypeKind, 0); |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 2553 | else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { |
| 2554 | if (argc > 2) |
| 2555 | return print_usrs(argv + 2, argv + argc); |
| 2556 | else { |
| 2557 | display_usrs(); |
| 2558 | return 1; |
| 2559 | } |
| 2560 | } |
| 2561 | else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0) |
| 2562 | return print_usrs_file(argv[2]); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2563 | else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0) |
| 2564 | return write_pch_file(argv[2], argc - 3, argv + 3); |
| 2565 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2566 | print_usage(); |
| 2567 | return 1; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 2568 | } |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 2569 | |
| 2570 | /***/ |
| 2571 | |
| 2572 | /* We intentionally run in a separate thread to ensure we at least minimal |
| 2573 | * testing of a multithreaded environment (for example, having a reduced stack |
| 2574 | * size). */ |
| 2575 | |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 2576 | typedef struct thread_info { |
| 2577 | int argc; |
| 2578 | const char **argv; |
| 2579 | int result; |
| 2580 | } thread_info; |
Benjamin Kramer | 8429491 | 2010-11-04 19:11:31 +0000 | [diff] [blame] | 2581 | void thread_runner(void *client_data_v) { |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 2582 | thread_info *client_data = client_data_v; |
| 2583 | client_data->result = cindextest_main(client_data->argc, client_data->argv); |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | int main(int argc, const char **argv) { |
| 2587 | thread_info client_data; |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 2588 | |
Douglas Gregor | 6160598 | 2010-10-27 16:00:01 +0000 | [diff] [blame] | 2589 | if (getenv("CINDEXTEST_NOTHREADS")) |
| 2590 | return cindextest_main(argc, argv); |
| 2591 | |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 2592 | client_data.argc = argc; |
| 2593 | client_data.argv = argv; |
Daniel Dunbar | a32a6e1 | 2010-11-04 01:26:31 +0000 | [diff] [blame] | 2594 | clang_executeOnThread(thread_runner, &client_data, 0); |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 2595 | return client_data.result; |
| 2596 | } |