blob: b2fca60a6f7c0fcd6750c7a5b3f1848e330e9cd0 [file] [log] [blame]
Ted Kremenekb60d87c2009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
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 file implements the Clang-C Source Indexing library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang-c/Index.h"
Steve Naroffa1c72842009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroffef9618b2009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Steve Naroffa1c72842009-08-28 15:28:48 +000019#include "clang/AST/DeclVisitor.h"
Steve Naroff3645f5a2009-09-02 13:28:54 +000020#include "clang/AST/Decl.h"
Benjamin Kramer8b83f5d2009-08-29 12:56:35 +000021#include "clang/Basic/FileManager.h"
Steve Naroff772c1a42009-08-31 14:26:51 +000022#include "clang/Basic/SourceManager.h"
Benjamin Kramer8b83f5d2009-08-29 12:56:35 +000023#include "clang/Frontend/ASTUnit.h"
24#include <cstdio>
Steve Naroffa1c72842009-08-28 15:28:48 +000025using namespace clang;
26using namespace idx;
27
Steve Naroff1054e602009-08-31 00:59:03 +000028namespace {
29
30// Translation Unit Visitor.
31class TUVisitor : public DeclVisitor<TUVisitor> {
32 CXTranslationUnit TUnit;
33 CXTranslationUnitIterator Callback;
Steve Naroff69b10fd2009-09-01 15:55:40 +000034 CXClientData CData;
35
36 void Call(enum CXCursorKind CK, NamedDecl *ND) {
37 CXCursor C = { CK, ND };
38 Callback(TUnit, C, CData);
39 }
Steve Naroff1054e602009-08-31 00:59:03 +000040public:
Steve Naroff69b10fd2009-09-01 15:55:40 +000041 TUVisitor(CXTranslationUnit CTU,
42 CXTranslationUnitIterator cback, CXClientData D) :
43 TUnit(CTU), Callback(cback), CData(D) {}
Steve Naroff1054e602009-08-31 00:59:03 +000044
45 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
46 VisitDeclContext(dyn_cast<DeclContext>(D));
47 }
48 void VisitDeclContext(DeclContext *DC) {
49 for (DeclContext::decl_iterator
50 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
51 Visit(*I);
52 }
Steve Naroff69b10fd2009-09-01 15:55:40 +000053 void VisitTypedefDecl(TypedefDecl *ND) {
54 Call(CXCursor_TypedefDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +000055 }
56 void VisitTagDecl(TagDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +000057 switch (ND->getTagKind()) {
58 case TagDecl::TK_struct:
59 Call(CXCursor_StructDecl, ND);
60 break;
61 case TagDecl::TK_class:
62 Call(CXCursor_ClassDecl, ND);
63 break;
64 case TagDecl::TK_union:
65 Call(CXCursor_UnionDecl, ND);
66 break;
67 case TagDecl::TK_enum:
68 Call(CXCursor_EnumDecl, ND);
69 break;
70 }
Steve Naroff1054e602009-08-31 00:59:03 +000071 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +000072 void VisitVarDecl(VarDecl *ND) {
73 Call(CXCursor_VarDecl, ND);
74 }
Steve Naroff1054e602009-08-31 00:59:03 +000075 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +000076 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
77 : CXCursor_FunctionDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +000078 }
79 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +000080 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +000081 }
82 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +000083 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +000084 }
85 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +000086 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +000087 }
Steve Naroff3645f5a2009-09-02 13:28:54 +000088 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
89 Call(CXCursor_ObjCClassDefn, ND);
90 }
91 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
92 Call(CXCursor_ObjCCategoryDefn, ND);
93 }
Steve Naroff1054e602009-08-31 00:59:03 +000094};
95
Steve Naroff3645f5a2009-09-02 13:28:54 +000096// Declaration visitor.
97class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
98 CXDecl CDecl;
99 CXDeclIterator Callback;
100 CXClientData CData;
101
102 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000103 // Disable the callback when the context is equal to the visiting decl.
104 if (CDecl == ND && !clang_isReference(CK))
105 return;
Steve Naroff3645f5a2009-09-02 13:28:54 +0000106 CXCursor C = { CK, ND };
107 Callback(CDecl, C, CData);
108 }
Steve Naroff1054e602009-08-31 00:59:03 +0000109public:
Steve Naroff3645f5a2009-09-02 13:28:54 +0000110 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
111 CDecl(C), Callback(cback), CData(D) {}
112
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000113 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
114 // Issue callbacks for the containing class.
115 Call(CXCursor_ObjCClassRef, ND);
116 // FIXME: Issue callbacks for protocol refs.
117 VisitDeclContext(dyn_cast<DeclContext>(ND));
118 }
Steve Naroff3645f5a2009-09-02 13:28:54 +0000119 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000120 // Issue callbacks for super class.
Steve Naroff80a766b2009-09-02 18:26:48 +0000121 if (D->getSuperClass())
122 Call(CXCursor_ObjCSuperClassRef, D);
123
Steve Naroffef9618b2009-09-04 15:44:05 +0000124 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
125 E = D->protocol_end(); I != E; ++I)
126 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroff3645f5a2009-09-02 13:28:54 +0000127 VisitDeclContext(dyn_cast<DeclContext>(D));
128 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000129 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
130 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
131 E = PID->protocol_end(); I != E; ++I)
132 Call(CXCursor_ObjCProtocolRef, *I);
133
134 VisitDeclContext(dyn_cast<DeclContext>(PID));
135 }
Steve Naroff3645f5a2009-09-02 13:28:54 +0000136 void VisitTagDecl(TagDecl *D) {
137 VisitDeclContext(dyn_cast<DeclContext>(D));
138 }
139 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
140 VisitDeclContext(dyn_cast<DeclContext>(D));
141 }
142 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
143 VisitDeclContext(dyn_cast<DeclContext>(D));
144 }
Steve Naroff1054e602009-08-31 00:59:03 +0000145 void VisitDeclContext(DeclContext *DC) {
146 for (DeclContext::decl_iterator
147 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
148 Visit(*I);
149 }
150 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000151 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000152 }
153 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000154 Call(CXCursor_FieldDecl, ND);
155 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000156 void VisitVarDecl(VarDecl *ND) {
157 Call(CXCursor_VarDecl, ND);
158 }
159 void VisitParmVarDecl(ParmVarDecl *ND) {
160 Call(CXCursor_ParmDecl, ND);
161 }
Steve Naroff3645f5a2009-09-02 13:28:54 +0000162 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
163 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000164 }
165 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000166 Call(CXCursor_ObjCIvarDecl, ND);
167 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000168 void VisitFunctionDecl(FunctionDecl *ND) {
169 if (ND->isThisDeclarationADefinition()) {
170 VisitDeclContext(dyn_cast<DeclContext>(ND));
171 }
172 }
Steve Naroff3645f5a2009-09-02 13:28:54 +0000173 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
174 if (ND->getBody()) {
175 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
176 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000177 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff3645f5a2009-09-02 13:28:54 +0000178 } else
179 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
180 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000181 }
182};
183
184}
185
Steve Naroffd5e8e862009-08-27 19:51:58 +0000186extern "C" {
Ted Kremenekb60d87c2009-08-26 22:36:44 +0000187
Steve Naroffd5e8e862009-08-27 19:51:58 +0000188CXIndex clang_createIndex()
Steve Naroffa1c72842009-08-28 15:28:48 +0000189{
190 return new Indexer(*new Program(), *new FileManager());
Steve Naroffd5e8e862009-08-27 19:51:58 +0000191}
192
Steve Naroff3aa2d732009-09-17 18:33:27 +0000193void clang_disposeIndex(CXIndex CIdx)
194{
195 assert(CIdx && "Passed null CXIndex");
196 delete static_cast<Indexer *>(CIdx);
197}
198
Steve Naroffa1c72842009-08-28 15:28:48 +0000199// FIXME: need to pass back error info.
200CXTranslationUnit clang_createTranslationUnit(
201 CXIndex CIdx, const char *ast_filename)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000202{
Steve Naroffa1c72842009-08-28 15:28:48 +0000203 assert(CIdx && "Passed null CXIndex");
204 Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
205 std::string astName(ast_filename);
206 std::string ErrMsg;
207
208 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getFileManager(), &ErrMsg);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000209}
210
Steve Naroff3aa2d732009-09-17 18:33:27 +0000211void clang_disposeTranslationUnit(
212 CXTranslationUnit CTUnit)
213{
214 assert(CTUnit && "Passed null CXTranslationUnit");
215 delete static_cast<ASTUnit *>(CTUnit);
216}
217
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000218const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
219{
220 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroffc0683b92009-09-03 18:19:54 +0000221 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
222 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000223}
Daniel Dunbare58bd8b2009-08-28 16:30:07 +0000224
Steve Naroff3645f5a2009-09-02 13:28:54 +0000225void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
226 CXTranslationUnitIterator callback,
227 CXClientData CData)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000228{
Steve Naroffa1c72842009-08-28 15:28:48 +0000229 assert(CTUnit && "Passed null CXTranslationUnit");
230 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
231 ASTContext &Ctx = CXXUnit->getASTContext();
232
Steve Naroff69b10fd2009-09-01 15:55:40 +0000233 TUVisitor DVisit(CTUnit, callback, CData);
Steve Naroffa1c72842009-08-28 15:28:48 +0000234 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroffd5e8e862009-08-27 19:51:58 +0000235}
236
Steve Naroff3645f5a2009-09-02 13:28:54 +0000237void clang_loadDeclaration(CXDecl Dcl,
238 CXDeclIterator callback,
239 CXClientData CData)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000240{
Steve Naroff3645f5a2009-09-02 13:28:54 +0000241 assert(Dcl && "Passed null CXDecl");
242
243 CDeclVisitor DVisit(Dcl, callback, CData);
244 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroffd5e8e862009-08-27 19:51:58 +0000245}
246
Steve Naroff87219592009-08-28 12:07:44 +0000247// Some notes on CXEntity:
248//
249// - Since the 'ordinary' namespace includes functions, data, typedefs,
250// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
251// entity for 2 different types). For example:
252//
253// module1.m: @interface Foo @end Foo *x;
254// module2.m: void Foo(int);
255//
256// - Since the unique name spans translation units, static data/functions
257// within a CXTranslationUnit are *not* currently represented by entities.
258// As a result, there will be no entity for the following:
259//
260// module.m: static void Foo() { }
261//
262
263
Steve Naroffd5e8e862009-08-27 19:51:58 +0000264const char *clang_getDeclarationName(CXEntity)
265{
266 return "";
267}
268const char *clang_getURI(CXEntity)
269{
270 return "";
271}
272
273CXEntity clang_getEntity(const char *URI)
274{
275 return 0;
276}
277
278//
279// CXDecl Operations.
280//
Steve Naroffd5e8e862009-08-27 19:51:58 +0000281CXEntity clang_getEntityFromDecl(CXDecl)
282{
283 return 0;
284}
Steve Naroff1054e602009-08-31 00:59:03 +0000285const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000286{
Steve Naroff1054e602009-08-31 00:59:03 +0000287 assert(AnonDecl && "Passed null CXDecl");
288 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroff3645f5a2009-09-02 13:28:54 +0000289
290 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
291 return OMD->getSelector().getAsString().c_str();
292 }
Steve Naroff1054e602009-08-31 00:59:03 +0000293 if (ND->getIdentifier())
294 return ND->getIdentifier()->getName();
Steve Naroff3645f5a2009-09-02 13:28:54 +0000295 else
Steve Naroff1054e602009-08-31 00:59:03 +0000296 return "";
Steve Naroffd5e8e862009-08-27 19:51:58 +0000297}
Steve Naroff80a766b2009-09-02 18:26:48 +0000298
299const char *clang_getCursorSpelling(CXCursor C)
300{
301 assert(C.decl && "CXCursor has null decl");
302 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
303
304 if (clang_isReference(C.kind)) {
305 switch (C.kind) {
Steve Naroffb92c73a2009-09-02 18:58:52 +0000306 case CXCursor_ObjCSuperClassRef:
307 {
Steve Naroff80a766b2009-09-02 18:26:48 +0000308 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
309 assert(OID && "clang_getCursorLine(): Missing interface decl");
310 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroffb92c73a2009-09-02 18:58:52 +0000311 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000312 case CXCursor_ObjCClassRef:
313 {
314 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
315 assert(OID && "clang_getCursorLine(): Missing category decl");
316 return OID->getClassInterface()->getIdentifier()->getName();
317 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000318 case CXCursor_ObjCProtocolRef:
319 {
320 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
321 assert(OID && "clang_getCursorLine(): Missing protocol decl");
322 return OID->getIdentifier()->getName();
323 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000324 default:
325 return "<not implemented>";
326 }
327 }
328 return clang_getDeclSpelling(C.decl);
329}
330
331const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000332{
Steve Naroff1054e602009-08-31 00:59:03 +0000333 switch (Kind) {
334 case CXCursor_FunctionDecl: return "FunctionDecl";
335 case CXCursor_TypedefDecl: return "TypedefDecl";
336 case CXCursor_EnumDecl: return "EnumDecl";
337 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroff3645f5a2009-09-02 13:28:54 +0000338 case CXCursor_StructDecl: return "StructDecl";
339 case CXCursor_UnionDecl: return "UnionDecl";
340 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff1054e602009-08-31 00:59:03 +0000341 case CXCursor_FieldDecl: return "FieldDecl";
342 case CXCursor_VarDecl: return "VarDecl";
343 case CXCursor_ParmDecl: return "ParmDecl";
344 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
345 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
346 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
347 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
348 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroff3645f5a2009-09-02 13:28:54 +0000349 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
350 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
351 case CXCursor_FunctionDefn: return "FunctionDefn";
352 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
353 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
354 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
355 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Naroff80a766b2009-09-02 18:26:48 +0000356 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroffef9618b2009-09-04 15:44:05 +0000357 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000358 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Naroff54f22fb2009-09-15 20:25:34 +0000359 case CXCursor_InvalidFile: return "InvalidFile";
360 case CXCursor_NoDeclFound: return "NoDeclFound";
361 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff1054e602009-08-31 00:59:03 +0000362 default: return "<not implemented>";
363 }
Steve Naroffd5e8e862009-08-27 19:51:58 +0000364}
Steve Naroff1054e602009-08-31 00:59:03 +0000365
Steve Naroffef9618b2009-09-04 15:44:05 +0000366static enum CXCursorKind TranslateKind(Decl *D) {
367 switch (D->getKind()) {
368 case Decl::Function: return CXCursor_FunctionDecl;
369 case Decl::Typedef: return CXCursor_TypedefDecl;
370 case Decl::Enum: return CXCursor_EnumDecl;
371 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
372 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
373 case Decl::Field: return CXCursor_FieldDecl;
374 case Decl::Var: return CXCursor_VarDecl;
375 case Decl::ParmVar: return CXCursor_ParmDecl;
376 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
377 case Decl::ObjCMethod: {
378 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
379 if (MD->isInstanceMethod())
380 return CXCursor_ObjCInstanceMethodDecl;
381 return CXCursor_ObjCClassMethodDecl;
382 }
383 default: break;
384 }
Steve Naroff54f22fb2009-09-15 20:25:34 +0000385 return CXCursor_NotImplemented;
Steve Naroffef9618b2009-09-04 15:44:05 +0000386}
Steve Naroffd5e8e862009-08-27 19:51:58 +0000387//
388// CXCursor Operations.
389//
Steve Naroffef9618b2009-09-04 15:44:05 +0000390CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroffd5e8e862009-08-27 19:51:58 +0000391 unsigned line, unsigned column)
392{
Steve Naroffef9618b2009-09-04 15:44:05 +0000393 assert(CTUnit && "Passed null CXTranslationUnit");
394 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
395
396 FileManager &FMgr = CXXUnit->getFileManager();
397 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff54f22fb2009-09-15 20:25:34 +0000398 source_name+strlen(source_name));
399 if (!File) {
400 CXCursor C = { CXCursor_InvalidFile, 0 };
401 return C;
402 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000403 SourceLocation SLoc =
404 CXXUnit->getSourceManager().getLocation(File, line, column);
405
406 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
407
408 Decl *Dcl = ALoc.getDecl();
Steve Naroff54f22fb2009-09-15 20:25:34 +0000409 if (Dcl) {
410 CXCursor C = { TranslateKind(Dcl), Dcl };
411 return C;
412 }
413 CXCursor C = { CXCursor_NoDeclFound, 0 };
Steve Naroffef9618b2009-09-04 15:44:05 +0000414 return C;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000415}
416
Steve Naroff54f22fb2009-09-15 20:25:34 +0000417CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
418{
419 assert(AnonDecl && "Passed null CXDecl");
420 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
421
422 CXCursor C = { TranslateKind(ND), ND };
423 return C;
424}
425
426unsigned clang_isInvalid(enum CXCursorKind K)
427{
428 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
429}
430
Steve Naroff1054e602009-08-31 00:59:03 +0000431unsigned clang_isDeclaration(enum CXCursorKind K)
432{
433 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
434}
Steve Naroff772c1a42009-08-31 14:26:51 +0000435
Steve Naroff80a766b2009-09-02 18:26:48 +0000436unsigned clang_isReference(enum CXCursorKind K)
437{
438 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
439}
440
441unsigned clang_isDefinition(enum CXCursorKind K)
442{
443 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
444}
445
Steve Naroffef9618b2009-09-04 15:44:05 +0000446CXCursorKind clang_getCursorKind(CXCursor C)
447{
448 return C.kind;
449}
450
451CXDecl clang_getCursorDecl(CXCursor C)
452{
453 return C.decl;
454}
455
Steve Naroff80a766b2009-09-02 18:26:48 +0000456static SourceLocation getLocationFromCursor(CXCursor C,
457 SourceManager &SourceMgr,
458 NamedDecl *ND) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000459 if (clang_isReference(C.kind)) {
460 switch (C.kind) {
Steve Naroffef9618b2009-09-04 15:44:05 +0000461 case CXCursor_ObjCSuperClassRef:
Steve Naroffb92c73a2009-09-02 18:58:52 +0000462 {
Steve Naroff80a766b2009-09-02 18:26:48 +0000463 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
464 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroffb92c73a2009-09-02 18:58:52 +0000465 return OID->getSuperClassLoc();
466 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000467 case CXCursor_ObjCProtocolRef:
468 {
469 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
470 assert(OID && "clang_getCursorLine(): Missing protocol decl");
471 return OID->getLocation();
472 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000473 default:
Steve Naroffb92c73a2009-09-02 18:58:52 +0000474 return SourceLocation();
Steve Naroff80a766b2009-09-02 18:26:48 +0000475 }
476 } else { // We have a declaration or a definition.
Steve Naroffef9618b2009-09-04 15:44:05 +0000477 SourceLocation SLoc;
478 switch (ND->getKind()) {
479 case Decl::ObjCInterface:
480 {
481 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
482 break;
483 }
484 case Decl::ObjCProtocol:
485 {
486 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
487 break;
488 }
489 default:
490 {
491 SLoc = ND->getLocation();
492 break;
493 }
494 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000495 if (SLoc.isInvalid())
496 return SourceLocation();
Steve Naroffb92c73a2009-09-02 18:58:52 +0000497 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Naroff80a766b2009-09-02 18:26:48 +0000498 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000499}
500
Steve Naroff772c1a42009-08-31 14:26:51 +0000501unsigned clang_getCursorLine(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000502{
Steve Naroff772c1a42009-08-31 14:26:51 +0000503 assert(C.decl && "CXCursor has null decl");
504 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000505 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000506
507 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000508 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000509}
Steve Naroff80a766b2009-09-02 18:26:48 +0000510
Steve Naroff772c1a42009-08-31 14:26:51 +0000511unsigned clang_getCursorColumn(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000512{
Steve Naroff772c1a42009-08-31 14:26:51 +0000513 assert(C.decl && "CXCursor has null decl");
514 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000515 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000516
517 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000518 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000519}
Steve Naroff772c1a42009-08-31 14:26:51 +0000520const char *clang_getCursorSource(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000521{
Steve Naroff772c1a42009-08-31 14:26:51 +0000522 assert(C.decl && "CXCursor has null decl");
523 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000524 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000525
526 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000527 return SourceMgr.getBufferName(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000528}
529
Steve Naroffd5e8e862009-08-27 19:51:58 +0000530} // end extern "C"