blob: 8a2ff8822e621b9a65ba01179eefa4a0750c27f6 [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,
Douglas Gregor97b98722010-01-19 23:20:36 +0000128 CXCursor_LastInvalid = 72,
129
130 /* Expressions */
131 CXCursor_FirstExpr = 100,
132
133 /**
134 * \brief An expression whose specific kind is not exposed via this
135 * interface.
136 *
137 * Unexposed expressions have the same operations as any other kind
138 * of expression; one can extract their location information,
139 * spelling, children, etc. However, the specific kind of the
140 * expression is not reported.
141 */
142 CXCursor_UnexposedExpr = 100,
143
144 /**
145 * \brief An expression that refers to some value declaration, such
146 * as a function, varible, or enumerator.
147 */
148 CXCursor_DeclRefExpr = 101,
149
150 /**
151 * \brief An expression that refers to a member of a struct, union,
152 * class, Objective-C class, etc.
153 */
154 CXCursor_MemberRefExpr = 102,
155
156 /** \brief An expression that calls a function. */
157 CXCursor_CallExpr = 103,
158
159 /** \brief An expression that sends a message to an Objective-C
160 object or class. */
161 CXCursor_ObjCMessageExpr = 104,
162 CXCursor_LastExpr = 104,
163
164 /* Statements */
165 CXCursor_FirstStmt = 200,
166 /**
167 * \brief A statement whose specific kind is not exposed via this
168 * interface.
169 *
170 * Unexposed statements have the same operations as any other kind of
171 * statement; one can extract their location information, spelling,
172 * children, etc. However, the specific kind of the statement is not
173 * reported.
174 */
175 CXCursor_UnexposedStmt = 200,
176 CXCursor_LastStmt = 200
Steve Naroff600866c2009-08-27 19:51:58 +0000177};
178
Douglas Gregor735df882009-12-02 09:21:34 +0000179/**
180 * \brief Provides the contents of a file that has not yet been saved to disk.
181 *
182 * Each CXUnsavedFile instance provides the name of a file on the
183 * system along with the current contents of that file that have not
184 * yet been saved to disk.
185 */
186struct CXUnsavedFile {
187 /**
188 * \brief The file whose contents have not yet been saved.
189 *
190 * This file must already exist in the file system.
191 */
192 const char *Filename;
193
194 /**
195 * \brief A null-terminated buffer containing the unsaved contents
196 * of this file.
197 */
198 const char *Contents;
199
200 /**
201 * \brief The length of the unsaved contents of this buffer, not
202 * counting the NULL at the end of the buffer.
203 */
204 unsigned long Length;
205};
206
Steve Naroff89922f82009-08-31 00:59:03 +0000207/* A cursor into the CXTranslationUnit. */
Steve Narofffb570422009-09-22 19:25:29 +0000208
Steve Naroff89922f82009-08-31 00:59:03 +0000209typedef struct {
210 enum CXCursorKind kind;
Douglas Gregor283cae32010-01-15 21:56:13 +0000211 void *data[3];
Steve Naroff89922f82009-08-31 00:59:03 +0000212} CXCursor;
213
Steve Naroff50398192009-08-28 15:28:48 +0000214/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Ted Kremenek31723832010-01-11 23:56:39 +0000215typedef struct {
216 CXIndex index;
217 void *data;
218} CXEntity;
Steve Naroff600866c2009-08-27 19:51:58 +0000219
Steve Naroffef0cef62009-11-09 17:45:52 +0000220/**
221 * For functions returning a string that might or might not need
222 * to be internally allocated and freed.
223 * Use clang_getCString to access the C string value.
224 * Use clang_disposeString to free the value.
225 * Treat it as an opaque type.
226 */
227typedef struct {
228 const char *Spelling;
229 /* A 1 value indicates the clang_ indexing API needed to allocate the string
230 (and it must be freed by clang_disposeString()). */
231 int MustFreeString;
232} CXString;
233
234/* Get C string pointer from a CXString. */
235CINDEX_LINKAGE const char *clang_getCString(CXString string);
236
237/* Free CXString. */
238CINDEX_LINKAGE void clang_disposeString(CXString string);
239
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000240/**
241 * \brief clang_createIndex() provides a shared context for creating
242 * translation units. It provides two options:
243 *
244 * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local"
245 * declarations (when loading any new translation units). A "local" declaration
246 * is one that belongs in the translation unit itself and not in a precompiled
247 * header that was used by the translation unit. If zero, all declarations
248 * will be enumerated.
249 *
250 * - displayDiagnostics: when non-zero, diagnostics will be output. If zero,
251 * diagnostics will be ignored.
Steve Naroffb4ece632009-10-20 16:36:34 +0000252 *
253 * Here is an example:
254 *
255 * // excludeDeclsFromPCH = 1, displayDiagnostics = 1
256 * Idx = clang_createIndex(1, 1);
257 *
258 * // IndexTest.pch was produced with the following command:
259 * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
260 * TU = clang_createTranslationUnit(Idx, "IndexTest.pch");
261 *
262 * // This will load all the symbols from 'IndexTest.pch'
263 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
264 * clang_disposeTranslationUnit(TU);
265 *
266 * // This will load all the symbols from 'IndexTest.c', excluding symbols
267 * // from 'IndexTest.pch'.
268 * char *args[] = { "-Xclang", "-include-pch=IndexTest.pch", 0 };
269 * TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args);
270 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
271 * clang_disposeTranslationUnit(TU);
272 *
273 * This process of creating the 'pch', loading it separately, and using it (via
274 * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks
275 * (which gives the indexer the same performance benefit as the compiler).
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000276 */
John Thompson2e06fc82009-10-27 13:42:56 +0000277CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000278 int displayDiagnostics);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000279CINDEX_LINKAGE void clang_disposeIndex(CXIndex index);
280CINDEX_LINKAGE CXString
281clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
282
283/*
284 * \brief Request that AST's be generated external for API calls which parse
285 * source code on the fly, e.g. \see createTranslationUnitFromSourceFile.
286 *
287 * Note: This is for debugging purposes only, and may be removed at a later
288 * date.
289 *
290 * \param index - The index to update.
291 * \param value - The new flag value.
292 */
293CINDEX_LINKAGE void clang_setUseExternalASTGeneration(CXIndex index,
294 int value);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000295
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000296/*
297 * \brief Create a translation unit from an AST file (-emit-ast).
298 */
John Thompson2e06fc82009-10-27 13:42:56 +0000299CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000300 CXIndex, const char *ast_filename
Steve Naroff600866c2009-08-27 19:51:58 +0000301);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000302
Ted Kremenek13745982009-10-19 22:15:09 +0000303/**
304 * \brief Destroy the specified CXTranslationUnit object.
305 */
John Thompson2e06fc82009-10-27 13:42:56 +0000306CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
Ted Kremenek13745982009-10-19 22:15:09 +0000307
308/**
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000309 * \brief Return the CXTranslationUnit for a given source file and the provided
310 * command line arguments one would pass to the compiler.
311 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000312 * Note: The 'source_filename' argument is optional. If the caller provides a
313 * NULL pointer, the name of the source file is expected to reside in the
314 * specified command line arguments.
Ted Kremenek139ba862009-10-22 00:03:57 +0000315 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000316 * Note: When encountered in 'clang_command_line_args', the following options
317 * are ignored:
Ted Kremenek139ba862009-10-22 00:03:57 +0000318 *
319 * '-c'
320 * '-emit-ast'
321 * '-fsyntax-only'
322 * '-o <output file>' (both '-o' and '<output file>' are ignored)
323 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000324 *
325 * \param source_filename - The name of the source file to load, or NULL if the
326 * source file is included in clang_command_line_args.
Ted Kremenek13745982009-10-19 22:15:09 +0000327 */
John Thompson2e06fc82009-10-27 13:42:56 +0000328CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile(
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000329 CXIndex CIdx,
330 const char *source_filename,
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000331 int num_clang_command_line_args,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000332 const char **clang_command_line_args
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000333);
Steve Naroff600866c2009-08-27 19:51:58 +0000334
335/*
336 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
337 within a translation unit, issuing a 'callback' for each one.
338
339 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
340 if (clang_getCursorKind(C) == Cursor_Declaration) {
341 CXDecl D = clang_getCursorDecl(C);
342 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
343 printf("@interface %s in file %s on line %d column %d\n",
344 clang_getDeclSpelling(D), clang_getCursorSource(C),
345 clang_getCursorLine(C), clang_getCursorColumn(C));
346 }
347 }
348 static void usage {
349 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000350 }
Steve Naroff600866c2009-08-27 19:51:58 +0000351*/
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000352typedef void *CXClientData;
353typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
354 CXClientData);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000355CINDEX_LINKAGE void clang_loadTranslationUnit(CXTranslationUnit,
356 CXTranslationUnitIterator,
357 CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000358
359/*
360 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
361 'callback' for each declaration/reference within the respective declaration.
362
363 For interface declarations, this will index the super class, protocols,
364 ivars, methods, etc. For structure declarations, this will index the fields.
365 For functions, this will index the parameters (and body, for function
366 definitions), local declarations/references.
367
368 void getInterfaceDetails(CXDecl X, CXCursor C) {
369 switch (clang_getCursorKind(C)) {
370 case Cursor_ObjC_ClassRef:
371 CXDecl SuperClass = clang_getCursorDecl(C);
372 case Cursor_ObjC_ProtocolRef:
373 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
374 case Cursor_Declaration:
375 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
376 }
377 }
378 static void usage() {
379 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
380 clang_loadDeclaration(D, getInterfaceDetails);
381 }
382 }
383*/
Steve Naroffc857ea42009-09-02 13:28:54 +0000384typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
Steve Naroff89922f82009-08-31 00:59:03 +0000385
John Thompson2e06fc82009-10-27 13:42:56 +0000386CINDEX_LINKAGE void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000387
Steve Naroff50398192009-08-28 15:28:48 +0000388/*
Steve Naroff88145032009-10-27 14:35:18 +0000389 * CXFile Operations.
390 */
Douglas Gregor08b0e8d2009-10-31 15:48:08 +0000391CINDEX_LINKAGE const char *clang_getFileName(CXFile SFile);
392CINDEX_LINKAGE time_t clang_getFileTime(CXFile SFile);
Steve Naroff88145032009-10-27 14:35:18 +0000393
394/*
Steve Naroff50398192009-08-28 15:28:48 +0000395 * CXEntity Operations.
396 */
Ted Kremenek31723832010-01-11 23:56:39 +0000397
398/* clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
399 * in a specified translation unit. */
400CINDEX_LINKAGE CXDecl clang_getDeclaration(CXEntity, CXTranslationUnit);
401
Steve Naroff50398192009-08-28 15:28:48 +0000402/*
403 * CXDecl Operations.
404 */
John Thompson2e06fc82009-10-27 13:42:56 +0000405CINDEX_LINKAGE CXCursor clang_getCursorFromDecl(CXDecl);
Ted Kremenek31723832010-01-11 23:56:39 +0000406CINDEX_LINKAGE CXEntity clang_getEntityFromDecl(CXIndex, CXDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000407CINDEX_LINKAGE CXString clang_getDeclSpelling(CXDecl);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000408CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl); /* deprecate */
409CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl); /* deprecate */
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000410CINDEX_LINKAGE const char *clang_getDeclSource(CXDecl); /* deprecate */
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000411CINDEX_LINKAGE CXFile clang_getDeclSourceFile(CXDecl); /* deprecate */
Steve Naroff699a07d2009-09-25 21:32:34 +0000412
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000413/**
Douglas Gregor1db19de2010-01-19 21:36:55 +0000414 * \brief Identifies a specific source location within a translation
415 * unit.
416 *
417 * Use clang_getInstantiationLocation() to map a source location to a
418 * particular file, line, and column.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000419 */
420typedef struct {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000421 void *ptr_data;
422 unsigned int_data;
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000423} CXSourceLocation;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000424
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000425/**
Douglas Gregor1db19de2010-01-19 21:36:55 +0000426 * \brief Identifies a range of source locations in the source code.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000427 *
Douglas Gregor1db19de2010-01-19 21:36:55 +0000428 * Use clang_getRangeStart() and clang_getRangeEnd() to retrieve the
429 * starting and end locations from a source range, respectively.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000430 */
431typedef struct {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000432 void *ptr_data;
433 unsigned begin_int_data;
434 unsigned end_int_data;
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000435} CXSourceRange;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000436
Douglas Gregor1db19de2010-01-19 21:36:55 +0000437/**
438 * \brief Retrieve the file, line, and column represented by the
439 * given source location.
440 *
441 * \param location the location within a source file that will be
442 * decomposed into its parts.
443 *
444 * \param file if non-NULL, will be set to the file to which the given
445 * source location points.
446 *
447 * \param line if non-NULL, will be set to the line to which the given
448 * source location points.
449 *
450 * \param column if non-NULL, will be set to the column to which the
451 * given source location points.
452 */
453CINDEX_LINKAGE void clang_getInstantiationLocation(CXSourceLocation location,
454 CXFile *file,
455 unsigned *line,
456 unsigned *column);
457
458/**
459 * \brief Retrieve a source location representing the first
460 * character within a source range.
461 */
462CINDEX_LINKAGE CXSourceLocation clang_getRangeStart(CXSourceRange range);
463
464/**
465 * \brief Retrieve a source location representing the last
466 * character within a source range.
467 */
468CINDEX_LINKAGE CXSourceLocation clang_getRangeEnd(CXSourceRange range);
469
Daniel Dunbar3aacd532010-01-06 06:51:48 +0000470/* clang_getDeclExtent() returns the physical extent of a declaration. The
471 * beginning line/column pair points to the start of the first token in the
Ted Kremenekd8210652010-01-06 23:43:31 +0000472 * declaration, and the ending line/column pair points to the last character in
473 * the last token of the declaration.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000474 */
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000475CINDEX_LINKAGE CXSourceRange clang_getDeclExtent(CXDecl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000476
Steve Naroff50398192009-08-28 15:28:48 +0000477/*
478 * CXCursor Operations.
479 */
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000480/**
481 Usage: clang_getCursor() will translate a source/line/column position
482 into an AST cursor (to derive semantic information from the source code).
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000483 */
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000484CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit,
485 const char *source_name,
486 unsigned line, unsigned column);
Ted Kremenek73885552009-11-17 19:28:59 +0000487
488CINDEX_LINKAGE CXCursor clang_getNullCursor(void);
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000489
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000490/* clang_getCursorUSR() returns the USR (if any) associated with entity referred to by the
491 * provided CXCursor object. */
492CINDEX_LINKAGE CXString clang_getCursorUSR(CXCursor);
493
John Thompson2e06fc82009-10-27 13:42:56 +0000494CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor);
495CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind);
496CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind);
Douglas Gregor97b98722010-01-19 23:20:36 +0000497CINDEX_LINKAGE unsigned clang_isExpression(enum CXCursorKind);
498CINDEX_LINKAGE unsigned clang_isStatement(enum CXCursorKind);
John Thompson2e06fc82009-10-27 13:42:56 +0000499CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
Steve Naroff600866c2009-08-27 19:51:58 +0000500
Ted Kremenek73885552009-11-17 19:28:59 +0000501CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor);
502
Steve Naroffef0cef62009-11-09 17:45:52 +0000503CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);
Steve Narofff334b4e2009-09-02 18:26:48 +0000504
Douglas Gregor98258af2010-01-18 22:46:11 +0000505/**
506 * \brief Retrieve the physical location of the source constructor referenced
507 * by the given cursor.
508 *
509 * The location of a declaration is typically the location of the name of that
510 * declaration, where the name of that declaration would occur if it is
511 * unnamed, or some keyword that introduces that particular declaration.
512 * The location of a reference is where that reference occurs within the
513 * source code.
514 */
515CINDEX_LINKAGE CXSourceLocation clang_getCursorLocation(CXCursor);
516
Douglas Gregorb6998662010-01-19 19:34:47 +0000517/**
518 * \brief Retrieve the physical extent of the source construct referenced by
Douglas Gregora7bde202010-01-19 00:34:46 +0000519 * the given cursor.
520 *
521 * The extent of a cursor starts with the file/line/column pointing at the
522 * first character within the source construct that the cursor refers to and
523 * ends with the last character withinin that source construct. For a
524 * declaration, the extent covers the declaration itself. For a reference,
525 * the extent covers the location of the reference (e.g., where the referenced
526 * entity was actually used).
527 */
528CINDEX_LINKAGE CXSourceRange clang_getCursorExtent(CXCursor);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000529
530/** \brief For a cursor that is a reference, retrieve a cursor representing the
531 * entity that it references.
532 *
533 * Reference cursors refer to other entities in the AST. For example, an
534 * Objective-C superclass reference cursor refers to an Objective-C class.
535 * This function produces the cursor for the Objective-C class from the
536 * cursor for the superclass reference. If the input cursor is a declaration or
537 * definition, it returns that declaration or definition unchanged.
538 * Othewise, returns the NULL cursor.
539 */
540CINDEX_LINKAGE CXCursor clang_getCursorReferenced(CXCursor);
Douglas Gregorb6998662010-01-19 19:34:47 +0000541
542/**
543 * \brief For a cursor that is either a reference to or a declaration
544 * of some entity, retrieve a cursor that describes the definition of
545 * that entity.
546 *
547 * Some entities can be declared multiple times within a translation
548 * unit, but only one of those declarations can also be a
549 * definition. For example, given:
550 *
551 * \code
552 * int f(int, int);
553 * int g(int x, int y) { return f(x, y); }
554 * int f(int a, int b) { return a + b; }
555 * int f(int, int);
556 * \endcode
557 *
558 * there are three declarations of the function "f", but only the
559 * second one is a definition. The clang_getCursorDefinition()
560 * function will take any cursor pointing to a declaration of "f"
561 * (the first or fourth lines of the example) or a cursor referenced
562 * that uses "f" (the call to "f' inside "g") and will return a
563 * declaration cursor pointing to the definition (the second "f"
564 * declaration).
565 *
566 * If given a cursor for which there is no corresponding definition,
567 * e.g., because there is no definition of that entity within this
568 * translation unit, returns a NULL cursor.
569 */
570CINDEX_LINKAGE CXCursor clang_getCursorDefinition(CXCursor);
571
572/**
573 * \brief Determine whether the declaration pointed to by this cursor
574 * is also a definition of that entity.
575 */
576CINDEX_LINKAGE unsigned clang_isCursorDefinition(CXCursor);
577
Steve Naroff4ade6d62009-09-23 17:52:52 +0000578/* for debug/testing */
John Thompson2e06fc82009-10-27 13:42:56 +0000579CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind);
580CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000581 const char **startBuf,
582 const char **endBuf,
583 unsigned *startLine,
584 unsigned *startColumn,
585 unsigned *endLine,
586 unsigned *endColumn);
Steve Naroff600866c2009-08-27 19:51:58 +0000587
Steve Naroff50398192009-08-28 15:28:48 +0000588/*
Chris Lattner459789b2009-09-16 20:18:54 +0000589 * If CXCursorKind == Cursor_Reference, then this will return the referenced
590 * declaration.
Steve Naroff50398192009-08-28 15:28:48 +0000591 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
592 */
John Thompson2e06fc82009-10-27 13:42:56 +0000593CINDEX_LINKAGE CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000594
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000595/**
596 * \brief A semantic string that describes a code-completion result.
597 *
598 * A semantic string that describes the formatting of a code-completion
599 * result as a single "template" of text that should be inserted into the
600 * source buffer when a particular code-completion result is selected.
601 * Each semantic string is made up of some number of "chunks", each of which
602 * contains some text along with a description of what that text means, e.g.,
603 * the name of the entity being referenced, whether the text chunk is part of
604 * the template, or whether it is a "placeholder" that the user should replace
605 * with actual code,of a specific kind. See \c CXCompletionChunkKind for a
606 * description of the different kinds of chunks.
607 */
608typedef void *CXCompletionString;
609
610/**
611 * \brief A single result of code completion.
612 */
613typedef struct {
614 /**
615 * \brief The kind of entity that this completion refers to.
616 *
617 * The cursor kind will be a macro, keyword, or a declaration (one of the
618 * *Decl cursor kinds), describing the entity that the completion is
619 * referring to.
620 *
621 * \todo In the future, we would like to provide a full cursor, to allow
622 * the client to extract additional information from declaration.
623 */
624 enum CXCursorKind CursorKind;
625
626 /**
627 * \brief The code-completion string that describes how to insert this
628 * code-completion result into the editing buffer.
629 */
630 CXCompletionString CompletionString;
631} CXCompletionResult;
632
633/**
634 * \brief Describes a single piece of text within a code-completion string.
635 *
636 * Each "chunk" within a code-completion string (\c CXCompletionString) is
637 * either a piece of text with a specific "kind" that describes how that text
638 * should be interpreted by the client or is another completion string.
639 */
640enum CXCompletionChunkKind {
641 /**
642 * \brief A code-completion string that describes "optional" text that
643 * could be a part of the template (but is not required).
644 *
645 * The Optional chunk is the only kind of chunk that has a code-completion
646 * string for its representation, which is accessible via
647 * \c clang_getCompletionChunkCompletionString(). The code-completion string
648 * describes an additional part of the template that is completely optional.
649 * For example, optional chunks can be used to describe the placeholders for
650 * arguments that match up with defaulted function parameters, e.g. given:
651 *
652 * \code
653 * void f(int x, float y = 3.14, double z = 2.71828);
654 * \endcode
655 *
656 * The code-completion string for this function would contain:
657 * - a TypedText chunk for "f".
658 * - a LeftParen chunk for "(".
659 * - a Placeholder chunk for "int x"
660 * - an Optional chunk containing the remaining defaulted arguments, e.g.,
661 * - a Comma chunk for ","
662 * - a Placeholder chunk for "float x"
663 * - an Optional chunk containing the last defaulted argument:
664 * - a Comma chunk for ","
665 * - a Placeholder chunk for "double z"
666 * - a RightParen chunk for ")"
667 *
668 * There are many ways two handle Optional chunks. Two simple approaches are:
669 * - Completely ignore optional chunks, in which case the template for the
670 * function "f" would only include the first parameter ("int x").
671 * - Fully expand all optional chunks, in which case the template for the
672 * function "f" would have all of the parameters.
673 */
674 CXCompletionChunk_Optional,
675 /**
676 * \brief Text that a user would be expected to type to get this
677 * code-completion result.
678 *
679 * There will be exactly one "typed text" chunk in a semantic string, which
680 * will typically provide the spelling of a keyword or the name of a
681 * declaration that could be used at the current code point. Clients are
682 * expected to filter the code-completion results based on the text in this
683 * chunk.
684 */
685 CXCompletionChunk_TypedText,
686 /**
687 * \brief Text that should be inserted as part of a code-completion result.
688 *
689 * A "text" chunk represents text that is part of the template to be
690 * inserted into user code should this particular code-completion result
691 * be selected.
692 */
693 CXCompletionChunk_Text,
694 /**
695 * \brief Placeholder text that should be replaced by the user.
696 *
697 * A "placeholder" chunk marks a place where the user should insert text
698 * into the code-completion template. For example, placeholders might mark
699 * the function parameters for a function declaration, to indicate that the
700 * user should provide arguments for each of those parameters. The actual
701 * text in a placeholder is a suggestion for the text to display before
702 * the user replaces the placeholder with real code.
703 */
704 CXCompletionChunk_Placeholder,
705 /**
706 * \brief Informative text that should be displayed but never inserted as
707 * part of the template.
708 *
709 * An "informative" chunk contains annotations that can be displayed to
710 * help the user decide whether a particular code-completion result is the
711 * right option, but which is not part of the actual template to be inserted
712 * by code completion.
713 */
714 CXCompletionChunk_Informative,
715 /**
716 * \brief Text that describes the current parameter when code-completion is
717 * referring to function call, message send, or template specialization.
718 *
719 * A "current parameter" chunk occurs when code-completion is providing
720 * information about a parameter corresponding to the argument at the
721 * code-completion point. For example, given a function
722 *
723 * \code
724 * int add(int x, int y);
725 * \endcode
726 *
727 * and the source code \c add(, where the code-completion point is after the
728 * "(", the code-completion string will contain a "current parameter" chunk
729 * for "int x", indicating that the current argument will initialize that
730 * parameter. After typing further, to \c add(17, (where the code-completion
731 * point is after the ","), the code-completion string will contain a
732 * "current paremeter" chunk to "int y".
733 */
734 CXCompletionChunk_CurrentParameter,
735 /**
736 * \brief A left parenthesis ('('), used to initiate a function call or
737 * signal the beginning of a function parameter list.
738 */
739 CXCompletionChunk_LeftParen,
740 /**
741 * \brief A right parenthesis (')'), used to finish a function call or
742 * signal the end of a function parameter list.
743 */
744 CXCompletionChunk_RightParen,
745 /**
746 * \brief A left bracket ('[').
747 */
748 CXCompletionChunk_LeftBracket,
749 /**
750 * \brief A right bracket (']').
751 */
752 CXCompletionChunk_RightBracket,
753 /**
754 * \brief A left brace ('{').
755 */
756 CXCompletionChunk_LeftBrace,
757 /**
758 * \brief A right brace ('}').
759 */
760 CXCompletionChunk_RightBrace,
761 /**
762 * \brief A left angle bracket ('<').
763 */
764 CXCompletionChunk_LeftAngle,
765 /**
766 * \brief A right angle bracket ('>').
767 */
768 CXCompletionChunk_RightAngle,
769 /**
770 * \brief A comma separator (',').
771 */
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000772 CXCompletionChunk_Comma,
773 /**
774 * \brief Text that specifies the result type of a given result.
775 *
776 * This special kind of informative chunk is not meant to be inserted into
777 * the text buffer. Rather, it is meant to illustrate the type that an
778 * expression using the given completion string would have.
779 */
Douglas Gregor01dfea02010-01-10 23:08:15 +0000780 CXCompletionChunk_ResultType,
781 /**
782 * \brief A colon (':').
783 */
784 CXCompletionChunk_Colon,
785 /**
786 * \brief A semicolon (';').
787 */
788 CXCompletionChunk_SemiColon,
789 /**
790 * \brief An '=' sign.
791 */
792 CXCompletionChunk_Equal,
793 /**
794 * Horizontal space (' ').
795 */
796 CXCompletionChunk_HorizontalSpace,
797 /**
798 * Vertical space ('\n'), after which it is generally a good idea to
799 * perform indentation.
800 */
801 CXCompletionChunk_VerticalSpace
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000802};
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000803
804/**
805 * \brief Determine the kind of a particular chunk within a completion string.
806 *
807 * \param completion_string the completion string to query.
808 *
809 * \param chunk_number the 0-based index of the chunk in the completion string.
810 *
811 * \returns the kind of the chunk at the index \c chunk_number.
812 */
813CINDEX_LINKAGE enum CXCompletionChunkKind
814clang_getCompletionChunkKind(CXCompletionString completion_string,
815 unsigned chunk_number);
816
817/**
818 * \brief Retrieve the text associated with a particular chunk within a
819 * completion string.
820 *
821 * \param completion_string the completion string to query.
822 *
823 * \param chunk_number the 0-based index of the chunk in the completion string.
824 *
825 * \returns the text associated with the chunk at index \c chunk_number.
826 */
827CINDEX_LINKAGE const char *
828clang_getCompletionChunkText(CXCompletionString completion_string,
829 unsigned chunk_number);
830
831/**
832 * \brief Retrieve the completion string associated with a particular chunk
833 * within a completion string.
834 *
835 * \param completion_string the completion string to query.
836 *
837 * \param chunk_number the 0-based index of the chunk in the completion string.
838 *
839 * \returns the completion string associated with the chunk at index
840 * \c chunk_number, or NULL if that chunk is not represented by a completion
841 * string.
842 */
843CINDEX_LINKAGE CXCompletionString
844clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
845 unsigned chunk_number);
846
847/**
848 * \brief Retrieve the number of chunks in the given code-completion string.
849 */
850CINDEX_LINKAGE unsigned
851clang_getNumCompletionChunks(CXCompletionString completion_string);
852
853/**
Douglas Gregorec6762c2009-12-18 16:20:58 +0000854 * \brief Contains the results of code-completion.
855 *
856 * This data structure contains the results of code completion, as
857 * produced by \c clang_codeComplete. Its contents must be freed by
858 * \c clang_disposeCodeCompleteResults.
859 */
860typedef struct {
861 /**
862 * \brief The code-completion results.
863 */
864 CXCompletionResult *Results;
865
866 /**
867 * \brief The number of code-completion results stored in the
868 * \c Results array.
869 */
870 unsigned NumResults;
871} CXCodeCompleteResults;
872
873/**
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000874 * \brief Perform code completion at a given location in a source file.
875 *
876 * This function performs code completion at a particular file, line, and
877 * column within source code, providing results that suggest potential
878 * code snippets based on the context of the completion. The basic model
879 * for code completion is that Clang will parse a complete source file,
880 * performing syntax checking up to the location where code-completion has
881 * been requested. At that point, a special code-completion token is passed
882 * to the parser, which recognizes this token and determines, based on the
883 * current location in the C/Objective-C/C++ grammar and the state of
884 * semantic analysis, what completions to provide. These completions are
Douglas Gregorec6762c2009-12-18 16:20:58 +0000885 * returned via a new \c CXCodeCompleteResults structure.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000886 *
887 * Code completion itself is meant to be triggered by the client when the
888 * user types punctuation characters or whitespace, at which point the
889 * code-completion location will coincide with the cursor. For example, if \c p
890 * is a pointer, code-completion might be triggered after the "-" and then
891 * after the ">" in \c p->. When the code-completion location is afer the ">",
892 * the completion results will provide, e.g., the members of the struct that
893 * "p" points to. The client is responsible for placing the cursor at the
894 * beginning of the token currently being typed, then filtering the results
895 * based on the contents of the token. For example, when code-completing for
896 * the expression \c p->get, the client should provide the location just after
897 * the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the
898 * client can filter the results based on the current token text ("get"), only
899 * showing those results that start with "get". The intent of this interface
Douglas Gregorec6762c2009-12-18 16:20:58 +0000900 * is to separate the relatively high-latency acquisition of code-completion
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000901 * results from the filtering of results on a per-character basis, which must
902 * have a lower latency.
903 *
904 * \param CIdx the \c CXIndex instance that will be used to perform code
905 * completion.
906 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000907 * \param source_filename the name of the source file that should be parsed to
908 * perform code-completion. This source file must be the same as or include the
909 * filename described by \p complete_filename, or no code-completion results
910 * will be produced. NOTE: One can also specify NULL for this argument if the
911 * source file is included in command_line_args.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000912 *
913 * \param num_command_line_args the number of command-line arguments stored in
914 * \p command_line_args.
915 *
916 * \param command_line_args the command-line arguments to pass to the Clang
917 * compiler to build the given source file. This should include all of the
918 * necessary include paths, language-dialect switches, precompiled header
919 * includes, etc., but should not include any information specific to
920 * code completion.
921 *
Douglas Gregor735df882009-12-02 09:21:34 +0000922 * \param num_unsaved_files the number of unsaved file entries in \p
923 * unsaved_files.
924 *
925 * \param unsaved_files the files that have not yet been saved to disk
926 * but may be required for code completion, including the contents of
927 * those files.
928 *
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000929 * \param complete_filename the name of the source file where code completion
930 * should be performed. In many cases, this name will be the same as the
931 * source filename. However, the completion filename may also be a file
932 * included by the source file, which is required when producing
933 * code-completion results for a header.
934 *
935 * \param complete_line the line at which code-completion should occur.
936 *
937 * \param complete_column the column at which code-completion should occur.
938 * Note that the column should point just after the syntactic construct that
939 * initiated code completion, and not in the middle of a lexical token.
940 *
Douglas Gregorec6762c2009-12-18 16:20:58 +0000941 * \returns if successful, a new CXCodeCompleteResults structure
942 * containing code-completion results, which should eventually be
943 * freed with \c clang_disposeCodeCompleteResults(). If code
944 * completion fails, returns NULL.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000945 */
Douglas Gregorec6762c2009-12-18 16:20:58 +0000946CINDEX_LINKAGE
947CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
948 const char *source_filename,
949 int num_command_line_args,
950 const char **command_line_args,
951 unsigned num_unsaved_files,
952 struct CXUnsavedFile *unsaved_files,
953 const char *complete_filename,
954 unsigned complete_line,
955 unsigned complete_column);
956
957/**
958 * \brief Free the given set of code-completion results.
959 */
960CINDEX_LINKAGE
961void clang_disposeCodeCompleteResults(CXCodeCompleteResults *Results);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000962
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000963#ifdef __cplusplus
964}
965#endif
966#endif
967