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