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