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 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 41 | /******************************************************************************/ |
| 42 | /* Pretty-printing. */ |
| 43 | /******************************************************************************/ |
| 44 | |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 45 | static void PrintCursor(CXCursor Cursor) { |
Steve Naroff | 77128dd | 2009-09-15 20:25:34 +0000 | [diff] [blame] | 46 | if (clang_isInvalid(Cursor.kind)) |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 47 | printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 48 | else { |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 49 | CXDecl DeclReferenced; |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 50 | CXString string; |
| 51 | string = clang_getCursorSpelling(Cursor); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 52 | printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind), |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 53 | clang_getCString(string)); |
| 54 | clang_disposeString(string); |
Eric Christopher | f393c3b | 2009-10-05 21:33:42 +0000 | [diff] [blame] | 55 | DeclReferenced = clang_getCursorDecl(Cursor); |
Steve Naroff | 85e2db7 | 2009-10-01 00:31:07 +0000 | [diff] [blame] | 56 | if (DeclReferenced) |
| 57 | printf(":%d:%d", clang_getDeclLine(DeclReferenced), |
| 58 | clang_getDeclColumn(DeclReferenced)); |
Steve Naroff | 699a07d | 2009-09-25 21:32:34 +0000 | [diff] [blame] | 59 | } |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 60 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 61 | |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 62 | static const char* GetCursorSource(CXCursor Cursor) { |
| 63 | const char *source = clang_getCursorSource(Cursor); |
| 64 | if (!source) |
| 65 | return "<invalid loc>"; |
| 66 | return basename(source); |
| 67 | } |
| 68 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 69 | /******************************************************************************/ |
| 70 | /* Logic for testing clang_loadTranslationUnit(). */ |
| 71 | /******************************************************************************/ |
| 72 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 73 | static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) { |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 74 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 75 | CXString string; |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 76 | printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor), |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 77 | clang_getCursorLine(Cursor), |
| 78 | clang_getCursorColumn(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 79 | PrintCursor(Cursor); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 80 | string = clang_getDeclSpelling(Dcl); |
| 81 | printf(" [Context=%s]\n", clang_getCString(string)); |
| 82 | clang_disposeString(string); |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 83 | } |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 84 | } |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 85 | |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 86 | static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor, |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 87 | CXClientData Filter) { |
Steve Naroff | c857ea4 | 2009-09-02 13:28:54 +0000 | [diff] [blame] | 88 | if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) { |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 89 | CXString string; |
Ted Kremenek | 9298cfc | 2009-11-17 05:31:58 +0000 | [diff] [blame] | 90 | printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor), |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 91 | clang_getCursorLine(Cursor), |
| 92 | clang_getCursorColumn(Cursor)); |
Steve Naroff | af08ddc | 2009-09-03 15:49:00 +0000 | [diff] [blame] | 93 | PrintCursor(Cursor); |
Steve Naroff | ef0cef6 | 2009-11-09 17:45:52 +0000 | [diff] [blame] | 94 | string = clang_getTranslationUnitSpelling(Unit); |
| 95 | printf(" [Context=%s]\n", |
| 96 | basename(clang_getCString(string))); |
| 97 | clang_disposeString(string); |
Steve Naroff | ff9e18c | 2009-09-24 20:03:06 +0000 | [diff] [blame] | 98 | |
| 99 | clang_loadDeclaration(Cursor.decl, DeclVisitor, 0); |
Steve Naroff | 2d4d629 | 2009-08-31 14:26:51 +0000 | [diff] [blame] | 100 | } |
Steve Naroff | 89922f8 | 2009-08-31 00:59:03 +0000 | [diff] [blame] | 101 | } |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 102 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 103 | static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor, |
| 104 | CXClientData Filter) { |
| 105 | const char *startBuf, *endBuf; |
| 106 | unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; |
| 107 | CXCursor Ref; |
| 108 | |
| 109 | if (Cursor.kind != CXCursor_FunctionDefn) |
| 110 | return; |
| 111 | |
| 112 | clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| 113 | &startLine, &startColumn, |
| 114 | &endLine, &endColumn); |
| 115 | /* Probe the entire body, looking for both decls and refs. */ |
| 116 | curLine = startLine; |
| 117 | curColumn = startColumn; |
| 118 | |
| 119 | while (startBuf < endBuf) { |
| 120 | if (*startBuf == '\n') { |
| 121 | startBuf++; |
| 122 | curLine++; |
| 123 | curColumn = 1; |
| 124 | } else if (*startBuf != '\t') |
| 125 | curColumn++; |
| 126 | |
| 127 | Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor), |
| 128 | curLine, curColumn); |
| 129 | if (Ref.kind == CXCursor_NoDeclFound) { |
| 130 | /* Nothing found here; that's fine. */ |
| 131 | } else if (Ref.kind != CXCursor_FunctionDecl) { |
| 132 | CXString string; |
| 133 | printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref), |
| 134 | curLine, curColumn); |
| 135 | PrintCursor(Ref); |
| 136 | string = clang_getDeclSpelling(Ref.decl); |
| 137 | printf(" [Context:%s]\n", clang_getCString(string)); |
| 138 | clang_disposeString(string); |
| 139 | } |
| 140 | startBuf++; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, |
| 145 | const char *filter) { |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 146 | enum CXCursorKind K = CXCursor_NotImplemented; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 147 | CXTranslationUnitIterator Visitor = TranslationUnitVisitor; |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 148 | enum CXCursorKind *ck = &K; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 149 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 150 | /* Perform some simple filtering. */ |
| 151 | if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
| 152 | else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| 153 | else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| 154 | else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| 155 | else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| 156 | else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 157 | else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 158 | else { |
| 159 | fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| 160 | return 1; |
| 161 | } |
| 162 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 163 | clang_loadTranslationUnit(TU, Visitor, ck); |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 164 | clang_disposeTranslationUnit(TU); |
| 165 | return 0; |
| 166 | } |
| 167 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 168 | int perform_test_load_tu(const char *file, const char *filter) { |
| 169 | CXIndex Idx; |
| 170 | CXTranslationUnit TU; |
| 171 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| 172 | !strcmp(filter, "local") ? 1 : 0, |
| 173 | /* displayDiagnostics */ 1); |
| 174 | |
| 175 | if (!CreateTranslationUnit(Idx, file, &TU)) |
| 176 | return 1; |
| 177 | |
| 178 | return perform_test_load(Idx, TU, filter); |
| 179 | } |
| 180 | |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 181 | int perform_test_load_source(int argc, const char **argv, const char *filter) { |
| 182 | CXIndex Idx; |
| 183 | CXTranslationUnit TU; |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 184 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| 185 | !strcmp(filter, "local") ? 1 : 0, |
| 186 | /* displayDiagnostics */ 1); |
| 187 | |
| 188 | TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv); |
| 189 | if (!TU) { |
| 190 | fprintf(stderr, "Unable to load translation unit!\n"); |
| 191 | return 1; |
| 192 | } |
| 193 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 194 | return perform_test_load(Idx, TU, filter); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 197 | /******************************************************************************/ |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 198 | /* Logic for testing clang_getCursor(). */ |
| 199 | /******************************************************************************/ |
| 200 | |
| 201 | static void print_cursor_file_scan(CXCursor cursor, |
| 202 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 203 | unsigned end_line, unsigned end_col, |
| 204 | const char *prefix) { |
| 205 | printf("// CHECK"); |
| 206 | if (prefix) |
| 207 | printf("-%s", prefix); |
| 208 | printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ", |
| 209 | start_line, start_col, end_line, end_col); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 210 | PrintCursor(cursor); |
| 211 | printf("\n"); |
| 212 | } |
| 213 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 214 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 215 | const char *prefix) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 216 | CXIndex Idx; |
| 217 | CXTranslationUnit TU; |
| 218 | FILE *fp; |
| 219 | unsigned line; |
| 220 | CXCursor prevCursor; |
| 221 | unsigned printed; |
| 222 | unsigned start_line, start_col, last_line, last_col; |
| 223 | size_t i; |
| 224 | |
| 225 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
| 226 | /* displayDiagnostics */ 1))) { |
| 227 | fprintf(stderr, "Could not create Index\n"); |
| 228 | return 1; |
| 229 | } |
| 230 | |
| 231 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 232 | return 1; |
| 233 | |
| 234 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 235 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | line = 0; |
| 240 | prevCursor = clang_getNullCursor(); |
| 241 | printed = 0; |
| 242 | start_line = last_line = 1; |
| 243 | start_col = last_col = 1; |
| 244 | |
| 245 | while (!feof(fp)) { |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 246 | size_t len = 0; |
| 247 | int c; |
| 248 | |
| 249 | while ((c = fgetc(fp)) != EOF) { |
| 250 | len++; |
| 251 | if (c == '\n') |
| 252 | break; |
| 253 | } |
| 254 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 255 | ++line; |
| 256 | |
| 257 | for (i = 0; i < len ; ++i) { |
| 258 | CXCursor cursor; |
| 259 | cursor = clang_getCursor(TU, source_file, line, i+1); |
| 260 | |
| 261 | if (!clang_equalCursors(cursor, prevCursor) && |
| 262 | prevCursor.kind != CXCursor_InvalidFile) { |
| 263 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 264 | last_line, last_col, prefix); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 265 | printed = 1; |
| 266 | start_line = line; |
| 267 | start_col = (unsigned) i+1; |
| 268 | } |
| 269 | else { |
| 270 | printed = 0; |
| 271 | } |
| 272 | |
| 273 | prevCursor = cursor; |
| 274 | last_line = line; |
| 275 | last_col = (unsigned) i+1; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (!printed && prevCursor.kind != CXCursor_InvalidFile) { |
| 280 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 281 | last_line, last_col, prefix); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | fclose(fp); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 289 | /* Logic for testing clang_codeComplete(). */ |
| 290 | /******************************************************************************/ |
| 291 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 292 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 293 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 294 | memory (that will be owned by the caller) to store the file name. */ |
| 295 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
| 296 | unsigned *column) { |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 297 | /* Find the second colon. */ |
| 298 | const char *second_colon = strrchr(input, ':'), *first_colon; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 299 | char *endptr = 0; |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 300 | if (!second_colon || second_colon == input) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 301 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
| 302 | return 1; |
| 303 | } |
| 304 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 305 | /* Parse the column number. */ |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 306 | *column = strtol(second_colon + 1, &endptr, 10); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 307 | if (*endptr != 0) { |
| 308 | fprintf(stderr, "could not parse column in '%s'\n", input); |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | /* Find the first colon. */ |
| 313 | first_colon = second_colon - 1; |
| 314 | while (first_colon != input && *first_colon != ':') |
| 315 | --first_colon; |
| 316 | if (first_colon == input) { |
| 317 | fprintf(stderr, "could not parse line in '%s'\n", input); |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | /* Parse the line number. */ |
| 322 | *line = strtol(first_colon + 1, &endptr, 10); |
| 323 | if (*endptr != ':') { |
| 324 | fprintf(stderr, "could not parse line in '%s'\n", input); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 325 | return 1; |
| 326 | } |
| 327 | |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 328 | /* Copy the file name. */ |
| 329 | *filename = (char*)malloc(first_colon - input + 1); |
| 330 | memcpy(*filename, input, first_colon - input); |
| 331 | (*filename)[first_colon - input] = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | const char * |
| 336 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 337 | switch (Kind) { |
| 338 | case CXCompletionChunk_Optional: return "Optional"; |
| 339 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 340 | case CXCompletionChunk_Text: return "Text"; |
| 341 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 342 | case CXCompletionChunk_Informative: return "Informative"; |
| 343 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 344 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 345 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 346 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 347 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 348 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 349 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 350 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 351 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 352 | case CXCompletionChunk_Comma: return "Comma"; |
| 353 | } |
| 354 | |
| 355 | return "Unknown"; |
| 356 | } |
| 357 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 358 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 359 | int I, N; |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 360 | |
| 361 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 362 | for (I = 0; I != N; ++I) { |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 363 | const char *text = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 364 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 365 | = clang_getCompletionChunkKind(completion_string, I); |
| 366 | |
| 367 | if (Kind == CXCompletionChunk_Optional) { |
| 368 | fprintf(file, "{Optional "); |
| 369 | print_completion_string( |
| 370 | clang_getCompletionChunkCompletionString(completion_string, I), |
| 371 | file); |
| 372 | fprintf(file, "}"); |
| 373 | continue; |
| 374 | } |
| 375 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 376 | text = clang_getCompletionChunkText(completion_string, I); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 377 | fprintf(file, "{%s %s}", |
| 378 | clang_getCompletionChunkKindSpelling(Kind), |
| 379 | text? text : ""); |
| 380 | } |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void print_completion_result(CXCompletionResult *completion_result, |
| 384 | CXClientData client_data) { |
| 385 | FILE *file = (FILE *)client_data; |
| 386 | fprintf(file, "%s:", |
| 387 | clang_getCursorKindSpelling(completion_result->CursorKind)); |
| 388 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 389 | fprintf(file, "\n"); |
| 390 | } |
| 391 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame^] | 392 | void free_remapped_files(struct CXUnsavedFile *unsaved_files, |
| 393 | int num_unsaved_files) { |
| 394 | int i; |
| 395 | for (i = 0; i != num_unsaved_files; ++i) { |
| 396 | free((char *)unsaved_files[i].Filename); |
| 397 | free((char *)unsaved_files[i].Contents); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | int parse_remapped_files(int argc, const char **argv, int start_arg, |
| 402 | struct CXUnsavedFile **unsaved_files, |
| 403 | int *num_unsaved_files) { |
| 404 | int i; |
| 405 | int arg; |
| 406 | int prefix_len = strlen("-remap-file="); |
| 407 | *unsaved_files = 0; |
| 408 | *num_unsaved_files = 0; |
| 409 | |
| 410 | /* Count the number of remapped files. */ |
| 411 | for (arg = start_arg; arg < argc; ++arg) { |
| 412 | if (strncmp(argv[arg], "-remap-file=", prefix_len)) |
| 413 | break; |
| 414 | |
| 415 | ++*num_unsaved_files; |
| 416 | } |
| 417 | |
| 418 | if (*num_unsaved_files == 0) |
| 419 | return 0; |
| 420 | |
| 421 | *unsaved_files |
| 422 | = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * |
| 423 | *num_unsaved_files); |
| 424 | for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) { |
| 425 | struct CXUnsavedFile *unsaved = *unsaved_files + i; |
| 426 | const char *arg_string = argv[arg] + prefix_len; |
| 427 | int filename_len; |
| 428 | char *filename; |
| 429 | char *contents; |
| 430 | FILE *to_file; |
| 431 | const char *semi = strchr(arg_string, ';'); |
| 432 | if (!semi) { |
| 433 | fprintf(stderr, |
| 434 | "error: -remap-file=from;to argument is missing semicolon\n"); |
| 435 | free_remapped_files(*unsaved_files, i); |
| 436 | *unsaved_files = 0; |
| 437 | *num_unsaved_files = 0; |
| 438 | return -1; |
| 439 | } |
| 440 | |
| 441 | /* Open the file that we're remapping to. */ |
| 442 | to_file = fopen(semi + 1, "r"); |
| 443 | if (!to_file) { |
| 444 | fprintf(stderr, "error: cannot open file %s that we are remapping to\n", |
| 445 | semi + 1); |
| 446 | free_remapped_files(*unsaved_files, i); |
| 447 | *unsaved_files = 0; |
| 448 | *num_unsaved_files = 0; |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | /* Determine the length of the file we're remapping to. */ |
| 453 | fseek(to_file, 0, SEEK_END); |
| 454 | unsaved->Length = ftell(to_file); |
| 455 | fseek(to_file, 0, SEEK_SET); |
| 456 | |
| 457 | /* Read the contents of the file we're remapping to. */ |
| 458 | contents = (char *)malloc(unsaved->Length + 1); |
| 459 | fread(contents, 1, unsaved->Length, to_file); |
| 460 | contents[unsaved->Length] = 0; |
| 461 | unsaved->Contents = contents; |
| 462 | |
| 463 | /* Close the file. */ |
| 464 | fclose(to_file); |
| 465 | |
| 466 | /* Copy the file name that we're remapping from. */ |
| 467 | filename_len = semi - arg_string; |
| 468 | filename = (char *)malloc(filename_len + 1); |
| 469 | memcpy(filename, arg_string, filename_len); |
| 470 | filename[filename_len] = 0; |
| 471 | unsaved->Filename = filename; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 477 | int perform_code_completion(int argc, const char **argv) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 478 | const char *input = argv[1]; |
| 479 | char *filename = 0; |
| 480 | unsigned line; |
| 481 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 482 | CXIndex CIdx; |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 483 | int errorCode; |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame^] | 484 | struct CXUnsavedFile *unsaved_files = 0; |
| 485 | int num_unsaved_files = 0; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 486 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 487 | input += strlen("-code-completion-at="); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 488 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column))) |
| 489 | return errorCode; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 490 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame^] | 491 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 492 | return -1; |
| 493 | |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 494 | CIdx = clang_createIndex(0, 0); |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame^] | 495 | clang_codeComplete(CIdx, argv[argc - 1], argc - num_unsaved_files - 3, |
| 496 | argv + num_unsaved_files + 2, |
| 497 | num_unsaved_files, unsaved_files, |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 498 | filename, line, column, &print_completion_result, stdout); |
| 499 | clang_disposeIndex(CIdx); |
| 500 | free(filename); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 501 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame^] | 502 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 503 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 504 | return 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 507 | /******************************************************************************/ |
| 508 | /* Command line processing. */ |
| 509 | /******************************************************************************/ |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 510 | |
| 511 | static void print_usage(void) { |
| 512 | fprintf(stderr, |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 513 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 514 | " c-index-test -test-file-scan <AST file> <source file> " |
| 515 | "[FileCheck prefix]\n" |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 516 | " c-index-test -test-load-tu <AST file> <symbol filter>\n\n" |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 517 | " c-index-test -test-load-source <symbol filter> {<args>}*\n\n" |
| 518 | " <symbol filter> options for -test-load-tu and -test-load-source:\n%s", |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 519 | " all - load all symbols, including those from PCH\n" |
| 520 | " local - load all symbols except those in PCH\n" |
| 521 | " category - only load ObjC categories (non-PCH)\n" |
| 522 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 523 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 524 | " function - only load functions (non-PCH)\n" |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 525 | " typedef - only load typdefs (non-PCH)\n" |
| 526 | " scan-function - scan function bodies (non-PCH)\n\n"); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | int main(int argc, const char **argv) { |
| 530 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
| 531 | return perform_code_completion(argc, argv); |
| 532 | if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0) |
| 533 | return perform_test_load_tu(argv[2], argv[3]); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 534 | if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0) |
| 535 | return perform_test_load_source(argc - 3, argv + 3, argv[2]); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 536 | if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
| 537 | return perform_file_scan(argv[2], argv[3], |
| 538 | argc >= 5 ? argv[4] : 0); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 539 | |
| 540 | print_usage(); |
| 541 | return 1; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 542 | } |