blob: a417209d837c69cd70537b2aff42f9f092d00d38 [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 */
118 CXCursor_ObjCIvarRef = 44,
119 CXCursor_VarRef = 45,
120 CXCursor_FunctionRef = 46,
121 CXCursor_EnumConstantRef = 47,
122 CXCursor_MemberRef = 48,
123 CXCursor_LastRef = 48,
Steve Naroff77128dd2009-09-15 20:25:34 +0000124
125 /* Error conditions */
126 CXCursor_FirstInvalid = 70,
127 CXCursor_InvalidFile = 70,
128 CXCursor_NoDeclFound = 71,
129 CXCursor_NotImplemented = 72,
130 CXCursor_LastInvalid = 72
Steve Naroff600866c2009-08-27 19:51:58 +0000131};
132
Douglas Gregor735df882009-12-02 09:21:34 +0000133/**
134 * \brief Provides the contents of a file that has not yet been saved to disk.
135 *
136 * Each CXUnsavedFile instance provides the name of a file on the
137 * system along with the current contents of that file that have not
138 * yet been saved to disk.
139 */
140struct CXUnsavedFile {
141 /**
142 * \brief The file whose contents have not yet been saved.
143 *
144 * This file must already exist in the file system.
145 */
146 const char *Filename;
147
148 /**
149 * \brief A null-terminated buffer containing the unsaved contents
150 * of this file.
151 */
152 const char *Contents;
153
154 /**
155 * \brief The length of the unsaved contents of this buffer, not
156 * counting the NULL at the end of the buffer.
157 */
158 unsigned long Length;
159};
160
Steve Naroff89922f82009-08-31 00:59:03 +0000161/* A cursor into the CXTranslationUnit. */
Steve Narofffb570422009-09-22 19:25:29 +0000162
Steve Naroff89922f82009-08-31 00:59:03 +0000163typedef struct {
164 enum CXCursorKind kind;
Douglas Gregor283cae32010-01-15 21:56:13 +0000165 void *data[3];
Steve Naroff89922f82009-08-31 00:59:03 +0000166} CXCursor;
167
Steve Naroff50398192009-08-28 15:28:48 +0000168/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Ted Kremenek31723832010-01-11 23:56:39 +0000169typedef struct {
170 CXIndex index;
171 void *data;
172} CXEntity;
Steve Naroff600866c2009-08-27 19:51:58 +0000173
Steve Naroffef0cef62009-11-09 17:45:52 +0000174/**
175 * For functions returning a string that might or might not need
176 * to be internally allocated and freed.
177 * Use clang_getCString to access the C string value.
178 * Use clang_disposeString to free the value.
179 * Treat it as an opaque type.
180 */
181typedef struct {
182 const char *Spelling;
183 /* A 1 value indicates the clang_ indexing API needed to allocate the string
184 (and it must be freed by clang_disposeString()). */
185 int MustFreeString;
186} CXString;
187
188/* Get C string pointer from a CXString. */
189CINDEX_LINKAGE const char *clang_getCString(CXString string);
190
191/* Free CXString. */
192CINDEX_LINKAGE void clang_disposeString(CXString string);
193
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000194/**
195 * \brief clang_createIndex() provides a shared context for creating
196 * translation units. It provides two options:
197 *
198 * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local"
199 * declarations (when loading any new translation units). A "local" declaration
200 * is one that belongs in the translation unit itself and not in a precompiled
201 * header that was used by the translation unit. If zero, all declarations
202 * will be enumerated.
203 *
204 * - displayDiagnostics: when non-zero, diagnostics will be output. If zero,
205 * diagnostics will be ignored.
Steve Naroffb4ece632009-10-20 16:36:34 +0000206 *
207 * Here is an example:
208 *
209 * // excludeDeclsFromPCH = 1, displayDiagnostics = 1
210 * Idx = clang_createIndex(1, 1);
211 *
212 * // IndexTest.pch was produced with the following command:
213 * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
214 * TU = clang_createTranslationUnit(Idx, "IndexTest.pch");
215 *
216 * // This will load all the symbols from 'IndexTest.pch'
217 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
218 * clang_disposeTranslationUnit(TU);
219 *
220 * // This will load all the symbols from 'IndexTest.c', excluding symbols
221 * // from 'IndexTest.pch'.
222 * char *args[] = { "-Xclang", "-include-pch=IndexTest.pch", 0 };
223 * TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args);
224 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
225 * clang_disposeTranslationUnit(TU);
226 *
227 * This process of creating the 'pch', loading it separately, and using it (via
228 * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks
229 * (which gives the indexer the same performance benefit as the compiler).
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000230 */
John Thompson2e06fc82009-10-27 13:42:56 +0000231CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000232 int displayDiagnostics);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000233CINDEX_LINKAGE void clang_disposeIndex(CXIndex index);
234CINDEX_LINKAGE CXString
235clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
236
237/*
238 * \brief Request that AST's be generated external for API calls which parse
239 * source code on the fly, e.g. \see createTranslationUnitFromSourceFile.
240 *
241 * Note: This is for debugging purposes only, and may be removed at a later
242 * date.
243 *
244 * \param index - The index to update.
245 * \param value - The new flag value.
246 */
247CINDEX_LINKAGE void clang_setUseExternalASTGeneration(CXIndex index,
248 int value);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000249
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000250/*
251 * \brief Create a translation unit from an AST file (-emit-ast).
252 */
John Thompson2e06fc82009-10-27 13:42:56 +0000253CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000254 CXIndex, const char *ast_filename
Steve Naroff600866c2009-08-27 19:51:58 +0000255);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000256
Ted Kremenek13745982009-10-19 22:15:09 +0000257/**
258 * \brief Destroy the specified CXTranslationUnit object.
259 */
John Thompson2e06fc82009-10-27 13:42:56 +0000260CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
Ted Kremenek13745982009-10-19 22:15:09 +0000261
262/**
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000263 * \brief Return the CXTranslationUnit for a given source file and the provided
264 * command line arguments one would pass to the compiler.
265 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000266 * Note: The 'source_filename' argument is optional. If the caller provides a
267 * NULL pointer, the name of the source file is expected to reside in the
268 * specified command line arguments.
Ted Kremenek139ba862009-10-22 00:03:57 +0000269 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000270 * Note: When encountered in 'clang_command_line_args', the following options
271 * are ignored:
Ted Kremenek139ba862009-10-22 00:03:57 +0000272 *
273 * '-c'
274 * '-emit-ast'
275 * '-fsyntax-only'
276 * '-o <output file>' (both '-o' and '<output file>' are ignored)
277 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000278 *
279 * \param source_filename - The name of the source file to load, or NULL if the
280 * source file is included in clang_command_line_args.
Ted Kremenek13745982009-10-19 22:15:09 +0000281 */
John Thompson2e06fc82009-10-27 13:42:56 +0000282CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile(
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000283 CXIndex CIdx,
284 const char *source_filename,
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000285 int num_clang_command_line_args,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000286 const char **clang_command_line_args
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000287);
Steve Naroff600866c2009-08-27 19:51:58 +0000288
289/*
290 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
291 within a translation unit, issuing a 'callback' for each one.
292
293 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
294 if (clang_getCursorKind(C) == Cursor_Declaration) {
295 CXDecl D = clang_getCursorDecl(C);
296 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
297 printf("@interface %s in file %s on line %d column %d\n",
298 clang_getDeclSpelling(D), clang_getCursorSource(C),
299 clang_getCursorLine(C), clang_getCursorColumn(C));
300 }
301 }
302 static void usage {
303 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000304 }
Steve Naroff600866c2009-08-27 19:51:58 +0000305*/
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000306typedef void *CXClientData;
307typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
308 CXClientData);
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000309CINDEX_LINKAGE void clang_loadTranslationUnit(CXTranslationUnit,
310 CXTranslationUnitIterator,
311 CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000312
313/*
314 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
315 'callback' for each declaration/reference within the respective declaration.
316
317 For interface declarations, this will index the super class, protocols,
318 ivars, methods, etc. For structure declarations, this will index the fields.
319 For functions, this will index the parameters (and body, for function
320 definitions), local declarations/references.
321
322 void getInterfaceDetails(CXDecl X, CXCursor C) {
323 switch (clang_getCursorKind(C)) {
324 case Cursor_ObjC_ClassRef:
325 CXDecl SuperClass = clang_getCursorDecl(C);
326 case Cursor_ObjC_ProtocolRef:
327 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
328 case Cursor_Declaration:
329 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
330 }
331 }
332 static void usage() {
333 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
334 clang_loadDeclaration(D, getInterfaceDetails);
335 }
336 }
337*/
Steve Naroffc857ea42009-09-02 13:28:54 +0000338typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
Steve Naroff89922f82009-08-31 00:59:03 +0000339
John Thompson2e06fc82009-10-27 13:42:56 +0000340CINDEX_LINKAGE void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000341
Steve Naroff50398192009-08-28 15:28:48 +0000342/*
Steve Naroff88145032009-10-27 14:35:18 +0000343 * CXFile Operations.
344 */
Douglas Gregor08b0e8d2009-10-31 15:48:08 +0000345CINDEX_LINKAGE const char *clang_getFileName(CXFile SFile);
346CINDEX_LINKAGE time_t clang_getFileTime(CXFile SFile);
Steve Naroff88145032009-10-27 14:35:18 +0000347
348/*
Steve Naroff50398192009-08-28 15:28:48 +0000349 * CXEntity Operations.
350 */
Ted Kremenek31723832010-01-11 23:56:39 +0000351
352/* clang_getDeclaration() maps from a CXEntity to the matching CXDecl (if any)
353 * in a specified translation unit. */
354CINDEX_LINKAGE CXDecl clang_getDeclaration(CXEntity, CXTranslationUnit);
355
Steve Naroff50398192009-08-28 15:28:48 +0000356/*
357 * CXDecl Operations.
358 */
John Thompson2e06fc82009-10-27 13:42:56 +0000359CINDEX_LINKAGE CXCursor clang_getCursorFromDecl(CXDecl);
Ted Kremenek31723832010-01-11 23:56:39 +0000360CINDEX_LINKAGE CXEntity clang_getEntityFromDecl(CXIndex, CXDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000361CINDEX_LINKAGE CXString clang_getDeclSpelling(CXDecl);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000362CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl); /* deprecate */
363CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl); /* deprecate */
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000364CINDEX_LINKAGE const char *clang_getDeclSource(CXDecl); /* deprecate */
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000365CINDEX_LINKAGE CXFile clang_getDeclSourceFile(CXDecl); /* deprecate */
Steve Naroff699a07d2009-09-25 21:32:34 +0000366
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000367/**
Douglas Gregor1db19de2010-01-19 21:36:55 +0000368 * \brief Identifies a specific source location within a translation
369 * unit.
370 *
371 * Use clang_getInstantiationLocation() to map a source location to a
372 * particular file, line, and column.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000373 */
374typedef struct {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000375 void *ptr_data;
376 unsigned int_data;
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000377} CXSourceLocation;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000378
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000379/**
Douglas Gregor1db19de2010-01-19 21:36:55 +0000380 * \brief Identifies a range of source locations in the source code.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000381 *
Douglas Gregor1db19de2010-01-19 21:36:55 +0000382 * Use clang_getRangeStart() and clang_getRangeEnd() to retrieve the
383 * starting and end locations from a source range, respectively.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000384 */
385typedef struct {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000386 void *ptr_data;
387 unsigned begin_int_data;
388 unsigned end_int_data;
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000389} CXSourceRange;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000390
Douglas Gregor1db19de2010-01-19 21:36:55 +0000391/**
392 * \brief Retrieve the file, line, and column represented by the
393 * given source location.
394 *
395 * \param location the location within a source file that will be
396 * decomposed into its parts.
397 *
398 * \param file if non-NULL, will be set to the file to which the given
399 * source location points.
400 *
401 * \param line if non-NULL, will be set to the line to which the given
402 * source location points.
403 *
404 * \param column if non-NULL, will be set to the column to which the
405 * given source location points.
406 */
407CINDEX_LINKAGE void clang_getInstantiationLocation(CXSourceLocation location,
408 CXFile *file,
409 unsigned *line,
410 unsigned *column);
411
412/**
413 * \brief Retrieve a source location representing the first
414 * character within a source range.
415 */
416CINDEX_LINKAGE CXSourceLocation clang_getRangeStart(CXSourceRange range);
417
418/**
419 * \brief Retrieve a source location representing the last
420 * character within a source range.
421 */
422CINDEX_LINKAGE CXSourceLocation clang_getRangeEnd(CXSourceRange range);
423
Daniel Dunbar3aacd532010-01-06 06:51:48 +0000424/* clang_getDeclExtent() returns the physical extent of a declaration. The
425 * beginning line/column pair points to the start of the first token in the
Ted Kremenekd8210652010-01-06 23:43:31 +0000426 * declaration, and the ending line/column pair points to the last character in
427 * the last token of the declaration.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000428 */
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000429CINDEX_LINKAGE CXSourceRange clang_getDeclExtent(CXDecl);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000430
Steve Naroff50398192009-08-28 15:28:48 +0000431/*
432 * CXCursor Operations.
433 */
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000434/**
435 Usage: clang_getCursor() will translate a source/line/column position
436 into an AST cursor (to derive semantic information from the source code).
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000437 */
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000438CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit,
439 const char *source_name,
440 unsigned line, unsigned column);
Ted Kremenek73885552009-11-17 19:28:59 +0000441
442CINDEX_LINKAGE CXCursor clang_getNullCursor(void);
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000443
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000444/* clang_getCursorUSR() returns the USR (if any) associated with entity referred to by the
445 * provided CXCursor object. */
446CINDEX_LINKAGE CXString clang_getCursorUSR(CXCursor);
447
John Thompson2e06fc82009-10-27 13:42:56 +0000448CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor);
449CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind);
450CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind);
John Thompson2e06fc82009-10-27 13:42:56 +0000451CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
Steve Naroff600866c2009-08-27 19:51:58 +0000452
Ted Kremenek73885552009-11-17 19:28:59 +0000453CINDEX_LINKAGE unsigned clang_equalCursors(CXCursor, CXCursor);
454
Steve Naroffef0cef62009-11-09 17:45:52 +0000455CINDEX_LINKAGE CXString clang_getCursorSpelling(CXCursor);
Steve Narofff334b4e2009-09-02 18:26:48 +0000456
Douglas Gregor98258af2010-01-18 22:46:11 +0000457/**
458 * \brief Retrieve the physical location of the source constructor referenced
459 * by the given cursor.
460 *
461 * The location of a declaration is typically the location of the name of that
462 * declaration, where the name of that declaration would occur if it is
463 * unnamed, or some keyword that introduces that particular declaration.
464 * The location of a reference is where that reference occurs within the
465 * source code.
466 */
467CINDEX_LINKAGE CXSourceLocation clang_getCursorLocation(CXCursor);
468
Douglas Gregorb6998662010-01-19 19:34:47 +0000469/**
470 * \brief Retrieve the physical extent of the source construct referenced by
Douglas Gregora7bde202010-01-19 00:34:46 +0000471 * the given cursor.
472 *
473 * The extent of a cursor starts with the file/line/column pointing at the
474 * first character within the source construct that the cursor refers to and
475 * ends with the last character withinin that source construct. For a
476 * declaration, the extent covers the declaration itself. For a reference,
477 * the extent covers the location of the reference (e.g., where the referenced
478 * entity was actually used).
479 */
480CINDEX_LINKAGE CXSourceRange clang_getCursorExtent(CXCursor);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000481
482/** \brief For a cursor that is a reference, retrieve a cursor representing the
483 * entity that it references.
484 *
485 * Reference cursors refer to other entities in the AST. For example, an
486 * Objective-C superclass reference cursor refers to an Objective-C class.
487 * This function produces the cursor for the Objective-C class from the
488 * cursor for the superclass reference. If the input cursor is a declaration or
489 * definition, it returns that declaration or definition unchanged.
490 * Othewise, returns the NULL cursor.
491 */
492CINDEX_LINKAGE CXCursor clang_getCursorReferenced(CXCursor);
Douglas Gregorb6998662010-01-19 19:34:47 +0000493
494/**
495 * \brief For a cursor that is either a reference to or a declaration
496 * of some entity, retrieve a cursor that describes the definition of
497 * that entity.
498 *
499 * Some entities can be declared multiple times within a translation
500 * unit, but only one of those declarations can also be a
501 * definition. For example, given:
502 *
503 * \code
504 * int f(int, int);
505 * int g(int x, int y) { return f(x, y); }
506 * int f(int a, int b) { return a + b; }
507 * int f(int, int);
508 * \endcode
509 *
510 * there are three declarations of the function "f", but only the
511 * second one is a definition. The clang_getCursorDefinition()
512 * function will take any cursor pointing to a declaration of "f"
513 * (the first or fourth lines of the example) or a cursor referenced
514 * that uses "f" (the call to "f' inside "g") and will return a
515 * declaration cursor pointing to the definition (the second "f"
516 * declaration).
517 *
518 * If given a cursor for which there is no corresponding definition,
519 * e.g., because there is no definition of that entity within this
520 * translation unit, returns a NULL cursor.
521 */
522CINDEX_LINKAGE CXCursor clang_getCursorDefinition(CXCursor);
523
524/**
525 * \brief Determine whether the declaration pointed to by this cursor
526 * is also a definition of that entity.
527 */
528CINDEX_LINKAGE unsigned clang_isCursorDefinition(CXCursor);
529
Steve Naroff4ade6d62009-09-23 17:52:52 +0000530/* for debug/testing */
John Thompson2e06fc82009-10-27 13:42:56 +0000531CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind);
532CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000533 const char **startBuf,
534 const char **endBuf,
535 unsigned *startLine,
536 unsigned *startColumn,
537 unsigned *endLine,
538 unsigned *endColumn);
Steve Naroff600866c2009-08-27 19:51:58 +0000539
Steve Naroff50398192009-08-28 15:28:48 +0000540/*
Chris Lattner459789b2009-09-16 20:18:54 +0000541 * If CXCursorKind == Cursor_Reference, then this will return the referenced
542 * declaration.
Steve Naroff50398192009-08-28 15:28:48 +0000543 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
544 */
John Thompson2e06fc82009-10-27 13:42:56 +0000545CINDEX_LINKAGE CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000546
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000547/**
548 * \brief A semantic string that describes a code-completion result.
549 *
550 * A semantic string that describes the formatting of a code-completion
551 * result as a single "template" of text that should be inserted into the
552 * source buffer when a particular code-completion result is selected.
553 * Each semantic string is made up of some number of "chunks", each of which
554 * contains some text along with a description of what that text means, e.g.,
555 * the name of the entity being referenced, whether the text chunk is part of
556 * the template, or whether it is a "placeholder" that the user should replace
557 * with actual code,of a specific kind. See \c CXCompletionChunkKind for a
558 * description of the different kinds of chunks.
559 */
560typedef void *CXCompletionString;
561
562/**
563 * \brief A single result of code completion.
564 */
565typedef struct {
566 /**
567 * \brief The kind of entity that this completion refers to.
568 *
569 * The cursor kind will be a macro, keyword, or a declaration (one of the
570 * *Decl cursor kinds), describing the entity that the completion is
571 * referring to.
572 *
573 * \todo In the future, we would like to provide a full cursor, to allow
574 * the client to extract additional information from declaration.
575 */
576 enum CXCursorKind CursorKind;
577
578 /**
579 * \brief The code-completion string that describes how to insert this
580 * code-completion result into the editing buffer.
581 */
582 CXCompletionString CompletionString;
583} CXCompletionResult;
584
585/**
586 * \brief Describes a single piece of text within a code-completion string.
587 *
588 * Each "chunk" within a code-completion string (\c CXCompletionString) is
589 * either a piece of text with a specific "kind" that describes how that text
590 * should be interpreted by the client or is another completion string.
591 */
592enum CXCompletionChunkKind {
593 /**
594 * \brief A code-completion string that describes "optional" text that
595 * could be a part of the template (but is not required).
596 *
597 * The Optional chunk is the only kind of chunk that has a code-completion
598 * string for its representation, which is accessible via
599 * \c clang_getCompletionChunkCompletionString(). The code-completion string
600 * describes an additional part of the template that is completely optional.
601 * For example, optional chunks can be used to describe the placeholders for
602 * arguments that match up with defaulted function parameters, e.g. given:
603 *
604 * \code
605 * void f(int x, float y = 3.14, double z = 2.71828);
606 * \endcode
607 *
608 * The code-completion string for this function would contain:
609 * - a TypedText chunk for "f".
610 * - a LeftParen chunk for "(".
611 * - a Placeholder chunk for "int x"
612 * - an Optional chunk containing the remaining defaulted arguments, e.g.,
613 * - a Comma chunk for ","
614 * - a Placeholder chunk for "float x"
615 * - an Optional chunk containing the last defaulted argument:
616 * - a Comma chunk for ","
617 * - a Placeholder chunk for "double z"
618 * - a RightParen chunk for ")"
619 *
620 * There are many ways two handle Optional chunks. Two simple approaches are:
621 * - Completely ignore optional chunks, in which case the template for the
622 * function "f" would only include the first parameter ("int x").
623 * - Fully expand all optional chunks, in which case the template for the
624 * function "f" would have all of the parameters.
625 */
626 CXCompletionChunk_Optional,
627 /**
628 * \brief Text that a user would be expected to type to get this
629 * code-completion result.
630 *
631 * There will be exactly one "typed text" chunk in a semantic string, which
632 * will typically provide the spelling of a keyword or the name of a
633 * declaration that could be used at the current code point. Clients are
634 * expected to filter the code-completion results based on the text in this
635 * chunk.
636 */
637 CXCompletionChunk_TypedText,
638 /**
639 * \brief Text that should be inserted as part of a code-completion result.
640 *
641 * A "text" chunk represents text that is part of the template to be
642 * inserted into user code should this particular code-completion result
643 * be selected.
644 */
645 CXCompletionChunk_Text,
646 /**
647 * \brief Placeholder text that should be replaced by the user.
648 *
649 * A "placeholder" chunk marks a place where the user should insert text
650 * into the code-completion template. For example, placeholders might mark
651 * the function parameters for a function declaration, to indicate that the
652 * user should provide arguments for each of those parameters. The actual
653 * text in a placeholder is a suggestion for the text to display before
654 * the user replaces the placeholder with real code.
655 */
656 CXCompletionChunk_Placeholder,
657 /**
658 * \brief Informative text that should be displayed but never inserted as
659 * part of the template.
660 *
661 * An "informative" chunk contains annotations that can be displayed to
662 * help the user decide whether a particular code-completion result is the
663 * right option, but which is not part of the actual template to be inserted
664 * by code completion.
665 */
666 CXCompletionChunk_Informative,
667 /**
668 * \brief Text that describes the current parameter when code-completion is
669 * referring to function call, message send, or template specialization.
670 *
671 * A "current parameter" chunk occurs when code-completion is providing
672 * information about a parameter corresponding to the argument at the
673 * code-completion point. For example, given a function
674 *
675 * \code
676 * int add(int x, int y);
677 * \endcode
678 *
679 * and the source code \c add(, where the code-completion point is after the
680 * "(", the code-completion string will contain a "current parameter" chunk
681 * for "int x", indicating that the current argument will initialize that
682 * parameter. After typing further, to \c add(17, (where the code-completion
683 * point is after the ","), the code-completion string will contain a
684 * "current paremeter" chunk to "int y".
685 */
686 CXCompletionChunk_CurrentParameter,
687 /**
688 * \brief A left parenthesis ('('), used to initiate a function call or
689 * signal the beginning of a function parameter list.
690 */
691 CXCompletionChunk_LeftParen,
692 /**
693 * \brief A right parenthesis (')'), used to finish a function call or
694 * signal the end of a function parameter list.
695 */
696 CXCompletionChunk_RightParen,
697 /**
698 * \brief A left bracket ('[').
699 */
700 CXCompletionChunk_LeftBracket,
701 /**
702 * \brief A right bracket (']').
703 */
704 CXCompletionChunk_RightBracket,
705 /**
706 * \brief A left brace ('{').
707 */
708 CXCompletionChunk_LeftBrace,
709 /**
710 * \brief A right brace ('}').
711 */
712 CXCompletionChunk_RightBrace,
713 /**
714 * \brief A left angle bracket ('<').
715 */
716 CXCompletionChunk_LeftAngle,
717 /**
718 * \brief A right angle bracket ('>').
719 */
720 CXCompletionChunk_RightAngle,
721 /**
722 * \brief A comma separator (',').
723 */
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000724 CXCompletionChunk_Comma,
725 /**
726 * \brief Text that specifies the result type of a given result.
727 *
728 * This special kind of informative chunk is not meant to be inserted into
729 * the text buffer. Rather, it is meant to illustrate the type that an
730 * expression using the given completion string would have.
731 */
Douglas Gregor01dfea02010-01-10 23:08:15 +0000732 CXCompletionChunk_ResultType,
733 /**
734 * \brief A colon (':').
735 */
736 CXCompletionChunk_Colon,
737 /**
738 * \brief A semicolon (';').
739 */
740 CXCompletionChunk_SemiColon,
741 /**
742 * \brief An '=' sign.
743 */
744 CXCompletionChunk_Equal,
745 /**
746 * Horizontal space (' ').
747 */
748 CXCompletionChunk_HorizontalSpace,
749 /**
750 * Vertical space ('\n'), after which it is generally a good idea to
751 * perform indentation.
752 */
753 CXCompletionChunk_VerticalSpace
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000754};
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000755
756/**
757 * \brief Determine the kind of a particular chunk within a completion string.
758 *
759 * \param completion_string the completion string to query.
760 *
761 * \param chunk_number the 0-based index of the chunk in the completion string.
762 *
763 * \returns the kind of the chunk at the index \c chunk_number.
764 */
765CINDEX_LINKAGE enum CXCompletionChunkKind
766clang_getCompletionChunkKind(CXCompletionString completion_string,
767 unsigned chunk_number);
768
769/**
770 * \brief Retrieve the text associated with a particular chunk within a
771 * completion string.
772 *
773 * \param completion_string the completion string to query.
774 *
775 * \param chunk_number the 0-based index of the chunk in the completion string.
776 *
777 * \returns the text associated with the chunk at index \c chunk_number.
778 */
779CINDEX_LINKAGE const char *
780clang_getCompletionChunkText(CXCompletionString completion_string,
781 unsigned chunk_number);
782
783/**
784 * \brief Retrieve the completion string associated with a particular chunk
785 * within a completion string.
786 *
787 * \param completion_string the completion string to query.
788 *
789 * \param chunk_number the 0-based index of the chunk in the completion string.
790 *
791 * \returns the completion string associated with the chunk at index
792 * \c chunk_number, or NULL if that chunk is not represented by a completion
793 * string.
794 */
795CINDEX_LINKAGE CXCompletionString
796clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
797 unsigned chunk_number);
798
799/**
800 * \brief Retrieve the number of chunks in the given code-completion string.
801 */
802CINDEX_LINKAGE unsigned
803clang_getNumCompletionChunks(CXCompletionString completion_string);
804
805/**
Douglas Gregorec6762c2009-12-18 16:20:58 +0000806 * \brief Contains the results of code-completion.
807 *
808 * This data structure contains the results of code completion, as
809 * produced by \c clang_codeComplete. Its contents must be freed by
810 * \c clang_disposeCodeCompleteResults.
811 */
812typedef struct {
813 /**
814 * \brief The code-completion results.
815 */
816 CXCompletionResult *Results;
817
818 /**
819 * \brief The number of code-completion results stored in the
820 * \c Results array.
821 */
822 unsigned NumResults;
823} CXCodeCompleteResults;
824
825/**
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000826 * \brief Perform code completion at a given location in a source file.
827 *
828 * This function performs code completion at a particular file, line, and
829 * column within source code, providing results that suggest potential
830 * code snippets based on the context of the completion. The basic model
831 * for code completion is that Clang will parse a complete source file,
832 * performing syntax checking up to the location where code-completion has
833 * been requested. At that point, a special code-completion token is passed
834 * to the parser, which recognizes this token and determines, based on the
835 * current location in the C/Objective-C/C++ grammar and the state of
836 * semantic analysis, what completions to provide. These completions are
Douglas Gregorec6762c2009-12-18 16:20:58 +0000837 * returned via a new \c CXCodeCompleteResults structure.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000838 *
839 * Code completion itself is meant to be triggered by the client when the
840 * user types punctuation characters or whitespace, at which point the
841 * code-completion location will coincide with the cursor. For example, if \c p
842 * is a pointer, code-completion might be triggered after the "-" and then
843 * after the ">" in \c p->. When the code-completion location is afer the ">",
844 * the completion results will provide, e.g., the members of the struct that
845 * "p" points to. The client is responsible for placing the cursor at the
846 * beginning of the token currently being typed, then filtering the results
847 * based on the contents of the token. For example, when code-completing for
848 * the expression \c p->get, the client should provide the location just after
849 * the ">" (e.g., pointing at the "g") to this code-completion hook. Then, the
850 * client can filter the results based on the current token text ("get"), only
851 * showing those results that start with "get". The intent of this interface
Douglas Gregorec6762c2009-12-18 16:20:58 +0000852 * is to separate the relatively high-latency acquisition of code-completion
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000853 * results from the filtering of results on a per-character basis, which must
854 * have a lower latency.
855 *
856 * \param CIdx the \c CXIndex instance that will be used to perform code
857 * completion.
858 *
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000859 * \param source_filename the name of the source file that should be parsed to
860 * perform code-completion. This source file must be the same as or include the
861 * filename described by \p complete_filename, or no code-completion results
862 * will be produced. NOTE: One can also specify NULL for this argument if the
863 * source file is included in command_line_args.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000864 *
865 * \param num_command_line_args the number of command-line arguments stored in
866 * \p command_line_args.
867 *
868 * \param command_line_args the command-line arguments to pass to the Clang
869 * compiler to build the given source file. This should include all of the
870 * necessary include paths, language-dialect switches, precompiled header
871 * includes, etc., but should not include any information specific to
872 * code completion.
873 *
Douglas Gregor735df882009-12-02 09:21:34 +0000874 * \param num_unsaved_files the number of unsaved file entries in \p
875 * unsaved_files.
876 *
877 * \param unsaved_files the files that have not yet been saved to disk
878 * but may be required for code completion, including the contents of
879 * those files.
880 *
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000881 * \param complete_filename the name of the source file where code completion
882 * should be performed. In many cases, this name will be the same as the
883 * source filename. However, the completion filename may also be a file
884 * included by the source file, which is required when producing
885 * code-completion results for a header.
886 *
887 * \param complete_line the line at which code-completion should occur.
888 *
889 * \param complete_column the column at which code-completion should occur.
890 * Note that the column should point just after the syntactic construct that
891 * initiated code completion, and not in the middle of a lexical token.
892 *
Douglas Gregorec6762c2009-12-18 16:20:58 +0000893 * \returns if successful, a new CXCodeCompleteResults structure
894 * containing code-completion results, which should eventually be
895 * freed with \c clang_disposeCodeCompleteResults(). If code
896 * completion fails, returns NULL.
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000897 */
Douglas Gregorec6762c2009-12-18 16:20:58 +0000898CINDEX_LINKAGE
899CXCodeCompleteResults *clang_codeComplete(CXIndex CIdx,
900 const char *source_filename,
901 int num_command_line_args,
902 const char **command_line_args,
903 unsigned num_unsaved_files,
904 struct CXUnsavedFile *unsaved_files,
905 const char *complete_filename,
906 unsigned complete_line,
907 unsigned complete_column);
908
909/**
910 * \brief Free the given set of code-completion results.
911 */
912CINDEX_LINKAGE
913void clang_disposeCodeCompleteResults(CXCodeCompleteResults *Results);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000914
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000915#ifdef __cplusplus
916}
917#endif
918#endif
919