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