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