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