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