blob: d203ff8b8f412fcb922ee25ed45e8dbe809068e4 [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 Gregor36103f42010-01-20 00:07:45 +000058 /**
59 * \brief A declaration whose specific kind is not exposed via this
60 * interface.
61 *
62 * Unexposed declarations have the same operations as any other kind
63 * of declaration; one can extract their location information,
64 * spelling, find their definitions, etc. However, the specific kind
65 * of the declaration is not reported.
66 */
67 CXCursor_UnexposedDecl = 1,
Douglas Gregor30122132010-01-19 22:07:56 +000068 /** \brief A C or C++ struct. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000069 CXCursor_StructDecl = 2,
Douglas Gregor30122132010-01-19 22:07:56 +000070 /** \brief A C or C++ union. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000071 CXCursor_UnionDecl = 3,
Douglas Gregor30122132010-01-19 22:07:56 +000072 /** \brief A C++ class. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000073 CXCursor_ClassDecl = 4,
Douglas Gregor30122132010-01-19 22:07:56 +000074 /** \brief An enumeration. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000075 CXCursor_EnumDecl = 5,
Douglas Gregor30122132010-01-19 22:07:56 +000076 /**
77 * \brief A field (in C) or non-static data member (in C++) in a
78 * struct, union, or C++ class.
79 */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000080 CXCursor_FieldDecl = 6,
Douglas Gregor30122132010-01-19 22:07:56 +000081 /** \brief An enumerator constant. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000082 CXCursor_EnumConstantDecl = 7,
Douglas Gregor30122132010-01-19 22:07:56 +000083 /** \brief A function. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000084 CXCursor_FunctionDecl = 8,
Douglas Gregor30122132010-01-19 22:07:56 +000085 /** \brief A variable. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000086 CXCursor_VarDecl = 9,
Douglas Gregor30122132010-01-19 22:07:56 +000087 /** \brief A function or method parameter. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000088 CXCursor_ParmDecl = 10,
Douglas Gregor30122132010-01-19 22:07:56 +000089 /** \brief An Objective-C @interface. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000090 CXCursor_ObjCInterfaceDecl = 11,
Douglas Gregor30122132010-01-19 22:07:56 +000091 /** \brief An Objective-C @interface for a category. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000092 CXCursor_ObjCCategoryDecl = 12,
Douglas Gregor30122132010-01-19 22:07:56 +000093 /** \brief An Objective-C @protocol declaration. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000094 CXCursor_ObjCProtocolDecl = 13,
Douglas Gregor30122132010-01-19 22:07:56 +000095 /** \brief An Objective-C @property declaration. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000096 CXCursor_ObjCPropertyDecl = 14,
Douglas Gregor30122132010-01-19 22:07:56 +000097 /** \brief An Objective-C instance variable. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +000098 CXCursor_ObjCIvarDecl = 15,
Douglas Gregor30122132010-01-19 22:07:56 +000099 /** \brief An Objective-C instance method. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000100 CXCursor_ObjCInstanceMethodDecl = 16,
Douglas Gregor30122132010-01-19 22:07:56 +0000101 /** \brief An Objective-C class method. */
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000102 CXCursor_ObjCClassMethodDecl = 17,
Douglas Gregor30122132010-01-19 22:07:56 +0000103 /** \brief An Objective-C @implementation. */
Douglas Gregorb6998662010-01-19 19:34:47 +0000104 CXCursor_ObjCImplementationDecl = 18,
Douglas Gregor30122132010-01-19 22:07:56 +0000105 /** \brief An Objective-C @implementation for a category. */
Douglas Gregorb6998662010-01-19 19:34:47 +0000106 CXCursor_ObjCCategoryImplDecl = 19,
Douglas Gregor36103f42010-01-20 00:07:45 +0000107 /** \brief A typedef */
108 CXCursor_TypedefDecl = 20,
Douglas Gregor30122132010-01-19 22:07:56 +0000109 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,
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000171 CXCursor_LastStmt = 200,
172
173 /**
174 * \brief Cursor that represents the translation unit itself.
175 *
176 * The translation unit cursor exists primarily to act as the root
177 * cursor for traversing the contents of a translation unit.
178 */
179 CXCursor_TranslationUnit = 300
Steve Naroff600866c2009-08-27 19:51:58 +0000180};
181
Douglas Gregor735df882009-12-02 09:21:34 +0000182/**
183 * \brief Provides the contents of a file that has not yet been saved to disk.
184 *
185 * Each CXUnsavedFile instance provides the name of a file on the
186 * system along with the current contents of that file that have not
187 * yet been saved to disk.
188 */
189struct CXUnsavedFile {
190 /**
191 * \brief The file whose contents have not yet been saved.
192 *
193 * This file must already exist in the file system.
194 */
195 const char *Filename;
196
197 /**
198 * \brief A null-terminated buffer containing the unsaved contents
199 * of this file.
200 */
201 const char *Contents;
202
203 /**
204 * \brief The length of the unsaved contents of this buffer, not
205 * counting the NULL at the end of the buffer.
206 */
207 unsigned long Length;
208};
209
Steve Naroff89922f82009-08-31 00:59:03 +0000210/* A cursor into the CXTranslationUnit. */
Steve Narofffb570422009-09-22 19:25:29 +0000211
Steve Naroff89922f82009-08-31 00:59:03 +0000212typedef struct {
213 enum CXCursorKind kind;
Douglas Gregor283cae32010-01-15 21:56:13 +0000214 void *data[3];
Steve Naroff89922f82009-08-31 00:59:03 +0000215} CXCursor;
216
Steve Naroff50398192009-08-28 15:28:48 +0000217/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Ted Kremenek31723832010-01-11 23:56:39 +0000218typedef struct {
219 CXIndex index;
220 void *data;
221} CXEntity;
Steve Naroff600866c2009-08-27 19:51:58 +0000222
Steve Naroffef0cef62009-11-09 17:45:52 +0000223/**
224 * For functions returning a string that might or might not need
225 * to be internally allocated and freed.
226 * Use clang_getCString to access the C string value.
227 * Use clang_disposeString to free the value.
228 * Treat it as an opaque type.
229 */
230typedef struct {
231 const char *Spelling;
232 /* A 1 value indicates the clang_ indexing API needed to allocate the string
233 (and it must be freed by clang_disposeString()). */
234 int MustFreeString;
235} CXString;
236
237/* Get C string pointer from a CXString. */
238CINDEX_LINKAGE const char *clang_getCString(CXString string);
239
240/* Free CXString. */
241CINDEX_LINKAGE void clang_disposeString(CXString string);
242
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000243/**
244 * \brief clang_createIndex() provides a shared context for creating
245 * translation units. It provides two options:
246 *
247 * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local"
248 * declarations (when loading any new translation units). A "local" declaration
249 * is one that belongs in the translation unit itself and not in a precompiled
250 * header that was used by the translation unit. If zero, all declarations
251 * will be enumerated.
252 *
253 * - displayDiagnostics: when non-zero, diagnostics will be output. If zero,
254 * diagnostics will be ignored.
Steve Naroffb4ece632009-10-20 16:36:34 +0000255 *
256 * Here is an example:
257 *
258 * // excludeDeclsFromPCH = 1, displayDiagnostics = 1
259 * Idx = clang_createIndex(1, 1);
260 *
261 * // IndexTest.pch was produced with the following command:
262 * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
263 * TU = clang_createTranslationUnit(Idx, "IndexTest.pch");
264 *
265 * // This will load all the symbols from 'IndexTest.pch'
266 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
267 * clang_disposeTranslationUnit(TU);
268 *
269 * // This will load all the symbols from 'IndexTest.c', excluding symbols
270 * // from 'IndexTest.pch'.
271 * char *args[] = { "-Xclang", "-include-pch=IndexTest.pch", 0 };
272 * TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args);
273 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
274 * clang_disposeTranslationUnit(TU);
275 *
276 * This process of creating the 'pch', loading it separately, and using it (via
277 * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks
278 * (which gives the indexer the same performance benefit as the compiler).
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000279 */
John Thompson2e06fc82009-10-27 13:42:56 +0000280CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000281 int displayDiagnostics);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000282CINDEX_LINKAGE void clang_disposeIndex(CXIndex index);
283CINDEX_LINKAGE CXString
284clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
285
286/*
287 * \brief Request that AST's be generated external for API calls which parse
288 * source code on the fly, e.g. \see createTranslationUnitFromSourceFile.
289 *
290 * Note: This is for debugging purposes only, and may be removed at a later
291 * date.
292 *
293 * \param index - The index to update.
294 * \param value - The new flag value.
295 */
296CINDEX_LINKAGE void clang_setUseExternalASTGeneration(CXIndex index,
297 int value);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000298
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000299/*
300 * \brief Create a translation unit from an AST file (-emit-ast).
301 */
John Thompson2e06fc82009-10-27 13:42:56 +0000302CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000303 CXIndex, const char *ast_filename
Steve Naroff600866c2009-08-27 19:51:58 +0000304);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000305
Ted Kremenek13745982009-10-19 22:15:09 +0000306/**
307 * \brief Destroy the specified CXTranslationUnit object.
308 */
John Thompson2e06fc82009-10-27 13:42:56 +0000309CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
Ted Kremenek13745982009-10-19 22:15:09 +0000310
311/**
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000312 * \brief Return the CXTranslationUnit for a given source file and the provided
313 * command line arguments one would pass to the compiler.
314 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000315 * Note: The 'source_filename' argument is optional. If the caller provides a
316 * NULL pointer, the name of the source file is expected to reside in the
317 * specified command line arguments.
Ted Kremenek139ba862009-10-22 00:03:57 +0000318 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000319 * Note: When encountered in 'clang_command_line_args', the following options
320 * are ignored:
Ted Kremenek139ba862009-10-22 00:03:57 +0000321 *
322 * '-c'
323 * '-emit-ast'
324 * '-fsyntax-only'
325 * '-o <output file>' (both '-o' and '<output file>' are ignored)
326 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000327 *
328 * \param source_filename - The name of the source file to load, or NULL if the
329 * source file is included in clang_command_line_args.
Ted Kremenek13745982009-10-19 22:15:09 +0000330 */
John Thompson2e06fc82009-10-27 13:42:56 +0000331CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile(
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000332 CXIndex CIdx,
333 const char *source_filename,
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000334 int num_clang_command_line_args,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000335 const char **clang_command_line_args
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000336);
Steve Naroff600866c2009-08-27 19:51:58 +0000337
338/*
339 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
340 within a translation unit, issuing a 'callback' for each one.
341
342 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
343 if (clang_getCursorKind(C) == Cursor_Declaration) {
344 CXDecl D = clang_getCursorDecl(C);
345 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
346 printf("@interface %s in file %s on line %d column %d\n",
347 clang_getDeclSpelling(D), clang_getCursorSource(C),
348 clang_getCursorLine(C), clang_getCursorColumn(C));
349 }
350 }
351 static void usage {
352 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000353 }
Steve Naroff600866c2009-08-27 19:51:58 +0000354*/
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000355typedef void *CXClientData;
356typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
357 CXClientData);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000358CINDEX_LINKAGE void clang_loadTranslationUnit(CXTranslationUnit,
359 CXTranslationUnitIterator,
360 CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000361
362/*
363 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
364 'callback' for each declaration/reference within the respective declaration.
365
366 For interface declarations, this will index the super class, protocols,
367 ivars, methods, etc. For structure declarations, this will index the fields.
368 For functions, this will index the parameters (and body, for function
369 definitions), local declarations/references.
370
371 void getInterfaceDetails(CXDecl X, CXCursor C) {
372 switch (clang_getCursorKind(C)) {
373 case Cursor_ObjC_ClassRef:
374 CXDecl SuperClass = clang_getCursorDecl(C);
375 case Cursor_ObjC_ProtocolRef:
376 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
377 case Cursor_Declaration:
378 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
379 }
380 }
381 static void usage() {
382 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
383 clang_loadDeclaration(D, getInterfaceDetails);
384 }
385 }
386*/
Steve Naroffc857ea42009-09-02 13:28:54 +0000387typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
Steve Naroff89922f82009-08-31 00:59:03 +0000388
John Thompson2e06fc82009-10-27 13:42:56 +0000389CINDEX_LINKAGE void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000390
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000391/**
392 * \brief Retrieve the cursor that represents the given translation unit.
393 *
394 * The translation unit cursor can be used to start traversing the
395 * various declarations within the given translation unit.
396 */
397CINDEX_LINKAGE CXCursor clang_getTranslationUnitCursor(CXTranslationUnit);
398
Steve Naroff50398192009-08-28 15:28:48 +0000399/*
Steve Naroff88145032009-10-27 14:35:18 +0000400 * CXFile Operations.
401 */
Douglas Gregor08b0e8d2009-10-31 15:48:08 +0000402CINDEX_LINKAGE const char *clang_getFileName(CXFile SFile);
403CINDEX_LINKAGE time_t clang_getFileTime(CXFile SFile);
Steve Naroff88145032009-10-27 14:35:18 +0000404
405/*
Steve Naroff50398192009-08-28 15:28:48 +0000406 * CXEntity Operations.
407 */
Ted Kremenek31723832010-01-11 23:56:39 +0000408
409/* clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
410 * in a specified translation unit. */
411CINDEX_LINKAGE CXDecl clang_getDeclaration(CXEntity, CXTranslationUnit);
412
Steve Naroff50398192009-08-28 15:28:48 +0000413/*
414 * CXDecl Operations.
415 */
John Thompson2e06fc82009-10-27 13:42:56 +0000416CINDEX_LINKAGE CXCursor clang_getCursorFromDecl(CXDecl);
Ted Kremenek31723832010-01-11 23:56:39 +0000417CINDEX_LINKAGE CXEntity clang_getEntityFromDecl(CXIndex, CXDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000418CINDEX_LINKAGE CXString clang_getDeclSpelling(CXDecl);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000419CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl); /* deprecate */
420CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl); /* deprecate */
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000421CINDEX_LINKAGE const char *clang_getDeclSource(CXDecl); /* deprecate */
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000422CINDEX_LINKAGE CXFile clang_getDeclSourceFile(CXDecl); /* deprecate */
Steve Naroff699a07d2009-09-25 21:32:34 +0000423
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000424/**
Douglas Gregor1db19de2010-01-19 21:36:55 +0000425 * \brief Identifies a specific source location within a translation
426 * unit.
427 *
428 * Use clang_getInstantiationLocation() to map a source location to a
429 * particular file, line, and column.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000430 */
431typedef struct {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000432 void *ptr_data;
433 unsigned int_data;
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000434} CXSourceLocation;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000435
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000436/**
Douglas Gregor1db19de2010-01-19 21:36:55 +0000437 * \brief Identifies a range of source locations in the source code.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000438 *
Douglas Gregor1db19de2010-01-19 21:36:55 +0000439 * Use clang_getRangeStart() and clang_getRangeEnd() to retrieve the
440 * starting and end locations from a source range, respectively.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000441 */
442typedef struct {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000443 void *ptr_data;
444 unsigned begin_int_data;
445 unsigned end_int_data;
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000446} CXSourceRange;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000447
Douglas Gregor1db19de2010-01-19 21:36:55 +0000448/**
449 * \brief Retrieve the file, line, and column represented by the
450 * given source location.
451 *
452 * \param location the location within a source file that will be
453 * decomposed into its parts.
454 *
455 * \param file if non-NULL, will be set to the file to which the given
456 * source location points.
457 *
458 * \param line if non-NULL, will be set to the line to which the given
459 * source location points.
460 *
461 * \param column if non-NULL, will be set to the column to which the
462 * given source location points.
463 */
464CINDEX_LINKAGE void clang_getInstantiationLocation(CXSourceLocation location,
465 CXFile *file,
466 unsigned *line,
467 unsigned *column);
468
469/**
470 * \brief Retrieve a source location representing the first
471 * character within a source range.
472 */
473CINDEX_LINKAGE CXSourceLocation clang_getRangeStart(CXSourceRange range);
474
475/**
476 * \brief Retrieve a source location representing the last
477 * character within a source range.
478 */
479CINDEX_LINKAGE CXSourceLocation clang_getRangeEnd(CXSourceRange range);
480
Daniel Dunbar3aacd532010-01-06 06:51:48 +0000481/* clang_getDeclExtent() returns the physical extent of a declaration. The
482 * beginning line/column pair points to the start of the first token in the
Ted Kremenekd8210652010-01-06 23:43:31 +0000483 * declaration, and the ending line/column pair points to the last character in
484 * the last token of the declaration.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000485 */
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000486CINDEX_LINKAGE CXSourceRange clang_getDeclExtent(CXDecl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000487
Steve Naroff50398192009-08-28 15:28:48 +0000488/*
489 * CXCursor Operations.
490 */
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000491/**
492 Usage: clang_getCursor() will translate a source/line/column position
493 into an AST cursor (to derive semantic information from the source code).
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000494 */
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000495CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit,
496 const char *source_name,
497 unsigned line, unsigned column);
Ted Kremenek73885552009-11-17 19:28:59 +0000498
499CINDEX_LINKAGE CXCursor clang_getNullCursor(void);
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000500
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000501/* clang_getCursorUSR() returns the USR (if any) associated with entity referred to by the
502 * provided CXCursor object. */
503CINDEX_LINKAGE CXString clang_getCursorUSR(CXCursor);
504
John Thompson2e06fc82009-10-27 13:42:56 +0000505CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor);
506CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind);
507CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind);
Douglas Gregor97b98722010-01-19 23:20:36 +0000508CINDEX_LINKAGE unsigned clang_isExpression(enum CXCursorKind);
509CINDEX_LINKAGE unsigned clang_isStatement(enum CXCursorKind);
John Thompson2e06fc82009-10-27 13:42:56 +0000510CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000511CINDEX_LINKAGE unsigned clang_isTranslationUnit(enum CXCursorKind);
Ted Kremenek73885552009-11-17 19:28:59 +0000512CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor);
513
Steve Naroffef0cef62009-11-09 17:45:52 +0000514CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);
Steve Narofff334b4e2009-09-02 18:26:48 +0000515
Douglas Gregor98258af2010-01-18 22:46:11 +0000516/**
517 * \brief Retrieve the physical location of the source constructor referenced
518 * by the given cursor.
519 *
520 * The location of a declaration is typically the location of the name of that
521 * declaration, where the name of that declaration would occur if it is
522 * unnamed, or some keyword that introduces that particular declaration.
523 * The location of a reference is where that reference occurs within the
524 * source code.
525 */
526CINDEX_LINKAGE CXSourceLocation clang_getCursorLocation(CXCursor);
527
Douglas Gregorb6998662010-01-19 19:34:47 +0000528/**
529 * \brief Retrieve the physical extent of the source construct referenced by
Douglas Gregora7bde202010-01-19 00:34:46 +0000530 * the given cursor.
531 *
532 * The extent of a cursor starts with the file/line/column pointing at the
533 * first character within the source construct that the cursor refers to and
534 * ends with the last character withinin that source construct. For a
535 * declaration, the extent covers the declaration itself. For a reference,
536 * the extent covers the location of the reference (e.g., where the referenced
537 * entity was actually used).
538 */
539CINDEX_LINKAGE CXSourceRange clang_getCursorExtent(CXCursor);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000540
541/** \brief For a cursor that is a reference, retrieve a cursor representing the
542 * entity that it references.
543 *
544 * Reference cursors refer to other entities in the AST. For example, an
545 * Objective-C superclass reference cursor refers to an Objective-C class.
546 * This function produces the cursor for the Objective-C class from the
547 * cursor for the superclass reference. If the input cursor is a declaration or
548 * definition, it returns that declaration or definition unchanged.
549 * Othewise, returns the NULL cursor.
550 */
551CINDEX_LINKAGE CXCursor clang_getCursorReferenced(CXCursor);
Douglas Gregorb6998662010-01-19 19:34:47 +0000552
553/**
554 * \brief For a cursor that is either a reference to or a declaration
555 * of some entity, retrieve a cursor that describes the definition of
556 * that entity.
557 *
558 * Some entities can be declared multiple times within a translation
559 * unit, but only one of those declarations can also be a
560 * definition. For example, given:
561 *
562 * \code
563 * int f(int, int);
564 * int g(int x, int y) { return f(x, y); }
565 * int f(int a, int b) { return a + b; }
566 * int f(int, int);
567 * \endcode
568 *
569 * there are three declarations of the function "f", but only the
570 * second one is a definition. The clang_getCursorDefinition()
571 * function will take any cursor pointing to a declaration of "f"
572 * (the first or fourth lines of the example) or a cursor referenced
573 * that uses "f" (the call to "f' inside "g") and will return a
574 * declaration cursor pointing to the definition (the second "f"
575 * declaration).
576 *
577 * If given a cursor for which there is no corresponding definition,
578 * e.g., because there is no definition of that entity within this
579 * translation unit, returns a NULL cursor.
580 */
581CINDEX_LINKAGE CXCursor clang_getCursorDefinition(CXCursor);
582
583/**
584 * \brief Determine whether the declaration pointed to by this cursor
585 * is also a definition of that entity.
586 */
587CINDEX_LINKAGE unsigned clang_isCursorDefinition(CXCursor);
588
Steve Naroff4ade6d62009-09-23 17:52:52 +0000589/* for debug/testing */
John Thompson2e06fc82009-10-27 13:42:56 +0000590CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind);
591CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000592 const char **startBuf,
593 const char **endBuf,
594 unsigned *startLine,
595 unsigned *startColumn,
596 unsigned *endLine,
597 unsigned *endColumn);
Steve Naroff600866c2009-08-27 19:51:58 +0000598
Steve Naroff50398192009-08-28 15:28:48 +0000599/*
Chris Lattner459789b2009-09-16 20:18:54 +0000600 * If CXCursorKind == Cursor_Reference, then this will return the referenced
601 * declaration.
Steve Naroff50398192009-08-28 15:28:48 +0000602 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
603 */
John Thompson2e06fc82009-10-27 13:42:56 +0000604CINDEX_LINKAGE CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000605
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000606/**
607 * \brief A semantic string that describes a code-completion result.
608 *
609 * A semantic string that describes the formatting of a code-completion
610 * result as a single "template" of text that should be inserted into the
611 * source buffer when a particular code-completion result is selected.
612 * Each semantic string is made up of some number of "chunks", each of which
613 * contains some text along with a description of what that text means, e.g.,
614 * the name of the entity being referenced, whether the text chunk is part of
615 * the template, or whether it is a "placeholder" that the user should replace
616 * with actual code,of a specific kind. See \c CXCompletionChunkKind for a
617 * description of the different kinds of chunks.
618 */
619typedef void *CXCompletionString;
620
621/**
622 * \brief A single result of code completion.
623 */
624typedef struct {
625 /**
626 * \brief The kind of entity that this completion refers to.
627 *
628 * The cursor kind will be a macro, keyword, or a declaration (one of the
629 * *Decl cursor kinds), describing the entity that the completion is
630 * referring to.
631 *
632 * \todo In the future, we would like to provide a full cursor, to allow
633 * the client to extract additional information from declaration.
634 */
635 enum CXCursorKind CursorKind;
636
637 /**
638 * \brief The code-completion string that describes how to insert this
639 * code-completion result into the editing buffer.
640 */
641 CXCompletionString CompletionString;
642} CXCompletionResult;
643
644/**
645 * \brief Describes a single piece of text within a code-completion string.
646 *
647 * Each "chunk" within a code-completion string (\c CXCompletionString) is
648 * either a piece of text with a specific "kind" that describes how that text
649 * should be interpreted by the client or is another completion string.
650 */
651enum CXCompletionChunkKind {
652 /**
653 * \brief A code-completion string that describes "optional" text that
654 * could be a part of the template (but is not required).
655 *
656 * The Optional chunk is the only kind of chunk that has a code-completion
657 * string for its representation, which is accessible via
658 * \c clang_getCompletionChunkCompletionString(). The code-completion string
659 * describes an additional part of the template that is completely optional.
660 * For example, optional chunks can be used to describe the placeholders for
661 * arguments that match up with defaulted function parameters, e.g. given:
662 *
663 * \code
664 * void f(int x, float y = 3.14, double z = 2.71828);
665 * \endcode
666 *
667 * The code-completion string for this function would contain:
668 * - a TypedText chunk for "f".
669 * - a LeftParen chunk for "(".
670 * - a Placeholder chunk for "int x"
671 * - an Optional chunk containing the remaining defaulted arguments, e.g.,
672 * - a Comma chunk for ","
673 * - a Placeholder chunk for "float x"
674 * - an Optional chunk containing the last defaulted argument:
675 * - a Comma chunk for ","
676 * - a Placeholder chunk for "double z"
677 * - a RightParen chunk for ")"
678 *
679 * There are many ways two handle Optional chunks. Two simple approaches are:
680 * - Completely ignore optional chunks, in which case the template for the
681 * function "f" would only include the first parameter ("int x").
682 * - Fully expand all optional chunks, in which case the template for the
683 * function "f" would have all of the parameters.
684 */
685 CXCompletionChunk_Optional,
686 /**
687 * \brief Text that a user would be expected to type to get this
688 * code-completion result.
689 *
690 * There will be exactly one "typed text" chunk in a semantic string, which
691 * will typically provide the spelling of a keyword or the name of a
692 * declaration that could be used at the current code point. Clients are
693 * expected to filter the code-completion results based on the text in this
694 * chunk.
695 */
696 CXCompletionChunk_TypedText,
697 /**
698 * \brief Text that should be inserted as part of a code-completion result.
699 *
700 * A "text" chunk represents text that is part of the template to be
701 * inserted into user code should this particular code-completion result
702 * be selected.
703 */
704 CXCompletionChunk_Text,
705 /**
706 * \brief Placeholder text that should be replaced by the user.
707 *
708 * A "placeholder" chunk marks a place where the user should insert text
709 * into the code-completion template. For example, placeholders might mark
710 * the function parameters for a function declaration, to indicate that the
711 * user should provide arguments for each of those parameters. The actual
712 * text in a placeholder is a suggestion for the text to display before
713 * the user replaces the placeholder with real code.
714 */
715 CXCompletionChunk_Placeholder,
716 /**
717 * \brief Informative text that should be displayed but never inserted as
718 * part of the template.
719 *
720 * An "informative" chunk contains annotations that can be displayed to
721 * help the user decide whether a particular code-completion result is the
722 * right option, but which is not part of the actual template to be inserted
723 * by code completion.
724 */
725 CXCompletionChunk_Informative,
726 /**
727 * \brief Text that describes the current parameter when code-completion is
728 * referring to function call, message send, or template specialization.
729 *
730 * A "current parameter" chunk occurs when code-completion is providing
731 * information about a parameter corresponding to the argument at the
732 * code-completion point. For example, given a function
733 *
734 * \code
735 * int add(int x, int y);
736 * \endcode
737 *
738 * and the source code \c add(, where the code-completion point is after the
739 * "(", the code-completion string will contain a "current parameter" chunk
740 * for "int x", indicating that the current argument will initialize that
741 * parameter. After typing further, to \c add(17, (where the code-completion
742 * point is after the ","), the code-completion string will contain a
743 * "current paremeter" chunk to "int y".
744 */
745 CXCompletionChunk_CurrentParameter,
746 /**
747 * \brief A left parenthesis ('('), used to initiate a function call or
748 * signal the beginning of a function parameter list.
749 */
750 CXCompletionChunk_LeftParen,
751 /**
752 * \brief A right parenthesis (')'), used to finish a function call or
753 * signal the end of a function parameter list.
754 */
755 CXCompletionChunk_RightParen,
756 /**
757 * \brief A left bracket ('[').
758 */
759 CXCompletionChunk_LeftBracket,
760 /**
761 * \brief A right bracket (']').
762 */
763 CXCompletionChunk_RightBracket,
764 /**
765 * \brief A left brace ('{').
766 */
767 CXCompletionChunk_LeftBrace,
768 /**
769 * \brief A right brace ('}').
770 */
771 CXCompletionChunk_RightBrace,
772 /**
773 * \brief A left angle bracket ('<').
774 */
775 CXCompletionChunk_LeftAngle,
776 /**
777 * \brief A right angle bracket ('>').
778 */
779 CXCompletionChunk_RightAngle,
780 /**
781 * \brief A comma separator (',').
782 */
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000783 CXCompletionChunk_Comma,
784 /**
785 * \brief Text that specifies the result type of a given result.
786 *
787 * This special kind of informative chunk is not meant to be inserted into
788 * the text buffer. Rather, it is meant to illustrate the type that an
789 * expression using the given completion string would have.
790 */
Douglas Gregor01dfea02010-01-10 23:08:15 +0000791 CXCompletionChunk_ResultType,
792 /**
793 * \brief A colon (':').
794 */
795 CXCompletionChunk_Colon,
796 /**
797 * \brief A semicolon (';').
798 */
799 CXCompletionChunk_SemiColon,
800 /**
801 * \brief An '=' sign.
802 */
803 CXCompletionChunk_Equal,
804 /**
805 * Horizontal space (' ').
806 */
807 CXCompletionChunk_HorizontalSpace,
808 /**
809 * Vertical space ('\n'), after which it is generally a good idea to
810 * perform indentation.
811 */
812 CXCompletionChunk_VerticalSpace
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000813};
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000814
815/**
816 * \brief Determine the kind of a particular chunk within a completion string.
817 *
818 * \param completion_string the completion string to query.
819 *
820 * \param chunk_number the 0-based index of the chunk in the completion string.
821 *
822 * \returns the kind of the chunk at the index \c chunk_number.
823 */
824CINDEX_LINKAGE enum CXCompletionChunkKind
825clang_getCompletionChunkKind(CXCompletionString completion_string,
826 unsigned chunk_number);
827
828/**
829 * \brief Retrieve the text associated with a particular chunk within a
830 * completion string.
831 *
832 * \param completion_string the completion string to query.
833 *
834 * \param chunk_number the 0-based index of the chunk in the completion string.
835 *
836 * \returns the text associated with the chunk at index \c chunk_number.
837 */
838CINDEX_LINKAGE const char *
839clang_getCompletionChunkText(CXCompletionString completion_string,
840 unsigned chunk_number);
841
842/**
843 * \brief Retrieve the completion string associated with a particular chunk
844 * within a completion string.
845 *
846 * \param completion_string the completion string to query.
847 *
848 * \param chunk_number the 0-based index of the chunk in the completion string.
849 *
850 * \returns the completion string associated with the chunk at index
851 * \c chunk_number, or NULL if that chunk is not represented by a completion
852 * string.
853 */
854CINDEX_LINKAGE CXCompletionString
855clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
856 unsigned chunk_number);
857
858/**
859 * \brief Retrieve the number of chunks in the given code-completion string.
860 */
861CINDEX_LINKAGE unsigned
862clang_getNumCompletionChunks(CXCompletionString completion_string);
863
864/**
Douglas Gregorec6762c2009-12-18 16:20:58 +0000865 * \brief Contains the results of code-completion.
866 *
867 * This data structure contains the results of code completion, as
868 * produced by \c clang_codeComplete. Its contents must be freed by
869 * \c clang_disposeCodeCompleteResults.
870 */
871typedef struct {
872 /**
873 * \brief The code-completion results.
874 */
875 CXCompletionResult *Results;
876
877 /**
878 * \brief The number of code-completion results stored in the
879 * \c Results array.
880 */
881 unsigned NumResults;
882} CXCodeCompleteResults;
883
884/**
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000885 * \brief Perform code completion at a given location in a source file.
886 *
887 * This function performs code completion at a particular file, line, and
888 * column within source code, providing results that suggest potential
889 * code snippets based on the context of the completion. The basic model
890 * for code completion is that Clang will parse a complete source file,
891 * performing syntax checking up to the location where code-completion has
892 * been requested. At that point, a special code-completion token is passed
893 * to the parser, which recognizes this token and determines, based on the
894 * current location in the C/Objective-C/C++ grammar and the state of
895 * semantic analysis, what completions to provide. These completions are
Douglas Gregorec6762c2009-12-18 16:20:58 +0000896 * returned via a new \c CXCodeCompleteResults structure.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000897 *
898 * Code completion itself is meant to be triggered by the client when the
899 * user types punctuation characters or whitespace, at which point the
900 * code-completion location will coincide with the cursor. For example, if \c p
901 * is a pointer, code-completion might be triggered after the "-" and then
902 * after the ">" in \c p->. When the code-completion location is afer the ">",
903 * the completion results will provide, e.g., the members of the struct that
904 * "p" points to. The client is responsible for placing the cursor at the
905 * beginning of the token currently being typed, then filtering the results
906 * based on the contents of the token. For example, when code-completing for
907 * the expression \c p->get, the client should provide the location just after
908 * the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the
909 * client can filter the results based on the current token text ("get"), only
910 * showing those results that start with "get". The intent of this interface
Douglas Gregorec6762c2009-12-18 16:20:58 +0000911 * is to separate the relatively high-latency acquisition of code-completion
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000912 * results from the filtering of results on a per-character basis, which must
913 * have a lower latency.
914 *
915 * \param CIdx the \c CXIndex instance that will be used to perform code
916 * completion.
917 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000918 * \param source_filename the name of the source file that should be parsed to
919 * perform code-completion. This source file must be the same as or include the
920 * filename described by \p complete_filename, or no code-completion results
921 * will be produced. NOTE: One can also specify NULL for this argument if the
922 * source file is included in command_line_args.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000923 *
924 * \param num_command_line_args the number of command-line arguments stored in
925 * \p command_line_args.
926 *
927 * \param command_line_args the command-line arguments to pass to the Clang
928 * compiler to build the given source file. This should include all of the
929 * necessary include paths, language-dialect switches, precompiled header
930 * includes, etc., but should not include any information specific to
931 * code completion.
932 *
Douglas Gregor735df882009-12-02 09:21:34 +0000933 * \param num_unsaved_files the number of unsaved file entries in \p
934 * unsaved_files.
935 *
936 * \param unsaved_files the files that have not yet been saved to disk
937 * but may be required for code completion, including the contents of
938 * those files.
939 *
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000940 * \param complete_filename the name of the source file where code completion
941 * should be performed. In many cases, this name will be the same as the
942 * source filename. However, the completion filename may also be a file
943 * included by the source file, which is required when producing
944 * code-completion results for a header.
945 *
946 * \param complete_line the line at which code-completion should occur.
947 *
948 * \param complete_column the column at which code-completion should occur.
949 * Note that the column should point just after the syntactic construct that
950 * initiated code completion, and not in the middle of a lexical token.
951 *
Douglas Gregorec6762c2009-12-18 16:20:58 +0000952 * \returns if successful, a new CXCodeCompleteResults structure
953 * containing code-completion results, which should eventually be
954 * freed with \c clang_disposeCodeCompleteResults(). If code
955 * completion fails, returns NULL.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000956 */
Douglas Gregorec6762c2009-12-18 16:20:58 +0000957CINDEX_LINKAGE
958CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
959 const char *source_filename,
960 int num_command_line_args,
961 const char **command_line_args,
962 unsigned num_unsaved_files,
963 struct CXUnsavedFile *unsaved_files,
964 const char *complete_filename,
965 unsigned complete_line,
966 unsigned complete_column);
967
968/**
969 * \brief Free the given set of code-completion results.
970 */
971CINDEX_LINKAGE
972void clang_disposeCodeCompleteResults(CXCodeCompleteResults *Results);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000973
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000974#ifdef __cplusplus
975}
976#endif
977#endif
978