blob: 5e999c10037eae65ac510f782c740e91774910ae [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>
Ted Kremenek8c4195e2009-10-15 22:10:56 +000027#include <sys/wait.h>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000028#include "llvm/System/Path.h"
29
Steve Naroff50398192009-08-28 15:28:48 +000030using namespace clang;
31using namespace idx;
32
Steve Naroff89922f82009-08-31 00:59:03 +000033namespace {
34
Steve Naroff4ade6d62009-09-23 17:52:52 +000035static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
36{
37 NamedDecl *D = DRE->getDecl();
38 if (isa<VarDecl>(D))
39 return CXCursor_VarRef;
40 else if (isa<FunctionDecl>(D))
41 return CXCursor_FunctionRef;
42 else if (isa<EnumConstantDecl>(D))
43 return CXCursor_EnumConstantRef;
44 else
45 return CXCursor_NotImplemented;
46}
47
48#if 0
49// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000050class CRefVisitor : public StmtVisitor<CRefVisitor> {
51 CXDecl CDecl;
52 CXDeclIterator Callback;
53 CXClientData CData;
54
55 void Call(enum CXCursorKind CK, Stmt *SRef) {
56 CXCursor C = { CK, CDecl, SRef };
57 Callback(CDecl, C, CData);
58 }
59
60public:
61 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
62 CDecl(C), Callback(cback), CData(D) {}
63
64 void VisitStmt(Stmt *S) {
65 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
66 C != CEnd; ++C)
67 Visit(*C);
68 }
69 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000070 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000071 }
72 void VisitMemberExpr(MemberExpr *Node) {
73 Call(CXCursor_MemberRef, Node);
74 }
75 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
76 Call(CXCursor_ObjCSelectorRef, Node);
77 }
78 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
79 Call(CXCursor_ObjCIvarRef, Node);
80 }
81};
Steve Naroff4ade6d62009-09-23 17:52:52 +000082#endif
Steve Narofffb570422009-09-22 19:25:29 +000083
Steve Naroff89922f82009-08-31 00:59:03 +000084// Translation Unit Visitor.
85class TUVisitor : public DeclVisitor<TUVisitor> {
86 CXTranslationUnit TUnit;
87 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000088 CXClientData CData;
89
90 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Narofffb570422009-09-22 19:25:29 +000091 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000092 Callback(TUnit, C, CData);
93 }
Steve Naroff89922f82009-08-31 00:59:03 +000094public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000095 TUVisitor(CXTranslationUnit CTU,
96 CXTranslationUnitIterator cback, CXClientData D) :
97 TUnit(CTU), Callback(cback), CData(D) {}
Steve Naroff89922f82009-08-31 00:59:03 +000098
99 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
100 VisitDeclContext(dyn_cast<DeclContext>(D));
101 }
102 void VisitDeclContext(DeclContext *DC) {
103 for (DeclContext::decl_iterator
104 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
105 Visit(*I);
106 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000107 void VisitTypedefDecl(TypedefDecl *ND) {
108 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000109 }
110 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000111 switch (ND->getTagKind()) {
112 case TagDecl::TK_struct:
113 Call(CXCursor_StructDecl, ND);
114 break;
115 case TagDecl::TK_class:
116 Call(CXCursor_ClassDecl, ND);
117 break;
118 case TagDecl::TK_union:
119 Call(CXCursor_UnionDecl, ND);
120 break;
121 case TagDecl::TK_enum:
122 Call(CXCursor_EnumDecl, ND);
123 break;
124 }
Steve Naroff89922f82009-08-31 00:59:03 +0000125 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000126 void VisitVarDecl(VarDecl *ND) {
127 Call(CXCursor_VarDecl, ND);
128 }
Steve Naroff89922f82009-08-31 00:59:03 +0000129 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000130 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
131 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000132 }
133 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000134 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000135 }
136 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000137 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000138 }
139 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000140 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000141 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000142 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
143 Call(CXCursor_ObjCClassDefn, ND);
144 }
145 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
146 Call(CXCursor_ObjCCategoryDefn, ND);
147 }
Steve Naroff89922f82009-08-31 00:59:03 +0000148};
149
Steve Naroffc857ea42009-09-02 13:28:54 +0000150// Declaration visitor.
151class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
152 CXDecl CDecl;
153 CXDeclIterator Callback;
154 CXClientData CData;
155
156 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000157 // Disable the callback when the context is equal to the visiting decl.
158 if (CDecl == ND && !clang_isReference(CK))
159 return;
Steve Narofffb570422009-09-22 19:25:29 +0000160 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000161 Callback(CDecl, C, CData);
162 }
Steve Naroff89922f82009-08-31 00:59:03 +0000163public:
Steve Naroffc857ea42009-09-02 13:28:54 +0000164 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
165 CDecl(C), Callback(cback), CData(D) {}
166
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000167 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
168 // Issue callbacks for the containing class.
169 Call(CXCursor_ObjCClassRef, ND);
170 // FIXME: Issue callbacks for protocol refs.
171 VisitDeclContext(dyn_cast<DeclContext>(ND));
172 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000173 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000174 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000175 if (D->getSuperClass())
176 Call(CXCursor_ObjCSuperClassRef, D);
177
Steve Naroff9efa7672009-09-04 15:44:05 +0000178 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
179 E = D->protocol_end(); I != E; ++I)
180 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000181 VisitDeclContext(dyn_cast<DeclContext>(D));
182 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000183 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
184 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
185 E = PID->protocol_end(); I != E; ++I)
186 Call(CXCursor_ObjCProtocolRef, *I);
187
188 VisitDeclContext(dyn_cast<DeclContext>(PID));
189 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000190 void VisitTagDecl(TagDecl *D) {
191 VisitDeclContext(dyn_cast<DeclContext>(D));
192 }
193 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
194 VisitDeclContext(dyn_cast<DeclContext>(D));
195 }
196 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
197 VisitDeclContext(dyn_cast<DeclContext>(D));
198 }
Steve Naroff89922f82009-08-31 00:59:03 +0000199 void VisitDeclContext(DeclContext *DC) {
200 for (DeclContext::decl_iterator
201 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
202 Visit(*I);
203 }
204 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000205 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000206 }
207 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000208 Call(CXCursor_FieldDecl, ND);
209 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000210 void VisitVarDecl(VarDecl *ND) {
211 Call(CXCursor_VarDecl, ND);
212 }
213 void VisitParmVarDecl(ParmVarDecl *ND) {
214 Call(CXCursor_ParmDecl, ND);
215 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000216 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
217 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000218 }
219 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000220 Call(CXCursor_ObjCIvarDecl, ND);
221 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000222 void VisitFunctionDecl(FunctionDecl *ND) {
223 if (ND->isThisDeclarationADefinition()) {
224 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000225#if 0
226 // Not currently needed.
227 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000228 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000229 RVisit.Visit(Body);
230#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000231 }
232 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000233 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
234 if (ND->getBody()) {
235 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
236 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000237 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000238 } else
239 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
240 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000241 }
242};
243
244}
245
Steve Naroff600866c2009-08-27 19:51:58 +0000246extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000247
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000248static const char *clangPath;
249
Steve Naroff600866c2009-08-27 19:51:58 +0000250CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000251{
Daniel Dunbara3907592009-09-21 03:03:22 +0000252 // FIXME: Program is leaked.
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000253
254 // Find the location where this library lives (libCIndex.dylib).
255 // We do the lookup here to avoid poking dladdr too many times.
256 // This silly cast below avoids a C++ warning.
257 Dl_info info;
258 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
259 assert(0 && "Call to dladdr() failed");
260
261 llvm::sys::Path CIndexPath(info.dli_fname);
262 std::string CIndexDir = CIndexPath.getDirname();
263
264 // We now have the CIndex directory, locate clang relative to it.
265 std::string ClangPath = CIndexDir + "/../bin/clang";
266
267 clangPath = ClangPath.c_str();
268
Daniel Dunbara3907592009-09-21 03:03:22 +0000269 return new Indexer(*new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000270}
271
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000272void clang_disposeIndex(CXIndex CIdx)
273{
274 assert(CIdx && "Passed null CXIndex");
275 delete static_cast<Indexer *>(CIdx);
276}
277
Steve Naroff50398192009-08-28 15:28:48 +0000278// FIXME: need to pass back error info.
279CXTranslationUnit clang_createTranslationUnit(
280 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000281{
Steve Naroff50398192009-08-28 15:28:48 +0000282 assert(CIdx && "Passed null CXIndex");
283 Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
284 std::string astName(ast_filename);
285 std::string ErrMsg;
286
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000287 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
288 CXXIdx->getFileManager(), &ErrMsg);
Steve Naroff600866c2009-08-27 19:51:58 +0000289}
290
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000291CXTranslationUnit clang_createTranslationUnitFromSourceFile(
292 CXIndex CIdx,
293 const char *source_filename,
294 int num_command_line_args, const char **command_line_args)
295{
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000296 // Build up the arguments for involking clang.
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000297 int argc = 0;
Steve Naroff37b5ac22009-10-15 20:50:09 +0000298 const char * argv[ARG_MAX];
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000299 argv[argc++] = clangPath;
300 argv[argc++] = "-emit-ast";
301 argv[argc++] = source_filename;
302 argv[argc++] = "-o";
Steve Naroff37b5ac22009-10-15 20:50:09 +0000303 // Generate a temporary name for the AST file.
304 char astTmpFile[L_tmpnam];
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000305 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");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000319 }
320 // This is run by the parent.
321 int child_status;
322 pid_t tpid;
323 do { // Wait for the child to terminate.
324 tpid = wait(&child_status);
325 } while (tpid != child_pid);
326
327 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000328 ASTUnit *ATU = static_cast<ASTUnit *>(
329 clang_createTranslationUnit(CIdx, astTmpFile));
330 ATU->unlinkTemporaryFile();
331 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000332}
333
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000334void clang_disposeTranslationUnit(
335 CXTranslationUnit CTUnit)
336{
337 assert(CTUnit && "Passed null CXTranslationUnit");
338 delete static_cast<ASTUnit *>(CTUnit);
339}
340
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000341const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
342{
343 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000344 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
345 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000346}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000347
Steve Naroffc857ea42009-09-02 13:28:54 +0000348void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
349 CXTranslationUnitIterator callback,
350 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000351{
Steve Naroff50398192009-08-28 15:28:48 +0000352 assert(CTUnit && "Passed null CXTranslationUnit");
353 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
354 ASTContext &Ctx = CXXUnit->getASTContext();
355
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000356 TUVisitor DVisit(CTUnit, callback, CData);
Steve Naroff50398192009-08-28 15:28:48 +0000357 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000358}
359
Steve Naroffc857ea42009-09-02 13:28:54 +0000360void clang_loadDeclaration(CXDecl Dcl,
361 CXDeclIterator callback,
362 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000363{
Steve Naroffc857ea42009-09-02 13:28:54 +0000364 assert(Dcl && "Passed null CXDecl");
365
366 CDeclVisitor DVisit(Dcl, callback, CData);
367 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000368}
369
Steve Naroff7e8f8182009-08-28 12:07:44 +0000370// Some notes on CXEntity:
371//
372// - Since the 'ordinary' namespace includes functions, data, typedefs,
373// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
374// entity for 2 different types). For example:
375//
376// module1.m: @interface Foo @end Foo *x;
377// module2.m: void Foo(int);
378//
379// - Since the unique name spans translation units, static data/functions
380// within a CXTranslationUnit are *not* currently represented by entities.
381// As a result, there will be no entity for the following:
382//
383// module.m: static void Foo() { }
384//
385
386
Steve Naroff600866c2009-08-27 19:51:58 +0000387const char *clang_getDeclarationName(CXEntity)
388{
389 return "";
390}
391const char *clang_getURI(CXEntity)
392{
393 return "";
394}
395
396CXEntity clang_getEntity(const char *URI)
397{
398 return 0;
399}
400
401//
402// CXDecl Operations.
403//
Steve Naroff600866c2009-08-27 19:51:58 +0000404CXEntity clang_getEntityFromDecl(CXDecl)
405{
406 return 0;
407}
Steve Naroff89922f82009-08-31 00:59:03 +0000408const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000409{
Steve Naroff89922f82009-08-31 00:59:03 +0000410 assert(AnonDecl && "Passed null CXDecl");
411 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000412
413 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
414 return OMD->getSelector().getAsString().c_str();
415 }
Steve Naroff89922f82009-08-31 00:59:03 +0000416 if (ND->getIdentifier())
417 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000418 else
Steve Naroff89922f82009-08-31 00:59:03 +0000419 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000420}
Steve Narofff334b4e2009-09-02 18:26:48 +0000421
Steve Naroff699a07d2009-09-25 21:32:34 +0000422unsigned clang_getDeclLine(CXDecl AnonDecl)
423{
424 assert(AnonDecl && "Passed null CXDecl");
425 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
426 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
427 return SourceMgr.getSpellingLineNumber(ND->getLocation());
428}
429
430unsigned clang_getDeclColumn(CXDecl AnonDecl)
431{
432 assert(AnonDecl && "Passed null CXDecl");
433 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
434 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000435 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000436}
437
Steve Naroffee9405e2009-09-25 21:45:39 +0000438const char *clang_getDeclSource(CXDecl AnonDecl)
439{
440 assert(AnonDecl && "Passed null CXDecl");
441 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
442 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
443 return SourceMgr.getBufferName(ND->getLocation());
444}
445
Steve Narofff334b4e2009-09-02 18:26:48 +0000446const char *clang_getCursorSpelling(CXCursor C)
447{
448 assert(C.decl && "CXCursor has null decl");
449 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
450
451 if (clang_isReference(C.kind)) {
452 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000453 case CXCursor_ObjCSuperClassRef:
454 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000455 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
456 assert(OID && "clang_getCursorLine(): Missing interface decl");
457 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000458 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000459 case CXCursor_ObjCClassRef:
460 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000461 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
462 return OID->getIdentifier()->getName();
463 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000464 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
465 assert(OID && "clang_getCursorLine(): Missing category decl");
466 return OID->getClassInterface()->getIdentifier()->getName();
467 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000468 case CXCursor_ObjCProtocolRef:
469 {
470 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
471 assert(OID && "clang_getCursorLine(): Missing protocol decl");
472 return OID->getIdentifier()->getName();
473 }
Steve Narofffb570422009-09-22 19:25:29 +0000474 case CXCursor_ObjCSelectorRef:
475 {
476 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
477 static_cast<Stmt *>(C.stmt));
478 assert(OME && "clang_getCursorLine(): Missing message expr");
479 return OME->getSelector().getAsString().c_str();
480 }
481 case CXCursor_VarRef:
482 case CXCursor_FunctionRef:
483 case CXCursor_EnumConstantRef:
484 {
485 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
486 static_cast<Stmt *>(C.stmt));
487 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
488 return DRE->getDecl()->getIdentifier()->getName();
489 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000490 default:
491 return "<not implemented>";
492 }
493 }
494 return clang_getDeclSpelling(C.decl);
495}
496
497const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000498{
Steve Naroff89922f82009-08-31 00:59:03 +0000499 switch (Kind) {
500 case CXCursor_FunctionDecl: return "FunctionDecl";
501 case CXCursor_TypedefDecl: return "TypedefDecl";
502 case CXCursor_EnumDecl: return "EnumDecl";
503 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000504 case CXCursor_StructDecl: return "StructDecl";
505 case CXCursor_UnionDecl: return "UnionDecl";
506 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000507 case CXCursor_FieldDecl: return "FieldDecl";
508 case CXCursor_VarDecl: return "VarDecl";
509 case CXCursor_ParmDecl: return "ParmDecl";
510 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
511 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
512 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
513 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
514 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000515 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
516 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
517 case CXCursor_FunctionDefn: return "FunctionDefn";
518 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
519 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
520 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
521 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000522 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000523 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000524 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000525 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
526
527 case CXCursor_VarRef: return "VarRef";
528 case CXCursor_FunctionRef: return "FunctionRef";
529 case CXCursor_EnumConstantRef: return "EnumConstantRef";
530 case CXCursor_MemberRef: return "MemberRef";
531
Steve Naroff77128dd2009-09-15 20:25:34 +0000532 case CXCursor_InvalidFile: return "InvalidFile";
533 case CXCursor_NoDeclFound: return "NoDeclFound";
534 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000535 default: return "<not implemented>";
536 }
Steve Naroff600866c2009-08-27 19:51:58 +0000537}
Steve Naroff89922f82009-08-31 00:59:03 +0000538
Steve Naroff9efa7672009-09-04 15:44:05 +0000539static enum CXCursorKind TranslateKind(Decl *D) {
540 switch (D->getKind()) {
541 case Decl::Function: return CXCursor_FunctionDecl;
542 case Decl::Typedef: return CXCursor_TypedefDecl;
543 case Decl::Enum: return CXCursor_EnumDecl;
544 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
545 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
546 case Decl::Field: return CXCursor_FieldDecl;
547 case Decl::Var: return CXCursor_VarDecl;
548 case Decl::ParmVar: return CXCursor_ParmDecl;
549 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000550 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
551 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000552 case Decl::ObjCMethod: {
553 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
554 if (MD->isInstanceMethod())
555 return CXCursor_ObjCInstanceMethodDecl;
556 return CXCursor_ObjCClassMethodDecl;
557 }
558 default: break;
559 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000560 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000561}
Steve Naroff600866c2009-08-27 19:51:58 +0000562//
563// CXCursor Operations.
564//
Steve Naroff9efa7672009-09-04 15:44:05 +0000565CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000566 unsigned line, unsigned column)
567{
Steve Naroff9efa7672009-09-04 15:44:05 +0000568 assert(CTUnit && "Passed null CXTranslationUnit");
569 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
570
571 FileManager &FMgr = CXXUnit->getFileManager();
572 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000573 source_name+strlen(source_name));
574 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000575 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000576 return C;
577 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000578 SourceLocation SLoc =
579 CXXUnit->getSourceManager().getLocation(File, line, column);
580
581 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
582
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000583 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000584 if (ALoc.isNamedRef())
585 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000586 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000587 if (Dcl) {
588 if (Stm) {
589 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
590 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
591 return C;
592 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
593 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
594 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000595 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000596 // Fall through...treat as a decl, not a ref.
597 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000598 if (ALoc.isNamedRef()) {
599 if (isa<ObjCInterfaceDecl>(Dcl)) {
600 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
601 return C;
602 }
603 if (isa<ObjCProtocolDecl>(Dcl)) {
604 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
605 return C;
606 }
607 }
608 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000609 return C;
610 }
Steve Narofffb570422009-09-22 19:25:29 +0000611 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000612 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000613}
614
Steve Naroff77128dd2009-09-15 20:25:34 +0000615CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
616{
617 assert(AnonDecl && "Passed null CXDecl");
618 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
619
Steve Narofffb570422009-09-22 19:25:29 +0000620 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000621 return C;
622}
623
624unsigned clang_isInvalid(enum CXCursorKind K)
625{
626 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
627}
628
Steve Naroff89922f82009-08-31 00:59:03 +0000629unsigned clang_isDeclaration(enum CXCursorKind K)
630{
631 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
632}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000633
Steve Narofff334b4e2009-09-02 18:26:48 +0000634unsigned clang_isReference(enum CXCursorKind K)
635{
636 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
637}
638
639unsigned clang_isDefinition(enum CXCursorKind K)
640{
641 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
642}
643
Steve Naroff9efa7672009-09-04 15:44:05 +0000644CXCursorKind clang_getCursorKind(CXCursor C)
645{
646 return C.kind;
647}
648
Steve Naroff699a07d2009-09-25 21:32:34 +0000649static Decl *getDeclFromExpr(Stmt *E) {
650 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
651 return RefExpr->getDecl();
652 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
653 return ME->getMemberDecl();
654 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
655 return RE->getDecl();
656
657 if (CallExpr *CE = dyn_cast<CallExpr>(E))
658 return getDeclFromExpr(CE->getCallee());
659 if (CastExpr *CE = dyn_cast<CastExpr>(E))
660 return getDeclFromExpr(CE->getSubExpr());
661 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
662 return OME->getMethodDecl();
663
664 return 0;
665}
666
Steve Naroff9efa7672009-09-04 15:44:05 +0000667CXDecl clang_getCursorDecl(CXCursor C)
668{
Steve Naroff699a07d2009-09-25 21:32:34 +0000669 if (clang_isDeclaration(C.kind))
670 return C.decl;
671
672 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000673 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000674 if (C.kind == CXCursor_ObjCClassRef ||
675 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000676 return static_cast<Stmt *>(C.stmt);
677 else
678 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
679 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000680 return C.decl;
681 }
682 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000683}
684
Steve Naroff85e2db72009-10-01 00:31:07 +0000685
Steve Narofff334b4e2009-09-02 18:26:48 +0000686static SourceLocation getLocationFromCursor(CXCursor C,
687 SourceManager &SourceMgr,
688 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000689 if (clang_isReference(C.kind)) {
690 switch (C.kind) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000691 case CXCursor_ObjCClassRef:
692 {
693 if (isa<ObjCInterfaceDecl>(ND)) {
694 // FIXME: This is a hack (storing the parent decl in the stmt slot).
695 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
696 return parentDecl->getLocation();
697 }
698 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
699 assert(OID && "clang_getCursorLine(): Missing category decl");
700 return OID->getClassInterface()->getLocation();
701 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000702 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000703 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000704 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
705 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000706 return OID->getSuperClassLoc();
707 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000708 case CXCursor_ObjCProtocolRef:
709 {
710 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
711 assert(OID && "clang_getCursorLine(): Missing protocol decl");
712 return OID->getLocation();
713 }
Steve Narofffb570422009-09-22 19:25:29 +0000714 case CXCursor_ObjCSelectorRef:
715 {
716 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
717 static_cast<Stmt *>(C.stmt));
718 assert(OME && "clang_getCursorLine(): Missing message expr");
719 return OME->getLeftLoc(); /* FIXME: should be a range */
720 }
721 case CXCursor_VarRef:
722 case CXCursor_FunctionRef:
723 case CXCursor_EnumConstantRef:
724 {
725 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
726 static_cast<Stmt *>(C.stmt));
727 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
728 return DRE->getLocation();
729 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000730 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000731 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000732 }
733 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000734 SourceLocation SLoc;
735 switch (ND->getKind()) {
736 case Decl::ObjCInterface:
737 {
738 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
739 break;
740 }
741 case Decl::ObjCProtocol:
742 {
743 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
744 break;
745 }
746 default:
747 {
748 SLoc = ND->getLocation();
749 break;
750 }
751 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000752 if (SLoc.isInvalid())
753 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000754 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000755 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000756}
757
Steve Naroff2d4d6292009-08-31 14:26:51 +0000758unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000759{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000760 assert(C.decl && "CXCursor has null decl");
761 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000762 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000763
764 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000765 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000766}
Steve Narofff334b4e2009-09-02 18:26:48 +0000767
Steve Naroff2d4d6292009-08-31 14:26:51 +0000768unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000769{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000770 assert(C.decl && "CXCursor has null decl");
771 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000772 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000773
774 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000775 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000776}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000777const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000778{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000779 assert(C.decl && "CXCursor has null decl");
780 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000781 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000782
783 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000784 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000785}
786
Steve Naroff4ade6d62009-09-23 17:52:52 +0000787void clang_getDefinitionSpellingAndExtent(CXCursor C,
788 const char **startBuf,
789 const char **endBuf,
790 unsigned *startLine,
791 unsigned *startColumn,
792 unsigned *endLine,
793 unsigned *endColumn)
794{
795 assert(C.decl && "CXCursor has null decl");
796 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
797 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
798 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
799
800 SourceManager &SM = FD->getASTContext().getSourceManager();
801 *startBuf = SM.getCharacterData(Body->getLBracLoc());
802 *endBuf = SM.getCharacterData(Body->getRBracLoc());
803 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
804 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
805 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
806 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
807}
808
809
Steve Naroff600866c2009-08-27 19:51:58 +0000810} // end extern "C"