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