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) { |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 182 | const char *UseExternalASTs = |
| 183 | getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION"); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 184 | CXIndex Idx; |
| 185 | CXTranslationUnit TU; |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 186 | Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| 187 | !strcmp(filter, "local") ? 1 : 0, |
| 188 | /* displayDiagnostics */ 1); |
| 189 | |
Daniel Dunbar | 8506dde | 2009-12-03 01:54:28 +0000 | [diff] [blame] | 190 | if (UseExternalASTs && strlen(UseExternalASTs)) |
| 191 | clang_setUseExternalASTGeneration(Idx, 1); |
| 192 | |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 193 | TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv); |
| 194 | if (!TU) { |
| 195 | fprintf(stderr, "Unable to load translation unit!\n"); |
| 196 | return 1; |
| 197 | } |
| 198 | |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 199 | return perform_test_load(Idx, TU, filter); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 202 | /******************************************************************************/ |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 203 | /* Logic for testing clang_getCursor(). */ |
| 204 | /******************************************************************************/ |
| 205 | |
| 206 | static void print_cursor_file_scan(CXCursor cursor, |
| 207 | unsigned start_line, unsigned start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 208 | unsigned end_line, unsigned end_col, |
| 209 | const char *prefix) { |
| 210 | printf("// CHECK"); |
| 211 | if (prefix) |
| 212 | printf("-%s", prefix); |
| 213 | printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ", |
| 214 | start_line, start_col, end_line, end_col); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 215 | PrintCursor(cursor); |
| 216 | printf("\n"); |
| 217 | } |
| 218 | |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 219 | static int perform_file_scan(const char *ast_file, const char *source_file, |
| 220 | const char *prefix) { |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 221 | CXIndex Idx; |
| 222 | CXTranslationUnit TU; |
| 223 | FILE *fp; |
| 224 | unsigned line; |
| 225 | CXCursor prevCursor; |
| 226 | unsigned printed; |
| 227 | unsigned start_line, start_col, last_line, last_col; |
| 228 | size_t i; |
| 229 | |
| 230 | if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
| 231 | /* displayDiagnostics */ 1))) { |
| 232 | fprintf(stderr, "Could not create Index\n"); |
| 233 | return 1; |
| 234 | } |
| 235 | |
| 236 | if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| 237 | return 1; |
| 238 | |
| 239 | if ((fp = fopen(source_file, "r")) == NULL) { |
| 240 | fprintf(stderr, "Could not open '%s'\n", source_file); |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | line = 0; |
| 245 | prevCursor = clang_getNullCursor(); |
| 246 | printed = 0; |
| 247 | start_line = last_line = 1; |
| 248 | start_col = last_col = 1; |
| 249 | |
| 250 | while (!feof(fp)) { |
Benjamin Kramer | a9933b9 | 2009-11-17 20:51:40 +0000 | [diff] [blame] | 251 | size_t len = 0; |
| 252 | int c; |
| 253 | |
| 254 | while ((c = fgetc(fp)) != EOF) { |
| 255 | len++; |
| 256 | if (c == '\n') |
| 257 | break; |
| 258 | } |
| 259 | |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 260 | ++line; |
| 261 | |
| 262 | for (i = 0; i < len ; ++i) { |
| 263 | CXCursor cursor; |
| 264 | cursor = clang_getCursor(TU, source_file, line, i+1); |
| 265 | |
| 266 | if (!clang_equalCursors(cursor, prevCursor) && |
| 267 | prevCursor.kind != CXCursor_InvalidFile) { |
| 268 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 269 | last_line, last_col, prefix); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 270 | printed = 1; |
| 271 | start_line = line; |
| 272 | start_col = (unsigned) i+1; |
| 273 | } |
| 274 | else { |
| 275 | printed = 0; |
| 276 | } |
| 277 | |
| 278 | prevCursor = cursor; |
| 279 | last_line = line; |
| 280 | last_col = (unsigned) i+1; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (!printed && prevCursor.kind != CXCursor_InvalidFile) { |
| 285 | print_cursor_file_scan(prevCursor, start_line, start_col, |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 286 | last_line, last_col, prefix); |
Ted Kremenek | 1c6da17 | 2009-11-17 19:37:36 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | fclose(fp); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /******************************************************************************/ |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 294 | /* Logic for testing clang_codeComplete(). */ |
| 295 | /******************************************************************************/ |
| 296 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 297 | /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| 298 | on failure. If successful, the pointer *filename will contain newly-allocated |
| 299 | memory (that will be owned by the caller) to store the file name. */ |
| 300 | int parse_file_line_column(const char *input, char **filename, unsigned *line, |
| 301 | unsigned *column) { |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 302 | /* Find the second colon. */ |
| 303 | const char *second_colon = strrchr(input, ':'), *first_colon; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 304 | char *endptr = 0; |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 305 | if (!second_colon || second_colon == input) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 306 | fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
| 307 | return 1; |
| 308 | } |
| 309 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 310 | /* Parse the column number. */ |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 311 | *column = strtol(second_colon + 1, &endptr, 10); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 312 | if (*endptr != 0) { |
| 313 | fprintf(stderr, "could not parse column in '%s'\n", input); |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | /* Find the first colon. */ |
| 318 | first_colon = second_colon - 1; |
| 319 | while (first_colon != input && *first_colon != ':') |
| 320 | --first_colon; |
| 321 | if (first_colon == input) { |
| 322 | fprintf(stderr, "could not parse line in '%s'\n", input); |
| 323 | return 1; |
| 324 | } |
| 325 | |
| 326 | /* Parse the line number. */ |
| 327 | *line = strtol(first_colon + 1, &endptr, 10); |
| 328 | if (*endptr != ':') { |
| 329 | fprintf(stderr, "could not parse line in '%s'\n", input); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 330 | return 1; |
| 331 | } |
| 332 | |
Douglas Gregor | 88d2395 | 2009-11-09 18:19:57 +0000 | [diff] [blame] | 333 | /* Copy the file name. */ |
| 334 | *filename = (char*)malloc(first_colon - input + 1); |
| 335 | memcpy(*filename, input, first_colon - input); |
| 336 | (*filename)[first_colon - input] = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | const char * |
| 341 | clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| 342 | switch (Kind) { |
| 343 | case CXCompletionChunk_Optional: return "Optional"; |
| 344 | case CXCompletionChunk_TypedText: return "TypedText"; |
| 345 | case CXCompletionChunk_Text: return "Text"; |
| 346 | case CXCompletionChunk_Placeholder: return "Placeholder"; |
| 347 | case CXCompletionChunk_Informative: return "Informative"; |
| 348 | case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| 349 | case CXCompletionChunk_LeftParen: return "LeftParen"; |
| 350 | case CXCompletionChunk_RightParen: return "RightParen"; |
| 351 | case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| 352 | case CXCompletionChunk_RightBracket: return "RightBracket"; |
| 353 | case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| 354 | case CXCompletionChunk_RightBrace: return "RightBrace"; |
| 355 | case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| 356 | case CXCompletionChunk_RightAngle: return "RightAngle"; |
| 357 | case CXCompletionChunk_Comma: return "Comma"; |
| 358 | } |
| 359 | |
| 360 | return "Unknown"; |
| 361 | } |
| 362 | |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 363 | void print_completion_string(CXCompletionString completion_string, FILE *file) { |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 364 | int I, N; |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 365 | |
| 366 | N = clang_getNumCompletionChunks(completion_string); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 367 | for (I = 0; I != N; ++I) { |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 368 | const char *text = 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 369 | enum CXCompletionChunkKind Kind |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 370 | = clang_getCompletionChunkKind(completion_string, I); |
| 371 | |
| 372 | if (Kind == CXCompletionChunk_Optional) { |
| 373 | fprintf(file, "{Optional "); |
| 374 | print_completion_string( |
| 375 | clang_getCompletionChunkCompletionString(completion_string, I), |
| 376 | file); |
| 377 | fprintf(file, "}"); |
| 378 | continue; |
| 379 | } |
| 380 | |
Douglas Gregor | d5a2089 | 2009-11-09 17:05:28 +0000 | [diff] [blame] | 381 | text = clang_getCompletionChunkText(completion_string, I); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 382 | fprintf(file, "{%s %s}", |
| 383 | clang_getCompletionChunkKindSpelling(Kind), |
| 384 | text? text : ""); |
| 385 | } |
Douglas Gregor | 3ac7385 | 2009-11-09 16:04:45 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void print_completion_result(CXCompletionResult *completion_result, |
| 389 | CXClientData client_data) { |
| 390 | FILE *file = (FILE *)client_data; |
| 391 | fprintf(file, "%s:", |
| 392 | clang_getCursorKindSpelling(completion_result->CursorKind)); |
| 393 | print_completion_string(completion_result->CompletionString, file); |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 394 | fprintf(file, "\n"); |
| 395 | } |
| 396 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 397 | void free_remapped_files(struct CXUnsavedFile *unsaved_files, |
| 398 | int num_unsaved_files) { |
| 399 | int i; |
| 400 | for (i = 0; i != num_unsaved_files; ++i) { |
| 401 | free((char *)unsaved_files[i].Filename); |
| 402 | free((char *)unsaved_files[i].Contents); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | int parse_remapped_files(int argc, const char **argv, int start_arg, |
| 407 | struct CXUnsavedFile **unsaved_files, |
| 408 | int *num_unsaved_files) { |
| 409 | int i; |
| 410 | int arg; |
| 411 | int prefix_len = strlen("-remap-file="); |
| 412 | *unsaved_files = 0; |
| 413 | *num_unsaved_files = 0; |
| 414 | |
| 415 | /* Count the number of remapped files. */ |
| 416 | for (arg = start_arg; arg < argc; ++arg) { |
| 417 | if (strncmp(argv[arg], "-remap-file=", prefix_len)) |
| 418 | break; |
| 419 | |
| 420 | ++*num_unsaved_files; |
| 421 | } |
| 422 | |
| 423 | if (*num_unsaved_files == 0) |
| 424 | return 0; |
| 425 | |
| 426 | *unsaved_files |
| 427 | = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * |
| 428 | *num_unsaved_files); |
| 429 | for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) { |
| 430 | struct CXUnsavedFile *unsaved = *unsaved_files + i; |
| 431 | const char *arg_string = argv[arg] + prefix_len; |
| 432 | int filename_len; |
| 433 | char *filename; |
| 434 | char *contents; |
| 435 | FILE *to_file; |
| 436 | const char *semi = strchr(arg_string, ';'); |
| 437 | if (!semi) { |
| 438 | fprintf(stderr, |
| 439 | "error: -remap-file=from;to argument is missing semicolon\n"); |
| 440 | free_remapped_files(*unsaved_files, i); |
| 441 | *unsaved_files = 0; |
| 442 | *num_unsaved_files = 0; |
| 443 | return -1; |
| 444 | } |
| 445 | |
| 446 | /* Open the file that we're remapping to. */ |
| 447 | to_file = fopen(semi + 1, "r"); |
| 448 | if (!to_file) { |
| 449 | fprintf(stderr, "error: cannot open file %s that we are remapping to\n", |
| 450 | semi + 1); |
| 451 | free_remapped_files(*unsaved_files, i); |
| 452 | *unsaved_files = 0; |
| 453 | *num_unsaved_files = 0; |
| 454 | return -1; |
| 455 | } |
| 456 | |
| 457 | /* Determine the length of the file we're remapping to. */ |
| 458 | fseek(to_file, 0, SEEK_END); |
| 459 | unsaved->Length = ftell(to_file); |
| 460 | fseek(to_file, 0, SEEK_SET); |
| 461 | |
| 462 | /* Read the contents of the file we're remapping to. */ |
| 463 | contents = (char *)malloc(unsaved->Length + 1); |
Chandler Carruth | 4da689a | 2009-12-17 09:18:43 +0000 | [diff] [blame] | 464 | if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) { |
| 465 | fprintf(stderr, "error: unexpected %s reading 'to' file %s\n", |
| 466 | (feof(to_file) ? "EOF" : "error"), semi + 1); |
| 467 | fclose(to_file); |
| 468 | free_remapped_files(*unsaved_files, i); |
| 469 | *unsaved_files = 0; |
| 470 | *num_unsaved_files = 0; |
| 471 | return -1; |
| 472 | } |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 473 | contents[unsaved->Length] = 0; |
| 474 | unsaved->Contents = contents; |
| 475 | |
| 476 | /* Close the file. */ |
| 477 | fclose(to_file); |
| 478 | |
| 479 | /* Copy the file name that we're remapping from. */ |
| 480 | filename_len = semi - arg_string; |
| 481 | filename = (char *)malloc(filename_len + 1); |
| 482 | memcpy(filename, arg_string, filename_len); |
| 483 | filename[filename_len] = 0; |
| 484 | unsaved->Filename = filename; |
| 485 | } |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 490 | int perform_code_completion(int argc, const char **argv) { |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 491 | const char *input = argv[1]; |
| 492 | char *filename = 0; |
| 493 | unsigned line; |
| 494 | unsigned column; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 495 | CXIndex CIdx; |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 496 | int errorCode; |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 497 | struct CXUnsavedFile *unsaved_files = 0; |
| 498 | int num_unsaved_files = 0; |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame^] | 499 | CXCodeCompleteResults *results = 0; |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 501 | input += strlen("-code-completion-at="); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 502 | if ((errorCode = parse_file_line_column(input, &filename, &line, &column))) |
| 503 | return errorCode; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 504 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 505 | if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| 506 | return -1; |
| 507 | |
Daniel Dunbar | f8297f1 | 2009-11-07 18:34:24 +0000 | [diff] [blame] | 508 | CIdx = clang_createIndex(0, 0); |
Douglas Gregor | ec6762c | 2009-12-18 16:20:58 +0000 | [diff] [blame^] | 509 | results = clang_codeComplete(CIdx, |
| 510 | argv[argc - 1], argc - num_unsaved_files - 3, |
| 511 | argv + num_unsaved_files + 2, |
| 512 | num_unsaved_files, unsaved_files, |
| 513 | filename, line, column); |
| 514 | if (results) { |
| 515 | unsigned i, n = results->NumResults; |
| 516 | for (i = 0; i != n; ++i) |
| 517 | print_completion_result(results->Results + i, stdout); |
| 518 | clang_disposeCodeCompleteResults(results); |
| 519 | } |
| 520 | |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 521 | clang_disposeIndex(CIdx); |
| 522 | free(filename); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 523 | |
Douglas Gregor | 735df88 | 2009-12-02 09:21:34 +0000 | [diff] [blame] | 524 | free_remapped_files(unsaved_files, num_unsaved_files); |
| 525 | |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 526 | return 0; |
Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 529 | /******************************************************************************/ |
| 530 | /* Command line processing. */ |
| 531 | /******************************************************************************/ |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 532 | |
| 533 | static void print_usage(void) { |
| 534 | fprintf(stderr, |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 535 | "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 536 | " c-index-test -test-file-scan <AST file> <source file> " |
| 537 | "[FileCheck prefix]\n" |
Ted Kremenek | 0d43519 | 2009-11-17 18:13:31 +0000 | [diff] [blame] | 538 | " c-index-test -test-load-tu <AST file> <symbol filter>\n\n" |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 539 | " c-index-test -test-load-source <symbol filter> {<args>}*\n\n" |
| 540 | " <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] | 541 | " all - load all symbols, including those from PCH\n" |
| 542 | " local - load all symbols except those in PCH\n" |
| 543 | " category - only load ObjC categories (non-PCH)\n" |
| 544 | " interface - only load ObjC interfaces (non-PCH)\n" |
| 545 | " protocol - only load ObjC protocols (non-PCH)\n" |
| 546 | " function - only load functions (non-PCH)\n" |
Daniel Dunbar | 625e4ef | 2009-12-01 02:35:37 +0000 | [diff] [blame] | 547 | " typedef - only load typdefs (non-PCH)\n" |
| 548 | " scan-function - scan function bodies (non-PCH)\n\n"); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | int main(int argc, const char **argv) { |
| 552 | if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) |
| 553 | return perform_code_completion(argc, argv); |
| 554 | if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0) |
| 555 | return perform_test_load_tu(argv[2], argv[3]); |
Daniel Dunbar | ada487d | 2009-12-01 02:03:10 +0000 | [diff] [blame] | 556 | if (argc >= 4 && strcmp(argv[1], "-test-load-source") == 0) |
| 557 | return perform_test_load_source(argc - 3, argv + 3, argv[2]); |
Ted Kremenek | 1d5fdf3 | 2009-11-18 02:02:52 +0000 | [diff] [blame] | 558 | if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) |
| 559 | return perform_file_scan(argv[2], argv[3], |
| 560 | argc >= 5 ? argv[4] : 0); |
Ted Kremenek | f5d9c93 | 2009-11-17 18:09:14 +0000 | [diff] [blame] | 561 | |
| 562 | print_usage(); |
| 563 | return 1; |
Steve Naroff | 5039819 | 2009-08-28 15:28:48 +0000 | [diff] [blame] | 564 | } |