blob: 3b0e4c8e9c3a4354462208265e0845da69c7e408 [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
John Thompson2e06fc82009-10-27 13:42:56 +00008#ifdef _MSC_VER
9char *basename(const char* path)
10{
11 char* base1 = (char*)strrchr(path, '/');
12 char* base2 = (char*)strrchr(path, '\\');
13 if (base1 && base2)
14 return((base1 > base2) ? base1 + 1 : base2 + 1);
15 else if (base1)
16 return(base1 + 1);
17 else if (base2)
18 return(base2 + 1);
19
20 return((char*)path);
21}
22#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000023extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000024#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000025
Steve Naroffaf08ddc2009-09-03 15:49:00 +000026static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000027 if (clang_isInvalid(Cursor.kind))
28 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000029 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000030 CXDecl DeclReferenced;
Steve Naroffef0cef62009-11-09 17:45:52 +000031 CXString string;
32 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000033 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000034 clang_getCString(string));
35 clang_disposeString(string);
Eric Christopherf393c3b2009-10-05 21:33:42 +000036 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000037 if (DeclReferenced)
38 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
39 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000040 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000041}
Steve Naroff89922f82009-08-31 00:59:03 +000042
Ted Kremenek9298cfc2009-11-17 05:31:58 +000043static const char* GetCursorSource(CXCursor Cursor) {
44 const char *source = clang_getCursorSource(Cursor);
45 if (!source)
46 return "<invalid loc>";
47 return basename(source);
48}
49
Eric Christopherf393c3b2009-10-05 21:33:42 +000050static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000051{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000052 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000053 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000054 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000055 clang_getCursorLine(Cursor),
56 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000057 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000058 string = clang_getDeclSpelling(Dcl);
59 printf(" [Context=%s]\n", clang_getCString(string));
60 clang_disposeString(string);
Daniel Dunbarbce6f622009-09-03 05:59:50 +000061 }
Steve Naroffc857ea42009-09-02 13:28:54 +000062}
63static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherf393c3b2009-10-05 21:33:42 +000064 CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000065{
66 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000067 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000068 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000069 clang_getCursorLine(Cursor),
70 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000071 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000072 string = clang_getTranslationUnitSpelling(Unit);
73 printf(" [Context=%s]\n",
74 basename(clang_getCString(string)));
75 clang_disposeString(string);
Steve Naroffff9e18c2009-09-24 20:03:06 +000076
77 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000078
Steve Naroff4ade6d62009-09-23 17:52:52 +000079 if (Cursor.kind == CXCursor_FunctionDefn) {
80 const char *startBuf, *endBuf;
81 unsigned startLine, startColumn, endLine, endColumn;
82 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
83 &startLine, &startColumn,
84 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000085 {
86 /* Probe the entire body, looking for both decls and refs. */
87 unsigned curLine = startLine, curColumn = startColumn;
88 CXCursor Ref;
Eric Christopherf393c3b2009-10-05 21:33:42 +000089
Steve Naroff6a6de8b2009-10-21 13:56:23 +000090 while (startBuf < endBuf) {
Steve Narofff7469a32009-09-23 20:00:53 +000091 if (*startBuf == '\n') {
92 startBuf++;
93 curLine++;
94 curColumn = 1;
95 } else if (*startBuf != '\t')
96 curColumn++;
Ted Kremenekfbcb2b72009-10-22 17:22:53 +000097
Steve Narofff96b5242009-10-28 20:44:47 +000098 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
99 curLine, curColumn);
Douglas Gregor02465752009-10-16 21:24:31 +0000100 if (Ref.kind == CXCursor_NoDeclFound) {
Ted Kremenek8ce88a42009-10-17 06:37:16 +0000101 /* Nothing found here; that's fine. */
Douglas Gregor02465752009-10-16 21:24:31 +0000102 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffef0cef62009-11-09 17:45:52 +0000103 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000104 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref),
105 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +0000106 PrintCursor(Ref);
Steve Naroffef0cef62009-11-09 17:45:52 +0000107 string = clang_getDeclSpelling(Ref.decl);
108 printf(" [Context:%s]\n", clang_getCString(string));
109 clang_disposeString(string);
Steve Narofff7469a32009-09-23 20:00:53 +0000110 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000111 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +0000112 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000113 }
114 }
Steve Naroff2d4d6292009-08-31 14:26:51 +0000115 }
Steve Naroff89922f82009-08-31 00:59:03 +0000116}
Steve Naroff50398192009-08-28 15:28:48 +0000117
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000118/* Parse file:line:column from the input string. Returns 0 on success, non-zero
119 on failure. If successful, the pointer *filename will contain newly-allocated
120 memory (that will be owned by the caller) to store the file name. */
121int parse_file_line_column(const char *input, char **filename, unsigned *line,
122 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000123 /* Find the second colon. */
124 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000125 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000126 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000127 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
128 return 1;
129 }
130
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000131 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000132 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000133 if (*endptr != 0) {
134 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000135 return 1;
136 }
137
138 /* Find the first colon. */
139 first_colon = second_colon - 1;
140 while (first_colon != input && *first_colon != ':')
141 --first_colon;
142 if (first_colon == input) {
143 fprintf(stderr, "could not parse line in '%s'\n", input);
144 return 1;
145 }
146
147 /* Parse the line number. */
148 *line = strtol(first_colon + 1, &endptr, 10);
149 if (*endptr != ':') {
150 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000151 return 1;
152 }
153
Douglas Gregor88d23952009-11-09 18:19:57 +0000154 /* Copy the file name. */
155 *filename = (char*)malloc(first_colon - input + 1);
156 memcpy(*filename, input, first_colon - input);
157 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000158 return 0;
159}
160
161const char *
162clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
163 switch (Kind) {
164 case CXCompletionChunk_Optional: return "Optional";
165 case CXCompletionChunk_TypedText: return "TypedText";
166 case CXCompletionChunk_Text: return "Text";
167 case CXCompletionChunk_Placeholder: return "Placeholder";
168 case CXCompletionChunk_Informative: return "Informative";
169 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
170 case CXCompletionChunk_LeftParen: return "LeftParen";
171 case CXCompletionChunk_RightParen: return "RightParen";
172 case CXCompletionChunk_LeftBracket: return "LeftBracket";
173 case CXCompletionChunk_RightBracket: return "RightBracket";
174 case CXCompletionChunk_LeftBrace: return "LeftBrace";
175 case CXCompletionChunk_RightBrace: return "RightBrace";
176 case CXCompletionChunk_LeftAngle: return "LeftAngle";
177 case CXCompletionChunk_RightAngle: return "RightAngle";
178 case CXCompletionChunk_Comma: return "Comma";
179 }
180
181 return "Unknown";
182}
183
Douglas Gregor3ac73852009-11-09 16:04:45 +0000184void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000185 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000186
187 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000188 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000189 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000190 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000191 = clang_getCompletionChunkKind(completion_string, I);
192
193 if (Kind == CXCompletionChunk_Optional) {
194 fprintf(file, "{Optional ");
195 print_completion_string(
196 clang_getCompletionChunkCompletionString(completion_string, I),
197 file);
198 fprintf(file, "}");
199 continue;
200 }
201
Douglas Gregord5a20892009-11-09 17:05:28 +0000202 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000203 fprintf(file, "{%s %s}",
204 clang_getCompletionChunkKindSpelling(Kind),
205 text? text : "");
206 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000207}
208
209void print_completion_result(CXCompletionResult *completion_result,
210 CXClientData client_data) {
211 FILE *file = (FILE *)client_data;
212 fprintf(file, "%s:",
213 clang_getCursorKindSpelling(completion_result->CursorKind));
214 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000215 fprintf(file, "\n");
216}
217
218void perform_code_completion(int argc, const char **argv) {
219 const char *input = argv[1];
220 char *filename = 0;
221 unsigned line;
222 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000223 CXIndex CIdx;
224
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000225 input += strlen("-code-completion-at=");
226 if (parse_file_line_column(input, &filename, &line, &column))
227 return;
228
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000229 CIdx = clang_createIndex(0, 0);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000230 clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2,
231 filename, line, column, &print_completion_result, stdout);
232 clang_disposeIndex(CIdx);
233 free(filename);
234}
235
Steve Naroff50398192009-08-28 15:28:48 +0000236/*
237 * First sign of life:-)
238 */
239int main(int argc, char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000240 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) {
241 perform_code_completion(argc, (const char **)argv);
242 return 0;
243 }
244
245
Steve Narofff7469a32009-09-23 20:00:53 +0000246 if (argc != 3) {
247 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
248 return 0;
249 }
250 {
Ted Kremenek85f54682009-10-17 06:42:15 +0000251 CXIndex Idx;
252 CXTranslationUnit TU;
253 enum CXCursorKind K = CXCursor_NotImplemented;
254
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000255 Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(argv[2], "local") ? 1 : 0,
256 /* displayDiagnostics */ 1);
Ted Kremenek85f54682009-10-17 06:42:15 +0000257
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000258 TU = clang_createTranslationUnit(Idx, argv[1]);
Ted Kremenek85f54682009-10-17 06:42:15 +0000259
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000260 if (!TU) {
261 fprintf(stderr, "Unable to load translation unit!\n");
262 return 1;
263 }
Eric Christopherf393c3b2009-10-05 21:33:42 +0000264
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000265 if (!strcmp(argv[2], "all") || !strcmp(argv[2], "local")) {
Steve Narofff7469a32009-09-23 20:00:53 +0000266 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
Steve Naroffe19944c2009-10-15 22:23:48 +0000267 clang_disposeTranslationUnit(TU);
Steve Narofff7469a32009-09-23 20:00:53 +0000268 return 1;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000269 }
Steve Narofff7469a32009-09-23 20:00:53 +0000270 /* Perform some simple filtering. */
271 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
272 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
273 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
274 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
275 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000276
Steve Narofff7469a32009-09-23 20:00:53 +0000277 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroffe19944c2009-10-15 22:23:48 +0000278 clang_disposeTranslationUnit(TU);
Steve Naroff50398192009-08-28 15:28:48 +0000279 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +0000280 }
Steve Naroff50398192009-08-28 15:28:48 +0000281}