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