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