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