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