blob: 9ab3f790ec961ab659d2fa0a853182d87902cac8 [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 Naroff66af1ae2009-09-22 19:25:29 +000020#include "clang/AST/StmtVisitor.h"
Steve Naroff3645f5a2009-09-02 13:28:54 +000021#include "clang/AST/Decl.h"
Benjamin Kramer8b83f5d2009-08-29 12:56:35 +000022#include "clang/Basic/FileManager.h"
Steve Naroff772c1a42009-08-31 14:26:51 +000023#include "clang/Basic/SourceManager.h"
Benjamin Kramer8b83f5d2009-08-29 12:56:35 +000024#include "clang/Frontend/ASTUnit.h"
25#include <cstdio>
Steve Naroff7781daa2009-10-15 20:04:39 +000026#include <dlfcn.h>
27#include "llvm/System/Path.h"
28
Steve Naroffa1c72842009-08-28 15:28:48 +000029using namespace clang;
30using namespace idx;
31
Steve Naroff1054e602009-08-31 00:59:03 +000032namespace {
33
Steve Naroff76b8f132009-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 Naroff66af1ae2009-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 Naroff76b8f132009-09-23 17:52:52 +000069 Call(TranslateDeclRefExpr(Node), Node);
Steve Naroff66af1ae2009-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 Naroff76b8f132009-09-23 17:52:52 +000081#endif
Steve Naroff66af1ae2009-09-22 19:25:29 +000082
Steve Naroff1054e602009-08-31 00:59:03 +000083// Translation Unit Visitor.
84class TUVisitor : public DeclVisitor<TUVisitor> {
85 CXTranslationUnit TUnit;
86 CXTranslationUnitIterator Callback;
Steve Naroff69b10fd2009-09-01 15:55:40 +000087 CXClientData CData;
88
89 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroff66af1ae2009-09-22 19:25:29 +000090 CXCursor C = { CK, ND, 0 };
Steve Naroff69b10fd2009-09-01 15:55:40 +000091 Callback(TUnit, C, CData);
92 }
Steve Naroff1054e602009-08-31 00:59:03 +000093public:
Steve Naroff69b10fd2009-09-01 15:55:40 +000094 TUVisitor(CXTranslationUnit CTU,
95 CXTranslationUnitIterator cback, CXClientData D) :
96 TUnit(CTU), Callback(cback), CData(D) {}
Steve Naroff1054e602009-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 Naroff69b10fd2009-09-01 15:55:40 +0000106 void VisitTypedefDecl(TypedefDecl *ND) {
107 Call(CXCursor_TypedefDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000108 }
109 void VisitTagDecl(TagDecl *ND) {
Steve Naroff3645f5a2009-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 Naroff1054e602009-08-31 00:59:03 +0000124 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000125 void VisitVarDecl(VarDecl *ND) {
126 Call(CXCursor_VarDecl, ND);
127 }
Steve Naroff1054e602009-08-31 00:59:03 +0000128 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000129 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
130 : CXCursor_FunctionDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000131 }
132 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +0000133 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000134 }
135 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +0000136 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000137 }
138 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +0000139 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000140 }
Steve Naroff3645f5a2009-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 Naroff1054e602009-08-31 00:59:03 +0000147};
148
Steve Naroff3645f5a2009-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 Naroff38c1a7b2009-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 Naroff66af1ae2009-09-22 19:25:29 +0000159 CXCursor C = { CK, ND, 0 };
Steve Naroff3645f5a2009-09-02 13:28:54 +0000160 Callback(CDecl, C, CData);
161 }
Steve Naroff1054e602009-08-31 00:59:03 +0000162public:
Steve Naroff3645f5a2009-09-02 13:28:54 +0000163 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
164 CDecl(C), Callback(cback), CData(D) {}
165
Steve Naroff38c1a7b2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000172 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000173 // Issue callbacks for super class.
Steve Naroff80a766b2009-09-02 18:26:48 +0000174 if (D->getSuperClass())
175 Call(CXCursor_ObjCSuperClassRef, D);
176
Steve Naroffef9618b2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000180 VisitDeclContext(dyn_cast<DeclContext>(D));
181 }
Steve Naroffef9618b2009-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 Naroff3645f5a2009-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 Naroff1054e602009-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 Naroff3645f5a2009-09-02 13:28:54 +0000204 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000205 }
206 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000207 Call(CXCursor_FieldDecl, ND);
208 }
Steve Naroff38c1a7b2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000215 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
216 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000217 }
218 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000219 Call(CXCursor_ObjCIvarDecl, ND);
220 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000221 void VisitFunctionDecl(FunctionDecl *ND) {
222 if (ND->isThisDeclarationADefinition()) {
223 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff76b8f132009-09-23 17:52:52 +0000224#if 0
225 // Not currently needed.
226 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Naroff66af1ae2009-09-22 19:25:29 +0000227 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff76b8f132009-09-23 17:52:52 +0000228 RVisit.Visit(Body);
229#endif
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000230 }
231 }
Steve Naroff3645f5a2009-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 Naroff38c1a7b2009-09-03 15:49:00 +0000236 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff3645f5a2009-09-02 13:28:54 +0000237 } else
238 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
239 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000240 }
241};
242
243}
244
Steve Naroffd5e8e862009-08-27 19:51:58 +0000245extern "C" {
Ted Kremenekb60d87c2009-08-26 22:36:44 +0000246
Steve Naroff7781daa2009-10-15 20:04:39 +0000247static const char *clangPath;
248
Steve Naroffd5e8e862009-08-27 19:51:58 +0000249CXIndex clang_createIndex()
Steve Naroffa1c72842009-08-28 15:28:48 +0000250{
Daniel Dunbar948062a2009-09-21 03:03:22 +0000251 // FIXME: Program is leaked.
Steve Naroff7781daa2009-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 Dunbar948062a2009-09-21 03:03:22 +0000268 return new Indexer(*new Program());
Steve Naroffd5e8e862009-08-27 19:51:58 +0000269}
270
Steve Naroff3aa2d732009-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 Naroffa1c72842009-08-28 15:28:48 +0000277// FIXME: need to pass back error info.
278CXTranslationUnit clang_createTranslationUnit(
279 CXIndex CIdx, const char *ast_filename)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000280{
Steve Naroffa1c72842009-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 Dunbar7cd285f2009-09-21 03:03:39 +0000286 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
287 CXXIdx->getFileManager(), &ErrMsg);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000288}
289
Steve Naroff7781daa2009-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{
295 // Generate a temporary name for the AST file.
296 char astTmpFile[L_tmpnam];
297
298 // Build up the arguments for involking clang.
299 const char * argv[ARG_MAX];
300 int argc = 0;
301 argv[argc++] = clangPath;
302 argv[argc++] = "-emit-ast";
303 argv[argc++] = source_filename;
304 argv[argc++] = "-o";
305 argv[argc++] = tmpnam(astTmpFile);
306 for (int i = num_command_line_args; i < num_command_line_args; i++)
307 argv[argc++] = command_line_args[i];
308 argv[argc] = 0;
309
310 // Generate the AST file in a separate process.
311 pid_t child_pid = fork();
312 if (child_pid == 0) { // Child process
313
314 // Execute the command, passing the appropriate arguments.
315 execv(argv[0], (char *const *)argv);
316
317 // If execv returns, it failed.
318 assert(0 && "execv() failed");
319 } else { // 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);
328 }
329}
330
Steve Naroff3aa2d732009-09-17 18:33:27 +0000331void clang_disposeTranslationUnit(
332 CXTranslationUnit CTUnit)
333{
334 assert(CTUnit && "Passed null CXTranslationUnit");
335 delete static_cast<ASTUnit *>(CTUnit);
336}
337
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000338const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
339{
340 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroffc0683b92009-09-03 18:19:54 +0000341 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
342 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000343}
Daniel Dunbare58bd8b2009-08-28 16:30:07 +0000344
Steve Naroff3645f5a2009-09-02 13:28:54 +0000345void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
346 CXTranslationUnitIterator callback,
347 CXClientData CData)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000348{
Steve Naroffa1c72842009-08-28 15:28:48 +0000349 assert(CTUnit && "Passed null CXTranslationUnit");
350 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
351 ASTContext &Ctx = CXXUnit->getASTContext();
352
Steve Naroff69b10fd2009-09-01 15:55:40 +0000353 TUVisitor DVisit(CTUnit, callback, CData);
Steve Naroffa1c72842009-08-28 15:28:48 +0000354 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroffd5e8e862009-08-27 19:51:58 +0000355}
356
Steve Naroff3645f5a2009-09-02 13:28:54 +0000357void clang_loadDeclaration(CXDecl Dcl,
358 CXDeclIterator callback,
359 CXClientData CData)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000360{
Steve Naroff3645f5a2009-09-02 13:28:54 +0000361 assert(Dcl && "Passed null CXDecl");
362
363 CDeclVisitor DVisit(Dcl, callback, CData);
364 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroffd5e8e862009-08-27 19:51:58 +0000365}
366
Steve Naroff87219592009-08-28 12:07:44 +0000367// Some notes on CXEntity:
368//
369// - Since the 'ordinary' namespace includes functions, data, typedefs,
370// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
371// entity for 2 different types). For example:
372//
373// module1.m: @interface Foo @end Foo *x;
374// module2.m: void Foo(int);
375//
376// - Since the unique name spans translation units, static data/functions
377// within a CXTranslationUnit are *not* currently represented by entities.
378// As a result, there will be no entity for the following:
379//
380// module.m: static void Foo() { }
381//
382
383
Steve Naroffd5e8e862009-08-27 19:51:58 +0000384const char *clang_getDeclarationName(CXEntity)
385{
386 return "";
387}
388const char *clang_getURI(CXEntity)
389{
390 return "";
391}
392
393CXEntity clang_getEntity(const char *URI)
394{
395 return 0;
396}
397
398//
399// CXDecl Operations.
400//
Steve Naroffd5e8e862009-08-27 19:51:58 +0000401CXEntity clang_getEntityFromDecl(CXDecl)
402{
403 return 0;
404}
Steve Naroff1054e602009-08-31 00:59:03 +0000405const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000406{
Steve Naroff1054e602009-08-31 00:59:03 +0000407 assert(AnonDecl && "Passed null CXDecl");
408 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroff3645f5a2009-09-02 13:28:54 +0000409
410 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
411 return OMD->getSelector().getAsString().c_str();
412 }
Steve Naroff1054e602009-08-31 00:59:03 +0000413 if (ND->getIdentifier())
414 return ND->getIdentifier()->getName();
Steve Naroff3645f5a2009-09-02 13:28:54 +0000415 else
Steve Naroff1054e602009-08-31 00:59:03 +0000416 return "";
Steve Naroffd5e8e862009-08-27 19:51:58 +0000417}
Steve Naroff80a766b2009-09-02 18:26:48 +0000418
Steve Naroff63f475a2009-09-25 21:32:34 +0000419unsigned clang_getDeclLine(CXDecl AnonDecl)
420{
421 assert(AnonDecl && "Passed null CXDecl");
422 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
423 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
424 return SourceMgr.getSpellingLineNumber(ND->getLocation());
425}
426
427unsigned clang_getDeclColumn(CXDecl AnonDecl)
428{
429 assert(AnonDecl && "Passed null CXDecl");
430 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
431 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff43b118f2009-09-25 22:15:54 +0000432 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff63f475a2009-09-25 21:32:34 +0000433}
434
Steve Naroff26760892009-09-25 21:45:39 +0000435const char *clang_getDeclSource(CXDecl AnonDecl)
436{
437 assert(AnonDecl && "Passed null CXDecl");
438 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
439 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
440 return SourceMgr.getBufferName(ND->getLocation());
441}
442
Steve Naroff80a766b2009-09-02 18:26:48 +0000443const char *clang_getCursorSpelling(CXCursor C)
444{
445 assert(C.decl && "CXCursor has null decl");
446 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
447
448 if (clang_isReference(C.kind)) {
449 switch (C.kind) {
Steve Naroffb92c73a2009-09-02 18:58:52 +0000450 case CXCursor_ObjCSuperClassRef:
451 {
Steve Naroff80a766b2009-09-02 18:26:48 +0000452 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
453 assert(OID && "clang_getCursorLine(): Missing interface decl");
454 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroffb92c73a2009-09-02 18:58:52 +0000455 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000456 case CXCursor_ObjCClassRef:
457 {
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000458 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
459 return OID->getIdentifier()->getName();
460 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000461 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
462 assert(OID && "clang_getCursorLine(): Missing category decl");
463 return OID->getClassInterface()->getIdentifier()->getName();
464 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000465 case CXCursor_ObjCProtocolRef:
466 {
467 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
468 assert(OID && "clang_getCursorLine(): Missing protocol decl");
469 return OID->getIdentifier()->getName();
470 }
Steve Naroff66af1ae2009-09-22 19:25:29 +0000471 case CXCursor_ObjCSelectorRef:
472 {
473 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
474 static_cast<Stmt *>(C.stmt));
475 assert(OME && "clang_getCursorLine(): Missing message expr");
476 return OME->getSelector().getAsString().c_str();
477 }
478 case CXCursor_VarRef:
479 case CXCursor_FunctionRef:
480 case CXCursor_EnumConstantRef:
481 {
482 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
483 static_cast<Stmt *>(C.stmt));
484 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
485 return DRE->getDecl()->getIdentifier()->getName();
486 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000487 default:
488 return "<not implemented>";
489 }
490 }
491 return clang_getDeclSpelling(C.decl);
492}
493
494const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000495{
Steve Naroff1054e602009-08-31 00:59:03 +0000496 switch (Kind) {
497 case CXCursor_FunctionDecl: return "FunctionDecl";
498 case CXCursor_TypedefDecl: return "TypedefDecl";
499 case CXCursor_EnumDecl: return "EnumDecl";
500 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroff3645f5a2009-09-02 13:28:54 +0000501 case CXCursor_StructDecl: return "StructDecl";
502 case CXCursor_UnionDecl: return "UnionDecl";
503 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff1054e602009-08-31 00:59:03 +0000504 case CXCursor_FieldDecl: return "FieldDecl";
505 case CXCursor_VarDecl: return "VarDecl";
506 case CXCursor_ParmDecl: return "ParmDecl";
507 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
508 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
509 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
510 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
511 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroff3645f5a2009-09-02 13:28:54 +0000512 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
513 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
514 case CXCursor_FunctionDefn: return "FunctionDefn";
515 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
516 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
517 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
518 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Naroff80a766b2009-09-02 18:26:48 +0000519 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroffef9618b2009-09-04 15:44:05 +0000520 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000521 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Naroff66af1ae2009-09-22 19:25:29 +0000522 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
523
524 case CXCursor_VarRef: return "VarRef";
525 case CXCursor_FunctionRef: return "FunctionRef";
526 case CXCursor_EnumConstantRef: return "EnumConstantRef";
527 case CXCursor_MemberRef: return "MemberRef";
528
Steve Naroff54f22fb2009-09-15 20:25:34 +0000529 case CXCursor_InvalidFile: return "InvalidFile";
530 case CXCursor_NoDeclFound: return "NoDeclFound";
531 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff1054e602009-08-31 00:59:03 +0000532 default: return "<not implemented>";
533 }
Steve Naroffd5e8e862009-08-27 19:51:58 +0000534}
Steve Naroff1054e602009-08-31 00:59:03 +0000535
Steve Naroffef9618b2009-09-04 15:44:05 +0000536static enum CXCursorKind TranslateKind(Decl *D) {
537 switch (D->getKind()) {
538 case Decl::Function: return CXCursor_FunctionDecl;
539 case Decl::Typedef: return CXCursor_TypedefDecl;
540 case Decl::Enum: return CXCursor_EnumDecl;
541 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
542 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
543 case Decl::Field: return CXCursor_FieldDecl;
544 case Decl::Var: return CXCursor_VarDecl;
545 case Decl::ParmVar: return CXCursor_ParmDecl;
546 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Naroff66af1ae2009-09-22 19:25:29 +0000547 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
548 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroffef9618b2009-09-04 15:44:05 +0000549 case Decl::ObjCMethod: {
550 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
551 if (MD->isInstanceMethod())
552 return CXCursor_ObjCInstanceMethodDecl;
553 return CXCursor_ObjCClassMethodDecl;
554 }
555 default: break;
556 }
Steve Naroff54f22fb2009-09-15 20:25:34 +0000557 return CXCursor_NotImplemented;
Steve Naroffef9618b2009-09-04 15:44:05 +0000558}
Steve Naroffd5e8e862009-08-27 19:51:58 +0000559//
560// CXCursor Operations.
561//
Steve Naroffef9618b2009-09-04 15:44:05 +0000562CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroffd5e8e862009-08-27 19:51:58 +0000563 unsigned line, unsigned column)
564{
Steve Naroffef9618b2009-09-04 15:44:05 +0000565 assert(CTUnit && "Passed null CXTranslationUnit");
566 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
567
568 FileManager &FMgr = CXXUnit->getFileManager();
569 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff54f22fb2009-09-15 20:25:34 +0000570 source_name+strlen(source_name));
571 if (!File) {
Steve Naroff66af1ae2009-09-22 19:25:29 +0000572 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000573 return C;
574 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000575 SourceLocation SLoc =
576 CXXUnit->getSourceManager().getLocation(File, line, column);
577
578 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
579
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000580 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis419e38b2009-09-29 19:45:58 +0000581 if (ALoc.isNamedRef())
582 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000583 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff76b8f132009-09-23 17:52:52 +0000584 if (Dcl) {
585 if (Stm) {
586 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
587 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
588 return C;
589 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
590 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
591 return C;
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000592 }
Steve Naroff76b8f132009-09-23 17:52:52 +0000593 // Fall through...treat as a decl, not a ref.
594 }
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000595 if (ALoc.isNamedRef()) {
596 if (isa<ObjCInterfaceDecl>(Dcl)) {
597 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
598 return C;
599 }
600 if (isa<ObjCProtocolDecl>(Dcl)) {
601 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
602 return C;
603 }
604 }
605 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000606 return C;
607 }
Steve Naroff66af1ae2009-09-22 19:25:29 +0000608 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroffef9618b2009-09-04 15:44:05 +0000609 return C;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000610}
611
Steve Naroff54f22fb2009-09-15 20:25:34 +0000612CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
613{
614 assert(AnonDecl && "Passed null CXDecl");
615 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
616
Steve Naroff66af1ae2009-09-22 19:25:29 +0000617 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000618 return C;
619}
620
621unsigned clang_isInvalid(enum CXCursorKind K)
622{
623 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
624}
625
Steve Naroff1054e602009-08-31 00:59:03 +0000626unsigned clang_isDeclaration(enum CXCursorKind K)
627{
628 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
629}
Steve Naroff772c1a42009-08-31 14:26:51 +0000630
Steve Naroff80a766b2009-09-02 18:26:48 +0000631unsigned clang_isReference(enum CXCursorKind K)
632{
633 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
634}
635
636unsigned clang_isDefinition(enum CXCursorKind K)
637{
638 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
639}
640
Steve Naroffef9618b2009-09-04 15:44:05 +0000641CXCursorKind clang_getCursorKind(CXCursor C)
642{
643 return C.kind;
644}
645
Steve Naroff63f475a2009-09-25 21:32:34 +0000646static Decl *getDeclFromExpr(Stmt *E) {
647 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
648 return RefExpr->getDecl();
649 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
650 return ME->getMemberDecl();
651 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
652 return RE->getDecl();
653
654 if (CallExpr *CE = dyn_cast<CallExpr>(E))
655 return getDeclFromExpr(CE->getCallee());
656 if (CastExpr *CE = dyn_cast<CastExpr>(E))
657 return getDeclFromExpr(CE->getSubExpr());
658 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
659 return OME->getMethodDecl();
660
661 return 0;
662}
663
Steve Naroffef9618b2009-09-04 15:44:05 +0000664CXDecl clang_getCursorDecl(CXCursor C)
665{
Steve Naroff63f475a2009-09-25 21:32:34 +0000666 if (clang_isDeclaration(C.kind))
667 return C.decl;
668
669 if (clang_isReference(C.kind)) {
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000670 if (C.stmt) {
Steve Naroffd7eb7172009-10-05 17:58:19 +0000671 if (C.kind == CXCursor_ObjCClassRef ||
672 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000673 return static_cast<Stmt *>(C.stmt);
674 else
675 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
676 } else
Steve Naroff63f475a2009-09-25 21:32:34 +0000677 return C.decl;
678 }
679 return 0;
Steve Naroffef9618b2009-09-04 15:44:05 +0000680}
681
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000682
Steve Naroff80a766b2009-09-02 18:26:48 +0000683static SourceLocation getLocationFromCursor(CXCursor C,
684 SourceManager &SourceMgr,
685 NamedDecl *ND) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000686 if (clang_isReference(C.kind)) {
687 switch (C.kind) {
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000688 case CXCursor_ObjCClassRef:
689 {
690 if (isa<ObjCInterfaceDecl>(ND)) {
691 // FIXME: This is a hack (storing the parent decl in the stmt slot).
692 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
693 return parentDecl->getLocation();
694 }
695 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
696 assert(OID && "clang_getCursorLine(): Missing category decl");
697 return OID->getClassInterface()->getLocation();
698 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000699 case CXCursor_ObjCSuperClassRef:
Steve Naroffb92c73a2009-09-02 18:58:52 +0000700 {
Steve Naroff80a766b2009-09-02 18:26:48 +0000701 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
702 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroffb92c73a2009-09-02 18:58:52 +0000703 return OID->getSuperClassLoc();
704 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000705 case CXCursor_ObjCProtocolRef:
706 {
707 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
708 assert(OID && "clang_getCursorLine(): Missing protocol decl");
709 return OID->getLocation();
710 }
Steve Naroff66af1ae2009-09-22 19:25:29 +0000711 case CXCursor_ObjCSelectorRef:
712 {
713 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
714 static_cast<Stmt *>(C.stmt));
715 assert(OME && "clang_getCursorLine(): Missing message expr");
716 return OME->getLeftLoc(); /* FIXME: should be a range */
717 }
718 case CXCursor_VarRef:
719 case CXCursor_FunctionRef:
720 case CXCursor_EnumConstantRef:
721 {
722 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
723 static_cast<Stmt *>(C.stmt));
724 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
725 return DRE->getLocation();
726 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000727 default:
Steve Naroffb92c73a2009-09-02 18:58:52 +0000728 return SourceLocation();
Steve Naroff80a766b2009-09-02 18:26:48 +0000729 }
730 } else { // We have a declaration or a definition.
Steve Naroffef9618b2009-09-04 15:44:05 +0000731 SourceLocation SLoc;
732 switch (ND->getKind()) {
733 case Decl::ObjCInterface:
734 {
735 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
736 break;
737 }
738 case Decl::ObjCProtocol:
739 {
740 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
741 break;
742 }
743 default:
744 {
745 SLoc = ND->getLocation();
746 break;
747 }
748 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000749 if (SLoc.isInvalid())
750 return SourceLocation();
Steve Naroffb92c73a2009-09-02 18:58:52 +0000751 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Naroff80a766b2009-09-02 18:26:48 +0000752 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000753}
754
Steve Naroff772c1a42009-08-31 14:26:51 +0000755unsigned clang_getCursorLine(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000756{
Steve Naroff772c1a42009-08-31 14:26:51 +0000757 assert(C.decl && "CXCursor has null decl");
758 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000759 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000760
761 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000762 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000763}
Steve Naroff80a766b2009-09-02 18:26:48 +0000764
Steve Naroff772c1a42009-08-31 14:26:51 +0000765unsigned clang_getCursorColumn(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000766{
Steve Naroff772c1a42009-08-31 14:26:51 +0000767 assert(C.decl && "CXCursor has null decl");
768 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000769 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000770
771 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000772 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000773}
Steve Naroff772c1a42009-08-31 14:26:51 +0000774const char *clang_getCursorSource(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000775{
Steve Naroff772c1a42009-08-31 14:26:51 +0000776 assert(C.decl && "CXCursor has null decl");
777 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000778 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000779
780 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000781 return SourceMgr.getBufferName(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000782}
783
Steve Naroff76b8f132009-09-23 17:52:52 +0000784void clang_getDefinitionSpellingAndExtent(CXCursor C,
785 const char **startBuf,
786 const char **endBuf,
787 unsigned *startLine,
788 unsigned *startColumn,
789 unsigned *endLine,
790 unsigned *endColumn)
791{
792 assert(C.decl && "CXCursor has null decl");
793 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
794 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
795 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
796
797 SourceManager &SM = FD->getASTContext().getSourceManager();
798 *startBuf = SM.getCharacterData(Body->getLBracLoc());
799 *endBuf = SM.getCharacterData(Body->getRBracLoc());
800 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
801 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
802 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
803 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
804}
805
806
Steve Naroffd5e8e862009-08-27 19:51:58 +0000807} // end extern "C"