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