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