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