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