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