blob: cbc1df9fa3ab79353ed5b97d840642f288594647 [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
19#ifdef __cplusplus
20extern "C" {
21#endif
22
John Thompson2e06fc82009-10-27 13:42:56 +000023// MSVC DLL import/export.
24#ifdef _MSC_VER
25 #ifdef _CINDEX_LIB_
26 #define CINDEX_LINKAGE __declspec(dllexport)
27 #else
28 #define CINDEX_LINKAGE __declspec(dllimport)
29 #endif
30#else
31 #define CINDEX_LINKAGE
32#endif
33
Steve Naroff600866c2009-08-27 19:51:58 +000034/*
35 Clang indeX abstractions. The backing store for the following API's will be
Steve Naroffb7cd17c2009-09-01 17:13:31 +000036 clangs AST file (currently based on PCH). AST files are created as follows:
Steve Naroff600866c2009-08-27 19:51:58 +000037
Steve Naroffb7cd17c2009-09-01 17:13:31 +000038 "clang -emit-ast <sourcefile.langsuffix> -o <sourcefile.ast>".
Steve Naroff600866c2009-08-27 19:51:58 +000039
Steve Naroff600866c2009-08-27 19:51:58 +000040 Naming Conventions: To avoid namespace pollution, data types are prefixed
41 with "CX" and functions are prefixed with "clang_".
42*/
Steve Naroff50398192009-08-28 15:28:48 +000043typedef void *CXIndex; /* An indexing instance. */
Steve Naroff600866c2009-08-27 19:51:58 +000044
Steve Naroff50398192009-08-28 15:28:48 +000045typedef void *CXTranslationUnit; /* A translation unit instance. */
Steve Naroff600866c2009-08-27 19:51:58 +000046
Steve Naroff50398192009-08-28 15:28:48 +000047typedef void *CXDecl; /* A specific declaration within a translation unit. */
Steve Narofffb570422009-09-22 19:25:29 +000048typedef void *CXStmt; /* A specific statement within a function/method */
Steve Naroff600866c2009-08-27 19:51:58 +000049
Steve Naroffc857ea42009-09-02 13:28:54 +000050/* Cursors represent declarations, definitions, and references. */
Steve Naroff89922f82009-08-31 00:59:03 +000051enum CXCursorKind {
Steve Naroff89922f82009-08-31 00:59:03 +000052 /* Declarations */
53 CXCursor_FirstDecl = 1,
Steve Naroffc857ea42009-09-02 13:28:54 +000054 CXCursor_TypedefDecl = 2,
55 CXCursor_StructDecl = 3,
56 CXCursor_UnionDecl = 4,
57 CXCursor_ClassDecl = 5,
58 CXCursor_EnumDecl = 6,
59 CXCursor_FieldDecl = 7,
60 CXCursor_EnumConstantDecl = 8,
61 CXCursor_FunctionDecl = 9,
62 CXCursor_VarDecl = 10,
63 CXCursor_ParmDecl = 11,
64 CXCursor_ObjCInterfaceDecl = 12,
65 CXCursor_ObjCCategoryDecl = 13,
66 CXCursor_ObjCProtocolDecl = 14,
67 CXCursor_ObjCPropertyDecl = 15,
68 CXCursor_ObjCIvarDecl = 16,
69 CXCursor_ObjCInstanceMethodDecl = 17,
70 CXCursor_ObjCClassMethodDecl = 18,
71 CXCursor_LastDecl = 18,
Steve Naroff89922f82009-08-31 00:59:03 +000072
Steve Naroffc857ea42009-09-02 13:28:54 +000073 /* Definitions */
74 CXCursor_FirstDefn = 32,
75 CXCursor_FunctionDefn = 32,
76 CXCursor_ObjCClassDefn = 33,
77 CXCursor_ObjCCategoryDefn = 34,
78 CXCursor_ObjCInstanceMethodDefn = 35,
79 CXCursor_ObjCClassMethodDefn = 36,
80 CXCursor_LastDefn = 36,
81
Steve Naroff89922f82009-08-31 00:59:03 +000082 /* References */
Steve Narofffb570422009-09-22 19:25:29 +000083 CXCursor_FirstRef = 40, /* Decl references */
Steve Narofff334b4e2009-09-02 18:26:48 +000084 CXCursor_ObjCSuperClassRef = 40,
85 CXCursor_ObjCProtocolRef = 41,
Steve Narofffb570422009-09-22 19:25:29 +000086 CXCursor_ObjCClassRef = 42,
87
88 CXCursor_ObjCSelectorRef = 43, /* Expression references */
89 CXCursor_ObjCIvarRef = 44,
90 CXCursor_VarRef = 45,
91 CXCursor_FunctionRef = 46,
92 CXCursor_EnumConstantRef = 47,
93 CXCursor_MemberRef = 48,
94 CXCursor_LastRef = 48,
Steve Naroff77128dd2009-09-15 20:25:34 +000095
96 /* Error conditions */
97 CXCursor_FirstInvalid = 70,
98 CXCursor_InvalidFile = 70,
99 CXCursor_NoDeclFound = 71,
100 CXCursor_NotImplemented = 72,
101 CXCursor_LastInvalid = 72
Steve Naroff600866c2009-08-27 19:51:58 +0000102};
103
Steve Naroff89922f82009-08-31 00:59:03 +0000104/* A cursor into the CXTranslationUnit. */
Steve Narofffb570422009-09-22 19:25:29 +0000105
Steve Naroff89922f82009-08-31 00:59:03 +0000106typedef struct {
107 enum CXCursorKind kind;
108 CXDecl decl;
Steve Narofffb570422009-09-22 19:25:29 +0000109 CXStmt stmt; /* expression reference */
Steve Naroff89922f82009-08-31 00:59:03 +0000110} CXCursor;
111
Steve Naroff50398192009-08-28 15:28:48 +0000112/* A unique token for looking up "visible" CXDecls from a CXTranslationUnit. */
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000113typedef void *CXEntity;
Steve Naroff600866c2009-08-27 19:51:58 +0000114
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000115/**
116 * \brief clang_createIndex() provides a shared context for creating
117 * translation units. It provides two options:
118 *
119 * - excludeDeclarationsFromPCH: When non-zero, allows enumeration of "local"
120 * declarations (when loading any new translation units). A "local" declaration
121 * is one that belongs in the translation unit itself and not in a precompiled
122 * header that was used by the translation unit. If zero, all declarations
123 * will be enumerated.
124 *
125 * - displayDiagnostics: when non-zero, diagnostics will be output. If zero,
126 * diagnostics will be ignored.
Steve Naroffb4ece632009-10-20 16:36:34 +0000127 *
128 * Here is an example:
129 *
130 * // excludeDeclsFromPCH = 1, displayDiagnostics = 1
131 * Idx = clang_createIndex(1, 1);
132 *
133 * // IndexTest.pch was produced with the following command:
134 * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch"
135 * TU = clang_createTranslationUnit(Idx, "IndexTest.pch");
136 *
137 * // This will load all the symbols from 'IndexTest.pch'
138 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
139 * clang_disposeTranslationUnit(TU);
140 *
141 * // This will load all the symbols from 'IndexTest.c', excluding symbols
142 * // from 'IndexTest.pch'.
143 * char *args[] = { "-Xclang", "-include-pch=IndexTest.pch", 0 };
144 * TU = clang_createTranslationUnitFromSourceFile(Idx, "IndexTest.c", 2, args);
145 * clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
146 * clang_disposeTranslationUnit(TU);
147 *
148 * This process of creating the 'pch', loading it separately, and using it (via
149 * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks
150 * (which gives the indexer the same performance benefit as the compiler).
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000151 */
John Thompson2e06fc82009-10-27 13:42:56 +0000152CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000153 int displayDiagnostics);
John Thompson2e06fc82009-10-27 13:42:56 +0000154CINDEX_LINKAGE void clang_disposeIndex(CXIndex);
Steve Naroff600866c2009-08-27 19:51:58 +0000155
John Thompson2e06fc82009-10-27 13:42:56 +0000156CINDEX_LINKAGE const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000157
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000158/*
159 * \brief Create a translation unit from an AST file (-emit-ast).
160 */
John Thompson2e06fc82009-10-27 13:42:56 +0000161CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000162 CXIndex, const char *ast_filename
Steve Naroff600866c2009-08-27 19:51:58 +0000163);
Ted Kremenek13745982009-10-19 22:15:09 +0000164/**
165 * \brief Destroy the specified CXTranslationUnit object.
166 */
John Thompson2e06fc82009-10-27 13:42:56 +0000167CINDEX_LINKAGE void clang_disposeTranslationUnit(CXTranslationUnit);
Ted Kremenek13745982009-10-19 22:15:09 +0000168
169/**
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000170 * \brief Return the CXTranslationUnit for a given source file and the provided
171 * command line arguments one would pass to the compiler.
172 *
Ted Kremenek139ba862009-10-22 00:03:57 +0000173 * Note: The 'source_filename' argument is optional. If the caller provides a NULL pointer,
174 * the name of the source file is expected to reside in the specified command line arguments.
175 *
176 * Note: When encountered in 'clang_command_line_args', the following options are ignored:
177 *
178 * '-c'
179 * '-emit-ast'
180 * '-fsyntax-only'
181 * '-o <output file>' (both '-o' and '<output file>' are ignored)
182 *
Ted Kremenek13745982009-10-19 22:15:09 +0000183 */
John Thompson2e06fc82009-10-27 13:42:56 +0000184CINDEX_LINKAGE CXTranslationUnit clang_createTranslationUnitFromSourceFile(
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000185 CXIndex CIdx,
Ted Kremenek139ba862009-10-22 00:03:57 +0000186 const char *source_filename /* specify NULL if the source file is in clang_command_line_args */,
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000187 int num_clang_command_line_args,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000188 const char **clang_command_line_args
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000189);
Steve Naroff600866c2009-08-27 19:51:58 +0000190
191/*
192 Usage: clang_loadTranslationUnit(). Will load the toplevel declarations
193 within a translation unit, issuing a 'callback' for each one.
194
195 void printObjCInterfaceNames(CXTranslationUnit X, CXCursor C) {
196 if (clang_getCursorKind(C) == Cursor_Declaration) {
197 CXDecl D = clang_getCursorDecl(C);
198 if (clang_getDeclKind(D) == CXDecl_ObjC_interface)
199 printf("@interface %s in file %s on line %d column %d\n",
200 clang_getDeclSpelling(D), clang_getCursorSource(C),
201 clang_getCursorLine(C), clang_getCursorColumn(C));
202 }
203 }
204 static void usage {
205 clang_loadTranslationUnit(CXTranslationUnit, printObjCInterfaceNames);
206 }
207*/
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000208typedef void *CXClientData;
209typedef void (*CXTranslationUnitIterator)(CXTranslationUnit, CXCursor,
210 CXClientData);
John Thompson2e06fc82009-10-27 13:42:56 +0000211CINDEX_LINKAGE void clang_loadTranslationUnit(CXTranslationUnit, CXTranslationUnitIterator,
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000212 CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000213
214/*
215 Usage: clang_loadDeclaration(). Will load the declaration, issuing a
216 'callback' for each declaration/reference within the respective declaration.
217
218 For interface declarations, this will index the super class, protocols,
219 ivars, methods, etc. For structure declarations, this will index the fields.
220 For functions, this will index the parameters (and body, for function
221 definitions), local declarations/references.
222
223 void getInterfaceDetails(CXDecl X, CXCursor C) {
224 switch (clang_getCursorKind(C)) {
225 case Cursor_ObjC_ClassRef:
226 CXDecl SuperClass = clang_getCursorDecl(C);
227 case Cursor_ObjC_ProtocolRef:
228 CXDecl AdoptsProtocol = clang_getCursorDecl(C);
229 case Cursor_Declaration:
230 CXDecl AnIvarOrMethod = clang_getCursorDecl(C);
231 }
232 }
233 static void usage() {
234 if (clang_getDeclKind(D) == CXDecl_ObjC_interface) {
235 clang_loadDeclaration(D, getInterfaceDetails);
236 }
237 }
238*/
Steve Naroffc857ea42009-09-02 13:28:54 +0000239typedef void (*CXDeclIterator)(CXDecl, CXCursor, CXClientData);
Steve Naroff89922f82009-08-31 00:59:03 +0000240
John Thompson2e06fc82009-10-27 13:42:56 +0000241CINDEX_LINKAGE void clang_loadDeclaration(CXDecl, CXDeclIterator, CXClientData);
Steve Naroff600866c2009-08-27 19:51:58 +0000242
Steve Naroff50398192009-08-28 15:28:48 +0000243/*
244 * CXEntity Operations.
245 */
John Thompson2e06fc82009-10-27 13:42:56 +0000246CINDEX_LINKAGE const char *clang_getDeclarationName(CXEntity);
247CINDEX_LINKAGE const char *clang_getURI(CXEntity);
248CINDEX_LINKAGE CXEntity clang_getEntity(const char *URI);
Steve Naroff50398192009-08-28 15:28:48 +0000249/*
250 * CXDecl Operations.
251 */
John Thompson2e06fc82009-10-27 13:42:56 +0000252CINDEX_LINKAGE CXCursor clang_getCursorFromDecl(CXDecl);
253CINDEX_LINKAGE CXEntity clang_getEntityFromDecl(CXDecl);
254CINDEX_LINKAGE const char *clang_getDeclSpelling(CXDecl);
255CINDEX_LINKAGE unsigned clang_getDeclLine(CXDecl);
256CINDEX_LINKAGE unsigned clang_getDeclColumn(CXDecl);
257CINDEX_LINKAGE const char *clang_getDeclSource(CXDecl);
Steve Naroff699a07d2009-09-25 21:32:34 +0000258
Steve Naroff50398192009-08-28 15:28:48 +0000259/*
260 * CXCursor Operations.
261 */
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000262/**
263 Usage: clang_getCursor() will translate a source/line/column position
264 into an AST cursor (to derive semantic information from the source code).
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000265 */
John Thompson2e06fc82009-10-27 13:42:56 +0000266CINDEX_LINKAGE CXCursor clang_getCursor(CXTranslationUnit, const char *source_name,
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000267 unsigned line, unsigned column);
268
269/**
270 Usage: clang_getCursorWithHint() provides the same functionality as
271 clang_getCursor() except that it takes an option 'hint' argument.
272 The 'hint' is a temporary CXLookupHint object (whose lifetime is managed by
273 the caller) that should be initialized with clang_initCXLookupHint().
274
275 FIXME: Add a better comment once getCursorWithHint() has more functionality.
276 */
277typedef CXCursor CXLookupHint;
John Thompson2e06fc82009-10-27 13:42:56 +0000278CINDEX_LINKAGE CXCursor clang_getCursorWithHint(CXTranslationUnit, const char *source_name,
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000279 unsigned line, unsigned column,
280 CXLookupHint *hint);
281
John Thompson2e06fc82009-10-27 13:42:56 +0000282CINDEX_LINKAGE void clang_initCXLookupHint(CXLookupHint *hint);
Steve Naroff600866c2009-08-27 19:51:58 +0000283
John Thompson2e06fc82009-10-27 13:42:56 +0000284CINDEX_LINKAGE enum CXCursorKind clang_getCursorKind(CXCursor);
285CINDEX_LINKAGE unsigned clang_isDeclaration(enum CXCursorKind);
286CINDEX_LINKAGE unsigned clang_isReference(enum CXCursorKind);
287CINDEX_LINKAGE unsigned clang_isDefinition(enum CXCursorKind);
288CINDEX_LINKAGE unsigned clang_isInvalid(enum CXCursorKind);
Steve Naroff600866c2009-08-27 19:51:58 +0000289
John Thompson2e06fc82009-10-27 13:42:56 +0000290CINDEX_LINKAGE unsigned clang_getCursorLine(CXCursor);
291CINDEX_LINKAGE unsigned clang_getCursorColumn(CXCursor);
292CINDEX_LINKAGE const char *clang_getCursorSource(CXCursor);
293CINDEX_LINKAGE const char *clang_getCursorSpelling(CXCursor);
Steve Narofff334b4e2009-09-02 18:26:48 +0000294
Steve Naroff4ade6d62009-09-23 17:52:52 +0000295/* for debug/testing */
John Thompson2e06fc82009-10-27 13:42:56 +0000296CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind);
297CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000298 const char **startBuf,
299 const char **endBuf,
300 unsigned *startLine,
301 unsigned *startColumn,
302 unsigned *endLine,
303 unsigned *endColumn);
Steve Naroff600866c2009-08-27 19:51:58 +0000304
Steve Naroff50398192009-08-28 15:28:48 +0000305/*
Chris Lattner459789b2009-09-16 20:18:54 +0000306 * If CXCursorKind == Cursor_Reference, then this will return the referenced
307 * declaration.
Steve Naroff50398192009-08-28 15:28:48 +0000308 * If CXCursorKind == Cursor_Declaration, then this will return the declaration.
309 */
John Thompson2e06fc82009-10-27 13:42:56 +0000310CINDEX_LINKAGE CXDecl clang_getCursorDecl(CXCursor);
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000311
312#ifdef __cplusplus
313}
314#endif
315#endif
316