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