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