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