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" |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 4 | #include <stdlib.h> |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 6 | #include <string.h> |
| 7 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 8 | /******************************************************************************/ |
| 9 | /* Utility functions. */ |
| 10 | /******************************************************************************/ |
| 11 | |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 12 | #ifdef _MSC_VER |
| 13 | char *basename(const char* path) |
| 14 | { |
| 15 | char* base1 = (char*)strrchr(path, '/'); |
| 16 | char* base2 = (char*)strrchr(path, '\\'); |
| 17 | if (base1 && base2) |
| 18 | return((base1 > base2) ? base1 + 1 : base2 + 1); |
| 19 | else if (base1) |
| 20 | return(base1 + 1); |
| 21 | else if (base2) |
| 22 | return(base2 + 1); |
| 23 | |
| 24 | return((char*)path); |
| 25 | } |
| 26 | #else |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 27 | extern char *basename(const char *); |
John Thompson | 2e06fc8 | 2009-10-27 13:42:56 +0000 | [diff] [blame] | 28 | #endif |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 30 | static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, |
| 31 | CXTranslationUnit *TU) { |
| 32 | |
| 33 | *TU = clang_createTranslationUnit(Idx, file); |
| 34 | if (!TU) { |
| 35 | fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); |
| 36 | return 0; |
| 37 | } |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 42 | /******************************************************************************/ |
| 43 | /* Pretty-printing. */ |
| 44 | /******************************************************************************/ |
| 45 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 46 | static void PrintCursor(CXCursor Cursor) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 47 | if (clang_isInvalid(Cursor.kind)) |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 48 | printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 49 | else { |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 50 | CXDecl DeclReferenced; |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 51 | CXString string; |
| 52 | string = clang_getCursorSpelling(Cursor); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 53 | printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind), |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 54 | clang_getCString(string)); |
| 55 | clang_disposeString(string); |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 56 | DeclReferenced = clang_getCursorDecl(Cursor); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 57 | if (DeclReferenced) |
| 58 | printf(":%d:%d", clang_getDeclLine(DeclReferenced), |
| 59 | clang_getDeclColumn(DeclReferenced)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 60 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 61 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 63 | static const char* GetCursorSource(CXCursor Cursor) { |
| 64 | const char *source = clang_getCursorSource(Cursor); |
| 65 | if (!source) |
| 66 | return "<invalid loc>"; |
| 67 | return basename(source); |
| 68 | } |
| 69 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 70 | /******************************************************************************/ |
| 71 | /* Logic for testing clang_loadTranslationUnit(). */ |
| 72 | /******************************************************************************/ |
| 73 | |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 74 | static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 75 | { |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 76 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 77 | CXString string; |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 78 | printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor), |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 79 | clang_getCursorLine(Cursor), |
| 80 | clang_getCursorColumn(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 81 | PrintCursor(Cursor); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 82 | string = clang_getDeclSpelling(Dcl); |
| 83 | printf(" [Context=%s]\n", clang_getCString(string)); |
| 84 | clang_disposeString(string); |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 85 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 86 | } |
| 87 | static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor, |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 88 | CXClientData Filter) |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 89 | { |
| 90 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 91 | CXString string; |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 92 | printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor), |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 93 | clang_getCursorLine(Cursor), |
| 94 | clang_getCursorColumn(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 95 | PrintCursor(Cursor); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 96 | string = clang_getTranslationUnitSpelling(Unit); |
| 97 | printf(" [Context=%s]\n", |
| 98 | basename(clang_getCString(string))); |
| 99 | clang_disposeString(string); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 100 | |
| 101 | clang_loadDeclaration(Cursor.decl, DeclVisitor, 0); |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 102 | |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 103 | if (Cursor.kind == CXCursor_FunctionDefn) { |
| 104 | const char *startBuf, *endBuf; |
| 105 | unsigned startLine, startColumn, endLine, endColumn; |
| 106 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 107 | &startLine, &startColumn, |
| 108 | &endLine, &endColumn); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 109 | { |
| 110 | /* Probe the entire body, looking for both decls and refs. */ |
| 111 | unsigned curLine = startLine, curColumn = startColumn; |
| 112 | CXCursor Ref; |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 113 | |
Steve Naroff | 6a6de8b | 2009-10-21 13:56:23 +0000 | [diff] [blame] | 114 | while (startBuf < endBuf) { |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 115 | if (*startBuf == '\n') { |
| 116 | startBuf++; |
| 117 | curLine++; |
| 118 | curColumn = 1; |
| 119 | } else if (*startBuf != '\t') |
| 120 | curColumn++; |
Ted Kremenek | fbcb2b7 | 2009-10-22 17:22:53 +0000 | [diff] [blame] | 121 | |
Steve Naroff | f96b524 | 2009-10-28 20:44:47 +0000 | [diff] [blame] | 122 | Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor), |
| 123 | curLine, curColumn); |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 124 | if (Ref.kind == CXCursor_NoDeclFound) { |
Ted Kremenek | 8ce88a4 | 2009-10-17 06:37:16 +0000 | [diff] [blame] | 125 | /* Nothing found here; that's fine. */ |
Douglas Gregor | 0246575 | 2009-10-16 21:24:31 +0000 | [diff] [blame] | 126 | } else if (Ref.kind != CXCursor_FunctionDecl) { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 127 | CXString string; |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 128 | printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref), |
| 129 | curLine, curColumn); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 130 | PrintCursor(Ref); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 131 | string = clang_getDeclSpelling(Ref.decl); |
| 132 | printf(" [Context:%s]\n", clang_getCString(string)); |
| 133 | clang_disposeString(string); |
Steve Naroff | f7469a3 | 2009-09-23 20:00:53 +0000 | [diff] [blame] | 134 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 135 | startBuf++; |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 136 | } |
Steve Naroff | 4ade6d6 | 2009-09-23 17:52:52 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 139 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 140 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 142 | int perform_test_load_tu(const char *file, const char *filter) { |
| 143 | CXIndex Idx; |
| 144 | CXTranslationUnit TU; |
| 145 | enum CXCursorKind K = CXCursor_NotImplemented; |
| 146 | enum CXCursorKind *ck = &K; |
| 147 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| 148 | !strcmp(filter, "local") ? 1 : 0, |
| 149 | /* displayDiagnostics */ 1); |
| 150 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 151 | if (!CreateTranslationUnit(Idx, file, &TU)) |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 152 | return 1; |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 153 | |
| 154 | /* Perform some simple filtering. */ |
| 155 | if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
| 156 | else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| 157 | else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 158 | else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 159 | else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| 160 | else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
| 161 | else { |
| 162 | fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | clang_loadTranslationUnit(TU, TranslationUnitVisitor, ck); |
| 167 | clang_disposeTranslationUnit(TU); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | /******************************************************************************/ |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 172 | /* Logic for testing clang_getCursor(). */ |
| 173 | /******************************************************************************/ |
| 174 | |
| 175 | static void print_cursor_file_scan(CXCursor cursor, |
| 176 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame^] | 177 | unsigned end_line, unsigned end_col, |
| 178 | const char *prefix) { |
| 179 | printf("// CHECK"); |
| 180 | if (prefix) |
| 181 | printf("-%s", prefix); |
| 182 | printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ", |
| 183 | start_line, start_col, end_line, end_col); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 184 | PrintCursor(cursor); |
| 185 | printf("\n"); |
| 186 | } |
| 187 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame^] | 188 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 189 | const char *prefix) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 190 | CXIndex Idx; |
| 191 | CXTranslationUnit TU; |
| 192 | FILE *fp; |
| 193 | unsigned line; |
| 194 | CXCursor prevCursor; |
| 195 | unsigned printed; |
| 196 | unsigned start_line, start_col, last_line, last_col; |
| 197 | size_t i; |
| 198 | |
| 199 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
| 200 | /* displayDiagnostics */ 1))) { |
| 201 | fprintf(stderr, "Could not create Index\n"); |
| 202 | return 1; |
| 203 | } |
| 204 | |
| 205 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 206 | return 1; |
| 207 | |
| 208 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 209 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | line = 0; |
| 214 | prevCursor = clang_getNullCursor(); |
| 215 | printed = 0; |
| 216 | start_line = last_line = 1; |
| 217 | start_col = last_col = 1; |
| 218 | |
| 219 | while (!feof(fp)) { |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 220 | size_t len = 0; |
| 221 | int c; |
| 222 | |
| 223 | while ((c = fgetc(fp)) != EOF) { |
| 224 | len++; |
| 225 | if (c == '\n') |
| 226 | break; |
| 227 | } |
| 228 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 229 | ++line; |
| 230 | |
| 231 | for (i = 0; i < len ; ++i) { |
| 232 | CXCursor cursor; |
| 233 | cursor = clang_getCursor(TU, source_file, line, i+1); |
| 234 | |
| 235 | if (!clang_equalCursors(cursor, prevCursor) && |
| 236 | prevCursor.kind != CXCursor_InvalidFile) { |
| 237 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame^] | 238 | last_line, last_col, prefix); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 239 | printed = 1; |
| 240 | start_line = line; |
| 241 | start_col = (unsigned) i+1; |
| 242 | } |
| 243 | else { |
| 244 | printed = 0; |
| 245 | } |
| 246 | |
| 247 | prevCursor = cursor; |
| 248 | last_line = line; |
| 249 | last_col = (unsigned) i+1; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (!printed && prevCursor.kind != CXCursor_InvalidFile) { |
| 254 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame^] | 255 | last_line, last_col, prefix); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | fclose(fp); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 263 | /* Logic for testing clang_codeComplete(). */ |
| 264 | /******************************************************************************/ |
| 265 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 266 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 267 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 268 | memory (that will be owned by the caller) to store the file name. */ |
| 269 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
| 270 | unsigned *column) { |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 271 | /* Find the second colon. */ |
| 272 | const char *second_colon = strrchr(input, ':'), *first_colon; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 273 | char *endptr = 0; |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 274 | if (!second_colon || second_colon == input) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 275 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
| 276 | return 1; |
| 277 | } |
| 278 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 279 | /* Parse the column number. */ |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 280 | *column = strtol(second_colon + 1, &endptr, 10); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 281 | if (*endptr != 0) { |
| 282 | fprintf(stderr, "could not parse column in '%s'\n", input); |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | /* Find the first colon. */ |
| 287 | first_colon = second_colon - 1; |
| 288 | while (first_colon != input && *first_colon != ':') |
| 289 | --first_colon; |
| 290 | if (first_colon == input) { |
| 291 | fprintf(stderr, "could not parse line in '%s'\n", input); |
| 292 | return 1; |
| 293 | } |
| 294 | |
| 295 | /* Parse the line number. */ |
| 296 | *line = strtol(first_colon + 1, &endptr, 10); |
| 297 | if (*endptr != ':') { |
| 298 | fprintf(stderr, "could not parse line in '%s'\n", input); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 299 | return 1; |
| 300 | } |
| 301 | |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 302 | /* Copy the file name. */ |
| 303 | *filename = (char*)malloc(first_colon - input + 1); |
| 304 | memcpy(*filename, input, first_colon - input); |
| 305 | (*filename)[first_colon - input] = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | const char * |
| 310 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 311 | switch (Kind) { |
| 312 | case CXCompletionChunk_Optional: return "Optional"; |
| 313 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 314 | case CXCompletionChunk_Text: return "Text"; |
| 315 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 316 | case CXCompletionChunk_Informative: return "Informative"; |
| 317 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 318 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 319 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 320 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 321 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 322 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 323 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 324 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 325 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 326 | case CXCompletionChunk_Comma: return "Comma"; |
| 327 | } |
| 328 | |
| 329 | return "Unknown"; |
| 330 | } |
| 331 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 332 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 333 | int I, N; |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 334 | |
| 335 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 336 | for (I = 0; I != N; ++I) { |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 337 | const char *text = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 338 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 339 | = clang_getCompletionChunkKind(completion_string, I); |
| 340 | |
| 341 | if (Kind == CXCompletionChunk_Optional) { |
| 342 | fprintf(file, "{Optional "); |
| 343 | print_completion_string( |
| 344 | clang_getCompletionChunkCompletionString(completion_string, I), |
| 345 | file); |
| 346 | fprintf(file, "}"); |
| 347 | continue; |
| 348 | } |
| 349 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 350 | text = clang_getCompletionChunkText(completion_string, I); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 351 | fprintf(file, "{%s %s}", |
| 352 | clang_getCompletionChunkKindSpelling(Kind), |
| 353 | text? text : ""); |
| 354 | } |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void print_completion_result(CXCompletionResult *completion_result, |
| 358 | CXClientData client_data) { |
| 359 | FILE *file = (FILE *)client_data; |
| 360 | fprintf(file, "%s:", |
| 361 | clang_getCursorKindSpelling(completion_result->CursorKind)); |
| 362 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 363 | fprintf(file, "\n"); |
| 364 | } |
| 365 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 366 | int perform_code_completion(int argc, const char **argv) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 367 | const char *input = argv[1]; |
| 368 | char *filename = 0; |
| 369 | unsigned line; |
| 370 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 371 | CXIndex CIdx; |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 372 | int errorCode; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 374 | input += strlen("-code-completion-at="); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 375 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column))) |
| 376 | return errorCode; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 377 | |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 378 | CIdx = clang_createIndex(0, 0); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 379 | clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2, |
| 380 | filename, line, column, &print_completion_result, stdout); |
| 381 | clang_disposeIndex(CIdx); |
| 382 | free(filename); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 383 | |
| 384 | return 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 387 | /******************************************************************************/ |
| 388 | /* Command line processing. */ |
| 389 | /******************************************************************************/ |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 390 | |
| 391 | static void print_usage(void) { |
| 392 | fprintf(stderr, |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 393 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame^] | 394 | " c-index-test -test-file-scan <AST file> <source file> " |
| 395 | "[FileCheck prefix]\n" |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 396 | " c-index-test -test-load-tu <AST file> <symbol filter>\n\n" |
| 397 | " <symbol filter> options for -test-load-tu:\n%s", |
| 398 | " all - load all symbols, including those from PCH\n" |
| 399 | " local - load all symbols except those in PCH\n" |
| 400 | " category - only load ObjC categories (non-PCH)\n" |
| 401 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 402 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 403 | " function - only load functions (non-PCH)\n" |
| 404 | " typedef - only load typdefs (non-PCH)\n\n"); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | int main(int argc, const char **argv) { |
| 408 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
| 409 | return perform_code_completion(argc, argv); |
| 410 | if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0) |
| 411 | return perform_test_load_tu(argv[2], argv[3]); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame^] | 412 | if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
| 413 | return perform_file_scan(argv[2], argv[3], |
| 414 | argc >= 5 ? argv[4] : 0); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 415 | |
| 416 | print_usage(); |
| 417 | return 1; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 418 | } |