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