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