blob: 33099cc2393cbd5ca0788bc2fc2c84f5cf70477d [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.
328 return clang_createTranslationUnit(CIdx, astTmpFile);
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000329}
330
Steve Naroff2bd6b9f2009-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 Naroffaf08ddc2009-09-03 15:49:00 +0000338const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
339{
340 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000341 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
342 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000343}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000344
Steve Naroffc857ea42009-09-02 13:28:54 +0000345void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
346 CXTranslationUnitIterator callback,
347 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000348{
Steve Naroff50398192009-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 Naroff2b8ee6c2009-09-01 15:55:40 +0000353 TUVisitor DVisit(CTUnit, callback, CData);
Steve Naroff50398192009-08-28 15:28:48 +0000354 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000355}
356
Steve Naroffc857ea42009-09-02 13:28:54 +0000357void clang_loadDeclaration(CXDecl Dcl,
358 CXDeclIterator callback,
359 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000360{
Steve Naroffc857ea42009-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 Naroff600866c2009-08-27 19:51:58 +0000365}
366
Steve Naroff7e8f8182009-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 Naroff600866c2009-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 Naroff600866c2009-08-27 19:51:58 +0000401CXEntity clang_getEntityFromDecl(CXDecl)
402{
403 return 0;
404}
Steve Naroff89922f82009-08-31 00:59:03 +0000405const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000406{
Steve Naroff89922f82009-08-31 00:59:03 +0000407 assert(AnonDecl && "Passed null CXDecl");
408 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000409
410 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
411 return OMD->getSelector().getAsString().c_str();
412 }
Steve Naroff89922f82009-08-31 00:59:03 +0000413 if (ND->getIdentifier())
414 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000415 else
Steve Naroff89922f82009-08-31 00:59:03 +0000416 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000417}
Steve Narofff334b4e2009-09-02 18:26:48 +0000418
Steve Naroff699a07d2009-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 Naroff74165242009-09-25 22:15:54 +0000432 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000433}
434
Steve Naroffee9405e2009-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 Narofff334b4e2009-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 Naroff1164d852009-09-02 18:58:52 +0000450 case CXCursor_ObjCSuperClassRef:
451 {
Steve Narofff334b4e2009-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 Naroff1164d852009-09-02 18:58:52 +0000455 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000456 case CXCursor_ObjCClassRef:
457 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000458 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
459 return OID->getIdentifier()->getName();
460 }
Steve Naroffaf08ddc2009-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 Naroff9efa7672009-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 Narofffb570422009-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 Narofff334b4e2009-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 Naroff600866c2009-08-27 19:51:58 +0000495{
Steve Naroff89922f82009-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 Naroffc857ea42009-09-02 13:28:54 +0000501 case CXCursor_StructDecl: return "StructDecl";
502 case CXCursor_UnionDecl: return "UnionDecl";
503 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-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 Naroffc857ea42009-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 Narofff334b4e2009-09-02 18:26:48 +0000519 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000520 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000521 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-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 Naroff77128dd2009-09-15 20:25:34 +0000529 case CXCursor_InvalidFile: return "InvalidFile";
530 case CXCursor_NoDeclFound: return "NoDeclFound";
531 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000532 default: return "<not implemented>";
533 }
Steve Naroff600866c2009-08-27 19:51:58 +0000534}
Steve Naroff89922f82009-08-31 00:59:03 +0000535
Steve Naroff9efa7672009-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 Narofffb570422009-09-22 19:25:29 +0000547 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
548 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-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 Naroff77128dd2009-09-15 20:25:34 +0000557 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000558}
Steve Naroff600866c2009-08-27 19:51:58 +0000559//
560// CXCursor Operations.
561//
Steve Naroff9efa7672009-09-04 15:44:05 +0000562CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000563 unsigned line, unsigned column)
564{
Steve Naroff9efa7672009-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 Naroff77128dd2009-09-15 20:25:34 +0000570 source_name+strlen(source_name));
571 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000572 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000573 return C;
574 }
Steve Naroff9efa7672009-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 Kyrtzidisf4526e32009-09-29 19:44:27 +0000580 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000581 if (ALoc.isNamedRef())
582 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000583 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-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 Naroff85e2db72009-10-01 00:31:07 +0000592 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000593 // Fall through...treat as a decl, not a ref.
594 }
Steve Naroff85e2db72009-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 Naroff77128dd2009-09-15 20:25:34 +0000606 return C;
607 }
Steve Narofffb570422009-09-22 19:25:29 +0000608 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000609 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000610}
611
Steve Naroff77128dd2009-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 Narofffb570422009-09-22 19:25:29 +0000617 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-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 Naroff89922f82009-08-31 00:59:03 +0000626unsigned clang_isDeclaration(enum CXCursorKind K)
627{
628 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
629}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000630
Steve Narofff334b4e2009-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 Naroff9efa7672009-09-04 15:44:05 +0000641CXCursorKind clang_getCursorKind(CXCursor C)
642{
643 return C.kind;
644}
645
Steve Naroff699a07d2009-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 Naroff9efa7672009-09-04 15:44:05 +0000664CXDecl clang_getCursorDecl(CXCursor C)
665{
Steve Naroff699a07d2009-09-25 21:32:34 +0000666 if (clang_isDeclaration(C.kind))
667 return C.decl;
668
669 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000670 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000671 if (C.kind == CXCursor_ObjCClassRef ||
672 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-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 Naroff699a07d2009-09-25 21:32:34 +0000677 return C.decl;
678 }
679 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000680}
681
Steve Naroff85e2db72009-10-01 00:31:07 +0000682
Steve Narofff334b4e2009-09-02 18:26:48 +0000683static SourceLocation getLocationFromCursor(CXCursor C,
684 SourceManager &SourceMgr,
685 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000686 if (clang_isReference(C.kind)) {
687 switch (C.kind) {
Steve Naroff85e2db72009-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 Naroff9efa7672009-09-04 15:44:05 +0000699 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000700 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000701 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
702 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000703 return OID->getSuperClassLoc();
704 }
Steve Naroff9efa7672009-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 Narofffb570422009-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 Narofff334b4e2009-09-02 18:26:48 +0000727 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000728 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000729 }
730 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-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 Narofff334b4e2009-09-02 18:26:48 +0000749 if (SLoc.isInvalid())
750 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000751 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000752 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000753}
754
Steve Naroff2d4d6292009-08-31 14:26:51 +0000755unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000756{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000757 assert(C.decl && "CXCursor has null decl");
758 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000759 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000760
761 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000762 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000763}
Steve Narofff334b4e2009-09-02 18:26:48 +0000764
Steve Naroff2d4d6292009-08-31 14:26:51 +0000765unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000766{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000767 assert(C.decl && "CXCursor has null decl");
768 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000769 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000770
771 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000772 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000773}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000774const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000775{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000776 assert(C.decl && "CXCursor has null decl");
777 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000778 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000779
780 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000781 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000782}
783
Steve Naroff4ade6d62009-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 Naroff600866c2009-08-27 19:51:58 +0000807} // end extern "C"