blob: 62a6bf76741ac74809d291e5534680680844d06c [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,
Douglas Gregor30122132010-01-19 22:07:56 +000058 /** \brief A typedef */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000059 CXCursor_TypedefDecl = 1,
Douglas Gregor30122132010-01-19 22:07:56 +000060 /** \brief A C or C++ struct. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000061 CXCursor_StructDecl = 2,
Douglas Gregor30122132010-01-19 22:07:56 +000062 /** \brief A C or C++ union. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000063 CXCursor_UnionDecl = 3,
Douglas Gregor30122132010-01-19 22:07:56 +000064 /** \brief A C++ class. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000065 CXCursor_ClassDecl = 4,
Douglas Gregor30122132010-01-19 22:07:56 +000066 /** \brief An enumeration. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000067 CXCursor_EnumDecl = 5,
Douglas Gregor30122132010-01-19 22:07:56 +000068 /**
69 * \brief A field (in C) or non-static data member (in C++) in a
70 * struct, union, or C++ class.
71 */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000072 CXCursor_FieldDecl = 6,
Douglas Gregor30122132010-01-19 22:07:56 +000073 /** \brief An enumerator constant. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000074 CXCursor_EnumConstantDecl = 7,
Douglas Gregor30122132010-01-19 22:07:56 +000075 /** \brief A function. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000076 CXCursor_FunctionDecl = 8,
Douglas Gregor30122132010-01-19 22:07:56 +000077 /** \brief A variable. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000078 CXCursor_VarDecl = 9,
Douglas Gregor30122132010-01-19 22:07:56 +000079 /** \brief A function or method parameter. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000080 CXCursor_ParmDecl = 10,
Douglas Gregor30122132010-01-19 22:07:56 +000081 /** \brief An Objective-C @interface. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000082 CXCursor_ObjCInterfaceDecl = 11,
Douglas Gregor30122132010-01-19 22:07:56 +000083 /** \brief An Objective-C @interface for a category. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000084 CXCursor_ObjCCategoryDecl = 12,
Douglas Gregor30122132010-01-19 22:07:56 +000085 /** \brief An Objective-C @protocol declaration. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000086 CXCursor_ObjCProtocolDecl = 13,
Douglas Gregor30122132010-01-19 22:07:56 +000087 /** \brief An Objective-C @property declaration. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000088 CXCursor_ObjCPropertyDecl = 14,
Douglas Gregor30122132010-01-19 22:07:56 +000089 /** \brief An Objective-C instance variable. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000090 CXCursor_ObjCIvarDecl = 15,
Douglas Gregor30122132010-01-19 22:07:56 +000091 /** \brief An Objective-C instance method. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000092 CXCursor_ObjCInstanceMethodDecl = 16,
Douglas Gregor30122132010-01-19 22:07:56 +000093 /** \brief An Objective-C class method. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000094 CXCursor_ObjCClassMethodDecl = 17,
Douglas Gregor30122132010-01-19 22:07:56 +000095 /** \brief An Objective-C @implementation. */
Douglas Gregorb6998662010-01-19 19:34:47 +000096 CXCursor_ObjCImplementationDecl = 18,
Douglas Gregor30122132010-01-19 22:07:56 +000097 /** \brief An Objective-C @implementation for a category. */
Douglas Gregorb6998662010-01-19 19:34:47 +000098 CXCursor_ObjCCategoryImplDecl = 19,
Douglas Gregor30122132010-01-19 22:07:56 +000099 /**
100 * \brief A declaration whose specific kind is not exposed via this
101 * interface.
102 *
103 * Unexposed declarations have the same operations as any other kind
104 * of declaration; one can extract their location information,
105 * spelling, find their definitions, etc. However, the specific kind
106 * of the declaration is not reported.
107 */
108 CXCursor_UnexposedDecl = 20,
109 CXCursor_LastDecl = 20,
Steve Naroff89922f82009-08-31 00:59:03 +0000110
111 /* References */
Steve Narofffb570422009-09-22 19:25:29 +0000112 CXCursor_FirstRef = 40, /* Decl references */
Steve Narofff334b4e2009-09-02 18:26:48 +0000113 CXCursor_ObjCSuperClassRef = 40,
114 CXCursor_ObjCProtocolRef = 41,
Steve Narofffb570422009-09-22 19:25:29 +0000115 CXCursor_ObjCClassRef = 42,
116
117 CXCursor_ObjCSelectorRef = 43, /* Expression references */
Douglas Gregore8c70432010-01-19 22:15:34 +0000118 CXCursor_VarRef = 44,
119 CXCursor_FunctionRef = 45,
120 CXCursor_EnumConstantRef = 46,
121 CXCursor_LastRef = 46,
Steve Naroff77128dd2009-09-15 20:25:34 +0000122
123 /* Error conditions */
124 CXCursor_FirstInvalid = 70,
125 CXCursor_InvalidFile = 70,
126 CXCursor_NoDeclFound = 71,
127 CXCursor_NotImplemented = 72,
128 CXCursor_LastInvalid = 72
Steve Naroff600866c2009-08-27 19:51:58 +0000129};
130
Douglas Gregor735df882009-12-02 09:21:34 +0000131/**
132 * \brief Provides the contents of a file that has not yet been saved to disk.
133 *
134 * Each CXUnsavedFile instance provides the name of a file on the
135 * system along with the current contents of that file that have not
136 * yet been saved to disk.
137 */
138struct CXUnsavedFile {
139 /**
140 * \brief The file whose contents have not yet been saved.
141 *
142 * This file must already exist in the file system.
143 */
144 const char *Filename;
145
146 /**
147 * \brief A null-terminated buffer containing the unsaved contents
148 * of this file.
149 */
150 const char *Contents;
151
152 /**
153 * \brief The length of the unsaved contents of this buffer, not
154 * counting the NULL at the end of the buffer.
155 */
156 unsigned long Length;
157};
158
Steve Naroff89922f82009-08-31 00:59:03 +0000159/* A cursor into the CXTranslationUnit. */
Steve Narofffb570422009-09-22 19:25:29 +0000160
Steve Naroff89922f82009-08-31 00:59:03 +0000161typedef struct {
162 enum CXCursorKind kind;
Douglas Gregor283cae32010-01-15 21:56:13 +0000163 void *data[3];
Steve Naroff89922f82009-08-31 00:59:03 +0000164} CXCursor;
165
Steve Naroff50398192009-08-28 15:28:48 +0000166/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Ted Kremenek31723832010-01-11 23:56:39 +0000167typedef struct {
168 CXIndex index;
169 void *data;
170} CXEntity;
Steve Naroff600866c2009-08-27 19:51:58 +0000171
Steve Naroffef0cef62009-11-09 17:45:52 +0000172/**
173 * For functions returning a string that might or might not need
174 * to be internally allocated and freed.
175 * Use clang_getCString to access the C string value.
176 * Use clang_disposeString to free the value.
177 * Treat it as an opaque type.
178 */
179typedef struct {
180 const char *Spelling;
181 /* A 1 value indicates the clang_ indexing API needed to allocate the string
182 (and it must be freed by clang_disposeString()). */
183 int MustFreeString;
184} CXString;
185
186/* Get C string pointer from a CXString. */
187CINDEX_LINKAGE const char *clang_getCString(CXString string);
188
189/* Free CXString. */
190CINDEX_LINKAGE void clang_disposeString(CXString string);
191
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000192/**
193 * \brief clang_createIndex() provides a shared context for creating
194 * translation units. It provides two options:
195 *
196 * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local"
197 * declarations (when loading any new translation units). A "local" declaration
198 * is one that belongs in the translation unit itself and not in a precompiled
199 * header that was used by the translation unit. If zero, all declarations
200 * will be enumerated.
201 *
202 * - displayDiagnostics: when non-zero, diagnostics will be output. If zero,
203 * diagnostics will be ignored.
Steve Naroffb4ece632009-10-20 16:36:34 +0000204 *
205 * Here is an example:
206 *
207 * // excludeDeclsFromPCH = 1, displayDiagnostics = 1
208 * Idx = clang_createIndex(1, 1);
209 *
210 * // IndexTest.pch was produced with the following command:
211 * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
212 * TU = clang_createTranslationUnit(Idx, "IndexTest.pch");
213 *
214 * // This will load all the symbols from 'IndexTest.pch'
215 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
216 * clang_disposeTranslationUnit(TU);
217 *
218 * // This will load all the symbols from 'IndexTest.c', excluding symbols
219 * // from 'IndexTest.pch'.
220 * char *args[] = { "-Xclang", "-include-pch=IndexTest.pch", 0 };
221 * TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args);
222 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
223 * clang_disposeTranslationUnit(TU);
224 *
225 * This process of creating the 'pch', loading it separately, and using it (via
226 * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks
227 * (which gives the indexer the same performance benefit as the compiler).
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000228 */
John Thompson2e06fc82009-10-27 13:42:56 +0000229CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000230 int displayDiagnostics);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000231CINDEX_LINKAGE void clang_disposeIndex(CXIndex index);
232CINDEX_LINKAGE CXString
233clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
234
235/*
236 * \brief Request that AST's be generated external for API calls which parse
237 * source code on the fly, e.g. \see createTranslationUnitFromSourceFile.
238 *
239 * Note: This is for debugging purposes only, and may be removed at a later
240 * date.
241 *
242 * \param index - The index to update.
243 * \param value - The new flag value.
244 */
245CINDEX_LINKAGE void clang_setUseExternalASTGeneration(CXIndex index,
246 int value);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000247
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000248/*
249 * \brief Create a translation unit from an AST file (-emit-ast).
250 */
John Thompson2e06fc82009-10-27 13:42:56 +0000251CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000252 CXIndex, const char *ast_filename
Steve Naroff600866c2009-08-27 19:51:58 +0000253);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000254
Ted Kremenek13745982009-10-19 22:15:09 +0000255/**
256 * \brief Destroy the specified CXTranslationUnit object.
257 */
John Thompson2e06fc82009-10-27 13:42:56 +0000258CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
Ted Kremenek13745982009-10-19 22:15:09 +0000259
260/**
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000261 * \brief Return the CXTranslationUnit for a given source file and the provided
262 * command line arguments one would pass to the compiler.
263 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000264 * Note: The 'source_filename' argument is optional. If the caller provides a
265 * NULL pointer, the name of the source file is expected to reside in the
266 * specified command line arguments.
Ted Kremenek139ba862009-10-22 00:03:57 +0000267 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000268 * Note: When encountered in 'clang_command_line_args', the following options
269 * are ignored:
Ted Kremenek139ba862009-10-22 00:03:57 +0000270 *
271 * '-c'
272 * '-emit-ast'
273 * '-fsyntax-only'
274 * '-o <output file>' (both '-o' and '<output file>' are ignored)
275 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000276 *
277 * \param source_filename - The name of the source file to load, or NULL if the
278 * source file is included in clang_command_line_args.
Ted Kremenek13745982009-10-19 22:15:09 +0000279 */
John Thompson2e06fc82009-10-27 13:42:56 +0000280CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile(
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000281 CXIndex CIdx,
282 const char *source_filename,
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000283 int num_clang_command_line_args,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000284 const char **clang_command_line_args
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000285);
Steve Naroff600866c2009-08-27 19:51:58 +0000286
287/*
288 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
289 within a translation unit, issuing a 'callback' for each one.
290
291 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
292 if (clang_getCursorKind(C) == Cursor_Declaration) {
293 CXDecl D = clang_getCursorDecl(C);
294 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
295 printf("@interface %s in file %s on line %d column %d\n",
296 clang_getDeclSpelling(D), clang_getCursorSource(C),
297 clang_getCursorLine(C), clang_getCursorColumn(C));
298 }
299 }
300 static void usage {
301 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000302 }
Steve Naroff600866c2009-08-27 19:51:58 +0000303*/
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000304typedef void *CXClientData;
305typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
306 CXClientData);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000307CINDEX_LINKAGE void clang_loadTranslationUnit(CXTranslationUnit,
308 CXTranslationUnitIterator,
309 CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000310
311/*
312 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
313 'callback' for each declaration/reference within the respective declaration.
314
315 For interface declarations, this will index the super class, protocols,
316 ivars, methods, etc. For structure declarations, this will index the fields.
317 For functions, this will index the parameters (and body, for function
318 definitions), local declarations/references.
319
320 void getInterfaceDetails(CXDecl X, CXCursor C) {
321 switch (clang_getCursorKind(C)) {
322 case Cursor_ObjC_ClassRef:
323 CXDecl SuperClass = clang_getCursorDecl(C);
324 case Cursor_ObjC_ProtocolRef:
325 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
326 case Cursor_Declaration:
327 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
328 }
329 }
330 static void usage() {
331 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
332 clang_loadDeclaration(D, getInterfaceDetails);
333 }
334 }
335*/
Steve Naroffc857ea42009-09-02 13:28:54 +0000336typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
Steve Naroff89922f82009-08-31 00:59:03 +0000337
John Thompson2e06fc82009-10-27 13:42:56 +0000338CINDEX_LINKAGE void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000339
Steve Naroff50398192009-08-28 15:28:48 +0000340/*
Steve Naroff88145032009-10-27 14:35:18 +0000341 * CXFile Operations.
342 */
Douglas Gregor08b0e8d2009-10-31 15:48:08 +0000343CINDEX_LINKAGE const char *clang_getFileName(CXFile SFile);
344CINDEX_LINKAGE time_t clang_getFileTime(CXFile SFile);
Steve Naroff88145032009-10-27 14:35:18 +0000345
346/*
Steve Naroff50398192009-08-28 15:28:48 +0000347 * CXEntity Operations.
348 */
Ted Kremenek31723832010-01-11 23:56:39 +0000349
350/* clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
351 * in a specified translation unit. */
352CINDEX_LINKAGE CXDecl clang_getDeclaration(CXEntity, CXTranslationUnit);
353
Steve Naroff50398192009-08-28 15:28:48 +0000354/*
355 * CXDecl Operations.
356 */
John Thompson2e06fc82009-10-27 13:42:56 +0000357CINDEX_LINKAGE CXCursor clang_getCursorFromDecl(CXDecl);
Ted Kremenek31723832010-01-11 23:56:39 +0000358CINDEX_LINKAGE CXEntity clang_getEntityFromDecl(CXIndex, CXDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000359CINDEX_LINKAGE CXString clang_getDeclSpelling(CXDecl);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000360CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl); /* deprecate */
361CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl); /* deprecate */
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000362CINDEX_LINKAGE const char *clang_getDeclSource(CXDecl); /* deprecate */
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000363CINDEX_LINKAGE CXFile clang_getDeclSourceFile(CXDecl); /* deprecate */
Steve Naroff699a07d2009-09-25 21:32:34 +0000364
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000365/**
Douglas Gregor1db19de2010-01-19 21:36:55 +0000366 * \brief Identifies a specific source location within a translation
367 * unit.
368 *
369 * Use clang_getInstantiationLocation() to map a source location to a
370 * particular file, line, and column.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000371 */
372typedef struct {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000373 void *ptr_data;
374 unsigned int_data;
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000375} CXSourceLocation;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000376
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000377/**
Douglas Gregor1db19de2010-01-19 21:36:55 +0000378 * \brief Identifies a range of source locations in the source code.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000379 *
Douglas Gregor1db19de2010-01-19 21:36:55 +0000380 * Use clang_getRangeStart() and clang_getRangeEnd() to retrieve the
381 * starting and end locations from a source range, respectively.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000382 */
383typedef struct {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000384 void *ptr_data;
385 unsigned begin_int_data;
386 unsigned end_int_data;
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000387} CXSourceRange;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000388
Douglas Gregor1db19de2010-01-19 21:36:55 +0000389/**
390 * \brief Retrieve the file, line, and column represented by the
391 * given source location.
392 *
393 * \param location the location within a source file that will be
394 * decomposed into its parts.
395 *
396 * \param file if non-NULL, will be set to the file to which the given
397 * source location points.
398 *
399 * \param line if non-NULL, will be set to the line to which the given
400 * source location points.
401 *
402 * \param column if non-NULL, will be set to the column to which the
403 * given source location points.
404 */
405CINDEX_LINKAGE void clang_getInstantiationLocation(CXSourceLocation location,
406 CXFile *file,
407 unsigned *line,
408 unsigned *column);
409
410/**
411 * \brief Retrieve a source location representing the first
412 * character within a source range.
413 */
414CINDEX_LINKAGE CXSourceLocation clang_getRangeStart(CXSourceRange range);
415
416/**
417 * \brief Retrieve a source location representing the last
418 * character within a source range.
419 */
420CINDEX_LINKAGE CXSourceLocation clang_getRangeEnd(CXSourceRange range);
421
Daniel Dunbar3aacd532010-01-06 06:51:48 +0000422/* clang_getDeclExtent() returns the physical extent of a declaration. The
423 * beginning line/column pair points to the start of the first token in the
Ted Kremenekd8210652010-01-06 23:43:31 +0000424 * declaration, and the ending line/column pair points to the last character in
425 * the last token of the declaration.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000426 */
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000427CINDEX_LINKAGE CXSourceRange clang_getDeclExtent(CXDecl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000428
Steve Naroff50398192009-08-28 15:28:48 +0000429/*
430 * CXCursor Operations.
431 */
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000432/**
433 Usage: clang_getCursor() will translate a source/line/column position
434 into an AST cursor (to derive semantic information from the source code).
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000435 */
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000436CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit,
437 const char *source_name,
438 unsigned line, unsigned column);
Ted Kremenek73885552009-11-17 19:28:59 +0000439
440CINDEX_LINKAGE CXCursor clang_getNullCursor(void);
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000441
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000442/* clang_getCursorUSR() returns the USR (if any) associated with entity referred to by the
443 * provided CXCursor object. */
444CINDEX_LINKAGE CXString clang_getCursorUSR(CXCursor);
445
John Thompson2e06fc82009-10-27 13:42:56 +0000446CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor);
447CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind);
448CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind);
John Thompson2e06fc82009-10-27 13:42:56 +0000449CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
Steve Naroff600866c2009-08-27 19:51:58 +0000450
Ted Kremenek73885552009-11-17 19:28:59 +0000451CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor);
452
Steve Naroffef0cef62009-11-09 17:45:52 +0000453CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);
Steve Narofff334b4e2009-09-02 18:26:48 +0000454
Douglas Gregor98258af2010-01-18 22:46:11 +0000455/**
456 * \brief Retrieve the physical location of the source constructor referenced
457 * by the given cursor.
458 *
459 * The location of a declaration is typically the location of the name of that
460 * declaration, where the name of that declaration would occur if it is
461 * unnamed, or some keyword that introduces that particular declaration.
462 * The location of a reference is where that reference occurs within the
463 * source code.
464 */
465CINDEX_LINKAGE CXSourceLocation clang_getCursorLocation(CXCursor);
466
Douglas Gregorb6998662010-01-19 19:34:47 +0000467/**
468 * \brief Retrieve the physical extent of the source construct referenced by
Douglas Gregora7bde202010-01-19 00:34:46 +0000469 * the given cursor.
470 *
471 * The extent of a cursor starts with the file/line/column pointing at the
472 * first character within the source construct that the cursor refers to and
473 * ends with the last character withinin that source construct. For a
474 * declaration, the extent covers the declaration itself. For a reference,
475 * the extent covers the location of the reference (e.g., where the referenced
476 * entity was actually used).
477 */
478CINDEX_LINKAGE CXSourceRange clang_getCursorExtent(CXCursor);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000479
480/** \brief For a cursor that is a reference, retrieve a cursor representing the
481 * entity that it references.
482 *
483 * Reference cursors refer to other entities in the AST. For example, an
484 * Objective-C superclass reference cursor refers to an Objective-C class.
485 * This function produces the cursor for the Objective-C class from the
486 * cursor for the superclass reference. If the input cursor is a declaration or
487 * definition, it returns that declaration or definition unchanged.
488 * Othewise, returns the NULL cursor.
489 */
490CINDEX_LINKAGE CXCursor clang_getCursorReferenced(CXCursor);
Douglas Gregorb6998662010-01-19 19:34:47 +0000491
492/**
493 * \brief For a cursor that is either a reference to or a declaration
494 * of some entity, retrieve a cursor that describes the definition of
495 * that entity.
496 *
497 * Some entities can be declared multiple times within a translation
498 * unit, but only one of those declarations can also be a
499 * definition. For example, given:
500 *
501 * \code
502 * int f(int, int);
503 * int g(int x, int y) { return f(x, y); }
504 * int f(int a, int b) { return a + b; }
505 * int f(int, int);
506 * \endcode
507 *
508 * there are three declarations of the function "f", but only the
509 * second one is a definition. The clang_getCursorDefinition()
510 * function will take any cursor pointing to a declaration of "f"
511 * (the first or fourth lines of the example) or a cursor referenced
512 * that uses "f" (the call to "f' inside "g") and will return a
513 * declaration cursor pointing to the definition (the second "f"
514 * declaration).
515 *
516 * If given a cursor for which there is no corresponding definition,
517 * e.g., because there is no definition of that entity within this
518 * translation unit, returns a NULL cursor.
519 */
520CINDEX_LINKAGE CXCursor clang_getCursorDefinition(CXCursor);
521
522/**
523 * \brief Determine whether the declaration pointed to by this cursor
524 * is also a definition of that entity.
525 */
526CINDEX_LINKAGE unsigned clang_isCursorDefinition(CXCursor);
527
Steve Naroff4ade6d62009-09-23 17:52:52 +0000528/* for debug/testing */
John Thompson2e06fc82009-10-27 13:42:56 +0000529CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind);
530CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000531 const char **startBuf,
532 const char **endBuf,
533 unsigned *startLine,
534 unsigned *startColumn,
535 unsigned *endLine,
536 unsigned *endColumn);
Steve Naroff600866c2009-08-27 19:51:58 +0000537
Steve Naroff50398192009-08-28 15:28:48 +0000538/*
Chris Lattner459789b2009-09-16 20:18:54 +0000539 * If CXCursorKind == Cursor_Reference, then this will return the referenced
540 * declaration.
Steve Naroff50398192009-08-28 15:28:48 +0000541 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
542 */
John Thompson2e06fc82009-10-27 13:42:56 +0000543CINDEX_LINKAGE CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000544
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000545/**
546 * \brief A semantic string that describes a code-completion result.
547 *
548 * A semantic string that describes the formatting of a code-completion
549 * result as a single "template" of text that should be inserted into the
550 * source buffer when a particular code-completion result is selected.
551 * Each semantic string is made up of some number of "chunks", each of which
552 * contains some text along with a description of what that text means, e.g.,
553 * the name of the entity being referenced, whether the text chunk is part of
554 * the template, or whether it is a "placeholder" that the user should replace
555 * with actual code,of a specific kind. See \c CXCompletionChunkKind for a
556 * description of the different kinds of chunks.
557 */
558typedef void *CXCompletionString;
559
560/**
561 * \brief A single result of code completion.
562 */
563typedef struct {
564 /**
565 * \brief The kind of entity that this completion refers to.
566 *
567 * The cursor kind will be a macro, keyword, or a declaration (one of the
568 * *Decl cursor kinds), describing the entity that the completion is
569 * referring to.
570 *
571 * \todo In the future, we would like to provide a full cursor, to allow
572 * the client to extract additional information from declaration.
573 */
574 enum CXCursorKind CursorKind;
575
576 /**
577 * \brief The code-completion string that describes how to insert this
578 * code-completion result into the editing buffer.
579 */
580 CXCompletionString CompletionString;
581} CXCompletionResult;
582
583/**
584 * \brief Describes a single piece of text within a code-completion string.
585 *
586 * Each "chunk" within a code-completion string (\c CXCompletionString) is
587 * either a piece of text with a specific "kind" that describes how that text
588 * should be interpreted by the client or is another completion string.
589 */
590enum CXCompletionChunkKind {
591 /**
592 * \brief A code-completion string that describes "optional" text that
593 * could be a part of the template (but is not required).
594 *
595 * The Optional chunk is the only kind of chunk that has a code-completion
596 * string for its representation, which is accessible via
597 * \c clang_getCompletionChunkCompletionString(). The code-completion string
598 * describes an additional part of the template that is completely optional.
599 * For example, optional chunks can be used to describe the placeholders for
600 * arguments that match up with defaulted function parameters, e.g. given:
601 *
602 * \code
603 * void f(int x, float y = 3.14, double z = 2.71828);
604 * \endcode
605 *
606 * The code-completion string for this function would contain:
607 * - a TypedText chunk for "f".
608 * - a LeftParen chunk for "(".
609 * - a Placeholder chunk for "int x"
610 * - an Optional chunk containing the remaining defaulted arguments, e.g.,
611 * - a Comma chunk for ","
612 * - a Placeholder chunk for "float x"
613 * - an Optional chunk containing the last defaulted argument:
614 * - a Comma chunk for ","
615 * - a Placeholder chunk for "double z"
616 * - a RightParen chunk for ")"
617 *
618 * There are many ways two handle Optional chunks. Two simple approaches are:
619 * - Completely ignore optional chunks, in which case the template for the
620 * function "f" would only include the first parameter ("int x").
621 * - Fully expand all optional chunks, in which case the template for the
622 * function "f" would have all of the parameters.
623 */
624 CXCompletionChunk_Optional,
625 /**
626 * \brief Text that a user would be expected to type to get this
627 * code-completion result.
628 *
629 * There will be exactly one "typed text" chunk in a semantic string, which
630 * will typically provide the spelling of a keyword or the name of a
631 * declaration that could be used at the current code point. Clients are
632 * expected to filter the code-completion results based on the text in this
633 * chunk.
634 */
635 CXCompletionChunk_TypedText,
636 /**
637 * \brief Text that should be inserted as part of a code-completion result.
638 *
639 * A "text" chunk represents text that is part of the template to be
640 * inserted into user code should this particular code-completion result
641 * be selected.
642 */
643 CXCompletionChunk_Text,
644 /**
645 * \brief Placeholder text that should be replaced by the user.
646 *
647 * A "placeholder" chunk marks a place where the user should insert text
648 * into the code-completion template. For example, placeholders might mark
649 * the function parameters for a function declaration, to indicate that the
650 * user should provide arguments for each of those parameters. The actual
651 * text in a placeholder is a suggestion for the text to display before
652 * the user replaces the placeholder with real code.
653 */
654 CXCompletionChunk_Placeholder,
655 /**
656 * \brief Informative text that should be displayed but never inserted as
657 * part of the template.
658 *
659 * An "informative" chunk contains annotations that can be displayed to
660 * help the user decide whether a particular code-completion result is the
661 * right option, but which is not part of the actual template to be inserted
662 * by code completion.
663 */
664 CXCompletionChunk_Informative,
665 /**
666 * \brief Text that describes the current parameter when code-completion is
667 * referring to function call, message send, or template specialization.
668 *
669 * A "current parameter" chunk occurs when code-completion is providing
670 * information about a parameter corresponding to the argument at the
671 * code-completion point. For example, given a function
672 *
673 * \code
674 * int add(int x, int y);
675 * \endcode
676 *
677 * and the source code \c add(, where the code-completion point is after the
678 * "(", the code-completion string will contain a "current parameter" chunk
679 * for "int x", indicating that the current argument will initialize that
680 * parameter. After typing further, to \c add(17, (where the code-completion
681 * point is after the ","), the code-completion string will contain a
682 * "current paremeter" chunk to "int y".
683 */
684 CXCompletionChunk_CurrentParameter,
685 /**
686 * \brief A left parenthesis ('('), used to initiate a function call or
687 * signal the beginning of a function parameter list.
688 */
689 CXCompletionChunk_LeftParen,
690 /**
691 * \brief A right parenthesis (')'), used to finish a function call or
692 * signal the end of a function parameter list.
693 */
694 CXCompletionChunk_RightParen,
695 /**
696 * \brief A left bracket ('[').
697 */
698 CXCompletionChunk_LeftBracket,
699 /**
700 * \brief A right bracket (']').
701 */
702 CXCompletionChunk_RightBracket,
703 /**
704 * \brief A left brace ('{').
705 */
706 CXCompletionChunk_LeftBrace,
707 /**
708 * \brief A right brace ('}').
709 */
710 CXCompletionChunk_RightBrace,
711 /**
712 * \brief A left angle bracket ('<').
713 */
714 CXCompletionChunk_LeftAngle,
715 /**
716 * \brief A right angle bracket ('>').
717 */
718 CXCompletionChunk_RightAngle,
719 /**
720 * \brief A comma separator (',').
721 */
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000722 CXCompletionChunk_Comma,
723 /**
724 * \brief Text that specifies the result type of a given result.
725 *
726 * This special kind of informative chunk is not meant to be inserted into
727 * the text buffer. Rather, it is meant to illustrate the type that an
728 * expression using the given completion string would have.
729 */
Douglas Gregor01dfea02010-01-10 23:08:15 +0000730 CXCompletionChunk_ResultType,
731 /**
732 * \brief A colon (':').
733 */
734 CXCompletionChunk_Colon,
735 /**
736 * \brief A semicolon (';').
737 */
738 CXCompletionChunk_SemiColon,
739 /**
740 * \brief An '=' sign.
741 */
742 CXCompletionChunk_Equal,
743 /**
744 * Horizontal space (' ').
745 */
746 CXCompletionChunk_HorizontalSpace,
747 /**
748 * Vertical space ('\n'), after which it is generally a good idea to
749 * perform indentation.
750 */
751 CXCompletionChunk_VerticalSpace
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000752};
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000753
754/**
755 * \brief Determine the kind of a particular chunk within a completion string.
756 *
757 * \param completion_string the completion string to query.
758 *
759 * \param chunk_number the 0-based index of the chunk in the completion string.
760 *
761 * \returns the kind of the chunk at the index \c chunk_number.
762 */
763CINDEX_LINKAGE enum CXCompletionChunkKind
764clang_getCompletionChunkKind(CXCompletionString completion_string,
765 unsigned chunk_number);
766
767/**
768 * \brief Retrieve the text associated with a particular chunk within a
769 * completion string.
770 *
771 * \param completion_string the completion string to query.
772 *
773 * \param chunk_number the 0-based index of the chunk in the completion string.
774 *
775 * \returns the text associated with the chunk at index \c chunk_number.
776 */
777CINDEX_LINKAGE const char *
778clang_getCompletionChunkText(CXCompletionString completion_string,
779 unsigned chunk_number);
780
781/**
782 * \brief Retrieve the completion string associated with a particular chunk
783 * within a completion string.
784 *
785 * \param completion_string the completion string to query.
786 *
787 * \param chunk_number the 0-based index of the chunk in the completion string.
788 *
789 * \returns the completion string associated with the chunk at index
790 * \c chunk_number, or NULL if that chunk is not represented by a completion
791 * string.
792 */
793CINDEX_LINKAGE CXCompletionString
794clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
795 unsigned chunk_number);
796
797/**
798 * \brief Retrieve the number of chunks in the given code-completion string.
799 */
800CINDEX_LINKAGE unsigned
801clang_getNumCompletionChunks(CXCompletionString completion_string);
802
803/**
Douglas Gregorec6762c2009-12-18 16:20:58 +0000804 * \brief Contains the results of code-completion.
805 *
806 * This data structure contains the results of code completion, as
807 * produced by \c clang_codeComplete. Its contents must be freed by
808 * \c clang_disposeCodeCompleteResults.
809 */
810typedef struct {
811 /**
812 * \brief The code-completion results.
813 */
814 CXCompletionResult *Results;
815
816 /**
817 * \brief The number of code-completion results stored in the
818 * \c Results array.
819 */
820 unsigned NumResults;
821} CXCodeCompleteResults;
822
823/**
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000824 * \brief Perform code completion at a given location in a source file.
825 *
826 * This function performs code completion at a particular file, line, and
827 * column within source code, providing results that suggest potential
828 * code snippets based on the context of the completion. The basic model
829 * for code completion is that Clang will parse a complete source file,
830 * performing syntax checking up to the location where code-completion has
831 * been requested. At that point, a special code-completion token is passed
832 * to the parser, which recognizes this token and determines, based on the
833 * current location in the C/Objective-C/C++ grammar and the state of
834 * semantic analysis, what completions to provide. These completions are
Douglas Gregorec6762c2009-12-18 16:20:58 +0000835 * returned via a new \c CXCodeCompleteResults structure.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000836 *
837 * Code completion itself is meant to be triggered by the client when the
838 * user types punctuation characters or whitespace, at which point the
839 * code-completion location will coincide with the cursor. For example, if \c p
840 * is a pointer, code-completion might be triggered after the "-" and then
841 * after the ">" in \c p->. When the code-completion location is afer the ">",
842 * the completion results will provide, e.g., the members of the struct that
843 * "p" points to. The client is responsible for placing the cursor at the
844 * beginning of the token currently being typed, then filtering the results
845 * based on the contents of the token. For example, when code-completing for
846 * the expression \c p->get, the client should provide the location just after
847 * the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the
848 * client can filter the results based on the current token text ("get"), only
849 * showing those results that start with "get". The intent of this interface
Douglas Gregorec6762c2009-12-18 16:20:58 +0000850 * is to separate the relatively high-latency acquisition of code-completion
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000851 * results from the filtering of results on a per-character basis, which must
852 * have a lower latency.
853 *
854 * \param CIdx the \c CXIndex instance that will be used to perform code
855 * completion.
856 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000857 * \param source_filename the name of the source file that should be parsed to
858 * perform code-completion. This source file must be the same as or include the
859 * filename described by \p complete_filename, or no code-completion results
860 * will be produced. NOTE: One can also specify NULL for this argument if the
861 * source file is included in command_line_args.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000862 *
863 * \param num_command_line_args the number of command-line arguments stored in
864 * \p command_line_args.
865 *
866 * \param command_line_args the command-line arguments to pass to the Clang
867 * compiler to build the given source file. This should include all of the
868 * necessary include paths, language-dialect switches, precompiled header
869 * includes, etc., but should not include any information specific to
870 * code completion.
871 *
Douglas Gregor735df882009-12-02 09:21:34 +0000872 * \param num_unsaved_files the number of unsaved file entries in \p
873 * unsaved_files.
874 *
875 * \param unsaved_files the files that have not yet been saved to disk
876 * but may be required for code completion, including the contents of
877 * those files.
878 *
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000879 * \param complete_filename the name of the source file where code completion
880 * should be performed. In many cases, this name will be the same as the
881 * source filename. However, the completion filename may also be a file
882 * included by the source file, which is required when producing
883 * code-completion results for a header.
884 *
885 * \param complete_line the line at which code-completion should occur.
886 *
887 * \param complete_column the column at which code-completion should occur.
888 * Note that the column should point just after the syntactic construct that
889 * initiated code completion, and not in the middle of a lexical token.
890 *
Douglas Gregorec6762c2009-12-18 16:20:58 +0000891 * \returns if successful, a new CXCodeCompleteResults structure
892 * containing code-completion results, which should eventually be
893 * freed with \c clang_disposeCodeCompleteResults(). If code
894 * completion fails, returns NULL.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000895 */
Douglas Gregorec6762c2009-12-18 16:20:58 +0000896CINDEX_LINKAGE
897CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
898 const char *source_filename,
899 int num_command_line_args,
900 const char **command_line_args,
901 unsigned num_unsaved_files,
902 struct CXUnsavedFile *unsaved_files,
903 const char *complete_filename,
904 unsigned complete_line,
905 unsigned complete_column);
906
907/**
908 * \brief Free the given set of code-completion results.
909 */
910CINDEX_LINKAGE
911void clang_disposeCodeCompleteResults(CXCodeCompleteResults *Results);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000912
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000913#ifdef __cplusplus
914}
915#endif
916#endif
917