blob: a42ec0669efdab188090c86cf8d7912850aae840 [file] [log] [blame]
Ted Kremenekb60d87c2009-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 Naroff6231f182009-10-27 14:35:18 +000019#include <sys/stat.h>
Chandler Carruthaacafe52009-12-17 09:27:29 +000020#include <time.h>
Steve Naroff6231f182009-10-27 14:35:18 +000021
Ted Kremenekb60d87c2009-08-26 22:36:44 +000022#ifdef __cplusplus
23extern "C" {
24#endif
25
Steve Naroff6231f182009-10-27 14:35:18 +000026/* MSVC DLL import/export. */
John Thompsonde258b52009-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
Douglas Gregor52606ff2010-01-20 01:10:47 +000037/** \defgroup CINDEX C Interface to Clang
38 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +000039 * The C Interface to Clang provides a relatively small API that exposes
Douglas Gregorc8e390c2010-01-20 22:45:41 +000040 * facilities for parsing source code into an abstract syntax tree (AST),
41 * loading already-parsed ASTs, traversing the AST, associating
42 * physical source locations with elements within the AST, and other
43 * facilities that support Clang-based development tools.
Douglas Gregor52606ff2010-01-20 01:10:47 +000044 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +000045 * This C interface to Clang will never provide all of the information
Douglas Gregorc8e390c2010-01-20 22:45:41 +000046 * representation stored in Clang's C++ AST, nor should it: the intent is to
47 * maintain an API that is relatively stable from one release to the next,
48 * providing only the basic functionality needed to support development tools.
Daniel Dunbar62ebf252010-01-24 02:54:26 +000049 *
50 * To avoid namespace pollution, data types are prefixed with "CX" and
Douglas Gregorc8e390c2010-01-20 22:45:41 +000051 * functions are prefixed with "clang_".
Douglas Gregor52606ff2010-01-20 01:10:47 +000052 *
53 * @{
54 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +000055
Douglas Gregor802f12f2010-01-20 22:28:27 +000056/**
57 * \brief An "index" that consists of a set of translation units that would
58 * typically be linked together into an executable or library.
59 */
60typedef void *CXIndex;
Steve Naroffd5e8e862009-08-27 19:51:58 +000061
Douglas Gregor802f12f2010-01-20 22:28:27 +000062/**
63 * \brief A single translation unit, which resides in an index.
64 */
Steve Naroffa1c72842009-08-28 15:28:48 +000065typedef void *CXTranslationUnit; /* A translation unit instance. */
Steve Naroffd5e8e862009-08-27 19:51:58 +000066
Douglas Gregor802f12f2010-01-20 22:28:27 +000067/**
Douglas Gregor6007cf22010-01-22 22:29:16 +000068 * \brief Opaque pointer representing client data that will be passed through
69 * to various callbacks and visitors.
Douglas Gregor802f12f2010-01-20 22:28:27 +000070 */
Douglas Gregor6007cf22010-01-22 22:29:16 +000071typedef void *CXClientData;
Daniel Dunbar62ebf252010-01-24 02:54:26 +000072
Douglas Gregor9485bf92009-12-02 09:21:34 +000073/**
74 * \brief Provides the contents of a file that has not yet been saved to disk.
75 *
76 * Each CXUnsavedFile instance provides the name of a file on the
77 * system along with the current contents of that file that have not
78 * yet been saved to disk.
79 */
80struct CXUnsavedFile {
Daniel Dunbar62ebf252010-01-24 02:54:26 +000081 /**
82 * \brief The file whose contents have not yet been saved.
Douglas Gregor9485bf92009-12-02 09:21:34 +000083 *
84 * This file must already exist in the file system.
85 */
86 const char *Filename;
87
Daniel Dunbar62ebf252010-01-24 02:54:26 +000088 /**
Douglas Gregor9485bf92009-12-02 09:21:34 +000089 * \brief A null-terminated buffer containing the unsaved contents
90 * of this file.
91 */
92 const char *Contents;
93
94 /**
95 * \brief The length of the unsaved contents of this buffer, not
96 * counting the NULL at the end of the buffer.
97 */
98 unsigned long Length;
99};
100
Douglas Gregor802f12f2010-01-20 22:28:27 +0000101/**
Douglas Gregor802f12f2010-01-20 22:28:27 +0000102 * \defgroup CINDEX_STRING String manipulation routines
103 *
104 * @{
105 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000106
Douglas Gregor802f12f2010-01-20 22:28:27 +0000107/**
108 * \brief A character string.
109 *
110 * The \c CXString type is used to return strings from the interface when
111 * the ownership of that string might different from one call to the next.
112 * Use \c clang_getCString() to retrieve the string data and, once finished
113 * with the string data, call \c clang_disposeString() to free the string.
Steve Naroff8675d5c2009-11-09 17:45:52 +0000114 */
115typedef struct {
116 const char *Spelling;
117 /* A 1 value indicates the clang_ indexing API needed to allocate the string
118 (and it must be freed by clang_disposeString()). */
119 int MustFreeString;
120} CXString;
121
Douglas Gregor802f12f2010-01-20 22:28:27 +0000122/**
123 * \brief Retrieve the character data associated with the given string.
124 */
Steve Naroff8675d5c2009-11-09 17:45:52 +0000125CINDEX_LINKAGE const char *clang_getCString(CXString string);
126
Douglas Gregor802f12f2010-01-20 22:28:27 +0000127/**
128 * \brief Free the given string,
129 */
Steve Naroff8675d5c2009-11-09 17:45:52 +0000130CINDEX_LINKAGE void clang_disposeString(CXString string);
131
Douglas Gregor802f12f2010-01-20 22:28:27 +0000132/**
133 * @}
134 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000135
136/**
Steve Naroff531e2842009-10-20 14:46:24 +0000137 * \brief clang_createIndex() provides a shared context for creating
138 * translation units. It provides two options:
139 *
140 * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local"
141 * declarations (when loading any new translation units). A "local" declaration
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000142 * is one that belongs in the translation unit itself and not in a precompiled
Steve Naroff531e2842009-10-20 14:46:24 +0000143 * header that was used by the translation unit. If zero, all declarations
144 * will be enumerated.
145 *
Steve Naroffbb4568a2009-10-20 16:36:34 +0000146 * Here is an example:
147 *
Douglas Gregorba965fb2010-01-28 00:56:43 +0000148 * // excludeDeclsFromPCH = 1
149 * Idx = clang_createIndex(1);
Steve Naroffbb4568a2009-10-20 16:36:34 +0000150 *
151 * // IndexTest.pch was produced with the following command:
152 * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
153 * TU = clang_createTranslationUnit(Idx, "IndexTest.pch");
154 *
155 * // This will load all the symbols from 'IndexTest.pch'
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000156 * clang_visitChildren(clang_getTranslationUnitCursor(TU),
Douglas Gregor990b5762010-01-20 21:37:00 +0000157 * TranslationUnitVisitor, 0);
Steve Naroffbb4568a2009-10-20 16:36:34 +0000158 * clang_disposeTranslationUnit(TU);
159 *
160 * // This will load all the symbols from 'IndexTest.c', excluding symbols
161 * // from 'IndexTest.pch'.
Daniel Dunbard0159262010-01-25 00:43:14 +0000162 * char *args[] = { "-Xclang", "-include-pch=IndexTest.pch" };
163 * TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args,
164 * 0, 0);
Douglas Gregorfed36b12010-01-20 23:57:43 +0000165 * clang_visitChildren(clang_getTranslationUnitCursor(TU),
166 * TranslationUnitVisitor, 0);
Steve Naroffbb4568a2009-10-20 16:36:34 +0000167 * clang_disposeTranslationUnit(TU);
168 *
169 * This process of creating the 'pch', loading it separately, and using it (via
170 * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks
171 * (which gives the indexer the same performance benefit as the compiler).
Steve Naroff531e2842009-10-20 14:46:24 +0000172 */
Douglas Gregorba965fb2010-01-28 00:56:43 +0000173CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH);
Douglas Gregor408bb742010-02-08 23:03:06 +0000174
175/**
176 * \brief Destroy the given index.
177 *
178 * The index must not be destroyed until all of the translation units created
179 * within that index have been destroyed.
180 */
Daniel Dunbar11089662009-12-03 01:54:28 +0000181CINDEX_LINKAGE void clang_disposeIndex(CXIndex index);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000182
183/**
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000184 * \brief Request that AST's be generated externally for API calls which parse
Daniel Dunbar11089662009-12-03 01:54:28 +0000185 * source code on the fly, e.g. \see createTranslationUnitFromSourceFile.
186 *
187 * Note: This is for debugging purposes only, and may be removed at a later
188 * date.
189 *
190 * \param index - The index to update.
191 * \param value - The new flag value.
192 */
193CINDEX_LINKAGE void clang_setUseExternalASTGeneration(CXIndex index,
194 int value);
Douglas Gregord2fc7272010-01-20 00:23:15 +0000195/**
Douglas Gregor6007cf22010-01-22 22:29:16 +0000196 * \defgroup CINDEX_FILES File manipulation routines
Douglas Gregorc8e390c2010-01-20 22:45:41 +0000197 *
198 * @{
199 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000200
Douglas Gregorc8e390c2010-01-20 22:45:41 +0000201/**
202 * \brief A particular source file that is part of a translation unit.
203 */
204typedef void *CXFile;
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000205
Douglas Gregorc8e390c2010-01-20 22:45:41 +0000206
207/**
208 * \brief Retrieve the complete file and path name of the given file.
Steve Naroff6231f182009-10-27 14:35:18 +0000209 */
Douglas Gregor249c1212009-10-31 15:48:08 +0000210CINDEX_LINKAGE const char *clang_getFileName(CXFile SFile);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000211
Douglas Gregorc8e390c2010-01-20 22:45:41 +0000212/**
213 * \brief Retrieve the last modification time of the given file.
214 */
Douglas Gregor249c1212009-10-31 15:48:08 +0000215CINDEX_LINKAGE time_t clang_getFileTime(CXFile SFile);
Steve Naroff6231f182009-10-27 14:35:18 +0000216
Douglas Gregor49c4baf2010-01-18 22:13:09 +0000217/**
Douglas Gregor816fd362010-01-22 21:44:22 +0000218 * \brief Retrieve a file handle within the given translation unit.
219 *
220 * \param tu the translation unit
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000221 *
Douglas Gregor816fd362010-01-22 21:44:22 +0000222 * \param file_name the name of the file.
223 *
224 * \returns the file handle for the named file in the translation unit \p tu,
225 * or a NULL file handle if the file was not a part of this translation unit.
226 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000227CINDEX_LINKAGE CXFile clang_getFile(CXTranslationUnit tu,
Douglas Gregor816fd362010-01-22 21:44:22 +0000228 const char *file_name);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000229
Douglas Gregor816fd362010-01-22 21:44:22 +0000230/**
Douglas Gregorc8e390c2010-01-20 22:45:41 +0000231 * @}
232 */
233
234/**
235 * \defgroup CINDEX_LOCATIONS Physical source locations
236 *
237 * Clang represents physical source locations in its abstract syntax tree in
238 * great detail, with file, line, and column information for the majority of
239 * the tokens parsed in the source code. These data types and functions are
240 * used to represent source location information, either for a particular
241 * point in the program or for a range of points in the program, and extract
242 * specific location information from those data types.
243 *
244 * @{
245 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000246
Douglas Gregorc8e390c2010-01-20 22:45:41 +0000247/**
Douglas Gregor4f46e782010-01-19 21:36:55 +0000248 * \brief Identifies a specific source location within a translation
249 * unit.
250 *
251 * Use clang_getInstantiationLocation() to map a source location to a
252 * particular file, line, and column.
Douglas Gregor49c4baf2010-01-18 22:13:09 +0000253 */
254typedef struct {
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000255 void *ptr_data[2];
Douglas Gregor4f46e782010-01-19 21:36:55 +0000256 unsigned int_data;
Douglas Gregor49c4baf2010-01-18 22:13:09 +0000257} CXSourceLocation;
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000258
Douglas Gregor49c4baf2010-01-18 22:13:09 +0000259/**
Douglas Gregor4f46e782010-01-19 21:36:55 +0000260 * \brief Identifies a range of source locations in the source code.
Douglas Gregor49c4baf2010-01-18 22:13:09 +0000261 *
Douglas Gregor4f46e782010-01-19 21:36:55 +0000262 * Use clang_getRangeStart() and clang_getRangeEnd() to retrieve the
263 * starting and end locations from a source range, respectively.
Douglas Gregor49c4baf2010-01-18 22:13:09 +0000264 */
265typedef struct {
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000266 void *ptr_data[2];
Douglas Gregor4f46e782010-01-19 21:36:55 +0000267 unsigned begin_int_data;
268 unsigned end_int_data;
Douglas Gregor49c4baf2010-01-18 22:13:09 +0000269} CXSourceRange;
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000270
Douglas Gregor4f46e782010-01-19 21:36:55 +0000271/**
Douglas Gregor816fd362010-01-22 21:44:22 +0000272 * \brief Retrieve a NULL (invalid) source location.
273 */
274CINDEX_LINKAGE CXSourceLocation clang_getNullLocation();
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000275
Douglas Gregor816fd362010-01-22 21:44:22 +0000276/**
277 * \determine Determine whether two source locations, which must refer into
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000278 * the same translation unit, refer to exactly the same point in the source
Douglas Gregor816fd362010-01-22 21:44:22 +0000279 * code.
280 *
281 * \returns non-zero if the source locations refer to the same location, zero
282 * if they refer to different locations.
283 */
284CINDEX_LINKAGE unsigned clang_equalLocations(CXSourceLocation loc1,
285 CXSourceLocation loc2);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000286
Douglas Gregor816fd362010-01-22 21:44:22 +0000287/**
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000288 * \brief Retrieves the source location associated with a given file/line/column
289 * in a particular translation unit.
Douglas Gregor816fd362010-01-22 21:44:22 +0000290 */
291CINDEX_LINKAGE CXSourceLocation clang_getLocation(CXTranslationUnit tu,
292 CXFile file,
293 unsigned line,
294 unsigned column);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000295
Douglas Gregor816fd362010-01-22 21:44:22 +0000296/**
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000297 * \brief Retrieve a NULL (invalid) source range.
298 */
299CINDEX_LINKAGE CXSourceRange clang_getNullRange();
300
301/**
Douglas Gregor816fd362010-01-22 21:44:22 +0000302 * \brief Retrieve a source range given the beginning and ending source
303 * locations.
304 */
305CINDEX_LINKAGE CXSourceRange clang_getRange(CXSourceLocation begin,
306 CXSourceLocation end);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000307
Douglas Gregor816fd362010-01-22 21:44:22 +0000308/**
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000309 * \brief Retrieve the file, line, column, and offset represented by
310 * the given source location.
Douglas Gregor4f46e782010-01-19 21:36:55 +0000311 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000312 * \param location the location within a source file that will be decomposed
313 * into its parts.
Douglas Gregor4f46e782010-01-19 21:36:55 +0000314 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000315 * \param file [out] if non-NULL, will be set to the file to which the given
Douglas Gregor4f46e782010-01-19 21:36:55 +0000316 * source location points.
317 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000318 * \param line [out] if non-NULL, will be set to the line to which the given
Douglas Gregor4f46e782010-01-19 21:36:55 +0000319 * source location points.
320 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000321 * \param column [out] if non-NULL, will be set to the column to which the given
322 * source location points.
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000323 *
324 * \param offset [out] if non-NULL, will be set to the offset into the
325 * buffer to which the given source location points.
Douglas Gregor4f46e782010-01-19 21:36:55 +0000326 */
327CINDEX_LINKAGE void clang_getInstantiationLocation(CXSourceLocation location,
328 CXFile *file,
329 unsigned *line,
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000330 unsigned *column,
331 unsigned *offset);
Douglas Gregor47751d62010-01-26 03:07:15 +0000332
333/**
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000334 * \brief Retrieve a source location representing the first character within a
335 * source range.
Douglas Gregor4f46e782010-01-19 21:36:55 +0000336 */
337CINDEX_LINKAGE CXSourceLocation clang_getRangeStart(CXSourceRange range);
338
339/**
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000340 * \brief Retrieve a source location representing the last character within a
341 * source range.
Douglas Gregor4f46e782010-01-19 21:36:55 +0000342 */
343CINDEX_LINKAGE CXSourceLocation clang_getRangeEnd(CXSourceRange range);
344
Douglas Gregorc8e390c2010-01-20 22:45:41 +0000345/**
346 * @}
347 */
Douglas Gregor6007cf22010-01-22 22:29:16 +0000348
349/**
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000350 * \defgroup CINDEX_DIAG Diagnostic reporting
351 *
352 * @{
353 */
354
355/**
356 * \brief Describes the severity of a particular diagnostic.
357 */
358enum CXDiagnosticSeverity {
359 /**
360 * \brief A diagnostic that has been suppressed, e.g., by a command-line
361 * option.
362 */
363 CXDiagnostic_Ignored = 0,
364
365 /**
366 * \brief This diagnostic is a note that should be attached to the
367 * previous (non-note) diagnostic.
368 */
369 CXDiagnostic_Note = 1,
370
371 /**
372 * \brief This diagnostic indicates suspicious code that may not be
373 * wrong.
374 */
375 CXDiagnostic_Warning = 2,
376
377 /**
378 * \brief This diagnostic indicates that the code is ill-formed.
379 */
380 CXDiagnostic_Error = 3,
381
382 /**
383 * \brief This diagnostic indicates that the code is ill-formed such
384 * that future parser recovery is unlikely to produce useful
385 * results.
386 */
387 CXDiagnostic_Fatal = 4
388};
389
390/**
391 * \brief Describes the kind of fix-it hint expressed within a
392 * diagnostic.
393 */
394enum CXFixItKind {
395 /**
396 * \brief A fix-it hint that inserts code at a particular position.
397 */
398 CXFixIt_Insertion = 0,
399
400 /**
401 * \brief A fix-it hint that removes code within a range.
402 */
403 CXFixIt_Removal = 1,
404
405 /**
406 * \brief A fix-it hint that replaces the code within a range with another
407 * string.
408 */
409 CXFixIt_Replacement = 2
410};
411
412/**
413 * \brief A single diagnostic, containing the diagnostic's severity,
414 * location, text, source ranges, and fix-it hints.
415 */
416typedef void *CXDiagnostic;
417
418/**
419 * \brief Callback function invoked for each diagnostic emitted during
420 * translation.
421 *
422 * \param Diagnostic the diagnostic emitted during translation. This
423 * diagnostic pointer is only valid during the execution of the
424 * callback.
425 *
426 * \param ClientData the callback client data.
427 */
428typedef void (*CXDiagnosticCallback)(CXDiagnostic Diagnostic,
429 CXClientData ClientData);
430
431/**
432 * \brief Determine the severity of the given diagnostic.
433 */
434CINDEX_LINKAGE enum CXDiagnosticSeverity
435clang_getDiagnosticSeverity(CXDiagnostic);
436
437/**
438 * \brief Retrieve the source location of the given diagnostic.
439 *
440 * This location is where Clang would print the caret ('^') when
441 * displaying the diagnostic on the command line.
442 */
443CINDEX_LINKAGE CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic);
444
445/**
446 * \brief Retrieve the text of the given diagnostic.
447 */
448CINDEX_LINKAGE CXString clang_getDiagnosticSpelling(CXDiagnostic);
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000449
450/**
451 * \brief Determine the number of source ranges associated with the given
452 * diagnostic.
453 */
454CINDEX_LINKAGE unsigned clang_getDiagnosticNumRanges(CXDiagnostic);
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000455
456/**
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000457 * \brief Retrieve a source range associated with the diagnostic.
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000458 *
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000459 * A diagnostic's source ranges highlight important elements in the source
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000460 * code. On the command line, Clang displays source ranges by
461 * underlining them with '~' characters.
462 *
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000463 * \param Diagnostic the diagnostic whose range is being extracted.
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000464 *
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000465 * \param Range the zero-based index specifying which range to
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000466 *
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000467 * \returns the requested source range.
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000468 */
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000469CINDEX_LINKAGE CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diagnostic,
470 unsigned Range);
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000471
472/**
473 * \brief Determine the number of fix-it hints associated with the
474 * given diagnostic.
475 */
476CINDEX_LINKAGE unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diagnostic);
477
478/**
479 * \brief Retrieve the kind of the given fix-it.
480 *
481 * \param Diagnostic the diagnostic whose fix-its are being queried.
482 *
483 * \param FixIt the zero-based index of the fix-it to query.
484 */
485CINDEX_LINKAGE enum CXFixItKind
486clang_getDiagnosticFixItKind(CXDiagnostic Diagnostic, unsigned FixIt);
487
488/**
489 * \brief Retrieve the insertion information for an insertion fix-it.
490 *
491 * For a fix-it that describes an insertion into a text buffer,
492 * retrieve the source location where the text should be inserted and
493 * the text to be inserted.
494 *
495 * \param Diagnostic the diagnostic whose fix-its are being queried.
496 *
497 * \param FixIt the zero-based index of the insertion fix-it.
498 *
499 * \param Location will be set to the location where text should be
500 * inserted.
501 *
502 * \returns the text string to insert at the given location.
503 */
504CINDEX_LINKAGE CXString
505clang_getDiagnosticFixItInsertion(CXDiagnostic Diagnostic, unsigned FixIt,
506 CXSourceLocation *Location);
507
508/**
509 * \brief Retrieve the removal information for a removal fix-it.
510 *
511 * For a fix-it that describes a removal from a text buffer, retrieve
512 * the source range that should be removed.
513 *
514 * \param Diagnostic the diagnostic whose fix-its are being queried.
515 *
516 * \param FixIt the zero-based index of the removal fix-it.
517 *
518 * \returns a source range describing the text that should be removed
519 * from the buffer.
520 */
521CINDEX_LINKAGE CXSourceRange
522clang_getDiagnosticFixItRemoval(CXDiagnostic Diagnostic, unsigned FixIt);
523
524/**
525 * \brief Retrieve the replacement information for an replacement fix-it.
526 *
527 * For a fix-it that describes replacement of text in the text buffer
528 * with alternative text.
529 *
530 * \param Diagnostic the diagnostic whose fix-its are being queried.
531 *
532 * \param FixIt the zero-based index of the replacement fix-it.
533 *
534 * \param Range will be set to the source range whose text should be
535 * replaced with the returned text.
536 *
537 * \returns the text string to use as replacement text.
538 */
539CINDEX_LINKAGE CXString
540clang_getDiagnosticFixItReplacement(CXDiagnostic Diagnostic, unsigned FixIt,
541 CXSourceRange *Range);
542
543/**
544 * @}
545 */
546
547/**
548 * \defgroup CINDEX_TRANSLATION_UNIT Translation unit manipulation
549 *
550 * The routines in this group provide the ability to create and destroy
551 * translation units from files, either by parsing the contents of the files or
552 * by reading in a serialized representation of a translation unit.
553 *
554 * @{
555 */
556
557/**
558 * \brief Get the original translation unit source file name.
559 */
560CINDEX_LINKAGE CXString
561clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
562
563/**
564 * \brief Return the CXTranslationUnit for a given source file and the provided
565 * command line arguments one would pass to the compiler.
566 *
567 * Note: The 'source_filename' argument is optional. If the caller provides a
568 * NULL pointer, the name of the source file is expected to reside in the
569 * specified command line arguments.
570 *
571 * Note: When encountered in 'clang_command_line_args', the following options
572 * are ignored:
573 *
574 * '-c'
575 * '-emit-ast'
576 * '-fsyntax-only'
577 * '-o <output file>' (both '-o' and '<output file>' are ignored)
578 *
579 *
580 * \param source_filename - The name of the source file to load, or NULL if the
581 * source file is included in clang_command_line_args.
582 *
583 * \param num_unsaved_files the number of unsaved file entries in \p
584 * unsaved_files.
585 *
586 * \param unsaved_files the files that have not yet been saved to disk
587 * but may be required for code completion, including the contents of
588 * those files.
589 *
590 * \param diag_callback callback function that will receive any diagnostics
591 * emitted while processing this source file. If NULL, diagnostics will be
592 * suppressed.
593 *
594 * \param diag_client_data client data that will be passed to the diagnostic
595 * callback function.
596 */
597CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile(
598 CXIndex CIdx,
599 const char *source_filename,
600 int num_clang_command_line_args,
601 const char **clang_command_line_args,
602 unsigned num_unsaved_files,
603 struct CXUnsavedFile *unsaved_files,
604 CXDiagnosticCallback diag_callback,
605 CXClientData diag_client_data);
606
607/**
608 * \brief Create a translation unit from an AST file (-emit-ast).
609 */
610CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(CXIndex,
611 const char *ast_filename,
612 CXDiagnosticCallback diag_callback,
613 CXClientData diag_client_data);
614
615/**
616 * \brief Destroy the specified CXTranslationUnit object.
617 */
618CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
619
620/**
621 * @}
622 */
623
624/**
Douglas Gregor6007cf22010-01-22 22:29:16 +0000625 * \brief Describes the kind of entity that a cursor refers to.
626 */
627enum CXCursorKind {
628 /* Declarations */
629 CXCursor_FirstDecl = 1,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000630 /**
Douglas Gregor6007cf22010-01-22 22:29:16 +0000631 * \brief A declaration whose specific kind is not exposed via this
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000632 * interface.
Douglas Gregor6007cf22010-01-22 22:29:16 +0000633 *
634 * Unexposed declarations have the same operations as any other kind
635 * of declaration; one can extract their location information,
636 * spelling, find their definitions, etc. However, the specific kind
637 * of the declaration is not reported.
638 */
639 CXCursor_UnexposedDecl = 1,
640 /** \brief A C or C++ struct. */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000641 CXCursor_StructDecl = 2,
Douglas Gregor6007cf22010-01-22 22:29:16 +0000642 /** \brief A C or C++ union. */
643 CXCursor_UnionDecl = 3,
644 /** \brief A C++ class. */
645 CXCursor_ClassDecl = 4,
646 /** \brief An enumeration. */
647 CXCursor_EnumDecl = 5,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000648 /**
Douglas Gregor6007cf22010-01-22 22:29:16 +0000649 * \brief A field (in C) or non-static data member (in C++) in a
650 * struct, union, or C++ class.
651 */
652 CXCursor_FieldDecl = 6,
653 /** \brief An enumerator constant. */
654 CXCursor_EnumConstantDecl = 7,
655 /** \brief A function. */
656 CXCursor_FunctionDecl = 8,
657 /** \brief A variable. */
658 CXCursor_VarDecl = 9,
659 /** \brief A function or method parameter. */
660 CXCursor_ParmDecl = 10,
661 /** \brief An Objective-C @interface. */
662 CXCursor_ObjCInterfaceDecl = 11,
663 /** \brief An Objective-C @interface for a category. */
664 CXCursor_ObjCCategoryDecl = 12,
665 /** \brief An Objective-C @protocol declaration. */
666 CXCursor_ObjCProtocolDecl = 13,
667 /** \brief An Objective-C @property declaration. */
668 CXCursor_ObjCPropertyDecl = 14,
669 /** \brief An Objective-C instance variable. */
670 CXCursor_ObjCIvarDecl = 15,
671 /** \brief An Objective-C instance method. */
672 CXCursor_ObjCInstanceMethodDecl = 16,
673 /** \brief An Objective-C class method. */
674 CXCursor_ObjCClassMethodDecl = 17,
675 /** \brief An Objective-C @implementation. */
676 CXCursor_ObjCImplementationDecl = 18,
677 /** \brief An Objective-C @implementation for a category. */
678 CXCursor_ObjCCategoryImplDecl = 19,
679 /** \brief A typedef */
680 CXCursor_TypedefDecl = 20,
681 CXCursor_LastDecl = 20,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000682
Douglas Gregor6007cf22010-01-22 22:29:16 +0000683 /* References */
684 CXCursor_FirstRef = 40, /* Decl references */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000685 CXCursor_ObjCSuperClassRef = 40,
Douglas Gregor6007cf22010-01-22 22:29:16 +0000686 CXCursor_ObjCProtocolRef = 41,
687 CXCursor_ObjCClassRef = 42,
688 /**
689 * \brief A reference to a type declaration.
690 *
691 * A type reference occurs anywhere where a type is named but not
692 * declared. For example, given:
693 *
694 * \code
695 * typedef unsigned size_type;
696 * size_type size;
697 * \endcode
698 *
699 * The typedef is a declaration of size_type (CXCursor_TypedefDecl),
700 * while the type of the variable "size" is referenced. The cursor
701 * referenced by the type of size is the typedef for size_type.
702 */
703 CXCursor_TypeRef = 43,
704 CXCursor_LastRef = 43,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000705
Douglas Gregor6007cf22010-01-22 22:29:16 +0000706 /* Error conditions */
707 CXCursor_FirstInvalid = 70,
708 CXCursor_InvalidFile = 70,
709 CXCursor_NoDeclFound = 71,
710 CXCursor_NotImplemented = 72,
711 CXCursor_LastInvalid = 72,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000712
Douglas Gregor6007cf22010-01-22 22:29:16 +0000713 /* Expressions */
714 CXCursor_FirstExpr = 100,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000715
Douglas Gregor6007cf22010-01-22 22:29:16 +0000716 /**
717 * \brief An expression whose specific kind is not exposed via this
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000718 * interface.
Douglas Gregor6007cf22010-01-22 22:29:16 +0000719 *
720 * Unexposed expressions have the same operations as any other kind
721 * of expression; one can extract their location information,
722 * spelling, children, etc. However, the specific kind of the
723 * expression is not reported.
724 */
725 CXCursor_UnexposedExpr = 100,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000726
Douglas Gregor6007cf22010-01-22 22:29:16 +0000727 /**
728 * \brief An expression that refers to some value declaration, such
729 * as a function, varible, or enumerator.
730 */
731 CXCursor_DeclRefExpr = 101,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000732
Douglas Gregor6007cf22010-01-22 22:29:16 +0000733 /**
734 * \brief An expression that refers to a member of a struct, union,
735 * class, Objective-C class, etc.
736 */
737 CXCursor_MemberRefExpr = 102,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000738
Douglas Gregor6007cf22010-01-22 22:29:16 +0000739 /** \brief An expression that calls a function. */
740 CXCursor_CallExpr = 103,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000741
Douglas Gregor6007cf22010-01-22 22:29:16 +0000742 /** \brief An expression that sends a message to an Objective-C
743 object or class. */
744 CXCursor_ObjCMessageExpr = 104,
745 CXCursor_LastExpr = 104,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000746
Douglas Gregor6007cf22010-01-22 22:29:16 +0000747 /* Statements */
748 CXCursor_FirstStmt = 200,
749 /**
750 * \brief A statement whose specific kind is not exposed via this
751 * interface.
752 *
753 * Unexposed statements have the same operations as any other kind of
754 * statement; one can extract their location information, spelling,
755 * children, etc. However, the specific kind of the statement is not
756 * reported.
757 */
758 CXCursor_UnexposedStmt = 200,
759 CXCursor_LastStmt = 200,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000760
Douglas Gregor6007cf22010-01-22 22:29:16 +0000761 /**
762 * \brief Cursor that represents the translation unit itself.
763 *
764 * The translation unit cursor exists primarily to act as the root
765 * cursor for traversing the contents of a translation unit.
766 */
767 CXCursor_TranslationUnit = 300
768};
769
770/**
771 * \brief A cursor representing some element in the abstract syntax tree for
772 * a translation unit.
773 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000774 * The cursor abstraction unifies the different kinds of entities in a
Douglas Gregor6007cf22010-01-22 22:29:16 +0000775 * program--declaration, statements, expressions, references to declarations,
776 * etc.--under a single "cursor" abstraction with a common set of operations.
777 * Common operation for a cursor include: getting the physical location in
778 * a source file where the cursor points, getting the name associated with a
779 * cursor, and retrieving cursors for any child nodes of a particular cursor.
780 *
781 * Cursors can be produced in two specific ways.
782 * clang_getTranslationUnitCursor() produces a cursor for a translation unit,
783 * from which one can use clang_visitChildren() to explore the rest of the
784 * translation unit. clang_getCursor() maps from a physical source location
785 * to the entity that resides at that location, allowing one to map from the
786 * source code into the AST.
787 */
788typedef struct {
789 enum CXCursorKind kind;
790 void *data[3];
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000791} CXCursor;
Douglas Gregor6007cf22010-01-22 22:29:16 +0000792
793/**
794 * \defgroup CINDEX_CURSOR_MANIP Cursor manipulations
795 *
796 * @{
797 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000798
Douglas Gregor6007cf22010-01-22 22:29:16 +0000799/**
800 * \brief Retrieve the NULL cursor, which represents no entity.
801 */
802CINDEX_LINKAGE CXCursor clang_getNullCursor(void);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000803
Douglas Gregor6007cf22010-01-22 22:29:16 +0000804/**
805 * \brief Retrieve the cursor that represents the given translation unit.
806 *
807 * The translation unit cursor can be used to start traversing the
808 * various declarations within the given translation unit.
809 */
810CINDEX_LINKAGE CXCursor clang_getTranslationUnitCursor(CXTranslationUnit);
811
812/**
813 * \brief Determine whether two cursors are equivalent.
814 */
815CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000816
Douglas Gregor6007cf22010-01-22 22:29:16 +0000817/**
818 * \brief Retrieve the kind of the given cursor.
819 */
820CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor);
821
822/**
823 * \brief Determine whether the given cursor kind represents a declaration.
824 */
825CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind);
826
827/**
828 * \brief Determine whether the given cursor kind represents a simple
829 * reference.
830 *
831 * Note that other kinds of cursors (such as expressions) can also refer to
832 * other cursors. Use clang_getCursorReferenced() to determine whether a
833 * particular cursor refers to another entity.
834 */
835CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind);
836
837/**
838 * \brief Determine whether the given cursor kind represents an expression.
839 */
840CINDEX_LINKAGE unsigned clang_isExpression(enum CXCursorKind);
841
842/**
843 * \brief Determine whether the given cursor kind represents a statement.
844 */
845CINDEX_LINKAGE unsigned clang_isStatement(enum CXCursorKind);
846
847/**
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000848 * \brief Determine whether the given cursor kind represents an invalid
Douglas Gregor6007cf22010-01-22 22:29:16 +0000849 * cursor.
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000850 */
Douglas Gregor6007cf22010-01-22 22:29:16 +0000851CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
852
853/**
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000854 * \brief Determine whether the given cursor kind represents a translation
855 * unit.
Douglas Gregor6007cf22010-01-22 22:29:16 +0000856 */
857CINDEX_LINKAGE unsigned clang_isTranslationUnit(enum CXCursorKind);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000858
Douglas Gregor6007cf22010-01-22 22:29:16 +0000859/**
860 * @}
861 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000862
Douglas Gregor6007cf22010-01-22 22:29:16 +0000863/**
864 * \defgroup CINDEX_CURSOR_SOURCE Mapping between cursors and source code
865 *
866 * Cursors represent a location within the Abstract Syntax Tree (AST). These
867 * routines help map between cursors and the physical locations where the
868 * described entities occur in the source code. The mapping is provided in
869 * both directions, so one can map from source code to the AST and back.
870 *
871 * @{
Steve Naroffa1c72842009-08-28 15:28:48 +0000872 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000873
Steve Naroff20bad0b2009-10-21 13:56:23 +0000874/**
Douglas Gregor816fd362010-01-22 21:44:22 +0000875 * \brief Map a source location to the cursor that describes the entity at that
876 * location in the source code.
877 *
878 * clang_getCursor() maps an arbitrary source location within a translation
879 * unit down to the most specific cursor that describes the entity at that
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000880 * location. For example, given an expression \c x + y, invoking
Douglas Gregor816fd362010-01-22 21:44:22 +0000881 * clang_getCursor() with a source location pointing to "x" will return the
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000882 * cursor for "x"; similarly for "y". If the cursor points anywhere between
Douglas Gregor816fd362010-01-22 21:44:22 +0000883 * "x" or "y" (e.g., on the + or the whitespace around it), clang_getCursor()
884 * will return a cursor referring to the "+" expression.
885 *
886 * \returns a cursor representing the entity at the given source location, or
887 * a NULL cursor if no such entity can be found.
Steve Naroff20bad0b2009-10-21 13:56:23 +0000888 */
Douglas Gregor816fd362010-01-22 21:44:22 +0000889CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit, CXSourceLocation);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000890
Douglas Gregor66a58812010-01-18 22:46:11 +0000891/**
892 * \brief Retrieve the physical location of the source constructor referenced
893 * by the given cursor.
894 *
895 * The location of a declaration is typically the location of the name of that
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000896 * declaration, where the name of that declaration would occur if it is
897 * unnamed, or some keyword that introduces that particular declaration.
898 * The location of a reference is where that reference occurs within the
Douglas Gregor66a58812010-01-18 22:46:11 +0000899 * source code.
900 */
901CINDEX_LINKAGE CXSourceLocation clang_getCursorLocation(CXCursor);
Douglas Gregor6007cf22010-01-22 22:29:16 +0000902
Douglas Gregor6b8232f2010-01-19 19:34:47 +0000903/**
904 * \brief Retrieve the physical extent of the source construct referenced by
Douglas Gregor33c34ac2010-01-19 00:34:46 +0000905 * the given cursor.
906 *
907 * The extent of a cursor starts with the file/line/column pointing at the
908 * first character within the source construct that the cursor refers to and
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000909 * ends with the last character withinin that source construct. For a
Douglas Gregor33c34ac2010-01-19 00:34:46 +0000910 * declaration, the extent covers the declaration itself. For a reference,
911 * the extent covers the location of the reference (e.g., where the referenced
912 * entity was actually used).
913 */
914CINDEX_LINKAGE CXSourceRange clang_getCursorExtent(CXCursor);
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000915
Douglas Gregor6007cf22010-01-22 22:29:16 +0000916/**
917 * @}
918 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000919
Douglas Gregor6007cf22010-01-22 22:29:16 +0000920/**
921 * \defgroup CINDEX_CURSOR_TRAVERSAL Traversing the AST with cursors
922 *
923 * These routines provide the ability to traverse the abstract syntax tree
924 * using cursors.
925 *
926 * @{
927 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000928
Douglas Gregor6007cf22010-01-22 22:29:16 +0000929/**
930 * \brief Describes how the traversal of the children of a particular
931 * cursor should proceed after visiting a particular child cursor.
932 *
933 * A value of this enumeration type should be returned by each
934 * \c CXCursorVisitor to indicate how clang_visitChildren() proceed.
935 */
936enum CXChildVisitResult {
937 /**
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000938 * \brief Terminates the cursor traversal.
Douglas Gregor6007cf22010-01-22 22:29:16 +0000939 */
940 CXChildVisit_Break,
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000941 /**
Douglas Gregor6007cf22010-01-22 22:29:16 +0000942 * \brief Continues the cursor traversal with the next sibling of
943 * the cursor just visited, without visiting its children.
944 */
945 CXChildVisit_Continue,
946 /**
947 * \brief Recursively traverse the children of this cursor, using
948 * the same visitor and client data.
949 */
950 CXChildVisit_Recurse
951};
952
953/**
954 * \brief Visitor invoked for each cursor found by a traversal.
955 *
956 * This visitor function will be invoked for each cursor found by
957 * clang_visitCursorChildren(). Its first argument is the cursor being
958 * visited, its second argument is the parent visitor for that cursor,
959 * and its third argument is the client data provided to
960 * clang_visitCursorChildren().
961 *
962 * The visitor should return one of the \c CXChildVisitResult values
963 * to direct clang_visitCursorChildren().
964 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000965typedef enum CXChildVisitResult (*CXCursorVisitor)(CXCursor cursor,
966 CXCursor parent,
Douglas Gregor6007cf22010-01-22 22:29:16 +0000967 CXClientData client_data);
968
969/**
970 * \brief Visit the children of a particular cursor.
971 *
972 * This function visits all the direct children of the given cursor,
973 * invoking the given \p visitor function with the cursors of each
974 * visited child. The traversal may be recursive, if the visitor returns
975 * \c CXChildVisit_Recurse. The traversal may also be ended prematurely, if
976 * the visitor returns \c CXChildVisit_Break.
977 *
Douglas Gregor6007cf22010-01-22 22:29:16 +0000978 * \param parent the cursor whose child may be visited. All kinds of
Daniel Dunbarb9999fd2010-01-24 04:10:31 +0000979 * cursors can be visited, including invalid cursors (which, by
Douglas Gregor6007cf22010-01-22 22:29:16 +0000980 * definition, have no children).
981 *
982 * \param visitor the visitor function that will be invoked for each
983 * child of \p parent.
984 *
985 * \param client_data pointer data supplied by the client, which will
986 * be passed to the visitor each time it is invoked.
987 *
988 * \returns a non-zero value if the traversal was terminated
989 * prematurely by the visitor returning \c CXChildVisit_Break.
990 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000991CINDEX_LINKAGE unsigned clang_visitChildren(CXCursor parent,
Douglas Gregor6007cf22010-01-22 22:29:16 +0000992 CXCursorVisitor visitor,
993 CXClientData client_data);
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000994
Douglas Gregor6007cf22010-01-22 22:29:16 +0000995/**
996 * @}
997 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +0000998
Douglas Gregor6007cf22010-01-22 22:29:16 +0000999/**
1000 * \defgroup CINDEX_CURSOR_XREF Cross-referencing in the AST
1001 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001002 * These routines provide the ability to determine references within and
Douglas Gregor6007cf22010-01-22 22:29:16 +00001003 * across translation units, by providing the names of the entities referenced
1004 * by cursors, follow reference cursors to the declarations they reference,
1005 * and associate declarations with their definitions.
1006 *
1007 * @{
1008 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001009
Douglas Gregor6007cf22010-01-22 22:29:16 +00001010/**
1011 * \brief Retrieve a Unified Symbol Resolution (USR) for the entity referenced
1012 * by the given cursor.
1013 *
1014 * A Unified Symbol Resolution (USR) is a string that identifies a particular
1015 * entity (function, class, variable, etc.) within a program. USRs can be
1016 * compared across translation units to determine, e.g., when references in
1017 * one translation refer to an entity defined in another translation unit.
1018 */
1019CINDEX_LINKAGE CXString clang_getCursorUSR(CXCursor);
1020
1021/**
1022 * \brief Retrieve a name for the entity referenced by this cursor.
1023 */
1024CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);
1025
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001026/** \brief For a cursor that is a reference, retrieve a cursor representing the
1027 * entity that it references.
1028 *
1029 * Reference cursors refer to other entities in the AST. For example, an
1030 * Objective-C superclass reference cursor refers to an Objective-C class.
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001031 * This function produces the cursor for the Objective-C class from the
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001032 * cursor for the superclass reference. If the input cursor is a declaration or
1033 * definition, it returns that declaration or definition unchanged.
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001034 * Otherwise, returns the NULL cursor.
Douglas Gregorad27e8b2010-01-19 01:20:04 +00001035 */
1036CINDEX_LINKAGE CXCursor clang_getCursorReferenced(CXCursor);
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001037
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001038/**
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001039 * \brief For a cursor that is either a reference to or a declaration
1040 * of some entity, retrieve a cursor that describes the definition of
1041 * that entity.
1042 *
1043 * Some entities can be declared multiple times within a translation
1044 * unit, but only one of those declarations can also be a
1045 * definition. For example, given:
1046 *
1047 * \code
1048 * int f(int, int);
1049 * int g(int x, int y) { return f(x, y); }
1050 * int f(int a, int b) { return a + b; }
1051 * int f(int, int);
1052 * \endcode
1053 *
1054 * there are three declarations of the function "f", but only the
1055 * second one is a definition. The clang_getCursorDefinition()
1056 * function will take any cursor pointing to a declaration of "f"
1057 * (the first or fourth lines of the example) or a cursor referenced
1058 * that uses "f" (the call to "f' inside "g") and will return a
1059 * declaration cursor pointing to the definition (the second "f"
1060 * declaration).
1061 *
1062 * If given a cursor for which there is no corresponding definition,
1063 * e.g., because there is no definition of that entity within this
1064 * translation unit, returns a NULL cursor.
1065 */
1066CINDEX_LINKAGE CXCursor clang_getCursorDefinition(CXCursor);
1067
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001068/**
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001069 * \brief Determine whether the declaration pointed to by this cursor
1070 * is also a definition of that entity.
1071 */
1072CINDEX_LINKAGE unsigned clang_isCursorDefinition(CXCursor);
1073
Douglas Gregor6007cf22010-01-22 22:29:16 +00001074/**
1075 * @}
1076 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001077
Douglas Gregor6007cf22010-01-22 22:29:16 +00001078/**
Douglas Gregor61656112010-01-26 18:31:56 +00001079 * \defgroup CINDEX_LEX Token extraction and manipulation
1080 *
1081 * The routines in this group provide access to the tokens within a
1082 * translation unit, along with a semantic mapping of those tokens to
1083 * their corresponding cursors.
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001084 *
1085 * @{
1086 */
1087
1088/**
1089 * \brief Describes a kind of token.
1090 */
1091typedef enum CXTokenKind {
1092 /**
1093 * \brief A token that contains some kind of punctuation.
1094 */
1095 CXToken_Punctuation,
1096
1097 /**
Douglas Gregor61656112010-01-26 18:31:56 +00001098 * \brief A language keyword.
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001099 */
1100 CXToken_Keyword,
1101
1102 /**
1103 * \brief An identifier (that is not a keyword).
1104 */
1105 CXToken_Identifier,
1106
1107 /**
1108 * \brief A numeric, string, or character literal.
1109 */
1110 CXToken_Literal,
1111
1112 /**
1113 * \brief A comment.
1114 */
1115 CXToken_Comment
1116} CXTokenKind;
1117
1118/**
1119 * \brief Describes a single preprocessing token.
1120 */
1121typedef struct {
1122 unsigned int_data[4];
1123 void *ptr_data;
1124} CXToken;
1125
1126/**
1127 * \brief Determine the kind of the given token.
1128 */
1129CINDEX_LINKAGE CXTokenKind clang_getTokenKind(CXToken);
1130
1131/**
1132 * \brief Determine the spelling of the given token.
1133 *
1134 * The spelling of a token is the textual representation of that token, e.g.,
1135 * the text of an identifier or keyword.
1136 */
1137CINDEX_LINKAGE CXString clang_getTokenSpelling(CXTranslationUnit, CXToken);
1138
1139/**
1140 * \brief Retrieve the source location of the given token.
1141 */
1142CINDEX_LINKAGE CXSourceLocation clang_getTokenLocation(CXTranslationUnit,
1143 CXToken);
1144
1145/**
1146 * \brief Retrieve a source range that covers the given token.
1147 */
1148CINDEX_LINKAGE CXSourceRange clang_getTokenExtent(CXTranslationUnit, CXToken);
1149
1150/**
1151 * \brief Tokenize the source code described by the given range into raw
1152 * lexical tokens.
1153 *
1154 * \param TU the translation unit whose text is being tokenized.
1155 *
1156 * \param Range the source range in which text should be tokenized. All of the
1157 * tokens produced by tokenization will fall within this source range,
1158 *
1159 * \param Tokens this pointer will be set to point to the array of tokens
1160 * that occur within the given source range. The returned pointer must be
1161 * freed with clang_disposeTokens() before the translation unit is destroyed.
1162 *
1163 * \param NumTokens will be set to the number of tokens in the \c *Tokens
1164 * array.
1165 *
1166 */
1167CINDEX_LINKAGE void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
1168 CXToken **Tokens, unsigned *NumTokens);
1169
1170/**
1171 * \brief Annotate the given set of tokens by providing cursors for each token
1172 * that can be mapped to a specific entity within the abstract syntax tree.
1173 *
Douglas Gregor61656112010-01-26 18:31:56 +00001174 * This token-annotation routine is equivalent to invoking
1175 * clang_getCursor() for the source locations of each of the
1176 * tokens. The cursors provided are filtered, so that only those
1177 * cursors that have a direct correspondence to the token are
1178 * accepted. For example, given a function call \c f(x),
1179 * clang_getCursor() would provide the following cursors:
1180 *
1181 * * when the cursor is over the 'f', a DeclRefExpr cursor referring to 'f'.
1182 * * when the cursor is over the '(' or the ')', a CallExpr referring to 'f'.
1183 * * when the cursor is over the 'x', a DeclRefExpr cursor referring to 'x'.
1184 *
1185 * Only the first and last of these cursors will occur within the
1186 * annotate, since the tokens "f" and "x' directly refer to a function
1187 * and a variable, respectively, but the parentheses are just a small
1188 * part of the full syntax of the function call expression, which is
1189 * not provided as an annotation.
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001190 *
1191 * \param TU the translation unit that owns the given tokens.
1192 *
1193 * \param Tokens the set of tokens to annotate.
1194 *
1195 * \param NumTokens the number of tokens in \p Tokens.
1196 *
1197 * \param Cursors an array of \p NumTokens cursors, whose contents will be
1198 * replaced with the cursors corresponding to each token.
1199 */
1200CINDEX_LINKAGE void clang_annotateTokens(CXTranslationUnit TU,
1201 CXToken *Tokens, unsigned NumTokens,
1202 CXCursor *Cursors);
1203
1204/**
1205 * \brief Free the given set of tokens.
1206 */
1207CINDEX_LINKAGE void clang_disposeTokens(CXTranslationUnit TU,
1208 CXToken *Tokens, unsigned NumTokens);
1209
1210/**
1211 * @}
1212 */
1213
1214/**
Douglas Gregor6007cf22010-01-22 22:29:16 +00001215 * \defgroup CINDEX_DEBUG Debugging facilities
1216 *
1217 * These routines are used for testing and debugging, only, and should not
1218 * be relied upon.
1219 *
1220 * @{
1221 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001222
Steve Naroff76b8f132009-09-23 17:52:52 +00001223/* for debug/testing */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001224CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind);
1225CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor,
1226 const char **startBuf,
Steve Naroff76b8f132009-09-23 17:52:52 +00001227 const char **endBuf,
1228 unsigned *startLine,
1229 unsigned *startColumn,
1230 unsigned *endLine,
1231 unsigned *endColumn);
Steve Naroffd5e8e862009-08-27 19:51:58 +00001232
Douglas Gregor9eb77012009-11-07 00:00:49 +00001233/**
Douglas Gregor6007cf22010-01-22 22:29:16 +00001234 * @}
1235 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001236
Douglas Gregor6007cf22010-01-22 22:29:16 +00001237/**
1238 * \defgroup CINDEX_CODE_COMPLET Code completion
1239 *
1240 * Code completion involves taking an (incomplete) source file, along with
1241 * knowledge of where the user is actively editing that file, and suggesting
1242 * syntactically- and semantically-valid constructs that the user might want to
1243 * use at that particular point in the source code. These data structures and
1244 * routines provide support for code completion.
1245 *
1246 * @{
1247 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001248
Douglas Gregor6007cf22010-01-22 22:29:16 +00001249/**
Douglas Gregor9eb77012009-11-07 00:00:49 +00001250 * \brief A semantic string that describes a code-completion result.
1251 *
1252 * A semantic string that describes the formatting of a code-completion
1253 * result as a single "template" of text that should be inserted into the
1254 * source buffer when a particular code-completion result is selected.
1255 * Each semantic string is made up of some number of "chunks", each of which
1256 * contains some text along with a description of what that text means, e.g.,
1257 * the name of the entity being referenced, whether the text chunk is part of
1258 * the template, or whether it is a "placeholder" that the user should replace
1259 * with actual code,of a specific kind. See \c CXCompletionChunkKind for a
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001260 * description of the different kinds of chunks.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001261 */
1262typedef void *CXCompletionString;
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001263
Douglas Gregor9eb77012009-11-07 00:00:49 +00001264/**
1265 * \brief A single result of code completion.
1266 */
1267typedef struct {
1268 /**
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001269 * \brief The kind of entity that this completion refers to.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001270 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001271 * The cursor kind will be a macro, keyword, or a declaration (one of the
Douglas Gregor9eb77012009-11-07 00:00:49 +00001272 * *Decl cursor kinds), describing the entity that the completion is
1273 * referring to.
1274 *
1275 * \todo In the future, we would like to provide a full cursor, to allow
1276 * the client to extract additional information from declaration.
1277 */
1278 enum CXCursorKind CursorKind;
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001279
1280 /**
Douglas Gregor9eb77012009-11-07 00:00:49 +00001281 * \brief The code-completion string that describes how to insert this
1282 * code-completion result into the editing buffer.
1283 */
1284 CXCompletionString CompletionString;
1285} CXCompletionResult;
1286
1287/**
1288 * \brief Describes a single piece of text within a code-completion string.
1289 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001290 * Each "chunk" within a code-completion string (\c CXCompletionString) is
1291 * either a piece of text with a specific "kind" that describes how that text
Douglas Gregor9eb77012009-11-07 00:00:49 +00001292 * should be interpreted by the client or is another completion string.
1293 */
1294enum CXCompletionChunkKind {
1295 /**
1296 * \brief A code-completion string that describes "optional" text that
1297 * could be a part of the template (but is not required).
1298 *
1299 * The Optional chunk is the only kind of chunk that has a code-completion
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001300 * string for its representation, which is accessible via
Douglas Gregor9eb77012009-11-07 00:00:49 +00001301 * \c clang_getCompletionChunkCompletionString(). The code-completion string
1302 * describes an additional part of the template that is completely optional.
1303 * For example, optional chunks can be used to describe the placeholders for
1304 * arguments that match up with defaulted function parameters, e.g. given:
1305 *
1306 * \code
1307 * void f(int x, float y = 3.14, double z = 2.71828);
1308 * \endcode
1309 *
1310 * The code-completion string for this function would contain:
1311 * - a TypedText chunk for "f".
1312 * - a LeftParen chunk for "(".
1313 * - a Placeholder chunk for "int x"
1314 * - an Optional chunk containing the remaining defaulted arguments, e.g.,
1315 * - a Comma chunk for ","
1316 * - a Placeholder chunk for "float x"
1317 * - an Optional chunk containing the last defaulted argument:
1318 * - a Comma chunk for ","
1319 * - a Placeholder chunk for "double z"
1320 * - a RightParen chunk for ")"
1321 *
1322 * There are many ways two handle Optional chunks. Two simple approaches are:
1323 * - Completely ignore optional chunks, in which case the template for the
1324 * function "f" would only include the first parameter ("int x").
1325 * - Fully expand all optional chunks, in which case the template for the
1326 * function "f" would have all of the parameters.
1327 */
1328 CXCompletionChunk_Optional,
1329 /**
1330 * \brief Text that a user would be expected to type to get this
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001331 * code-completion result.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001332 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001333 * There will be exactly one "typed text" chunk in a semantic string, which
1334 * will typically provide the spelling of a keyword or the name of a
Douglas Gregor9eb77012009-11-07 00:00:49 +00001335 * declaration that could be used at the current code point. Clients are
1336 * expected to filter the code-completion results based on the text in this
1337 * chunk.
1338 */
1339 CXCompletionChunk_TypedText,
1340 /**
1341 * \brief Text that should be inserted as part of a code-completion result.
1342 *
1343 * A "text" chunk represents text that is part of the template to be
1344 * inserted into user code should this particular code-completion result
1345 * be selected.
1346 */
1347 CXCompletionChunk_Text,
1348 /**
1349 * \brief Placeholder text that should be replaced by the user.
1350 *
1351 * A "placeholder" chunk marks a place where the user should insert text
1352 * into the code-completion template. For example, placeholders might mark
1353 * the function parameters for a function declaration, to indicate that the
1354 * user should provide arguments for each of those parameters. The actual
1355 * text in a placeholder is a suggestion for the text to display before
1356 * the user replaces the placeholder with real code.
1357 */
1358 CXCompletionChunk_Placeholder,
1359 /**
1360 * \brief Informative text that should be displayed but never inserted as
1361 * part of the template.
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001362 *
Douglas Gregor9eb77012009-11-07 00:00:49 +00001363 * An "informative" chunk contains annotations that can be displayed to
1364 * help the user decide whether a particular code-completion result is the
1365 * right option, but which is not part of the actual template to be inserted
1366 * by code completion.
1367 */
1368 CXCompletionChunk_Informative,
1369 /**
1370 * \brief Text that describes the current parameter when code-completion is
1371 * referring to function call, message send, or template specialization.
1372 *
1373 * A "current parameter" chunk occurs when code-completion is providing
1374 * information about a parameter corresponding to the argument at the
1375 * code-completion point. For example, given a function
1376 *
1377 * \code
1378 * int add(int x, int y);
1379 * \endcode
1380 *
1381 * and the source code \c add(, where the code-completion point is after the
1382 * "(", the code-completion string will contain a "current parameter" chunk
1383 * for "int x", indicating that the current argument will initialize that
1384 * parameter. After typing further, to \c add(17, (where the code-completion
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001385 * point is after the ","), the code-completion string will contain a
Douglas Gregor9eb77012009-11-07 00:00:49 +00001386 * "current paremeter" chunk to "int y".
1387 */
1388 CXCompletionChunk_CurrentParameter,
1389 /**
1390 * \brief A left parenthesis ('('), used to initiate a function call or
1391 * signal the beginning of a function parameter list.
1392 */
1393 CXCompletionChunk_LeftParen,
1394 /**
1395 * \brief A right parenthesis (')'), used to finish a function call or
1396 * signal the end of a function parameter list.
1397 */
1398 CXCompletionChunk_RightParen,
1399 /**
1400 * \brief A left bracket ('[').
1401 */
1402 CXCompletionChunk_LeftBracket,
1403 /**
1404 * \brief A right bracket (']').
1405 */
1406 CXCompletionChunk_RightBracket,
1407 /**
1408 * \brief A left brace ('{').
1409 */
1410 CXCompletionChunk_LeftBrace,
1411 /**
1412 * \brief A right brace ('}').
1413 */
1414 CXCompletionChunk_RightBrace,
1415 /**
1416 * \brief A left angle bracket ('<').
1417 */
1418 CXCompletionChunk_LeftAngle,
1419 /**
1420 * \brief A right angle bracket ('>').
1421 */
1422 CXCompletionChunk_RightAngle,
1423 /**
1424 * \brief A comma separator (',').
1425 */
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001426 CXCompletionChunk_Comma,
1427 /**
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001428 * \brief Text that specifies the result type of a given result.
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001429 *
1430 * This special kind of informative chunk is not meant to be inserted into
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001431 * the text buffer. Rather, it is meant to illustrate the type that an
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001432 * expression using the given completion string would have.
1433 */
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001434 CXCompletionChunk_ResultType,
1435 /**
1436 * \brief A colon (':').
1437 */
1438 CXCompletionChunk_Colon,
1439 /**
1440 * \brief A semicolon (';').
1441 */
1442 CXCompletionChunk_SemiColon,
1443 /**
1444 * \brief An '=' sign.
1445 */
1446 CXCompletionChunk_Equal,
1447 /**
1448 * Horizontal space (' ').
1449 */
1450 CXCompletionChunk_HorizontalSpace,
1451 /**
1452 * Vertical space ('\n'), after which it is generally a good idea to
1453 * perform indentation.
1454 */
1455 CXCompletionChunk_VerticalSpace
Douglas Gregor9eb77012009-11-07 00:00:49 +00001456};
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001457
Douglas Gregor9eb77012009-11-07 00:00:49 +00001458/**
1459 * \brief Determine the kind of a particular chunk within a completion string.
1460 *
1461 * \param completion_string the completion string to query.
1462 *
1463 * \param chunk_number the 0-based index of the chunk in the completion string.
1464 *
1465 * \returns the kind of the chunk at the index \c chunk_number.
1466 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001467CINDEX_LINKAGE enum CXCompletionChunkKind
Douglas Gregor9eb77012009-11-07 00:00:49 +00001468clang_getCompletionChunkKind(CXCompletionString completion_string,
1469 unsigned chunk_number);
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001470
Douglas Gregor9eb77012009-11-07 00:00:49 +00001471/**
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001472 * \brief Retrieve the text associated with a particular chunk within a
Douglas Gregor9eb77012009-11-07 00:00:49 +00001473 * completion string.
1474 *
1475 * \param completion_string the completion string to query.
1476 *
1477 * \param chunk_number the 0-based index of the chunk in the completion string.
1478 *
1479 * \returns the text associated with the chunk at index \c chunk_number.
1480 */
1481CINDEX_LINKAGE const char *
1482clang_getCompletionChunkText(CXCompletionString completion_string,
1483 unsigned chunk_number);
1484
1485/**
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001486 * \brief Retrieve the completion string associated with a particular chunk
Douglas Gregor9eb77012009-11-07 00:00:49 +00001487 * within a completion string.
1488 *
1489 * \param completion_string the completion string to query.
1490 *
1491 * \param chunk_number the 0-based index of the chunk in the completion string.
1492 *
1493 * \returns the completion string associated with the chunk at index
1494 * \c chunk_number, or NULL if that chunk is not represented by a completion
1495 * string.
1496 */
1497CINDEX_LINKAGE CXCompletionString
1498clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
1499 unsigned chunk_number);
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001500
Douglas Gregor9eb77012009-11-07 00:00:49 +00001501/**
1502 * \brief Retrieve the number of chunks in the given code-completion string.
1503 */
1504CINDEX_LINKAGE unsigned
1505clang_getNumCompletionChunks(CXCompletionString completion_string);
1506
1507/**
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001508 * \brief Contains the results of code-completion.
1509 *
1510 * This data structure contains the results of code completion, as
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001511 * produced by \c clang_codeComplete. Its contents must be freed by
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001512 * \c clang_disposeCodeCompleteResults.
1513 */
1514typedef struct {
1515 /**
1516 * \brief The code-completion results.
1517 */
1518 CXCompletionResult *Results;
1519
1520 /**
1521 * \brief The number of code-completion results stored in the
1522 * \c Results array.
1523 */
1524 unsigned NumResults;
1525} CXCodeCompleteResults;
1526
1527/**
Douglas Gregor9eb77012009-11-07 00:00:49 +00001528 * \brief Perform code completion at a given location in a source file.
1529 *
1530 * This function performs code completion at a particular file, line, and
1531 * column within source code, providing results that suggest potential
1532 * code snippets based on the context of the completion. The basic model
1533 * for code completion is that Clang will parse a complete source file,
1534 * performing syntax checking up to the location where code-completion has
1535 * been requested. At that point, a special code-completion token is passed
1536 * to the parser, which recognizes this token and determines, based on the
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001537 * current location in the C/Objective-C/C++ grammar and the state of
Douglas Gregor9eb77012009-11-07 00:00:49 +00001538 * semantic analysis, what completions to provide. These completions are
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001539 * returned via a new \c CXCodeCompleteResults structure.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001540 *
1541 * Code completion itself is meant to be triggered by the client when the
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001542 * user types punctuation characters or whitespace, at which point the
Douglas Gregor9eb77012009-11-07 00:00:49 +00001543 * code-completion location will coincide with the cursor. For example, if \c p
1544 * is a pointer, code-completion might be triggered after the "-" and then
1545 * after the ">" in \c p->. When the code-completion location is afer the ">",
1546 * the completion results will provide, e.g., the members of the struct that
1547 * "p" points to. The client is responsible for placing the cursor at the
1548 * beginning of the token currently being typed, then filtering the results
1549 * based on the contents of the token. For example, when code-completing for
1550 * the expression \c p->get, the client should provide the location just after
1551 * the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the
1552 * client can filter the results based on the current token text ("get"), only
1553 * showing those results that start with "get". The intent of this interface
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001554 * is to separate the relatively high-latency acquisition of code-completion
Douglas Gregor9eb77012009-11-07 00:00:49 +00001555 * results from the filtering of results on a per-character basis, which must
1556 * have a lower latency.
1557 *
1558 * \param CIdx the \c CXIndex instance that will be used to perform code
1559 * completion.
1560 *
Daniel Dunbar11089662009-12-03 01:54:28 +00001561 * \param source_filename the name of the source file that should be parsed to
1562 * perform code-completion. This source file must be the same as or include the
1563 * filename described by \p complete_filename, or no code-completion results
1564 * will be produced. NOTE: One can also specify NULL for this argument if the
1565 * source file is included in command_line_args.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001566 *
1567 * \param num_command_line_args the number of command-line arguments stored in
1568 * \p command_line_args.
1569 *
1570 * \param command_line_args the command-line arguments to pass to the Clang
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001571 * compiler to build the given source file. This should include all of the
Douglas Gregor9eb77012009-11-07 00:00:49 +00001572 * necessary include paths, language-dialect switches, precompiled header
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001573 * includes, etc., but should not include any information specific to
Douglas Gregor9eb77012009-11-07 00:00:49 +00001574 * code completion.
1575 *
Douglas Gregor9485bf92009-12-02 09:21:34 +00001576 * \param num_unsaved_files the number of unsaved file entries in \p
1577 * unsaved_files.
1578 *
1579 * \param unsaved_files the files that have not yet been saved to disk
1580 * but may be required for code completion, including the contents of
1581 * those files.
1582 *
Douglas Gregor9eb77012009-11-07 00:00:49 +00001583 * \param complete_filename the name of the source file where code completion
1584 * should be performed. In many cases, this name will be the same as the
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001585 * source filename. However, the completion filename may also be a file
1586 * included by the source file, which is required when producing
Douglas Gregor9eb77012009-11-07 00:00:49 +00001587 * code-completion results for a header.
1588 *
1589 * \param complete_line the line at which code-completion should occur.
1590 *
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001591 * \param complete_column the column at which code-completion should occur.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001592 * Note that the column should point just after the syntactic construct that
1593 * initiated code completion, and not in the middle of a lexical token.
1594 *
Douglas Gregorba965fb2010-01-28 00:56:43 +00001595 * \param diag_callback callback function that will receive any diagnostics
1596 * emitted while processing this source file. If NULL, diagnostics will be
1597 * suppressed.
1598 *
1599 * \param diag_client_data client data that will be passed to the diagnostic
1600 * callback function.
1601 *
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001602 * \returns if successful, a new CXCodeCompleteResults structure
1603 * containing code-completion results, which should eventually be
1604 * freed with \c clang_disposeCodeCompleteResults(). If code
1605 * completion fails, returns NULL.
Douglas Gregor9eb77012009-11-07 00:00:49 +00001606 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001607CINDEX_LINKAGE
1608CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001609 const char *source_filename,
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001610 int num_command_line_args,
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001611 const char **command_line_args,
1612 unsigned num_unsaved_files,
1613 struct CXUnsavedFile *unsaved_files,
1614 const char *complete_filename,
1615 unsigned complete_line,
Douglas Gregorba965fb2010-01-28 00:56:43 +00001616 unsigned complete_column,
1617 CXDiagnosticCallback diag_callback,
1618 CXClientData diag_client_data);
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001619
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001620/**
1621 * \brief Free the given set of code-completion results.
1622 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001623CINDEX_LINKAGE
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001624void clang_disposeCodeCompleteResults(CXCodeCompleteResults *Results);
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001625
Douglas Gregor52606ff2010-01-20 01:10:47 +00001626/**
1627 * @}
1628 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001629
1630
Ted Kremenekc0f3f722010-01-22 22:44:15 +00001631/**
1632 * \defgroup CINDEX_MISC Miscellaneous utility functions
1633 *
1634 * @{
1635 */
Ted Kremenek3e315a22010-01-23 17:51:23 +00001636
1637/**
1638 * \brief Return a version string, suitable for showing to a user, but not
1639 * intended to be parsed (the format is not guaranteed to be stable).
1640 */
Ted Kremeneka3e65702010-02-12 22:54:40 +00001641CINDEX_LINKAGE CXString clang_getClangVersion();
Ted Kremenekc0f3f722010-01-22 22:44:15 +00001642
1643/**
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001644 * \brief Return a version string, suitable for showing to a user, but not
1645 * intended to be parsed (the format is not guaranteed to be stable).
1646 */
1647
1648
1649 /**
1650 * \brief Visitor invoked for each file in a translation unit
1651 * (used with clang_getInclusions()).
1652 *
1653 * This visitor function will be invoked by clang_getInclusions() for each
1654 * file included (either at the top-level or by #include directives) within
1655 * a translation unit. The first argument is the file being included, and
1656 * the second and third arguments provide the inclusion stack. The
1657 * array is sorted in order of immediate inclusion. For example,
1658 * the first element refers to the location that included 'included_file'.
1659 */
1660typedef void (*CXInclusionVisitor)(CXFile included_file,
1661 CXSourceLocation* inclusion_stack,
1662 unsigned include_len,
1663 CXClientData client_data);
1664
1665/**
1666 * \brief Visit the set of preprocessor inclusions in a translation unit.
1667 * The visitor function is called with the provided data for every included
1668 * file. This does not include headers included by the PCH file (unless one
1669 * is inspecting the inclusions in the PCH file itself).
1670 */
1671CINDEX_LINKAGE void clang_getInclusions(CXTranslationUnit tu,
1672 CXInclusionVisitor visitor,
1673 CXClientData client_data);
1674
1675/**
Ted Kremenekc0f3f722010-01-22 22:44:15 +00001676 * @}
1677 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001678
Douglas Gregor6007cf22010-01-22 22:29:16 +00001679/**
1680 * @}
1681 */
Daniel Dunbar62ebf252010-01-24 02:54:26 +00001682
Ted Kremenekb60d87c2009-08-26 22:36:44 +00001683#ifdef __cplusplus
1684}
1685#endif
1686#endif
1687