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