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" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5 | #include "clang-c/BuildSystem.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6 | #include "clang-c/Documentation.h" |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 7 | #include "llvm/Config/config.h" |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 8 | #include <ctype.h> |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 9 | #include <stdlib.h> |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 11 | #include <string.h> |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 12 | #include <assert.h> |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 13 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 14 | #ifdef CLANG_HAVE_LIBXML |
| 15 | #include <libxml/parser.h> |
| 16 | #include <libxml/relaxng.h> |
| 17 | #include <libxml/xmlerror.h> |
| 18 | #endif |
| 19 | |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 20 | #ifdef _WIN32 |
| 21 | # include <direct.h> |
| 22 | #else |
| 23 | # include <unistd.h> |
| 24 | #endif |
| 25 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 26 | /******************************************************************************/ |
| 27 | /* Utility functions. */ |
| 28 | /******************************************************************************/ |
| 29 | |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 30 | #ifdef _MSC_VER |
| 31 | char *basename(const char* path) |
| 32 | { |
| 33 | char* base1 = (char*)strrchr(path, '/'); |
| 34 | char* base2 = (char*)strrchr(path, '\\'); |
| 35 | if (base1 && base2) |
| 36 | return((base1 > base2) ? base1 + 1 : base2 + 1); |
| 37 | else if (base1) |
| 38 | return(base1 + 1); |
| 39 | else if (base2) |
| 40 | return(base2 + 1); |
| 41 | |
| 42 | return((char*)path); |
| 43 | } |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 44 | char *dirname(char* path) |
| 45 | { |
| 46 | char* base1 = (char*)strrchr(path, '/'); |
| 47 | char* base2 = (char*)strrchr(path, '\\'); |
| 48 | if (base1 && base2) |
| 49 | if (base1 > base2) |
| 50 | *base1 = 0; |
| 51 | else |
| 52 | *base2 = 0; |
| 53 | else if (base1) |
NAKAMURA Takumi | 0fb474a | 2012-06-30 11:47:18 +0000 | [diff] [blame] | 54 | *base1 = 0; |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 55 | else if (base2) |
NAKAMURA Takumi | 0fb474a | 2012-06-30 11:47:18 +0000 | [diff] [blame] | 56 | *base2 = 0; |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 57 | |
| 58 | return path; |
| 59 | } |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 60 | #else |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 61 | extern char *basename(const char *); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 62 | extern char *dirname(char *); |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 63 | #endif |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | 45ba9a1 | 2010-07-25 17:39:21 +0000 | [diff] [blame] | 65 | /** \brief Return the default parsing options. */ |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 66 | static unsigned getDefaultParsingOptions() { |
| 67 | unsigned options = CXTranslationUnit_DetailedPreprocessingRecord; |
| 68 | |
| 69 | if (getenv("CINDEXTEST_EDITING")) |
Douglas Gregor | b1c031b | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 70 | options |= clang_defaultEditingTranslationUnitOptions(); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 71 | if (getenv("CINDEXTEST_COMPLETION_CACHING")) |
| 72 | options |= CXTranslationUnit_CacheCompletionResults; |
Argyrios Kyrtzidis | dcaca01 | 2011-11-03 02:20:25 +0000 | [diff] [blame] | 73 | if (getenv("CINDEXTEST_COMPLETION_NO_CACHING")) |
| 74 | options &= ~CXTranslationUnit_CacheCompletionResults; |
Erik Verbruggen | 6a91d38 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 75 | if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES")) |
| 76 | options |= CXTranslationUnit_SkipFunctionBodies; |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 77 | if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS")) |
| 78 | options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 79 | |
| 80 | return options; |
| 81 | } |
| 82 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 83 | /** \brief Returns 0 in case of success, non-zero in case of a failure. */ |
Argyrios Kyrtzidis | bda536d | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 84 | static int checkForErrors(CXTranslationUnit TU); |
| 85 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 86 | static void describeLibclangFailure(enum CXErrorCode Err) { |
| 87 | switch (Err) { |
| 88 | case CXError_Success: |
| 89 | fprintf(stderr, "Success\n"); |
| 90 | return; |
| 91 | |
| 92 | case CXError_Failure: |
| 93 | fprintf(stderr, "Failure (no details available)\n"); |
| 94 | return; |
| 95 | |
| 96 | case CXError_Crashed: |
| 97 | fprintf(stderr, "Failure: libclang crashed\n"); |
| 98 | return; |
| 99 | |
| 100 | case CXError_InvalidArguments: |
| 101 | fprintf(stderr, "Failure: invalid arguments passed to a libclang routine\n"); |
| 102 | return; |
| 103 | |
| 104 | case CXError_ASTReadError: |
| 105 | fprintf(stderr, "Failure: AST deserialization error occurred\n"); |
| 106 | return; |
| 107 | } |
| 108 | } |
| 109 | |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 110 | static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column, |
| 111 | unsigned end_line, unsigned end_column) { |
| 112 | fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column, |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 113 | end_line, end_column); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 116 | static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, |
| 117 | CXTranslationUnit *TU) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 118 | enum CXErrorCode Err = clang_createTranslationUnit2(Idx, file, TU); |
| 119 | if (Err != CXError_Success) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 120 | fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 121 | describeLibclangFailure(Err); |
| 122 | *TU = 0; |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 123 | return 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 124 | } |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 128 | void free_remapped_files(struct CXUnsavedFile *unsaved_files, |
| 129 | int num_unsaved_files) { |
| 130 | int i; |
| 131 | for (i = 0; i != num_unsaved_files; ++i) { |
| 132 | free((char *)unsaved_files[i].Filename); |
| 133 | free((char *)unsaved_files[i].Contents); |
| 134 | } |
Douglas Gregor | 653a55f | 2010-08-19 20:50:29 +0000 | [diff] [blame] | 135 | free(unsaved_files); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 138 | static int parse_remapped_files_with_opt(const char *opt_name, |
| 139 | int argc, const char **argv, |
| 140 | int start_arg, |
| 141 | struct CXUnsavedFile **unsaved_files, |
| 142 | int *num_unsaved_files) { |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 143 | int i; |
| 144 | int arg; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 145 | int prefix_len = strlen(opt_name); |
| 146 | int arg_indices[20]; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 147 | *unsaved_files = 0; |
| 148 | *num_unsaved_files = 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 150 | /* Count the number of remapped files. */ |
| 151 | for (arg = start_arg; arg < argc; ++arg) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 152 | if (strncmp(argv[arg], opt_name, prefix_len)) |
| 153 | continue; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 154 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 155 | assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int))); |
| 156 | arg_indices[*num_unsaved_files] = arg; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 157 | ++*num_unsaved_files; |
| 158 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 160 | if (*num_unsaved_files == 0) |
| 161 | return 0; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 162 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 163 | *unsaved_files |
Douglas Gregor | 653a55f | 2010-08-19 20:50:29 +0000 | [diff] [blame] | 164 | = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * |
| 165 | *num_unsaved_files); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 166 | for (i = 0; i != *num_unsaved_files; ++i) { |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 167 | struct CXUnsavedFile *unsaved = *unsaved_files + i; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 168 | const char *arg_string = argv[arg_indices[i]] + prefix_len; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 169 | int filename_len; |
| 170 | char *filename; |
| 171 | char *contents; |
| 172 | FILE *to_file; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 173 | const char *sep = strchr(arg_string, ','); |
| 174 | if (!sep) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 175 | fprintf(stderr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 176 | "error: %sfrom:to argument is missing comma\n", opt_name); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 177 | free_remapped_files(*unsaved_files, i); |
| 178 | *unsaved_files = 0; |
| 179 | *num_unsaved_files = 0; |
| 180 | return -1; |
| 181 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 183 | /* Open the file that we're remapping to. */ |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 184 | to_file = fopen(sep + 1, "rb"); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 185 | if (!to_file) { |
| 186 | fprintf(stderr, "error: cannot open file %s that we are remapping to\n", |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 187 | sep + 1); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 188 | free_remapped_files(*unsaved_files, i); |
| 189 | *unsaved_files = 0; |
| 190 | *num_unsaved_files = 0; |
| 191 | return -1; |
| 192 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 193 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 194 | /* Determine the length of the file we're remapping to. */ |
| 195 | fseek(to_file, 0, SEEK_END); |
| 196 | unsaved->Length = ftell(to_file); |
| 197 | fseek(to_file, 0, SEEK_SET); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 198 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 199 | /* Read the contents of the file we're remapping to. */ |
| 200 | contents = (char *)malloc(unsaved->Length + 1); |
| 201 | if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) { |
| 202 | fprintf(stderr, "error: unexpected %s reading 'to' file %s\n", |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 203 | (feof(to_file) ? "EOF" : "error"), sep + 1); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 204 | fclose(to_file); |
| 205 | free_remapped_files(*unsaved_files, i); |
Richard Smith | e07c5f8 | 2012-07-05 08:20:49 +0000 | [diff] [blame] | 206 | free(contents); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 207 | *unsaved_files = 0; |
| 208 | *num_unsaved_files = 0; |
| 209 | return -1; |
| 210 | } |
| 211 | contents[unsaved->Length] = 0; |
| 212 | unsaved->Contents = contents; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 213 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 214 | /* Close the file. */ |
| 215 | fclose(to_file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 216 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 217 | /* Copy the file name that we're remapping from. */ |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 218 | filename_len = sep - arg_string; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 219 | filename = (char *)malloc(filename_len + 1); |
| 220 | memcpy(filename, arg_string, filename_len); |
| 221 | filename[filename_len] = 0; |
| 222 | unsaved->Filename = filename; |
| 223 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 228 | static int parse_remapped_files(int argc, const char **argv, int start_arg, |
| 229 | struct CXUnsavedFile **unsaved_files, |
| 230 | int *num_unsaved_files) { |
| 231 | return parse_remapped_files_with_opt("-remap-file=", argc, argv, start_arg, |
| 232 | unsaved_files, num_unsaved_files); |
| 233 | } |
| 234 | |
| 235 | static int parse_remapped_files_with_try(int try_idx, |
| 236 | int argc, const char **argv, |
| 237 | int start_arg, |
| 238 | struct CXUnsavedFile **unsaved_files, |
| 239 | int *num_unsaved_files) { |
| 240 | struct CXUnsavedFile *unsaved_files_no_try_idx; |
| 241 | int num_unsaved_files_no_try_idx; |
| 242 | struct CXUnsavedFile *unsaved_files_try_idx; |
| 243 | int num_unsaved_files_try_idx; |
| 244 | int ret; |
| 245 | char opt_name[32]; |
| 246 | |
| 247 | ret = parse_remapped_files(argc, argv, start_arg, |
| 248 | &unsaved_files_no_try_idx, &num_unsaved_files_no_try_idx); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | |
| 252 | sprintf(opt_name, "-remap-file-%d=", try_idx); |
| 253 | ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg, |
| 254 | &unsaved_files_try_idx, &num_unsaved_files_try_idx); |
| 255 | if (ret) |
| 256 | return ret; |
| 257 | |
| 258 | *num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx; |
| 259 | *unsaved_files |
| 260 | = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx, |
| 261 | sizeof(struct CXUnsavedFile) * |
| 262 | *num_unsaved_files); |
| 263 | memcpy(*unsaved_files + num_unsaved_files_no_try_idx, |
| 264 | unsaved_files_try_idx, sizeof(struct CXUnsavedFile) * |
| 265 | num_unsaved_files_try_idx); |
| 266 | free(unsaved_files_try_idx); |
| 267 | return 0; |
| 268 | } |
| 269 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 270 | static const char *parse_comments_schema(int argc, const char **argv) { |
| 271 | const char *CommentsSchemaArg = "-comments-xml-schema="; |
| 272 | const char *CommentSchemaFile = NULL; |
| 273 | |
| 274 | if (argc == 0) |
| 275 | return CommentSchemaFile; |
| 276 | |
| 277 | if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg))) |
| 278 | CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg); |
| 279 | |
| 280 | return CommentSchemaFile; |
| 281 | } |
| 282 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 283 | /******************************************************************************/ |
| 284 | /* Pretty-printing. */ |
| 285 | /******************************************************************************/ |
| 286 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 287 | static const char *FileCheckPrefix = "CHECK"; |
| 288 | |
| 289 | static void PrintCString(const char *CStr) { |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 290 | if (CStr != NULL && CStr[0] != '\0') { |
| 291 | for ( ; *CStr; ++CStr) { |
| 292 | const char C = *CStr; |
| 293 | switch (C) { |
| 294 | case '\n': printf("\\n"); break; |
| 295 | case '\r': printf("\\r"); break; |
| 296 | case '\t': printf("\\t"); break; |
| 297 | case '\v': printf("\\v"); break; |
| 298 | case '\f': printf("\\f"); break; |
| 299 | default: putchar(C); break; |
| 300 | } |
| 301 | } |
| 302 | } |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) { |
| 306 | printf(" %s=[", Prefix); |
| 307 | PrintCString(CStr); |
Dmitri Gribenko | 2d44d77 | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 308 | printf("]"); |
| 309 | } |
| 310 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 311 | static void PrintCXStringAndDispose(CXString Str) { |
| 312 | PrintCString(clang_getCString(Str)); |
| 313 | clang_disposeString(Str); |
| 314 | } |
| 315 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 316 | static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) { |
| 317 | PrintCStringWithPrefix(Prefix, clang_getCString(Str)); |
| 318 | } |
| 319 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 320 | static void PrintCXStringWithPrefixAndDispose(const char *Prefix, |
| 321 | CXString Str) { |
| 322 | PrintCStringWithPrefix(Prefix, clang_getCString(Str)); |
| 323 | clang_disposeString(Str); |
| 324 | } |
| 325 | |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 326 | static void PrintRange(CXSourceRange R, const char *str) { |
| 327 | CXFile begin_file, end_file; |
| 328 | unsigned begin_line, begin_column, end_line, end_column; |
| 329 | |
| 330 | clang_getSpellingLocation(clang_getRangeStart(R), |
| 331 | &begin_file, &begin_line, &begin_column, 0); |
| 332 | clang_getSpellingLocation(clang_getRangeEnd(R), |
| 333 | &end_file, &end_line, &end_column, 0); |
| 334 | if (!begin_file || !end_file) |
| 335 | return; |
| 336 | |
Argyrios Kyrtzidis | ba1da14 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 337 | if (str) |
| 338 | printf(" %s=", str); |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 339 | PrintExtent(stdout, begin_line, begin_column, end_line, end_column); |
| 340 | } |
| 341 | |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 342 | int want_display_name = 0; |
| 343 | |
Douglas Gregor | cc88966 | 2012-05-08 00:14:45 +0000 | [diff] [blame] | 344 | static void printVersion(const char *Prefix, CXVersion Version) { |
| 345 | if (Version.Major < 0) |
| 346 | return; |
| 347 | printf("%s%d", Prefix, Version.Major); |
| 348 | |
| 349 | if (Version.Minor < 0) |
| 350 | return; |
| 351 | printf(".%d", Version.Minor); |
| 352 | |
| 353 | if (Version.Subminor < 0) |
| 354 | return; |
| 355 | printf(".%d", Version.Subminor); |
| 356 | } |
| 357 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 358 | struct CommentASTDumpingContext { |
| 359 | int IndentLevel; |
| 360 | }; |
| 361 | |
| 362 | static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx, |
| 363 | CXComment Comment) { |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 364 | unsigned i; |
| 365 | unsigned e; |
| 366 | enum CXCommentKind Kind = clang_Comment_getKind(Comment); |
| 367 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 368 | Ctx->IndentLevel++; |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 369 | for (i = 0, e = Ctx->IndentLevel; i != e; ++i) |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 370 | printf(" "); |
| 371 | |
| 372 | printf("("); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 373 | switch (Kind) { |
| 374 | case CXComment_Null: |
| 375 | printf("CXComment_Null"); |
| 376 | break; |
| 377 | case CXComment_Text: |
| 378 | printf("CXComment_Text"); |
| 379 | PrintCXStringWithPrefixAndDispose("Text", |
| 380 | clang_TextComment_getText(Comment)); |
| 381 | if (clang_Comment_isWhitespace(Comment)) |
| 382 | printf(" IsWhitespace"); |
| 383 | if (clang_InlineContentComment_hasTrailingNewline(Comment)) |
| 384 | printf(" HasTrailingNewline"); |
| 385 | break; |
| 386 | case CXComment_InlineCommand: |
| 387 | printf("CXComment_InlineCommand"); |
| 388 | PrintCXStringWithPrefixAndDispose( |
| 389 | "CommandName", |
| 390 | clang_InlineCommandComment_getCommandName(Comment)); |
Dmitri Gribenko | 2d66a50 | 2012-07-23 16:43:01 +0000 | [diff] [blame] | 391 | switch (clang_InlineCommandComment_getRenderKind(Comment)) { |
| 392 | case CXCommentInlineCommandRenderKind_Normal: |
| 393 | printf(" RenderNormal"); |
| 394 | break; |
| 395 | case CXCommentInlineCommandRenderKind_Bold: |
| 396 | printf(" RenderBold"); |
| 397 | break; |
| 398 | case CXCommentInlineCommandRenderKind_Monospaced: |
| 399 | printf(" RenderMonospaced"); |
| 400 | break; |
| 401 | case CXCommentInlineCommandRenderKind_Emphasized: |
| 402 | printf(" RenderEmphasized"); |
| 403 | break; |
| 404 | } |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 405 | for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 406 | i != e; ++i) { |
| 407 | printf(" Arg[%u]=", i); |
| 408 | PrintCXStringAndDispose( |
| 409 | clang_InlineCommandComment_getArgText(Comment, i)); |
| 410 | } |
| 411 | if (clang_InlineContentComment_hasTrailingNewline(Comment)) |
| 412 | printf(" HasTrailingNewline"); |
| 413 | break; |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 414 | case CXComment_HTMLStartTag: { |
| 415 | unsigned NumAttrs; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 416 | printf("CXComment_HTMLStartTag"); |
| 417 | PrintCXStringWithPrefixAndDispose( |
| 418 | "Name", |
| 419 | clang_HTMLTagComment_getTagName(Comment)); |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 420 | NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 421 | if (NumAttrs != 0) { |
| 422 | printf(" Attrs:"); |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 423 | for (i = 0; i != NumAttrs; ++i) { |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 424 | printf(" "); |
| 425 | PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i)); |
| 426 | printf("="); |
| 427 | PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i)); |
| 428 | } |
| 429 | } |
| 430 | if (clang_HTMLStartTagComment_isSelfClosing(Comment)) |
| 431 | printf(" SelfClosing"); |
| 432 | if (clang_InlineContentComment_hasTrailingNewline(Comment)) |
| 433 | printf(" HasTrailingNewline"); |
| 434 | break; |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 435 | } |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 436 | case CXComment_HTMLEndTag: |
| 437 | printf("CXComment_HTMLEndTag"); |
| 438 | PrintCXStringWithPrefixAndDispose( |
| 439 | "Name", |
| 440 | clang_HTMLTagComment_getTagName(Comment)); |
| 441 | if (clang_InlineContentComment_hasTrailingNewline(Comment)) |
| 442 | printf(" HasTrailingNewline"); |
| 443 | break; |
| 444 | case CXComment_Paragraph: |
| 445 | printf("CXComment_Paragraph"); |
| 446 | if (clang_Comment_isWhitespace(Comment)) |
| 447 | printf(" IsWhitespace"); |
| 448 | break; |
| 449 | case CXComment_BlockCommand: |
| 450 | printf("CXComment_BlockCommand"); |
| 451 | PrintCXStringWithPrefixAndDispose( |
| 452 | "CommandName", |
| 453 | clang_BlockCommandComment_getCommandName(Comment)); |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 454 | for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 455 | i != e; ++i) { |
| 456 | printf(" Arg[%u]=", i); |
| 457 | PrintCXStringAndDispose( |
| 458 | clang_BlockCommandComment_getArgText(Comment, i)); |
| 459 | } |
| 460 | break; |
| 461 | case CXComment_ParamCommand: |
| 462 | printf("CXComment_ParamCommand"); |
| 463 | switch (clang_ParamCommandComment_getDirection(Comment)) { |
| 464 | case CXCommentParamPassDirection_In: |
| 465 | printf(" in"); |
| 466 | break; |
| 467 | case CXCommentParamPassDirection_Out: |
| 468 | printf(" out"); |
| 469 | break; |
| 470 | case CXCommentParamPassDirection_InOut: |
| 471 | printf(" in,out"); |
| 472 | break; |
| 473 | } |
| 474 | if (clang_ParamCommandComment_isDirectionExplicit(Comment)) |
| 475 | printf(" explicitly"); |
| 476 | else |
| 477 | printf(" implicitly"); |
| 478 | PrintCXStringWithPrefixAndDispose( |
| 479 | "ParamName", |
| 480 | clang_ParamCommandComment_getParamName(Comment)); |
| 481 | if (clang_ParamCommandComment_isParamIndexValid(Comment)) |
| 482 | printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment)); |
| 483 | else |
| 484 | printf(" ParamIndex=Invalid"); |
| 485 | break; |
Dmitri Gribenko | 96b0986 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 486 | case CXComment_TParamCommand: |
| 487 | printf("CXComment_TParamCommand"); |
| 488 | PrintCXStringWithPrefixAndDispose( |
| 489 | "ParamName", |
| 490 | clang_TParamCommandComment_getParamName(Comment)); |
| 491 | if (clang_TParamCommandComment_isParamPositionValid(Comment)) { |
| 492 | printf(" ParamPosition={"); |
| 493 | for (i = 0, e = clang_TParamCommandComment_getDepth(Comment); |
| 494 | i != e; ++i) { |
| 495 | printf("%u", clang_TParamCommandComment_getIndex(Comment, i)); |
| 496 | if (i != e - 1) |
| 497 | printf(", "); |
| 498 | } |
| 499 | printf("}"); |
| 500 | } else |
| 501 | printf(" ParamPosition=Invalid"); |
| 502 | break; |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 503 | case CXComment_VerbatimBlockCommand: |
| 504 | printf("CXComment_VerbatimBlockCommand"); |
| 505 | PrintCXStringWithPrefixAndDispose( |
| 506 | "CommandName", |
| 507 | clang_BlockCommandComment_getCommandName(Comment)); |
| 508 | break; |
| 509 | case CXComment_VerbatimBlockLine: |
| 510 | printf("CXComment_VerbatimBlockLine"); |
| 511 | PrintCXStringWithPrefixAndDispose( |
| 512 | "Text", |
| 513 | clang_VerbatimBlockLineComment_getText(Comment)); |
| 514 | break; |
| 515 | case CXComment_VerbatimLine: |
| 516 | printf("CXComment_VerbatimLine"); |
| 517 | PrintCXStringWithPrefixAndDispose( |
| 518 | "Text", |
| 519 | clang_VerbatimLineComment_getText(Comment)); |
| 520 | break; |
| 521 | case CXComment_FullComment: |
| 522 | printf("CXComment_FullComment"); |
| 523 | break; |
| 524 | } |
| 525 | if (Kind != CXComment_Null) { |
| 526 | const unsigned NumChildren = clang_Comment_getNumChildren(Comment); |
Dmitri Gribenko | 5ef6ea5 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 527 | unsigned i; |
| 528 | for (i = 0; i != NumChildren; ++i) { |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 529 | printf("\n// %s: ", FileCheckPrefix); |
| 530 | DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i)); |
| 531 | } |
| 532 | } |
| 533 | printf(")"); |
| 534 | Ctx->IndentLevel--; |
| 535 | } |
| 536 | |
| 537 | static void DumpCXComment(CXComment Comment) { |
| 538 | struct CommentASTDumpingContext Ctx; |
| 539 | Ctx.IndentLevel = 1; |
| 540 | printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix); |
| 541 | DumpCXCommentInternal(&Ctx, Comment); |
| 542 | printf("]"); |
| 543 | } |
| 544 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 545 | static void ValidateCommentXML(const char *Str, const char *CommentSchemaFile) { |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 546 | #ifdef CLANG_HAVE_LIBXML |
| 547 | xmlRelaxNGParserCtxtPtr RNGParser; |
| 548 | xmlRelaxNGPtr Schema; |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 549 | xmlDocPtr Doc; |
| 550 | xmlRelaxNGValidCtxtPtr ValidationCtxt; |
| 551 | int status; |
| 552 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 553 | if (!CommentSchemaFile) |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 554 | return; |
| 555 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 556 | RNGParser = xmlRelaxNGNewParserCtxt(CommentSchemaFile); |
| 557 | if (!RNGParser) { |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 558 | printf(" libXMLError"); |
| 559 | return; |
| 560 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 561 | Schema = xmlRelaxNGParse(RNGParser); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 562 | |
| 563 | Doc = xmlParseDoc((const xmlChar *) Str); |
| 564 | |
| 565 | if (!Doc) { |
| 566 | xmlErrorPtr Error = xmlGetLastError(); |
| 567 | printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message); |
| 568 | return; |
| 569 | } |
| 570 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 571 | ValidationCtxt = xmlRelaxNGNewValidCtxt(Schema); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 572 | status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc); |
| 573 | if (!status) |
| 574 | printf(" CommentXMLValid"); |
| 575 | else if (status > 0) { |
| 576 | xmlErrorPtr Error = xmlGetLastError(); |
| 577 | printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message); |
| 578 | } else |
| 579 | printf(" libXMLError"); |
| 580 | |
| 581 | xmlRelaxNGFreeValidCtxt(ValidationCtxt); |
| 582 | xmlFreeDoc(Doc); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 583 | xmlRelaxNGFree(Schema); |
| 584 | xmlRelaxNGFreeParserCtxt(RNGParser); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 585 | #endif |
| 586 | } |
| 587 | |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 588 | static void PrintCursorComments(CXCursor Cursor, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 589 | const char *CommentSchemaFile) { |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 590 | { |
| 591 | CXString RawComment; |
| 592 | const char *RawCommentCString; |
| 593 | CXString BriefComment; |
| 594 | const char *BriefCommentCString; |
| 595 | |
| 596 | RawComment = clang_Cursor_getRawCommentText(Cursor); |
| 597 | RawCommentCString = clang_getCString(RawComment); |
| 598 | if (RawCommentCString != NULL && RawCommentCString[0] != '\0') { |
| 599 | PrintCStringWithPrefix("RawComment", RawCommentCString); |
| 600 | PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange"); |
| 601 | |
| 602 | BriefComment = clang_Cursor_getBriefCommentText(Cursor); |
| 603 | BriefCommentCString = clang_getCString(BriefComment); |
| 604 | if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0') |
| 605 | PrintCStringWithPrefix("BriefComment", BriefCommentCString); |
| 606 | clang_disposeString(BriefComment); |
| 607 | } |
| 608 | clang_disposeString(RawComment); |
| 609 | } |
| 610 | |
| 611 | { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 612 | CXComment Comment = clang_Cursor_getParsedComment(Cursor); |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 613 | if (clang_Comment_getKind(Comment) != CXComment_Null) { |
| 614 | PrintCXStringWithPrefixAndDispose("FullCommentAsHTML", |
| 615 | clang_FullComment_getAsHTML(Comment)); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 616 | { |
| 617 | CXString XML; |
Dmitri Gribenko | e4330a3 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 618 | XML = clang_FullComment_getAsXML(Comment); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 619 | PrintCXStringWithPrefix("FullCommentAsXML", XML); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 620 | ValidateCommentXML(clang_getCString(XML), CommentSchemaFile); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 621 | clang_disposeString(XML); |
| 622 | } |
| 623 | |
Dmitri Gribenko | ae99b75 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 624 | DumpCXComment(Comment); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
Argyrios Kyrtzidis | b3dd988 | 2012-08-22 23:15:52 +0000 | [diff] [blame] | 629 | typedef struct { |
| 630 | unsigned line; |
| 631 | unsigned col; |
| 632 | } LineCol; |
| 633 | |
| 634 | static int lineCol_cmp(const void *p1, const void *p2) { |
| 635 | const LineCol *lhs = p1; |
| 636 | const LineCol *rhs = p2; |
| 637 | if (lhs->line != rhs->line) |
| 638 | return (int)lhs->line - (int)rhs->line; |
| 639 | return (int)lhs->col - (int)rhs->col; |
| 640 | } |
| 641 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 642 | static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 643 | CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 644 | if (clang_isInvalid(Cursor.kind)) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 645 | CXString ks = clang_getCursorKindSpelling(Cursor.kind); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 646 | printf("Invalid Cursor => %s", clang_getCString(ks)); |
| 647 | clang_disposeString(ks); |
| 648 | } |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 649 | else { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 650 | CXString string, ks; |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 651 | CXCursor Referenced; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 652 | unsigned line, column; |
Douglas Gregor | e0329ac | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 653 | CXCursor SpecializationOf; |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 654 | CXCursor *overridden; |
| 655 | unsigned num_overridden; |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 656 | unsigned RefNameRangeNr; |
| 657 | CXSourceRange CursorExtent; |
| 658 | CXSourceRange RefNameRange; |
Douglas Gregor | cc88966 | 2012-05-08 00:14:45 +0000 | [diff] [blame] | 659 | int AlwaysUnavailable; |
| 660 | int AlwaysDeprecated; |
| 661 | CXString UnavailableMessage; |
| 662 | CXString DeprecatedMessage; |
| 663 | CXPlatformAvailability PlatformAvailability[2]; |
| 664 | int NumPlatformAvailability; |
| 665 | int I; |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 666 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 667 | ks = clang_getCursorKindSpelling(Cursor.kind); |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 668 | string = want_display_name? clang_getCursorDisplayName(Cursor) |
| 669 | : clang_getCursorSpelling(Cursor); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 670 | printf("%s=%s", clang_getCString(ks), |
| 671 | clang_getCString(string)); |
| 672 | clang_disposeString(ks); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 673 | clang_disposeString(string); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 675 | Referenced = clang_getCursorReferenced(Cursor); |
| 676 | if (!clang_equalCursors(Referenced, clang_getNullCursor())) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 677 | if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) { |
| 678 | unsigned I, N = clang_getNumOverloadedDecls(Referenced); |
| 679 | printf("["); |
| 680 | for (I = 0; I != N; ++I) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 681 | CXCursor Ovl = clang_getOverloadedDecl(Referenced, I); |
Douglas Gregor | 1f6206e | 2010-09-14 00:20:32 +0000 | [diff] [blame] | 682 | CXSourceLocation Loc; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 683 | if (I) |
| 684 | printf(", "); |
| 685 | |
Douglas Gregor | 1f6206e | 2010-09-14 00:20:32 +0000 | [diff] [blame] | 686 | Loc = clang_getCursorLocation(Ovl); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 687 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 688 | printf("%d:%d", line, column); |
| 689 | } |
| 690 | printf("]"); |
| 691 | } else { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 692 | CXSourceLocation Loc = clang_getCursorLocation(Referenced); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 693 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 694 | printf(":%d:%d", line, column); |
| 695 | } |
Douglas Gregor | c5d1e93 | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 696 | } |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 697 | |
| 698 | if (clang_isCursorDefinition(Cursor)) |
| 699 | printf(" (Definition)"); |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 700 | |
| 701 | switch (clang_getCursorAvailability(Cursor)) { |
| 702 | case CXAvailability_Available: |
| 703 | break; |
| 704 | |
| 705 | case CXAvailability_Deprecated: |
| 706 | printf(" (deprecated)"); |
| 707 | break; |
| 708 | |
| 709 | case CXAvailability_NotAvailable: |
| 710 | printf(" (unavailable)"); |
| 711 | break; |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 712 | |
| 713 | case CXAvailability_NotAccessible: |
| 714 | printf(" (inaccessible)"); |
| 715 | break; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 716 | } |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 717 | |
Douglas Gregor | cc88966 | 2012-05-08 00:14:45 +0000 | [diff] [blame] | 718 | NumPlatformAvailability |
| 719 | = clang_getCursorPlatformAvailability(Cursor, |
| 720 | &AlwaysDeprecated, |
| 721 | &DeprecatedMessage, |
| 722 | &AlwaysUnavailable, |
| 723 | &UnavailableMessage, |
| 724 | PlatformAvailability, 2); |
| 725 | if (AlwaysUnavailable) { |
| 726 | printf(" (always unavailable: \"%s\")", |
| 727 | clang_getCString(UnavailableMessage)); |
| 728 | } else if (AlwaysDeprecated) { |
| 729 | printf(" (always deprecated: \"%s\")", |
| 730 | clang_getCString(DeprecatedMessage)); |
| 731 | } else { |
| 732 | for (I = 0; I != NumPlatformAvailability; ++I) { |
| 733 | if (I >= 2) |
| 734 | break; |
| 735 | |
| 736 | printf(" (%s", clang_getCString(PlatformAvailability[I].Platform)); |
| 737 | if (PlatformAvailability[I].Unavailable) |
| 738 | printf(", unavailable"); |
| 739 | else { |
| 740 | printVersion(", introduced=", PlatformAvailability[I].Introduced); |
| 741 | printVersion(", deprecated=", PlatformAvailability[I].Deprecated); |
| 742 | printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted); |
| 743 | } |
| 744 | if (clang_getCString(PlatformAvailability[I].Message)[0]) |
| 745 | printf(", message=\"%s\"", |
| 746 | clang_getCString(PlatformAvailability[I].Message)); |
| 747 | printf(")"); |
| 748 | } |
| 749 | } |
| 750 | for (I = 0; I != NumPlatformAvailability; ++I) { |
| 751 | if (I >= 2) |
| 752 | break; |
| 753 | clang_disposeCXPlatformAvailability(PlatformAvailability + I); |
| 754 | } |
| 755 | |
| 756 | clang_disposeString(DeprecatedMessage); |
| 757 | clang_disposeString(UnavailableMessage); |
| 758 | |
Douglas Gregor | b83d4d7 | 2011-05-13 15:54:42 +0000 | [diff] [blame] | 759 | if (clang_CXXMethod_isStatic(Cursor)) |
| 760 | printf(" (static)"); |
| 761 | if (clang_CXXMethod_isVirtual(Cursor)) |
| 762 | printf(" (virtual)"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 763 | if (clang_CXXMethod_isConst(Cursor)) |
| 764 | printf(" (const)"); |
Dmitri Gribenko | c965f76 | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 765 | if (clang_CXXMethod_isPureVirtual(Cursor)) |
| 766 | printf(" (pure)"); |
Argyrios Kyrtzidis | 80e1aca | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 767 | if (clang_Cursor_isVariadic(Cursor)) |
| 768 | printf(" (variadic)"); |
Argyrios Kyrtzidis | 514afc7 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 769 | if (clang_Cursor_isObjCOptional(Cursor)) |
| 770 | printf(" (@optional)"); |
| 771 | |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 772 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 773 | CXType T = |
| 774 | clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor)); |
| 775 | CXString S = clang_getTypeKindSpelling(T.kind); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 776 | printf(" [IBOutletCollection=%s]", clang_getCString(S)); |
| 777 | clang_disposeString(S); |
| 778 | } |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 779 | |
| 780 | if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| 781 | enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor); |
| 782 | unsigned isVirtual = clang_isVirtualBase(Cursor); |
| 783 | const char *accessStr = 0; |
| 784 | |
| 785 | switch (access) { |
| 786 | case CX_CXXInvalidAccessSpecifier: |
| 787 | accessStr = "invalid"; break; |
| 788 | case CX_CXXPublic: |
| 789 | accessStr = "public"; break; |
| 790 | case CX_CXXProtected: |
| 791 | accessStr = "protected"; break; |
| 792 | case CX_CXXPrivate: |
| 793 | accessStr = "private"; break; |
| 794 | } |
| 795 | |
| 796 | printf(" [access=%s isVirtual=%s]", accessStr, |
| 797 | isVirtual ? "true" : "false"); |
| 798 | } |
Douglas Gregor | e0329ac | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 799 | |
| 800 | SpecializationOf = clang_getSpecializedCursorTemplate(Cursor); |
| 801 | if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 802 | CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf); |
| 803 | CXString Name = clang_getCursorSpelling(SpecializationOf); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 804 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | e0329ac | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 805 | printf(" [Specialization of %s:%d:%d]", |
| 806 | clang_getCString(Name), line, column); |
| 807 | clang_disposeString(Name); |
| 808 | } |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 809 | |
| 810 | clang_getOverriddenCursors(Cursor, &overridden, &num_overridden); |
| 811 | if (num_overridden) { |
| 812 | unsigned I; |
Argyrios Kyrtzidis | b3dd988 | 2012-08-22 23:15:52 +0000 | [diff] [blame] | 813 | LineCol lineCols[50]; |
| 814 | assert(num_overridden <= 50); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 815 | printf(" [Overrides "); |
| 816 | for (I = 0; I != num_overridden; ++I) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 817 | CXSourceLocation Loc = clang_getCursorLocation(overridden[I]); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 818 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Argyrios Kyrtzidis | b3dd988 | 2012-08-22 23:15:52 +0000 | [diff] [blame] | 819 | lineCols[I].line = line; |
| 820 | lineCols[I].col = column; |
| 821 | } |
Michael Liao | 6422149 | 2012-08-30 00:45:32 +0000 | [diff] [blame] | 822 | /* Make the order of the override list deterministic. */ |
Argyrios Kyrtzidis | b3dd988 | 2012-08-22 23:15:52 +0000 | [diff] [blame] | 823 | qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp); |
| 824 | for (I = 0; I != num_overridden; ++I) { |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 825 | if (I) |
| 826 | printf(", "); |
Argyrios Kyrtzidis | b3dd988 | 2012-08-22 23:15:52 +0000 | [diff] [blame] | 827 | printf("@%d:%d", lineCols[I].line, lineCols[I].col); |
Douglas Gregor | 9f59234 | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 828 | } |
| 829 | printf("]"); |
| 830 | clang_disposeOverriddenCursors(overridden); |
| 831 | } |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 832 | |
| 833 | if (Cursor.kind == CXCursor_InclusionDirective) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 834 | CXFile File = clang_getIncludedFile(Cursor); |
| 835 | CXString Included = clang_getFileName(File); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 836 | printf(" (%s)", clang_getCString(Included)); |
| 837 | clang_disposeString(Included); |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 838 | |
| 839 | if (clang_isFileMultipleIncludeGuarded(TU, File)) |
| 840 | printf(" [multi-include guarded]"); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 841 | } |
Douglas Gregor | 430d7a1 | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 842 | |
| 843 | CursorExtent = clang_getCursorExtent(Cursor); |
| 844 | RefNameRange = clang_getCursorReferenceNameRange(Cursor, |
| 845 | CXNameRange_WantQualifier |
| 846 | | CXNameRange_WantSinglePiece |
| 847 | | CXNameRange_WantTemplateArgs, |
| 848 | 0); |
| 849 | if (!clang_equalRanges(CursorExtent, RefNameRange)) |
| 850 | PrintRange(RefNameRange, "SingleRefName"); |
| 851 | |
| 852 | for (RefNameRangeNr = 0; 1; RefNameRangeNr++) { |
| 853 | RefNameRange = clang_getCursorReferenceNameRange(Cursor, |
| 854 | CXNameRange_WantQualifier |
| 855 | | CXNameRange_WantTemplateArgs, |
| 856 | RefNameRangeNr); |
| 857 | if (clang_equalRanges(clang_getNullRange(), RefNameRange)) |
| 858 | break; |
| 859 | if (!clang_equalRanges(CursorExtent, RefNameRange)) |
| 860 | PrintRange(RefNameRange, "RefName"); |
| 861 | } |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 862 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 863 | PrintCursorComments(Cursor, CommentSchemaFile); |
Argyrios Kyrtzidis | 9ee6a66 | 2013-04-18 22:15:49 +0000 | [diff] [blame] | 864 | |
| 865 | { |
| 866 | unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0); |
| 867 | if (PropAttrs != CXObjCPropertyAttr_noattr) { |
| 868 | printf(" ["); |
| 869 | #define PRINT_PROP_ATTR(A) \ |
| 870 | if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",") |
| 871 | PRINT_PROP_ATTR(readonly); |
| 872 | PRINT_PROP_ATTR(getter); |
| 873 | PRINT_PROP_ATTR(assign); |
| 874 | PRINT_PROP_ATTR(readwrite); |
| 875 | PRINT_PROP_ATTR(retain); |
| 876 | PRINT_PROP_ATTR(copy); |
| 877 | PRINT_PROP_ATTR(nonatomic); |
| 878 | PRINT_PROP_ATTR(setter); |
| 879 | PRINT_PROP_ATTR(atomic); |
| 880 | PRINT_PROP_ATTR(weak); |
| 881 | PRINT_PROP_ATTR(strong); |
| 882 | PRINT_PROP_ATTR(unsafe_unretained); |
| 883 | printf("]"); |
| 884 | } |
| 885 | } |
Argyrios Kyrtzidis | 38dbad2 | 2013-04-18 23:29:12 +0000 | [diff] [blame] | 886 | |
| 887 | { |
| 888 | unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor); |
| 889 | if (QT != CXObjCDeclQualifier_None) { |
| 890 | printf(" ["); |
| 891 | #define PRINT_OBJC_QUAL(A) \ |
| 892 | if (QT & CXObjCDeclQualifier_##A) printf(#A ",") |
| 893 | PRINT_OBJC_QUAL(In); |
| 894 | PRINT_OBJC_QUAL(Inout); |
| 895 | PRINT_OBJC_QUAL(Out); |
| 896 | PRINT_OBJC_QUAL(Bycopy); |
| 897 | PRINT_OBJC_QUAL(Byref); |
| 898 | PRINT_OBJC_QUAL(Oneway); |
| 899 | printf("]"); |
| 900 | } |
| 901 | } |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 902 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 903 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 904 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 905 | static const char* GetCursorSource(CXCursor Cursor) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 906 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 907 | CXString source; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 908 | CXFile file; |
Argyrios Kyrtzidis | b4efaa0 | 2011-11-03 02:20:36 +0000 | [diff] [blame] | 909 | clang_getExpansionLocation(Loc, &file, 0, 0, 0); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 910 | source = clang_getFileName(file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 911 | if (!clang_getCString(source)) { |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 912 | clang_disposeString(source); |
| 913 | return "<invalid loc>"; |
| 914 | } |
| 915 | else { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 916 | const char *b = basename(clang_getCString(source)); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 917 | clang_disposeString(source); |
| 918 | return b; |
| 919 | } |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 922 | /******************************************************************************/ |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 923 | /* Callbacks. */ |
| 924 | /******************************************************************************/ |
| 925 | |
| 926 | typedef void (*PostVisitTU)(CXTranslationUnit); |
| 927 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 928 | void PrintDiagnostic(CXDiagnostic Diagnostic) { |
| 929 | FILE *out = stderr; |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 930 | CXFile file; |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 931 | CXString Msg; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 932 | unsigned display_opts = CXDiagnostic_DisplaySourceLocation |
Douglas Gregor | aa5f135 | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 933 | | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges |
| 934 | | CXDiagnostic_DisplayOption; |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 935 | unsigned i, num_fixits; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 937 | if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 938 | return; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | 274f190 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 940 | Msg = clang_formatDiagnostic(Diagnostic, display_opts); |
| 941 | fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| 942 | clang_disposeString(Msg); |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 943 | |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 944 | clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| 945 | &file, 0, 0, 0); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 946 | if (!file) |
| 947 | return; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 948 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 949 | num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); |
Ted Kremenek | 3739b32 | 2012-03-20 20:49:45 +0000 | [diff] [blame] | 950 | fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits); |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 951 | for (i = 0; i != num_fixits; ++i) { |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 952 | CXSourceRange range; |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 953 | CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range); |
| 954 | CXSourceLocation start = clang_getRangeStart(range); |
| 955 | CXSourceLocation end = clang_getRangeEnd(range); |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 956 | unsigned start_line, start_column, end_line, end_column; |
| 957 | CXFile start_file, end_file; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 958 | clang_getSpellingLocation(start, &start_file, &start_line, |
| 959 | &start_column, 0); |
| 960 | clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0); |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 961 | if (clang_equalLocations(start, end)) { |
| 962 | /* Insertion. */ |
| 963 | if (start_file == file) |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 964 | fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 965 | clang_getCString(insertion_text), start_line, start_column); |
| 966 | } else if (strcmp(clang_getCString(insertion_text), "") == 0) { |
| 967 | /* Removal. */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 968 | if (start_file == file && end_file == file) { |
| 969 | fprintf(out, "FIX-IT: Remove "); |
| 970 | PrintExtent(out, start_line, start_column, end_line, end_column); |
| 971 | fprintf(out, "\n"); |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 972 | } |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 973 | } else { |
| 974 | /* Replacement. */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 975 | if (start_file == end_file) { |
| 976 | fprintf(out, "FIX-IT: Replace "); |
| 977 | PrintExtent(out, start_line, start_column, end_line, end_column); |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 978 | fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); |
Douglas Gregor | 436f3f0 | 2010-02-18 22:27:07 +0000 | [diff] [blame] | 979 | } |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 980 | } |
Douglas Gregor | 473d701 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 981 | clang_disposeString(insertion_text); |
Douglas Gregor | 51c6d38 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 982 | } |
Douglas Gregor | 5352ac0 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 985 | void PrintDiagnosticSet(CXDiagnosticSet Set) { |
| 986 | int i = 0, n = clang_getNumDiagnosticsInSet(Set); |
| 987 | for ( ; i != n ; ++i) { |
| 988 | CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i); |
| 989 | CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 990 | PrintDiagnostic(Diag); |
Ted Kremenek | 7473b1c | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 991 | if (ChildDiags) |
| 992 | PrintDiagnosticSet(ChildDiags); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | void PrintDiagnostics(CXTranslationUnit TU) { |
| 997 | CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU); |
| 998 | PrintDiagnosticSet(TUSet); |
| 999 | clang_disposeDiagnosticSet(TUSet); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1002 | void PrintMemoryUsage(CXTranslationUnit TU) { |
Matt Beaumont-Gay | b227323 | 2011-08-29 16:37:29 +0000 | [diff] [blame] | 1003 | unsigned long total = 0; |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 1004 | unsigned i = 0; |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1005 | CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU); |
Francois Pichet | 3c68336 | 2011-04-18 23:33:22 +0000 | [diff] [blame] | 1006 | fprintf(stderr, "Memory usage:\n"); |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 1007 | for (i = 0 ; i != usage.numEntries; ++i) { |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 1008 | const char *name = clang_getTUResourceUsageName(usage.entries[i].kind); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1009 | unsigned long amount = usage.entries[i].amount; |
| 1010 | total += amount; |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 1011 | fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1012 | ((double) amount)/(1024*1024)); |
| 1013 | } |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 1014 | fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1015 | ((double) total)/(1024*1024)); |
Ted Kremenek | f787002 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 1016 | clang_disposeCXTUResourceUsage(usage); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1019 | /******************************************************************************/ |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1020 | /* Logic for testing traversal. */ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1021 | /******************************************************************************/ |
| 1022 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1023 | static void PrintCursorExtent(CXCursor C) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1024 | CXSourceRange extent = clang_getCursorExtent(C); |
| 1025 | PrintRange(extent, "Extent"); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1028 | /* Data used by the visitors. */ |
| 1029 | typedef struct { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1030 | CXTranslationUnit TU; |
| 1031 | enum CXCursorKind *Filter; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1032 | const char *CommentSchemaFile; |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1033 | } VisitorData; |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1034 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1035 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1036 | enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1037 | CXCursor Parent, |
| 1038 | CXClientData ClientData) { |
| 1039 | VisitorData *Data = (VisitorData *)ClientData; |
| 1040 | if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1041 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1042 | unsigned line, column; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 1043 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1044 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1045 | GetCursorSource(Cursor), line, column); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1046 | PrintCursor(Cursor, Data->CommentSchemaFile); |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1047 | PrintCursorExtent(Cursor); |
Argyrios Kyrtzidis | 04b6748 | 2013-04-11 17:02:10 +0000 | [diff] [blame] | 1048 | if (clang_isDeclaration(Cursor.kind)) { |
| 1049 | enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor); |
| 1050 | const char *accessStr = 0; |
| 1051 | |
| 1052 | switch (access) { |
| 1053 | case CX_CXXInvalidAccessSpecifier: break; |
| 1054 | case CX_CXXPublic: |
| 1055 | accessStr = "public"; break; |
| 1056 | case CX_CXXProtected: |
| 1057 | accessStr = "protected"; break; |
| 1058 | case CX_CXXPrivate: |
| 1059 | accessStr = "private"; break; |
| 1060 | } |
| 1061 | |
| 1062 | if (accessStr) |
| 1063 | printf(" [access=%s]", accessStr); |
| 1064 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1065 | printf("\n"); |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1066 | return CXChildVisit_Recurse; |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1067 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1068 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1069 | return CXChildVisit_Continue; |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 1070 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 1071 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1072 | static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1073 | CXCursor Parent, |
| 1074 | CXClientData ClientData) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1075 | const char *startBuf, *endBuf; |
| 1076 | unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; |
| 1077 | CXCursor Ref; |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1078 | VisitorData *Data = (VisitorData *)ClientData; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1079 | |
Douglas Gregor | b699866 | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1080 | if (Cursor.kind != CXCursor_FunctionDecl || |
| 1081 | !clang_isCursorDefinition(Cursor)) |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1082 | return CXChildVisit_Continue; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1083 | |
| 1084 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 1085 | &startLine, &startColumn, |
| 1086 | &endLine, &endColumn); |
| 1087 | /* Probe the entire body, looking for both decls and refs. */ |
| 1088 | curLine = startLine; |
| 1089 | curColumn = startColumn; |
| 1090 | |
| 1091 | while (startBuf < endBuf) { |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1092 | CXSourceLocation Loc; |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1093 | CXFile file; |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1094 | CXString source; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1095 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1096 | if (*startBuf == '\n') { |
| 1097 | startBuf++; |
| 1098 | curLine++; |
| 1099 | curColumn = 1; |
| 1100 | } else if (*startBuf != '\t') |
| 1101 | curColumn++; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1103 | Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 1104 | clang_getSpellingLocation(Loc, &file, 0, 0, 0); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1105 | |
Douglas Gregor | 1db19de | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1106 | source = clang_getFileName(file); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1107 | if (clang_getCString(source)) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1108 | CXSourceLocation RefLoc |
| 1109 | = clang_getLocation(Data->TU, file, curLine, curColumn); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1110 | Ref = clang_getCursor(Data->TU, RefLoc); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1111 | if (Ref.kind == CXCursor_NoDeclFound) { |
| 1112 | /* Nothing found here; that's fine. */ |
| 1113 | } else if (Ref.kind != CXCursor_FunctionDecl) { |
| 1114 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), |
| 1115 | curLine, curColumn); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1116 | PrintCursor(Ref, Data->CommentSchemaFile); |
Douglas Gregor | 98258af | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1117 | printf("\n"); |
| 1118 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1119 | } |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1120 | clang_disposeString(source); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1121 | startBuf++; |
| 1122 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1124 | return CXChildVisit_Continue; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1127 | /******************************************************************************/ |
| 1128 | /* USR testing. */ |
| 1129 | /******************************************************************************/ |
| 1130 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1131 | enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, |
| 1132 | CXClientData ClientData) { |
| 1133 | VisitorData *Data = (VisitorData *)ClientData; |
| 1134 | if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1135 | CXString USR = clang_getCursorUSR(C); |
| 1136 | const char *cstr = clang_getCString(USR); |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 1137 | if (!cstr || cstr[0] == '\0') { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1138 | clang_disposeString(USR); |
Ted Kremenek | e74ef12 | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 1139 | return CXChildVisit_Recurse; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1140 | } |
Ted Kremenek | e542f77 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 1141 | printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr); |
| 1142 | |
Douglas Gregor | a7bde20 | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1143 | PrintCursorExtent(C); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1144 | printf("\n"); |
| 1145 | clang_disposeString(USR); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1146 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1147 | return CXChildVisit_Recurse; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1150 | return CXChildVisit_Continue; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /******************************************************************************/ |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1154 | /* Inclusion stack testing. */ |
| 1155 | /******************************************************************************/ |
| 1156 | |
| 1157 | void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, |
| 1158 | unsigned includeStackLen, CXClientData data) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1159 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1160 | unsigned i; |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1161 | CXString fname; |
| 1162 | |
| 1163 | fname = clang_getFileName(includedFile); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1164 | printf("file: %s\nincluded by:\n", clang_getCString(fname)); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1165 | clang_disposeString(fname); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1166 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1167 | for (i = 0; i < includeStackLen; ++i) { |
| 1168 | CXFile includingFile; |
| 1169 | unsigned line, column; |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 1170 | clang_getSpellingLocation(includeStack[i], &includingFile, &line, |
| 1171 | &column, 0); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1172 | fname = clang_getFileName(includingFile); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1173 | printf(" %s:%d:%d\n", clang_getCString(fname), line, column); |
Ted Kremenek | 7484407 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1174 | clang_disposeString(fname); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1175 | } |
| 1176 | printf("\n"); |
| 1177 | } |
| 1178 | |
| 1179 | void PrintInclusionStack(CXTranslationUnit TU) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1180 | clang_getInclusions(TU, InclusionVisitor, NULL); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | /******************************************************************************/ |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1184 | /* Linkage testing. */ |
| 1185 | /******************************************************************************/ |
| 1186 | |
| 1187 | static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, |
| 1188 | CXClientData d) { |
| 1189 | const char *linkage = 0; |
| 1190 | |
| 1191 | if (clang_isInvalid(clang_getCursorKind(cursor))) |
| 1192 | return CXChildVisit_Recurse; |
| 1193 | |
| 1194 | switch (clang_getCursorLinkage(cursor)) { |
| 1195 | case CXLinkage_Invalid: break; |
Douglas Gregor | c2a2b3c | 2010-03-04 19:36:27 +0000 | [diff] [blame] | 1196 | case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; |
| 1197 | case CXLinkage_Internal: linkage = "Internal"; break; |
| 1198 | case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; |
| 1199 | case CXLinkage_External: linkage = "External"; break; |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | if (linkage) { |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1203 | PrintCursor(cursor, NULL); |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1204 | printf("linkage=%s\n", linkage); |
| 1205 | } |
| 1206 | |
| 1207 | return CXChildVisit_Recurse; |
| 1208 | } |
| 1209 | |
| 1210 | /******************************************************************************/ |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1211 | /* Typekind testing. */ |
| 1212 | /******************************************************************************/ |
| 1213 | |
Dmitri Gribenko | ae03d8e | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1214 | static void PrintTypeAndTypeKind(CXType T, const char *Format) { |
| 1215 | CXString TypeSpelling, TypeKindSpelling; |
| 1216 | |
| 1217 | TypeSpelling = clang_getTypeSpelling(T); |
| 1218 | TypeKindSpelling = clang_getTypeKindSpelling(T.kind); |
| 1219 | printf(Format, |
| 1220 | clang_getCString(TypeSpelling), |
| 1221 | clang_getCString(TypeKindSpelling)); |
| 1222 | clang_disposeString(TypeSpelling); |
| 1223 | clang_disposeString(TypeKindSpelling); |
| 1224 | } |
| 1225 | |
| 1226 | static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p, |
| 1227 | CXClientData d) { |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1228 | if (!clang_isInvalid(clang_getCursorKind(cursor))) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1229 | CXType T = clang_getCursorType(cursor); |
Argyrios Kyrtzidis | 659837e | 2013-10-11 19:58:38 +0000 | [diff] [blame] | 1230 | enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1231 | PrintCursor(cursor, NULL); |
Dmitri Gribenko | ae03d8e | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1232 | PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]"); |
Douglas Gregor | e72fb6f | 2011-01-27 16:27:11 +0000 | [diff] [blame] | 1233 | if (clang_isConstQualifiedType(T)) |
| 1234 | printf(" const"); |
| 1235 | if (clang_isVolatileQualifiedType(T)) |
| 1236 | printf(" volatile"); |
| 1237 | if (clang_isRestrictQualifiedType(T)) |
| 1238 | printf(" restrict"); |
Argyrios Kyrtzidis | 659837e | 2013-10-11 19:58:38 +0000 | [diff] [blame] | 1239 | if (RQ == CXRefQualifier_LValue) |
| 1240 | printf(" lvalue-ref-qualifier"); |
| 1241 | if (RQ == CXRefQualifier_RValue) |
| 1242 | printf(" rvalue-ref-qualifier"); |
Benjamin Kramer | e1403d2 | 2010-06-22 09:29:44 +0000 | [diff] [blame] | 1243 | /* Print the canonical type if it is different. */ |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1244 | { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1245 | CXType CT = clang_getCanonicalType(T); |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1246 | if (!clang_equalTypes(T, CT)) { |
Dmitri Gribenko | ae03d8e | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1247 | PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]"); |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1248 | } |
| 1249 | } |
Benjamin Kramer | e1403d2 | 2010-06-22 09:29:44 +0000 | [diff] [blame] | 1250 | /* Print the return type if it exists. */ |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1251 | { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1252 | CXType RT = clang_getCursorResultType(cursor); |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1253 | if (RT.kind != CXType_Invalid) { |
Dmitri Gribenko | ae03d8e | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1254 | PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]"); |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
Argyrios Kyrtzidis | d98ef9a | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1257 | /* Print the argument types if they exist. */ |
| 1258 | { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1259 | int NumArgs = clang_Cursor_getNumArguments(cursor); |
| 1260 | if (NumArgs != -1 && NumArgs != 0) { |
Argyrios Kyrtzidis | 47f1165 | 2012-04-11 19:54:09 +0000 | [diff] [blame] | 1261 | int i; |
Argyrios Kyrtzidis | d98ef9a | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1262 | printf(" [args="); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1263 | for (i = 0; i < NumArgs; ++i) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1264 | CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i)); |
Argyrios Kyrtzidis | d98ef9a | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1265 | if (T.kind != CXType_Invalid) { |
Dmitri Gribenko | ae03d8e | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1266 | PrintTypeAndTypeKind(T, " [%s] [%s]"); |
Argyrios Kyrtzidis | d98ef9a | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | printf("]"); |
| 1270 | } |
| 1271 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1272 | /* Print the template argument types if they exist. */ |
| 1273 | { |
| 1274 | int NumTArgs = clang_Type_getNumTemplateArguments(T); |
| 1275 | if (NumTArgs != -1 && NumTArgs != 0) { |
| 1276 | int i; |
| 1277 | printf(" [templateargs/%d=", NumTArgs); |
| 1278 | for (i = 0; i < NumTArgs; ++i) { |
| 1279 | CXType TArg = clang_Type_getTemplateArgumentAsType(T, i); |
| 1280 | if (TArg.kind != CXType_Invalid) { |
| 1281 | PrintTypeAndTypeKind(TArg, " [type=%s] [typekind=%s]"); |
| 1282 | } |
| 1283 | } |
| 1284 | printf("]"); |
| 1285 | } |
| 1286 | } |
Ted Kremenek | 3ce9e7d | 2010-07-30 00:14:11 +0000 | [diff] [blame] | 1287 | /* Print if this is a non-POD type. */ |
| 1288 | printf(" [isPOD=%d]", clang_isPODType(T)); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1289 | /* Print the pointee type. */ |
| 1290 | { |
| 1291 | CXType PT = clang_getPointeeType(T); |
| 1292 | if (PT.kind != CXType_Invalid) { |
| 1293 | PrintTypeAndTypeKind(PT, " [pointeetype=%s] [pointeekind=%s]"); |
| 1294 | } |
| 1295 | } |
Ted Kremenek | 04c3cf3 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1296 | |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1297 | printf("\n"); |
| 1298 | } |
| 1299 | return CXChildVisit_Recurse; |
| 1300 | } |
| 1301 | |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1302 | static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p, |
| 1303 | CXClientData d) { |
| 1304 | CXType T; |
| 1305 | enum CXCursorKind K = clang_getCursorKind(cursor); |
| 1306 | if (clang_isInvalid(K)) |
| 1307 | return CXChildVisit_Recurse; |
| 1308 | T = clang_getCursorType(cursor); |
| 1309 | PrintCursor(cursor, NULL); |
| 1310 | PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]"); |
| 1311 | /* Print the type sizeof if applicable. */ |
| 1312 | { |
| 1313 | long long Size = clang_Type_getSizeOf(T); |
| 1314 | if (Size >= 0 || Size < -1 ) { |
| 1315 | printf(" [sizeof=%lld]", Size); |
| 1316 | } |
| 1317 | } |
| 1318 | /* Print the type alignof if applicable. */ |
| 1319 | { |
| 1320 | long long Align = clang_Type_getAlignOf(T); |
| 1321 | if (Align >= 0 || Align < -1) { |
| 1322 | printf(" [alignof=%lld]", Align); |
| 1323 | } |
| 1324 | } |
| 1325 | /* Print the record field offset if applicable. */ |
| 1326 | { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1327 | CXString FieldSpelling = clang_getCursorSpelling(cursor); |
| 1328 | const char *FieldName = clang_getCString(FieldSpelling); |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1329 | /* recurse to get the root anonymous record parent */ |
| 1330 | CXCursor Parent, Root; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1331 | if (clang_getCursorKind(cursor) == CXCursor_FieldDecl) { |
| 1332 | CXString RootParentSpelling; |
| 1333 | const char *RootParentName = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1334 | Parent = p; |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1335 | do { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1336 | if (RootParentName != 0) |
| 1337 | clang_disposeString(RootParentSpelling); |
| 1338 | |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1339 | Root = Parent; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1340 | RootParentSpelling = clang_getCursorSpelling(Root); |
| 1341 | RootParentName = clang_getCString(RootParentSpelling); |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1342 | Parent = clang_getCursorSemanticParent(Root); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1343 | } while (clang_getCursorType(Parent).kind == CXType_Record && |
| 1344 | !strcmp(RootParentName, "")); |
| 1345 | clang_disposeString(RootParentSpelling); |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1346 | /* if RootParentName is "", record is anonymous. */ |
| 1347 | { |
| 1348 | long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root), |
| 1349 | FieldName); |
| 1350 | printf(" [offsetof=%lld]", Offset); |
| 1351 | } |
| 1352 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1353 | clang_disposeString(FieldSpelling); |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1354 | } |
| 1355 | /* Print if its a bitfield */ |
| 1356 | { |
| 1357 | int IsBitfield = clang_Cursor_isBitField(cursor); |
| 1358 | if (IsBitfield) |
| 1359 | printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor)); |
| 1360 | } |
| 1361 | printf("\n"); |
| 1362 | return CXChildVisit_Recurse; |
| 1363 | } |
| 1364 | |
Dmitri Gribenko | 1eb6082 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 1365 | /******************************************************************************/ |
| 1366 | /* Bitwidth testing. */ |
| 1367 | /******************************************************************************/ |
| 1368 | |
| 1369 | static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p, |
| 1370 | CXClientData d) { |
NAKAMURA Takumi | 02c1b86 | 2012-12-04 15:32:03 +0000 | [diff] [blame] | 1371 | int Bitwidth; |
Dmitri Gribenko | 1eb6082 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 1372 | if (clang_getCursorKind(cursor) != CXCursor_FieldDecl) |
| 1373 | return CXChildVisit_Recurse; |
| 1374 | |
NAKAMURA Takumi | 02c1b86 | 2012-12-04 15:32:03 +0000 | [diff] [blame] | 1375 | Bitwidth = clang_getFieldDeclBitWidth(cursor); |
Dmitri Gribenko | 1eb6082 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 1376 | if (Bitwidth >= 0) { |
| 1377 | PrintCursor(cursor, NULL); |
| 1378 | printf(" bitwidth=%d\n", Bitwidth); |
| 1379 | } |
| 1380 | |
| 1381 | return CXChildVisit_Recurse; |
| 1382 | } |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1383 | |
| 1384 | /******************************************************************************/ |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1385 | /* Loading ASTs/source. */ |
| 1386 | /******************************************************************************/ |
| 1387 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1388 | static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, |
Ted Kremenek | 9827156 | 2010-01-12 18:53:15 +0000 | [diff] [blame] | 1389 | const char *filter, const char *prefix, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1390 | CXCursorVisitor Visitor, |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1391 | PostVisitTU PV, |
| 1392 | const char *CommentSchemaFile) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1393 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1394 | if (prefix) |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1395 | FileCheckPrefix = prefix; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (Visitor) { |
| 1398 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 1399 | enum CXCursorKind *ck = &K; |
| 1400 | VisitorData Data; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1401 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1402 | /* Perform some simple filtering. */ |
| 1403 | if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 1404 | else if (!strcmp(filter, "all-display") || |
| 1405 | !strcmp(filter, "local-display")) { |
| 1406 | ck = NULL; |
| 1407 | want_display_name = 1; |
| 1408 | } |
Daniel Dunbar | b1ffee6 | 2010-02-10 20:42:40 +0000 | [diff] [blame] | 1409 | else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1410 | else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| 1411 | else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 1412 | else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 1413 | else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| 1414 | else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
| 1415 | else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; |
| 1416 | else { |
| 1417 | fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| 1418 | return 1; |
| 1419 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1420 | |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1421 | Data.TU = TU; |
| 1422 | Data.Filter = ck; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1423 | Data.CommentSchemaFile = CommentSchemaFile; |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1424 | clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1425 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1426 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1427 | if (PV) |
| 1428 | PV(TU); |
Ted Kremenek | e3ee02a | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1429 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1430 | PrintDiagnostics(TU); |
Argyrios Kyrtzidis | 16ac8be | 2011-11-13 23:39:14 +0000 | [diff] [blame] | 1431 | if (checkForErrors(TU) != 0) { |
| 1432 | clang_disposeTranslationUnit(TU); |
| 1433 | return -1; |
| 1434 | } |
| 1435 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1436 | clang_disposeTranslationUnit(TU); |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1440 | int perform_test_load_tu(const char *file, const char *filter, |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1441 | const char *prefix, CXCursorVisitor Visitor, |
| 1442 | PostVisitTU PV) { |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1443 | CXIndex Idx; |
| 1444 | CXTranslationUnit TU; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1445 | int result; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1446 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1447 | !strcmp(filter, "local") ? 1 : 0, |
Stefanus Du Toit | fc09336 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 1448 | /* displayDiagnostics=*/1); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1449 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1450 | if (!CreateTranslationUnit(Idx, file, &TU)) { |
| 1451 | clang_disposeIndex(Idx); |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1452 | return 1; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1453 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1454 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1455 | result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1456 | clang_disposeIndex(Idx); |
| 1457 | return result; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1460 | int perform_test_load_source(int argc, const char **argv, |
| 1461 | const char *filter, CXCursorVisitor Visitor, |
| 1462 | PostVisitTU PV) { |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1463 | CXIndex Idx; |
| 1464 | CXTranslationUnit TU; |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1465 | const char *CommentSchemaFile; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1466 | struct CXUnsavedFile *unsaved_files = 0; |
| 1467 | int num_unsaved_files = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1468 | enum CXErrorCode Err; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1469 | int result; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1470 | |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1471 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 358559d | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 1472 | (!strcmp(filter, "local") || |
| 1473 | !strcmp(filter, "local-display"))? 1 : 0, |
Argyrios Kyrtzidis | cd6dcb3 | 2013-04-09 20:29:24 +0000 | [diff] [blame] | 1474 | /* displayDiagnostics=*/1); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1475 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1476 | if ((CommentSchemaFile = parse_comments_schema(argc, argv))) { |
| 1477 | argc--; |
| 1478 | argv++; |
| 1479 | } |
| 1480 | |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1481 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 1482 | clang_disposeIndex(Idx); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1483 | return -1; |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1484 | } |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1485 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1486 | Err = clang_parseTranslationUnit2(Idx, 0, |
| 1487 | argv + num_unsaved_files, |
| 1488 | argc - num_unsaved_files, |
| 1489 | unsaved_files, num_unsaved_files, |
| 1490 | getDefaultParsingOptions(), &TU); |
| 1491 | if (Err != CXError_Success) { |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1492 | fprintf(stderr, "Unable to load translation unit!\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1493 | describeLibclangFailure(Err); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1494 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1495 | clang_disposeIndex(Idx); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1496 | return 1; |
| 1497 | } |
| 1498 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1499 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, |
| 1500 | CommentSchemaFile); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1501 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 020a095 | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1502 | clang_disposeIndex(Idx); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1503 | return result; |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1506 | int perform_test_reparse_source(int argc, const char **argv, int trials, |
| 1507 | const char *filter, CXCursorVisitor Visitor, |
| 1508 | PostVisitTU PV) { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1509 | CXIndex Idx; |
| 1510 | CXTranslationUnit TU; |
| 1511 | struct CXUnsavedFile *unsaved_files = 0; |
| 1512 | int num_unsaved_files = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1513 | int compiler_arg_idx = 0; |
| 1514 | enum CXErrorCode Err; |
| 1515 | int result, i; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1516 | int trial; |
Argyrios Kyrtzidis | 40098e8 | 2011-09-12 18:09:31 +0000 | [diff] [blame] | 1517 | int remap_after_trial = 0; |
| 1518 | char *endptr = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1519 | |
| 1520 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| 1521 | !strcmp(filter, "local") ? 1 : 0, |
Argyrios Kyrtzidis | cd6dcb3 | 2013-04-09 20:29:24 +0000 | [diff] [blame] | 1522 | /* displayDiagnostics=*/1); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1523 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1524 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 1525 | clang_disposeIndex(Idx); |
| 1526 | return -1; |
| 1527 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1528 | |
| 1529 | for (i = 0; i < argc; ++i) { |
| 1530 | if (strcmp(argv[i], "--") == 0) |
| 1531 | break; |
| 1532 | } |
| 1533 | if (i < argc) |
| 1534 | compiler_arg_idx = i+1; |
| 1535 | if (num_unsaved_files > compiler_arg_idx) |
| 1536 | compiler_arg_idx = num_unsaved_files; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1537 | |
Daniel Dunbar | c8a6180 | 2010-08-18 23:09:16 +0000 | [diff] [blame] | 1538 | /* Load the initial translation unit -- we do this without honoring remapped |
| 1539 | * files, so that we have a way to test results after changing the source. */ |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1540 | Err = clang_parseTranslationUnit2(Idx, 0, |
| 1541 | argv + compiler_arg_idx, |
| 1542 | argc - compiler_arg_idx, |
| 1543 | 0, 0, getDefaultParsingOptions(), &TU); |
| 1544 | if (Err != CXError_Success) { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1545 | fprintf(stderr, "Unable to load translation unit!\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1546 | describeLibclangFailure(Err); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1547 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1548 | clang_disposeIndex(Idx); |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
Argyrios Kyrtzidis | bda536d | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 1552 | if (checkForErrors(TU) != 0) |
| 1553 | return -1; |
| 1554 | |
Argyrios Kyrtzidis | 40098e8 | 2011-09-12 18:09:31 +0000 | [diff] [blame] | 1555 | if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) { |
| 1556 | remap_after_trial = |
| 1557 | strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10); |
| 1558 | } |
| 1559 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1560 | for (trial = 0; trial < trials; ++trial) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1561 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1562 | if (parse_remapped_files_with_try(trial, argc, argv, 0, |
| 1563 | &unsaved_files, &num_unsaved_files)) { |
| 1564 | clang_disposeTranslationUnit(TU); |
| 1565 | clang_disposeIndex(Idx); |
| 1566 | return -1; |
| 1567 | } |
| 1568 | |
| 1569 | Err = clang_reparseTranslationUnit( |
| 1570 | TU, |
| 1571 | trial >= remap_after_trial ? num_unsaved_files : 0, |
| 1572 | trial >= remap_after_trial ? unsaved_files : 0, |
| 1573 | clang_defaultReparseOptions(TU)); |
| 1574 | if (Err != CXError_Success) { |
Daniel Dunbar | c8a6180 | 2010-08-18 23:09:16 +0000 | [diff] [blame] | 1575 | fprintf(stderr, "Unable to reparse translation unit!\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1576 | describeLibclangFailure(Err); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1577 | clang_disposeTranslationUnit(TU); |
| 1578 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1579 | clang_disposeIndex(Idx); |
| 1580 | return -1; |
| 1581 | } |
Argyrios Kyrtzidis | bda536d | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 1582 | |
| 1583 | if (checkForErrors(TU) != 0) |
| 1584 | return -1; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1587 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL); |
Argyrios Kyrtzidis | bda536d | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 1588 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1589 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1590 | clang_disposeIndex(Idx); |
| 1591 | return result; |
| 1592 | } |
| 1593 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1594 | /******************************************************************************/ |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1595 | /* Logic for testing clang_getCursor(). */ |
| 1596 | /******************************************************************************/ |
| 1597 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 1598 | static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor, |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1599 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1600 | unsigned end_line, unsigned end_col, |
| 1601 | const char *prefix) { |
Ted Kremenek | 9096a20 | 2010-01-07 01:17:12 +0000 | [diff] [blame] | 1602 | printf("// %s: ", FileCheckPrefix); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1603 | if (prefix) |
| 1604 | printf("-%s", prefix); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 1605 | PrintExtent(stdout, start_line, start_col, end_line, end_col); |
| 1606 | printf(" "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1607 | PrintCursor(cursor, NULL); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1608 | printf("\n"); |
| 1609 | } |
| 1610 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1611 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 1612 | const char *prefix) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1613 | CXIndex Idx; |
| 1614 | CXTranslationUnit TU; |
| 1615 | FILE *fp; |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1616 | CXCursor prevCursor = clang_getNullCursor(); |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1617 | CXFile file; |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1618 | unsigned line = 1, col = 1; |
Daniel Dunbar | 8f0bf81 | 2010-02-14 08:32:51 +0000 | [diff] [blame] | 1619 | unsigned start_line = 1, start_col = 1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1620 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1621 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
Stefanus Du Toit | fc09336 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 1622 | /* displayDiagnostics=*/1))) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1623 | fprintf(stderr, "Could not create Index\n"); |
| 1624 | return 1; |
| 1625 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1626 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1627 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 1628 | return 1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1629 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1630 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 1631 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 1632 | return 1; |
| 1633 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1634 | |
Douglas Gregor | b979034 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1635 | file = clang_getFile(TU, source_file); |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1636 | for (;;) { |
| 1637 | CXCursor cursor; |
| 1638 | int c = fgetc(fp); |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 1639 | |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1640 | if (c == '\n') { |
| 1641 | ++line; |
| 1642 | col = 1; |
| 1643 | } else |
| 1644 | ++col; |
| 1645 | |
| 1646 | /* Check the cursor at this position, and dump the previous one if we have |
| 1647 | * found something new. |
| 1648 | */ |
| 1649 | cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); |
| 1650 | if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && |
| 1651 | prevCursor.kind != CXCursor_InvalidFile) { |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 1652 | print_cursor_file_scan(TU, prevCursor, start_line, start_col, |
Daniel Dunbar | d52864b | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 1653 | line, col, prefix); |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1654 | start_line = line; |
| 1655 | start_col = col; |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 1656 | } |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1657 | if (c == EOF) |
| 1658 | break; |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 1659 | |
Daniel Dunbar | 2389eff | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1660 | prevCursor = cursor; |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1661 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1662 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1663 | fclose(fp); |
Douglas Gregor | 4f5e21e | 2011-01-31 22:04:05 +0000 | [diff] [blame] | 1664 | clang_disposeTranslationUnit(TU); |
| 1665 | clang_disposeIndex(Idx); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | /******************************************************************************/ |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1670 | /* Logic for testing clang code completion. */ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1671 | /******************************************************************************/ |
| 1672 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1673 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 1674 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 1675 | 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] | 1676 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1677 | unsigned *column, unsigned *second_line, |
| 1678 | unsigned *second_column) { |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 1679 | /* Find the second colon. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1680 | const char *last_colon = strrchr(input, ':'); |
| 1681 | unsigned values[4], i; |
| 1682 | unsigned num_values = (second_line && second_column)? 4 : 2; |
| 1683 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1684 | char *endptr = 0; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1685 | if (!last_colon || last_colon == input) { |
| 1686 | if (num_values == 4) |
| 1687 | fprintf(stderr, "could not parse filename:line:column:line:column in " |
| 1688 | "'%s'\n", input); |
| 1689 | else |
| 1690 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1691 | return 1; |
| 1692 | } |
| 1693 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1694 | for (i = 0; i != num_values; ++i) { |
| 1695 | const char *prev_colon; |
| 1696 | |
| 1697 | /* Parse the next line or column. */ |
| 1698 | values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); |
| 1699 | if (*endptr != 0 && *endptr != ':') { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1700 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1701 | (i % 2 ? "column" : "line"), input); |
| 1702 | return 1; |
| 1703 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1704 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1705 | if (i + 1 == num_values) |
| 1706 | break; |
| 1707 | |
| 1708 | /* Find the previous colon. */ |
| 1709 | prev_colon = last_colon - 1; |
| 1710 | while (prev_colon != input && *prev_colon != ':') |
| 1711 | --prev_colon; |
| 1712 | if (prev_colon == input) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1713 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1714 | (i % 2 == 0? "column" : "line"), input); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1715 | return 1; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | last_colon = prev_colon; |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1721 | *line = values[0]; |
| 1722 | *column = values[1]; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1723 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1724 | if (second_line && second_column) { |
| 1725 | *second_line = values[2]; |
| 1726 | *second_column = values[3]; |
| 1727 | } |
| 1728 | |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 1729 | /* Copy the file name. */ |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1730 | *filename = (char*)malloc(last_colon - input + 1); |
| 1731 | memcpy(*filename, input, last_colon - input); |
| 1732 | (*filename)[last_colon - input] = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | const char * |
| 1737 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 1738 | switch (Kind) { |
| 1739 | case CXCompletionChunk_Optional: return "Optional"; |
| 1740 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 1741 | case CXCompletionChunk_Text: return "Text"; |
| 1742 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 1743 | case CXCompletionChunk_Informative: return "Informative"; |
| 1744 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 1745 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 1746 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 1747 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 1748 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 1749 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 1750 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 1751 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 1752 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 1753 | case CXCompletionChunk_Comma: return "Comma"; |
Douglas Gregor | ff5ce6e | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1754 | case CXCompletionChunk_ResultType: return "ResultType"; |
Douglas Gregor | 01dfea0 | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1755 | case CXCompletionChunk_Colon: return "Colon"; |
| 1756 | case CXCompletionChunk_SemiColon: return "SemiColon"; |
| 1757 | case CXCompletionChunk_Equal: return "Equal"; |
| 1758 | case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; |
| 1759 | case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1760 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1761 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1762 | return "Unknown"; |
| 1763 | } |
| 1764 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1765 | static int checkForErrors(CXTranslationUnit TU) { |
| 1766 | unsigned Num, i; |
| 1767 | CXDiagnostic Diag; |
| 1768 | CXString DiagStr; |
| 1769 | |
| 1770 | if (!getenv("CINDEXTEST_FAILONERROR")) |
| 1771 | return 0; |
| 1772 | |
| 1773 | Num = clang_getNumDiagnostics(TU); |
| 1774 | for (i = 0; i != Num; ++i) { |
| 1775 | Diag = clang_getDiagnostic(TU, i); |
| 1776 | if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) { |
| 1777 | DiagStr = clang_formatDiagnostic(Diag, |
| 1778 | clang_defaultDiagnosticDisplayOptions()); |
| 1779 | fprintf(stderr, "%s\n", clang_getCString(DiagStr)); |
| 1780 | clang_disposeString(DiagStr); |
| 1781 | clang_disposeDiagnostic(Diag); |
| 1782 | return -1; |
| 1783 | } |
| 1784 | clang_disposeDiagnostic(Diag); |
| 1785 | } |
| 1786 | |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1790 | static void print_completion_string(CXCompletionString completion_string, |
| 1791 | FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 1792 | int I, N; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1794 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1795 | for (I = 0; I != N; ++I) { |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1796 | CXString text; |
| 1797 | const char *cstr; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1798 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1799 | = clang_getCompletionChunkKind(completion_string, I); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1801 | if (Kind == CXCompletionChunk_Optional) { |
| 1802 | fprintf(file, "{Optional "); |
| 1803 | print_completion_string( |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1804 | clang_getCompletionChunkCompletionString(completion_string, I), |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1805 | file); |
| 1806 | fprintf(file, "}"); |
| 1807 | continue; |
Douglas Gregor | 5a9c0bc | 2010-10-08 20:39:29 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | if (Kind == CXCompletionChunk_VerticalSpace) { |
| 1811 | fprintf(file, "{VerticalSpace }"); |
| 1812 | continue; |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1813 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1814 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 1815 | text = clang_getCompletionChunkText(completion_string, I); |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1816 | cstr = clang_getCString(text); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1817 | fprintf(file, "{%s %s}", |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1818 | clang_getCompletionChunkKindSpelling(Kind), |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1819 | cstr ? cstr : ""); |
| 1820 | clang_disposeString(text); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1821 | } |
Ted Kremenek | 2ef6f8f | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1825 | static void print_completion_result(CXCompletionResult *completion_result, |
| 1826 | FILE *file) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1827 | CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 1828 | unsigned annotationCount; |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1829 | enum CXCursorKind ParentKind; |
| 1830 | CXString ParentName; |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1831 | CXString BriefComment; |
| 1832 | const char *BriefCommentCString; |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1833 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1834 | fprintf(file, "%s:", clang_getCString(ks)); |
| 1835 | clang_disposeString(ks); |
| 1836 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1837 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1838 | fprintf(file, " (%u)", |
Douglas Gregor | 12e1313 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1839 | clang_getCompletionPriority(completion_result->CompletionString)); |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1840 | switch (clang_getCompletionAvailability(completion_result->CompletionString)){ |
| 1841 | case CXAvailability_Available: |
| 1842 | break; |
| 1843 | |
| 1844 | case CXAvailability_Deprecated: |
| 1845 | fprintf(file, " (deprecated)"); |
| 1846 | break; |
| 1847 | |
| 1848 | case CXAvailability_NotAvailable: |
| 1849 | fprintf(file, " (unavailable)"); |
| 1850 | break; |
Erik Verbruggen | d120596 | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1851 | |
| 1852 | case CXAvailability_NotAccessible: |
| 1853 | fprintf(file, " (inaccessible)"); |
| 1854 | break; |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1855 | } |
Erik Verbruggen | 6164ea1 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 1856 | |
| 1857 | annotationCount = clang_getCompletionNumAnnotations( |
| 1858 | completion_result->CompletionString); |
| 1859 | if (annotationCount) { |
| 1860 | unsigned i; |
| 1861 | fprintf(file, " ("); |
| 1862 | for (i = 0; i < annotationCount; ++i) { |
| 1863 | if (i != 0) |
| 1864 | fprintf(file, ", "); |
| 1865 | fprintf(file, "\"%s\"", |
| 1866 | clang_getCString(clang_getCompletionAnnotation( |
| 1867 | completion_result->CompletionString, i))); |
| 1868 | } |
| 1869 | fprintf(file, ")"); |
| 1870 | } |
| 1871 | |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1872 | if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) { |
| 1873 | ParentName = clang_getCompletionParent(completion_result->CompletionString, |
| 1874 | &ParentKind); |
| 1875 | if (ParentKind != CXCursor_NotImplemented) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1876 | CXString KindSpelling = clang_getCursorKindSpelling(ParentKind); |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1877 | fprintf(file, " (parent: %s '%s')", |
| 1878 | clang_getCString(KindSpelling), |
| 1879 | clang_getCString(ParentName)); |
| 1880 | clang_disposeString(KindSpelling); |
| 1881 | } |
| 1882 | clang_disposeString(ParentName); |
| 1883 | } |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1884 | |
| 1885 | BriefComment = clang_getCompletionBriefComment( |
| 1886 | completion_result->CompletionString); |
| 1887 | BriefCommentCString = clang_getCString(BriefComment); |
| 1888 | if (BriefCommentCString && *BriefCommentCString != '\0') { |
| 1889 | fprintf(file, "(brief comment: %s)", BriefCommentCString); |
| 1890 | } |
| 1891 | clang_disposeString(BriefComment); |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | 58ddb60 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1893 | fprintf(file, "\n"); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1896 | void print_completion_contexts(unsigned long long contexts, FILE *file) { |
| 1897 | fprintf(file, "Completion contexts:\n"); |
| 1898 | if (contexts == CXCompletionContext_Unknown) { |
| 1899 | fprintf(file, "Unknown\n"); |
| 1900 | } |
| 1901 | if (contexts & CXCompletionContext_AnyType) { |
| 1902 | fprintf(file, "Any type\n"); |
| 1903 | } |
| 1904 | if (contexts & CXCompletionContext_AnyValue) { |
| 1905 | fprintf(file, "Any value\n"); |
| 1906 | } |
| 1907 | if (contexts & CXCompletionContext_ObjCObjectValue) { |
| 1908 | fprintf(file, "Objective-C object value\n"); |
| 1909 | } |
| 1910 | if (contexts & CXCompletionContext_ObjCSelectorValue) { |
| 1911 | fprintf(file, "Objective-C selector value\n"); |
| 1912 | } |
| 1913 | if (contexts & CXCompletionContext_CXXClassTypeValue) { |
| 1914 | fprintf(file, "C++ class type value\n"); |
| 1915 | } |
| 1916 | if (contexts & CXCompletionContext_DotMemberAccess) { |
| 1917 | fprintf(file, "Dot member access\n"); |
| 1918 | } |
| 1919 | if (contexts & CXCompletionContext_ArrowMemberAccess) { |
| 1920 | fprintf(file, "Arrow member access\n"); |
| 1921 | } |
| 1922 | if (contexts & CXCompletionContext_ObjCPropertyAccess) { |
| 1923 | fprintf(file, "Objective-C property access\n"); |
| 1924 | } |
| 1925 | if (contexts & CXCompletionContext_EnumTag) { |
| 1926 | fprintf(file, "Enum tag\n"); |
| 1927 | } |
| 1928 | if (contexts & CXCompletionContext_UnionTag) { |
| 1929 | fprintf(file, "Union tag\n"); |
| 1930 | } |
| 1931 | if (contexts & CXCompletionContext_StructTag) { |
| 1932 | fprintf(file, "Struct tag\n"); |
| 1933 | } |
| 1934 | if (contexts & CXCompletionContext_ClassTag) { |
| 1935 | fprintf(file, "Class name\n"); |
| 1936 | } |
| 1937 | if (contexts & CXCompletionContext_Namespace) { |
| 1938 | fprintf(file, "Namespace or namespace alias\n"); |
| 1939 | } |
| 1940 | if (contexts & CXCompletionContext_NestedNameSpecifier) { |
| 1941 | fprintf(file, "Nested name specifier\n"); |
| 1942 | } |
| 1943 | if (contexts & CXCompletionContext_ObjCInterface) { |
| 1944 | fprintf(file, "Objective-C interface\n"); |
| 1945 | } |
| 1946 | if (contexts & CXCompletionContext_ObjCProtocol) { |
| 1947 | fprintf(file, "Objective-C protocol\n"); |
| 1948 | } |
| 1949 | if (contexts & CXCompletionContext_ObjCCategory) { |
| 1950 | fprintf(file, "Objective-C category\n"); |
| 1951 | } |
| 1952 | if (contexts & CXCompletionContext_ObjCInstanceMessage) { |
| 1953 | fprintf(file, "Objective-C instance method\n"); |
| 1954 | } |
| 1955 | if (contexts & CXCompletionContext_ObjCClassMessage) { |
| 1956 | fprintf(file, "Objective-C class method\n"); |
| 1957 | } |
| 1958 | if (contexts & CXCompletionContext_ObjCSelectorName) { |
| 1959 | fprintf(file, "Objective-C selector name\n"); |
| 1960 | } |
| 1961 | if (contexts & CXCompletionContext_MacroName) { |
| 1962 | fprintf(file, "Macro name\n"); |
| 1963 | } |
| 1964 | if (contexts & CXCompletionContext_NaturalLanguage) { |
| 1965 | fprintf(file, "Natural language\n"); |
| 1966 | } |
| 1967 | } |
| 1968 | |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1969 | int my_stricmp(const char *s1, const char *s2) { |
| 1970 | while (*s1 && *s2) { |
NAKAMURA Takumi | 6d55521 | 2011-03-09 03:02:28 +0000 | [diff] [blame] | 1971 | int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1972 | if (c1 < c2) |
| 1973 | return -1; |
| 1974 | else if (c1 > c2) |
| 1975 | return 1; |
| 1976 | |
| 1977 | ++s1; |
| 1978 | ++s2; |
| 1979 | } |
| 1980 | |
| 1981 | if (*s1) |
| 1982 | return 1; |
| 1983 | else if (*s2) |
| 1984 | return -1; |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1988 | int perform_code_completion(int argc, const char **argv, int timing_only) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1989 | const char *input = argv[1]; |
| 1990 | char *filename = 0; |
| 1991 | unsigned line; |
| 1992 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 1993 | CXIndex CIdx; |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1994 | int errorCode; |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1995 | struct CXUnsavedFile *unsaved_files = 0; |
| 1996 | int num_unsaved_files = 0; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1997 | CXCodeCompleteResults *results = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1998 | enum CXErrorCode Err; |
| 1999 | CXTranslationUnit TU; |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2000 | unsigned I, Repeats = 1; |
| 2001 | unsigned completionOptions = clang_defaultCodeCompleteOptions(); |
| 2002 | |
| 2003 | if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS")) |
| 2004 | completionOptions |= CXCodeComplete_IncludeCodePatterns; |
Dmitri Gribenko | d99ef53 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2005 | if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS")) |
| 2006 | completionOptions |= CXCodeComplete_IncludeBriefComments; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2007 | |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 2008 | if (timing_only) |
| 2009 | input += strlen("-code-completion-timing="); |
| 2010 | else |
| 2011 | input += strlen("-code-completion-at="); |
| 2012 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2013 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2014 | 0, 0))) |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2015 | return errorCode; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2016 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 2017 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 2018 | return -1; |
| 2019 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2020 | CIdx = clang_createIndex(0, 0); |
| 2021 | |
| 2022 | if (getenv("CINDEXTEST_EDITING")) |
| 2023 | Repeats = 5; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2024 | |
| 2025 | Err = clang_parseTranslationUnit2(CIdx, 0, |
| 2026 | argv + num_unsaved_files + 2, |
| 2027 | argc - num_unsaved_files - 2, |
| 2028 | 0, 0, getDefaultParsingOptions(), &TU); |
| 2029 | if (Err != CXError_Success) { |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2030 | fprintf(stderr, "Unable to load translation unit!\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2031 | describeLibclangFailure(Err); |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2032 | return 1; |
| 2033 | } |
Douglas Gregor | 08bb4c6 | 2010-11-15 23:00:34 +0000 | [diff] [blame] | 2034 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2035 | Err = clang_reparseTranslationUnit(TU, 0, 0, |
| 2036 | clang_defaultReparseOptions(TU)); |
| 2037 | |
| 2038 | if (Err != CXError_Success) { |
Douglas Gregor | 08bb4c6 | 2010-11-15 23:00:34 +0000 | [diff] [blame] | 2039 | fprintf(stderr, "Unable to reparse translation init!\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2040 | describeLibclangFailure(Err); |
| 2041 | clang_disposeTranslationUnit(TU); |
Douglas Gregor | 08bb4c6 | 2010-11-15 23:00:34 +0000 | [diff] [blame] | 2042 | return 1; |
| 2043 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2044 | |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2045 | for (I = 0; I != Repeats; ++I) { |
| 2046 | results = clang_codeCompleteAt(TU, filename, line, column, |
| 2047 | unsaved_files, num_unsaved_files, |
| 2048 | completionOptions); |
| 2049 | if (!results) { |
| 2050 | fprintf(stderr, "Unable to perform code completion!\n"); |
Daniel Dunbar | 2de41c9 | 2010-08-19 23:44:06 +0000 | [diff] [blame] | 2051 | return 1; |
| 2052 | } |
Douglas Gregor | 32be4a5 | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2053 | if (I != Repeats-1) |
| 2054 | clang_disposeCodeCompleteResults(results); |
| 2055 | } |
Douglas Gregor | 936ea3b | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 2056 | |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 2057 | if (results) { |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 2058 | unsigned i, n = results->NumResults, containerIsIncomplete = 0; |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 2059 | unsigned long long contexts; |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 2060 | enum CXCursorKind containerKind; |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 2061 | CXString objCSelector; |
| 2062 | const char *selectorString; |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 2063 | if (!timing_only) { |
| 2064 | /* Sort the code-completion results based on the typed text. */ |
| 2065 | clang_sortCodeCompletionResults(results->Results, results->NumResults); |
| 2066 | |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 2067 | for (i = 0; i != n; ++i) |
| 2068 | print_completion_result(results->Results + i, stdout); |
Douglas Gregor | 1e5e668 | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 2069 | } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2070 | n = clang_codeCompleteGetNumDiagnostics(results); |
| 2071 | for (i = 0; i != n; ++i) { |
| 2072 | CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); |
| 2073 | PrintDiagnostic(diag); |
| 2074 | clang_disposeDiagnostic(diag); |
| 2075 | } |
Douglas Gregor | 3da626b | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 2076 | |
| 2077 | contexts = clang_codeCompleteGetContexts(results); |
| 2078 | print_completion_contexts(contexts, stdout); |
| 2079 | |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 2080 | containerKind = clang_codeCompleteGetContainerKind(results, |
| 2081 | &containerIsIncomplete); |
Douglas Gregor | e081a61 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 2082 | |
| 2083 | if (containerKind != CXCursor_InvalidCode) { |
| 2084 | /* We have found a container */ |
| 2085 | CXString containerUSR, containerKindSpelling; |
| 2086 | containerKindSpelling = clang_getCursorKindSpelling(containerKind); |
| 2087 | printf("Container Kind: %s\n", clang_getCString(containerKindSpelling)); |
| 2088 | clang_disposeString(containerKindSpelling); |
| 2089 | |
| 2090 | if (containerIsIncomplete) { |
| 2091 | printf("Container is incomplete\n"); |
| 2092 | } |
| 2093 | else { |
| 2094 | printf("Container is complete\n"); |
| 2095 | } |
| 2096 | |
| 2097 | containerUSR = clang_codeCompleteGetContainerUSR(results); |
| 2098 | printf("Container USR: %s\n", clang_getCString(containerUSR)); |
| 2099 | clang_disposeString(containerUSR); |
| 2100 | } |
| 2101 | |
Douglas Gregor | 0a47d69 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 2102 | objCSelector = clang_codeCompleteGetObjCSelector(results); |
| 2103 | selectorString = clang_getCString(objCSelector); |
| 2104 | if (selectorString && strlen(selectorString) > 0) { |
| 2105 | printf("Objective-C selector: %s\n", selectorString); |
| 2106 | } |
| 2107 | clang_disposeString(objCSelector); |
| 2108 | |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 2109 | clang_disposeCodeCompleteResults(results); |
| 2110 | } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2111 | clang_disposeTranslationUnit(TU); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2112 | clang_disposeIndex(CIdx); |
| 2113 | free(filename); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2114 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 2115 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 2116 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2117 | return 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2120 | typedef struct { |
| 2121 | char *filename; |
| 2122 | unsigned line; |
| 2123 | unsigned column; |
| 2124 | } CursorSourceLocation; |
| 2125 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2126 | static int inspect_cursor_at(int argc, const char **argv) { |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2127 | CXIndex CIdx; |
| 2128 | int errorCode; |
| 2129 | struct CXUnsavedFile *unsaved_files = 0; |
| 2130 | int num_unsaved_files = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2131 | enum CXErrorCode Err; |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2132 | CXTranslationUnit TU; |
| 2133 | CXCursor Cursor; |
| 2134 | CursorSourceLocation *Locations = 0; |
| 2135 | unsigned NumLocations = 0, Loc; |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2136 | unsigned Repeats = 1; |
Douglas Gregor | bdc4b36 | 2010-11-30 06:04:54 +0000 | [diff] [blame] | 2137 | unsigned I; |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2138 | |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2139 | /* Count the number of locations. */ |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2140 | while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) |
| 2141 | ++NumLocations; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2142 | |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2143 | /* Parse the locations. */ |
| 2144 | assert(NumLocations > 0 && "Unable to count locations?"); |
| 2145 | Locations = (CursorSourceLocation *)malloc( |
| 2146 | NumLocations * sizeof(CursorSourceLocation)); |
| 2147 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 2148 | const char *input = argv[Loc + 1] + strlen("-cursor-at="); |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2149 | if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| 2150 | &Locations[Loc].line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2151 | &Locations[Loc].column, 0, 0))) |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2152 | return errorCode; |
| 2153 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2154 | |
| 2155 | if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2156 | &num_unsaved_files)) |
| 2157 | return -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2159 | if (getenv("CINDEXTEST_EDITING")) |
| 2160 | Repeats = 5; |
| 2161 | |
| 2162 | /* Parse the translation unit. When we're testing clang_getCursor() after |
| 2163 | reparsing, don't remap unsaved files until the second parse. */ |
| 2164 | CIdx = clang_createIndex(1, 1); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2165 | Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1], |
| 2166 | argv + num_unsaved_files + 1 + NumLocations, |
| 2167 | argc - num_unsaved_files - 2 - NumLocations, |
| 2168 | unsaved_files, |
| 2169 | Repeats > 1? 0 : num_unsaved_files, |
| 2170 | getDefaultParsingOptions(), &TU); |
| 2171 | if (Err != CXError_Success) { |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2172 | fprintf(stderr, "unable to parse input\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2173 | describeLibclangFailure(Err); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2174 | return -1; |
| 2175 | } |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2176 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2177 | if (checkForErrors(TU) != 0) |
| 2178 | return -1; |
| 2179 | |
Douglas Gregor | bdc4b36 | 2010-11-30 06:04:54 +0000 | [diff] [blame] | 2180 | for (I = 0; I != Repeats; ++I) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2181 | if (Repeats > 1) { |
| 2182 | Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 2183 | clang_defaultReparseOptions(TU)); |
| 2184 | if (Err != CXError_Success) { |
| 2185 | describeLibclangFailure(Err); |
| 2186 | clang_disposeTranslationUnit(TU); |
| 2187 | return 1; |
| 2188 | } |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2189 | } |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2190 | |
| 2191 | if (checkForErrors(TU) != 0) |
| 2192 | return -1; |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2193 | |
| 2194 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 2195 | CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| 2196 | if (!file) |
| 2197 | continue; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2198 | |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2199 | Cursor = clang_getCursor(TU, |
| 2200 | clang_getLocation(TU, file, Locations[Loc].line, |
| 2201 | Locations[Loc].column)); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2202 | |
| 2203 | if (checkForErrors(TU) != 0) |
| 2204 | return -1; |
| 2205 | |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2206 | if (I + 1 == Repeats) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2207 | CXCompletionString completionString = clang_getCursorCompletionString( |
| 2208 | Cursor); |
| 2209 | CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); |
Argyrios Kyrtzidis | 66373dd | 2012-03-30 00:19:05 +0000 | [diff] [blame] | 2210 | CXString Spelling; |
| 2211 | const char *cspell; |
| 2212 | unsigned line, column; |
| 2213 | clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); |
| 2214 | printf("%d:%d ", line, column); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2215 | PrintCursor(Cursor, NULL); |
Argyrios Kyrtzidis | 66373dd | 2012-03-30 00:19:05 +0000 | [diff] [blame] | 2216 | PrintCursorExtent(Cursor); |
| 2217 | Spelling = clang_getCursorSpelling(Cursor); |
| 2218 | cspell = clang_getCString(Spelling); |
Argyrios Kyrtzidis | ba1da14 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 2219 | if (cspell && strlen(cspell) != 0) { |
| 2220 | unsigned pieceIndex; |
Argyrios Kyrtzidis | ba1da14 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 2221 | printf(" Spelling=%s (", cspell); |
| 2222 | for (pieceIndex = 0; ; ++pieceIndex) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2223 | CXSourceRange range = |
| 2224 | clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); |
Argyrios Kyrtzidis | ba1da14 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 2225 | if (clang_Range_isNull(range)) |
| 2226 | break; |
| 2227 | PrintRange(range, 0); |
| 2228 | } |
| 2229 | printf(")"); |
| 2230 | } |
Argyrios Kyrtzidis | 66373dd | 2012-03-30 00:19:05 +0000 | [diff] [blame] | 2231 | clang_disposeString(Spelling); |
Argyrios Kyrtzidis | 34ebe1e | 2012-03-30 22:15:48 +0000 | [diff] [blame] | 2232 | if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2233 | printf(" Selector index=%d", |
| 2234 | clang_Cursor_getObjCSelectorIndex(Cursor)); |
Argyrios Kyrtzidis | f39a7ae | 2012-07-02 23:54:36 +0000 | [diff] [blame] | 2235 | if (clang_Cursor_isDynamicCall(Cursor)) |
| 2236 | printf(" Dynamic-call"); |
Argyrios Kyrtzidis | e4a990f | 2012-11-01 02:01:34 +0000 | [diff] [blame] | 2237 | if (Cursor.kind == CXCursor_ObjCMessageExpr) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2238 | CXType T = clang_Cursor_getReceiverType(Cursor); |
| 2239 | CXString S = clang_getTypeKindSpelling(T.kind); |
Argyrios Kyrtzidis | e4a990f | 2012-11-01 02:01:34 +0000 | [diff] [blame] | 2240 | printf(" Receiver-type=%s", clang_getCString(S)); |
| 2241 | clang_disposeString(S); |
| 2242 | } |
Argyrios Kyrtzidis | f39a7ae | 2012-07-02 23:54:36 +0000 | [diff] [blame] | 2243 | |
Argyrios Kyrtzidis | 5d04b1a | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2244 | { |
| 2245 | CXModule mod = clang_Cursor_getModule(Cursor); |
Argyrios Kyrtzidis | e858e66 | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 2246 | CXFile astFile; |
| 2247 | CXString name, astFilename; |
Argyrios Kyrtzidis | 5d04b1a | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2248 | unsigned i, numHeaders; |
| 2249 | if (mod) { |
Argyrios Kyrtzidis | e858e66 | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 2250 | astFile = clang_Module_getASTFile(mod); |
| 2251 | astFilename = clang_getFileName(astFile); |
Argyrios Kyrtzidis | 5d04b1a | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2252 | name = clang_Module_getFullName(mod); |
Argyrios Kyrtzidis | c1d2239 | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 2253 | numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2254 | printf(" ModuleName=%s (%s) system=%d Headers(%d):", |
Argyrios Kyrtzidis | e858e66 | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 2255 | clang_getCString(name), clang_getCString(astFilename), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2256 | clang_Module_isSystem(mod), numHeaders); |
Argyrios Kyrtzidis | 5d04b1a | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2257 | clang_disposeString(name); |
Argyrios Kyrtzidis | e858e66 | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 2258 | clang_disposeString(astFilename); |
Argyrios Kyrtzidis | 5d04b1a | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2259 | for (i = 0; i < numHeaders; ++i) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2260 | CXFile file = clang_Module_getTopLevelHeader(TU, mod, i); |
| 2261 | CXString filename = clang_getFileName(file); |
Argyrios Kyrtzidis | 5d04b1a | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2262 | printf("\n%s", clang_getCString(filename)); |
| 2263 | clang_disposeString(filename); |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
Douglas Gregor | 8fa0a80 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 2268 | if (completionString != NULL) { |
| 2269 | printf("\nCompletion string: "); |
| 2270 | print_completion_string(completionString, stdout); |
| 2271 | } |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2272 | printf("\n"); |
| 2273 | free(Locations[Loc].filename); |
| 2274 | } |
| 2275 | } |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2276 | } |
Douglas Gregor | 8e08dec | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2277 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2278 | PrintDiagnostics(TU); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2279 | clang_disposeTranslationUnit(TU); |
| 2280 | clang_disposeIndex(CIdx); |
| 2281 | free(Locations); |
| 2282 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2286 | static enum CXVisitorResult findFileRefsVisit(void *context, |
| 2287 | CXCursor cursor, CXSourceRange range) { |
| 2288 | if (clang_Range_isNull(range)) |
| 2289 | return CXVisit_Continue; |
| 2290 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2291 | PrintCursor(cursor, NULL); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2292 | PrintRange(range, ""); |
| 2293 | printf("\n"); |
| 2294 | return CXVisit_Continue; |
| 2295 | } |
| 2296 | |
| 2297 | static int find_file_refs_at(int argc, const char **argv) { |
| 2298 | CXIndex CIdx; |
| 2299 | int errorCode; |
| 2300 | struct CXUnsavedFile *unsaved_files = 0; |
| 2301 | int num_unsaved_files = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2302 | enum CXErrorCode Err; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2303 | CXTranslationUnit TU; |
| 2304 | CXCursor Cursor; |
| 2305 | CursorSourceLocation *Locations = 0; |
| 2306 | unsigned NumLocations = 0, Loc; |
| 2307 | unsigned Repeats = 1; |
| 2308 | unsigned I; |
| 2309 | |
| 2310 | /* Count the number of locations. */ |
| 2311 | while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1]) |
| 2312 | ++NumLocations; |
| 2313 | |
| 2314 | /* Parse the locations. */ |
| 2315 | assert(NumLocations > 0 && "Unable to count locations?"); |
| 2316 | Locations = (CursorSourceLocation *)malloc( |
| 2317 | NumLocations * sizeof(CursorSourceLocation)); |
| 2318 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 2319 | const char *input = argv[Loc + 1] + strlen("-file-refs-at="); |
| 2320 | if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| 2321 | &Locations[Loc].line, |
| 2322 | &Locations[Loc].column, 0, 0))) |
| 2323 | return errorCode; |
| 2324 | } |
| 2325 | |
| 2326 | if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
| 2327 | &num_unsaved_files)) |
| 2328 | return -1; |
| 2329 | |
| 2330 | if (getenv("CINDEXTEST_EDITING")) |
| 2331 | Repeats = 5; |
| 2332 | |
| 2333 | /* Parse the translation unit. When we're testing clang_getCursor() after |
| 2334 | reparsing, don't remap unsaved files until the second parse. */ |
| 2335 | CIdx = clang_createIndex(1, 1); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2336 | Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1], |
| 2337 | argv + num_unsaved_files + 1 + NumLocations, |
| 2338 | argc - num_unsaved_files - 2 - NumLocations, |
| 2339 | unsaved_files, |
| 2340 | Repeats > 1? 0 : num_unsaved_files, |
| 2341 | getDefaultParsingOptions(), &TU); |
| 2342 | if (Err != CXError_Success) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2343 | fprintf(stderr, "unable to parse input\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2344 | describeLibclangFailure(Err); |
| 2345 | clang_disposeTranslationUnit(TU); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2346 | return -1; |
| 2347 | } |
| 2348 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2349 | if (checkForErrors(TU) != 0) |
| 2350 | return -1; |
| 2351 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2352 | for (I = 0; I != Repeats; ++I) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2353 | if (Repeats > 1) { |
| 2354 | Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 2355 | clang_defaultReparseOptions(TU)); |
| 2356 | if (Err != CXError_Success) { |
| 2357 | describeLibclangFailure(Err); |
| 2358 | clang_disposeTranslationUnit(TU); |
| 2359 | return 1; |
| 2360 | } |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2361 | } |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2362 | |
| 2363 | if (checkForErrors(TU) != 0) |
| 2364 | return -1; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2365 | |
| 2366 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 2367 | CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| 2368 | if (!file) |
| 2369 | continue; |
| 2370 | |
| 2371 | Cursor = clang_getCursor(TU, |
| 2372 | clang_getLocation(TU, file, Locations[Loc].line, |
| 2373 | Locations[Loc].column)); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2374 | |
| 2375 | if (checkForErrors(TU) != 0) |
| 2376 | return -1; |
| 2377 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2378 | if (I + 1 == Repeats) { |
Erik Verbruggen | 26fc0f9 | 2011-10-06 11:38:08 +0000 | [diff] [blame] | 2379 | CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit }; |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2380 | PrintCursor(Cursor, NULL); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2381 | printf("\n"); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2382 | clang_findReferencesInFile(Cursor, file, visitor); |
| 2383 | free(Locations[Loc].filename); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2384 | |
| 2385 | if (checkForErrors(TU) != 0) |
| 2386 | return -1; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2387 | } |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | PrintDiagnostics(TU); |
| 2392 | clang_disposeTranslationUnit(TU); |
| 2393 | clang_disposeIndex(CIdx); |
| 2394 | free(Locations); |
| 2395 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 2396 | return 0; |
| 2397 | } |
| 2398 | |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2399 | static enum CXVisitorResult findFileIncludesVisit(void *context, |
| 2400 | CXCursor cursor, CXSourceRange range) { |
| 2401 | PrintCursor(cursor, NULL); |
| 2402 | PrintRange(range, ""); |
| 2403 | printf("\n"); |
| 2404 | return CXVisit_Continue; |
| 2405 | } |
| 2406 | |
| 2407 | static int find_file_includes_in(int argc, const char **argv) { |
| 2408 | CXIndex CIdx; |
| 2409 | struct CXUnsavedFile *unsaved_files = 0; |
| 2410 | int num_unsaved_files = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2411 | enum CXErrorCode Err; |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2412 | CXTranslationUnit TU; |
| 2413 | const char **Filenames = 0; |
| 2414 | unsigned NumFilenames = 0; |
| 2415 | unsigned Repeats = 1; |
| 2416 | unsigned I, FI; |
| 2417 | |
| 2418 | /* Count the number of locations. */ |
| 2419 | while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1]) |
| 2420 | ++NumFilenames; |
| 2421 | |
| 2422 | /* Parse the locations. */ |
| 2423 | assert(NumFilenames > 0 && "Unable to count filenames?"); |
| 2424 | Filenames = (const char **)malloc(NumFilenames * sizeof(const char *)); |
| 2425 | for (I = 0; I < NumFilenames; ++I) { |
| 2426 | const char *input = argv[I + 1] + strlen("-file-includes-in="); |
| 2427 | /* Copy the file name. */ |
| 2428 | Filenames[I] = input; |
| 2429 | } |
| 2430 | |
| 2431 | if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files, |
| 2432 | &num_unsaved_files)) |
| 2433 | return -1; |
| 2434 | |
| 2435 | if (getenv("CINDEXTEST_EDITING")) |
| 2436 | Repeats = 2; |
| 2437 | |
| 2438 | /* Parse the translation unit. When we're testing clang_getCursor() after |
| 2439 | reparsing, don't remap unsaved files until the second parse. */ |
| 2440 | CIdx = clang_createIndex(1, 1); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2441 | Err = clang_parseTranslationUnit2( |
| 2442 | CIdx, argv[argc - 1], |
| 2443 | argv + num_unsaved_files + 1 + NumFilenames, |
| 2444 | argc - num_unsaved_files - 2 - NumFilenames, |
| 2445 | unsaved_files, |
| 2446 | Repeats > 1 ? 0 : num_unsaved_files, getDefaultParsingOptions(), &TU); |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2447 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2448 | if (Err != CXError_Success) { |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2449 | fprintf(stderr, "unable to parse input\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2450 | describeLibclangFailure(Err); |
| 2451 | clang_disposeTranslationUnit(TU); |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2452 | return -1; |
| 2453 | } |
| 2454 | |
| 2455 | if (checkForErrors(TU) != 0) |
| 2456 | return -1; |
| 2457 | |
| 2458 | for (I = 0; I != Repeats; ++I) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2459 | if (Repeats > 1) { |
| 2460 | Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 2461 | clang_defaultReparseOptions(TU)); |
| 2462 | if (Err != CXError_Success) { |
| 2463 | describeLibclangFailure(Err); |
| 2464 | clang_disposeTranslationUnit(TU); |
| 2465 | return 1; |
| 2466 | } |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | if (checkForErrors(TU) != 0) |
| 2470 | return -1; |
| 2471 | |
| 2472 | for (FI = 0; FI < NumFilenames; ++FI) { |
| 2473 | CXFile file = clang_getFile(TU, Filenames[FI]); |
| 2474 | if (!file) |
| 2475 | continue; |
| 2476 | |
| 2477 | if (checkForErrors(TU) != 0) |
| 2478 | return -1; |
| 2479 | |
| 2480 | if (I + 1 == Repeats) { |
| 2481 | CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit }; |
| 2482 | clang_findIncludesInFile(TU, file, visitor); |
| 2483 | |
| 2484 | if (checkForErrors(TU) != 0) |
| 2485 | return -1; |
| 2486 | } |
| 2487 | } |
| 2488 | } |
| 2489 | |
| 2490 | PrintDiagnostics(TU); |
| 2491 | clang_disposeTranslationUnit(TU); |
| 2492 | clang_disposeIndex(CIdx); |
Argyrios Kyrtzidis | 5256c1f | 2013-03-11 16:03:17 +0000 | [diff] [blame] | 2493 | free((void *)Filenames); |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2494 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 2498 | #define MAX_IMPORTED_ASTFILES 200 |
| 2499 | |
| 2500 | typedef struct { |
| 2501 | char **filenames; |
| 2502 | unsigned num_files; |
| 2503 | } ImportedASTFilesData; |
| 2504 | |
| 2505 | static ImportedASTFilesData *importedASTs_create() { |
| 2506 | ImportedASTFilesData *p; |
| 2507 | p = malloc(sizeof(ImportedASTFilesData)); |
| 2508 | p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *)); |
| 2509 | p->num_files = 0; |
| 2510 | return p; |
| 2511 | } |
| 2512 | |
| 2513 | static void importedASTs_dispose(ImportedASTFilesData *p) { |
| 2514 | unsigned i; |
| 2515 | if (!p) |
| 2516 | return; |
| 2517 | |
| 2518 | for (i = 0; i < p->num_files; ++i) |
| 2519 | free(p->filenames[i]); |
| 2520 | free(p->filenames); |
| 2521 | free(p); |
| 2522 | } |
| 2523 | |
| 2524 | static void importedASTS_insert(ImportedASTFilesData *p, const char *file) { |
| 2525 | unsigned i; |
| 2526 | assert(p && file); |
| 2527 | for (i = 0; i < p->num_files; ++i) |
| 2528 | if (strcmp(file, p->filenames[i]) == 0) |
| 2529 | return; |
| 2530 | assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES); |
| 2531 | p->filenames[p->num_files++] = strdup(file); |
| 2532 | } |
| 2533 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2534 | typedef struct IndexDataStringList_ { |
| 2535 | struct IndexDataStringList_ *next; |
| 2536 | char data[1]; /* Dynamically sized. */ |
| 2537 | } IndexDataStringList; |
| 2538 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2539 | typedef struct { |
| 2540 | const char *check_prefix; |
| 2541 | int first_check_printed; |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2542 | int fail_for_error; |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 2543 | int abort; |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2544 | const char *main_filename; |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 2545 | ImportedASTFilesData *importedASTs; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2546 | IndexDataStringList *strings; |
| 2547 | CXTranslationUnit TU; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2548 | } IndexData; |
| 2549 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2550 | static void free_client_data(IndexData *index_data) { |
| 2551 | IndexDataStringList *node = index_data->strings; |
| 2552 | while (node) { |
| 2553 | IndexDataStringList *next = node->next; |
| 2554 | free(node); |
| 2555 | node = next; |
| 2556 | } |
| 2557 | index_data->strings = NULL; |
| 2558 | } |
| 2559 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2560 | static void printCheck(IndexData *data) { |
| 2561 | if (data->check_prefix) { |
| 2562 | if (data->first_check_printed) { |
| 2563 | printf("// %s-NEXT: ", data->check_prefix); |
| 2564 | } else { |
| 2565 | printf("// %s : ", data->check_prefix); |
| 2566 | data->first_check_printed = 1; |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2571 | static void printCXIndexFile(CXIdxClientFile file) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2572 | CXString filename = clang_getFileName((CXFile)file); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2573 | printf("%s", clang_getCString(filename)); |
| 2574 | clang_disposeString(filename); |
| 2575 | } |
| 2576 | |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2577 | static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) { |
| 2578 | IndexData *index_data; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2579 | CXString filename; |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2580 | const char *cname; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2581 | CXIdxClientFile file; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2582 | unsigned line, column; |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2583 | int isMainFile; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2584 | |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2585 | index_data = (IndexData *)client_data; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2586 | clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); |
| 2587 | if (line == 0) { |
Argyrios Kyrtzidis | 8003fd6 | 2012-10-11 19:00:44 +0000 | [diff] [blame] | 2588 | printf("<invalid>"); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2589 | return; |
| 2590 | } |
Argyrios Kyrtzidis | c2be04e | 2011-12-13 18:47:35 +0000 | [diff] [blame] | 2591 | if (!file) { |
| 2592 | printf("<no idxfile>"); |
| 2593 | return; |
| 2594 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2595 | filename = clang_getFileName((CXFile)file); |
| 2596 | cname = clang_getCString(filename); |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2597 | if (strcmp(cname, index_data->main_filename) == 0) |
| 2598 | isMainFile = 1; |
| 2599 | else |
| 2600 | isMainFile = 0; |
| 2601 | clang_disposeString(filename); |
| 2602 | |
| 2603 | if (!isMainFile) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2604 | printCXIndexFile(file); |
| 2605 | printf(":"); |
| 2606 | } |
| 2607 | printf("%d:%d", line, column); |
| 2608 | } |
| 2609 | |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2610 | static unsigned digitCount(unsigned val) { |
| 2611 | unsigned c = 1; |
| 2612 | while (1) { |
| 2613 | if (val < 10) |
| 2614 | return c; |
| 2615 | ++c; |
| 2616 | val /= 10; |
| 2617 | } |
| 2618 | } |
| 2619 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2620 | static CXIdxClientContainer makeClientContainer(CXClientData *client_data, |
| 2621 | const CXIdxEntityInfo *info, |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2622 | CXIdxLoc loc) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2623 | IndexData *index_data; |
| 2624 | IndexDataStringList *node; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2625 | const char *name; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2626 | char *newStr; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2627 | CXIdxClientFile file; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2628 | unsigned line, column; |
| 2629 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2630 | name = info->name; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2631 | if (!name) |
| 2632 | name = "<anon-tag>"; |
| 2633 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2634 | clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2635 | |
| 2636 | node = |
| 2637 | (IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) + |
| 2638 | digitCount(line) + digitCount(column) + 2); |
| 2639 | newStr = node->data; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2640 | sprintf(newStr, "%s:%d:%d", name, line, column); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2641 | |
| 2642 | /* Remember string so it can be freed later. */ |
| 2643 | index_data = (IndexData *)client_data; |
| 2644 | node->next = index_data->strings; |
| 2645 | index_data->strings = node; |
| 2646 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2647 | return (CXIdxClientContainer)newStr; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2650 | static void printCXIndexContainer(const CXIdxContainerInfo *info) { |
| 2651 | CXIdxClientContainer container; |
| 2652 | container = clang_index_getClientContainer(info); |
Argyrios Kyrtzidis | 3e340a6 | 2011-11-16 02:35:05 +0000 | [diff] [blame] | 2653 | if (!container) |
| 2654 | printf("[<<NULL>>]"); |
| 2655 | else |
| 2656 | printf("[%s]", (const char *)container); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2659 | static const char *getEntityKindString(CXIdxEntityKind kind) { |
| 2660 | switch (kind) { |
| 2661 | case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>"; |
| 2662 | case CXIdxEntity_Typedef: return "typedef"; |
| 2663 | case CXIdxEntity_Function: return "function"; |
| 2664 | case CXIdxEntity_Variable: return "variable"; |
| 2665 | case CXIdxEntity_Field: return "field"; |
| 2666 | case CXIdxEntity_EnumConstant: return "enumerator"; |
| 2667 | case CXIdxEntity_ObjCClass: return "objc-class"; |
| 2668 | case CXIdxEntity_ObjCProtocol: return "objc-protocol"; |
| 2669 | case CXIdxEntity_ObjCCategory: return "objc-category"; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2670 | case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method"; |
| 2671 | case CXIdxEntity_ObjCClassMethod: return "objc-class-method"; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2672 | case CXIdxEntity_ObjCProperty: return "objc-property"; |
| 2673 | case CXIdxEntity_ObjCIvar: return "objc-ivar"; |
| 2674 | case CXIdxEntity_Enum: return "enum"; |
| 2675 | case CXIdxEntity_Struct: return "struct"; |
| 2676 | case CXIdxEntity_Union: return "union"; |
| 2677 | case CXIdxEntity_CXXClass: return "c++-class"; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2678 | case CXIdxEntity_CXXNamespace: return "namespace"; |
| 2679 | case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias"; |
| 2680 | case CXIdxEntity_CXXStaticVariable: return "c++-static-var"; |
| 2681 | case CXIdxEntity_CXXStaticMethod: return "c++-static-method"; |
| 2682 | case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method"; |
| 2683 | case CXIdxEntity_CXXConstructor: return "constructor"; |
| 2684 | case CXIdxEntity_CXXDestructor: return "destructor"; |
| 2685 | case CXIdxEntity_CXXConversionFunction: return "conversion-func"; |
| 2686 | case CXIdxEntity_CXXTypeAlias: return "type-alias"; |
David Blaikie | 35adca0 | 2012-08-31 21:55:26 +0000 | [diff] [blame] | 2687 | case CXIdxEntity_CXXInterface: return "c++-__interface"; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2688 | } |
| 2689 | assert(0 && "Garbage entity kind"); |
| 2690 | return 0; |
| 2691 | } |
| 2692 | |
| 2693 | static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) { |
| 2694 | switch (kind) { |
| 2695 | case CXIdxEntity_NonTemplate: return ""; |
| 2696 | case CXIdxEntity_Template: return "-template"; |
| 2697 | case CXIdxEntity_TemplatePartialSpecialization: |
| 2698 | return "-template-partial-spec"; |
| 2699 | case CXIdxEntity_TemplateSpecialization: return "-template-spec"; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2700 | } |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2701 | assert(0 && "Garbage entity kind"); |
| 2702 | return 0; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
Argyrios Kyrtzidis | 838d3c2 | 2011-12-07 20:44:12 +0000 | [diff] [blame] | 2705 | static const char *getEntityLanguageString(CXIdxEntityLanguage kind) { |
| 2706 | switch (kind) { |
| 2707 | case CXIdxEntityLang_None: return "<none>"; |
| 2708 | case CXIdxEntityLang_C: return "C"; |
| 2709 | case CXIdxEntityLang_ObjC: return "ObjC"; |
| 2710 | case CXIdxEntityLang_CXX: return "C++"; |
| 2711 | } |
| 2712 | assert(0 && "Garbage language kind"); |
| 2713 | return 0; |
| 2714 | } |
| 2715 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2716 | static void printEntityInfo(const char *cb, |
| 2717 | CXClientData client_data, |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2718 | const CXIdxEntityInfo *info) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2719 | const char *name; |
| 2720 | IndexData *index_data; |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 2721 | unsigned i; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2722 | index_data = (IndexData *)client_data; |
| 2723 | printCheck(index_data); |
| 2724 | |
Argyrios Kyrtzidis | c6b4a50 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 2725 | if (!info) { |
| 2726 | printf("%s: <<NULL>>", cb); |
| 2727 | return; |
| 2728 | } |
| 2729 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2730 | name = info->name; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2731 | if (!name) |
| 2732 | name = "<anon-tag>"; |
| 2733 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2734 | printf("%s: kind: %s%s", cb, getEntityKindString(info->kind), |
| 2735 | getEntityTemplateKindString(info->templateKind)); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2736 | printf(" | name: %s", name); |
| 2737 | printf(" | USR: %s", info->USR); |
Argyrios Kyrtzidis | c2be04e | 2011-12-13 18:47:35 +0000 | [diff] [blame] | 2738 | printf(" | lang: %s", getEntityLanguageString(info->lang)); |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 2739 | |
| 2740 | for (i = 0; i != info->numAttributes; ++i) { |
| 2741 | const CXIdxAttrInfo *Attr = info->attributes[i]; |
| 2742 | printf(" <attribute>: "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2743 | PrintCursor(Attr->cursor, NULL); |
Argyrios Kyrtzidis | 643d3ce | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 2744 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Argyrios Kyrtzidis | b526a87 | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2747 | static void printBaseClassInfo(CXClientData client_data, |
| 2748 | const CXIdxBaseClassInfo *info) { |
| 2749 | printEntityInfo(" <base>", client_data, info->base); |
| 2750 | printf(" | cursor: "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2751 | PrintCursor(info->cursor, NULL); |
Argyrios Kyrtzidis | b526a87 | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2752 | printf(" | loc: "); |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2753 | printCXIndexLoc(info->loc, client_data); |
Argyrios Kyrtzidis | b526a87 | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2756 | static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo, |
| 2757 | CXClientData client_data) { |
| 2758 | unsigned i; |
| 2759 | for (i = 0; i < ProtoInfo->numProtocols; ++i) { |
| 2760 | printEntityInfo(" <protocol>", client_data, |
| 2761 | ProtoInfo->protocols[i]->protocol); |
| 2762 | printf(" | cursor: "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2763 | PrintCursor(ProtoInfo->protocols[i]->cursor, NULL); |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2764 | printf(" | loc: "); |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2765 | printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data); |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2766 | printf("\n"); |
| 2767 | } |
| 2768 | } |
| 2769 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2770 | static void index_diagnostic(CXClientData client_data, |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 2771 | CXDiagnosticSet diagSet, void *reserved) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2772 | CXString str; |
| 2773 | const char *cstr; |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 2774 | unsigned numDiags, i; |
| 2775 | CXDiagnostic diag; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2776 | IndexData *index_data; |
| 2777 | index_data = (IndexData *)client_data; |
| 2778 | printCheck(index_data); |
| 2779 | |
Argyrios Kyrtzidis | 996e6e5 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 2780 | numDiags = clang_getNumDiagnosticsInSet(diagSet); |
| 2781 | for (i = 0; i != numDiags; ++i) { |
| 2782 | diag = clang_getDiagnosticInSet(diagSet, i); |
| 2783 | str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); |
| 2784 | cstr = clang_getCString(str); |
| 2785 | printf("[diagnostic]: %s\n", cstr); |
| 2786 | clang_disposeString(str); |
| 2787 | |
| 2788 | if (getenv("CINDEXTEST_FAILONERROR") && |
| 2789 | clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) { |
| 2790 | index_data->fail_for_error = 1; |
| 2791 | } |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2792 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2795 | static CXIdxClientFile index_enteredMainFile(CXClientData client_data, |
| 2796 | CXFile file, void *reserved) { |
| 2797 | IndexData *index_data; |
Argyrios Kyrtzidis | 62d7fea | 2012-03-15 18:48:52 +0000 | [diff] [blame] | 2798 | CXString filename; |
| 2799 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2800 | index_data = (IndexData *)client_data; |
| 2801 | printCheck(index_data); |
| 2802 | |
Argyrios Kyrtzidis | 62d7fea | 2012-03-15 18:48:52 +0000 | [diff] [blame] | 2803 | filename = clang_getFileName(file); |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2804 | index_data->main_filename = clang_getCString(filename); |
| 2805 | clang_disposeString(filename); |
| 2806 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2807 | printf("[enteredMainFile]: "); |
| 2808 | printCXIndexFile((CXIdxClientFile)file); |
| 2809 | printf("\n"); |
| 2810 | |
| 2811 | return (CXIdxClientFile)file; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2814 | static CXIdxClientFile index_ppIncludedFile(CXClientData client_data, |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2815 | const CXIdxIncludedFileInfo *info) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2816 | IndexData *index_data; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2817 | CXModule Mod; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2818 | index_data = (IndexData *)client_data; |
| 2819 | printCheck(index_data); |
| 2820 | |
Argyrios Kyrtzidis | 66042b3 | 2011-11-05 04:03:35 +0000 | [diff] [blame] | 2821 | printf("[ppIncludedFile]: "); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2822 | printCXIndexFile((CXIdxClientFile)info->file); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2823 | printf(" | name: \"%s\"", info->filename); |
| 2824 | printf(" | hash loc: "); |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2825 | printCXIndexLoc(info->hashLoc, client_data); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2826 | printf(" | isImport: %d | isAngled: %d | isModule: %d", |
Argyrios Kyrtzidis | 8d7a24e | 2012-10-18 00:17:05 +0000 | [diff] [blame] | 2827 | info->isImport, info->isAngled, info->isModuleImport); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2828 | |
| 2829 | Mod = clang_getModuleForFile(index_data->TU, (CXFile)info->file); |
| 2830 | if (Mod) { |
| 2831 | CXString str = clang_Module_getFullName(Mod); |
| 2832 | const char *cstr = clang_getCString(str); |
| 2833 | printf(" | module: %s", cstr); |
| 2834 | clang_disposeString(str); |
| 2835 | } |
| 2836 | |
| 2837 | printf("\n"); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2838 | |
| 2839 | return (CXIdxClientFile)info->file; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Argyrios Kyrtzidis | 2c3e05c | 2012-10-02 16:10:38 +0000 | [diff] [blame] | 2842 | static CXIdxClientFile index_importedASTFile(CXClientData client_data, |
| 2843 | const CXIdxImportedASTFileInfo *info) { |
| 2844 | IndexData *index_data; |
| 2845 | index_data = (IndexData *)client_data; |
| 2846 | printCheck(index_data); |
| 2847 | |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 2848 | if (index_data->importedASTs) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2849 | CXString filename = clang_getFileName(info->file); |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 2850 | importedASTS_insert(index_data->importedASTs, clang_getCString(filename)); |
| 2851 | clang_disposeString(filename); |
| 2852 | } |
| 2853 | |
Argyrios Kyrtzidis | 2c3e05c | 2012-10-02 16:10:38 +0000 | [diff] [blame] | 2854 | printf("[importedASTFile]: "); |
| 2855 | printCXIndexFile((CXIdxClientFile)info->file); |
Argyrios Kyrtzidis | 134d1e8a | 2012-10-05 00:22:40 +0000 | [diff] [blame] | 2856 | if (info->module) { |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2857 | CXString name = clang_Module_getFullName(info->module); |
Argyrios Kyrtzidis | 134d1e8a | 2012-10-05 00:22:40 +0000 | [diff] [blame] | 2858 | printf(" | loc: "); |
| 2859 | printCXIndexLoc(info->loc, client_data); |
| 2860 | printf(" | name: \"%s\"", clang_getCString(name)); |
| 2861 | printf(" | isImplicit: %d\n", info->isImplicit); |
| 2862 | clang_disposeString(name); |
Argyrios Kyrtzidis | 900ab95 | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2863 | } else { |
NAKAMURA Takumi | 3c5527e | 2012-10-12 14:25:52 +0000 | [diff] [blame] | 2864 | /* PCH file, the rest are not relevant. */ |
Argyrios Kyrtzidis | 900ab95 | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2865 | printf("\n"); |
Argyrios Kyrtzidis | 134d1e8a | 2012-10-05 00:22:40 +0000 | [diff] [blame] | 2866 | } |
Argyrios Kyrtzidis | 2c3e05c | 2012-10-02 16:10:38 +0000 | [diff] [blame] | 2867 | |
| 2868 | return (CXIdxClientFile)info->file; |
| 2869 | } |
| 2870 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2871 | static CXIdxClientContainer |
| 2872 | index_startedTranslationUnit(CXClientData client_data, void *reserved) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2873 | IndexData *index_data; |
| 2874 | index_data = (IndexData *)client_data; |
| 2875 | printCheck(index_data); |
| 2876 | |
Argyrios Kyrtzidis | 66042b3 | 2011-11-05 04:03:35 +0000 | [diff] [blame] | 2877 | printf("[startedTranslationUnit]\n"); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2878 | return (CXIdxClientContainer)"TU"; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2881 | static void index_indexDeclaration(CXClientData client_data, |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2882 | const CXIdxDeclInfo *info) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2883 | IndexData *index_data; |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2884 | const CXIdxObjCCategoryDeclInfo *CatInfo; |
| 2885 | const CXIdxObjCInterfaceDeclInfo *InterInfo; |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2886 | const CXIdxObjCProtocolRefListInfo *ProtoInfo; |
Argyrios Kyrtzidis | 792db26 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 2887 | const CXIdxObjCPropertyDeclInfo *PropInfo; |
Argyrios Kyrtzidis | b526a87 | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2888 | const CXIdxCXXClassDeclInfo *CXXClassInfo; |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2889 | unsigned i; |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2890 | index_data = (IndexData *)client_data; |
| 2891 | |
| 2892 | printEntityInfo("[indexDeclaration]", client_data, info->entityInfo); |
| 2893 | printf(" | cursor: "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2894 | PrintCursor(info->cursor, NULL); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2895 | printf(" | loc: "); |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2896 | printCXIndexLoc(info->loc, client_data); |
Argyrios Kyrtzidis | b1febb6 | 2011-12-07 20:44:19 +0000 | [diff] [blame] | 2897 | printf(" | semantic-container: "); |
| 2898 | printCXIndexContainer(info->semanticContainer); |
| 2899 | printf(" | lexical-container: "); |
| 2900 | printCXIndexContainer(info->lexicalContainer); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2901 | printf(" | isRedecl: %d", info->isRedeclaration); |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2902 | printf(" | isDef: %d", info->isDefinition); |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 2903 | if (info->flags & CXIdxDeclFlag_Skipped) { |
| 2904 | assert(!info->isContainer); |
| 2905 | printf(" | isContainer: skipped"); |
| 2906 | } else { |
| 2907 | printf(" | isContainer: %d", info->isContainer); |
| 2908 | } |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2909 | printf(" | isImplicit: %d\n", info->isImplicit); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2910 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2911 | for (i = 0; i != info->numAttributes; ++i) { |
NAKAMURA Takumi | 87adb0b | 2011-11-18 00:51:03 +0000 | [diff] [blame] | 2912 | const CXIdxAttrInfo *Attr = info->attributes[i]; |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2913 | printf(" <attribute>: "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2914 | PrintCursor(Attr->cursor, NULL); |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2915 | printf("\n"); |
| 2916 | } |
| 2917 | |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2918 | if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) { |
| 2919 | const char *kindName = 0; |
| 2920 | CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind; |
| 2921 | switch (K) { |
| 2922 | case CXIdxObjCContainer_ForwardRef: |
| 2923 | kindName = "forward-ref"; break; |
| 2924 | case CXIdxObjCContainer_Interface: |
| 2925 | kindName = "interface"; break; |
| 2926 | case CXIdxObjCContainer_Implementation: |
| 2927 | kindName = "implementation"; break; |
| 2928 | } |
| 2929 | printCheck(index_data); |
| 2930 | printf(" <ObjCContainerInfo>: kind: %s\n", kindName); |
| 2931 | } |
| 2932 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2933 | if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) { |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2934 | printEntityInfo(" <ObjCCategoryInfo>: class", client_data, |
| 2935 | CatInfo->objcClass); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 2936 | printf(" | cursor: "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2937 | PrintCursor(CatInfo->classCursor, NULL); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 2938 | printf(" | loc: "); |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2939 | printCXIndexLoc(CatInfo->classLoc, client_data); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2940 | printf("\n"); |
| 2941 | } |
| 2942 | |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2943 | if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) { |
| 2944 | if (InterInfo->superInfo) { |
Argyrios Kyrtzidis | b526a87 | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2945 | printBaseClassInfo(client_data, InterInfo->superInfo); |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2946 | printf("\n"); |
| 2947 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2950 | if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) { |
| 2951 | printProtocolList(ProtoInfo, client_data); |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2952 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2953 | |
Argyrios Kyrtzidis | 792db26 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 2954 | if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) { |
| 2955 | if (PropInfo->getter) { |
| 2956 | printEntityInfo(" <getter>", client_data, PropInfo->getter); |
| 2957 | printf("\n"); |
| 2958 | } |
| 2959 | if (PropInfo->setter) { |
| 2960 | printEntityInfo(" <setter>", client_data, PropInfo->setter); |
| 2961 | printf("\n"); |
| 2962 | } |
| 2963 | } |
| 2964 | |
Argyrios Kyrtzidis | b526a87 | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2965 | if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) { |
| 2966 | for (i = 0; i != CXXClassInfo->numBases; ++i) { |
| 2967 | printBaseClassInfo(client_data, CXXClassInfo->bases[i]); |
| 2968 | printf("\n"); |
| 2969 | } |
| 2970 | } |
| 2971 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2972 | if (info->declAsContainer) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2973 | clang_index_setClientContainer( |
| 2974 | info->declAsContainer, |
| 2975 | makeClientContainer(client_data, info->entityInfo, info->loc)); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | static void index_indexEntityReference(CXClientData client_data, |
Argyrios Kyrtzidis | 6ec43ad | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2979 | const CXIdxEntityRefInfo *info) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2980 | printEntityInfo("[indexEntityReference]", client_data, |
| 2981 | info->referencedEntity); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2982 | printf(" | cursor: "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2983 | PrintCursor(info->cursor, NULL); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2984 | printf(" | loc: "); |
Argyrios Kyrtzidis | 13c20a7 | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2985 | printCXIndexLoc(info->loc, client_data); |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2986 | printEntityInfo(" | <parent>:", client_data, info->parentEntity); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2987 | printf(" | container: "); |
| 2988 | printCXIndexContainer(info->container); |
Argyrios Kyrtzidis | c71d554 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2989 | printf(" | refkind: "); |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 2990 | switch (info->kind) { |
| 2991 | case CXIdxEntityRef_Direct: printf("direct"); break; |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2992 | case CXIdxEntityRef_Implicit: printf("implicit"); break; |
Argyrios Kyrtzidis | aca19be | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 2993 | } |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2994 | printf("\n"); |
| 2995 | } |
| 2996 | |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 2997 | static int index_abortQuery(CXClientData client_data, void *reserved) { |
| 2998 | IndexData *index_data; |
| 2999 | index_data = (IndexData *)client_data; |
| 3000 | return index_data->abort; |
| 3001 | } |
| 3002 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3003 | static IndexerCallbacks IndexCB = { |
Argyrios Kyrtzidis | 6f3ce97 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 3004 | index_abortQuery, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3005 | index_diagnostic, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 3006 | index_enteredMainFile, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3007 | index_ppIncludedFile, |
Argyrios Kyrtzidis | 2c3e05c | 2012-10-02 16:10:38 +0000 | [diff] [blame] | 3008 | index_importedASTFile, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3009 | index_startedTranslationUnit, |
Argyrios Kyrtzidis | dd93c59 | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 3010 | index_indexDeclaration, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3011 | index_indexEntityReference |
| 3012 | }; |
| 3013 | |
Argyrios Kyrtzidis | 2249074 | 2012-01-14 00:11:49 +0000 | [diff] [blame] | 3014 | static unsigned getIndexOptions(void) { |
| 3015 | unsigned index_opts; |
| 3016 | index_opts = 0; |
| 3017 | if (getenv("CINDEXTEST_SUPPRESSREFS")) |
| 3018 | index_opts |= CXIndexOpt_SuppressRedundantRefs; |
| 3019 | if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS")) |
| 3020 | index_opts |= CXIndexOpt_IndexFunctionLocalSymbols; |
Argyrios Kyrtzidis | 838eb7e | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 3021 | if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES")) |
| 3022 | index_opts |= CXIndexOpt_SkipParsedBodiesInSession; |
Argyrios Kyrtzidis | 2249074 | 2012-01-14 00:11:49 +0000 | [diff] [blame] | 3023 | |
| 3024 | return index_opts; |
| 3025 | } |
| 3026 | |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3027 | static int index_compile_args(int num_args, const char **args, |
| 3028 | CXIndexAction idxAction, |
| 3029 | ImportedASTFilesData *importedASTs, |
| 3030 | const char *check_prefix) { |
| 3031 | IndexData index_data; |
| 3032 | unsigned index_opts; |
| 3033 | int result; |
| 3034 | |
| 3035 | if (num_args == 0) { |
| 3036 | fprintf(stderr, "no compiler arguments\n"); |
| 3037 | return -1; |
| 3038 | } |
| 3039 | |
| 3040 | index_data.check_prefix = check_prefix; |
| 3041 | index_data.first_check_printed = 0; |
| 3042 | index_data.fail_for_error = 0; |
| 3043 | index_data.abort = 0; |
| 3044 | index_data.main_filename = ""; |
| 3045 | index_data.importedASTs = importedASTs; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3046 | index_data.strings = NULL; |
| 3047 | index_data.TU = NULL; |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3048 | |
| 3049 | index_opts = getIndexOptions(); |
| 3050 | result = clang_indexSourceFile(idxAction, &index_data, |
| 3051 | &IndexCB,sizeof(IndexCB), index_opts, |
| 3052 | 0, args, num_args, 0, 0, 0, |
| 3053 | getDefaultParsingOptions()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3054 | if (result != CXError_Success) |
| 3055 | describeLibclangFailure(result); |
| 3056 | |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3057 | if (index_data.fail_for_error) |
| 3058 | result = -1; |
| 3059 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3060 | free_client_data(&index_data); |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3061 | return result; |
| 3062 | } |
| 3063 | |
| 3064 | static int index_ast_file(const char *ast_file, |
| 3065 | CXIndex Idx, |
| 3066 | CXIndexAction idxAction, |
| 3067 | ImportedASTFilesData *importedASTs, |
| 3068 | const char *check_prefix) { |
| 3069 | CXTranslationUnit TU; |
| 3070 | IndexData index_data; |
| 3071 | unsigned index_opts; |
| 3072 | int result; |
| 3073 | |
| 3074 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 3075 | return -1; |
| 3076 | |
| 3077 | index_data.check_prefix = check_prefix; |
| 3078 | index_data.first_check_printed = 0; |
| 3079 | index_data.fail_for_error = 0; |
| 3080 | index_data.abort = 0; |
| 3081 | index_data.main_filename = ""; |
| 3082 | index_data.importedASTs = importedASTs; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3083 | index_data.strings = NULL; |
| 3084 | index_data.TU = TU; |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3085 | |
| 3086 | index_opts = getIndexOptions(); |
| 3087 | result = clang_indexTranslationUnit(idxAction, &index_data, |
| 3088 | &IndexCB,sizeof(IndexCB), |
| 3089 | index_opts, TU); |
| 3090 | if (index_data.fail_for_error) |
| 3091 | result = -1; |
| 3092 | |
| 3093 | clang_disposeTranslationUnit(TU); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3094 | free_client_data(&index_data); |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3095 | return result; |
| 3096 | } |
| 3097 | |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3098 | static int index_file(int argc, const char **argv, int full) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3099 | const char *check_prefix; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3100 | CXIndex Idx; |
| 3101 | CXIndexAction idxAction; |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3102 | ImportedASTFilesData *importedASTs; |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3103 | int result; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3104 | |
| 3105 | check_prefix = 0; |
| 3106 | if (argc > 0) { |
| 3107 | if (strstr(argv[0], "-check-prefix=") == argv[0]) { |
| 3108 | check_prefix = argv[0] + strlen("-check-prefix="); |
| 3109 | ++argv; |
| 3110 | --argc; |
| 3111 | } |
| 3112 | } |
| 3113 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3114 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
Stefanus Du Toit | fc09336 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 3115 | /* displayDiagnostics=*/1))) { |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3116 | fprintf(stderr, "Could not create Index\n"); |
| 3117 | return 1; |
| 3118 | } |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3119 | idxAction = clang_IndexAction_create(Idx); |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3120 | importedASTs = 0; |
| 3121 | if (full) |
| 3122 | importedASTs = importedASTs_create(); |
| 3123 | |
| 3124 | result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix); |
| 3125 | if (result != 0) |
| 3126 | goto finished; |
| 3127 | |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3128 | if (full) { |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3129 | unsigned i; |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3130 | for (i = 0; i < importedASTs->num_files && result == 0; ++i) { |
| 3131 | result = index_ast_file(importedASTs->filenames[i], Idx, idxAction, |
| 3132 | importedASTs, check_prefix); |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3133 | } |
| 3134 | } |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3135 | |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3136 | finished: |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3137 | importedASTs_dispose(importedASTs); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3138 | clang_IndexAction_dispose(idxAction); |
| 3139 | clang_disposeIndex(Idx); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3140 | return result; |
| 3141 | } |
| 3142 | |
| 3143 | static int index_tu(int argc, const char **argv) { |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3144 | const char *check_prefix; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3145 | CXIndex Idx; |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3146 | CXIndexAction idxAction; |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3147 | int result; |
| 3148 | |
| 3149 | check_prefix = 0; |
| 3150 | if (argc > 0) { |
| 3151 | if (strstr(argv[0], "-check-prefix=") == argv[0]) { |
| 3152 | check_prefix = argv[0] + strlen("-check-prefix="); |
| 3153 | ++argv; |
| 3154 | --argc; |
| 3155 | } |
| 3156 | } |
| 3157 | |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3158 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
Stefanus Du Toit | fc09336 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 3159 | /* displayDiagnostics=*/1))) { |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3160 | fprintf(stderr, "Could not create Index\n"); |
| 3161 | return 1; |
| 3162 | } |
| 3163 | idxAction = clang_IndexAction_create(Idx); |
| 3164 | |
| 3165 | result = index_ast_file(argv[0], Idx, idxAction, |
| 3166 | /*importedASTs=*/0, check_prefix); |
| 3167 | |
| 3168 | clang_IndexAction_dispose(idxAction); |
| 3169 | clang_disposeIndex(Idx); |
| 3170 | return result; |
| 3171 | } |
| 3172 | |
| 3173 | static int index_compile_db(int argc, const char **argv) { |
| 3174 | const char *check_prefix; |
| 3175 | CXIndex Idx; |
| 3176 | CXIndexAction idxAction; |
| 3177 | int errorCode = 0; |
| 3178 | |
| 3179 | check_prefix = 0; |
| 3180 | if (argc > 0) { |
| 3181 | if (strstr(argv[0], "-check-prefix=") == argv[0]) { |
| 3182 | check_prefix = argv[0] + strlen("-check-prefix="); |
| 3183 | ++argv; |
| 3184 | --argc; |
| 3185 | } |
| 3186 | } |
| 3187 | |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3188 | if (argc == 0) { |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3189 | fprintf(stderr, "no compilation database\n"); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3190 | return -1; |
| 3191 | } |
| 3192 | |
| 3193 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
Stefanus Du Toit | fc09336 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 3194 | /* displayDiagnostics=*/1))) { |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3195 | fprintf(stderr, "Could not create Index\n"); |
| 3196 | return 1; |
| 3197 | } |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3198 | idxAction = clang_IndexAction_create(Idx); |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3199 | |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3200 | { |
| 3201 | const char *database = argv[0]; |
| 3202 | CXCompilationDatabase db = 0; |
| 3203 | CXCompileCommands CCmds = 0; |
| 3204 | CXCompileCommand CCmd; |
| 3205 | CXCompilationDatabase_Error ec; |
| 3206 | CXString wd; |
| 3207 | #define MAX_COMPILE_ARGS 512 |
| 3208 | CXString cxargs[MAX_COMPILE_ARGS]; |
| 3209 | const char *args[MAX_COMPILE_ARGS]; |
| 3210 | char *tmp; |
| 3211 | unsigned len; |
| 3212 | char *buildDir; |
| 3213 | int i, a, numCmds, numArgs; |
| 3214 | |
| 3215 | len = strlen(database); |
| 3216 | tmp = (char *) malloc(len+1); |
| 3217 | memcpy(tmp, database, len+1); |
| 3218 | buildDir = dirname(tmp); |
| 3219 | |
| 3220 | db = clang_CompilationDatabase_fromDirectory(buildDir, &ec); |
| 3221 | |
| 3222 | if (db) { |
| 3223 | |
| 3224 | if (ec!=CXCompilationDatabase_NoError) { |
| 3225 | printf("unexpected error %d code while loading compilation database\n", ec); |
| 3226 | errorCode = -1; |
| 3227 | goto cdb_end; |
| 3228 | } |
| 3229 | |
Argyrios Kyrtzidis | 2bff7e5 | 2012-12-17 20:19:56 +0000 | [diff] [blame] | 3230 | if (chdir(buildDir) != 0) { |
| 3231 | printf("Could not chdir to %s\n", buildDir); |
| 3232 | errorCode = -1; |
| 3233 | goto cdb_end; |
| 3234 | } |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3235 | |
Argyrios Kyrtzidis | 2bff7e5 | 2012-12-17 20:19:56 +0000 | [diff] [blame] | 3236 | CCmds = clang_CompilationDatabase_getAllCompileCommands(db); |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3237 | if (!CCmds) { |
| 3238 | printf("compilation db is empty\n"); |
| 3239 | errorCode = -1; |
| 3240 | goto cdb_end; |
| 3241 | } |
| 3242 | |
| 3243 | numCmds = clang_CompileCommands_getSize(CCmds); |
| 3244 | |
| 3245 | if (numCmds==0) { |
| 3246 | fprintf(stderr, "should not get an empty compileCommand set\n"); |
| 3247 | errorCode = -1; |
| 3248 | goto cdb_end; |
| 3249 | } |
| 3250 | |
| 3251 | for (i=0; i<numCmds && errorCode == 0; ++i) { |
| 3252 | CCmd = clang_CompileCommands_getCommand(CCmds, i); |
| 3253 | |
| 3254 | wd = clang_CompileCommand_getDirectory(CCmd); |
Argyrios Kyrtzidis | 2bff7e5 | 2012-12-17 20:19:56 +0000 | [diff] [blame] | 3255 | if (chdir(clang_getCString(wd)) != 0) { |
| 3256 | printf("Could not chdir to %s\n", clang_getCString(wd)); |
| 3257 | errorCode = -1; |
| 3258 | goto cdb_end; |
| 3259 | } |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3260 | clang_disposeString(wd); |
| 3261 | |
| 3262 | numArgs = clang_CompileCommand_getNumArgs(CCmd); |
| 3263 | if (numArgs > MAX_COMPILE_ARGS){ |
| 3264 | fprintf(stderr, "got more compile arguments than maximum\n"); |
| 3265 | errorCode = -1; |
| 3266 | goto cdb_end; |
| 3267 | } |
| 3268 | for (a=0; a<numArgs; ++a) { |
| 3269 | cxargs[a] = clang_CompileCommand_getArg(CCmd, a); |
| 3270 | args[a] = clang_getCString(cxargs[a]); |
| 3271 | } |
| 3272 | |
| 3273 | errorCode = index_compile_args(numArgs, args, idxAction, |
| 3274 | /*importedASTs=*/0, check_prefix); |
| 3275 | |
| 3276 | for (a=0; a<numArgs; ++a) |
| 3277 | clang_disposeString(cxargs[a]); |
| 3278 | } |
| 3279 | } else { |
| 3280 | printf("database loading failed with error code %d.\n", ec); |
| 3281 | errorCode = -1; |
| 3282 | } |
| 3283 | |
| 3284 | cdb_end: |
| 3285 | clang_CompileCommands_dispose(CCmds); |
| 3286 | clang_CompilationDatabase_dispose(db); |
| 3287 | free(tmp); |
| 3288 | |
| 3289 | } |
| 3290 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3291 | clang_IndexAction_dispose(idxAction); |
| 3292 | clang_disposeIndex(Idx); |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3293 | return errorCode; |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3296 | int perform_token_annotation(int argc, const char **argv) { |
| 3297 | const char *input = argv[1]; |
| 3298 | char *filename = 0; |
| 3299 | unsigned line, second_line; |
| 3300 | unsigned column, second_column; |
| 3301 | CXIndex CIdx; |
| 3302 | CXTranslationUnit TU = 0; |
| 3303 | int errorCode; |
| 3304 | struct CXUnsavedFile *unsaved_files = 0; |
| 3305 | int num_unsaved_files = 0; |
| 3306 | CXToken *tokens; |
| 3307 | unsigned num_tokens; |
| 3308 | CXSourceRange range; |
| 3309 | CXSourceLocation startLoc, endLoc; |
| 3310 | CXFile file = 0; |
| 3311 | CXCursor *cursors = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3312 | CXSourceRangeList *skipped_ranges = 0; |
| 3313 | enum CXErrorCode Err; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3314 | unsigned i; |
| 3315 | |
| 3316 | input += strlen("-test-annotate-tokens="); |
| 3317 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
| 3318 | &second_line, &second_column))) |
| 3319 | return errorCode; |
| 3320 | |
Richard Smith | e07c5f8 | 2012-07-05 08:20:49 +0000 | [diff] [blame] | 3321 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) { |
| 3322 | free(filename); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3323 | return -1; |
Richard Smith | e07c5f8 | 2012-07-05 08:20:49 +0000 | [diff] [blame] | 3324 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3325 | |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 3326 | CIdx = clang_createIndex(0, 1); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3327 | Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1], |
| 3328 | argv + num_unsaved_files + 2, |
| 3329 | argc - num_unsaved_files - 3, |
| 3330 | unsaved_files, |
| 3331 | num_unsaved_files, |
| 3332 | getDefaultParsingOptions(), &TU); |
| 3333 | if (Err != CXError_Success) { |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3334 | fprintf(stderr, "unable to parse input\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3335 | describeLibclangFailure(Err); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3336 | clang_disposeIndex(CIdx); |
| 3337 | free(filename); |
| 3338 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 3339 | return -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3340 | } |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3341 | errorCode = 0; |
| 3342 | |
Richard Smith | e07c5f8 | 2012-07-05 08:20:49 +0000 | [diff] [blame] | 3343 | if (checkForErrors(TU) != 0) { |
| 3344 | errorCode = -1; |
| 3345 | goto teardown; |
| 3346 | } |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3347 | |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3348 | if (getenv("CINDEXTEST_EDITING")) { |
| 3349 | for (i = 0; i < 5; ++i) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3350 | Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 3351 | clang_defaultReparseOptions(TU)); |
| 3352 | if (Err != CXError_Success) { |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3353 | fprintf(stderr, "Unable to reparse translation unit!\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3354 | describeLibclangFailure(Err); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3355 | errorCode = -1; |
| 3356 | goto teardown; |
| 3357 | } |
| 3358 | } |
| 3359 | } |
| 3360 | |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3361 | if (checkForErrors(TU) != 0) { |
| 3362 | errorCode = -1; |
| 3363 | goto teardown; |
| 3364 | } |
| 3365 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3366 | file = clang_getFile(TU, filename); |
| 3367 | if (!file) { |
| 3368 | fprintf(stderr, "file %s is not in this translation unit\n", filename); |
| 3369 | errorCode = -1; |
| 3370 | goto teardown; |
| 3371 | } |
| 3372 | |
| 3373 | startLoc = clang_getLocation(TU, file, line, column); |
| 3374 | if (clang_equalLocations(clang_getNullLocation(), startLoc)) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3375 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3376 | column); |
| 3377 | errorCode = -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3378 | goto teardown; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | endLoc = clang_getLocation(TU, file, second_line, second_column); |
| 3382 | if (clang_equalLocations(clang_getNullLocation(), endLoc)) { |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3383 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3384 | second_line, second_column); |
| 3385 | errorCode = -1; |
Ted Kremenek | e68fff6 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3386 | goto teardown; |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | range = clang_getRange(startLoc, endLoc); |
| 3390 | clang_tokenize(TU, range, &tokens, &num_tokens); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3391 | |
| 3392 | if (checkForErrors(TU) != 0) { |
| 3393 | errorCode = -1; |
| 3394 | goto teardown; |
| 3395 | } |
| 3396 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3397 | cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); |
| 3398 | clang_annotateTokens(TU, tokens, num_tokens, cursors); |
Argyrios Kyrtzidis | dfca64d | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3399 | |
| 3400 | if (checkForErrors(TU) != 0) { |
| 3401 | errorCode = -1; |
| 3402 | goto teardown; |
| 3403 | } |
| 3404 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3405 | skipped_ranges = clang_getSkippedRanges(TU, file); |
| 3406 | for (i = 0; i != skipped_ranges->count; ++i) { |
| 3407 | unsigned start_line, start_column, end_line, end_column; |
| 3408 | clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]), |
| 3409 | 0, &start_line, &start_column, 0); |
| 3410 | clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]), |
| 3411 | 0, &end_line, &end_column, 0); |
| 3412 | printf("Skipping: "); |
| 3413 | PrintExtent(stdout, start_line, start_column, end_line, end_column); |
| 3414 | printf("\n"); |
| 3415 | } |
| 3416 | clang_disposeSourceRangeList(skipped_ranges); |
| 3417 | |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3418 | for (i = 0; i != num_tokens; ++i) { |
| 3419 | const char *kind = "<unknown>"; |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 3420 | CXString spelling = clang_getTokenSpelling(TU, tokens[i]); |
| 3421 | CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3422 | unsigned start_line, start_column, end_line, end_column; |
| 3423 | |
| 3424 | switch (clang_getTokenKind(tokens[i])) { |
| 3425 | case CXToken_Punctuation: kind = "Punctuation"; break; |
| 3426 | case CXToken_Keyword: kind = "Keyword"; break; |
| 3427 | case CXToken_Identifier: kind = "Identifier"; break; |
| 3428 | case CXToken_Literal: kind = "Literal"; break; |
| 3429 | case CXToken_Comment: kind = "Comment"; break; |
| 3430 | } |
Douglas Gregor | a9b06d4 | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 3431 | clang_getSpellingLocation(clang_getRangeStart(extent), |
| 3432 | 0, &start_line, &start_column, 0); |
| 3433 | clang_getSpellingLocation(clang_getRangeEnd(extent), |
| 3434 | 0, &end_line, &end_column, 0); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 3435 | printf("%s: \"%s\" ", kind, clang_getCString(spelling)); |
Benjamin Kramer | 342742a | 2012-04-14 09:11:51 +0000 | [diff] [blame] | 3436 | clang_disposeString(spelling); |
Daniel Dunbar | 51b058c | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 3437 | PrintExtent(stdout, start_line, start_column, end_line, end_column); |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3438 | if (!clang_isInvalid(cursors[i].kind)) { |
| 3439 | printf(" "); |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 3440 | PrintCursor(cursors[i], NULL); |
Douglas Gregor | 0045e9f | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3441 | } |
| 3442 | printf("\n"); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3443 | } |
| 3444 | free(cursors); |
Ted Kremenek | 93f5e6a | 2010-10-20 21:22:15 +0000 | [diff] [blame] | 3445 | clang_disposeTokens(TU, tokens, num_tokens); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3446 | |
| 3447 | teardown: |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 3448 | PrintDiagnostics(TU); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3449 | clang_disposeTranslationUnit(TU); |
| 3450 | clang_disposeIndex(CIdx); |
| 3451 | free(filename); |
| 3452 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 3453 | return errorCode; |
| 3454 | } |
| 3455 | |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3456 | static int |
| 3457 | perform_test_compilation_db(const char *database, int argc, const char **argv) { |
| 3458 | CXCompilationDatabase db; |
| 3459 | CXCompileCommands CCmds; |
| 3460 | CXCompileCommand CCmd; |
| 3461 | CXCompilationDatabase_Error ec; |
| 3462 | CXString wd; |
| 3463 | CXString arg; |
| 3464 | int errorCode = 0; |
| 3465 | char *tmp; |
| 3466 | unsigned len; |
| 3467 | char *buildDir; |
| 3468 | int i, j, a, numCmds, numArgs; |
| 3469 | |
| 3470 | len = strlen(database); |
| 3471 | tmp = (char *) malloc(len+1); |
| 3472 | memcpy(tmp, database, len+1); |
| 3473 | buildDir = dirname(tmp); |
| 3474 | |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3475 | db = clang_CompilationDatabase_fromDirectory(buildDir, &ec); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3476 | |
| 3477 | if (db) { |
| 3478 | |
| 3479 | if (ec!=CXCompilationDatabase_NoError) { |
| 3480 | printf("unexpected error %d code while loading compilation database\n", ec); |
| 3481 | errorCode = -1; |
| 3482 | goto cdb_end; |
| 3483 | } |
| 3484 | |
| 3485 | for (i=0; i<argc && errorCode==0; ) { |
| 3486 | if (strcmp(argv[i],"lookup")==0){ |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3487 | CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3488 | |
| 3489 | if (!CCmds) { |
| 3490 | printf("file %s not found in compilation db\n", argv[i+1]); |
| 3491 | errorCode = -1; |
| 3492 | break; |
| 3493 | } |
| 3494 | |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3495 | numCmds = clang_CompileCommands_getSize(CCmds); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3496 | |
| 3497 | if (numCmds==0) { |
| 3498 | fprintf(stderr, "should not get an empty compileCommand set for file" |
| 3499 | " '%s'\n", argv[i+1]); |
| 3500 | errorCode = -1; |
| 3501 | break; |
| 3502 | } |
| 3503 | |
| 3504 | for (j=0; j<numCmds; ++j) { |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3505 | CCmd = clang_CompileCommands_getCommand(CCmds, j); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3506 | |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3507 | wd = clang_CompileCommand_getDirectory(CCmd); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3508 | printf("workdir:'%s'", clang_getCString(wd)); |
| 3509 | clang_disposeString(wd); |
| 3510 | |
| 3511 | printf(" cmdline:'"); |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3512 | numArgs = clang_CompileCommand_getNumArgs(CCmd); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3513 | for (a=0; a<numArgs; ++a) { |
| 3514 | if (a) printf(" "); |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3515 | arg = clang_CompileCommand_getArg(CCmd, a); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3516 | printf("%s", clang_getCString(arg)); |
| 3517 | clang_disposeString(arg); |
| 3518 | } |
| 3519 | printf("'\n"); |
| 3520 | } |
| 3521 | |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3522 | clang_CompileCommands_dispose(CCmds); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3523 | |
| 3524 | i += 2; |
| 3525 | } |
| 3526 | } |
Arnaud A. de Grandmaison | c70851b | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3527 | clang_CompilationDatabase_dispose(db); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3528 | } else { |
| 3529 | printf("database loading failed with error code %d.\n", ec); |
| 3530 | errorCode = -1; |
| 3531 | } |
| 3532 | |
| 3533 | cdb_end: |
| 3534 | free(tmp); |
| 3535 | |
| 3536 | return errorCode; |
| 3537 | } |
| 3538 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 3539 | /******************************************************************************/ |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 3540 | /* USR printing. */ |
| 3541 | /******************************************************************************/ |
| 3542 | |
| 3543 | static int insufficient_usr(const char *kind, const char *usage) { |
| 3544 | fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage); |
| 3545 | return 1; |
| 3546 | } |
| 3547 | |
| 3548 | static unsigned isUSR(const char *s) { |
| 3549 | return s[0] == 'c' && s[1] == ':'; |
| 3550 | } |
| 3551 | |
| 3552 | static int not_usr(const char *s, const char *arg) { |
| 3553 | fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg); |
| 3554 | return 1; |
| 3555 | } |
| 3556 | |
| 3557 | static void print_usr(CXString usr) { |
| 3558 | const char *s = clang_getCString(usr); |
| 3559 | printf("%s\n", s); |
| 3560 | clang_disposeString(usr); |
| 3561 | } |
| 3562 | |
| 3563 | static void display_usrs() { |
| 3564 | fprintf(stderr, "-print-usrs options:\n" |
| 3565 | " ObjCCategory <class name> <category name>\n" |
| 3566 | " ObjCClass <class name>\n" |
| 3567 | " ObjCIvar <ivar name> <class USR>\n" |
| 3568 | " ObjCMethod <selector> [0=class method|1=instance method] " |
| 3569 | "<class USR>\n" |
| 3570 | " ObjCProperty <property name> <class USR>\n" |
| 3571 | " ObjCProtocol <protocol name>\n"); |
| 3572 | } |
| 3573 | |
| 3574 | int print_usrs(const char **I, const char **E) { |
| 3575 | while (I != E) { |
| 3576 | const char *kind = *I; |
| 3577 | unsigned len = strlen(kind); |
| 3578 | switch (len) { |
| 3579 | case 8: |
| 3580 | if (memcmp(kind, "ObjCIvar", 8) == 0) { |
| 3581 | if (I + 2 >= E) |
| 3582 | return insufficient_usr(kind, "<ivar name> <class USR>"); |
| 3583 | if (!isUSR(I[2])) |
| 3584 | return not_usr("<class USR>", I[2]); |
| 3585 | else { |
| 3586 | CXString x; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3587 | x.data = (void*) I[2]; |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 3588 | x.private_flags = 0; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 3589 | print_usr(clang_constructUSR_ObjCIvar(I[1], x)); |
| 3590 | } |
| 3591 | |
| 3592 | I += 3; |
| 3593 | continue; |
| 3594 | } |
| 3595 | break; |
| 3596 | case 9: |
| 3597 | if (memcmp(kind, "ObjCClass", 9) == 0) { |
| 3598 | if (I + 1 >= E) |
| 3599 | return insufficient_usr(kind, "<class name>"); |
| 3600 | print_usr(clang_constructUSR_ObjCClass(I[1])); |
| 3601 | I += 2; |
| 3602 | continue; |
| 3603 | } |
| 3604 | break; |
| 3605 | case 10: |
| 3606 | if (memcmp(kind, "ObjCMethod", 10) == 0) { |
| 3607 | if (I + 3 >= E) |
| 3608 | return insufficient_usr(kind, "<method selector> " |
| 3609 | "[0=class method|1=instance method] <class USR>"); |
| 3610 | if (!isUSR(I[3])) |
| 3611 | return not_usr("<class USR>", I[3]); |
| 3612 | else { |
| 3613 | CXString x; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3614 | x.data = (void*) I[3]; |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 3615 | x.private_flags = 0; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 3616 | print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x)); |
| 3617 | } |
| 3618 | I += 4; |
| 3619 | continue; |
| 3620 | } |
| 3621 | break; |
| 3622 | case 12: |
| 3623 | if (memcmp(kind, "ObjCCategory", 12) == 0) { |
| 3624 | if (I + 2 >= E) |
| 3625 | return insufficient_usr(kind, "<class name> <category name>"); |
| 3626 | print_usr(clang_constructUSR_ObjCCategory(I[1], I[2])); |
| 3627 | I += 3; |
| 3628 | continue; |
| 3629 | } |
| 3630 | if (memcmp(kind, "ObjCProtocol", 12) == 0) { |
| 3631 | if (I + 1 >= E) |
| 3632 | return insufficient_usr(kind, "<protocol name>"); |
| 3633 | print_usr(clang_constructUSR_ObjCProtocol(I[1])); |
| 3634 | I += 2; |
| 3635 | continue; |
| 3636 | } |
| 3637 | if (memcmp(kind, "ObjCProperty", 12) == 0) { |
| 3638 | if (I + 2 >= E) |
| 3639 | return insufficient_usr(kind, "<property name> <class USR>"); |
| 3640 | if (!isUSR(I[2])) |
| 3641 | return not_usr("<class USR>", I[2]); |
| 3642 | else { |
| 3643 | CXString x; |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3644 | x.data = (void*) I[2]; |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 3645 | x.private_flags = 0; |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 3646 | print_usr(clang_constructUSR_ObjCProperty(I[1], x)); |
| 3647 | } |
| 3648 | I += 3; |
| 3649 | continue; |
| 3650 | } |
| 3651 | break; |
| 3652 | default: |
| 3653 | break; |
| 3654 | } |
| 3655 | break; |
| 3656 | } |
| 3657 | |
| 3658 | if (I != E) { |
| 3659 | fprintf(stderr, "Invalid USR kind: %s\n", *I); |
| 3660 | display_usrs(); |
| 3661 | return 1; |
| 3662 | } |
| 3663 | return 0; |
| 3664 | } |
| 3665 | |
| 3666 | int print_usrs_file(const char *file_name) { |
| 3667 | char line[2048]; |
| 3668 | const char *args[128]; |
| 3669 | unsigned numChars = 0; |
| 3670 | |
| 3671 | FILE *fp = fopen(file_name, "r"); |
| 3672 | if (!fp) { |
| 3673 | fprintf(stderr, "error: cannot open '%s'\n", file_name); |
| 3674 | return 1; |
| 3675 | } |
| 3676 | |
| 3677 | /* This code is not really all that safe, but it works fine for testing. */ |
| 3678 | while (!feof(fp)) { |
| 3679 | char c = fgetc(fp); |
| 3680 | if (c == '\n') { |
| 3681 | unsigned i = 0; |
| 3682 | const char *s = 0; |
| 3683 | |
| 3684 | if (numChars == 0) |
| 3685 | continue; |
| 3686 | |
| 3687 | line[numChars] = '\0'; |
| 3688 | numChars = 0; |
| 3689 | |
| 3690 | if (line[0] == '/' && line[1] == '/') |
| 3691 | continue; |
| 3692 | |
| 3693 | s = strtok(line, " "); |
| 3694 | while (s) { |
| 3695 | args[i] = s; |
| 3696 | ++i; |
| 3697 | s = strtok(0, " "); |
| 3698 | } |
| 3699 | if (print_usrs(&args[0], &args[i])) |
| 3700 | return 1; |
| 3701 | } |
| 3702 | else |
| 3703 | line[numChars++] = c; |
| 3704 | } |
| 3705 | |
| 3706 | fclose(fp); |
| 3707 | return 0; |
| 3708 | } |
| 3709 | |
| 3710 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 3711 | /* Command line processing. */ |
| 3712 | /******************************************************************************/ |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3713 | int write_pch_file(const char *filename, int argc, const char *argv[]) { |
| 3714 | CXIndex Idx; |
| 3715 | CXTranslationUnit TU; |
| 3716 | struct CXUnsavedFile *unsaved_files = 0; |
| 3717 | int num_unsaved_files = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3718 | enum CXErrorCode Err; |
Francois Pichet | 08aa622 | 2011-07-06 22:09:44 +0000 | [diff] [blame] | 3719 | int result = 0; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3720 | |
Stefanus Du Toit | fc09336 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 3721 | Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3722 | |
| 3723 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 3724 | clang_disposeIndex(Idx); |
| 3725 | return -1; |
| 3726 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3727 | |
| 3728 | Err = clang_parseTranslationUnit2( |
| 3729 | Idx, 0, argv + num_unsaved_files, argc - num_unsaved_files, |
| 3730 | unsaved_files, num_unsaved_files, |
| 3731 | CXTranslationUnit_Incomplete | |
| 3732 | CXTranslationUnit_DetailedPreprocessingRecord | |
| 3733 | CXTranslationUnit_ForSerialization, |
| 3734 | &TU); |
| 3735 | if (Err != CXError_Success) { |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3736 | fprintf(stderr, "Unable to load translation unit!\n"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3737 | describeLibclangFailure(Err); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3738 | free_remapped_files(unsaved_files, num_unsaved_files); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3739 | clang_disposeTranslationUnit(TU); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3740 | clang_disposeIndex(Idx); |
| 3741 | return 1; |
| 3742 | } |
| 3743 | |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 3744 | switch (clang_saveTranslationUnit(TU, filename, |
| 3745 | clang_defaultSaveOptions(TU))) { |
| 3746 | case CXSaveError_None: |
| 3747 | break; |
| 3748 | |
| 3749 | case CXSaveError_TranslationErrors: |
| 3750 | fprintf(stderr, "Unable to write PCH file %s: translation errors\n", |
| 3751 | filename); |
| 3752 | result = 2; |
| 3753 | break; |
| 3754 | |
| 3755 | case CXSaveError_InvalidTU: |
| 3756 | fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n", |
| 3757 | filename); |
| 3758 | result = 3; |
| 3759 | break; |
| 3760 | |
| 3761 | case CXSaveError_Unknown: |
| 3762 | default: |
| 3763 | fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename); |
| 3764 | result = 1; |
| 3765 | break; |
| 3766 | } |
| 3767 | |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3768 | clang_disposeTranslationUnit(TU); |
| 3769 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 3770 | clang_disposeIndex(Idx); |
Douglas Gregor | 39c411f | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 3771 | return result; |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | /******************************************************************************/ |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3775 | /* Serialized diagnostics. */ |
| 3776 | /******************************************************************************/ |
| 3777 | |
| 3778 | static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) { |
| 3779 | switch (error) { |
| 3780 | case CXLoadDiag_CannotLoad: return "Cannot Load File"; |
| 3781 | case CXLoadDiag_None: break; |
| 3782 | case CXLoadDiag_Unknown: return "Unknown"; |
| 3783 | case CXLoadDiag_InvalidFile: return "Invalid File"; |
| 3784 | } |
| 3785 | return "None"; |
| 3786 | } |
| 3787 | |
| 3788 | static const char *getSeverityString(enum CXDiagnosticSeverity severity) { |
| 3789 | switch (severity) { |
| 3790 | case CXDiagnostic_Note: return "note"; |
| 3791 | case CXDiagnostic_Error: return "error"; |
| 3792 | case CXDiagnostic_Fatal: return "fatal"; |
| 3793 | case CXDiagnostic_Ignored: return "ignored"; |
| 3794 | case CXDiagnostic_Warning: return "warning"; |
| 3795 | } |
| 3796 | return "unknown"; |
| 3797 | } |
| 3798 | |
| 3799 | static void printIndent(unsigned indent) { |
Ted Kremenek | a7e8a83 | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 3800 | if (indent == 0) |
| 3801 | return; |
| 3802 | fprintf(stderr, "+"); |
| 3803 | --indent; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3804 | while (indent > 0) { |
Ted Kremenek | a7e8a83 | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 3805 | fprintf(stderr, "-"); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3806 | --indent; |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | static void printLocation(CXSourceLocation L) { |
| 3811 | CXFile File; |
| 3812 | CXString FileName; |
| 3813 | unsigned line, column, offset; |
| 3814 | |
| 3815 | clang_getExpansionLocation(L, &File, &line, &column, &offset); |
| 3816 | FileName = clang_getFileName(File); |
| 3817 | |
| 3818 | fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column); |
| 3819 | clang_disposeString(FileName); |
| 3820 | } |
| 3821 | |
| 3822 | static void printRanges(CXDiagnostic D, unsigned indent) { |
| 3823 | unsigned i, n = clang_getDiagnosticNumRanges(D); |
| 3824 | |
| 3825 | for (i = 0; i < n; ++i) { |
| 3826 | CXSourceLocation Start, End; |
Enea Zaffanella | 10f9004 | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 3827 | CXSourceRange SR = clang_getDiagnosticRange(D, i); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3828 | Start = clang_getRangeStart(SR); |
| 3829 | End = clang_getRangeEnd(SR); |
| 3830 | |
| 3831 | printIndent(indent); |
| 3832 | fprintf(stderr, "Range: "); |
| 3833 | printLocation(Start); |
| 3834 | fprintf(stderr, " "); |
| 3835 | printLocation(End); |
| 3836 | fprintf(stderr, "\n"); |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | static void printFixIts(CXDiagnostic D, unsigned indent) { |
| 3841 | unsigned i, n = clang_getDiagnosticNumFixIts(D); |
Ted Kremenek | 3739b32 | 2012-03-20 20:49:45 +0000 | [diff] [blame] | 3842 | fprintf(stderr, "Number FIXITs = %d\n", n); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3843 | for (i = 0 ; i < n; ++i) { |
| 3844 | CXSourceRange ReplacementRange; |
| 3845 | CXString text; |
| 3846 | text = clang_getDiagnosticFixIt(D, i, &ReplacementRange); |
| 3847 | |
| 3848 | printIndent(indent); |
| 3849 | fprintf(stderr, "FIXIT: ("); |
| 3850 | printLocation(clang_getRangeStart(ReplacementRange)); |
| 3851 | fprintf(stderr, " - "); |
| 3852 | printLocation(clang_getRangeEnd(ReplacementRange)); |
| 3853 | fprintf(stderr, "): \"%s\"\n", clang_getCString(text)); |
| 3854 | clang_disposeString(text); |
| 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) { |
NAKAMURA Takumi | 9190943 | 2011-11-10 09:30:15 +0000 | [diff] [blame] | 3859 | unsigned i, n; |
| 3860 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3861 | if (!Diags) |
| 3862 | return; |
| 3863 | |
NAKAMURA Takumi | 9190943 | 2011-11-10 09:30:15 +0000 | [diff] [blame] | 3864 | n = clang_getNumDiagnosticsInSet(Diags); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3865 | for (i = 0; i < n; ++i) { |
| 3866 | CXSourceLocation DiagLoc; |
| 3867 | CXDiagnostic D; |
| 3868 | CXFile File; |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 3869 | CXString FileName, DiagSpelling, DiagOption, DiagCat; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3870 | unsigned line, column, offset; |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 3871 | const char *DiagOptionStr = 0, *DiagCatStr = 0; |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3872 | |
| 3873 | D = clang_getDiagnosticInSet(Diags, i); |
| 3874 | DiagLoc = clang_getDiagnosticLocation(D); |
| 3875 | clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset); |
| 3876 | FileName = clang_getFileName(File); |
| 3877 | DiagSpelling = clang_getDiagnosticSpelling(D); |
| 3878 | |
| 3879 | printIndent(indent); |
| 3880 | |
| 3881 | fprintf(stderr, "%s:%d:%d: %s: %s", |
| 3882 | clang_getCString(FileName), |
| 3883 | line, |
| 3884 | column, |
| 3885 | getSeverityString(clang_getDiagnosticSeverity(D)), |
| 3886 | clang_getCString(DiagSpelling)); |
| 3887 | |
| 3888 | DiagOption = clang_getDiagnosticOption(D, 0); |
| 3889 | DiagOptionStr = clang_getCString(DiagOption); |
| 3890 | if (DiagOptionStr) { |
| 3891 | fprintf(stderr, " [%s]", DiagOptionStr); |
| 3892 | } |
| 3893 | |
Ted Kremenek | 78d5d3b | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 3894 | DiagCat = clang_getDiagnosticCategoryText(D); |
| 3895 | DiagCatStr = clang_getCString(DiagCat); |
| 3896 | if (DiagCatStr) { |
| 3897 | fprintf(stderr, " [%s]", DiagCatStr); |
| 3898 | } |
| 3899 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3900 | fprintf(stderr, "\n"); |
| 3901 | |
| 3902 | printRanges(D, indent); |
| 3903 | printFixIts(D, indent); |
| 3904 | |
NAKAMURA Takumi | a4ca95a | 2011-11-10 10:07:57 +0000 | [diff] [blame] | 3905 | /* Print subdiagnostics. */ |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3906 | printDiagnosticSet(clang_getChildDiagnostics(D), indent+2); |
| 3907 | |
| 3908 | clang_disposeString(FileName); |
| 3909 | clang_disposeString(DiagSpelling); |
| 3910 | clang_disposeString(DiagOption); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3911 | clang_disposeString(DiagCat); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3912 | } |
| 3913 | } |
| 3914 | |
| 3915 | static int read_diagnostics(const char *filename) { |
| 3916 | enum CXLoadDiag_Error error; |
| 3917 | CXString errorString; |
| 3918 | CXDiagnosticSet Diags = 0; |
| 3919 | |
| 3920 | Diags = clang_loadDiagnostics(filename, &error, &errorString); |
| 3921 | if (!Diags) { |
| 3922 | fprintf(stderr, "Trouble deserializing file (%s): %s\n", |
| 3923 | getDiagnosticCodeStr(error), |
| 3924 | clang_getCString(errorString)); |
| 3925 | clang_disposeString(errorString); |
| 3926 | return 1; |
| 3927 | } |
| 3928 | |
| 3929 | printDiagnosticSet(Diags, 0); |
Ted Kremenek | a7e8a83 | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 3930 | fprintf(stderr, "Number of diagnostics: %d\n", |
| 3931 | clang_getNumDiagnosticsInSet(Diags)); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3932 | clang_disposeDiagnosticSet(Diags); |
| 3933 | return 0; |
| 3934 | } |
| 3935 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3936 | static int perform_print_build_session_timestamp(void) { |
| 3937 | printf("%lld\n", clang_getBuildSessionTimestamp()); |
| 3938 | return 0; |
| 3939 | } |
| 3940 | |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3941 | /******************************************************************************/ |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3942 | /* Command line processing. */ |
| 3943 | /******************************************************************************/ |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 3944 | |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 3945 | static CXCursorVisitor GetVisitor(const char *s) { |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3946 | if (s[0] == '\0') |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 3947 | return FilteredPrintingVisitor; |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3948 | if (strcmp(s, "-usrs") == 0) |
| 3949 | return USRVisitor; |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 3950 | if (strncmp(s, "-memory-usage", 13) == 0) |
| 3951 | return GetVisitor(s + 13); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3952 | return NULL; |
| 3953 | } |
| 3954 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 3955 | static void print_usage(void) { |
| 3956 | fprintf(stderr, |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 3957 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 3958 | " c-index-test -code-completion-timing=<site> <compiler arguments>\n" |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 3959 | " c-index-test -cursor-at=<site> <compiler arguments>\n" |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 3960 | " c-index-test -file-refs-at=<site> <compiler arguments>\n" |
| 3961 | " c-index-test -file-includes-in=<filename> <compiler arguments>\n"); |
NAKAMURA Takumi | 3584972 | 2012-10-24 22:52:04 +0000 | [diff] [blame] | 3962 | fprintf(stderr, |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3963 | " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n" |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3964 | " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n" |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3965 | " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n" |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3966 | " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n" |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 3967 | " c-index-test -test-file-scan <AST file> <source file> " |
Erik Verbruggen | 26fc0f9 | 2011-10-06 11:38:08 +0000 | [diff] [blame] | 3968 | "[FileCheck prefix]\n"); |
| 3969 | fprintf(stderr, |
Ted Kremenek | fe6fd3d | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 3970 | " c-index-test -test-load-tu <AST file> <symbol filter> " |
| 3971 | "[FileCheck prefix]\n" |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3972 | " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " |
| 3973 | "[FileCheck prefix]\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 3974 | " c-index-test -test-load-source <symbol filter> {<args>}*\n"); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 3975 | fprintf(stderr, |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 3976 | " c-index-test -test-load-source-memory-usage " |
| 3977 | "<symbol filter> {<args>}*\n" |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 3978 | " c-index-test -test-load-source-reparse <trials> <symbol filter> " |
| 3979 | " {<args>}*\n" |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 3980 | " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 3981 | " c-index-test -test-load-source-usrs-memory-usage " |
| 3982 | "<symbol filter> {<args>}*\n" |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 3983 | " c-index-test -test-annotate-tokens=<range> {<args>}*\n" |
| 3984 | " c-index-test -test-inclusion-stack-source {<args>}*\n" |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 3985 | " c-index-test -test-inclusion-stack-tu <AST file>\n"); |
Chandler Carruth | 53513d2 | 2010-07-22 06:29:13 +0000 | [diff] [blame] | 3986 | fprintf(stderr, |
Ted Kremenek | 4e6a3f7 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 3987 | " c-index-test -test-print-linkage-source {<args>}*\n" |
Dmitri Gribenko | ae03d8e | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 3988 | " c-index-test -test-print-type {<args>}*\n" |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 3989 | " c-index-test -test-print-type-size {<args>}*\n" |
Dmitri Gribenko | 1eb6082 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 3990 | " c-index-test -test-print-bitwidth {<args>}*\n" |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 3991 | " c-index-test -print-usr [<CursorKind> {<args>}]*\n" |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3992 | " c-index-test -print-usr-file <file>\n" |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3993 | " c-index-test -write-pch <file> <compiler arguments>\n"); |
| 3994 | fprintf(stderr, |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3995 | " c-index-test -compilation-db [lookup <filename>] database\n"); |
| 3996 | fprintf(stderr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3997 | " c-index-test -print-build-session-timestamp\n"); |
| 3998 | fprintf(stderr, |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3999 | " c-index-test -read-diagnostics <file>\n\n"); |
Douglas Gregor | caf4bd3 | 2010-07-20 14:34:35 +0000 | [diff] [blame] | 4000 | fprintf(stderr, |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4001 | " <symbol filter> values:\n%s", |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 4002 | " all - load all symbols, including those from PCH\n" |
| 4003 | " local - load all symbols except those in PCH\n" |
| 4004 | " category - only load ObjC categories (non-PCH)\n" |
| 4005 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 4006 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 4007 | " function - only load functions (non-PCH)\n" |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 4008 | " typedef - only load typdefs (non-PCH)\n" |
| 4009 | " scan-function - scan function bodies (non-PCH)\n\n"); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4012 | /***/ |
| 4013 | |
| 4014 | int cindextest_main(int argc, const char **argv) { |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 4015 | clang_enableStackTraces(); |
Ted Kremenek | 1532217 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 4016 | if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0) |
| 4017 | return read_diagnostics(argv[2]); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 4018 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
Douglas Gregor | 1982c18 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 4019 | return perform_code_completion(argc, argv, 0); |
| 4020 | if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1]) |
| 4021 | return perform_code_completion(argc, argv, 1); |
Douglas Gregor | f2c87bd | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 4022 | if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) |
| 4023 | return inspect_cursor_at(argc, argv); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 4024 | if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1]) |
| 4025 | return find_file_refs_at(argc, argv); |
Argyrios Kyrtzidis | ee2d5fd | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 4026 | if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1]) |
| 4027 | return find_file_includes_in(argc, argv); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 4028 | if (argc > 2 && strcmp(argv[1], "-index-file") == 0) |
Argyrios Kyrtzidis | 11db182 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 4029 | return index_file(argc - 2, argv + 2, /*full=*/0); |
| 4030 | if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0) |
| 4031 | return index_file(argc - 2, argv + 2, /*full=*/1); |
Argyrios Kyrtzidis | 21ee570 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 4032 | if (argc > 2 && strcmp(argv[1], "-index-tu") == 0) |
| 4033 | return index_tu(argc - 2, argv + 2); |
Argyrios Kyrtzidis | d10682f | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 4034 | if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0) |
| 4035 | return index_compile_db(argc - 2, argv + 2); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4036 | else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 4037 | CXCursorVisitor I = GetVisitor(argv[1] + 13); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4038 | if (I) |
Ted Kremenek | ce2ae88 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 4039 | return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, |
| 4040 | NULL); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4041 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 4042 | else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){ |
| 4043 | CXCursorVisitor I = GetVisitor(argv[1] + 25); |
| 4044 | if (I) { |
| 4045 | int trials = atoi(argv[2]); |
| 4046 | return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I, |
| 4047 | NULL); |
| 4048 | } |
| 4049 | } |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4050 | else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { |
Douglas Gregor | e5b72ba | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 4051 | CXCursorVisitor I = GetVisitor(argv[1] + 17); |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 4052 | |
| 4053 | PostVisitTU postVisit = 0; |
| 4054 | if (strstr(argv[1], "-memory-usage")) |
| 4055 | postVisit = PrintMemoryUsage; |
| 4056 | |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4057 | if (I) |
Ted Kremenek | 59fc1e5 | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 4058 | return perform_test_load_source(argc - 3, argv + 3, argv[2], I, |
| 4059 | postVisit); |
Ted Kremenek | 7d40562 | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4060 | } |
| 4061 | else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 4062 | return perform_file_scan(argv[2], argv[3], |
| 4063 | argc >= 5 ? argv[4] : 0); |
Douglas Gregor | fc8ea23 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4064 | else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) |
| 4065 | return perform_token_annotation(argc, argv); |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 4066 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) |
| 4067 | return perform_test_load_source(argc - 2, argv + 2, "all", NULL, |
| 4068 | PrintInclusionStack); |
| 4069 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) |
| 4070 | return perform_test_load_tu(argv[2], "all", NULL, NULL, |
| 4071 | PrintInclusionStack); |
Ted Kremenek | 3bed527 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 4072 | else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0) |
| 4073 | return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage, |
| 4074 | NULL); |
Dmitri Gribenko | ae03d8e | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 4075 | else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0) |
Ted Kremenek | 8e0ac17 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 4076 | return perform_test_load_source(argc - 2, argv + 2, "all", |
Dmitri Gribenko | ae03d8e | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 4077 | PrintType, 0); |
Argyrios Kyrtzidis | 411d33a | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 4078 | else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0) |
| 4079 | return perform_test_load_source(argc - 2, argv + 2, "all", |
| 4080 | PrintTypeSize, 0); |
Dmitri Gribenko | 1eb6082 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 4081 | else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0) |
| 4082 | return perform_test_load_source(argc - 2, argv + 2, "all", |
| 4083 | PrintBitWidth, 0); |
Ted Kremenek | f7b714d | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 4084 | else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { |
| 4085 | if (argc > 2) |
| 4086 | return print_usrs(argv + 2, argv + argc); |
| 4087 | else { |
| 4088 | display_usrs(); |
| 4089 | return 1; |
| 4090 | } |
| 4091 | } |
| 4092 | else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0) |
| 4093 | return print_usrs_file(argv[2]); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 4094 | else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0) |
| 4095 | return write_pch_file(argv[2], argc - 3, argv + 3); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 4096 | else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0) |
| 4097 | return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4098 | else if (argc == 2 && strcmp(argv[1], "-print-build-session-timestamp") == 0) |
| 4099 | return perform_print_build_session_timestamp(); |
Arnaud A. de Grandmaison | db29318 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 4100 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 4101 | print_usage(); |
| 4102 | return 1; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 4103 | } |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4104 | |
| 4105 | /***/ |
| 4106 | |
| 4107 | /* We intentionally run in a separate thread to ensure we at least minimal |
| 4108 | * testing of a multithreaded environment (for example, having a reduced stack |
| 4109 | * size). */ |
| 4110 | |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4111 | typedef struct thread_info { |
| 4112 | int argc; |
| 4113 | const char **argv; |
| 4114 | int result; |
| 4115 | } thread_info; |
Benjamin Kramer | 8429491 | 2010-11-04 19:11:31 +0000 | [diff] [blame] | 4116 | void thread_runner(void *client_data_v) { |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4117 | thread_info *client_data = client_data_v; |
| 4118 | client_data->result = cindextest_main(client_data->argc, client_data->argv); |
NAKAMURA Takumi | 3be55cd | 2012-04-07 06:59:28 +0000 | [diff] [blame] | 4119 | #ifdef __CYGWIN__ |
| 4120 | fflush(stdout); /* stdout is not flushed on Cygwin. */ |
| 4121 | #endif |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | int main(int argc, const char **argv) { |
Benjamin Kramer | d1a4f68 | 2012-08-10 10:06:13 +0000 | [diff] [blame] | 4125 | thread_info client_data; |
| 4126 | |
Dmitri Gribenko | f303d4c | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 4127 | #ifdef CLANG_HAVE_LIBXML |
| 4128 | LIBXML_TEST_VERSION |
| 4129 | #endif |
| 4130 | |
Douglas Gregor | 6160598 | 2010-10-27 16:00:01 +0000 | [diff] [blame] | 4131 | if (getenv("CINDEXTEST_NOTHREADS")) |
| 4132 | return cindextest_main(argc, argv); |
| 4133 | |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4134 | client_data.argc = argc; |
| 4135 | client_data.argv = argv; |
Daniel Dunbar | a32a6e1 | 2010-11-04 01:26:31 +0000 | [diff] [blame] | 4136 | clang_executeOnThread(thread_runner, &client_data, 0); |
Daniel Dunbar | 6edc800 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4137 | return client_data.result; |
| 4138 | } |