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