blob: 970ba3878a6d8ca69714c81e5f964bbd69d12af1 [file] [log] [blame]
Ted Kremenekd2fa5662009-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 Naroff50398192009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroff9efa7672009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Steve Naroff50398192009-08-28 15:28:48 +000019#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000020#include "clang/AST/StmtVisitor.h"
Steve Naroffc857ea42009-09-02 13:28:54 +000021#include "clang/AST/Decl.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000022#include "clang/Basic/FileManager.h"
Steve Naroff2d4d6292009-08-31 14:26:51 +000023#include "clang/Basic/SourceManager.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000024#include "clang/Frontend/ASTUnit.h"
25#include <cstdio>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000026#include <dlfcn.h>
27#include "llvm/System/Path.h"
28
Steve Naroff50398192009-08-28 15:28:48 +000029using namespace clang;
30using namespace idx;
31
Steve Naroff89922f82009-08-31 00:59:03 +000032namespace {
33
Steve Naroff4ade6d62009-09-23 17:52:52 +000034static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
35{
36 NamedDecl *D = DRE->getDecl();
37 if (isa<VarDecl>(D))
38 return CXCursor_VarRef;
39 else if (isa<FunctionDecl>(D))
40 return CXCursor_FunctionRef;
41 else if (isa<EnumConstantDecl>(D))
42 return CXCursor_EnumConstantRef;
43 else
44 return CXCursor_NotImplemented;
45}
46
47#if 0
48// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000049class CRefVisitor : public StmtVisitor<CRefVisitor> {
50 CXDecl CDecl;
51 CXDeclIterator Callback;
52 CXClientData CData;
53
54 void Call(enum CXCursorKind CK, Stmt *SRef) {
55 CXCursor C = { CK, CDecl, SRef };
56 Callback(CDecl, C, CData);
57 }
58
59public:
60 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
61 CDecl(C), Callback(cback), CData(D) {}
62
63 void VisitStmt(Stmt *S) {
64 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
65 C != CEnd; ++C)
66 Visit(*C);
67 }
68 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000069 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000070 }
71 void VisitMemberExpr(MemberExpr *Node) {
72 Call(CXCursor_MemberRef, Node);
73 }
74 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
75 Call(CXCursor_ObjCSelectorRef, Node);
76 }
77 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
78 Call(CXCursor_ObjCIvarRef, Node);
79 }
80};
Steve Naroff4ade6d62009-09-23 17:52:52 +000081#endif
Steve Narofffb570422009-09-22 19:25:29 +000082
Steve Naroff89922f82009-08-31 00:59:03 +000083// Translation Unit Visitor.
84class TUVisitor : public DeclVisitor<TUVisitor> {
85 CXTranslationUnit TUnit;
86 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000087 CXClientData CData;
88
89 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Narofffb570422009-09-22 19:25:29 +000090 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000091 Callback(TUnit, C, CData);
92 }
Steve Naroff89922f82009-08-31 00:59:03 +000093public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000094 TUVisitor(CXTranslationUnit CTU,
95 CXTranslationUnitIterator cback, CXClientData D) :
96 TUnit(CTU), Callback(cback), CData(D) {}
Steve Naroff89922f82009-08-31 00:59:03 +000097
98 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
99 VisitDeclContext(dyn_cast<DeclContext>(D));
100 }
101 void VisitDeclContext(DeclContext *DC) {
102 for (DeclContext::decl_iterator
103 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
104 Visit(*I);
105 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000106 void VisitTypedefDecl(TypedefDecl *ND) {
107 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000108 }
109 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000110 switch (ND->getTagKind()) {
111 case TagDecl::TK_struct:
112 Call(CXCursor_StructDecl, ND);
113 break;
114 case TagDecl::TK_class:
115 Call(CXCursor_ClassDecl, ND);
116 break;
117 case TagDecl::TK_union:
118 Call(CXCursor_UnionDecl, ND);
119 break;
120 case TagDecl::TK_enum:
121 Call(CXCursor_EnumDecl, ND);
122 break;
123 }
Steve Naroff89922f82009-08-31 00:59:03 +0000124 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000125 void VisitVarDecl(VarDecl *ND) {
126 Call(CXCursor_VarDecl, ND);
127 }
Steve Naroff89922f82009-08-31 00:59:03 +0000128 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000129 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
130 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000131 }
132 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000133 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000134 }
135 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000136 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000137 }
138 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000139 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000140 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000141 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
142 Call(CXCursor_ObjCClassDefn, ND);
143 }
144 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
145 Call(CXCursor_ObjCCategoryDefn, ND);
146 }
Steve Naroff89922f82009-08-31 00:59:03 +0000147};
148
Steve Naroffc857ea42009-09-02 13:28:54 +0000149// Declaration visitor.
150class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
151 CXDecl CDecl;
152 CXDeclIterator Callback;
153 CXClientData CData;
154
155 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000156 // Disable the callback when the context is equal to the visiting decl.
157 if (CDecl == ND && !clang_isReference(CK))
158 return;
Steve Narofffb570422009-09-22 19:25:29 +0000159 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000160 Callback(CDecl, C, CData);
161 }
Steve Naroff89922f82009-08-31 00:59:03 +0000162public:
Steve Naroffc857ea42009-09-02 13:28:54 +0000163 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
164 CDecl(C), Callback(cback), CData(D) {}
165
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000166 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
167 // Issue callbacks for the containing class.
168 Call(CXCursor_ObjCClassRef, ND);
169 // FIXME: Issue callbacks for protocol refs.
170 VisitDeclContext(dyn_cast<DeclContext>(ND));
171 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000172 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000173 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000174 if (D->getSuperClass())
175 Call(CXCursor_ObjCSuperClassRef, D);
176
Steve Naroff9efa7672009-09-04 15:44:05 +0000177 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
178 E = D->protocol_end(); I != E; ++I)
179 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000180 VisitDeclContext(dyn_cast<DeclContext>(D));
181 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000182 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
183 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
184 E = PID->protocol_end(); I != E; ++I)
185 Call(CXCursor_ObjCProtocolRef, *I);
186
187 VisitDeclContext(dyn_cast<DeclContext>(PID));
188 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000189 void VisitTagDecl(TagDecl *D) {
190 VisitDeclContext(dyn_cast<DeclContext>(D));
191 }
192 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
193 VisitDeclContext(dyn_cast<DeclContext>(D));
194 }
195 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
196 VisitDeclContext(dyn_cast<DeclContext>(D));
197 }
Steve Naroff89922f82009-08-31 00:59:03 +0000198 void VisitDeclContext(DeclContext *DC) {
199 for (DeclContext::decl_iterator
200 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
201 Visit(*I);
202 }
203 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000204 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000205 }
206 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000207 Call(CXCursor_FieldDecl, ND);
208 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000209 void VisitVarDecl(VarDecl *ND) {
210 Call(CXCursor_VarDecl, ND);
211 }
212 void VisitParmVarDecl(ParmVarDecl *ND) {
213 Call(CXCursor_ParmDecl, ND);
214 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000215 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
216 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000217 }
218 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000219 Call(CXCursor_ObjCIvarDecl, ND);
220 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000221 void VisitFunctionDecl(FunctionDecl *ND) {
222 if (ND->isThisDeclarationADefinition()) {
223 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000224#if 0
225 // Not currently needed.
226 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000227 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000228 RVisit.Visit(Body);
229#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000230 }
231 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000232 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
233 if (ND->getBody()) {
234 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
235 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000236 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000237 } else
238 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
239 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000240 }
241};
242
243}
244
Steve Naroff600866c2009-08-27 19:51:58 +0000245extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000246
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000247static const char *clangPath;
248
Steve Naroff600866c2009-08-27 19:51:58 +0000249CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000250{
Daniel Dunbara3907592009-09-21 03:03:22 +0000251 // FIXME: Program is leaked.
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000252
253 // Find the location where this library lives (libCIndex.dylib).
254 // We do the lookup here to avoid poking dladdr too many times.
255 // This silly cast below avoids a C++ warning.
256 Dl_info info;
257 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
258 assert(0 && "Call to dladdr() failed");
259
260 llvm::sys::Path CIndexPath(info.dli_fname);
261 std::string CIndexDir = CIndexPath.getDirname();
262
263 // We now have the CIndex directory, locate clang relative to it.
264 std::string ClangPath = CIndexDir + "/../bin/clang";
265
266 clangPath = ClangPath.c_str();
267
Daniel Dunbara3907592009-09-21 03:03:22 +0000268 return new Indexer(*new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000269}
270
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000271void clang_disposeIndex(CXIndex CIdx)
272{
273 assert(CIdx && "Passed null CXIndex");
274 delete static_cast<Indexer *>(CIdx);
275}
276
Steve Naroff50398192009-08-28 15:28:48 +0000277// FIXME: need to pass back error info.
278CXTranslationUnit clang_createTranslationUnit(
279 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000280{
Steve Naroff50398192009-08-28 15:28:48 +0000281 assert(CIdx && "Passed null CXIndex");
282 Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
283 std::string astName(ast_filename);
284 std::string ErrMsg;
285
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000286 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
287 CXXIdx->getFileManager(), &ErrMsg);
Steve Naroff600866c2009-08-27 19:51:58 +0000288}
289
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000290CXTranslationUnit clang_createTranslationUnitFromSourceFile(
291 CXIndex CIdx,
292 const char *source_filename,
293 int num_command_line_args, const char **command_line_args)
294{
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000295 // Build up the arguments for involking clang.
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000296 int argc = 0;
Steve Naroff37b5ac22009-10-15 20:50:09 +0000297 const char * argv[ARG_MAX];
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000298 argv[argc++] = clangPath;
299 argv[argc++] = "-emit-ast";
300 argv[argc++] = source_filename;
301 argv[argc++] = "-o";
Steve Naroff37b5ac22009-10-15 20:50:09 +0000302 // Generate a temporary name for the AST file.
303 char astTmpFile[L_tmpnam];
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000304 argv[argc++] = tmpnam(astTmpFile);
305 for (int i = num_command_line_args; i < num_command_line_args; i++)
306 argv[argc++] = command_line_args[i];
307 argv[argc] = 0;
308
309 // Generate the AST file in a separate process.
310 pid_t child_pid = fork();
311 if (child_pid == 0) { // Child process
312
313 // Execute the command, passing the appropriate arguments.
314 execv(argv[0], (char *const *)argv);
315
316 // If execv returns, it failed.
317 assert(0 && "execv() failed");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000318 }
319 // This is run by the parent.
320 int child_status;
321 pid_t tpid;
322 do { // Wait for the child to terminate.
323 tpid = wait(&child_status);
324 } while (tpid != child_pid);
325
326 // Finally, we create the translation unit from the ast file.
327 return clang_createTranslationUnit(CIdx, astTmpFile);
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000328}
329
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000330void clang_disposeTranslationUnit(
331 CXTranslationUnit CTUnit)
332{
333 assert(CTUnit && "Passed null CXTranslationUnit");
334 delete static_cast<ASTUnit *>(CTUnit);
335}
336
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000337const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
338{
339 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000340 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
341 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000342}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000343
Steve Naroffc857ea42009-09-02 13:28:54 +0000344void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
345 CXTranslationUnitIterator callback,
346 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000347{
Steve Naroff50398192009-08-28 15:28:48 +0000348 assert(CTUnit && "Passed null CXTranslationUnit");
349 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
350 ASTContext &Ctx = CXXUnit->getASTContext();
351
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000352 TUVisitor DVisit(CTUnit, callback, CData);
Steve Naroff50398192009-08-28 15:28:48 +0000353 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000354}
355
Steve Naroffc857ea42009-09-02 13:28:54 +0000356void clang_loadDeclaration(CXDecl Dcl,
357 CXDeclIterator callback,
358 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000359{
Steve Naroffc857ea42009-09-02 13:28:54 +0000360 assert(Dcl && "Passed null CXDecl");
361
362 CDeclVisitor DVisit(Dcl, callback, CData);
363 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000364}
365
Steve Naroff7e8f8182009-08-28 12:07:44 +0000366// Some notes on CXEntity:
367//
368// - Since the 'ordinary' namespace includes functions, data, typedefs,
369// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
370// entity for 2 different types). For example:
371//
372// module1.m: @interface Foo @end Foo *x;
373// module2.m: void Foo(int);
374//
375// - Since the unique name spans translation units, static data/functions
376// within a CXTranslationUnit are *not* currently represented by entities.
377// As a result, there will be no entity for the following:
378//
379// module.m: static void Foo() { }
380//
381
382
Steve Naroff600866c2009-08-27 19:51:58 +0000383const char *clang_getDeclarationName(CXEntity)
384{
385 return "";
386}
387const char *clang_getURI(CXEntity)
388{
389 return "";
390}
391
392CXEntity clang_getEntity(const char *URI)
393{
394 return 0;
395}
396
397//
398// CXDecl Operations.
399//
Steve Naroff600866c2009-08-27 19:51:58 +0000400CXEntity clang_getEntityFromDecl(CXDecl)
401{
402 return 0;
403}
Steve Naroff89922f82009-08-31 00:59:03 +0000404const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000405{
Steve Naroff89922f82009-08-31 00:59:03 +0000406 assert(AnonDecl && "Passed null CXDecl");
407 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000408
409 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
410 return OMD->getSelector().getAsString().c_str();
411 }
Steve Naroff89922f82009-08-31 00:59:03 +0000412 if (ND->getIdentifier())
413 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000414 else
Steve Naroff89922f82009-08-31 00:59:03 +0000415 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000416}
Steve Narofff334b4e2009-09-02 18:26:48 +0000417
Steve Naroff699a07d2009-09-25 21:32:34 +0000418unsigned clang_getDeclLine(CXDecl AnonDecl)
419{
420 assert(AnonDecl && "Passed null CXDecl");
421 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
422 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
423 return SourceMgr.getSpellingLineNumber(ND->getLocation());
424}
425
426unsigned clang_getDeclColumn(CXDecl AnonDecl)
427{
428 assert(AnonDecl && "Passed null CXDecl");
429 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
430 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000431 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000432}
433
Steve Naroffee9405e2009-09-25 21:45:39 +0000434const char *clang_getDeclSource(CXDecl AnonDecl)
435{
436 assert(AnonDecl && "Passed null CXDecl");
437 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
438 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
439 return SourceMgr.getBufferName(ND->getLocation());
440}
441
Steve Narofff334b4e2009-09-02 18:26:48 +0000442const char *clang_getCursorSpelling(CXCursor C)
443{
444 assert(C.decl && "CXCursor has null decl");
445 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
446
447 if (clang_isReference(C.kind)) {
448 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000449 case CXCursor_ObjCSuperClassRef:
450 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000451 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
452 assert(OID && "clang_getCursorLine(): Missing interface decl");
453 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000454 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000455 case CXCursor_ObjCClassRef:
456 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000457 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
458 return OID->getIdentifier()->getName();
459 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000460 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
461 assert(OID && "clang_getCursorLine(): Missing category decl");
462 return OID->getClassInterface()->getIdentifier()->getName();
463 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000464 case CXCursor_ObjCProtocolRef:
465 {
466 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
467 assert(OID && "clang_getCursorLine(): Missing protocol decl");
468 return OID->getIdentifier()->getName();
469 }
Steve Narofffb570422009-09-22 19:25:29 +0000470 case CXCursor_ObjCSelectorRef:
471 {
472 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
473 static_cast<Stmt *>(C.stmt));
474 assert(OME && "clang_getCursorLine(): Missing message expr");
475 return OME->getSelector().getAsString().c_str();
476 }
477 case CXCursor_VarRef:
478 case CXCursor_FunctionRef:
479 case CXCursor_EnumConstantRef:
480 {
481 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
482 static_cast<Stmt *>(C.stmt));
483 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
484 return DRE->getDecl()->getIdentifier()->getName();
485 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000486 default:
487 return "<not implemented>";
488 }
489 }
490 return clang_getDeclSpelling(C.decl);
491}
492
493const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000494{
Steve Naroff89922f82009-08-31 00:59:03 +0000495 switch (Kind) {
496 case CXCursor_FunctionDecl: return "FunctionDecl";
497 case CXCursor_TypedefDecl: return "TypedefDecl";
498 case CXCursor_EnumDecl: return "EnumDecl";
499 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000500 case CXCursor_StructDecl: return "StructDecl";
501 case CXCursor_UnionDecl: return "UnionDecl";
502 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000503 case CXCursor_FieldDecl: return "FieldDecl";
504 case CXCursor_VarDecl: return "VarDecl";
505 case CXCursor_ParmDecl: return "ParmDecl";
506 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
507 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
508 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
509 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
510 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000511 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
512 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
513 case CXCursor_FunctionDefn: return "FunctionDefn";
514 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
515 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
516 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
517 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000518 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000519 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000520 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000521 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
522
523 case CXCursor_VarRef: return "VarRef";
524 case CXCursor_FunctionRef: return "FunctionRef";
525 case CXCursor_EnumConstantRef: return "EnumConstantRef";
526 case CXCursor_MemberRef: return "MemberRef";
527
Steve Naroff77128dd2009-09-15 20:25:34 +0000528 case CXCursor_InvalidFile: return "InvalidFile";
529 case CXCursor_NoDeclFound: return "NoDeclFound";
530 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000531 default: return "<not implemented>";
532 }
Steve Naroff600866c2009-08-27 19:51:58 +0000533}
Steve Naroff89922f82009-08-31 00:59:03 +0000534
Steve Naroff9efa7672009-09-04 15:44:05 +0000535static enum CXCursorKind TranslateKind(Decl *D) {
536 switch (D->getKind()) {
537 case Decl::Function: return CXCursor_FunctionDecl;
538 case Decl::Typedef: return CXCursor_TypedefDecl;
539 case Decl::Enum: return CXCursor_EnumDecl;
540 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
541 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
542 case Decl::Field: return CXCursor_FieldDecl;
543 case Decl::Var: return CXCursor_VarDecl;
544 case Decl::ParmVar: return CXCursor_ParmDecl;
545 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000546 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
547 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000548 case Decl::ObjCMethod: {
549 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
550 if (MD->isInstanceMethod())
551 return CXCursor_ObjCInstanceMethodDecl;
552 return CXCursor_ObjCClassMethodDecl;
553 }
554 default: break;
555 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000556 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000557}
Steve Naroff600866c2009-08-27 19:51:58 +0000558//
559// CXCursor Operations.
560//
Steve Naroff9efa7672009-09-04 15:44:05 +0000561CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000562 unsigned line, unsigned column)
563{
Steve Naroff9efa7672009-09-04 15:44:05 +0000564 assert(CTUnit && "Passed null CXTranslationUnit");
565 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
566
567 FileManager &FMgr = CXXUnit->getFileManager();
568 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000569 source_name+strlen(source_name));
570 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000571 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000572 return C;
573 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000574 SourceLocation SLoc =
575 CXXUnit->getSourceManager().getLocation(File, line, column);
576
577 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
578
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000579 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000580 if (ALoc.isNamedRef())
581 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000582 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000583 if (Dcl) {
584 if (Stm) {
585 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
586 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
587 return C;
588 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
589 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
590 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000591 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000592 // Fall through...treat as a decl, not a ref.
593 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000594 if (ALoc.isNamedRef()) {
595 if (isa<ObjCInterfaceDecl>(Dcl)) {
596 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
597 return C;
598 }
599 if (isa<ObjCProtocolDecl>(Dcl)) {
600 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
601 return C;
602 }
603 }
604 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000605 return C;
606 }
Steve Narofffb570422009-09-22 19:25:29 +0000607 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000608 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000609}
610
Steve Naroff77128dd2009-09-15 20:25:34 +0000611CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
612{
613 assert(AnonDecl && "Passed null CXDecl");
614 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
615
Steve Narofffb570422009-09-22 19:25:29 +0000616 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000617 return C;
618}
619
620unsigned clang_isInvalid(enum CXCursorKind K)
621{
622 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
623}
624
Steve Naroff89922f82009-08-31 00:59:03 +0000625unsigned clang_isDeclaration(enum CXCursorKind K)
626{
627 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
628}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000629
Steve Narofff334b4e2009-09-02 18:26:48 +0000630unsigned clang_isReference(enum CXCursorKind K)
631{
632 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
633}
634
635unsigned clang_isDefinition(enum CXCursorKind K)
636{
637 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
638}
639
Steve Naroff9efa7672009-09-04 15:44:05 +0000640CXCursorKind clang_getCursorKind(CXCursor C)
641{
642 return C.kind;
643}
644
Steve Naroff699a07d2009-09-25 21:32:34 +0000645static Decl *getDeclFromExpr(Stmt *E) {
646 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
647 return RefExpr->getDecl();
648 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
649 return ME->getMemberDecl();
650 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
651 return RE->getDecl();
652
653 if (CallExpr *CE = dyn_cast<CallExpr>(E))
654 return getDeclFromExpr(CE->getCallee());
655 if (CastExpr *CE = dyn_cast<CastExpr>(E))
656 return getDeclFromExpr(CE->getSubExpr());
657 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
658 return OME->getMethodDecl();
659
660 return 0;
661}
662
Steve Naroff9efa7672009-09-04 15:44:05 +0000663CXDecl clang_getCursorDecl(CXCursor C)
664{
Steve Naroff699a07d2009-09-25 21:32:34 +0000665 if (clang_isDeclaration(C.kind))
666 return C.decl;
667
668 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000669 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000670 if (C.kind == CXCursor_ObjCClassRef ||
671 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000672 return static_cast<Stmt *>(C.stmt);
673 else
674 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
675 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000676 return C.decl;
677 }
678 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000679}
680
Steve Naroff85e2db72009-10-01 00:31:07 +0000681
Steve Narofff334b4e2009-09-02 18:26:48 +0000682static SourceLocation getLocationFromCursor(CXCursor C,
683 SourceManager &SourceMgr,
684 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000685 if (clang_isReference(C.kind)) {
686 switch (C.kind) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000687 case CXCursor_ObjCClassRef:
688 {
689 if (isa<ObjCInterfaceDecl>(ND)) {
690 // FIXME: This is a hack (storing the parent decl in the stmt slot).
691 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
692 return parentDecl->getLocation();
693 }
694 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
695 assert(OID && "clang_getCursorLine(): Missing category decl");
696 return OID->getClassInterface()->getLocation();
697 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000698 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000699 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000700 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
701 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000702 return OID->getSuperClassLoc();
703 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000704 case CXCursor_ObjCProtocolRef:
705 {
706 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
707 assert(OID && "clang_getCursorLine(): Missing protocol decl");
708 return OID->getLocation();
709 }
Steve Narofffb570422009-09-22 19:25:29 +0000710 case CXCursor_ObjCSelectorRef:
711 {
712 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
713 static_cast<Stmt *>(C.stmt));
714 assert(OME && "clang_getCursorLine(): Missing message expr");
715 return OME->getLeftLoc(); /* FIXME: should be a range */
716 }
717 case CXCursor_VarRef:
718 case CXCursor_FunctionRef:
719 case CXCursor_EnumConstantRef:
720 {
721 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
722 static_cast<Stmt *>(C.stmt));
723 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
724 return DRE->getLocation();
725 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000726 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000727 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000728 }
729 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000730 SourceLocation SLoc;
731 switch (ND->getKind()) {
732 case Decl::ObjCInterface:
733 {
734 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
735 break;
736 }
737 case Decl::ObjCProtocol:
738 {
739 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
740 break;
741 }
742 default:
743 {
744 SLoc = ND->getLocation();
745 break;
746 }
747 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000748 if (SLoc.isInvalid())
749 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000750 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000751 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000752}
753
Steve Naroff2d4d6292009-08-31 14:26:51 +0000754unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000755{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000756 assert(C.decl && "CXCursor has null decl");
757 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000758 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000759
760 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000761 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000762}
Steve Narofff334b4e2009-09-02 18:26:48 +0000763
Steve Naroff2d4d6292009-08-31 14:26:51 +0000764unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000765{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000766 assert(C.decl && "CXCursor has null decl");
767 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000768 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000769
770 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000771 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000772}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000773const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000774{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000775 assert(C.decl && "CXCursor has null decl");
776 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000777 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000778
779 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000780 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000781}
782
Steve Naroff4ade6d62009-09-23 17:52:52 +0000783void clang_getDefinitionSpellingAndExtent(CXCursor C,
784 const char **startBuf,
785 const char **endBuf,
786 unsigned *startLine,
787 unsigned *startColumn,
788 unsigned *endLine,
789 unsigned *endColumn)
790{
791 assert(C.decl && "CXCursor has null decl");
792 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
793 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
794 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
795
796 SourceManager &SM = FD->getASTContext().getSourceManager();
797 *startBuf = SM.getCharacterData(Body->getLBracLoc());
798 *endBuf = SM.getCharacterData(Body->getRBracLoc());
799 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
800 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
801 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
802 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
803}
804
805
Steve Naroff600866c2009-08-27 19:51:58 +0000806} // end extern "C"