Steve Naroff | 69b10fd | 2009-09-01 15:55:40 +0000 | [diff] [blame] | 1 | /* c-index-test.c */ |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 2 | |
| 3 | #include "clang-c/Index.h" |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 4 | #include "clang-c/CXCompilationDatabase.h" |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 5 | #include "clang-c/BuildSystem.h" |
Alp Toker | 59c6bc5 | 2014-04-28 02:39:27 +0000 | [diff] [blame] | 6 | #include "clang-c/Documentation.h" |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 7 | #include "llvm/Config/config.h" |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 8 | #include <ctype.h> |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 9 | #include <stdlib.h> |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 11 | #include <string.h> |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 12 | #include <assert.h> |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 13 | |
Dmitri Gribenko | 740c0fb | 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 | f75d498 | 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 | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 26 | /******************************************************************************/ |
| 27 | /* Utility functions. */ |
| 28 | /******************************************************************************/ |
| 29 | |
John Thompson | de258b5 | 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 | 0fe28a1 | 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 | 1e43baa6 | 2012-06-30 11:47:18 +0000 | [diff] [blame] | 54 | *base1 = 0; |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 55 | else if (base2) |
NAKAMURA Takumi | 1e43baa6 | 2012-06-30 11:47:18 +0000 | [diff] [blame] | 56 | *base2 = 0; |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 57 | |
| 58 | return path; |
| 59 | } |
John Thompson | de258b5 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 60 | #else |
Steve Naroff | a7753c4 | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 61 | extern char *basename(const char *); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 62 | extern char *dirname(char *); |
John Thompson | de258b5 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 63 | #endif |
Steve Naroff | a7753c4 | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 64 | |
Douglas Gregor | f2430ba | 2010-07-25 17:39:21 +0000 | [diff] [blame] | 65 | /** \brief Return the default parsing options. */ |
Douglas Gregor | be2d8c6 | 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 | 4a47bca | 2010-08-09 22:28:58 +0000 | [diff] [blame] | 70 | options |= clang_defaultEditingTranslationUnitOptions(); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 71 | if (getenv("CINDEXTEST_COMPLETION_CACHING")) |
| 72 | options |= CXTranslationUnit_CacheCompletionResults; |
Argyrios Kyrtzidis | cb373e3 | 2011-11-03 02:20:25 +0000 | [diff] [blame] | 73 | if (getenv("CINDEXTEST_COMPLETION_NO_CACHING")) |
| 74 | options &= ~CXTranslationUnit_CacheCompletionResults; |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 75 | if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES")) |
| 76 | options |= CXTranslationUnit_SkipFunctionBodies; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 77 | if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS")) |
| 78 | options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 79 | |
| 80 | return options; |
| 81 | } |
| 82 | |
Patrik Hagglund | 55701d2 | 2014-02-17 11:54:08 +0000 | [diff] [blame] | 83 | /** \brief Returns 0 in case of success, non-zero in case of a failure. */ |
Argyrios Kyrtzidis | e74e822 | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 84 | static int checkForErrors(CXTranslationUnit TU); |
| 85 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [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 | 98c07e0 | 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 | 02968e5 | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 113 | end_line, end_column); |
Daniel Dunbar | 98c07e0 | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 116 | static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, |
| 117 | CXTranslationUnit *TU) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 118 | enum CXErrorCode Err = clang_createTranslationUnit2(Idx, file, TU); |
| 119 | if (Err != CXError_Success) { |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 120 | fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 121 | describeLibclangFailure(Err); |
| 122 | *TU = 0; |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 123 | return 0; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 124 | } |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
Douglas Gregor | aa98ed9 | 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 | 0e3da27 | 2010-08-19 20:50:29 +0000 | [diff] [blame] | 135 | free(unsaved_files); |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [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 | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 143 | int i; |
| 144 | int arg; |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 145 | int prefix_len = strlen(opt_name); |
| 146 | int arg_indices[20]; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 147 | *unsaved_files = 0; |
| 148 | *num_unsaved_files = 0; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 150 | /* Count the number of remapped files. */ |
| 151 | for (arg = start_arg; arg < argc; ++arg) { |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 152 | if (strncmp(argv[arg], opt_name, prefix_len)) |
| 153 | continue; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 154 | |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 155 | assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int))); |
| 156 | arg_indices[*num_unsaved_files] = arg; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 157 | ++*num_unsaved_files; |
| 158 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 160 | if (*num_unsaved_files == 0) |
| 161 | return 0; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 162 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 163 | *unsaved_files |
Douglas Gregor | 0e3da27 | 2010-08-19 20:50:29 +0000 | [diff] [blame] | 164 | = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * |
| 165 | *num_unsaved_files); |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 166 | for (i = 0; i != *num_unsaved_files; ++i) { |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 167 | struct CXUnsavedFile *unsaved = *unsaved_files + i; |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 168 | const char *arg_string = argv[arg_indices[i]] + prefix_len; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 169 | int filename_len; |
| 170 | char *filename; |
| 171 | char *contents; |
| 172 | FILE *to_file; |
Argyrios Kyrtzidis | 5899e89 | 2013-12-05 20:13:27 +0000 | [diff] [blame] | 173 | const char *sep = strchr(arg_string, ','); |
| 174 | if (!sep) { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 175 | fprintf(stderr, |
Argyrios Kyrtzidis | 5899e89 | 2013-12-05 20:13:27 +0000 | [diff] [blame] | 176 | "error: %sfrom:to argument is missing comma\n", opt_name); |
Douglas Gregor | aa98ed9 | 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 | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 183 | /* Open the file that we're remapping to. */ |
Argyrios Kyrtzidis | 5899e89 | 2013-12-05 20:13:27 +0000 | [diff] [blame] | 184 | to_file = fopen(sep + 1, "rb"); |
Douglas Gregor | aa98ed9 | 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", |
Argyrios Kyrtzidis | 5899e89 | 2013-12-05 20:13:27 +0000 | [diff] [blame] | 187 | sep + 1); |
Douglas Gregor | aa98ed9 | 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 | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 193 | |
Douglas Gregor | aa98ed9 | 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 | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 198 | |
Douglas Gregor | aa98ed9 | 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", |
Argyrios Kyrtzidis | 5899e89 | 2013-12-05 20:13:27 +0000 | [diff] [blame] | 203 | (feof(to_file) ? "EOF" : "error"), sep + 1); |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 204 | fclose(to_file); |
| 205 | free_remapped_files(*unsaved_files, i); |
Richard Smith | 1ea42eb | 2012-07-05 08:20:49 +0000 | [diff] [blame] | 206 | free(contents); |
Douglas Gregor | aa98ed9 | 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 | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 213 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 214 | /* Close the file. */ |
| 215 | fclose(to_file); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 216 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 217 | /* Copy the file name that we're remapping from. */ |
Argyrios Kyrtzidis | 5899e89 | 2013-12-05 20:13:27 +0000 | [diff] [blame] | 218 | filename_len = sep - arg_string; |
Douglas Gregor | aa98ed9 | 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 | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [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 | 740c0fb | 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 | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 283 | /******************************************************************************/ |
| 284 | /* Pretty-printing. */ |
| 285 | /******************************************************************************/ |
| 286 | |
Dmitri Gribenko | 5e4fe00 | 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 | 5188c4b | 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 | 5e4fe00 | 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 | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 308 | printf("]"); |
| 309 | } |
| 310 | |
Dmitri Gribenko | 5e4fe00 | 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 | 740c0fb | 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 | 5e4fe00 | 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 | c1679ec | 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 | 191a6a8 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 337 | if (str) |
| 338 | printf(" %s=", str); |
Douglas Gregor | c1679ec | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 339 | PrintExtent(stdout, begin_line, begin_column, end_line, end_column); |
| 340 | } |
| 341 | |
Douglas Gregor | 97c7571 | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 342 | int want_display_name = 0; |
| 343 | |
Douglas Gregor | d6225d3 | 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 | 5e4fe00 | 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 | f267c87 | 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 | 5e4fe00 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 368 | Ctx->IndentLevel++; |
Dmitri Gribenko | f267c87 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 369 | for (i = 0, e = Ctx->IndentLevel; i != e; ++i) |
Dmitri Gribenko | 5e4fe00 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 370 | printf(" "); |
| 371 | |
| 372 | printf("("); |
Dmitri Gribenko | 5e4fe00 | 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 | d73e4ce | 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 | f267c87 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 405 | for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment); |
Dmitri Gribenko | 5e4fe00 | 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 | f267c87 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 414 | case CXComment_HTMLStartTag: { |
| 415 | unsigned NumAttrs; |
Dmitri Gribenko | 5e4fe00 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 416 | printf("CXComment_HTMLStartTag"); |
| 417 | PrintCXStringWithPrefixAndDispose( |
| 418 | "Name", |
| 419 | clang_HTMLTagComment_getTagName(Comment)); |
Dmitri Gribenko | f267c87 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 420 | NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment); |
Dmitri Gribenko | 5e4fe00 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 421 | if (NumAttrs != 0) { |
| 422 | printf(" Attrs:"); |
Dmitri Gribenko | f267c87 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 423 | for (i = 0; i != NumAttrs; ++i) { |
Dmitri Gribenko | 5e4fe00 | 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 | f267c87 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 435 | } |
Dmitri Gribenko | 5e4fe00 | 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 | f267c87 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 454 | for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment); |
Dmitri Gribenko | 5e4fe00 | 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 | 34df220 | 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 | 5e4fe00 | 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 | f267c87 | 2012-07-20 22:00:35 +0000 | [diff] [blame] | 527 | unsigned i; |
| 528 | for (i = 0; i != NumChildren; ++i) { |
Dmitri Gribenko | 5e4fe00 | 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 | |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 545 | static void ValidateCommentXML(const char *Str, const char *CommentSchemaFile) { |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 546 | #ifdef CLANG_HAVE_LIBXML |
| 547 | xmlRelaxNGParserCtxtPtr RNGParser; |
| 548 | xmlRelaxNGPtr Schema; |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 549 | xmlDocPtr Doc; |
| 550 | xmlRelaxNGValidCtxtPtr ValidationCtxt; |
| 551 | int status; |
| 552 | |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 553 | if (!CommentSchemaFile) |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 554 | return; |
| 555 | |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 556 | RNGParser = xmlRelaxNGNewParserCtxt(CommentSchemaFile); |
| 557 | if (!RNGParser) { |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 558 | printf(" libXMLError"); |
| 559 | return; |
| 560 | } |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 561 | Schema = xmlRelaxNGParse(RNGParser); |
Dmitri Gribenko | 740c0fb | 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 | |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 571 | ValidationCtxt = xmlRelaxNGNewValidCtxt(Schema); |
Dmitri Gribenko | 740c0fb | 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); |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 583 | xmlRelaxNGFree(Schema); |
| 584 | xmlRelaxNGFreeParserCtxt(RNGParser); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 585 | #endif |
| 586 | } |
| 587 | |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 588 | static void PrintCursorComments(CXCursor Cursor, |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 589 | const char *CommentSchemaFile) { |
Dmitri Gribenko | 5e4fe00 | 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 | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 612 | CXComment Comment = clang_Cursor_getParsedComment(Cursor); |
Dmitri Gribenko | 5e4fe00 | 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 | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 616 | { |
| 617 | CXString XML; |
Dmitri Gribenko | 7acbf00 | 2012-09-10 20:32:42 +0000 | [diff] [blame] | 618 | XML = clang_FullComment_getAsXML(Comment); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 619 | PrintCXStringWithPrefix("FullCommentAsXML", XML); |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 620 | ValidateCommentXML(clang_getCString(XML), CommentSchemaFile); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 621 | clang_disposeString(XML); |
| 622 | } |
| 623 | |
Dmitri Gribenko | 5e4fe00 | 2012-07-20 21:34:34 +0000 | [diff] [blame] | 624 | DumpCXComment(Comment); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
Argyrios Kyrtzidis | 079ff5c | 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 | |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 642 | static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) { |
Argyrios Kyrtzidis | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 643 | CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 644 | if (clang_isInvalid(Cursor.kind)) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 645 | CXString ks = clang_getCursorKindSpelling(Cursor.kind); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 646 | printf("Invalid Cursor => %s", clang_getCString(ks)); |
| 647 | clang_disposeString(ks); |
| 648 | } |
Steve Naroff | 63f475a | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 649 | else { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 650 | CXString string, ks; |
Douglas Gregor | ad27e8b | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 651 | CXCursor Referenced; |
Douglas Gregor | 4f46e78 | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 652 | unsigned line, column; |
Douglas Gregor | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 653 | CXCursor SpecializationOf; |
Douglas Gregor | 99a26af | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 654 | CXCursor *overridden; |
| 655 | unsigned num_overridden; |
Douglas Gregor | c1679ec | 2011-07-25 17:48:11 +0000 | [diff] [blame] | 656 | unsigned RefNameRangeNr; |
| 657 | CXSourceRange CursorExtent; |
| 658 | CXSourceRange RefNameRange; |
Douglas Gregor | d6225d3 | 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 | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 666 | |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 667 | ks = clang_getCursorKindSpelling(Cursor.kind); |
Douglas Gregor | 97c7571 | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 668 | string = want_display_name? clang_getCursorDisplayName(Cursor) |
| 669 | : clang_getCursorSpelling(Cursor); |
Ted Kremenek | 2900467 | 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 | 8675d5c | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 673 | clang_disposeString(string); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | ad27e8b | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 675 | Referenced = clang_getCursorReferenced(Cursor); |
| 676 | if (!clang_equalCursors(Referenced, clang_getNullCursor())) { |
Douglas Gregor | 16a2bdd | 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 | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 681 | CXCursor Ovl = clang_getOverloadedDecl(Referenced, I); |
Douglas Gregor | 2967e28 | 2010-09-14 00:20:32 +0000 | [diff] [blame] | 682 | CXSourceLocation Loc; |
Douglas Gregor | 16a2bdd | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 683 | if (I) |
| 684 | printf(", "); |
| 685 | |
Douglas Gregor | 2967e28 | 2010-09-14 00:20:32 +0000 | [diff] [blame] | 686 | Loc = clang_getCursorLocation(Ovl); |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 687 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 16a2bdd | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 688 | printf("%d:%d", line, column); |
| 689 | } |
| 690 | printf("]"); |
| 691 | } else { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 692 | CXSourceLocation Loc = clang_getCursorLocation(Referenced); |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 693 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | 16a2bdd | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 694 | printf(":%d:%d", line, column); |
| 695 | } |
Douglas Gregor | ad27e8b | 2010-01-19 01:20:04 +0000 | [diff] [blame] | 696 | } |
Douglas Gregor | 6b8232f | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 697 | |
| 698 | if (clang_isCursorDefinition(Cursor)) |
| 699 | printf(" (Definition)"); |
Douglas Gregor | f757a12 | 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 | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 712 | |
| 713 | case CXAvailability_NotAccessible: |
| 714 | printf(" (inaccessible)"); |
| 715 | break; |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 716 | } |
Ted Kremenek | a594082 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 717 | |
Douglas Gregor | d6225d3 | 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 | a8d0c77 | 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)"); |
Dmitri Gribenko | e570ede | 2014-04-07 14:59:13 +0000 | [diff] [blame] | 763 | if (clang_CXXMethod_isConst(Cursor)) |
| 764 | printf(" (const)"); |
Dmitri Gribenko | 62770be | 2013-05-17 18:38:35 +0000 | [diff] [blame] | 765 | if (clang_CXXMethod_isPureVirtual(Cursor)) |
| 766 | printf(" (pure)"); |
Argyrios Kyrtzidis | 23814e4 | 2013-04-18 23:53:05 +0000 | [diff] [blame] | 767 | if (clang_Cursor_isVariadic(Cursor)) |
| 768 | printf(" (variadic)"); |
Argyrios Kyrtzidis | 7b50fc5 | 2013-07-05 20:44:37 +0000 | [diff] [blame] | 769 | if (clang_Cursor_isObjCOptional(Cursor)) |
| 770 | printf(" (@optional)"); |
| 771 | |
Ted Kremenek | a594082 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 772 | if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
Enea Zaffanella | 476f38a | 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 | a594082 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 776 | printf(" [IBOutletCollection=%s]", clang_getCString(S)); |
| 777 | clang_disposeString(S); |
| 778 | } |
Ted Kremenek | ae9e221 | 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 | d3f48bd | 2010-09-02 00:07:54 +0000 | [diff] [blame] | 799 | |
| 800 | SpecializationOf = clang_getSpecializedCursorTemplate(Cursor); |
| 801 | if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 802 | CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf); |
| 803 | CXString Name = clang_getCursorSpelling(SpecializationOf); |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 804 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Douglas Gregor | d3f48bd | 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 | 99a26af | 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 | 079ff5c | 2012-08-22 23:15:52 +0000 | [diff] [blame] | 813 | LineCol lineCols[50]; |
| 814 | assert(num_overridden <= 50); |
Douglas Gregor | 99a26af | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 815 | printf(" [Overrides "); |
| 816 | for (I = 0; I != num_overridden; ++I) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 817 | CXSourceLocation Loc = clang_getCursorLocation(overridden[I]); |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 818 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Argyrios Kyrtzidis | 079ff5c | 2012-08-22 23:15:52 +0000 | [diff] [blame] | 819 | lineCols[I].line = line; |
| 820 | lineCols[I].col = column; |
| 821 | } |
Michael Liao | b94f47a | 2012-08-30 00:45:32 +0000 | [diff] [blame] | 822 | /* Make the order of the override list deterministic. */ |
Argyrios Kyrtzidis | 079ff5c | 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 | 99a26af | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 825 | if (I) |
| 826 | printf(", "); |
Argyrios Kyrtzidis | 079ff5c | 2012-08-22 23:15:52 +0000 | [diff] [blame] | 827 | printf("@%d:%d", lineCols[I].line, lineCols[I].col); |
Douglas Gregor | 99a26af | 2010-10-01 20:25:15 +0000 | [diff] [blame] | 828 | } |
| 829 | printf("]"); |
| 830 | clang_disposeOverriddenCursors(overridden); |
| 831 | } |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 832 | |
| 833 | if (Cursor.kind == CXCursor_InclusionDirective) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 834 | CXFile File = clang_getIncludedFile(Cursor); |
| 835 | CXString Included = clang_getFileName(File); |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 836 | printf(" (%s)", clang_getCString(Included)); |
| 837 | clang_disposeString(Included); |
Douglas Gregor | 37aa493 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 838 | |
| 839 | if (clang_isFileMultipleIncludeGuarded(TU, File)) |
| 840 | printf(" [multi-include guarded]"); |
Douglas Gregor | 796d76a | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 841 | } |
Douglas Gregor | c1679ec | 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 | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 862 | |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 863 | PrintCursorComments(Cursor, CommentSchemaFile); |
Argyrios Kyrtzidis | 9adfd8a | 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 | 9d9bc01 | 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 | 63f475a | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 902 | } |
Steve Naroff | 38c1a7b | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 903 | } |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 904 | |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 905 | static const char* GetCursorSource(CXCursor Cursor) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 906 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 907 | CXString source; |
Douglas Gregor | 4f46e78 | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 908 | CXFile file; |
Argyrios Kyrtzidis | 7ca7735 | 2011-11-03 02:20:36 +0000 | [diff] [blame] | 909 | clang_getExpansionLocation(Loc, &file, 0, 0, 0); |
Douglas Gregor | 4f46e78 | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 910 | source = clang_getFileName(file); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 911 | if (!clang_getCString(source)) { |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 912 | clang_disposeString(source); |
| 913 | return "<invalid loc>"; |
| 914 | } |
| 915 | else { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 916 | const char *b = basename(clang_getCString(source)); |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 917 | clang_disposeString(source); |
| 918 | return b; |
| 919 | } |
Ted Kremenek | 4c4d643 | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 922 | /******************************************************************************/ |
Ted Kremenek | b478ff4 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 923 | /* Callbacks. */ |
| 924 | /******************************************************************************/ |
| 925 | |
| 926 | typedef void (*PostVisitTU)(CXTranslationUnit); |
| 927 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 928 | void PrintDiagnostic(CXDiagnostic Diagnostic) { |
| 929 | FILE *out = stderr; |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 930 | CXFile file; |
Douglas Gregor | d770f73 | 2010-02-22 23:17:23 +0000 | [diff] [blame] | 931 | CXString Msg; |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 932 | unsigned display_opts = CXDiagnostic_DisplaySourceLocation |
Douglas Gregor | a750e8e | 2010-11-19 16:18:16 +0000 | [diff] [blame] | 933 | | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges |
| 934 | | CXDiagnostic_DisplayOption; |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 935 | unsigned i, num_fixits; |
Ted Kremenek | 599d73a | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 937 | if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 938 | return; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | d770f73 | 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 | 599d73a | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 943 | |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 944 | clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| 945 | &file, 0, 0, 0); |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 946 | if (!file) |
| 947 | return; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 948 | |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 949 | num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); |
Ted Kremenek | 4a64230 | 2012-03-20 20:49:45 +0000 | [diff] [blame] | 950 | fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits); |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 951 | for (i = 0; i != num_fixits; ++i) { |
Douglas Gregor | 836ec94 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 952 | CXSourceRange range; |
Enea Zaffanella | 476f38a | 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 | 836ec94 | 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 | 229bebd | 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 | 836ec94 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 961 | if (clang_equalLocations(start, end)) { |
| 962 | /* Insertion. */ |
| 963 | if (start_file == file) |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 964 | fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", |
Douglas Gregor | 836ec94 | 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 | 1e21cc7 | 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 | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 972 | } |
Douglas Gregor | 836ec94 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 973 | } else { |
| 974 | /* Replacement. */ |
Douglas Gregor | 1e21cc7 | 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 | 836ec94 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 978 | fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); |
Douglas Gregor | 9773e3d | 2010-02-18 22:27:07 +0000 | [diff] [blame] | 979 | } |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 980 | break; |
| 981 | } |
Douglas Gregor | 836ec94 | 2010-02-19 18:16:06 +0000 | [diff] [blame] | 982 | clang_disposeString(insertion_text); |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 983 | } |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Ted Kremenek | 914c7e6 | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 986 | void PrintDiagnosticSet(CXDiagnosticSet Set) { |
| 987 | int i = 0, n = clang_getNumDiagnosticsInSet(Set); |
| 988 | for ( ; i != n ; ++i) { |
| 989 | CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i); |
| 990 | CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 991 | PrintDiagnostic(Diag); |
Ted Kremenek | 914c7e6 | 2012-02-14 02:46:03 +0000 | [diff] [blame] | 992 | if (ChildDiags) |
| 993 | PrintDiagnosticSet(ChildDiags); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | void PrintDiagnostics(CXTranslationUnit TU) { |
| 998 | CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU); |
| 999 | PrintDiagnosticSet(TUSet); |
| 1000 | clang_disposeDiagnosticSet(TUSet); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1003 | void PrintMemoryUsage(CXTranslationUnit TU) { |
Matt Beaumont-Gay | d6238f4 | 2011-08-29 16:37:29 +0000 | [diff] [blame] | 1004 | unsigned long total = 0; |
Ted Kremenek | 11d1a42 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 1005 | unsigned i = 0; |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1006 | CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU); |
Francois Pichet | 45cc546 | 2011-04-18 23:33:22 +0000 | [diff] [blame] | 1007 | fprintf(stderr, "Memory usage:\n"); |
Ted Kremenek | 11d1a42 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 1008 | for (i = 0 ; i != usage.numEntries; ++i) { |
Ted Kremenek | 2332412 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 1009 | const char *name = clang_getTUResourceUsageName(usage.entries[i].kind); |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1010 | unsigned long amount = usage.entries[i].amount; |
| 1011 | total += amount; |
Ted Kremenek | 11d1a42 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 1012 | fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount, |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1013 | ((double) amount)/(1024*1024)); |
| 1014 | } |
Ted Kremenek | 11d1a42 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 1015 | fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total, |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1016 | ((double) total)/(1024*1024)); |
Ted Kremenek | 2332412 | 2011-04-20 16:41:07 +0000 | [diff] [blame] | 1017 | clang_disposeCXTUResourceUsage(usage); |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Ted Kremenek | b478ff4 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1020 | /******************************************************************************/ |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1021 | /* Logic for testing traversal. */ |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1022 | /******************************************************************************/ |
| 1023 | |
Douglas Gregor | 33c34ac | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1024 | static void PrintCursorExtent(CXCursor C) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1025 | CXSourceRange extent = clang_getCursorExtent(C); |
| 1026 | PrintRange(extent, "Extent"); |
Ted Kremenek | a44d99c | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1029 | /* Data used by the visitors. */ |
| 1030 | typedef struct { |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1031 | CXTranslationUnit TU; |
| 1032 | enum CXCursorKind *Filter; |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 1033 | const char *CommentSchemaFile; |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1034 | } VisitorData; |
Ted Kremenek | a44d99c | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1035 | |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1036 | |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1037 | enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1038 | CXCursor Parent, |
| 1039 | CXClientData ClientData) { |
| 1040 | VisitorData *Data = (VisitorData *)ClientData; |
| 1041 | if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1042 | CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | 4f46e78 | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1043 | unsigned line, column; |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 1044 | clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
Ted Kremenek | a44d99c | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1045 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, |
Douglas Gregor | 4f46e78 | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1046 | GetCursorSource(Cursor), line, column); |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 1047 | PrintCursor(Cursor, Data->CommentSchemaFile); |
Douglas Gregor | 33c34ac | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1048 | PrintCursorExtent(Cursor); |
Argyrios Kyrtzidis | 1ab09cc | 2013-04-11 17:02:10 +0000 | [diff] [blame] | 1049 | if (clang_isDeclaration(Cursor.kind)) { |
| 1050 | enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor); |
| 1051 | const char *accessStr = 0; |
| 1052 | |
| 1053 | switch (access) { |
| 1054 | case CX_CXXInvalidAccessSpecifier: break; |
| 1055 | case CX_CXXPublic: |
| 1056 | accessStr = "public"; break; |
| 1057 | case CX_CXXProtected: |
| 1058 | accessStr = "protected"; break; |
| 1059 | case CX_CXXPrivate: |
| 1060 | accessStr = "private"; break; |
| 1061 | } |
| 1062 | |
| 1063 | if (accessStr) |
| 1064 | printf(" [access=%s]", accessStr); |
| 1065 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1066 | printf("\n"); |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1067 | return CXChildVisit_Recurse; |
Steve Naroff | 772c1a4 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 1068 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1069 | |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1070 | return CXChildVisit_Continue; |
Steve Naroff | 1054e60 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 1071 | } |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 1072 | |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1073 | static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1074 | CXCursor Parent, |
| 1075 | CXClientData ClientData) { |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1076 | const char *startBuf, *endBuf; |
| 1077 | unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; |
| 1078 | CXCursor Ref; |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1079 | VisitorData *Data = (VisitorData *)ClientData; |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1080 | |
Douglas Gregor | 6b8232f | 2010-01-19 19:34:47 +0000 | [diff] [blame] | 1081 | if (Cursor.kind != CXCursor_FunctionDecl || |
| 1082 | !clang_isCursorDefinition(Cursor)) |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1083 | return CXChildVisit_Continue; |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1084 | |
| 1085 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 1086 | &startLine, &startColumn, |
| 1087 | &endLine, &endColumn); |
| 1088 | /* Probe the entire body, looking for both decls and refs. */ |
| 1089 | curLine = startLine; |
| 1090 | curColumn = startColumn; |
| 1091 | |
| 1092 | while (startBuf < endBuf) { |
Douglas Gregor | 66a5881 | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1093 | CXSourceLocation Loc; |
Douglas Gregor | 4f46e78 | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1094 | CXFile file; |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1095 | CXString source; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1096 | |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1097 | if (*startBuf == '\n') { |
| 1098 | startBuf++; |
| 1099 | curLine++; |
| 1100 | curColumn = 1; |
| 1101 | } else if (*startBuf != '\t') |
| 1102 | curColumn++; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1103 | |
Douglas Gregor | 66a5881 | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1104 | Loc = clang_getCursorLocation(Cursor); |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 1105 | clang_getSpellingLocation(Loc, &file, 0, 0, 0); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1106 | |
Douglas Gregor | 4f46e78 | 2010-01-19 21:36:55 +0000 | [diff] [blame] | 1107 | source = clang_getFileName(file); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1108 | if (clang_getCString(source)) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1109 | CXSourceLocation RefLoc |
| 1110 | = clang_getLocation(Data->TU, file, curLine, curColumn); |
Douglas Gregor | 816fd36 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1111 | Ref = clang_getCursor(Data->TU, RefLoc); |
Douglas Gregor | 66a5881 | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1112 | if (Ref.kind == CXCursor_NoDeclFound) { |
| 1113 | /* Nothing found here; that's fine. */ |
| 1114 | } else if (Ref.kind != CXCursor_FunctionDecl) { |
| 1115 | printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), |
| 1116 | curLine, curColumn); |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 1117 | PrintCursor(Ref, Data->CommentSchemaFile); |
Douglas Gregor | 66a5881 | 2010-01-18 22:46:11 +0000 | [diff] [blame] | 1118 | printf("\n"); |
| 1119 | } |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1120 | } |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1121 | clang_disposeString(source); |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1122 | startBuf++; |
| 1123 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1125 | return CXChildVisit_Continue; |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1128 | /******************************************************************************/ |
| 1129 | /* USR testing. */ |
| 1130 | /******************************************************************************/ |
| 1131 | |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1132 | enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, |
| 1133 | CXClientData ClientData) { |
| 1134 | VisitorData *Data = (VisitorData *)ClientData; |
| 1135 | if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1136 | CXString USR = clang_getCursorUSR(C); |
| 1137 | const char *cstr = clang_getCString(USR); |
Ted Kremenek | 6d159c1 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 1138 | if (!cstr || cstr[0] == '\0') { |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1139 | clang_disposeString(USR); |
Ted Kremenek | 7afa85b | 2010-04-16 21:31:52 +0000 | [diff] [blame] | 1140 | return CXChildVisit_Recurse; |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1141 | } |
Ted Kremenek | 6d159c1 | 2010-04-20 23:15:40 +0000 | [diff] [blame] | 1142 | printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr); |
| 1143 | |
Douglas Gregor | 33c34ac | 2010-01-19 00:34:46 +0000 | [diff] [blame] | 1144 | PrintCursorExtent(C); |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1145 | printf("\n"); |
| 1146 | clang_disposeString(USR); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1148 | return CXChildVisit_Recurse; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 1151 | return CXChildVisit_Continue; |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | /******************************************************************************/ |
Ted Kremenek | 0b86e3a | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1155 | /* Inclusion stack testing. */ |
| 1156 | /******************************************************************************/ |
| 1157 | |
| 1158 | void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, |
| 1159 | unsigned includeStackLen, CXClientData data) { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1160 | |
Ted Kremenek | 0b86e3a | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1161 | unsigned i; |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1162 | CXString fname; |
| 1163 | |
| 1164 | fname = clang_getFileName(includedFile); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1165 | printf("file: %s\nincluded by:\n", clang_getCString(fname)); |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1166 | clang_disposeString(fname); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1167 | |
Ted Kremenek | 0b86e3a | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1168 | for (i = 0; i < includeStackLen; ++i) { |
| 1169 | CXFile includingFile; |
| 1170 | unsigned line, column; |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 1171 | clang_getSpellingLocation(includeStack[i], &includingFile, &line, |
| 1172 | &column, 0); |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1173 | fname = clang_getFileName(includingFile); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1174 | printf(" %s:%d:%d\n", clang_getCString(fname), line, column); |
Ted Kremenek | c560b68 | 2010-02-17 00:41:20 +0000 | [diff] [blame] | 1175 | clang_disposeString(fname); |
Ted Kremenek | 0b86e3a | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1176 | } |
| 1177 | printf("\n"); |
| 1178 | } |
| 1179 | |
| 1180 | void PrintInclusionStack(CXTranslationUnit TU) { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1181 | clang_getInclusions(TU, InclusionVisitor, NULL); |
Ted Kremenek | 0b86e3a | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | /******************************************************************************/ |
Ted Kremenek | 83b28a2 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1185 | /* Linkage testing. */ |
| 1186 | /******************************************************************************/ |
| 1187 | |
| 1188 | static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, |
| 1189 | CXClientData d) { |
| 1190 | const char *linkage = 0; |
| 1191 | |
| 1192 | if (clang_isInvalid(clang_getCursorKind(cursor))) |
| 1193 | return CXChildVisit_Recurse; |
| 1194 | |
| 1195 | switch (clang_getCursorLinkage(cursor)) { |
| 1196 | case CXLinkage_Invalid: break; |
Douglas Gregor | 0b46650 | 2010-03-04 19:36:27 +0000 | [diff] [blame] | 1197 | case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; |
| 1198 | case CXLinkage_Internal: linkage = "Internal"; break; |
| 1199 | case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; |
| 1200 | case CXLinkage_External: linkage = "External"; break; |
Ted Kremenek | 83b28a2 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | if (linkage) { |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1204 | PrintCursor(cursor, NULL); |
Ted Kremenek | 83b28a2 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 1205 | printf("linkage=%s\n", linkage); |
| 1206 | } |
| 1207 | |
| 1208 | return CXChildVisit_Recurse; |
| 1209 | } |
| 1210 | |
| 1211 | /******************************************************************************/ |
Ted Kremenek | 6bca984 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1212 | /* Typekind testing. */ |
| 1213 | /******************************************************************************/ |
| 1214 | |
Dmitri Gribenko | 0035372 | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1215 | static void PrintTypeAndTypeKind(CXType T, const char *Format) { |
| 1216 | CXString TypeSpelling, TypeKindSpelling; |
| 1217 | |
| 1218 | TypeSpelling = clang_getTypeSpelling(T); |
| 1219 | TypeKindSpelling = clang_getTypeKindSpelling(T.kind); |
| 1220 | printf(Format, |
| 1221 | clang_getCString(TypeSpelling), |
| 1222 | clang_getCString(TypeKindSpelling)); |
| 1223 | clang_disposeString(TypeSpelling); |
| 1224 | clang_disposeString(TypeKindSpelling); |
| 1225 | } |
| 1226 | |
| 1227 | static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p, |
| 1228 | CXClientData d) { |
Ted Kremenek | 6bca984 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1229 | if (!clang_isInvalid(clang_getCursorKind(cursor))) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1230 | CXType T = clang_getCursorType(cursor); |
Argyrios Kyrtzidis | adff3ae | 2013-10-11 19:58:38 +0000 | [diff] [blame] | 1231 | enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1232 | PrintCursor(cursor, NULL); |
Dmitri Gribenko | 0035372 | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1233 | PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]"); |
Douglas Gregor | 56a6380 | 2011-01-27 16:27:11 +0000 | [diff] [blame] | 1234 | if (clang_isConstQualifiedType(T)) |
| 1235 | printf(" const"); |
| 1236 | if (clang_isVolatileQualifiedType(T)) |
| 1237 | printf(" volatile"); |
| 1238 | if (clang_isRestrictQualifiedType(T)) |
| 1239 | printf(" restrict"); |
Argyrios Kyrtzidis | adff3ae | 2013-10-11 19:58:38 +0000 | [diff] [blame] | 1240 | if (RQ == CXRefQualifier_LValue) |
| 1241 | printf(" lvalue-ref-qualifier"); |
| 1242 | if (RQ == CXRefQualifier_RValue) |
| 1243 | printf(" rvalue-ref-qualifier"); |
Benjamin Kramer | 1e63c74 | 2010-06-22 09:29:44 +0000 | [diff] [blame] | 1244 | /* Print the canonical type if it is different. */ |
Ted Kremenek | c150887 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1245 | { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1246 | CXType CT = clang_getCanonicalType(T); |
Ted Kremenek | c150887 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1247 | if (!clang_equalTypes(T, CT)) { |
Dmitri Gribenko | 0035372 | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1248 | PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]"); |
Ted Kremenek | c150887 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1249 | } |
| 1250 | } |
Benjamin Kramer | 1e63c74 | 2010-06-22 09:29:44 +0000 | [diff] [blame] | 1251 | /* Print the return type if it exists. */ |
Ted Kremenek | c150887 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1252 | { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1253 | CXType RT = clang_getCursorResultType(cursor); |
Ted Kremenek | c150887 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1254 | if (RT.kind != CXType_Invalid) { |
Dmitri Gribenko | 0035372 | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1255 | PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]"); |
Ted Kremenek | c150887 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1256 | } |
| 1257 | } |
Argyrios Kyrtzidis | 0c27e4b | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1258 | /* Print the argument types if they exist. */ |
| 1259 | { |
Dmitri Gribenko | 6ede6ab | 2014-02-27 16:05:05 +0000 | [diff] [blame] | 1260 | int NumArgs = clang_Cursor_getNumArguments(cursor); |
| 1261 | if (NumArgs != -1 && NumArgs != 0) { |
Argyrios Kyrtzidis | 0880417 | 2012-04-11 19:54:09 +0000 | [diff] [blame] | 1262 | int i; |
Argyrios Kyrtzidis | 0c27e4b | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1263 | printf(" [args="); |
Dmitri Gribenko | 6ede6ab | 2014-02-27 16:05:05 +0000 | [diff] [blame] | 1264 | for (i = 0; i < NumArgs; ++i) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1265 | CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i)); |
Argyrios Kyrtzidis | 0c27e4b | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1266 | if (T.kind != CXType_Invalid) { |
Dmitri Gribenko | 0035372 | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 1267 | PrintTypeAndTypeKind(T, " [%s] [%s]"); |
Argyrios Kyrtzidis | 0c27e4b | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | printf("]"); |
| 1271 | } |
| 1272 | } |
Dmitri Gribenko | 6ede6ab | 2014-02-27 16:05:05 +0000 | [diff] [blame] | 1273 | /* Print the template argument types if they exist. */ |
| 1274 | { |
| 1275 | int NumTArgs = clang_Type_getNumTemplateArguments(T); |
| 1276 | if (NumTArgs != -1 && NumTArgs != 0) { |
| 1277 | int i; |
| 1278 | printf(" [templateargs/%d=", NumTArgs); |
| 1279 | for (i = 0; i < NumTArgs; ++i) { |
| 1280 | CXType TArg = clang_Type_getTemplateArgumentAsType(T, i); |
| 1281 | if (TArg.kind != CXType_Invalid) { |
| 1282 | PrintTypeAndTypeKind(TArg, " [type=%s] [typekind=%s]"); |
| 1283 | } |
| 1284 | } |
| 1285 | printf("]"); |
| 1286 | } |
| 1287 | } |
Ted Kremenek | 0c7476a | 2010-07-30 00:14:11 +0000 | [diff] [blame] | 1288 | /* Print if this is a non-POD type. */ |
| 1289 | printf(" [isPOD=%d]", clang_isPODType(T)); |
Anders Waldenborg | ddce74f | 2014-04-09 19:16:08 +0000 | [diff] [blame] | 1290 | /* Print the pointee type. */ |
| 1291 | { |
| 1292 | CXType PT = clang_getPointeeType(T); |
| 1293 | if (PT.kind != CXType_Invalid) { |
| 1294 | PrintTypeAndTypeKind(PT, " [pointeetype=%s] [pointeekind=%s]"); |
| 1295 | } |
| 1296 | } |
Ted Kremenek | c150887 | 2010-06-21 20:15:39 +0000 | [diff] [blame] | 1297 | |
Ted Kremenek | 6bca984 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1298 | printf("\n"); |
| 1299 | } |
| 1300 | return CXChildVisit_Recurse; |
| 1301 | } |
| 1302 | |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1303 | static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p, |
| 1304 | CXClientData d) { |
| 1305 | CXType T; |
| 1306 | enum CXCursorKind K = clang_getCursorKind(cursor); |
| 1307 | if (clang_isInvalid(K)) |
| 1308 | return CXChildVisit_Recurse; |
| 1309 | T = clang_getCursorType(cursor); |
| 1310 | PrintCursor(cursor, NULL); |
| 1311 | PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]"); |
| 1312 | /* Print the type sizeof if applicable. */ |
| 1313 | { |
| 1314 | long long Size = clang_Type_getSizeOf(T); |
| 1315 | if (Size >= 0 || Size < -1 ) { |
| 1316 | printf(" [sizeof=%lld]", Size); |
| 1317 | } |
| 1318 | } |
| 1319 | /* Print the type alignof if applicable. */ |
| 1320 | { |
| 1321 | long long Align = clang_Type_getAlignOf(T); |
| 1322 | if (Align >= 0 || Align < -1) { |
| 1323 | printf(" [alignof=%lld]", Align); |
| 1324 | } |
| 1325 | } |
| 1326 | /* Print the record field offset if applicable. */ |
| 1327 | { |
Nico Weber | 82098cb | 2014-04-24 04:14:12 +0000 | [diff] [blame] | 1328 | CXString FieldSpelling = clang_getCursorSpelling(cursor); |
| 1329 | const char *FieldName = clang_getCString(FieldSpelling); |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1330 | /* recurse to get the root anonymous record parent */ |
| 1331 | CXCursor Parent, Root; |
Nico Weber | 82098cb | 2014-04-24 04:14:12 +0000 | [diff] [blame] | 1332 | if (clang_getCursorKind(cursor) == CXCursor_FieldDecl) { |
| 1333 | CXString RootParentSpelling; |
| 1334 | const char *RootParentName = 0; |
Richard Smith | 131daca | 2014-03-06 21:59:38 +0000 | [diff] [blame] | 1335 | Parent = p; |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1336 | do { |
Nico Weber | 82098cb | 2014-04-24 04:14:12 +0000 | [diff] [blame] | 1337 | if (RootParentName != 0) |
| 1338 | clang_disposeString(RootParentSpelling); |
| 1339 | |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1340 | Root = Parent; |
Nico Weber | 82098cb | 2014-04-24 04:14:12 +0000 | [diff] [blame] | 1341 | RootParentSpelling = clang_getCursorSpelling(Root); |
| 1342 | RootParentName = clang_getCString(RootParentSpelling); |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1343 | Parent = clang_getCursorSemanticParent(Root); |
Nico Weber | 82098cb | 2014-04-24 04:14:12 +0000 | [diff] [blame] | 1344 | } while (clang_getCursorType(Parent).kind == CXType_Record && |
| 1345 | !strcmp(RootParentName, "")); |
| 1346 | clang_disposeString(RootParentSpelling); |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1347 | /* if RootParentName is "", record is anonymous. */ |
| 1348 | { |
| 1349 | long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root), |
| 1350 | FieldName); |
| 1351 | printf(" [offsetof=%lld]", Offset); |
| 1352 | } |
| 1353 | } |
Nico Weber | 82098cb | 2014-04-24 04:14:12 +0000 | [diff] [blame] | 1354 | clang_disposeString(FieldSpelling); |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 1355 | } |
| 1356 | /* Print if its a bitfield */ |
| 1357 | { |
| 1358 | int IsBitfield = clang_Cursor_isBitField(cursor); |
| 1359 | if (IsBitfield) |
| 1360 | printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor)); |
| 1361 | } |
| 1362 | printf("\n"); |
| 1363 | return CXChildVisit_Recurse; |
| 1364 | } |
| 1365 | |
Dmitri Gribenko | b506ba1 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 1366 | /******************************************************************************/ |
| 1367 | /* Bitwidth testing. */ |
| 1368 | /******************************************************************************/ |
| 1369 | |
| 1370 | static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p, |
| 1371 | CXClientData d) { |
NAKAMURA Takumi | dfaed1b | 2012-12-04 15:32:03 +0000 | [diff] [blame] | 1372 | int Bitwidth; |
Dmitri Gribenko | b506ba1 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 1373 | if (clang_getCursorKind(cursor) != CXCursor_FieldDecl) |
| 1374 | return CXChildVisit_Recurse; |
| 1375 | |
NAKAMURA Takumi | dfaed1b | 2012-12-04 15:32:03 +0000 | [diff] [blame] | 1376 | Bitwidth = clang_getFieldDeclBitWidth(cursor); |
Dmitri Gribenko | b506ba1 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 1377 | if (Bitwidth >= 0) { |
| 1378 | PrintCursor(cursor, NULL); |
| 1379 | printf(" bitwidth=%d\n", Bitwidth); |
| 1380 | } |
| 1381 | |
| 1382 | return CXChildVisit_Recurse; |
| 1383 | } |
Ted Kremenek | 6bca984 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 1384 | |
| 1385 | /******************************************************************************/ |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 1386 | /* Loading ASTs/source. */ |
| 1387 | /******************************************************************************/ |
| 1388 | |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1389 | static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, |
Ted Kremenek | 73eccd2 | 2010-01-12 18:53:15 +0000 | [diff] [blame] | 1390 | const char *filter, const char *prefix, |
Ted Kremenek | b478ff4 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1391 | CXCursorVisitor Visitor, |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1392 | PostVisitTU PV, |
| 1393 | const char *CommentSchemaFile) { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1394 | |
Ted Kremenek | a44d99c | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1395 | if (prefix) |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1396 | FileCheckPrefix = prefix; |
Ted Kremenek | a97a5cd | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1397 | |
| 1398 | if (Visitor) { |
| 1399 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 1400 | enum CXCursorKind *ck = &K; |
| 1401 | VisitorData Data; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1402 | |
Ted Kremenek | a97a5cd | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1403 | /* Perform some simple filtering. */ |
| 1404 | if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
Douglas Gregor | 97c7571 | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 1405 | else if (!strcmp(filter, "all-display") || |
| 1406 | !strcmp(filter, "local-display")) { |
| 1407 | ck = NULL; |
| 1408 | want_display_name = 1; |
| 1409 | } |
Daniel Dunbar | d64ce7b | 2010-02-10 20:42:40 +0000 | [diff] [blame] | 1410 | else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; |
Ted Kremenek | a97a5cd | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1411 | else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| 1412 | else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 1413 | else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 1414 | else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| 1415 | else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
| 1416 | else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; |
| 1417 | else { |
| 1418 | fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| 1419 | return 1; |
| 1420 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1421 | |
Ted Kremenek | a97a5cd | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1422 | Data.TU = TU; |
| 1423 | Data.Filter = ck; |
Chandler Carruth | b2faa59 | 2014-05-02 23:30:59 +0000 | [diff] [blame] | 1424 | Data.CommentSchemaFile = CommentSchemaFile; |
Ted Kremenek | a97a5cd | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1425 | clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1426 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1427 | |
Ted Kremenek | b478ff4 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1428 | if (PV) |
| 1429 | PV(TU); |
Ted Kremenek | a97a5cd | 2010-01-26 17:55:33 +0000 | [diff] [blame] | 1430 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1431 | PrintDiagnostics(TU); |
Argyrios Kyrtzidis | 7048049 | 2011-11-13 23:39:14 +0000 | [diff] [blame] | 1432 | if (checkForErrors(TU) != 0) { |
| 1433 | clang_disposeTranslationUnit(TU); |
| 1434 | return -1; |
| 1435 | } |
| 1436 | |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1437 | clang_disposeTranslationUnit(TU); |
| 1438 | return 0; |
| 1439 | } |
| 1440 | |
Ted Kremenek | a44d99c | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 1441 | int perform_test_load_tu(const char *file, const char *filter, |
Ted Kremenek | b478ff4 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1442 | const char *prefix, CXCursorVisitor Visitor, |
| 1443 | PostVisitTU PV) { |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1444 | CXIndex Idx; |
| 1445 | CXTranslationUnit TU; |
Ted Kremenek | 50228be | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1446 | int result; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1447 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1448 | !strcmp(filter, "local") ? 1 : 0, |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 1449 | /* displayDiagnostics=*/1); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1450 | |
Ted Kremenek | 50228be | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1451 | if (!CreateTranslationUnit(Idx, file, &TU)) { |
| 1452 | clang_disposeIndex(Idx); |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1453 | return 1; |
Ted Kremenek | 50228be | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1454 | } |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1455 | |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1456 | result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL); |
Ted Kremenek | 50228be | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1457 | clang_disposeIndex(Idx); |
| 1458 | return result; |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Ted Kremenek | b478ff4 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 1461 | int perform_test_load_source(int argc, const char **argv, |
| 1462 | const char *filter, CXCursorVisitor Visitor, |
| 1463 | PostVisitTU PV) { |
Daniel Dunbar | 3e535d7 | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1464 | CXIndex Idx; |
| 1465 | CXTranslationUnit TU; |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1466 | const char *CommentSchemaFile; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1467 | struct CXUnsavedFile *unsaved_files = 0; |
| 1468 | int num_unsaved_files = 0; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1469 | enum CXErrorCode Err; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1470 | int result; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1471 | |
Daniel Dunbar | 3e535d7 | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1472 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
Douglas Gregor | 97c7571 | 2010-10-02 22:49:11 +0000 | [diff] [blame] | 1473 | (!strcmp(filter, "local") || |
| 1474 | !strcmp(filter, "local-display"))? 1 : 0, |
Argyrios Kyrtzidis | bcc8a5a | 2013-04-09 20:29:24 +0000 | [diff] [blame] | 1475 | /* displayDiagnostics=*/1); |
Daniel Dunbar | 3e535d7 | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1476 | |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1477 | if ((CommentSchemaFile = parse_comments_schema(argc, argv))) { |
| 1478 | argc--; |
| 1479 | argv++; |
| 1480 | } |
| 1481 | |
Ted Kremenek | 50228be | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1482 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 1483 | clang_disposeIndex(Idx); |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1484 | return -1; |
Ted Kremenek | 50228be | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1485 | } |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1486 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1487 | Err = clang_parseTranslationUnit2(Idx, 0, |
| 1488 | argv + num_unsaved_files, |
| 1489 | argc - num_unsaved_files, |
| 1490 | unsaved_files, num_unsaved_files, |
| 1491 | getDefaultParsingOptions(), &TU); |
| 1492 | if (Err != CXError_Success) { |
Daniel Dunbar | 3e535d7 | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1493 | fprintf(stderr, "Unable to load translation unit!\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1494 | describeLibclangFailure(Err); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1495 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 50228be | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1496 | clang_disposeIndex(Idx); |
Daniel Dunbar | 3e535d7 | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1497 | return 1; |
| 1498 | } |
| 1499 | |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1500 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, |
| 1501 | CommentSchemaFile); |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1502 | free_remapped_files(unsaved_files, num_unsaved_files); |
Ted Kremenek | 50228be | 2010-02-11 07:41:25 +0000 | [diff] [blame] | 1503 | clang_disposeIndex(Idx); |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1504 | return result; |
Daniel Dunbar | 3e535d7 | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1507 | int perform_test_reparse_source(int argc, const char **argv, int trials, |
| 1508 | const char *filter, CXCursorVisitor Visitor, |
| 1509 | PostVisitTU PV) { |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1510 | CXIndex Idx; |
| 1511 | CXTranslationUnit TU; |
| 1512 | struct CXUnsavedFile *unsaved_files = 0; |
| 1513 | int num_unsaved_files = 0; |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 1514 | int compiler_arg_idx = 0; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1515 | enum CXErrorCode Err; |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 1516 | int result, i; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1517 | int trial; |
Argyrios Kyrtzidis | 3405baa | 2011-09-12 18:09:31 +0000 | [diff] [blame] | 1518 | int remap_after_trial = 0; |
| 1519 | char *endptr = 0; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1520 | |
| 1521 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| 1522 | !strcmp(filter, "local") ? 1 : 0, |
Argyrios Kyrtzidis | bcc8a5a | 2013-04-09 20:29:24 +0000 | [diff] [blame] | 1523 | /* displayDiagnostics=*/1); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1524 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1525 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 1526 | clang_disposeIndex(Idx); |
| 1527 | return -1; |
| 1528 | } |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 1529 | |
| 1530 | for (i = 0; i < argc; ++i) { |
| 1531 | if (strcmp(argv[i], "--") == 0) |
| 1532 | break; |
| 1533 | } |
| 1534 | if (i < argc) |
| 1535 | compiler_arg_idx = i+1; |
| 1536 | if (num_unsaved_files > compiler_arg_idx) |
| 1537 | compiler_arg_idx = num_unsaved_files; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1538 | |
Daniel Dunbar | ec29d71 | 2010-08-18 23:09:16 +0000 | [diff] [blame] | 1539 | /* Load the initial translation unit -- we do this without honoring remapped |
| 1540 | * files, so that we have a way to test results after changing the source. */ |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1541 | Err = clang_parseTranslationUnit2(Idx, 0, |
| 1542 | argv + compiler_arg_idx, |
| 1543 | argc - compiler_arg_idx, |
| 1544 | 0, 0, getDefaultParsingOptions(), &TU); |
| 1545 | if (Err != CXError_Success) { |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1546 | fprintf(stderr, "Unable to load translation unit!\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1547 | describeLibclangFailure(Err); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1548 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1549 | clang_disposeIndex(Idx); |
| 1550 | return 1; |
| 1551 | } |
| 1552 | |
Argyrios Kyrtzidis | e74e822 | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 1553 | if (checkForErrors(TU) != 0) |
| 1554 | return -1; |
| 1555 | |
Argyrios Kyrtzidis | 3405baa | 2011-09-12 18:09:31 +0000 | [diff] [blame] | 1556 | if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) { |
| 1557 | remap_after_trial = |
| 1558 | strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10); |
| 1559 | } |
| 1560 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1561 | for (trial = 0; trial < trials; ++trial) { |
Argyrios Kyrtzidis | 011e6a5 | 2013-12-05 08:19:23 +0000 | [diff] [blame] | 1562 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1563 | if (parse_remapped_files_with_try(trial, argc, argv, 0, |
| 1564 | &unsaved_files, &num_unsaved_files)) { |
| 1565 | clang_disposeTranslationUnit(TU); |
| 1566 | clang_disposeIndex(Idx); |
| 1567 | return -1; |
| 1568 | } |
| 1569 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1570 | Err = clang_reparseTranslationUnit( |
| 1571 | TU, |
| 1572 | trial >= remap_after_trial ? num_unsaved_files : 0, |
| 1573 | trial >= remap_after_trial ? unsaved_files : 0, |
| 1574 | clang_defaultReparseOptions(TU)); |
| 1575 | if (Err != CXError_Success) { |
Daniel Dunbar | ec29d71 | 2010-08-18 23:09:16 +0000 | [diff] [blame] | 1576 | fprintf(stderr, "Unable to reparse translation unit!\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1577 | describeLibclangFailure(Err); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1578 | clang_disposeTranslationUnit(TU); |
| 1579 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1580 | clang_disposeIndex(Idx); |
| 1581 | return -1; |
| 1582 | } |
Argyrios Kyrtzidis | e74e822 | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 1583 | |
| 1584 | if (checkForErrors(TU) != 0) |
| 1585 | return -1; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1588 | result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL); |
Argyrios Kyrtzidis | e74e822 | 2011-11-13 22:08:33 +0000 | [diff] [blame] | 1589 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1590 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 1591 | clang_disposeIndex(Idx); |
| 1592 | return result; |
| 1593 | } |
| 1594 | |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1595 | /******************************************************************************/ |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1596 | /* Logic for testing clang_getCursor(). */ |
| 1597 | /******************************************************************************/ |
| 1598 | |
Douglas Gregor | 37aa493 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 1599 | static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor, |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1600 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 0469b7e | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1601 | unsigned end_line, unsigned end_col, |
| 1602 | const char *prefix) { |
Ted Kremenek | b58514e | 2010-01-07 01:17:12 +0000 | [diff] [blame] | 1603 | printf("// %s: ", FileCheckPrefix); |
Ted Kremenek | 0469b7e | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1604 | if (prefix) |
| 1605 | printf("-%s", prefix); |
Daniel Dunbar | 98c07e0 | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 1606 | PrintExtent(stdout, start_line, start_col, end_line, end_col); |
| 1607 | printf(" "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 1608 | PrintCursor(cursor, NULL); |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1609 | printf("\n"); |
| 1610 | } |
| 1611 | |
Ted Kremenek | 0469b7e | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 1612 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 1613 | const char *prefix) { |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1614 | CXIndex Idx; |
| 1615 | CXTranslationUnit TU; |
| 1616 | FILE *fp; |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1617 | CXCursor prevCursor = clang_getNullCursor(); |
Douglas Gregor | 816fd36 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1618 | CXFile file; |
Daniel Dunbar | eb27e7d | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1619 | unsigned line = 1, col = 1; |
Daniel Dunbar | 6092d50 | 2010-02-14 08:32:51 +0000 | [diff] [blame] | 1620 | unsigned start_line = 1, start_col = 1; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1621 | |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1622 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 1623 | /* displayDiagnostics=*/1))) { |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1624 | fprintf(stderr, "Could not create Index\n"); |
| 1625 | return 1; |
| 1626 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1627 | |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1628 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 1629 | return 1; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1630 | |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1631 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 1632 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 1633 | return 1; |
| 1634 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1635 | |
Douglas Gregor | 816fd36 | 2010-01-22 21:44:22 +0000 | [diff] [blame] | 1636 | file = clang_getFile(TU, source_file); |
Daniel Dunbar | eb27e7d | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1637 | for (;;) { |
| 1638 | CXCursor cursor; |
| 1639 | int c = fgetc(fp); |
Benjamin Kramer | 10d08317 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 1640 | |
Daniel Dunbar | eb27e7d | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1641 | if (c == '\n') { |
| 1642 | ++line; |
| 1643 | col = 1; |
| 1644 | } else |
| 1645 | ++col; |
| 1646 | |
| 1647 | /* Check the cursor at this position, and dump the previous one if we have |
| 1648 | * found something new. |
| 1649 | */ |
| 1650 | cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); |
| 1651 | if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && |
| 1652 | prevCursor.kind != CXCursor_InvalidFile) { |
Douglas Gregor | 37aa493 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 1653 | print_cursor_file_scan(TU, prevCursor, start_line, start_col, |
Daniel Dunbar | 02968e5 | 2010-02-14 10:02:57 +0000 | [diff] [blame] | 1654 | line, col, prefix); |
Daniel Dunbar | eb27e7d | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1655 | start_line = line; |
| 1656 | start_col = col; |
Benjamin Kramer | 10d08317 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 1657 | } |
Daniel Dunbar | eb27e7d | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1658 | if (c == EOF) |
| 1659 | break; |
Benjamin Kramer | 10d08317 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 1660 | |
Daniel Dunbar | eb27e7d | 2010-02-14 08:32:32 +0000 | [diff] [blame] | 1661 | prevCursor = cursor; |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1662 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1663 | |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1664 | fclose(fp); |
Douglas Gregor | 7a964ad | 2011-01-31 22:04:05 +0000 | [diff] [blame] | 1665 | clang_disposeTranslationUnit(TU); |
| 1666 | clang_disposeIndex(Idx); |
Ted Kremenek | 2df52dc | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 1667 | return 0; |
| 1668 | } |
| 1669 | |
| 1670 | /******************************************************************************/ |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1671 | /* Logic for testing clang code completion. */ |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 1672 | /******************************************************************************/ |
| 1673 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1674 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 1675 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 1676 | memory (that will be owned by the caller) to store the file name. */ |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1677 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1678 | unsigned *column, unsigned *second_line, |
| 1679 | unsigned *second_column) { |
Douglas Gregor | f96ea29 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 1680 | /* Find the second colon. */ |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1681 | const char *last_colon = strrchr(input, ':'); |
| 1682 | unsigned values[4], i; |
| 1683 | unsigned num_values = (second_line && second_column)? 4 : 2; |
| 1684 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1685 | char *endptr = 0; |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1686 | if (!last_colon || last_colon == input) { |
| 1687 | if (num_values == 4) |
| 1688 | fprintf(stderr, "could not parse filename:line:column:line:column in " |
| 1689 | "'%s'\n", input); |
| 1690 | else |
| 1691 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1692 | return 1; |
| 1693 | } |
| 1694 | |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1695 | for (i = 0; i != num_values; ++i) { |
| 1696 | const char *prev_colon; |
| 1697 | |
| 1698 | /* Parse the next line or column. */ |
| 1699 | values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); |
| 1700 | if (*endptr != 0 && *endptr != ':') { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1701 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1702 | (i % 2 ? "column" : "line"), input); |
| 1703 | return 1; |
| 1704 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1705 | |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1706 | if (i + 1 == num_values) |
| 1707 | break; |
| 1708 | |
| 1709 | /* Find the previous colon. */ |
| 1710 | prev_colon = last_colon - 1; |
| 1711 | while (prev_colon != input && *prev_colon != ':') |
| 1712 | --prev_colon; |
| 1713 | if (prev_colon == input) { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1714 | fprintf(stderr, "could not parse %s in '%s'\n", |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1715 | (i % 2 == 0? "column" : "line"), input); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1716 | return 1; |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | last_colon = prev_colon; |
Douglas Gregor | f96ea29 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1722 | *line = values[0]; |
| 1723 | *column = values[1]; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1724 | |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1725 | if (second_line && second_column) { |
| 1726 | *second_line = values[2]; |
| 1727 | *second_column = values[3]; |
| 1728 | } |
| 1729 | |
Douglas Gregor | f96ea29 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 1730 | /* Copy the file name. */ |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 1731 | *filename = (char*)malloc(last_colon - input + 1); |
| 1732 | memcpy(*filename, input, last_colon - input); |
| 1733 | (*filename)[last_colon - input] = 0; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | const char * |
| 1738 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 1739 | switch (Kind) { |
| 1740 | case CXCompletionChunk_Optional: return "Optional"; |
| 1741 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 1742 | case CXCompletionChunk_Text: return "Text"; |
| 1743 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 1744 | case CXCompletionChunk_Informative: return "Informative"; |
| 1745 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 1746 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 1747 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 1748 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 1749 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 1750 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 1751 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 1752 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 1753 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 1754 | case CXCompletionChunk_Comma: return "Comma"; |
Douglas Gregor | b3fa919 | 2009-12-18 18:53:37 +0000 | [diff] [blame] | 1755 | case CXCompletionChunk_ResultType: return "ResultType"; |
Douglas Gregor | 504a6ae | 2010-01-10 23:08:15 +0000 | [diff] [blame] | 1756 | case CXCompletionChunk_Colon: return "Colon"; |
| 1757 | case CXCompletionChunk_SemiColon: return "SemiColon"; |
| 1758 | case CXCompletionChunk_Equal: return "Equal"; |
| 1759 | case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; |
| 1760 | case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1761 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1762 | |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1763 | return "Unknown"; |
| 1764 | } |
| 1765 | |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 1766 | static int checkForErrors(CXTranslationUnit TU) { |
| 1767 | unsigned Num, i; |
| 1768 | CXDiagnostic Diag; |
| 1769 | CXString DiagStr; |
| 1770 | |
| 1771 | if (!getenv("CINDEXTEST_FAILONERROR")) |
| 1772 | return 0; |
| 1773 | |
| 1774 | Num = clang_getNumDiagnostics(TU); |
| 1775 | for (i = 0; i != Num; ++i) { |
| 1776 | Diag = clang_getDiagnostic(TU, i); |
| 1777 | if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) { |
| 1778 | DiagStr = clang_formatDiagnostic(Diag, |
| 1779 | clang_defaultDiagnosticDisplayOptions()); |
| 1780 | fprintf(stderr, "%s\n", clang_getCString(DiagStr)); |
| 1781 | clang_disposeString(DiagStr); |
| 1782 | clang_disposeDiagnostic(Diag); |
| 1783 | return -1; |
| 1784 | } |
| 1785 | clang_disposeDiagnostic(Diag); |
| 1786 | } |
| 1787 | |
| 1788 | return 0; |
| 1789 | } |
| 1790 | |
Douglas Gregor | 8b14f8f | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1791 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | 4ba3b29 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 1792 | int I, N; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | 8b14f8f | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1794 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1795 | for (I = 0; I != N; ++I) { |
Ted Kremenek | f602f96 | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1796 | CXString text; |
| 1797 | const char *cstr; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1798 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 8b14f8f | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1799 | = clang_getCompletionChunkKind(completion_string, I); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | 8b14f8f | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1801 | if (Kind == CXCompletionChunk_Optional) { |
| 1802 | fprintf(file, "{Optional "); |
| 1803 | print_completion_string( |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1804 | clang_getCompletionChunkCompletionString(completion_string, I), |
Douglas Gregor | 8b14f8f | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1805 | file); |
| 1806 | fprintf(file, "}"); |
| 1807 | continue; |
Douglas Gregor | 8ed5b77 | 2010-10-08 20:39:29 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | if (Kind == CXCompletionChunk_VerticalSpace) { |
| 1811 | fprintf(file, "{VerticalSpace }"); |
| 1812 | continue; |
Douglas Gregor | 8b14f8f | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1813 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1814 | |
Douglas Gregor | f81f528 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 1815 | text = clang_getCompletionChunkText(completion_string, I); |
Ted Kremenek | f602f96 | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1816 | cstr = clang_getCString(text); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1817 | fprintf(file, "{%s %s}", |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1818 | clang_getCompletionChunkKindSpelling(Kind), |
Ted Kremenek | f602f96 | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1819 | cstr ? cstr : ""); |
| 1820 | clang_disposeString(text); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1821 | } |
Ted Kremenek | f602f96 | 2010-02-17 01:42:24 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 8b14f8f | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | void print_completion_result(CXCompletionResult *completion_result, |
| 1826 | CXClientData client_data) { |
| 1827 | FILE *file = (FILE *)client_data; |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1828 | CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 1829 | unsigned annotationCount; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1830 | enum CXCursorKind ParentKind; |
| 1831 | CXString ParentName; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1832 | CXString BriefComment; |
| 1833 | const char *BriefCommentCString; |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1834 | |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 1835 | fprintf(file, "%s:", clang_getCString(ks)); |
| 1836 | clang_disposeString(ks); |
| 1837 | |
Douglas Gregor | 8b14f8f | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 1838 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1839 | fprintf(file, " (%u)", |
Douglas Gregor | a2db793 | 2010-05-26 22:00:08 +0000 | [diff] [blame] | 1840 | clang_getCompletionPriority(completion_result->CompletionString)); |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1841 | switch (clang_getCompletionAvailability(completion_result->CompletionString)){ |
| 1842 | case CXAvailability_Available: |
| 1843 | break; |
| 1844 | |
| 1845 | case CXAvailability_Deprecated: |
| 1846 | fprintf(file, " (deprecated)"); |
| 1847 | break; |
| 1848 | |
| 1849 | case CXAvailability_NotAvailable: |
| 1850 | fprintf(file, " (unavailable)"); |
| 1851 | break; |
Erik Verbruggen | 2e657ff | 2011-10-06 07:27:49 +0000 | [diff] [blame] | 1852 | |
| 1853 | case CXAvailability_NotAccessible: |
| 1854 | fprintf(file, " (inaccessible)"); |
| 1855 | break; |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1856 | } |
Erik Verbruggen | 98ea7f6 | 2011-10-14 15:31:08 +0000 | [diff] [blame] | 1857 | |
| 1858 | annotationCount = clang_getCompletionNumAnnotations( |
| 1859 | completion_result->CompletionString); |
| 1860 | if (annotationCount) { |
| 1861 | unsigned i; |
| 1862 | fprintf(file, " ("); |
| 1863 | for (i = 0; i < annotationCount; ++i) { |
| 1864 | if (i != 0) |
| 1865 | fprintf(file, ", "); |
| 1866 | fprintf(file, "\"%s\"", |
| 1867 | clang_getCString(clang_getCompletionAnnotation( |
| 1868 | completion_result->CompletionString, i))); |
| 1869 | } |
| 1870 | fprintf(file, ")"); |
| 1871 | } |
| 1872 | |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1873 | if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) { |
| 1874 | ParentName = clang_getCompletionParent(completion_result->CompletionString, |
| 1875 | &ParentKind); |
| 1876 | if (ParentKind != CXCursor_NotImplemented) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 1877 | CXString KindSpelling = clang_getCursorKindSpelling(ParentKind); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1878 | fprintf(file, " (parent: %s '%s')", |
| 1879 | clang_getCString(KindSpelling), |
| 1880 | clang_getCString(ParentName)); |
| 1881 | clang_disposeString(KindSpelling); |
| 1882 | } |
| 1883 | clang_disposeString(ParentName); |
| 1884 | } |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1885 | |
| 1886 | BriefComment = clang_getCompletionBriefComment( |
| 1887 | completion_result->CompletionString); |
| 1888 | BriefCommentCString = clang_getCString(BriefComment); |
| 1889 | if (BriefCommentCString && *BriefCommentCString != '\0') { |
| 1890 | fprintf(file, "(brief comment: %s)", BriefCommentCString); |
| 1891 | } |
| 1892 | clang_disposeString(BriefComment); |
Douglas Gregor | 78254c8 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 1893 | |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 1894 | fprintf(file, "\n"); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1897 | void print_completion_contexts(unsigned long long contexts, FILE *file) { |
| 1898 | fprintf(file, "Completion contexts:\n"); |
| 1899 | if (contexts == CXCompletionContext_Unknown) { |
| 1900 | fprintf(file, "Unknown\n"); |
| 1901 | } |
| 1902 | if (contexts & CXCompletionContext_AnyType) { |
| 1903 | fprintf(file, "Any type\n"); |
| 1904 | } |
| 1905 | if (contexts & CXCompletionContext_AnyValue) { |
| 1906 | fprintf(file, "Any value\n"); |
| 1907 | } |
| 1908 | if (contexts & CXCompletionContext_ObjCObjectValue) { |
| 1909 | fprintf(file, "Objective-C object value\n"); |
| 1910 | } |
| 1911 | if (contexts & CXCompletionContext_ObjCSelectorValue) { |
| 1912 | fprintf(file, "Objective-C selector value\n"); |
| 1913 | } |
| 1914 | if (contexts & CXCompletionContext_CXXClassTypeValue) { |
| 1915 | fprintf(file, "C++ class type value\n"); |
| 1916 | } |
| 1917 | if (contexts & CXCompletionContext_DotMemberAccess) { |
| 1918 | fprintf(file, "Dot member access\n"); |
| 1919 | } |
| 1920 | if (contexts & CXCompletionContext_ArrowMemberAccess) { |
| 1921 | fprintf(file, "Arrow member access\n"); |
| 1922 | } |
| 1923 | if (contexts & CXCompletionContext_ObjCPropertyAccess) { |
| 1924 | fprintf(file, "Objective-C property access\n"); |
| 1925 | } |
| 1926 | if (contexts & CXCompletionContext_EnumTag) { |
| 1927 | fprintf(file, "Enum tag\n"); |
| 1928 | } |
| 1929 | if (contexts & CXCompletionContext_UnionTag) { |
| 1930 | fprintf(file, "Union tag\n"); |
| 1931 | } |
| 1932 | if (contexts & CXCompletionContext_StructTag) { |
| 1933 | fprintf(file, "Struct tag\n"); |
| 1934 | } |
| 1935 | if (contexts & CXCompletionContext_ClassTag) { |
| 1936 | fprintf(file, "Class name\n"); |
| 1937 | } |
| 1938 | if (contexts & CXCompletionContext_Namespace) { |
| 1939 | fprintf(file, "Namespace or namespace alias\n"); |
| 1940 | } |
| 1941 | if (contexts & CXCompletionContext_NestedNameSpecifier) { |
| 1942 | fprintf(file, "Nested name specifier\n"); |
| 1943 | } |
| 1944 | if (contexts & CXCompletionContext_ObjCInterface) { |
| 1945 | fprintf(file, "Objective-C interface\n"); |
| 1946 | } |
| 1947 | if (contexts & CXCompletionContext_ObjCProtocol) { |
| 1948 | fprintf(file, "Objective-C protocol\n"); |
| 1949 | } |
| 1950 | if (contexts & CXCompletionContext_ObjCCategory) { |
| 1951 | fprintf(file, "Objective-C category\n"); |
| 1952 | } |
| 1953 | if (contexts & CXCompletionContext_ObjCInstanceMessage) { |
| 1954 | fprintf(file, "Objective-C instance method\n"); |
| 1955 | } |
| 1956 | if (contexts & CXCompletionContext_ObjCClassMessage) { |
| 1957 | fprintf(file, "Objective-C class method\n"); |
| 1958 | } |
| 1959 | if (contexts & CXCompletionContext_ObjCSelectorName) { |
| 1960 | fprintf(file, "Objective-C selector name\n"); |
| 1961 | } |
| 1962 | if (contexts & CXCompletionContext_MacroName) { |
| 1963 | fprintf(file, "Macro name\n"); |
| 1964 | } |
| 1965 | if (contexts & CXCompletionContext_NaturalLanguage) { |
| 1966 | fprintf(file, "Natural language\n"); |
| 1967 | } |
| 1968 | } |
| 1969 | |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1970 | int my_stricmp(const char *s1, const char *s2) { |
| 1971 | while (*s1 && *s2) { |
NAKAMURA Takumi | d3cc220 | 2011-03-09 03:02:28 +0000 | [diff] [blame] | 1972 | int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2); |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 1973 | if (c1 < c2) |
| 1974 | return -1; |
| 1975 | else if (c1 > c2) |
| 1976 | return 1; |
| 1977 | |
| 1978 | ++s1; |
| 1979 | ++s2; |
| 1980 | } |
| 1981 | |
| 1982 | if (*s1) |
| 1983 | return 1; |
| 1984 | else if (*s2) |
| 1985 | return -1; |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
Douglas Gregor | 47815d5 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 1989 | int perform_code_completion(int argc, const char **argv, int timing_only) { |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 1990 | const char *input = argv[1]; |
| 1991 | char *filename = 0; |
| 1992 | unsigned line; |
| 1993 | unsigned column; |
Daniel Dunbar | 4ba3b29 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 1994 | CXIndex CIdx; |
Ted Kremenek | ef3339b | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 1995 | int errorCode; |
Douglas Gregor | 9485bf9 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 1996 | struct CXUnsavedFile *unsaved_files = 0; |
| 1997 | int num_unsaved_files = 0; |
Douglas Gregor | f72b6ac | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 1998 | CXCodeCompleteResults *results = 0; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 1999 | enum CXErrorCode Err; |
| 2000 | CXTranslationUnit TU; |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2001 | unsigned I, Repeats = 1; |
| 2002 | unsigned completionOptions = clang_defaultCodeCompleteOptions(); |
| 2003 | |
| 2004 | if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS")) |
| 2005 | completionOptions |= CXCodeComplete_IncludeCodePatterns; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2006 | if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS")) |
| 2007 | completionOptions |= CXCodeComplete_IncludeBriefComments; |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2008 | |
Douglas Gregor | 47815d5 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 2009 | if (timing_only) |
| 2010 | input += strlen("-code-completion-timing="); |
| 2011 | else |
| 2012 | input += strlen("-code-completion-at="); |
| 2013 | |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2014 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2015 | 0, 0))) |
Ted Kremenek | ef3339b | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2016 | return errorCode; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2017 | |
Douglas Gregor | 9485bf9 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 2018 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 2019 | return -1; |
| 2020 | |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2021 | CIdx = clang_createIndex(0, 0); |
| 2022 | |
| 2023 | if (getenv("CINDEXTEST_EDITING")) |
| 2024 | Repeats = 5; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2025 | |
| 2026 | Err = clang_parseTranslationUnit2(CIdx, 0, |
| 2027 | argv + num_unsaved_files + 2, |
| 2028 | argc - num_unsaved_files - 2, |
| 2029 | 0, 0, getDefaultParsingOptions(), &TU); |
| 2030 | if (Err != CXError_Success) { |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2031 | fprintf(stderr, "Unable to load translation unit!\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2032 | describeLibclangFailure(Err); |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2033 | return 1; |
| 2034 | } |
Douglas Gregor | c659292 | 2010-11-15 23:00:34 +0000 | [diff] [blame] | 2035 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2036 | Err = clang_reparseTranslationUnit(TU, 0, 0, |
| 2037 | clang_defaultReparseOptions(TU)); |
| 2038 | |
| 2039 | if (Err != CXError_Success) { |
Douglas Gregor | c659292 | 2010-11-15 23:00:34 +0000 | [diff] [blame] | 2040 | fprintf(stderr, "Unable to reparse translation init!\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2041 | describeLibclangFailure(Err); |
| 2042 | clang_disposeTranslationUnit(TU); |
Douglas Gregor | c659292 | 2010-11-15 23:00:34 +0000 | [diff] [blame] | 2043 | return 1; |
| 2044 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2045 | |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2046 | for (I = 0; I != Repeats; ++I) { |
| 2047 | results = clang_codeCompleteAt(TU, filename, line, column, |
| 2048 | unsaved_files, num_unsaved_files, |
| 2049 | completionOptions); |
| 2050 | if (!results) { |
| 2051 | fprintf(stderr, "Unable to perform code completion!\n"); |
Daniel Dunbar | 186f742 | 2010-08-19 23:44:06 +0000 | [diff] [blame] | 2052 | return 1; |
| 2053 | } |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 2054 | if (I != Repeats-1) |
| 2055 | clang_disposeCodeCompleteResults(results); |
| 2056 | } |
Douglas Gregor | ba965fb | 2010-01-28 00:56:43 +0000 | [diff] [blame] | 2057 | |
Douglas Gregor | f72b6ac | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 2058 | if (results) { |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 2059 | unsigned i, n = results->NumResults, containerIsIncomplete = 0; |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 2060 | unsigned long long contexts; |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 2061 | enum CXCursorKind containerKind; |
Douglas Gregor | ea77740 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 2062 | CXString objCSelector; |
| 2063 | const char *selectorString; |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 2064 | if (!timing_only) { |
| 2065 | /* Sort the code-completion results based on the typed text. */ |
| 2066 | clang_sortCodeCompletionResults(results->Results, results->NumResults); |
| 2067 | |
Douglas Gregor | 47815d5 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 2068 | for (i = 0; i != n; ++i) |
| 2069 | print_completion_result(results->Results + i, stdout); |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 2070 | } |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2071 | n = clang_codeCompleteGetNumDiagnostics(results); |
| 2072 | for (i = 0; i != n; ++i) { |
| 2073 | CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); |
| 2074 | PrintDiagnostic(diag); |
| 2075 | clang_disposeDiagnostic(diag); |
| 2076 | } |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 2077 | |
| 2078 | contexts = clang_codeCompleteGetContexts(results); |
| 2079 | print_completion_contexts(contexts, stdout); |
| 2080 | |
Douglas Gregor | ea77740 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 2081 | containerKind = clang_codeCompleteGetContainerKind(results, |
| 2082 | &containerIsIncomplete); |
Douglas Gregor | 63745d5 | 2011-07-21 01:05:26 +0000 | [diff] [blame] | 2083 | |
| 2084 | if (containerKind != CXCursor_InvalidCode) { |
| 2085 | /* We have found a container */ |
| 2086 | CXString containerUSR, containerKindSpelling; |
| 2087 | containerKindSpelling = clang_getCursorKindSpelling(containerKind); |
| 2088 | printf("Container Kind: %s\n", clang_getCString(containerKindSpelling)); |
| 2089 | clang_disposeString(containerKindSpelling); |
| 2090 | |
| 2091 | if (containerIsIncomplete) { |
| 2092 | printf("Container is incomplete\n"); |
| 2093 | } |
| 2094 | else { |
| 2095 | printf("Container is complete\n"); |
| 2096 | } |
| 2097 | |
| 2098 | containerUSR = clang_codeCompleteGetContainerUSR(results); |
| 2099 | printf("Container USR: %s\n", clang_getCString(containerUSR)); |
| 2100 | clang_disposeString(containerUSR); |
| 2101 | } |
| 2102 | |
Douglas Gregor | ea77740 | 2011-07-26 15:24:30 +0000 | [diff] [blame] | 2103 | objCSelector = clang_codeCompleteGetObjCSelector(results); |
| 2104 | selectorString = clang_getCString(objCSelector); |
| 2105 | if (selectorString && strlen(selectorString) > 0) { |
| 2106 | printf("Objective-C selector: %s\n", selectorString); |
| 2107 | } |
| 2108 | clang_disposeString(objCSelector); |
| 2109 | |
Douglas Gregor | f72b6ac | 2009-12-18 16:20:58 +0000 | [diff] [blame] | 2110 | clang_disposeCodeCompleteResults(results); |
| 2111 | } |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2112 | clang_disposeTranslationUnit(TU); |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2113 | clang_disposeIndex(CIdx); |
| 2114 | free(filename); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 9485bf9 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 2116 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 2117 | |
Ted Kremenek | ef3339b | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 2118 | return 0; |
Douglas Gregor | 9eb7701 | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2121 | typedef struct { |
| 2122 | char *filename; |
| 2123 | unsigned line; |
| 2124 | unsigned column; |
| 2125 | } CursorSourceLocation; |
| 2126 | |
Argyrios Kyrtzidis | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2127 | static int inspect_cursor_at(int argc, const char **argv) { |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2128 | CXIndex CIdx; |
| 2129 | int errorCode; |
| 2130 | struct CXUnsavedFile *unsaved_files = 0; |
| 2131 | int num_unsaved_files = 0; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2132 | enum CXErrorCode Err; |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2133 | CXTranslationUnit TU; |
| 2134 | CXCursor Cursor; |
| 2135 | CursorSourceLocation *Locations = 0; |
| 2136 | unsigned NumLocations = 0, Loc; |
Douglas Gregor | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2137 | unsigned Repeats = 1; |
Douglas Gregor | b42f34b | 2010-11-30 06:04:54 +0000 | [diff] [blame] | 2138 | unsigned I; |
Douglas Gregor | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2139 | |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2140 | /* Count the number of locations. */ |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2141 | while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) |
| 2142 | ++NumLocations; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2143 | |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2144 | /* Parse the locations. */ |
| 2145 | assert(NumLocations > 0 && "Unable to count locations?"); |
| 2146 | Locations = (CursorSourceLocation *)malloc( |
| 2147 | NumLocations * sizeof(CursorSourceLocation)); |
| 2148 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 2149 | const char *input = argv[Loc + 1] + strlen("-cursor-at="); |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2150 | if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| 2151 | &Locations[Loc].line, |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 2152 | &Locations[Loc].column, 0, 0))) |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2153 | return errorCode; |
| 2154 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2155 | |
| 2156 | if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2157 | &num_unsaved_files)) |
| 2158 | return -1; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2160 | if (getenv("CINDEXTEST_EDITING")) |
| 2161 | Repeats = 5; |
| 2162 | |
| 2163 | /* Parse the translation unit. When we're testing clang_getCursor() after |
| 2164 | reparsing, don't remap unsaved files until the second parse. */ |
| 2165 | CIdx = clang_createIndex(1, 1); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2166 | Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1], |
| 2167 | argv + num_unsaved_files + 1 + NumLocations, |
| 2168 | argc - num_unsaved_files - 2 - NumLocations, |
| 2169 | unsaved_files, |
| 2170 | Repeats > 1? 0 : num_unsaved_files, |
| 2171 | getDefaultParsingOptions(), &TU); |
| 2172 | if (Err != CXError_Success) { |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2173 | fprintf(stderr, "unable to parse input\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2174 | describeLibclangFailure(Err); |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2175 | return -1; |
| 2176 | } |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2177 | |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2178 | if (checkForErrors(TU) != 0) |
| 2179 | return -1; |
| 2180 | |
Douglas Gregor | b42f34b | 2010-11-30 06:04:54 +0000 | [diff] [blame] | 2181 | for (I = 0; I != Repeats; ++I) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2182 | if (Repeats > 1) { |
| 2183 | Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 2184 | clang_defaultReparseOptions(TU)); |
| 2185 | if (Err != CXError_Success) { |
| 2186 | describeLibclangFailure(Err); |
| 2187 | clang_disposeTranslationUnit(TU); |
| 2188 | return 1; |
| 2189 | } |
Douglas Gregor | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2190 | } |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2191 | |
| 2192 | if (checkForErrors(TU) != 0) |
| 2193 | return -1; |
Douglas Gregor | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2194 | |
| 2195 | for (Loc = 0; Loc < NumLocations; ++Loc) { |
| 2196 | CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| 2197 | if (!file) |
| 2198 | continue; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2200 | Cursor = clang_getCursor(TU, |
| 2201 | clang_getLocation(TU, file, Locations[Loc].line, |
| 2202 | Locations[Loc].column)); |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2203 | |
| 2204 | if (checkForErrors(TU) != 0) |
| 2205 | return -1; |
| 2206 | |
Douglas Gregor | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2207 | if (I + 1 == Repeats) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2208 | CXCompletionString completionString = clang_getCursorCompletionString( |
| 2209 | Cursor); |
| 2210 | CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); |
Argyrios Kyrtzidis | 7aa274f | 2012-03-30 00:19:05 +0000 | [diff] [blame] | 2211 | CXString Spelling; |
| 2212 | const char *cspell; |
| 2213 | unsigned line, column; |
| 2214 | clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); |
| 2215 | printf("%d:%d ", line, column); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2216 | PrintCursor(Cursor, NULL); |
Argyrios Kyrtzidis | 7aa274f | 2012-03-30 00:19:05 +0000 | [diff] [blame] | 2217 | PrintCursorExtent(Cursor); |
| 2218 | Spelling = clang_getCursorSpelling(Cursor); |
| 2219 | cspell = clang_getCString(Spelling); |
Argyrios Kyrtzidis | 191a6a8 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 2220 | if (cspell && strlen(cspell) != 0) { |
| 2221 | unsigned pieceIndex; |
Argyrios Kyrtzidis | 191a6a8 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 2222 | printf(" Spelling=%s (", cspell); |
| 2223 | for (pieceIndex = 0; ; ++pieceIndex) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2224 | CXSourceRange range = |
| 2225 | clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); |
Argyrios Kyrtzidis | 191a6a8 | 2012-03-30 20:58:35 +0000 | [diff] [blame] | 2226 | if (clang_Range_isNull(range)) |
| 2227 | break; |
| 2228 | PrintRange(range, 0); |
| 2229 | } |
| 2230 | printf(")"); |
| 2231 | } |
Argyrios Kyrtzidis | 7aa274f | 2012-03-30 00:19:05 +0000 | [diff] [blame] | 2232 | clang_disposeString(Spelling); |
Argyrios Kyrtzidis | 210f29f | 2012-03-30 22:15:48 +0000 | [diff] [blame] | 2233 | if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1) |
| 2234 | printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor)); |
Argyrios Kyrtzidis | b6df6821 | 2012-07-02 23:54:36 +0000 | [diff] [blame] | 2235 | if (clang_Cursor_isDynamicCall(Cursor)) |
| 2236 | printf(" Dynamic-call"); |
Argyrios Kyrtzidis | b26a24c | 2012-11-01 02:01:34 +0000 | [diff] [blame] | 2237 | if (Cursor.kind == CXCursor_ObjCMessageExpr) { |
Enea Zaffanella | 476f38a | 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 | b26a24c | 2012-11-01 02:01:34 +0000 | [diff] [blame] | 2240 | printf(" Receiver-type=%s", clang_getCString(S)); |
| 2241 | clang_disposeString(S); |
| 2242 | } |
Argyrios Kyrtzidis | b6df6821 | 2012-07-02 23:54:36 +0000 | [diff] [blame] | 2243 | |
Argyrios Kyrtzidis | 2b9b5bb | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2244 | { |
| 2245 | CXModule mod = clang_Cursor_getModule(Cursor); |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 2246 | CXFile astFile; |
| 2247 | CXString name, astFilename; |
Argyrios Kyrtzidis | 2b9b5bb | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2248 | unsigned i, numHeaders; |
| 2249 | if (mod) { |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 2250 | astFile = clang_Module_getASTFile(mod); |
| 2251 | astFilename = clang_getFileName(astFile); |
Argyrios Kyrtzidis | 2b9b5bb | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2252 | name = clang_Module_getFullName(mod); |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 2253 | numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod); |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 2254 | printf(" ModuleName=%s (%s) Headers(%d):", |
| 2255 | clang_getCString(name), clang_getCString(astFilename), |
| 2256 | numHeaders); |
Argyrios Kyrtzidis | 2b9b5bb | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2257 | clang_disposeString(name); |
Argyrios Kyrtzidis | 12fdb9e | 2013-04-26 22:47:49 +0000 | [diff] [blame] | 2258 | clang_disposeString(astFilename); |
Argyrios Kyrtzidis | 2b9b5bb | 2012-10-05 00:22:37 +0000 | [diff] [blame] | 2259 | for (i = 0; i < numHeaders; ++i) { |
Enea Zaffanella | 476f38a | 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 | 2b9b5bb | 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 | 3f35bb2 | 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 | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2272 | printf("\n"); |
| 2273 | free(Locations[Loc].filename); |
| 2274 | } |
| 2275 | } |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 2276 | } |
Douglas Gregor | 2f6358b | 2010-11-30 05:52:55 +0000 | [diff] [blame] | 2277 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 2278 | PrintDiagnostics(TU); |
Douglas Gregor | 082c3e6 | 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 | cddafd3 | 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 | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2291 | PrintCursor(cursor, NULL); |
Argyrios Kyrtzidis | cddafd3 | 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; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2302 | enum CXErrorCode Err; |
Argyrios Kyrtzidis | cddafd3 | 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); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [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 | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2343 | fprintf(stderr, "unable to parse input\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2344 | describeLibclangFailure(Err); |
| 2345 | clang_disposeTranslationUnit(TU); |
Argyrios Kyrtzidis | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2346 | return -1; |
| 2347 | } |
| 2348 | |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2349 | if (checkForErrors(TU) != 0) |
| 2350 | return -1; |
| 2351 | |
Argyrios Kyrtzidis | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2352 | for (I = 0; I != Repeats; ++I) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [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 | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2361 | } |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2362 | |
| 2363 | if (checkForErrors(TU) != 0) |
| 2364 | return -1; |
Argyrios Kyrtzidis | cddafd3 | 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 | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2374 | |
| 2375 | if (checkForErrors(TU) != 0) |
| 2376 | return -1; |
| 2377 | |
Argyrios Kyrtzidis | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2378 | if (I + 1 == Repeats) { |
Erik Verbruggen | 338b55c | 2011-10-06 11:38:08 +0000 | [diff] [blame] | 2379 | CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit }; |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2380 | PrintCursor(Cursor, NULL); |
Argyrios Kyrtzidis | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2381 | printf("\n"); |
Argyrios Kyrtzidis | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 2382 | clang_findReferencesInFile(Cursor, file, visitor); |
| 2383 | free(Locations[Loc].filename); |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2384 | |
| 2385 | if (checkForErrors(TU) != 0) |
| 2386 | return -1; |
Argyrios Kyrtzidis | cddafd3 | 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 | 503c83a | 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; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2411 | enum CXErrorCode Err; |
Argyrios Kyrtzidis | 503c83a | 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); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [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 | 503c83a | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2447 | |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2448 | if (Err != CXError_Success) { |
Argyrios Kyrtzidis | 503c83a | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 2449 | fprintf(stderr, "unable to parse input\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 2450 | describeLibclangFailure(Err); |
| 2451 | clang_disposeTranslationUnit(TU); |
Argyrios Kyrtzidis | 503c83a | 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) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [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 | 503c83a | 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 | 1b5b1ce | 2013-03-11 16:03:17 +0000 | [diff] [blame] | 2493 | free((void *)Filenames); |
Argyrios Kyrtzidis | 503c83a | 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 | e26c557 | 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 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2534 | typedef struct { |
| 2535 | const char *check_prefix; |
| 2536 | int first_check_printed; |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2537 | int fail_for_error; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 2538 | int abort; |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2539 | const char *main_filename; |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 2540 | ImportedASTFilesData *importedASTs; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2541 | } IndexData; |
| 2542 | |
| 2543 | static void printCheck(IndexData *data) { |
| 2544 | if (data->check_prefix) { |
| 2545 | if (data->first_check_printed) { |
| 2546 | printf("// %s-NEXT: ", data->check_prefix); |
| 2547 | } else { |
| 2548 | printf("// %s : ", data->check_prefix); |
| 2549 | data->first_check_printed = 1; |
| 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2554 | static void printCXIndexFile(CXIdxClientFile file) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2555 | CXString filename = clang_getFileName((CXFile)file); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2556 | printf("%s", clang_getCString(filename)); |
| 2557 | clang_disposeString(filename); |
| 2558 | } |
| 2559 | |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2560 | static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) { |
| 2561 | IndexData *index_data; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2562 | CXString filename; |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2563 | const char *cname; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2564 | CXIdxClientFile file; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2565 | unsigned line, column; |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2566 | int isMainFile; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2567 | |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2568 | index_data = (IndexData *)client_data; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2569 | clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); |
| 2570 | if (line == 0) { |
Argyrios Kyrtzidis | 9f57186 | 2012-10-11 19:00:44 +0000 | [diff] [blame] | 2571 | printf("<invalid>"); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2572 | return; |
| 2573 | } |
Argyrios Kyrtzidis | ccdf827 | 2011-12-13 18:47:35 +0000 | [diff] [blame] | 2574 | if (!file) { |
| 2575 | printf("<no idxfile>"); |
| 2576 | return; |
| 2577 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2578 | filename = clang_getFileName((CXFile)file); |
| 2579 | cname = clang_getCString(filename); |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2580 | if (strcmp(cname, index_data->main_filename) == 0) |
| 2581 | isMainFile = 1; |
| 2582 | else |
| 2583 | isMainFile = 0; |
| 2584 | clang_disposeString(filename); |
| 2585 | |
| 2586 | if (!isMainFile) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2587 | printCXIndexFile(file); |
| 2588 | printf(":"); |
| 2589 | } |
| 2590 | printf("%d:%d", line, column); |
| 2591 | } |
| 2592 | |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2593 | static unsigned digitCount(unsigned val) { |
| 2594 | unsigned c = 1; |
| 2595 | while (1) { |
| 2596 | if (val < 10) |
| 2597 | return c; |
| 2598 | ++c; |
| 2599 | val /= 10; |
| 2600 | } |
| 2601 | } |
| 2602 | |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2603 | static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info, |
| 2604 | CXIdxLoc loc) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2605 | const char *name; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2606 | char *newStr; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2607 | CXIdxClientFile file; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2608 | unsigned line, column; |
| 2609 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2610 | name = info->name; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2611 | if (!name) |
| 2612 | name = "<anon-tag>"; |
| 2613 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2614 | clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); |
Argyrios Kyrtzidis | 9006807 | 2011-10-20 17:21:46 +0000 | [diff] [blame] | 2615 | /* FIXME: free these.*/ |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2616 | newStr = (char *)malloc(strlen(name) + |
| 2617 | digitCount(line) + digitCount(column) + 3); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2618 | sprintf(newStr, "%s:%d:%d", name, line, column); |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2619 | return (CXIdxClientContainer)newStr; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2622 | static void printCXIndexContainer(const CXIdxContainerInfo *info) { |
| 2623 | CXIdxClientContainer container; |
| 2624 | container = clang_index_getClientContainer(info); |
Argyrios Kyrtzidis | df15c20 | 2011-11-16 02:35:05 +0000 | [diff] [blame] | 2625 | if (!container) |
| 2626 | printf("[<<NULL>>]"); |
| 2627 | else |
| 2628 | printf("[%s]", (const char *)container); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2631 | static const char *getEntityKindString(CXIdxEntityKind kind) { |
| 2632 | switch (kind) { |
| 2633 | case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>"; |
| 2634 | case CXIdxEntity_Typedef: return "typedef"; |
| 2635 | case CXIdxEntity_Function: return "function"; |
| 2636 | case CXIdxEntity_Variable: return "variable"; |
| 2637 | case CXIdxEntity_Field: return "field"; |
| 2638 | case CXIdxEntity_EnumConstant: return "enumerator"; |
| 2639 | case CXIdxEntity_ObjCClass: return "objc-class"; |
| 2640 | case CXIdxEntity_ObjCProtocol: return "objc-protocol"; |
| 2641 | case CXIdxEntity_ObjCCategory: return "objc-category"; |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2642 | case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method"; |
| 2643 | case CXIdxEntity_ObjCClassMethod: return "objc-class-method"; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2644 | case CXIdxEntity_ObjCProperty: return "objc-property"; |
| 2645 | case CXIdxEntity_ObjCIvar: return "objc-ivar"; |
| 2646 | case CXIdxEntity_Enum: return "enum"; |
| 2647 | case CXIdxEntity_Struct: return "struct"; |
| 2648 | case CXIdxEntity_Union: return "union"; |
| 2649 | case CXIdxEntity_CXXClass: return "c++-class"; |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2650 | case CXIdxEntity_CXXNamespace: return "namespace"; |
| 2651 | case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias"; |
| 2652 | case CXIdxEntity_CXXStaticVariable: return "c++-static-var"; |
| 2653 | case CXIdxEntity_CXXStaticMethod: return "c++-static-method"; |
| 2654 | case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method"; |
| 2655 | case CXIdxEntity_CXXConstructor: return "constructor"; |
| 2656 | case CXIdxEntity_CXXDestructor: return "destructor"; |
| 2657 | case CXIdxEntity_CXXConversionFunction: return "conversion-func"; |
| 2658 | case CXIdxEntity_CXXTypeAlias: return "type-alias"; |
David Blaikie | dcefd95 | 2012-08-31 21:55:26 +0000 | [diff] [blame] | 2659 | case CXIdxEntity_CXXInterface: return "c++-__interface"; |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2660 | } |
| 2661 | assert(0 && "Garbage entity kind"); |
| 2662 | return 0; |
| 2663 | } |
| 2664 | |
| 2665 | static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) { |
| 2666 | switch (kind) { |
| 2667 | case CXIdxEntity_NonTemplate: return ""; |
| 2668 | case CXIdxEntity_Template: return "-template"; |
| 2669 | case CXIdxEntity_TemplatePartialSpecialization: |
| 2670 | return "-template-partial-spec"; |
| 2671 | case CXIdxEntity_TemplateSpecialization: return "-template-spec"; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2672 | } |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2673 | assert(0 && "Garbage entity kind"); |
| 2674 | return 0; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
Argyrios Kyrtzidis | 5200288 | 2011-12-07 20:44:12 +0000 | [diff] [blame] | 2677 | static const char *getEntityLanguageString(CXIdxEntityLanguage kind) { |
| 2678 | switch (kind) { |
| 2679 | case CXIdxEntityLang_None: return "<none>"; |
| 2680 | case CXIdxEntityLang_C: return "C"; |
| 2681 | case CXIdxEntityLang_ObjC: return "ObjC"; |
| 2682 | case CXIdxEntityLang_CXX: return "C++"; |
| 2683 | } |
| 2684 | assert(0 && "Garbage language kind"); |
| 2685 | return 0; |
| 2686 | } |
| 2687 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2688 | static void printEntityInfo(const char *cb, |
| 2689 | CXClientData client_data, |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2690 | const CXIdxEntityInfo *info) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2691 | const char *name; |
| 2692 | IndexData *index_data; |
Argyrios Kyrtzidis | 4d873b7 | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 2693 | unsigned i; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2694 | index_data = (IndexData *)client_data; |
| 2695 | printCheck(index_data); |
| 2696 | |
Argyrios Kyrtzidis | e4acd23 | 2011-11-16 02:34:59 +0000 | [diff] [blame] | 2697 | if (!info) { |
| 2698 | printf("%s: <<NULL>>", cb); |
| 2699 | return; |
| 2700 | } |
| 2701 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2702 | name = info->name; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2703 | if (!name) |
| 2704 | name = "<anon-tag>"; |
| 2705 | |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2706 | printf("%s: kind: %s%s", cb, getEntityKindString(info->kind), |
| 2707 | getEntityTemplateKindString(info->templateKind)); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2708 | printf(" | name: %s", name); |
| 2709 | printf(" | USR: %s", info->USR); |
Argyrios Kyrtzidis | ccdf827 | 2011-12-13 18:47:35 +0000 | [diff] [blame] | 2710 | printf(" | lang: %s", getEntityLanguageString(info->lang)); |
Argyrios Kyrtzidis | 4d873b7 | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 2711 | |
| 2712 | for (i = 0; i != info->numAttributes; ++i) { |
| 2713 | const CXIdxAttrInfo *Attr = info->attributes[i]; |
| 2714 | printf(" <attribute>: "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2715 | PrintCursor(Attr->cursor, NULL); |
Argyrios Kyrtzidis | 4d873b7 | 2011-12-15 00:05:00 +0000 | [diff] [blame] | 2716 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Argyrios Kyrtzidis | b3c16ba | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2719 | static void printBaseClassInfo(CXClientData client_data, |
| 2720 | const CXIdxBaseClassInfo *info) { |
| 2721 | printEntityInfo(" <base>", client_data, info->base); |
| 2722 | printf(" | cursor: "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2723 | PrintCursor(info->cursor, NULL); |
Argyrios Kyrtzidis | b3c16ba | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2724 | printf(" | loc: "); |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2725 | printCXIndexLoc(info->loc, client_data); |
Argyrios Kyrtzidis | b3c16ba | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2728 | static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo, |
| 2729 | CXClientData client_data) { |
| 2730 | unsigned i; |
| 2731 | for (i = 0; i < ProtoInfo->numProtocols; ++i) { |
| 2732 | printEntityInfo(" <protocol>", client_data, |
| 2733 | ProtoInfo->protocols[i]->protocol); |
| 2734 | printf(" | cursor: "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2735 | PrintCursor(ProtoInfo->protocols[i]->cursor, NULL); |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2736 | printf(" | loc: "); |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2737 | printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data); |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2738 | printf("\n"); |
| 2739 | } |
| 2740 | } |
| 2741 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2742 | static void index_diagnostic(CXClientData client_data, |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 2743 | CXDiagnosticSet diagSet, void *reserved) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2744 | CXString str; |
| 2745 | const char *cstr; |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 2746 | unsigned numDiags, i; |
| 2747 | CXDiagnostic diag; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2748 | IndexData *index_data; |
| 2749 | index_data = (IndexData *)client_data; |
| 2750 | printCheck(index_data); |
| 2751 | |
Argyrios Kyrtzidis | f2d99b0 | 2011-12-01 02:42:50 +0000 | [diff] [blame] | 2752 | numDiags = clang_getNumDiagnosticsInSet(diagSet); |
| 2753 | for (i = 0; i != numDiags; ++i) { |
| 2754 | diag = clang_getDiagnosticInSet(diagSet, i); |
| 2755 | str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); |
| 2756 | cstr = clang_getCString(str); |
| 2757 | printf("[diagnostic]: %s\n", cstr); |
| 2758 | clang_disposeString(str); |
| 2759 | |
| 2760 | if (getenv("CINDEXTEST_FAILONERROR") && |
| 2761 | clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) { |
| 2762 | index_data->fail_for_error = 1; |
| 2763 | } |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 2764 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2767 | static CXIdxClientFile index_enteredMainFile(CXClientData client_data, |
| 2768 | CXFile file, void *reserved) { |
| 2769 | IndexData *index_data; |
Argyrios Kyrtzidis | a15f816 | 2012-03-15 18:48:52 +0000 | [diff] [blame] | 2770 | CXString filename; |
| 2771 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2772 | index_data = (IndexData *)client_data; |
| 2773 | printCheck(index_data); |
| 2774 | |
Argyrios Kyrtzidis | a15f816 | 2012-03-15 18:48:52 +0000 | [diff] [blame] | 2775 | filename = clang_getFileName(file); |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2776 | index_data->main_filename = clang_getCString(filename); |
| 2777 | clang_disposeString(filename); |
| 2778 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2779 | printf("[enteredMainFile]: "); |
| 2780 | printCXIndexFile((CXIdxClientFile)file); |
| 2781 | printf("\n"); |
| 2782 | |
| 2783 | return (CXIdxClientFile)file; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2786 | static CXIdxClientFile index_ppIncludedFile(CXClientData client_data, |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2787 | const CXIdxIncludedFileInfo *info) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2788 | IndexData *index_data; |
| 2789 | index_data = (IndexData *)client_data; |
| 2790 | printCheck(index_data); |
| 2791 | |
Argyrios Kyrtzidis | 8c25804 | 2011-11-05 04:03:35 +0000 | [diff] [blame] | 2792 | printf("[ppIncludedFile]: "); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2793 | printCXIndexFile((CXIdxClientFile)info->file); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2794 | printf(" | name: \"%s\"", info->filename); |
| 2795 | printf(" | hash loc: "); |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2796 | printCXIndexLoc(info->hashLoc, client_data); |
Argyrios Kyrtzidis | 5e2ec48 | 2012-10-18 00:17:05 +0000 | [diff] [blame] | 2797 | printf(" | isImport: %d | isAngled: %d | isModule: %d\n", |
| 2798 | info->isImport, info->isAngled, info->isModuleImport); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2799 | |
| 2800 | return (CXIdxClientFile)info->file; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Argyrios Kyrtzidis | 472eda0 | 2012-10-02 16:10:38 +0000 | [diff] [blame] | 2803 | static CXIdxClientFile index_importedASTFile(CXClientData client_data, |
| 2804 | const CXIdxImportedASTFileInfo *info) { |
| 2805 | IndexData *index_data; |
| 2806 | index_data = (IndexData *)client_data; |
| 2807 | printCheck(index_data); |
| 2808 | |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 2809 | if (index_data->importedASTs) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2810 | CXString filename = clang_getFileName(info->file); |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 2811 | importedASTS_insert(index_data->importedASTs, clang_getCString(filename)); |
| 2812 | clang_disposeString(filename); |
| 2813 | } |
| 2814 | |
Argyrios Kyrtzidis | 472eda0 | 2012-10-02 16:10:38 +0000 | [diff] [blame] | 2815 | printf("[importedASTFile]: "); |
| 2816 | printCXIndexFile((CXIdxClientFile)info->file); |
Argyrios Kyrtzidis | dc78f3e | 2012-10-05 00:22:40 +0000 | [diff] [blame] | 2817 | if (info->module) { |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 2818 | CXString name = clang_Module_getFullName(info->module); |
Argyrios Kyrtzidis | dc78f3e | 2012-10-05 00:22:40 +0000 | [diff] [blame] | 2819 | printf(" | loc: "); |
| 2820 | printCXIndexLoc(info->loc, client_data); |
| 2821 | printf(" | name: \"%s\"", clang_getCString(name)); |
| 2822 | printf(" | isImplicit: %d\n", info->isImplicit); |
| 2823 | clang_disposeString(name); |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2824 | } else { |
NAKAMURA Takumi | e259d91 | 2012-10-12 14:25:52 +0000 | [diff] [blame] | 2825 | /* PCH file, the rest are not relevant. */ |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2826 | printf("\n"); |
Argyrios Kyrtzidis | dc78f3e | 2012-10-05 00:22:40 +0000 | [diff] [blame] | 2827 | } |
Argyrios Kyrtzidis | 472eda0 | 2012-10-02 16:10:38 +0000 | [diff] [blame] | 2828 | |
| 2829 | return (CXIdxClientFile)info->file; |
| 2830 | } |
| 2831 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2832 | static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2833 | void *reserved) { |
| 2834 | IndexData *index_data; |
| 2835 | index_data = (IndexData *)client_data; |
| 2836 | printCheck(index_data); |
| 2837 | |
Argyrios Kyrtzidis | 8c25804 | 2011-11-05 04:03:35 +0000 | [diff] [blame] | 2838 | printf("[startedTranslationUnit]\n"); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2839 | return (CXIdxClientContainer)"TU"; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2842 | static void index_indexDeclaration(CXClientData client_data, |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2843 | const CXIdxDeclInfo *info) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2844 | IndexData *index_data; |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2845 | const CXIdxObjCCategoryDeclInfo *CatInfo; |
| 2846 | const CXIdxObjCInterfaceDeclInfo *InterInfo; |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2847 | const CXIdxObjCProtocolRefListInfo *ProtoInfo; |
Argyrios Kyrtzidis | 93db292 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 2848 | const CXIdxObjCPropertyDeclInfo *PropInfo; |
Argyrios Kyrtzidis | b3c16ba | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2849 | const CXIdxCXXClassDeclInfo *CXXClassInfo; |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2850 | unsigned i; |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2851 | index_data = (IndexData *)client_data; |
| 2852 | |
| 2853 | printEntityInfo("[indexDeclaration]", client_data, info->entityInfo); |
| 2854 | printf(" | cursor: "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2855 | PrintCursor(info->cursor, NULL); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2856 | printf(" | loc: "); |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2857 | printCXIndexLoc(info->loc, client_data); |
Argyrios Kyrtzidis | 663c8ec | 2011-12-07 20:44:19 +0000 | [diff] [blame] | 2858 | printf(" | semantic-container: "); |
| 2859 | printCXIndexContainer(info->semanticContainer); |
| 2860 | printf(" | lexical-container: "); |
| 2861 | printCXIndexContainer(info->lexicalContainer); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2862 | printf(" | isRedecl: %d", info->isRedeclaration); |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2863 | printf(" | isDef: %d", info->isDefinition); |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 2864 | if (info->flags & CXIdxDeclFlag_Skipped) { |
| 2865 | assert(!info->isContainer); |
| 2866 | printf(" | isContainer: skipped"); |
| 2867 | } else { |
| 2868 | printf(" | isContainer: %d", info->isContainer); |
| 2869 | } |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2870 | printf(" | isImplicit: %d\n", info->isImplicit); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2871 | |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2872 | for (i = 0; i != info->numAttributes; ++i) { |
NAKAMURA Takumi | 2a4859a | 2011-11-18 00:51:03 +0000 | [diff] [blame] | 2873 | const CXIdxAttrInfo *Attr = info->attributes[i]; |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2874 | printf(" <attribute>: "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2875 | PrintCursor(Attr->cursor, NULL); |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2876 | printf("\n"); |
| 2877 | } |
| 2878 | |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2879 | if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) { |
| 2880 | const char *kindName = 0; |
| 2881 | CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind; |
| 2882 | switch (K) { |
| 2883 | case CXIdxObjCContainer_ForwardRef: |
| 2884 | kindName = "forward-ref"; break; |
| 2885 | case CXIdxObjCContainer_Interface: |
| 2886 | kindName = "interface"; break; |
| 2887 | case CXIdxObjCContainer_Implementation: |
| 2888 | kindName = "implementation"; break; |
| 2889 | } |
| 2890 | printCheck(index_data); |
| 2891 | printf(" <ObjCContainerInfo>: kind: %s\n", kindName); |
| 2892 | } |
| 2893 | |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2894 | if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2895 | printEntityInfo(" <ObjCCategoryInfo>: class", client_data, |
| 2896 | CatInfo->objcClass); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 2897 | printf(" | cursor: "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2898 | PrintCursor(CatInfo->classCursor, NULL); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 2899 | printf(" | loc: "); |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2900 | printCXIndexLoc(CatInfo->classLoc, client_data); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2901 | printf("\n"); |
| 2902 | } |
| 2903 | |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2904 | if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) { |
| 2905 | if (InterInfo->superInfo) { |
Argyrios Kyrtzidis | b3c16ba | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2906 | printBaseClassInfo(client_data, InterInfo->superInfo); |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2907 | printf("\n"); |
| 2908 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2911 | if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) { |
| 2912 | printProtocolList(ProtoInfo, client_data); |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2913 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2914 | |
Argyrios Kyrtzidis | 93db292 | 2012-02-28 17:50:33 +0000 | [diff] [blame] | 2915 | if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) { |
| 2916 | if (PropInfo->getter) { |
| 2917 | printEntityInfo(" <getter>", client_data, PropInfo->getter); |
| 2918 | printf("\n"); |
| 2919 | } |
| 2920 | if (PropInfo->setter) { |
| 2921 | printEntityInfo(" <setter>", client_data, PropInfo->setter); |
| 2922 | printf("\n"); |
| 2923 | } |
| 2924 | } |
| 2925 | |
Argyrios Kyrtzidis | b3c16ba | 2011-12-07 20:44:15 +0000 | [diff] [blame] | 2926 | if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) { |
| 2927 | for (i = 0; i != CXXClassInfo->numBases; ++i) { |
| 2928 | printBaseClassInfo(client_data, CXXClassInfo->bases[i]); |
| 2929 | printf("\n"); |
| 2930 | } |
| 2931 | } |
| 2932 | |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 2933 | if (info->declAsContainer) |
| 2934 | clang_index_setClientContainer(info->declAsContainer, |
| 2935 | makeClientContainer(info->entityInfo, info->loc)); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
| 2938 | static void index_indexEntityReference(CXClientData client_data, |
Argyrios Kyrtzidis | 3e429e7 | 2011-11-12 02:16:30 +0000 | [diff] [blame] | 2939 | const CXIdxEntityRefInfo *info) { |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2940 | printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2941 | printf(" | cursor: "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 2942 | PrintCursor(info->cursor, NULL); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2943 | printf(" | loc: "); |
Argyrios Kyrtzidis | 0abc5eb | 2012-03-15 18:07:22 +0000 | [diff] [blame] | 2944 | printCXIndexLoc(info->loc, client_data); |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2945 | printEntityInfo(" | <parent>:", client_data, info->parentEntity); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2946 | printf(" | container: "); |
| 2947 | printCXIndexContainer(info->container); |
Argyrios Kyrtzidis | 86acd72 | 2011-11-14 22:39:19 +0000 | [diff] [blame] | 2948 | printf(" | refkind: "); |
Argyrios Kyrtzidis | 0c7735e5 | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 2949 | switch (info->kind) { |
| 2950 | case CXIdxEntityRef_Direct: printf("direct"); break; |
Argyrios Kyrtzidis | effdbf5 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 2951 | case CXIdxEntityRef_Implicit: printf("implicit"); break; |
Argyrios Kyrtzidis | 0c7735e5 | 2011-10-18 15:50:50 +0000 | [diff] [blame] | 2952 | } |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2953 | printf("\n"); |
| 2954 | } |
| 2955 | |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 2956 | static int index_abortQuery(CXClientData client_data, void *reserved) { |
| 2957 | IndexData *index_data; |
| 2958 | index_data = (IndexData *)client_data; |
| 2959 | return index_data->abort; |
| 2960 | } |
| 2961 | |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2962 | static IndexerCallbacks IndexCB = { |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 2963 | index_abortQuery, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2964 | index_diagnostic, |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2965 | index_enteredMainFile, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2966 | index_ppIncludedFile, |
Argyrios Kyrtzidis | 472eda0 | 2012-10-02 16:10:38 +0000 | [diff] [blame] | 2967 | index_importedASTFile, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2968 | index_startedTranslationUnit, |
Argyrios Kyrtzidis | 7519c5e | 2011-11-11 00:23:36 +0000 | [diff] [blame] | 2969 | index_indexDeclaration, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 2970 | index_indexEntityReference |
| 2971 | }; |
| 2972 | |
Argyrios Kyrtzidis | fb7d145 | 2012-01-14 00:11:49 +0000 | [diff] [blame] | 2973 | static unsigned getIndexOptions(void) { |
| 2974 | unsigned index_opts; |
| 2975 | index_opts = 0; |
| 2976 | if (getenv("CINDEXTEST_SUPPRESSREFS")) |
| 2977 | index_opts |= CXIndexOpt_SuppressRedundantRefs; |
| 2978 | if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS")) |
| 2979 | index_opts |= CXIndexOpt_IndexFunctionLocalSymbols; |
Argyrios Kyrtzidis | 8b71bc7 | 2012-12-06 19:41:16 +0000 | [diff] [blame] | 2980 | if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES")) |
| 2981 | index_opts |= CXIndexOpt_SkipParsedBodiesInSession; |
Argyrios Kyrtzidis | fb7d145 | 2012-01-14 00:11:49 +0000 | [diff] [blame] | 2982 | |
| 2983 | return index_opts; |
| 2984 | } |
| 2985 | |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 2986 | static int index_compile_args(int num_args, const char **args, |
| 2987 | CXIndexAction idxAction, |
| 2988 | ImportedASTFilesData *importedASTs, |
| 2989 | const char *check_prefix) { |
| 2990 | IndexData index_data; |
| 2991 | unsigned index_opts; |
| 2992 | int result; |
| 2993 | |
| 2994 | if (num_args == 0) { |
| 2995 | fprintf(stderr, "no compiler arguments\n"); |
| 2996 | return -1; |
| 2997 | } |
| 2998 | |
| 2999 | index_data.check_prefix = check_prefix; |
| 3000 | index_data.first_check_printed = 0; |
| 3001 | index_data.fail_for_error = 0; |
| 3002 | index_data.abort = 0; |
| 3003 | index_data.main_filename = ""; |
| 3004 | index_data.importedASTs = importedASTs; |
| 3005 | |
| 3006 | index_opts = getIndexOptions(); |
| 3007 | result = clang_indexSourceFile(idxAction, &index_data, |
| 3008 | &IndexCB,sizeof(IndexCB), index_opts, |
| 3009 | 0, args, num_args, 0, 0, 0, |
| 3010 | getDefaultParsingOptions()); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3011 | if (result != CXError_Success) |
| 3012 | describeLibclangFailure(result); |
| 3013 | |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3014 | if (index_data.fail_for_error) |
| 3015 | result = -1; |
| 3016 | |
| 3017 | return result; |
| 3018 | } |
| 3019 | |
| 3020 | static int index_ast_file(const char *ast_file, |
| 3021 | CXIndex Idx, |
| 3022 | CXIndexAction idxAction, |
| 3023 | ImportedASTFilesData *importedASTs, |
| 3024 | const char *check_prefix) { |
| 3025 | CXTranslationUnit TU; |
| 3026 | IndexData index_data; |
| 3027 | unsigned index_opts; |
| 3028 | int result; |
| 3029 | |
| 3030 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 3031 | return -1; |
| 3032 | |
| 3033 | index_data.check_prefix = check_prefix; |
| 3034 | index_data.first_check_printed = 0; |
| 3035 | index_data.fail_for_error = 0; |
| 3036 | index_data.abort = 0; |
| 3037 | index_data.main_filename = ""; |
| 3038 | index_data.importedASTs = importedASTs; |
| 3039 | |
| 3040 | index_opts = getIndexOptions(); |
| 3041 | result = clang_indexTranslationUnit(idxAction, &index_data, |
| 3042 | &IndexCB,sizeof(IndexCB), |
| 3043 | index_opts, TU); |
| 3044 | if (index_data.fail_for_error) |
| 3045 | result = -1; |
| 3046 | |
| 3047 | clang_disposeTranslationUnit(TU); |
| 3048 | return result; |
| 3049 | } |
| 3050 | |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3051 | static int index_file(int argc, const char **argv, int full) { |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3052 | const char *check_prefix; |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3053 | CXIndex Idx; |
| 3054 | CXIndexAction idxAction; |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3055 | ImportedASTFilesData *importedASTs; |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3056 | int result; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3057 | |
| 3058 | check_prefix = 0; |
| 3059 | if (argc > 0) { |
| 3060 | if (strstr(argv[0], "-check-prefix=") == argv[0]) { |
| 3061 | check_prefix = argv[0] + strlen("-check-prefix="); |
| 3062 | ++argv; |
| 3063 | --argc; |
| 3064 | } |
| 3065 | } |
| 3066 | |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3067 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 3068 | /* displayDiagnostics=*/1))) { |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3069 | fprintf(stderr, "Could not create Index\n"); |
| 3070 | return 1; |
| 3071 | } |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3072 | idxAction = clang_IndexAction_create(Idx); |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3073 | importedASTs = 0; |
| 3074 | if (full) |
| 3075 | importedASTs = importedASTs_create(); |
| 3076 | |
| 3077 | result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix); |
| 3078 | if (result != 0) |
| 3079 | goto finished; |
| 3080 | |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3081 | if (full) { |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3082 | unsigned i; |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3083 | for (i = 0; i < importedASTs->num_files && result == 0; ++i) { |
| 3084 | result = index_ast_file(importedASTs->filenames[i], Idx, idxAction, |
| 3085 | importedASTs, check_prefix); |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3086 | } |
| 3087 | } |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3088 | |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3089 | finished: |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3090 | importedASTs_dispose(importedASTs); |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3091 | clang_IndexAction_dispose(idxAction); |
| 3092 | clang_disposeIndex(Idx); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3093 | return result; |
| 3094 | } |
| 3095 | |
| 3096 | static int index_tu(int argc, const char **argv) { |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3097 | const char *check_prefix; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3098 | CXIndex Idx; |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3099 | CXIndexAction idxAction; |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3100 | int result; |
| 3101 | |
| 3102 | check_prefix = 0; |
| 3103 | if (argc > 0) { |
| 3104 | if (strstr(argv[0], "-check-prefix=") == argv[0]) { |
| 3105 | check_prefix = argv[0] + strlen("-check-prefix="); |
| 3106 | ++argv; |
| 3107 | --argc; |
| 3108 | } |
| 3109 | } |
| 3110 | |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3111 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 3112 | /* displayDiagnostics=*/1))) { |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3113 | fprintf(stderr, "Could not create Index\n"); |
| 3114 | return 1; |
| 3115 | } |
| 3116 | idxAction = clang_IndexAction_create(Idx); |
| 3117 | |
| 3118 | result = index_ast_file(argv[0], Idx, idxAction, |
| 3119 | /*importedASTs=*/0, check_prefix); |
| 3120 | |
| 3121 | clang_IndexAction_dispose(idxAction); |
| 3122 | clang_disposeIndex(Idx); |
| 3123 | return result; |
| 3124 | } |
| 3125 | |
| 3126 | static int index_compile_db(int argc, const char **argv) { |
| 3127 | const char *check_prefix; |
| 3128 | CXIndex Idx; |
| 3129 | CXIndexAction idxAction; |
| 3130 | int errorCode = 0; |
| 3131 | |
| 3132 | check_prefix = 0; |
| 3133 | if (argc > 0) { |
| 3134 | if (strstr(argv[0], "-check-prefix=") == argv[0]) { |
| 3135 | check_prefix = argv[0] + strlen("-check-prefix="); |
| 3136 | ++argv; |
| 3137 | --argc; |
| 3138 | } |
| 3139 | } |
| 3140 | |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3141 | if (argc == 0) { |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3142 | fprintf(stderr, "no compilation database\n"); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3143 | return -1; |
| 3144 | } |
| 3145 | |
| 3146 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 3147 | /* displayDiagnostics=*/1))) { |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3148 | fprintf(stderr, "Could not create Index\n"); |
| 3149 | return 1; |
| 3150 | } |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3151 | idxAction = clang_IndexAction_create(Idx); |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3152 | |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3153 | { |
| 3154 | const char *database = argv[0]; |
| 3155 | CXCompilationDatabase db = 0; |
| 3156 | CXCompileCommands CCmds = 0; |
| 3157 | CXCompileCommand CCmd; |
| 3158 | CXCompilationDatabase_Error ec; |
| 3159 | CXString wd; |
| 3160 | #define MAX_COMPILE_ARGS 512 |
| 3161 | CXString cxargs[MAX_COMPILE_ARGS]; |
| 3162 | const char *args[MAX_COMPILE_ARGS]; |
| 3163 | char *tmp; |
| 3164 | unsigned len; |
| 3165 | char *buildDir; |
| 3166 | int i, a, numCmds, numArgs; |
| 3167 | |
| 3168 | len = strlen(database); |
| 3169 | tmp = (char *) malloc(len+1); |
| 3170 | memcpy(tmp, database, len+1); |
| 3171 | buildDir = dirname(tmp); |
| 3172 | |
| 3173 | db = clang_CompilationDatabase_fromDirectory(buildDir, &ec); |
| 3174 | |
| 3175 | if (db) { |
| 3176 | |
| 3177 | if (ec!=CXCompilationDatabase_NoError) { |
| 3178 | printf("unexpected error %d code while loading compilation database\n", ec); |
| 3179 | errorCode = -1; |
| 3180 | goto cdb_end; |
| 3181 | } |
| 3182 | |
Argyrios Kyrtzidis | fdea813 | 2012-12-17 20:19:56 +0000 | [diff] [blame] | 3183 | if (chdir(buildDir) != 0) { |
| 3184 | printf("Could not chdir to %s\n", buildDir); |
| 3185 | errorCode = -1; |
| 3186 | goto cdb_end; |
| 3187 | } |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3188 | |
Argyrios Kyrtzidis | fdea813 | 2012-12-17 20:19:56 +0000 | [diff] [blame] | 3189 | CCmds = clang_CompilationDatabase_getAllCompileCommands(db); |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3190 | if (!CCmds) { |
| 3191 | printf("compilation db is empty\n"); |
| 3192 | errorCode = -1; |
| 3193 | goto cdb_end; |
| 3194 | } |
| 3195 | |
| 3196 | numCmds = clang_CompileCommands_getSize(CCmds); |
| 3197 | |
| 3198 | if (numCmds==0) { |
| 3199 | fprintf(stderr, "should not get an empty compileCommand set\n"); |
| 3200 | errorCode = -1; |
| 3201 | goto cdb_end; |
| 3202 | } |
| 3203 | |
| 3204 | for (i=0; i<numCmds && errorCode == 0; ++i) { |
| 3205 | CCmd = clang_CompileCommands_getCommand(CCmds, i); |
| 3206 | |
| 3207 | wd = clang_CompileCommand_getDirectory(CCmd); |
Argyrios Kyrtzidis | fdea813 | 2012-12-17 20:19:56 +0000 | [diff] [blame] | 3208 | if (chdir(clang_getCString(wd)) != 0) { |
| 3209 | printf("Could not chdir to %s\n", clang_getCString(wd)); |
| 3210 | errorCode = -1; |
| 3211 | goto cdb_end; |
| 3212 | } |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3213 | clang_disposeString(wd); |
| 3214 | |
| 3215 | numArgs = clang_CompileCommand_getNumArgs(CCmd); |
| 3216 | if (numArgs > MAX_COMPILE_ARGS){ |
| 3217 | fprintf(stderr, "got more compile arguments than maximum\n"); |
| 3218 | errorCode = -1; |
| 3219 | goto cdb_end; |
| 3220 | } |
| 3221 | for (a=0; a<numArgs; ++a) { |
| 3222 | cxargs[a] = clang_CompileCommand_getArg(CCmd, a); |
| 3223 | args[a] = clang_getCString(cxargs[a]); |
| 3224 | } |
| 3225 | |
| 3226 | errorCode = index_compile_args(numArgs, args, idxAction, |
| 3227 | /*importedASTs=*/0, check_prefix); |
| 3228 | |
| 3229 | for (a=0; a<numArgs; ++a) |
| 3230 | clang_disposeString(cxargs[a]); |
| 3231 | } |
| 3232 | } else { |
| 3233 | printf("database loading failed with error code %d.\n", ec); |
| 3234 | errorCode = -1; |
| 3235 | } |
| 3236 | |
| 3237 | cdb_end: |
| 3238 | clang_CompileCommands_dispose(CCmds); |
| 3239 | clang_CompilationDatabase_dispose(db); |
| 3240 | free(tmp); |
| 3241 | |
| 3242 | } |
| 3243 | |
Argyrios Kyrtzidis | 4c910b1 | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 3244 | clang_IndexAction_dispose(idxAction); |
| 3245 | clang_disposeIndex(Idx); |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3246 | return errorCode; |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3249 | int perform_token_annotation(int argc, const char **argv) { |
| 3250 | const char *input = argv[1]; |
| 3251 | char *filename = 0; |
| 3252 | unsigned line, second_line; |
| 3253 | unsigned column, second_column; |
| 3254 | CXIndex CIdx; |
| 3255 | CXTranslationUnit TU = 0; |
| 3256 | int errorCode; |
| 3257 | struct CXUnsavedFile *unsaved_files = 0; |
| 3258 | int num_unsaved_files = 0; |
| 3259 | CXToken *tokens; |
| 3260 | unsigned num_tokens; |
| 3261 | CXSourceRange range; |
| 3262 | CXSourceLocation startLoc, endLoc; |
| 3263 | CXFile file = 0; |
| 3264 | CXCursor *cursors = 0; |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 3265 | CXSourceRangeList *skipped_ranges = 0; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3266 | enum CXErrorCode Err; |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3267 | unsigned i; |
| 3268 | |
| 3269 | input += strlen("-test-annotate-tokens="); |
| 3270 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
| 3271 | &second_line, &second_column))) |
| 3272 | return errorCode; |
| 3273 | |
Richard Smith | 1ea42eb | 2012-07-05 08:20:49 +0000 | [diff] [blame] | 3274 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) { |
| 3275 | free(filename); |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3276 | return -1; |
Richard Smith | 1ea42eb | 2012-07-05 08:20:49 +0000 | [diff] [blame] | 3277 | } |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3278 | |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 3279 | CIdx = clang_createIndex(0, 1); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3280 | Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1], |
| 3281 | argv + num_unsaved_files + 2, |
| 3282 | argc - num_unsaved_files - 3, |
| 3283 | unsaved_files, |
| 3284 | num_unsaved_files, |
| 3285 | getDefaultParsingOptions(), &TU); |
| 3286 | if (Err != CXError_Success) { |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3287 | fprintf(stderr, "unable to parse input\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3288 | describeLibclangFailure(Err); |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3289 | clang_disposeIndex(CIdx); |
| 3290 | free(filename); |
| 3291 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 3292 | return -1; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3293 | } |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3294 | errorCode = 0; |
| 3295 | |
Richard Smith | 1ea42eb | 2012-07-05 08:20:49 +0000 | [diff] [blame] | 3296 | if (checkForErrors(TU) != 0) { |
| 3297 | errorCode = -1; |
| 3298 | goto teardown; |
| 3299 | } |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3300 | |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3301 | if (getenv("CINDEXTEST_EDITING")) { |
| 3302 | for (i = 0; i < 5; ++i) { |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3303 | Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| 3304 | clang_defaultReparseOptions(TU)); |
| 3305 | if (Err != CXError_Success) { |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3306 | fprintf(stderr, "Unable to reparse translation unit!\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3307 | describeLibclangFailure(Err); |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 3308 | errorCode = -1; |
| 3309 | goto teardown; |
| 3310 | } |
| 3311 | } |
| 3312 | } |
| 3313 | |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3314 | if (checkForErrors(TU) != 0) { |
| 3315 | errorCode = -1; |
| 3316 | goto teardown; |
| 3317 | } |
| 3318 | |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3319 | file = clang_getFile(TU, filename); |
| 3320 | if (!file) { |
| 3321 | fprintf(stderr, "file %s is not in this translation unit\n", filename); |
| 3322 | errorCode = -1; |
| 3323 | goto teardown; |
| 3324 | } |
| 3325 | |
| 3326 | startLoc = clang_getLocation(TU, file, line, column); |
| 3327 | if (clang_equalLocations(clang_getNullLocation(), startLoc)) { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3328 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3329 | column); |
| 3330 | errorCode = -1; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3331 | goto teardown; |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | endLoc = clang_getLocation(TU, file, second_line, second_column); |
| 3335 | if (clang_equalLocations(clang_getNullLocation(), endLoc)) { |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3336 | fprintf(stderr, "invalid source location %s:%d:%d\n", filename, |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3337 | second_line, second_column); |
| 3338 | errorCode = -1; |
Ted Kremenek | 2900467 | 2010-02-17 00:41:32 +0000 | [diff] [blame] | 3339 | goto teardown; |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | range = clang_getRange(startLoc, endLoc); |
| 3343 | clang_tokenize(TU, range, &tokens, &num_tokens); |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3344 | |
| 3345 | if (checkForErrors(TU) != 0) { |
| 3346 | errorCode = -1; |
| 3347 | goto teardown; |
| 3348 | } |
| 3349 | |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3350 | cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); |
| 3351 | clang_annotateTokens(TU, tokens, num_tokens, cursors); |
Argyrios Kyrtzidis | a109e00 | 2011-10-28 22:54:36 +0000 | [diff] [blame] | 3352 | |
| 3353 | if (checkForErrors(TU) != 0) { |
| 3354 | errorCode = -1; |
| 3355 | goto teardown; |
| 3356 | } |
| 3357 | |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 3358 | skipped_ranges = clang_getSkippedRanges(TU, file); |
| 3359 | for (i = 0; i != skipped_ranges->count; ++i) { |
| 3360 | unsigned start_line, start_column, end_line, end_column; |
| 3361 | clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]), |
| 3362 | 0, &start_line, &start_column, 0); |
| 3363 | clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]), |
| 3364 | 0, &end_line, &end_column, 0); |
| 3365 | printf("Skipping: "); |
| 3366 | PrintExtent(stdout, start_line, start_column, end_line, end_column); |
| 3367 | printf("\n"); |
| 3368 | } |
Argyrios Kyrtzidis | 0e282ef | 2013-12-06 18:55:45 +0000 | [diff] [blame] | 3369 | clang_disposeSourceRangeList(skipped_ranges); |
Argyrios Kyrtzidis | 9ef5775 | 2013-12-05 08:19:32 +0000 | [diff] [blame] | 3370 | |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3371 | for (i = 0; i != num_tokens; ++i) { |
| 3372 | const char *kind = "<unknown>"; |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 3373 | CXString spelling = clang_getTokenSpelling(TU, tokens[i]); |
| 3374 | CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3375 | unsigned start_line, start_column, end_line, end_column; |
| 3376 | |
| 3377 | switch (clang_getTokenKind(tokens[i])) { |
| 3378 | case CXToken_Punctuation: kind = "Punctuation"; break; |
| 3379 | case CXToken_Keyword: kind = "Keyword"; break; |
| 3380 | case CXToken_Identifier: kind = "Identifier"; break; |
| 3381 | case CXToken_Literal: kind = "Literal"; break; |
| 3382 | case CXToken_Comment: kind = "Comment"; break; |
| 3383 | } |
Douglas Gregor | 229bebd | 2010-11-09 06:24:54 +0000 | [diff] [blame] | 3384 | clang_getSpellingLocation(clang_getRangeStart(extent), |
| 3385 | 0, &start_line, &start_column, 0); |
| 3386 | clang_getSpellingLocation(clang_getRangeEnd(extent), |
| 3387 | 0, &end_line, &end_column, 0); |
Daniel Dunbar | 98c07e0 | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 3388 | printf("%s: \"%s\" ", kind, clang_getCString(spelling)); |
Benjamin Kramer | af7ae31 | 2012-04-14 09:11:51 +0000 | [diff] [blame] | 3389 | clang_disposeString(spelling); |
Daniel Dunbar | 98c07e0 | 2010-02-14 08:32:24 +0000 | [diff] [blame] | 3390 | PrintExtent(stdout, start_line, start_column, end_line, end_column); |
Douglas Gregor | 6165611 | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3391 | if (!clang_isInvalid(cursors[i].kind)) { |
| 3392 | printf(" "); |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 3393 | PrintCursor(cursors[i], NULL); |
Douglas Gregor | 6165611 | 2010-01-26 18:31:56 +0000 | [diff] [blame] | 3394 | } |
| 3395 | printf("\n"); |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3396 | } |
| 3397 | free(cursors); |
Ted Kremenek | 983fb5de | 2010-10-20 21:22:15 +0000 | [diff] [blame] | 3398 | clang_disposeTokens(TU, tokens, num_tokens); |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3399 | |
| 3400 | teardown: |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 3401 | PrintDiagnostics(TU); |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 3402 | clang_disposeTranslationUnit(TU); |
| 3403 | clang_disposeIndex(CIdx); |
| 3404 | free(filename); |
| 3405 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 3406 | return errorCode; |
| 3407 | } |
| 3408 | |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3409 | static int |
| 3410 | perform_test_compilation_db(const char *database, int argc, const char **argv) { |
| 3411 | CXCompilationDatabase db; |
| 3412 | CXCompileCommands CCmds; |
| 3413 | CXCompileCommand CCmd; |
| 3414 | CXCompilationDatabase_Error ec; |
| 3415 | CXString wd; |
| 3416 | CXString arg; |
| 3417 | int errorCode = 0; |
| 3418 | char *tmp; |
| 3419 | unsigned len; |
| 3420 | char *buildDir; |
| 3421 | int i, j, a, numCmds, numArgs; |
| 3422 | |
| 3423 | len = strlen(database); |
| 3424 | tmp = (char *) malloc(len+1); |
| 3425 | memcpy(tmp, database, len+1); |
| 3426 | buildDir = dirname(tmp); |
| 3427 | |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3428 | db = clang_CompilationDatabase_fromDirectory(buildDir, &ec); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3429 | |
| 3430 | if (db) { |
| 3431 | |
| 3432 | if (ec!=CXCompilationDatabase_NoError) { |
| 3433 | printf("unexpected error %d code while loading compilation database\n", ec); |
| 3434 | errorCode = -1; |
| 3435 | goto cdb_end; |
| 3436 | } |
| 3437 | |
| 3438 | for (i=0; i<argc && errorCode==0; ) { |
| 3439 | if (strcmp(argv[i],"lookup")==0){ |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3440 | CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3441 | |
| 3442 | if (!CCmds) { |
| 3443 | printf("file %s not found in compilation db\n", argv[i+1]); |
| 3444 | errorCode = -1; |
| 3445 | break; |
| 3446 | } |
| 3447 | |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3448 | numCmds = clang_CompileCommands_getSize(CCmds); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3449 | |
| 3450 | if (numCmds==0) { |
| 3451 | fprintf(stderr, "should not get an empty compileCommand set for file" |
| 3452 | " '%s'\n", argv[i+1]); |
| 3453 | errorCode = -1; |
| 3454 | break; |
| 3455 | } |
| 3456 | |
| 3457 | for (j=0; j<numCmds; ++j) { |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3458 | CCmd = clang_CompileCommands_getCommand(CCmds, j); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3459 | |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3460 | wd = clang_CompileCommand_getDirectory(CCmd); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3461 | printf("workdir:'%s'", clang_getCString(wd)); |
| 3462 | clang_disposeString(wd); |
| 3463 | |
| 3464 | printf(" cmdline:'"); |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3465 | numArgs = clang_CompileCommand_getNumArgs(CCmd); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3466 | for (a=0; a<numArgs; ++a) { |
| 3467 | if (a) printf(" "); |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3468 | arg = clang_CompileCommand_getArg(CCmd, a); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3469 | printf("%s", clang_getCString(arg)); |
| 3470 | clang_disposeString(arg); |
| 3471 | } |
| 3472 | printf("'\n"); |
| 3473 | } |
| 3474 | |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3475 | clang_CompileCommands_dispose(CCmds); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3476 | |
| 3477 | i += 2; |
| 3478 | } |
| 3479 | } |
Arnaud A. de Grandmaison | fa6d73c | 2012-07-03 20:38:12 +0000 | [diff] [blame] | 3480 | clang_CompilationDatabase_dispose(db); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3481 | } else { |
| 3482 | printf("database loading failed with error code %d.\n", ec); |
| 3483 | errorCode = -1; |
| 3484 | } |
| 3485 | |
| 3486 | cdb_end: |
| 3487 | free(tmp); |
| 3488 | |
| 3489 | return errorCode; |
| 3490 | } |
| 3491 | |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 3492 | /******************************************************************************/ |
Ted Kremenek | 599d73a | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 3493 | /* USR printing. */ |
| 3494 | /******************************************************************************/ |
| 3495 | |
| 3496 | static int insufficient_usr(const char *kind, const char *usage) { |
| 3497 | fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage); |
| 3498 | return 1; |
| 3499 | } |
| 3500 | |
| 3501 | static unsigned isUSR(const char *s) { |
| 3502 | return s[0] == 'c' && s[1] == ':'; |
| 3503 | } |
| 3504 | |
| 3505 | static int not_usr(const char *s, const char *arg) { |
| 3506 | fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg); |
| 3507 | return 1; |
| 3508 | } |
| 3509 | |
| 3510 | static void print_usr(CXString usr) { |
| 3511 | const char *s = clang_getCString(usr); |
| 3512 | printf("%s\n", s); |
| 3513 | clang_disposeString(usr); |
| 3514 | } |
| 3515 | |
| 3516 | static void display_usrs() { |
| 3517 | fprintf(stderr, "-print-usrs options:\n" |
| 3518 | " ObjCCategory <class name> <category name>\n" |
| 3519 | " ObjCClass <class name>\n" |
| 3520 | " ObjCIvar <ivar name> <class USR>\n" |
| 3521 | " ObjCMethod <selector> [0=class method|1=instance method] " |
| 3522 | "<class USR>\n" |
| 3523 | " ObjCProperty <property name> <class USR>\n" |
| 3524 | " ObjCProtocol <protocol name>\n"); |
| 3525 | } |
| 3526 | |
| 3527 | int print_usrs(const char **I, const char **E) { |
| 3528 | while (I != E) { |
| 3529 | const char *kind = *I; |
| 3530 | unsigned len = strlen(kind); |
| 3531 | switch (len) { |
| 3532 | case 8: |
| 3533 | if (memcmp(kind, "ObjCIvar", 8) == 0) { |
| 3534 | if (I + 2 >= E) |
| 3535 | return insufficient_usr(kind, "<ivar name> <class USR>"); |
| 3536 | if (!isUSR(I[2])) |
| 3537 | return not_usr("<class USR>", I[2]); |
| 3538 | else { |
| 3539 | CXString x; |
Ted Kremenek | 9155428 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3540 | x.data = (void*) I[2]; |
Ted Kremenek | 4b4f369 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 3541 | x.private_flags = 0; |
Ted Kremenek | 599d73a | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 3542 | print_usr(clang_constructUSR_ObjCIvar(I[1], x)); |
| 3543 | } |
| 3544 | |
| 3545 | I += 3; |
| 3546 | continue; |
| 3547 | } |
| 3548 | break; |
| 3549 | case 9: |
| 3550 | if (memcmp(kind, "ObjCClass", 9) == 0) { |
| 3551 | if (I + 1 >= E) |
| 3552 | return insufficient_usr(kind, "<class name>"); |
| 3553 | print_usr(clang_constructUSR_ObjCClass(I[1])); |
| 3554 | I += 2; |
| 3555 | continue; |
| 3556 | } |
| 3557 | break; |
| 3558 | case 10: |
| 3559 | if (memcmp(kind, "ObjCMethod", 10) == 0) { |
| 3560 | if (I + 3 >= E) |
| 3561 | return insufficient_usr(kind, "<method selector> " |
| 3562 | "[0=class method|1=instance method] <class USR>"); |
| 3563 | if (!isUSR(I[3])) |
| 3564 | return not_usr("<class USR>", I[3]); |
| 3565 | else { |
| 3566 | CXString x; |
Ted Kremenek | 9155428 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3567 | x.data = (void*) I[3]; |
Ted Kremenek | 4b4f369 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 3568 | x.private_flags = 0; |
Ted Kremenek | 599d73a | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 3569 | print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x)); |
| 3570 | } |
| 3571 | I += 4; |
| 3572 | continue; |
| 3573 | } |
| 3574 | break; |
| 3575 | case 12: |
| 3576 | if (memcmp(kind, "ObjCCategory", 12) == 0) { |
| 3577 | if (I + 2 >= E) |
| 3578 | return insufficient_usr(kind, "<class name> <category name>"); |
| 3579 | print_usr(clang_constructUSR_ObjCCategory(I[1], I[2])); |
| 3580 | I += 3; |
| 3581 | continue; |
| 3582 | } |
| 3583 | if (memcmp(kind, "ObjCProtocol", 12) == 0) { |
| 3584 | if (I + 1 >= E) |
| 3585 | return insufficient_usr(kind, "<protocol name>"); |
| 3586 | print_usr(clang_constructUSR_ObjCProtocol(I[1])); |
| 3587 | I += 2; |
| 3588 | continue; |
| 3589 | } |
| 3590 | if (memcmp(kind, "ObjCProperty", 12) == 0) { |
| 3591 | if (I + 2 >= E) |
| 3592 | return insufficient_usr(kind, "<property name> <class USR>"); |
| 3593 | if (!isUSR(I[2])) |
| 3594 | return not_usr("<class USR>", I[2]); |
| 3595 | else { |
| 3596 | CXString x; |
Ted Kremenek | 9155428 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 3597 | x.data = (void*) I[2]; |
Ted Kremenek | 4b4f369 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 3598 | x.private_flags = 0; |
Ted Kremenek | 599d73a | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 3599 | print_usr(clang_constructUSR_ObjCProperty(I[1], x)); |
| 3600 | } |
| 3601 | I += 3; |
| 3602 | continue; |
| 3603 | } |
| 3604 | break; |
| 3605 | default: |
| 3606 | break; |
| 3607 | } |
| 3608 | break; |
| 3609 | } |
| 3610 | |
| 3611 | if (I != E) { |
| 3612 | fprintf(stderr, "Invalid USR kind: %s\n", *I); |
| 3613 | display_usrs(); |
| 3614 | return 1; |
| 3615 | } |
| 3616 | return 0; |
| 3617 | } |
| 3618 | |
| 3619 | int print_usrs_file(const char *file_name) { |
| 3620 | char line[2048]; |
| 3621 | const char *args[128]; |
| 3622 | unsigned numChars = 0; |
| 3623 | |
| 3624 | FILE *fp = fopen(file_name, "r"); |
| 3625 | if (!fp) { |
| 3626 | fprintf(stderr, "error: cannot open '%s'\n", file_name); |
| 3627 | return 1; |
| 3628 | } |
| 3629 | |
| 3630 | /* This code is not really all that safe, but it works fine for testing. */ |
| 3631 | while (!feof(fp)) { |
| 3632 | char c = fgetc(fp); |
| 3633 | if (c == '\n') { |
| 3634 | unsigned i = 0; |
| 3635 | const char *s = 0; |
| 3636 | |
| 3637 | if (numChars == 0) |
| 3638 | continue; |
| 3639 | |
| 3640 | line[numChars] = '\0'; |
| 3641 | numChars = 0; |
| 3642 | |
| 3643 | if (line[0] == '/' && line[1] == '/') |
| 3644 | continue; |
| 3645 | |
| 3646 | s = strtok(line, " "); |
| 3647 | while (s) { |
| 3648 | args[i] = s; |
| 3649 | ++i; |
| 3650 | s = strtok(0, " "); |
| 3651 | } |
| 3652 | if (print_usrs(&args[0], &args[i])) |
| 3653 | return 1; |
| 3654 | } |
| 3655 | else |
| 3656 | line[numChars++] = c; |
| 3657 | } |
| 3658 | |
| 3659 | fclose(fp); |
| 3660 | return 0; |
| 3661 | } |
| 3662 | |
| 3663 | /******************************************************************************/ |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 3664 | /* Command line processing. */ |
| 3665 | /******************************************************************************/ |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3666 | int write_pch_file(const char *filename, int argc, const char *argv[]) { |
| 3667 | CXIndex Idx; |
| 3668 | CXTranslationUnit TU; |
| 3669 | struct CXUnsavedFile *unsaved_files = 0; |
| 3670 | int num_unsaved_files = 0; |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3671 | enum CXErrorCode Err; |
Francois Pichet | abcfbec | 2011-07-06 22:09:44 +0000 | [diff] [blame] | 3672 | int result = 0; |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3673 | |
Stefanus Du Toit | b331850 | 2013-03-01 21:41:22 +0000 | [diff] [blame] | 3674 | Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1); |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3675 | |
| 3676 | if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| 3677 | clang_disposeIndex(Idx); |
| 3678 | return -1; |
| 3679 | } |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3680 | |
| 3681 | Err = clang_parseTranslationUnit2( |
| 3682 | Idx, 0, argv + num_unsaved_files, argc - num_unsaved_files, |
| 3683 | unsaved_files, num_unsaved_files, |
| 3684 | CXTranslationUnit_Incomplete | |
| 3685 | CXTranslationUnit_DetailedPreprocessingRecord | |
| 3686 | CXTranslationUnit_ForSerialization, |
| 3687 | &TU); |
| 3688 | if (Err != CXError_Success) { |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3689 | fprintf(stderr, "Unable to load translation unit!\n"); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3690 | describeLibclangFailure(Err); |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3691 | free_remapped_files(unsaved_files, num_unsaved_files); |
Dmitri Gribenko | ea4d1c3 | 2014-02-12 19:12:37 +0000 | [diff] [blame] | 3692 | clang_disposeTranslationUnit(TU); |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3693 | clang_disposeIndex(Idx); |
| 3694 | return 1; |
| 3695 | } |
| 3696 | |
Douglas Gregor | 30c80fa | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 3697 | switch (clang_saveTranslationUnit(TU, filename, |
| 3698 | clang_defaultSaveOptions(TU))) { |
| 3699 | case CXSaveError_None: |
| 3700 | break; |
| 3701 | |
| 3702 | case CXSaveError_TranslationErrors: |
| 3703 | fprintf(stderr, "Unable to write PCH file %s: translation errors\n", |
| 3704 | filename); |
| 3705 | result = 2; |
| 3706 | break; |
| 3707 | |
| 3708 | case CXSaveError_InvalidTU: |
| 3709 | fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n", |
| 3710 | filename); |
| 3711 | result = 3; |
| 3712 | break; |
| 3713 | |
| 3714 | case CXSaveError_Unknown: |
| 3715 | default: |
| 3716 | fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename); |
| 3717 | result = 1; |
| 3718 | break; |
| 3719 | } |
| 3720 | |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3721 | clang_disposeTranslationUnit(TU); |
| 3722 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 3723 | clang_disposeIndex(Idx); |
Douglas Gregor | 30c80fa | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 3724 | return result; |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | /******************************************************************************/ |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3728 | /* Serialized diagnostics. */ |
| 3729 | /******************************************************************************/ |
| 3730 | |
| 3731 | static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) { |
| 3732 | switch (error) { |
| 3733 | case CXLoadDiag_CannotLoad: return "Cannot Load File"; |
| 3734 | case CXLoadDiag_None: break; |
| 3735 | case CXLoadDiag_Unknown: return "Unknown"; |
| 3736 | case CXLoadDiag_InvalidFile: return "Invalid File"; |
| 3737 | } |
| 3738 | return "None"; |
| 3739 | } |
| 3740 | |
| 3741 | static const char *getSeverityString(enum CXDiagnosticSeverity severity) { |
| 3742 | switch (severity) { |
| 3743 | case CXDiagnostic_Note: return "note"; |
| 3744 | case CXDiagnostic_Error: return "error"; |
| 3745 | case CXDiagnostic_Fatal: return "fatal"; |
| 3746 | case CXDiagnostic_Ignored: return "ignored"; |
| 3747 | case CXDiagnostic_Warning: return "warning"; |
| 3748 | } |
| 3749 | return "unknown"; |
| 3750 | } |
| 3751 | |
| 3752 | static void printIndent(unsigned indent) { |
Ted Kremenek | a0e32fc | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 3753 | if (indent == 0) |
| 3754 | return; |
| 3755 | fprintf(stderr, "+"); |
| 3756 | --indent; |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3757 | while (indent > 0) { |
Ted Kremenek | a0e32fc | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 3758 | fprintf(stderr, "-"); |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3759 | --indent; |
| 3760 | } |
| 3761 | } |
| 3762 | |
| 3763 | static void printLocation(CXSourceLocation L) { |
| 3764 | CXFile File; |
| 3765 | CXString FileName; |
| 3766 | unsigned line, column, offset; |
| 3767 | |
| 3768 | clang_getExpansionLocation(L, &File, &line, &column, &offset); |
| 3769 | FileName = clang_getFileName(File); |
| 3770 | |
| 3771 | fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column); |
| 3772 | clang_disposeString(FileName); |
| 3773 | } |
| 3774 | |
| 3775 | static void printRanges(CXDiagnostic D, unsigned indent) { |
| 3776 | unsigned i, n = clang_getDiagnosticNumRanges(D); |
| 3777 | |
| 3778 | for (i = 0; i < n; ++i) { |
| 3779 | CXSourceLocation Start, End; |
Enea Zaffanella | 476f38a | 2013-07-22 20:58:30 +0000 | [diff] [blame] | 3780 | CXSourceRange SR = clang_getDiagnosticRange(D, i); |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3781 | Start = clang_getRangeStart(SR); |
| 3782 | End = clang_getRangeEnd(SR); |
| 3783 | |
| 3784 | printIndent(indent); |
| 3785 | fprintf(stderr, "Range: "); |
| 3786 | printLocation(Start); |
| 3787 | fprintf(stderr, " "); |
| 3788 | printLocation(End); |
| 3789 | fprintf(stderr, "\n"); |
| 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | static void printFixIts(CXDiagnostic D, unsigned indent) { |
| 3794 | unsigned i, n = clang_getDiagnosticNumFixIts(D); |
Ted Kremenek | 4a64230 | 2012-03-20 20:49:45 +0000 | [diff] [blame] | 3795 | fprintf(stderr, "Number FIXITs = %d\n", n); |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3796 | for (i = 0 ; i < n; ++i) { |
| 3797 | CXSourceRange ReplacementRange; |
| 3798 | CXString text; |
| 3799 | text = clang_getDiagnosticFixIt(D, i, &ReplacementRange); |
| 3800 | |
| 3801 | printIndent(indent); |
| 3802 | fprintf(stderr, "FIXIT: ("); |
| 3803 | printLocation(clang_getRangeStart(ReplacementRange)); |
| 3804 | fprintf(stderr, " - "); |
| 3805 | printLocation(clang_getRangeEnd(ReplacementRange)); |
| 3806 | fprintf(stderr, "): \"%s\"\n", clang_getCString(text)); |
| 3807 | clang_disposeString(text); |
| 3808 | } |
| 3809 | } |
| 3810 | |
| 3811 | static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) { |
NAKAMURA Takumi | 77d9739 | 2011-11-10 09:30:15 +0000 | [diff] [blame] | 3812 | unsigned i, n; |
| 3813 | |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3814 | if (!Diags) |
| 3815 | return; |
| 3816 | |
NAKAMURA Takumi | 77d9739 | 2011-11-10 09:30:15 +0000 | [diff] [blame] | 3817 | n = clang_getNumDiagnosticsInSet(Diags); |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3818 | for (i = 0; i < n; ++i) { |
| 3819 | CXSourceLocation DiagLoc; |
| 3820 | CXDiagnostic D; |
| 3821 | CXFile File; |
Ted Kremenek | 26a6d49 | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 3822 | CXString FileName, DiagSpelling, DiagOption, DiagCat; |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3823 | unsigned line, column, offset; |
Ted Kremenek | 26a6d49 | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 3824 | const char *DiagOptionStr = 0, *DiagCatStr = 0; |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3825 | |
| 3826 | D = clang_getDiagnosticInSet(Diags, i); |
| 3827 | DiagLoc = clang_getDiagnosticLocation(D); |
| 3828 | clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset); |
| 3829 | FileName = clang_getFileName(File); |
| 3830 | DiagSpelling = clang_getDiagnosticSpelling(D); |
| 3831 | |
| 3832 | printIndent(indent); |
| 3833 | |
| 3834 | fprintf(stderr, "%s:%d:%d: %s: %s", |
| 3835 | clang_getCString(FileName), |
| 3836 | line, |
| 3837 | column, |
| 3838 | getSeverityString(clang_getDiagnosticSeverity(D)), |
| 3839 | clang_getCString(DiagSpelling)); |
| 3840 | |
| 3841 | DiagOption = clang_getDiagnosticOption(D, 0); |
| 3842 | DiagOptionStr = clang_getCString(DiagOption); |
| 3843 | if (DiagOptionStr) { |
| 3844 | fprintf(stderr, " [%s]", DiagOptionStr); |
| 3845 | } |
| 3846 | |
Ted Kremenek | 26a6d49 | 2012-04-12 00:03:31 +0000 | [diff] [blame] | 3847 | DiagCat = clang_getDiagnosticCategoryText(D); |
| 3848 | DiagCatStr = clang_getCString(DiagCat); |
| 3849 | if (DiagCatStr) { |
| 3850 | fprintf(stderr, " [%s]", DiagCatStr); |
| 3851 | } |
| 3852 | |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3853 | fprintf(stderr, "\n"); |
| 3854 | |
| 3855 | printRanges(D, indent); |
| 3856 | printFixIts(D, indent); |
| 3857 | |
NAKAMURA Takumi | 27dd396 | 2011-11-10 10:07:57 +0000 | [diff] [blame] | 3858 | /* Print subdiagnostics. */ |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3859 | printDiagnosticSet(clang_getChildDiagnostics(D), indent+2); |
| 3860 | |
| 3861 | clang_disposeString(FileName); |
| 3862 | clang_disposeString(DiagSpelling); |
| 3863 | clang_disposeString(DiagOption); |
| 3864 | } |
| 3865 | } |
| 3866 | |
| 3867 | static int read_diagnostics(const char *filename) { |
| 3868 | enum CXLoadDiag_Error error; |
| 3869 | CXString errorString; |
| 3870 | CXDiagnosticSet Diags = 0; |
| 3871 | |
| 3872 | Diags = clang_loadDiagnostics(filename, &error, &errorString); |
| 3873 | if (!Diags) { |
| 3874 | fprintf(stderr, "Trouble deserializing file (%s): %s\n", |
| 3875 | getDiagnosticCodeStr(error), |
| 3876 | clang_getCString(errorString)); |
| 3877 | clang_disposeString(errorString); |
| 3878 | return 1; |
| 3879 | } |
| 3880 | |
| 3881 | printDiagnosticSet(Diags, 0); |
Ted Kremenek | a0e32fc | 2011-11-11 00:46:43 +0000 | [diff] [blame] | 3882 | fprintf(stderr, "Number of diagnostics: %d\n", |
| 3883 | clang_getNumDiagnosticsInSet(Diags)); |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3884 | clang_disposeDiagnosticSet(Diags); |
| 3885 | return 0; |
| 3886 | } |
| 3887 | |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 3888 | static int perform_print_build_session_timestamp(void) { |
| 3889 | printf("%lld\n", clang_getBuildSessionTimestamp()); |
| 3890 | return 0; |
| 3891 | } |
| 3892 | |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3893 | /******************************************************************************/ |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3894 | /* Command line processing. */ |
| 3895 | /******************************************************************************/ |
Ted Kremenek | ef3339b | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 3896 | |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 3897 | static CXCursorVisitor GetVisitor(const char *s) { |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3898 | if (s[0] == '\0') |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 3899 | return FilteredPrintingVisitor; |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3900 | if (strcmp(s, "-usrs") == 0) |
| 3901 | return USRVisitor; |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 3902 | if (strncmp(s, "-memory-usage", 13) == 0) |
| 3903 | return GetVisitor(s + 13); |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3904 | return NULL; |
| 3905 | } |
| 3906 | |
Ted Kremenek | ef3339b | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 3907 | static void print_usage(void) { |
| 3908 | fprintf(stderr, |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 3909 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Douglas Gregor | 47815d5 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 3910 | " c-index-test -code-completion-timing=<site> <compiler arguments>\n" |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 3911 | " c-index-test -cursor-at=<site> <compiler arguments>\n" |
Argyrios Kyrtzidis | 503c83a | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 3912 | " c-index-test -file-refs-at=<site> <compiler arguments>\n" |
| 3913 | " c-index-test -file-includes-in=<filename> <compiler arguments>\n"); |
NAKAMURA Takumi | 4deb9a9 | 2012-10-24 22:52:04 +0000 | [diff] [blame] | 3914 | fprintf(stderr, |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3915 | " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n" |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3916 | " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n" |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3917 | " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n" |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3918 | " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n" |
Ted Kremenek | 0469b7e | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 3919 | " c-index-test -test-file-scan <AST file> <source file> " |
Erik Verbruggen | 338b55c | 2011-10-06 11:38:08 +0000 | [diff] [blame] | 3920 | "[FileCheck prefix]\n"); |
| 3921 | fprintf(stderr, |
Ted Kremenek | a44d99c | 2010-01-05 23:18:49 +0000 | [diff] [blame] | 3922 | " c-index-test -test-load-tu <AST file> <symbol filter> " |
| 3923 | "[FileCheck prefix]\n" |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3924 | " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " |
| 3925 | "[FileCheck prefix]\n" |
Douglas Gregor | 47815d5 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 3926 | " c-index-test -test-load-source <symbol filter> {<args>}*\n"); |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 3927 | fprintf(stderr, |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 3928 | " c-index-test -test-load-source-memory-usage " |
| 3929 | "<symbol filter> {<args>}*\n" |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 3930 | " c-index-test -test-load-source-reparse <trials> <symbol filter> " |
| 3931 | " {<args>}*\n" |
Douglas Gregor | 47815d5 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 3932 | " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n" |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 3933 | " c-index-test -test-load-source-usrs-memory-usage " |
| 3934 | "<symbol filter> {<args>}*\n" |
Ted Kremenek | 0b86e3a | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 3935 | " c-index-test -test-annotate-tokens=<range> {<args>}*\n" |
| 3936 | " c-index-test -test-inclusion-stack-source {<args>}*\n" |
Ted Kremenek | 11d1a42 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 3937 | " c-index-test -test-inclusion-stack-tu <AST file>\n"); |
Chandler Carruth | 718df59 | 2010-07-22 06:29:13 +0000 | [diff] [blame] | 3938 | fprintf(stderr, |
Ted Kremenek | 11d1a42 | 2011-04-18 23:42:53 +0000 | [diff] [blame] | 3939 | " c-index-test -test-print-linkage-source {<args>}*\n" |
Dmitri Gribenko | 0035372 | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 3940 | " c-index-test -test-print-type {<args>}*\n" |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 3941 | " c-index-test -test-print-type-size {<args>}*\n" |
Dmitri Gribenko | b506ba1 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 3942 | " c-index-test -test-print-bitwidth {<args>}*\n" |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 3943 | " c-index-test -print-usr [<CursorKind> {<args>}]*\n" |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 3944 | " c-index-test -print-usr-file <file>\n" |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3945 | " c-index-test -write-pch <file> <compiler arguments>\n"); |
| 3946 | fprintf(stderr, |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 3947 | " c-index-test -compilation-db [lookup <filename>] database\n"); |
| 3948 | fprintf(stderr, |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 3949 | " c-index-test -print-build-session-timestamp\n"); |
| 3950 | fprintf(stderr, |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3951 | " c-index-test -read-diagnostics <file>\n\n"); |
Douglas Gregor | 73a18fd | 2010-07-20 14:34:35 +0000 | [diff] [blame] | 3952 | fprintf(stderr, |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3953 | " <symbol filter> values:\n%s", |
Ted Kremenek | 1cd27d5 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 3954 | " all - load all symbols, including those from PCH\n" |
| 3955 | " local - load all symbols except those in PCH\n" |
| 3956 | " category - only load ObjC categories (non-PCH)\n" |
| 3957 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 3958 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 3959 | " function - only load functions (non-PCH)\n" |
Daniel Dunbar | 5442bfc | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 3960 | " typedef - only load typdefs (non-PCH)\n" |
| 3961 | " scan-function - scan function bodies (non-PCH)\n\n"); |
Ted Kremenek | ef3339b | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
Daniel Dunbar | 08b33d0 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 3964 | /***/ |
| 3965 | |
| 3966 | int cindextest_main(int argc, const char **argv) { |
Douglas Gregor | 1e21cc7 | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 3967 | clang_enableStackTraces(); |
Ted Kremenek | d010ba4 | 2011-11-10 08:43:12 +0000 | [diff] [blame] | 3968 | if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0) |
| 3969 | return read_diagnostics(argv[2]); |
Ted Kremenek | ef3339b | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 3970 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
Douglas Gregor | 47815d5 | 2010-07-12 18:38:41 +0000 | [diff] [blame] | 3971 | return perform_code_completion(argc, argv, 0); |
| 3972 | if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1]) |
| 3973 | return perform_code_completion(argc, argv, 1); |
Douglas Gregor | 082c3e6 | 2010-01-15 19:40:17 +0000 | [diff] [blame] | 3974 | if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) |
| 3975 | return inspect_cursor_at(argc, argv); |
Argyrios Kyrtzidis | cddafd3 | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 3976 | if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1]) |
| 3977 | return find_file_refs_at(argc, argv); |
Argyrios Kyrtzidis | 503c83a | 2013-03-08 02:32:34 +0000 | [diff] [blame] | 3978 | if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1]) |
| 3979 | return find_file_includes_in(argc, argv); |
Argyrios Kyrtzidis | dc199a3 | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 3980 | if (argc > 2 && strcmp(argv[1], "-index-file") == 0) |
Argyrios Kyrtzidis | e26c557 | 2012-10-24 18:29:15 +0000 | [diff] [blame] | 3981 | return index_file(argc - 2, argv + 2, /*full=*/0); |
| 3982 | if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0) |
| 3983 | return index_file(argc - 2, argv + 2, /*full=*/1); |
Argyrios Kyrtzidis | d992e14 | 2011-11-15 06:20:16 +0000 | [diff] [blame] | 3984 | if (argc > 2 && strcmp(argv[1], "-index-tu") == 0) |
| 3985 | return index_tu(argc - 2, argv + 2); |
Argyrios Kyrtzidis | f75d498 | 2012-12-05 21:53:37 +0000 | [diff] [blame] | 3986 | if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0) |
| 3987 | return index_compile_db(argc - 2, argv + 2); |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3988 | else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 3989 | CXCursorVisitor I = GetVisitor(argv[1] + 13); |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3990 | if (I) |
Ted Kremenek | b478ff4 | 2010-01-26 17:59:48 +0000 | [diff] [blame] | 3991 | return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, |
| 3992 | NULL); |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 3993 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 3994 | else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){ |
| 3995 | CXCursorVisitor I = GetVisitor(argv[1] + 25); |
| 3996 | if (I) { |
| 3997 | int trials = atoi(argv[2]); |
| 3998 | return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I, |
| 3999 | NULL); |
| 4000 | } |
| 4001 | } |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4002 | else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { |
Douglas Gregor | 720d005 | 2010-01-20 21:32:04 +0000 | [diff] [blame] | 4003 | CXCursorVisitor I = GetVisitor(argv[1] + 17); |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 4004 | |
| 4005 | PostVisitTU postVisit = 0; |
| 4006 | if (strstr(argv[1], "-memory-usage")) |
| 4007 | postVisit = PrintMemoryUsage; |
| 4008 | |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4009 | if (I) |
Ted Kremenek | 83f642e | 2011-04-18 22:47:10 +0000 | [diff] [blame] | 4010 | return perform_test_load_source(argc - 3, argv + 3, argv[2], I, |
| 4011 | postVisit); |
Ted Kremenek | 58a6a8e | 2010-01-12 23:34:26 +0000 | [diff] [blame] | 4012 | } |
| 4013 | else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
Ted Kremenek | 0469b7e | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 4014 | return perform_file_scan(argv[2], argv[3], |
| 4015 | argc >= 5 ? argv[4] : 0); |
Douglas Gregor | 27b4fa9 | 2010-01-26 17:06:03 +0000 | [diff] [blame] | 4016 | else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) |
| 4017 | return perform_token_annotation(argc, argv); |
Ted Kremenek | 0b86e3a | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 4018 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) |
| 4019 | return perform_test_load_source(argc - 2, argv + 2, "all", NULL, |
| 4020 | PrintInclusionStack); |
| 4021 | else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) |
| 4022 | return perform_test_load_tu(argv[2], "all", NULL, NULL, |
| 4023 | PrintInclusionStack); |
Ted Kremenek | 83b28a2 | 2010-03-03 06:37:58 +0000 | [diff] [blame] | 4024 | else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0) |
| 4025 | return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage, |
| 4026 | NULL); |
Dmitri Gribenko | 0035372 | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 4027 | else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0) |
Ted Kremenek | 6bca984 | 2010-05-14 21:29:26 +0000 | [diff] [blame] | 4028 | return perform_test_load_source(argc - 2, argv + 2, "all", |
Dmitri Gribenko | 0035372 | 2013-02-15 21:15:49 +0000 | [diff] [blame] | 4029 | PrintType, 0); |
Argyrios Kyrtzidis | e822f58 | 2013-04-11 01:20:11 +0000 | [diff] [blame] | 4030 | else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0) |
| 4031 | return perform_test_load_source(argc - 2, argv + 2, "all", |
| 4032 | PrintTypeSize, 0); |
Dmitri Gribenko | b506ba1 | 2012-12-04 15:13:46 +0000 | [diff] [blame] | 4033 | else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0) |
| 4034 | return perform_test_load_source(argc - 2, argv + 2, "all", |
| 4035 | PrintBitWidth, 0); |
Ted Kremenek | 599d73a | 2010-03-25 02:00:39 +0000 | [diff] [blame] | 4036 | else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { |
| 4037 | if (argc > 2) |
| 4038 | return print_usrs(argv + 2, argv + argc); |
| 4039 | else { |
| 4040 | display_usrs(); |
| 4041 | return 1; |
| 4042 | } |
| 4043 | } |
| 4044 | else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0) |
| 4045 | return print_usrs_file(argv[2]); |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 4046 | else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0) |
| 4047 | return write_pch_file(argv[2], argc - 3, argv + 3); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 4048 | else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0) |
| 4049 | return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2); |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 4050 | else if (argc == 2 && strcmp(argv[1], "-print-build-session-timestamp") == 0) |
| 4051 | return perform_print_build_session_timestamp(); |
Arnaud A. de Grandmaison | 0fe28a1 | 2012-06-30 11:27:57 +0000 | [diff] [blame] | 4052 | |
Ted Kremenek | ef3339b | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 4053 | print_usage(); |
| 4054 | return 1; |
Steve Naroff | a1c7284 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 4055 | } |
Daniel Dunbar | 08b33d0 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4056 | |
| 4057 | /***/ |
| 4058 | |
| 4059 | /* We intentionally run in a separate thread to ensure we at least minimal |
| 4060 | * testing of a multithreaded environment (for example, having a reduced stack |
| 4061 | * size). */ |
| 4062 | |
Daniel Dunbar | 08b33d0 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4063 | typedef struct thread_info { |
| 4064 | int argc; |
| 4065 | const char **argv; |
| 4066 | int result; |
| 4067 | } thread_info; |
Benjamin Kramer | 112fc6c | 2010-11-04 19:11:31 +0000 | [diff] [blame] | 4068 | void thread_runner(void *client_data_v) { |
Daniel Dunbar | 08b33d0 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4069 | thread_info *client_data = client_data_v; |
| 4070 | client_data->result = cindextest_main(client_data->argc, client_data->argv); |
NAKAMURA Takumi | a7d4988 | 2012-04-07 06:59:28 +0000 | [diff] [blame] | 4071 | #ifdef __CYGWIN__ |
| 4072 | fflush(stdout); /* stdout is not flushed on Cygwin. */ |
| 4073 | #endif |
Daniel Dunbar | 08b33d0 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | int main(int argc, const char **argv) { |
Benjamin Kramer | 3a913ed | 2012-08-10 10:06:13 +0000 | [diff] [blame] | 4077 | thread_info client_data; |
| 4078 | |
Dmitri Gribenko | 740c0fb | 2012-08-07 17:54:38 +0000 | [diff] [blame] | 4079 | #ifdef CLANG_HAVE_LIBXML |
| 4080 | LIBXML_TEST_VERSION |
| 4081 | #endif |
| 4082 | |
Douglas Gregor | f428bf8 | 2010-10-27 16:00:01 +0000 | [diff] [blame] | 4083 | if (getenv("CINDEXTEST_NOTHREADS")) |
| 4084 | return cindextest_main(argc, argv); |
| 4085 | |
Daniel Dunbar | 08b33d0 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4086 | client_data.argc = argc; |
| 4087 | client_data.argv = argv; |
Daniel Dunbar | 23397c3 | 2010-11-04 01:26:31 +0000 | [diff] [blame] | 4088 | clang_executeOnThread(thread_runner, &client_data, 0); |
Daniel Dunbar | 08b33d0 | 2010-09-30 20:39:47 +0000 | [diff] [blame] | 4089 | return client_data.result; |
| 4090 | } |