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