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