blob: 23bbb0c0eb67020d88040194e8d6857045be06b8 [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>
Ted Kremenek74cd0692009-10-15 23:21:22 +000028#include <vector>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000029#include "llvm/System/Path.h"
30
Steve Naroff50398192009-08-28 15:28:48 +000031using namespace clang;
32using namespace idx;
33
Steve Naroff89922f82009-08-31 00:59:03 +000034namespace {
35
Steve Naroff4ade6d62009-09-23 17:52:52 +000036static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
37{
38 NamedDecl *D = DRE->getDecl();
39 if (isa<VarDecl>(D))
40 return CXCursor_VarRef;
41 else if (isa<FunctionDecl>(D))
42 return CXCursor_FunctionRef;
43 else if (isa<EnumConstantDecl>(D))
44 return CXCursor_EnumConstantRef;
45 else
46 return CXCursor_NotImplemented;
47}
48
49#if 0
50// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000051class CRefVisitor : public StmtVisitor<CRefVisitor> {
52 CXDecl CDecl;
53 CXDeclIterator Callback;
54 CXClientData CData;
55
56 void Call(enum CXCursorKind CK, Stmt *SRef) {
57 CXCursor C = { CK, CDecl, SRef };
58 Callback(CDecl, C, CData);
59 }
60
61public:
62 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
63 CDecl(C), Callback(cback), CData(D) {}
64
65 void VisitStmt(Stmt *S) {
66 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
67 C != CEnd; ++C)
68 Visit(*C);
69 }
70 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000071 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000072 }
73 void VisitMemberExpr(MemberExpr *Node) {
74 Call(CXCursor_MemberRef, Node);
75 }
76 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
77 Call(CXCursor_ObjCSelectorRef, Node);
78 }
79 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
80 Call(CXCursor_ObjCIvarRef, Node);
81 }
82};
Steve Naroff4ade6d62009-09-23 17:52:52 +000083#endif
Steve Narofffb570422009-09-22 19:25:29 +000084
Steve Naroff89922f82009-08-31 00:59:03 +000085// Translation Unit Visitor.
86class TUVisitor : public DeclVisitor<TUVisitor> {
87 CXTranslationUnit TUnit;
88 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000089 CXClientData CData;
90
91 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Narofffb570422009-09-22 19:25:29 +000092 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000093 Callback(TUnit, C, CData);
94 }
Steve Naroff89922f82009-08-31 00:59:03 +000095public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000096 TUVisitor(CXTranslationUnit CTU,
97 CXTranslationUnitIterator cback, CXClientData D) :
98 TUnit(CTU), Callback(cback), CData(D) {}
Steve Naroff89922f82009-08-31 00:59:03 +000099
100 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
101 VisitDeclContext(dyn_cast<DeclContext>(D));
102 }
103 void VisitDeclContext(DeclContext *DC) {
104 for (DeclContext::decl_iterator
105 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
106 Visit(*I);
107 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000108 void VisitTypedefDecl(TypedefDecl *ND) {
109 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000110 }
111 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000112 switch (ND->getTagKind()) {
113 case TagDecl::TK_struct:
114 Call(CXCursor_StructDecl, ND);
115 break;
116 case TagDecl::TK_class:
117 Call(CXCursor_ClassDecl, ND);
118 break;
119 case TagDecl::TK_union:
120 Call(CXCursor_UnionDecl, ND);
121 break;
122 case TagDecl::TK_enum:
123 Call(CXCursor_EnumDecl, ND);
124 break;
125 }
Steve Naroff89922f82009-08-31 00:59:03 +0000126 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000127 void VisitVarDecl(VarDecl *ND) {
128 Call(CXCursor_VarDecl, ND);
129 }
Steve Naroff89922f82009-08-31 00:59:03 +0000130 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000131 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
132 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000133 }
134 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000135 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000136 }
137 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000138 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000139 }
140 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000141 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000142 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000143 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
144 Call(CXCursor_ObjCClassDefn, ND);
145 }
146 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
147 Call(CXCursor_ObjCCategoryDefn, ND);
148 }
Steve Naroff89922f82009-08-31 00:59:03 +0000149};
150
Steve Naroffc857ea42009-09-02 13:28:54 +0000151// Declaration visitor.
152class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
153 CXDecl CDecl;
154 CXDeclIterator Callback;
155 CXClientData CData;
156
157 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000158 // Disable the callback when the context is equal to the visiting decl.
159 if (CDecl == ND && !clang_isReference(CK))
160 return;
Steve Narofffb570422009-09-22 19:25:29 +0000161 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000162 Callback(CDecl, C, CData);
163 }
Steve Naroff89922f82009-08-31 00:59:03 +0000164public:
Steve Naroffc857ea42009-09-02 13:28:54 +0000165 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
166 CDecl(C), Callback(cback), CData(D) {}
167
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000168 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
169 // Issue callbacks for the containing class.
170 Call(CXCursor_ObjCClassRef, ND);
171 // FIXME: Issue callbacks for protocol refs.
172 VisitDeclContext(dyn_cast<DeclContext>(ND));
173 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000174 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000175 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000176 if (D->getSuperClass())
177 Call(CXCursor_ObjCSuperClassRef, D);
178
Steve Naroff9efa7672009-09-04 15:44:05 +0000179 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
180 E = D->protocol_end(); I != E; ++I)
181 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000182 VisitDeclContext(dyn_cast<DeclContext>(D));
183 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000184 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
185 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
186 E = PID->protocol_end(); I != E; ++I)
187 Call(CXCursor_ObjCProtocolRef, *I);
188
189 VisitDeclContext(dyn_cast<DeclContext>(PID));
190 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000191 void VisitTagDecl(TagDecl *D) {
192 VisitDeclContext(dyn_cast<DeclContext>(D));
193 }
194 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
195 VisitDeclContext(dyn_cast<DeclContext>(D));
196 }
197 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
198 VisitDeclContext(dyn_cast<DeclContext>(D));
199 }
Steve Naroff89922f82009-08-31 00:59:03 +0000200 void VisitDeclContext(DeclContext *DC) {
201 for (DeclContext::decl_iterator
202 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
203 Visit(*I);
204 }
205 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000206 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000207 }
208 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000209 Call(CXCursor_FieldDecl, ND);
210 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000211 void VisitVarDecl(VarDecl *ND) {
212 Call(CXCursor_VarDecl, ND);
213 }
214 void VisitParmVarDecl(ParmVarDecl *ND) {
215 Call(CXCursor_ParmDecl, ND);
216 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000217 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
218 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000219 }
220 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000221 Call(CXCursor_ObjCIvarDecl, ND);
222 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000223 void VisitFunctionDecl(FunctionDecl *ND) {
224 if (ND->isThisDeclarationADefinition()) {
225 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000226#if 0
227 // Not currently needed.
228 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000229 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000230 RVisit.Visit(Body);
231#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000232 }
233 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000234 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
235 if (ND->getBody()) {
236 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
237 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000238 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000239 } else
240 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
241 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000242 }
243};
244
245}
246
Steve Naroff600866c2009-08-27 19:51:58 +0000247extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000248
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000249static const char *clangPath;
250
Steve Naroff600866c2009-08-27 19:51:58 +0000251CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000252{
Daniel Dunbara3907592009-09-21 03:03:22 +0000253 // FIXME: Program is leaked.
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000254
255 // Find the location where this library lives (libCIndex.dylib).
256 // We do the lookup here to avoid poking dladdr too many times.
257 // This silly cast below avoids a C++ warning.
258 Dl_info info;
259 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
260 assert(0 && "Call to dladdr() failed");
261
262 llvm::sys::Path CIndexPath(info.dli_fname);
263 std::string CIndexDir = CIndexPath.getDirname();
264
265 // We now have the CIndex directory, locate clang relative to it.
266 std::string ClangPath = CIndexDir + "/../bin/clang";
267
268 clangPath = ClangPath.c_str();
269
Daniel Dunbara3907592009-09-21 03:03:22 +0000270 return new Indexer(*new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000271}
272
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000273void clang_disposeIndex(CXIndex CIdx)
274{
275 assert(CIdx && "Passed null CXIndex");
276 delete static_cast<Indexer *>(CIdx);
277}
278
Steve Naroff50398192009-08-28 15:28:48 +0000279// FIXME: need to pass back error info.
280CXTranslationUnit clang_createTranslationUnit(
281 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000282{
Steve Naroff50398192009-08-28 15:28:48 +0000283 assert(CIdx && "Passed null CXIndex");
284 Indexer *CXXIdx = static_cast<Indexer *>(CIdx);
285 std::string astName(ast_filename);
286 std::string ErrMsg;
287
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000288 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
289 CXXIdx->getFileManager(), &ErrMsg);
Steve Naroff600866c2009-08-27 19:51:58 +0000290}
291
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000292CXTranslationUnit clang_createTranslationUnitFromSourceFile(
293 CXIndex CIdx,
294 const char *source_filename,
295 int num_command_line_args, const char **command_line_args)
296{
Ted Kremenek74cd0692009-10-15 23:21:22 +0000297 // Build up the arguments for involing clang.
298 std::vector<const char *> argv;
299 argv.push_back(clangPath);
300 argv.push_back("-emit-ast");
301 argv.push_back(source_filename);
302 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000303 // Generate a temporary name for the AST file.
304 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000305 argv.push_back(tmpnam(astTmpFile));
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000306 for (int i = num_command_line_args; i < num_command_line_args; i++)
Ted Kremenek74cd0692009-10-15 23:21:22 +0000307 argv.push_back(command_line_args[i]);
308 argv.push_back(NULL);
309
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000310 // 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.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000315 execv(argv[0], (char *const *)&argv[0]);
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000316
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"