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