blob: c7b0a5197c6c39dc41052f7370a1123f7a301fa3 [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001/*===-- clang-c/Index.h - Indexing Public C Interface -------------*- C -*-===*\
2|* *|
3|* The LLVM Compiler Infrastructure *|
4|* *|
5|* This file is distributed under the University of Illinois Open Source *|
6|* License. See LICENSE.TXT for details. *|
7|* *|
8|*===----------------------------------------------------------------------===*|
9|* *|
10|* This header provides a public inferface to a Clang library for extracting *|
11|* high-level symbol information from source files without exposing the full *|
12|* Clang C++ API. *|
13|* *|
14\*===----------------------------------------------------------------------===*/
15
16#ifndef CLANG_C_INDEX_H
17#define CLANG_C_INDEX_H
18
Steve Naroff88145032009-10-27 14:35:18 +000019#include <sys/stat.h>
Chandler Carruth3d315602009-12-17 09:27:29 +000020#include <time.h>
Steve Naroff88145032009-10-27 14:35:18 +000021
Ted Kremenekd2fa5662009-08-26 22:36:44 +000022#ifdef __cplusplus
23extern "C" {
24#endif
25
Steve Naroff88145032009-10-27 14:35:18 +000026/* MSVC DLL import/export. */
John Thompson2e06fc82009-10-27 13:42:56 +000027#ifdef _MSC_VER
28 #ifdef _CINDEX_LIB_
29 #define CINDEX_LINKAGE __declspec(dllexport)
30 #else
31 #define CINDEX_LINKAGE __declspec(dllimport)
32 #endif
33#else
34 #define CINDEX_LINKAGE
35#endif
36
Steve Naroff600866c2009-08-27 19:51:58 +000037/*
38 Clang indeX abstractions. The backing store for the following API's will be
Steve Naroffb7cd17c2009-09-01 17:13:31 +000039 clangs AST file (currently based on PCH). AST files are created as follows:
Steve Naroff600866c2009-08-27 19:51:58 +000040
Steve Naroffb7cd17c2009-09-01 17:13:31 +000041 "clang -emit-ast <sourcefile.langsuffix> -o <sourcefile.ast>".
Steve Naroff600866c2009-08-27 19:51:58 +000042
Steve Naroff600866c2009-08-27 19:51:58 +000043 Naming Conventions: To avoid namespace pollution, data types are prefixed
44 with "CX" and functions are prefixed with "clang_".
45*/
Steve Naroff50398192009-08-28 15:28:48 +000046typedef void *CXIndex; /* An indexing instance. */
Steve Naroff600866c2009-08-27 19:51:58 +000047
Steve Naroff50398192009-08-28 15:28:48 +000048typedef void *CXTranslationUnit; /* A translation unit instance. */
Steve Naroff600866c2009-08-27 19:51:58 +000049
Steve Naroff88145032009-10-27 14:35:18 +000050typedef void *CXFile; /* A source file */
Steve Naroff50398192009-08-28 15:28:48 +000051typedef void *CXDecl; /* A specific declaration within a translation unit. */
Steve Narofffb570422009-09-22 19:25:29 +000052typedef void *CXStmt; /* A specific statement within a function/method */
Steve Naroff600866c2009-08-27 19:51:58 +000053
Steve Naroffc857ea42009-09-02 13:28:54 +000054/* Cursors represent declarations, definitions, and references. */
Steve Naroff89922f82009-08-31 00:59:03 +000055enum CXCursorKind {
Steve Naroff89922f82009-08-31 00:59:03 +000056 /* Declarations */
57 CXCursor_FirstDecl = 1,
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000058 CXCursor_TypedefDecl = 1,
59 CXCursor_StructDecl = 2,
60 CXCursor_UnionDecl = 3,
61 CXCursor_ClassDecl = 4,
62 CXCursor_EnumDecl = 5,
63 CXCursor_FieldDecl = 6,
64 CXCursor_EnumConstantDecl = 7,
65 CXCursor_FunctionDecl = 8,
66 CXCursor_VarDecl = 9,
67 CXCursor_ParmDecl = 10,
68 CXCursor_ObjCInterfaceDecl = 11,
69 CXCursor_ObjCCategoryDecl = 12,
70 CXCursor_ObjCProtocolDecl = 13,
71 CXCursor_ObjCPropertyDecl = 14,
72 CXCursor_ObjCIvarDecl = 15,
73 CXCursor_ObjCInstanceMethodDecl = 16,
74 CXCursor_ObjCClassMethodDecl = 17,
75 CXCursor_LastDecl = 17,
Steve Naroff89922f82009-08-31 00:59:03 +000076
Steve Naroffc857ea42009-09-02 13:28:54 +000077 /* Definitions */
78 CXCursor_FirstDefn = 32,
79 CXCursor_FunctionDefn = 32,
80 CXCursor_ObjCClassDefn = 33,
81 CXCursor_ObjCCategoryDefn = 34,
82 CXCursor_ObjCInstanceMethodDefn = 35,
83 CXCursor_ObjCClassMethodDefn = 36,
84 CXCursor_LastDefn = 36,
85
Steve Naroff89922f82009-08-31 00:59:03 +000086 /* References */
Steve Narofffb570422009-09-22 19:25:29 +000087 CXCursor_FirstRef = 40, /* Decl references */
Steve Narofff334b4e2009-09-02 18:26:48 +000088 CXCursor_ObjCSuperClassRef = 40,
89 CXCursor_ObjCProtocolRef = 41,
Steve Narofffb570422009-09-22 19:25:29 +000090 CXCursor_ObjCClassRef = 42,
91
92 CXCursor_ObjCSelectorRef = 43, /* Expression references */
93 CXCursor_ObjCIvarRef = 44,
94 CXCursor_VarRef = 45,
95 CXCursor_FunctionRef = 46,
96 CXCursor_EnumConstantRef = 47,
97 CXCursor_MemberRef = 48,
98 CXCursor_LastRef = 48,
Steve Naroff77128dd2009-09-15 20:25:34 +000099
100 /* Error conditions */
101 CXCursor_FirstInvalid = 70,
102 CXCursor_InvalidFile = 70,
103 CXCursor_NoDeclFound = 71,
104 CXCursor_NotImplemented = 72,
105 CXCursor_LastInvalid = 72
Steve Naroff600866c2009-08-27 19:51:58 +0000106};
107
Douglas Gregor735df882009-12-02 09:21:34 +0000108/**
109 * \brief Provides the contents of a file that has not yet been saved to disk.
110 *
111 * Each CXUnsavedFile instance provides the name of a file on the
112 * system along with the current contents of that file that have not
113 * yet been saved to disk.
114 */
115struct CXUnsavedFile {
116 /**
117 * \brief The file whose contents have not yet been saved.
118 *
119 * This file must already exist in the file system.
120 */
121 const char *Filename;
122
123 /**
124 * \brief A null-terminated buffer containing the unsaved contents
125 * of this file.
126 */
127 const char *Contents;
128
129 /**
130 * \brief The length of the unsaved contents of this buffer, not
131 * counting the NULL at the end of the buffer.
132 */
133 unsigned long Length;
134};
135
Steve Naroff89922f82009-08-31 00:59:03 +0000136/* A cursor into the CXTranslationUnit. */
Steve Narofffb570422009-09-22 19:25:29 +0000137
Steve Naroff89922f82009-08-31 00:59:03 +0000138typedef struct {
139 enum CXCursorKind kind;
Douglas Gregor283cae32010-01-15 21:56:13 +0000140 void *data[3];
Steve Naroff89922f82009-08-31 00:59:03 +0000141} CXCursor;
142
Steve Naroff50398192009-08-28 15:28:48 +0000143/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Ted Kremenek31723832010-01-11 23:56:39 +0000144typedef struct {
145 CXIndex index;
146 void *data;
147} CXEntity;
Steve Naroff600866c2009-08-27 19:51:58 +0000148
Steve Naroffef0cef62009-11-09 17:45:52 +0000149/**
150 * For functions returning a string that might or might not need
151 * to be internally allocated and freed.
152 * Use clang_getCString to access the C string value.
153 * Use clang_disposeString to free the value.
154 * Treat it as an opaque type.
155 */
156typedef struct {
157 const char *Spelling;
158 /* A 1 value indicates the clang_ indexing API needed to allocate the string
159 (and it must be freed by clang_disposeString()). */
160 int MustFreeString;
161} CXString;
162
163/* Get C string pointer from a CXString. */
164CINDEX_LINKAGE const char *clang_getCString(CXString string);
165
166/* Free CXString. */
167CINDEX_LINKAGE void clang_disposeString(CXString string);
168
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000169/**
170 * \brief clang_createIndex() provides a shared context for creating
171 * translation units. It provides two options:
172 *
173 * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local"
174 * declarations (when loading any new translation units). A "local" declaration
175 * is one that belongs in the translation unit itself and not in a precompiled
176 * header that was used by the translation unit. If zero, all declarations
177 * will be enumerated.
178 *
179 * - displayDiagnostics: when non-zero, diagnostics will be output. If zero,
180 * diagnostics will be ignored.
Steve Naroffb4ece632009-10-20 16:36:34 +0000181 *
182 * Here is an example:
183 *
184 * // excludeDeclsFromPCH = 1, displayDiagnostics = 1
185 * Idx = clang_createIndex(1, 1);
186 *
187 * // IndexTest.pch was produced with the following command:
188 * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
189 * TU = clang_createTranslationUnit(Idx, "IndexTest.pch");
190 *
191 * // This will load all the symbols from 'IndexTest.pch'
192 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
193 * clang_disposeTranslationUnit(TU);
194 *
195 * // This will load all the symbols from 'IndexTest.c', excluding symbols
196 * // from 'IndexTest.pch'.
197 * char *args[] = { "-Xclang", "-include-pch=IndexTest.pch", 0 };
198 * TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args);
199 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
200 * clang_disposeTranslationUnit(TU);
201 *
202 * This process of creating the 'pch', loading it separately, and using it (via
203 * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks
204 * (which gives the indexer the same performance benefit as the compiler).
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000205 */
John Thompson2e06fc82009-10-27 13:42:56 +0000206CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000207 int displayDiagnostics);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000208CINDEX_LINKAGE void clang_disposeIndex(CXIndex index);
209CINDEX_LINKAGE CXString
210clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
211
212/*
213 * \brief Request that AST's be generated external for API calls which parse
214 * source code on the fly, e.g. \see createTranslationUnitFromSourceFile.
215 *
216 * Note: This is for debugging purposes only, and may be removed at a later
217 * date.
218 *
219 * \param index - The index to update.
220 * \param value - The new flag value.
221 */
222CINDEX_LINKAGE void clang_setUseExternalASTGeneration(CXIndex index,
223 int value);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000224
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000225/*
226 * \brief Create a translation unit from an AST file (-emit-ast).
227 */
John Thompson2e06fc82009-10-27 13:42:56 +0000228CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000229 CXIndex, const char *ast_filename
Steve Naroff600866c2009-08-27 19:51:58 +0000230);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000231
Ted Kremenek13745982009-10-19 22:15:09 +0000232/**
233 * \brief Destroy the specified CXTranslationUnit object.
234 */
John Thompson2e06fc82009-10-27 13:42:56 +0000235CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
Ted Kremenek13745982009-10-19 22:15:09 +0000236
237/**
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000238 * \brief Return the CXTranslationUnit for a given source file and the provided
239 * command line arguments one would pass to the compiler.
240 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000241 * Note: The 'source_filename' argument is optional. If the caller provides a
242 * NULL pointer, the name of the source file is expected to reside in the
243 * specified command line arguments.
Ted Kremenek139ba862009-10-22 00:03:57 +0000244 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000245 * Note: When encountered in 'clang_command_line_args', the following options
246 * are ignored:
Ted Kremenek139ba862009-10-22 00:03:57 +0000247 *
248 * '-c'
249 * '-emit-ast'
250 * '-fsyntax-only'
251 * '-o <output file>' (both '-o' and '<output file>' are ignored)
252 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000253 *
254 * \param source_filename - The name of the source file to load, or NULL if the
255 * source file is included in clang_command_line_args.
Ted Kremenek13745982009-10-19 22:15:09 +0000256 */
John Thompson2e06fc82009-10-27 13:42:56 +0000257CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile(
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000258 CXIndex CIdx,
259 const char *source_filename,
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000260 int num_clang_command_line_args,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000261 const char **clang_command_line_args
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000262);
Steve Naroff600866c2009-08-27 19:51:58 +0000263
264/*
265 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
266 within a translation unit, issuing a 'callback' for each one.
267
268 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
269 if (clang_getCursorKind(C) == Cursor_Declaration) {
270 CXDecl D = clang_getCursorDecl(C);
271 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
272 printf("@interface %s in file %s on line %d column %d\n",
273 clang_getDeclSpelling(D), clang_getCursorSource(C),
274 clang_getCursorLine(C), clang_getCursorColumn(C));
275 }
276 }
277 static void usage {
278 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000279 }
Steve Naroff600866c2009-08-27 19:51:58 +0000280*/
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000281typedef void *CXClientData;
282typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
283 CXClientData);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000284CINDEX_LINKAGE void clang_loadTranslationUnit(CXTranslationUnit,
285 CXTranslationUnitIterator,
286 CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000287
288/*
289 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
290 'callback' for each declaration/reference within the respective declaration.
291
292 For interface declarations, this will index the super class, protocols,
293 ivars, methods, etc. For structure declarations, this will index the fields.
294 For functions, this will index the parameters (and body, for function
295 definitions), local declarations/references.
296
297 void getInterfaceDetails(CXDecl X, CXCursor C) {
298 switch (clang_getCursorKind(C)) {
299 case Cursor_ObjC_ClassRef:
300 CXDecl SuperClass = clang_getCursorDecl(C);
301 case Cursor_ObjC_ProtocolRef:
302 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
303 case Cursor_Declaration:
304 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
305 }
306 }
307 static void usage() {
308 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
309 clang_loadDeclaration(D, getInterfaceDetails);
310 }
311 }
312*/
Steve Naroffc857ea42009-09-02 13:28:54 +0000313typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
Steve Naroff89922f82009-08-31 00:59:03 +0000314
John Thompson2e06fc82009-10-27 13:42:56 +0000315CINDEX_LINKAGE void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000316
Steve Naroff50398192009-08-28 15:28:48 +0000317/*
Steve Naroff88145032009-10-27 14:35:18 +0000318 * CXFile Operations.
319 */
Douglas Gregor08b0e8d2009-10-31 15:48:08 +0000320CINDEX_LINKAGE const char *clang_getFileName(CXFile SFile);
321CINDEX_LINKAGE time_t clang_getFileTime(CXFile SFile);
Steve Naroff88145032009-10-27 14:35:18 +0000322
323/*
Steve Naroff50398192009-08-28 15:28:48 +0000324 * CXEntity Operations.
325 */
Ted Kremenek31723832010-01-11 23:56:39 +0000326
327/* clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
328 * in a specified translation unit. */
329CINDEX_LINKAGE CXDecl clang_getDeclaration(CXEntity, CXTranslationUnit);
330
Steve Naroff50398192009-08-28 15:28:48 +0000331/*
332 * CXDecl Operations.
333 */
John Thompson2e06fc82009-10-27 13:42:56 +0000334CINDEX_LINKAGE CXCursor clang_getCursorFromDecl(CXDecl);
Ted Kremenek31723832010-01-11 23:56:39 +0000335CINDEX_LINKAGE CXEntity clang_getEntityFromDecl(CXIndex, CXDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000336CINDEX_LINKAGE CXString clang_getDeclSpelling(CXDecl);
John Thompson2e06fc82009-10-27 13:42:56 +0000337CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl);
338CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl);
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000339CINDEX_LINKAGE const char *clang_getDeclSource(CXDecl); /* deprecate */
Steve Naroff88145032009-10-27 14:35:18 +0000340CINDEX_LINKAGE CXFile clang_getDeclSourceFile(CXDecl);
Steve Naroff699a07d2009-09-25 21:32:34 +0000341
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000342typedef struct CXSourceLineColumn {
343 unsigned line;
344 unsigned column;
345} CXSourceLineColumn;
346
347typedef struct CXDeclExtent {
348 CXSourceLineColumn begin;
349 CXSourceLineColumn end;
350} CXSourceExtent;
351
Daniel Dunbar3aacd532010-01-06 06:51:48 +0000352/* clang_getDeclExtent() returns the physical extent of a declaration. The
353 * beginning line/column pair points to the start of the first token in the
Ted Kremenekd8210652010-01-06 23:43:31 +0000354 * declaration, and the ending line/column pair points to the last character in
355 * the last token of the declaration.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000356 */
Daniel Dunbar3aacd532010-01-06 06:51:48 +0000357CINDEX_LINKAGE CXSourceExtent clang_getDeclExtent(CXDecl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000358
Steve Naroff50398192009-08-28 15:28:48 +0000359/*
360 * CXCursor Operations.
361 */
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000362/**
363 Usage: clang_getCursor() will translate a source/line/column position
364 into an AST cursor (to derive semantic information from the source code).
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000365 */
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000366CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit,
367 const char *source_name,
368 unsigned line, unsigned column);
Ted Kremenek73885552009-11-17 19:28:59 +0000369
370CINDEX_LINKAGE CXCursor clang_getNullCursor(void);
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000371
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000372/* clang_getCursorUSR() returns the USR (if any) associated with entity referred to by the
373 * provided CXCursor object. */
374CINDEX_LINKAGE CXString clang_getCursorUSR(CXCursor);
375
John Thompson2e06fc82009-10-27 13:42:56 +0000376CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor);
377CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind);
378CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind);
379CINDEX_LINKAGE unsigned clang_isDefinition(enum CXCursorKind);
380CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
Steve Naroff600866c2009-08-27 19:51:58 +0000381
Ted Kremenek73885552009-11-17 19:28:59 +0000382CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor);
383
John Thompson2e06fc82009-10-27 13:42:56 +0000384CINDEX_LINKAGE unsigned clang_getCursorLine(CXCursor);
385CINDEX_LINKAGE unsigned clang_getCursorColumn(CXCursor);
Steve Naroffef0cef62009-11-09 17:45:52 +0000386CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);
Steve Naroff88145032009-10-27 14:35:18 +0000387CINDEX_LINKAGE const char *clang_getCursorSource(CXCursor); /* deprecate */
388CINDEX_LINKAGE CXFile clang_getCursorSourceFile(CXCursor);
Steve Narofff334b4e2009-09-02 18:26:48 +0000389
Steve Naroff4ade6d62009-09-23 17:52:52 +0000390/* for debug/testing */
John Thompson2e06fc82009-10-27 13:42:56 +0000391CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind);
392CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000393 const char **startBuf,
394 const char **endBuf,
395 unsigned *startLine,
396 unsigned *startColumn,
397 unsigned *endLine,
398 unsigned *endColumn);
Steve Naroff600866c2009-08-27 19:51:58 +0000399
Steve Naroff50398192009-08-28 15:28:48 +0000400/*
Chris Lattner459789b2009-09-16 20:18:54 +0000401 * If CXCursorKind == Cursor_Reference, then this will return the referenced
402 * declaration.
Steve Naroff50398192009-08-28 15:28:48 +0000403 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
404 */
John Thompson2e06fc82009-10-27 13:42:56 +0000405CINDEX_LINKAGE CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000406
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000407/**
408 * \brief A semantic string that describes a code-completion result.
409 *
410 * A semantic string that describes the formatting of a code-completion
411 * result as a single "template" of text that should be inserted into the
412 * source buffer when a particular code-completion result is selected.
413 * Each semantic string is made up of some number of "chunks", each of which
414 * contains some text along with a description of what that text means, e.g.,
415 * the name of the entity being referenced, whether the text chunk is part of
416 * the template, or whether it is a "placeholder" that the user should replace
417 * with actual code,of a specific kind. See \c CXCompletionChunkKind for a
418 * description of the different kinds of chunks.
419 */
420typedef void *CXCompletionString;
421
422/**
423 * \brief A single result of code completion.
424 */
425typedef struct {
426 /**
427 * \brief The kind of entity that this completion refers to.
428 *
429 * The cursor kind will be a macro, keyword, or a declaration (one of the
430 * *Decl cursor kinds), describing the entity that the completion is
431 * referring to.
432 *
433 * \todo In the future, we would like to provide a full cursor, to allow
434 * the client to extract additional information from declaration.
435 */
436 enum CXCursorKind CursorKind;
437
438 /**
439 * \brief The code-completion string that describes how to insert this
440 * code-completion result into the editing buffer.
441 */
442 CXCompletionString CompletionString;
443} CXCompletionResult;
444
445/**
446 * \brief Describes a single piece of text within a code-completion string.
447 *
448 * Each "chunk" within a code-completion string (\c CXCompletionString) is
449 * either a piece of text with a specific "kind" that describes how that text
450 * should be interpreted by the client or is another completion string.
451 */
452enum CXCompletionChunkKind {
453 /**
454 * \brief A code-completion string that describes "optional" text that
455 * could be a part of the template (but is not required).
456 *
457 * The Optional chunk is the only kind of chunk that has a code-completion
458 * string for its representation, which is accessible via
459 * \c clang_getCompletionChunkCompletionString(). The code-completion string
460 * describes an additional part of the template that is completely optional.
461 * For example, optional chunks can be used to describe the placeholders for
462 * arguments that match up with defaulted function parameters, e.g. given:
463 *
464 * \code
465 * void f(int x, float y = 3.14, double z = 2.71828);
466 * \endcode
467 *
468 * The code-completion string for this function would contain:
469 * - a TypedText chunk for "f".
470 * - a LeftParen chunk for "(".
471 * - a Placeholder chunk for "int x"
472 * - an Optional chunk containing the remaining defaulted arguments, e.g.,
473 * - a Comma chunk for ","
474 * - a Placeholder chunk for "float x"
475 * - an Optional chunk containing the last defaulted argument:
476 * - a Comma chunk for ","
477 * - a Placeholder chunk for "double z"
478 * - a RightParen chunk for ")"
479 *
480 * There are many ways two handle Optional chunks. Two simple approaches are:
481 * - Completely ignore optional chunks, in which case the template for the
482 * function "f" would only include the first parameter ("int x").
483 * - Fully expand all optional chunks, in which case the template for the
484 * function "f" would have all of the parameters.
485 */
486 CXCompletionChunk_Optional,
487 /**
488 * \brief Text that a user would be expected to type to get this
489 * code-completion result.
490 *
491 * There will be exactly one "typed text" chunk in a semantic string, which
492 * will typically provide the spelling of a keyword or the name of a
493 * declaration that could be used at the current code point. Clients are
494 * expected to filter the code-completion results based on the text in this
495 * chunk.
496 */
497 CXCompletionChunk_TypedText,
498 /**
499 * \brief Text that should be inserted as part of a code-completion result.
500 *
501 * A "text" chunk represents text that is part of the template to be
502 * inserted into user code should this particular code-completion result
503 * be selected.
504 */
505 CXCompletionChunk_Text,
506 /**
507 * \brief Placeholder text that should be replaced by the user.
508 *
509 * A "placeholder" chunk marks a place where the user should insert text
510 * into the code-completion template. For example, placeholders might mark
511 * the function parameters for a function declaration, to indicate that the
512 * user should provide arguments for each of those parameters. The actual
513 * text in a placeholder is a suggestion for the text to display before
514 * the user replaces the placeholder with real code.
515 */
516 CXCompletionChunk_Placeholder,
517 /**
518 * \brief Informative text that should be displayed but never inserted as
519 * part of the template.
520 *
521 * An "informative" chunk contains annotations that can be displayed to
522 * help the user decide whether a particular code-completion result is the
523 * right option, but which is not part of the actual template to be inserted
524 * by code completion.
525 */
526 CXCompletionChunk_Informative,
527 /**
528 * \brief Text that describes the current parameter when code-completion is
529 * referring to function call, message send, or template specialization.
530 *
531 * A "current parameter" chunk occurs when code-completion is providing
532 * information about a parameter corresponding to the argument at the
533 * code-completion point. For example, given a function
534 *
535 * \code
536 * int add(int x, int y);
537 * \endcode
538 *
539 * and the source code \c add(, where the code-completion point is after the
540 * "(", the code-completion string will contain a "current parameter" chunk
541 * for "int x", indicating that the current argument will initialize that
542 * parameter. After typing further, to \c add(17, (where the code-completion
543 * point is after the ","), the code-completion string will contain a
544 * "current paremeter" chunk to "int y".
545 */
546 CXCompletionChunk_CurrentParameter,
547 /**
548 * \brief A left parenthesis ('('), used to initiate a function call or
549 * signal the beginning of a function parameter list.
550 */
551 CXCompletionChunk_LeftParen,
552 /**
553 * \brief A right parenthesis (')'), used to finish a function call or
554 * signal the end of a function parameter list.
555 */
556 CXCompletionChunk_RightParen,
557 /**
558 * \brief A left bracket ('[').
559 */
560 CXCompletionChunk_LeftBracket,
561 /**
562 * \brief A right bracket (']').
563 */
564 CXCompletionChunk_RightBracket,
565 /**
566 * \brief A left brace ('{').
567 */
568 CXCompletionChunk_LeftBrace,
569 /**
570 * \brief A right brace ('}').
571 */
572 CXCompletionChunk_RightBrace,
573 /**
574 * \brief A left angle bracket ('<').
575 */
576 CXCompletionChunk_LeftAngle,
577 /**
578 * \brief A right angle bracket ('>').
579 */
580 CXCompletionChunk_RightAngle,
581 /**
582 * \brief A comma separator (',').
583 */
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000584 CXCompletionChunk_Comma,
585 /**
586 * \brief Text that specifies the result type of a given result.
587 *
588 * This special kind of informative chunk is not meant to be inserted into
589 * the text buffer. Rather, it is meant to illustrate the type that an
590 * expression using the given completion string would have.
591 */
Douglas Gregor01dfea02010-01-10 23:08:15 +0000592 CXCompletionChunk_ResultType,
593 /**
594 * \brief A colon (':').
595 */
596 CXCompletionChunk_Colon,
597 /**
598 * \brief A semicolon (';').
599 */
600 CXCompletionChunk_SemiColon,
601 /**
602 * \brief An '=' sign.
603 */
604 CXCompletionChunk_Equal,
605 /**
606 * Horizontal space (' ').
607 */
608 CXCompletionChunk_HorizontalSpace,
609 /**
610 * Vertical space ('\n'), after which it is generally a good idea to
611 * perform indentation.
612 */
613 CXCompletionChunk_VerticalSpace
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000614};
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000615
616/**
617 * \brief Determine the kind of a particular chunk within a completion string.
618 *
619 * \param completion_string the completion string to query.
620 *
621 * \param chunk_number the 0-based index of the chunk in the completion string.
622 *
623 * \returns the kind of the chunk at the index \c chunk_number.
624 */
625CINDEX_LINKAGE enum CXCompletionChunkKind
626clang_getCompletionChunkKind(CXCompletionString completion_string,
627 unsigned chunk_number);
628
629/**
630 * \brief Retrieve the text associated with a particular chunk within a
631 * completion string.
632 *
633 * \param completion_string the completion string to query.
634 *
635 * \param chunk_number the 0-based index of the chunk in the completion string.
636 *
637 * \returns the text associated with the chunk at index \c chunk_number.
638 */
639CINDEX_LINKAGE const char *
640clang_getCompletionChunkText(CXCompletionString completion_string,
641 unsigned chunk_number);
642
643/**
644 * \brief Retrieve the completion string associated with a particular chunk
645 * within a completion string.
646 *
647 * \param completion_string the completion string to query.
648 *
649 * \param chunk_number the 0-based index of the chunk in the completion string.
650 *
651 * \returns the completion string associated with the chunk at index
652 * \c chunk_number, or NULL if that chunk is not represented by a completion
653 * string.
654 */
655CINDEX_LINKAGE CXCompletionString
656clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
657 unsigned chunk_number);
658
659/**
660 * \brief Retrieve the number of chunks in the given code-completion string.
661 */
662CINDEX_LINKAGE unsigned
663clang_getNumCompletionChunks(CXCompletionString completion_string);
664
665/**
Douglas Gregorec6762c2009-12-18 16:20:58 +0000666 * \brief Contains the results of code-completion.
667 *
668 * This data structure contains the results of code completion, as
669 * produced by \c clang_codeComplete. Its contents must be freed by
670 * \c clang_disposeCodeCompleteResults.
671 */
672typedef struct {
673 /**
674 * \brief The code-completion results.
675 */
676 CXCompletionResult *Results;
677
678 /**
679 * \brief The number of code-completion results stored in the
680 * \c Results array.
681 */
682 unsigned NumResults;
683} CXCodeCompleteResults;
684
685/**
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000686 * \brief Perform code completion at a given location in a source file.
687 *
688 * This function performs code completion at a particular file, line, and
689 * column within source code, providing results that suggest potential
690 * code snippets based on the context of the completion. The basic model
691 * for code completion is that Clang will parse a complete source file,
692 * performing syntax checking up to the location where code-completion has
693 * been requested. At that point, a special code-completion token is passed
694 * to the parser, which recognizes this token and determines, based on the
695 * current location in the C/Objective-C/C++ grammar and the state of
696 * semantic analysis, what completions to provide. These completions are
Douglas Gregorec6762c2009-12-18 16:20:58 +0000697 * returned via a new \c CXCodeCompleteResults structure.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000698 *
699 * Code completion itself is meant to be triggered by the client when the
700 * user types punctuation characters or whitespace, at which point the
701 * code-completion location will coincide with the cursor. For example, if \c p
702 * is a pointer, code-completion might be triggered after the "-" and then
703 * after the ">" in \c p->. When the code-completion location is afer the ">",
704 * the completion results will provide, e.g., the members of the struct that
705 * "p" points to. The client is responsible for placing the cursor at the
706 * beginning of the token currently being typed, then filtering the results
707 * based on the contents of the token. For example, when code-completing for
708 * the expression \c p->get, the client should provide the location just after
709 * the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the
710 * client can filter the results based on the current token text ("get"), only
711 * showing those results that start with "get". The intent of this interface
Douglas Gregorec6762c2009-12-18 16:20:58 +0000712 * is to separate the relatively high-latency acquisition of code-completion
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000713 * results from the filtering of results on a per-character basis, which must
714 * have a lower latency.
715 *
716 * \param CIdx the \c CXIndex instance that will be used to perform code
717 * completion.
718 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000719 * \param source_filename the name of the source file that should be parsed to
720 * perform code-completion. This source file must be the same as or include the
721 * filename described by \p complete_filename, or no code-completion results
722 * will be produced. NOTE: One can also specify NULL for this argument if the
723 * source file is included in command_line_args.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000724 *
725 * \param num_command_line_args the number of command-line arguments stored in
726 * \p command_line_args.
727 *
728 * \param command_line_args the command-line arguments to pass to the Clang
729 * compiler to build the given source file. This should include all of the
730 * necessary include paths, language-dialect switches, precompiled header
731 * includes, etc., but should not include any information specific to
732 * code completion.
733 *
Douglas Gregor735df882009-12-02 09:21:34 +0000734 * \param num_unsaved_files the number of unsaved file entries in \p
735 * unsaved_files.
736 *
737 * \param unsaved_files the files that have not yet been saved to disk
738 * but may be required for code completion, including the contents of
739 * those files.
740 *
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000741 * \param complete_filename the name of the source file where code completion
742 * should be performed. In many cases, this name will be the same as the
743 * source filename. However, the completion filename may also be a file
744 * included by the source file, which is required when producing
745 * code-completion results for a header.
746 *
747 * \param complete_line the line at which code-completion should occur.
748 *
749 * \param complete_column the column at which code-completion should occur.
750 * Note that the column should point just after the syntactic construct that
751 * initiated code completion, and not in the middle of a lexical token.
752 *
Douglas Gregorec6762c2009-12-18 16:20:58 +0000753 * \returns if successful, a new CXCodeCompleteResults structure
754 * containing code-completion results, which should eventually be
755 * freed with \c clang_disposeCodeCompleteResults(). If code
756 * completion fails, returns NULL.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000757 */
Douglas Gregorec6762c2009-12-18 16:20:58 +0000758CINDEX_LINKAGE
759CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
760 const char *source_filename,
761 int num_command_line_args,
762 const char **command_line_args,
763 unsigned num_unsaved_files,
764 struct CXUnsavedFile *unsaved_files,
765 const char *complete_filename,
766 unsigned complete_line,
767 unsigned complete_column);
768
769/**
770 * \brief Free the given set of code-completion results.
771 */
772CINDEX_LINKAGE
773void clang_disposeCodeCompleteResults(CXCodeCompleteResults *Results);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000774
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000775#ifdef __cplusplus
776}
777#endif
778#endif
779