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