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