blob: 573222b96c571de7fcd63245a2d573d98ae207e8 [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Douglas Gregor0c8296d2009-11-07 00:00:49 +00004#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00005#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00006#include <string.h>
7
Ted Kremenek0d435192009-11-17 18:13:31 +00008/******************************************************************************/
9/* Utility functions. */
10/******************************************************************************/
11
John Thompson2e06fc82009-10-27 13:42:56 +000012#ifdef _MSC_VER
13char *basename(const char* path)
14{
15 char* base1 = (char*)strrchr(path, '/');
16 char* base2 = (char*)strrchr(path, '\\');
17 if (base1 && base2)
18 return((base1 > base2) ? base1 + 1 : base2 + 1);
19 else if (base1)
20 return(base1 + 1);
21 else if (base2)
22 return(base2 + 1);
23
24 return((char*)path);
25}
26#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000027extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000028#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000029
Ted Kremenek0d435192009-11-17 18:13:31 +000030/******************************************************************************/
31/* Pretty-printing. */
32/******************************************************************************/
33
Steve Naroffaf08ddc2009-09-03 15:49:00 +000034static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000035 if (clang_isInvalid(Cursor.kind))
36 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000037 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000038 CXDecl DeclReferenced;
Steve Naroffef0cef62009-11-09 17:45:52 +000039 CXString string;
40 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000041 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000042 clang_getCString(string));
43 clang_disposeString(string);
Eric Christopherf393c3b2009-10-05 21:33:42 +000044 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000045 if (DeclReferenced)
46 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
47 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000048 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000049}
Steve Naroff89922f82009-08-31 00:59:03 +000050
Ted Kremenek9298cfc2009-11-17 05:31:58 +000051static const char* GetCursorSource(CXCursor Cursor) {
52 const char *source = clang_getCursorSource(Cursor);
53 if (!source)
54 return "<invalid loc>";
55 return basename(source);
56}
57
Ted Kremenek0d435192009-11-17 18:13:31 +000058/******************************************************************************/
59/* Logic for testing clang_loadTranslationUnit(). */
60/******************************************************************************/
61
Eric Christopherf393c3b2009-10-05 21:33:42 +000062static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000063{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000064 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000065 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000066 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000067 clang_getCursorLine(Cursor),
68 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000069 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000070 string = clang_getDeclSpelling(Dcl);
71 printf(" [Context=%s]\n", clang_getCString(string));
72 clang_disposeString(string);
Daniel Dunbarbce6f622009-09-03 05:59:50 +000073 }
Steve Naroffc857ea42009-09-02 13:28:54 +000074}
75static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherf393c3b2009-10-05 21:33:42 +000076 CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000077{
78 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000079 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000080 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000081 clang_getCursorLine(Cursor),
82 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000083 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000084 string = clang_getTranslationUnitSpelling(Unit);
85 printf(" [Context=%s]\n",
86 basename(clang_getCString(string)));
87 clang_disposeString(string);
Steve Naroffff9e18c2009-09-24 20:03:06 +000088
89 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000090
Steve Naroff4ade6d62009-09-23 17:52:52 +000091 if (Cursor.kind == CXCursor_FunctionDefn) {
92 const char *startBuf, *endBuf;
93 unsigned startLine, startColumn, endLine, endColumn;
94 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
95 &startLine, &startColumn,
96 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000097 {
98 /* Probe the entire body, looking for both decls and refs. */
99 unsigned curLine = startLine, curColumn = startColumn;
100 CXCursor Ref;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000101
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000102 while (startBuf < endBuf) {
Steve Narofff7469a32009-09-23 20:00:53 +0000103 if (*startBuf == '\n') {
104 startBuf++;
105 curLine++;
106 curColumn = 1;
107 } else if (*startBuf != '\t')
108 curColumn++;
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000109
Steve Narofff96b5242009-10-28 20:44:47 +0000110 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
111 curLine, curColumn);
Douglas Gregor02465752009-10-16 21:24:31 +0000112 if (Ref.kind == CXCursor_NoDeclFound) {
Ted Kremenek8ce88a42009-10-17 06:37:16 +0000113 /* Nothing found here; that's fine. */
Douglas Gregor02465752009-10-16 21:24:31 +0000114 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffef0cef62009-11-09 17:45:52 +0000115 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000116 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref),
117 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +0000118 PrintCursor(Ref);
Steve Naroffef0cef62009-11-09 17:45:52 +0000119 string = clang_getDeclSpelling(Ref.decl);
120 printf(" [Context:%s]\n", clang_getCString(string));
121 clang_disposeString(string);
Steve Narofff7469a32009-09-23 20:00:53 +0000122 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000123 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +0000124 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000125 }
126 }
Steve Naroff2d4d6292009-08-31 14:26:51 +0000127 }
Steve Naroff89922f82009-08-31 00:59:03 +0000128}
Steve Naroff50398192009-08-28 15:28:48 +0000129
Ted Kremenek0d435192009-11-17 18:13:31 +0000130int perform_test_load_tu(const char *file, const char *filter) {
131 CXIndex Idx;
132 CXTranslationUnit TU;
133 enum CXCursorKind K = CXCursor_NotImplemented;
134 enum CXCursorKind *ck = &K;
135 Idx = clang_createIndex(/* excludeDeclsFromPCH */
136 !strcmp(filter, "local") ? 1 : 0,
137 /* displayDiagnostics */ 1);
138
139 TU = clang_createTranslationUnit(Idx, file);
140
141 if (!TU) {
142 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
143 return 1;
144 }
145
146 /* Perform some simple filtering. */
147 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
148 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
149 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
150 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
151 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
152 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
153 else {
154 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
155 return 1;
156 }
157
158 clang_loadTranslationUnit(TU, TranslationUnitVisitor, ck);
159 clang_disposeTranslationUnit(TU);
160 return 0;
161}
162
163/******************************************************************************/
164/* Logic for testing clang_codeComplete(). */
165/******************************************************************************/
166
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000167/* Parse file:line:column from the input string. Returns 0 on success, non-zero
168 on failure. If successful, the pointer *filename will contain newly-allocated
169 memory (that will be owned by the caller) to store the file name. */
170int parse_file_line_column(const char *input, char **filename, unsigned *line,
171 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000172 /* Find the second colon. */
173 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000174 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000175 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000176 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
177 return 1;
178 }
179
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000180 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000181 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000182 if (*endptr != 0) {
183 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000184 return 1;
185 }
186
187 /* Find the first colon. */
188 first_colon = second_colon - 1;
189 while (first_colon != input && *first_colon != ':')
190 --first_colon;
191 if (first_colon == input) {
192 fprintf(stderr, "could not parse line in '%s'\n", input);
193 return 1;
194 }
195
196 /* Parse the line number. */
197 *line = strtol(first_colon + 1, &endptr, 10);
198 if (*endptr != ':') {
199 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000200 return 1;
201 }
202
Douglas Gregor88d23952009-11-09 18:19:57 +0000203 /* Copy the file name. */
204 *filename = (char*)malloc(first_colon - input + 1);
205 memcpy(*filename, input, first_colon - input);
206 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000207 return 0;
208}
209
210const char *
211clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
212 switch (Kind) {
213 case CXCompletionChunk_Optional: return "Optional";
214 case CXCompletionChunk_TypedText: return "TypedText";
215 case CXCompletionChunk_Text: return "Text";
216 case CXCompletionChunk_Placeholder: return "Placeholder";
217 case CXCompletionChunk_Informative: return "Informative";
218 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
219 case CXCompletionChunk_LeftParen: return "LeftParen";
220 case CXCompletionChunk_RightParen: return "RightParen";
221 case CXCompletionChunk_LeftBracket: return "LeftBracket";
222 case CXCompletionChunk_RightBracket: return "RightBracket";
223 case CXCompletionChunk_LeftBrace: return "LeftBrace";
224 case CXCompletionChunk_RightBrace: return "RightBrace";
225 case CXCompletionChunk_LeftAngle: return "LeftAngle";
226 case CXCompletionChunk_RightAngle: return "RightAngle";
227 case CXCompletionChunk_Comma: return "Comma";
228 }
229
230 return "Unknown";
231}
232
Douglas Gregor3ac73852009-11-09 16:04:45 +0000233void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000234 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000235
236 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000237 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000238 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000239 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000240 = clang_getCompletionChunkKind(completion_string, I);
241
242 if (Kind == CXCompletionChunk_Optional) {
243 fprintf(file, "{Optional ");
244 print_completion_string(
245 clang_getCompletionChunkCompletionString(completion_string, I),
246 file);
247 fprintf(file, "}");
248 continue;
249 }
250
Douglas Gregord5a20892009-11-09 17:05:28 +0000251 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000252 fprintf(file, "{%s %s}",
253 clang_getCompletionChunkKindSpelling(Kind),
254 text? text : "");
255 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000256}
257
258void print_completion_result(CXCompletionResult *completion_result,
259 CXClientData client_data) {
260 FILE *file = (FILE *)client_data;
261 fprintf(file, "%s:",
262 clang_getCursorKindSpelling(completion_result->CursorKind));
263 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000264 fprintf(file, "\n");
265}
266
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000267int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000268 const char *input = argv[1];
269 char *filename = 0;
270 unsigned line;
271 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000272 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000273 int errorCode;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000274
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000275 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000276 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
277 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000278
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000279 CIdx = clang_createIndex(0, 0);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000280 clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2,
281 filename, line, column, &print_completion_result, stdout);
282 clang_disposeIndex(CIdx);
283 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000284
285 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000286}
287
Ted Kremenek0d435192009-11-17 18:13:31 +0000288/******************************************************************************/
289/* Command line processing. */
290/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000291
292static void print_usage(void) {
293 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000294 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
295 " c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
296 " <symbol filter> options for -test-load-tu:\n%s",
297 " all - load all symbols, including those from PCH\n"
298 " local - load all symbols except those in PCH\n"
299 " category - only load ObjC categories (non-PCH)\n"
300 " interface - only load ObjC interfaces (non-PCH)\n"
301 " protocol - only load ObjC protocols (non-PCH)\n"
302 " function - only load functions (non-PCH)\n"
303 " typedef - only load typdefs (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000304}
305
306int main(int argc, const char **argv) {
307 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
308 return perform_code_completion(argc, argv);
309 if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
310 return perform_test_load_tu(argv[2], argv[3]);
311
312 print_usage();
313 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000314}