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