blob: 133a269afdff8f9ff9025aaf4f4295ff2b2c20c0 [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
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000091 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
92 // to the visitor. Declarations with a PCH level greater than this value will
93 // be suppressed.
94 unsigned MaxPCHLevel;
95
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000096 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000097 // Filter any declarations that have a PCH level greater than what we allow.
98 if (ND->getPCHLevel() > MaxPCHLevel)
99 return;
100
Steve Narofffb570422009-09-22 19:25:29 +0000101 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000102 Callback(TUnit, C, CData);
103 }
Steve Naroff89922f82009-08-31 00:59:03 +0000104public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000105 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000106 CXTranslationUnitIterator cback, CXClientData D,
107 unsigned MaxPCHLevel) :
108 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000109
110 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
111 VisitDeclContext(dyn_cast<DeclContext>(D));
112 }
113 void VisitDeclContext(DeclContext *DC) {
114 for (DeclContext::decl_iterator
115 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
116 Visit(*I);
117 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000118 void VisitTypedefDecl(TypedefDecl *ND) {
119 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000120 }
121 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000122 switch (ND->getTagKind()) {
123 case TagDecl::TK_struct:
124 Call(CXCursor_StructDecl, ND);
125 break;
126 case TagDecl::TK_class:
127 Call(CXCursor_ClassDecl, ND);
128 break;
129 case TagDecl::TK_union:
130 Call(CXCursor_UnionDecl, ND);
131 break;
132 case TagDecl::TK_enum:
133 Call(CXCursor_EnumDecl, ND);
134 break;
135 }
Steve Naroff89922f82009-08-31 00:59:03 +0000136 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000137 void VisitVarDecl(VarDecl *ND) {
138 Call(CXCursor_VarDecl, ND);
139 }
Steve Naroff89922f82009-08-31 00:59:03 +0000140 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000141 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
142 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000143 }
144 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000145 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000146 }
147 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000148 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000149 }
150 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000151 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000152 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000153 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
154 Call(CXCursor_ObjCClassDefn, ND);
155 }
156 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
157 Call(CXCursor_ObjCCategoryDefn, ND);
158 }
Steve Naroff89922f82009-08-31 00:59:03 +0000159};
160
Steve Naroffc857ea42009-09-02 13:28:54 +0000161// Declaration visitor.
162class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
163 CXDecl CDecl;
164 CXDeclIterator Callback;
165 CXClientData CData;
166
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000167 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
168 // to the visitor. Declarations with a PCH level greater than this value will
169 // be suppressed.
170 unsigned MaxPCHLevel;
171
Steve Naroffc857ea42009-09-02 13:28:54 +0000172 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000173 // Disable the callback when the context is equal to the visiting decl.
174 if (CDecl == ND && !clang_isReference(CK))
175 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000176
177 // Filter any declarations that have a PCH level greater than what we allow.
178 if (ND->getPCHLevel() > MaxPCHLevel)
179 return;
180
Steve Narofffb570422009-09-22 19:25:29 +0000181 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000182 Callback(CDecl, C, CData);
183 }
Steve Naroff89922f82009-08-31 00:59:03 +0000184public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000185 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
186 unsigned MaxPCHLevel) :
187 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000188
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000189 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
190 // Issue callbacks for the containing class.
191 Call(CXCursor_ObjCClassRef, ND);
192 // FIXME: Issue callbacks for protocol refs.
193 VisitDeclContext(dyn_cast<DeclContext>(ND));
194 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000195 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000196 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000197 if (D->getSuperClass())
198 Call(CXCursor_ObjCSuperClassRef, D);
199
Steve Naroff9efa7672009-09-04 15:44:05 +0000200 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
201 E = D->protocol_end(); I != E; ++I)
202 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000203 VisitDeclContext(dyn_cast<DeclContext>(D));
204 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000205 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
206 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
207 E = PID->protocol_end(); I != E; ++I)
208 Call(CXCursor_ObjCProtocolRef, *I);
209
210 VisitDeclContext(dyn_cast<DeclContext>(PID));
211 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000212 void VisitTagDecl(TagDecl *D) {
213 VisitDeclContext(dyn_cast<DeclContext>(D));
214 }
215 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
216 VisitDeclContext(dyn_cast<DeclContext>(D));
217 }
218 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
219 VisitDeclContext(dyn_cast<DeclContext>(D));
220 }
Steve Naroff89922f82009-08-31 00:59:03 +0000221 void VisitDeclContext(DeclContext *DC) {
222 for (DeclContext::decl_iterator
223 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
224 Visit(*I);
225 }
226 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000227 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000228 }
229 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000230 Call(CXCursor_FieldDecl, ND);
231 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000232 void VisitVarDecl(VarDecl *ND) {
233 Call(CXCursor_VarDecl, ND);
234 }
235 void VisitParmVarDecl(ParmVarDecl *ND) {
236 Call(CXCursor_ParmDecl, ND);
237 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000238 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
239 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000240 }
241 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000242 Call(CXCursor_ObjCIvarDecl, ND);
243 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000244 void VisitFunctionDecl(FunctionDecl *ND) {
245 if (ND->isThisDeclarationADefinition()) {
246 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000247#if 0
248 // Not currently needed.
249 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000250 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000251 RVisit.Visit(Body);
252#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000253 }
254 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000255 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
256 if (ND->getBody()) {
257 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
258 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000259 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000260 } else
261 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
262 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000263 }
264};
265
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000266class CIndexer : public Indexer {
267public:
268 explicit CIndexer(Program &prog) : Indexer(prog), OnlyLocalDecls(false) { }
269
270 /// \brief Whether we only want to see "local" declarations (that did not
271 /// come from a previous precompiled header). If false, we want to see all
272 /// declarations.
273 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
274 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
275
276private:
277 bool OnlyLocalDecls;
278};
279
Steve Naroff89922f82009-08-31 00:59:03 +0000280}
281
Steve Naroff600866c2009-08-27 19:51:58 +0000282extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000283
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000284static const char *clangPath;
285
Steve Naroff600866c2009-08-27 19:51:58 +0000286CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000287{
Daniel Dunbara3907592009-09-21 03:03:22 +0000288 // FIXME: Program is leaked.
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000289
290 // Find the location where this library lives (libCIndex.dylib).
291 // We do the lookup here to avoid poking dladdr too many times.
292 // This silly cast below avoids a C++ warning.
293 Dl_info info;
294 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
295 assert(0 && "Call to dladdr() failed");
296
297 llvm::sys::Path CIndexPath(info.dli_fname);
298 std::string CIndexDir = CIndexPath.getDirname();
299
300 // We now have the CIndex directory, locate clang relative to it.
301 std::string ClangPath = CIndexDir + "/../bin/clang";
302
303 clangPath = ClangPath.c_str();
304
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000305 return new CIndexer(*new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000306}
307
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000308void clang_disposeIndex(CXIndex CIdx)
309{
310 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000311 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000312}
313
Steve Naroff50398192009-08-28 15:28:48 +0000314// FIXME: need to pass back error info.
315CXTranslationUnit clang_createTranslationUnit(
316 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000317{
Steve Naroff50398192009-08-28 15:28:48 +0000318 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000319 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000320 std::string astName(ast_filename);
321 std::string ErrMsg;
322
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000323 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000324 CXXIdx->getFileManager(), &ErrMsg,
325 CXXIdx->getOnlyLocalDecls());
Steve Naroff600866c2009-08-27 19:51:58 +0000326}
327
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000328CXTranslationUnit clang_createTranslationUnitFromSourceFile(
329 CXIndex CIdx,
330 const char *source_filename,
331 int num_command_line_args, const char **command_line_args)
332{
Ted Kremenek74cd0692009-10-15 23:21:22 +0000333 // Build up the arguments for involing clang.
334 std::vector<const char *> argv;
335 argv.push_back(clangPath);
336 argv.push_back("-emit-ast");
337 argv.push_back(source_filename);
338 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000339 // Generate a temporary name for the AST file.
340 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000341 argv.push_back(tmpnam(astTmpFile));
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000342 for (int i = num_command_line_args; i < num_command_line_args; i++)
Ted Kremenek74cd0692009-10-15 23:21:22 +0000343 argv.push_back(command_line_args[i]);
344 argv.push_back(NULL);
345
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000346 // Generate the AST file in a separate process.
347 pid_t child_pid = fork();
348 if (child_pid == 0) { // Child process
349
350 // Execute the command, passing the appropriate arguments.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000351 execv(argv[0], (char *const *)&argv[0]);
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000352
353 // If execv returns, it failed.
354 assert(0 && "execv() failed");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000355 }
356 // This is run by the parent.
357 int child_status;
358 pid_t tpid;
359 do { // Wait for the child to terminate.
360 tpid = wait(&child_status);
361 } while (tpid != child_pid);
362
363 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000364 ASTUnit *ATU = static_cast<ASTUnit *>(
365 clang_createTranslationUnit(CIdx, astTmpFile));
366 ATU->unlinkTemporaryFile();
367 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000368}
369
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000370void clang_disposeTranslationUnit(
371 CXTranslationUnit CTUnit)
372{
373 assert(CTUnit && "Passed null CXTranslationUnit");
374 delete static_cast<ASTUnit *>(CTUnit);
375}
376
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000377void clang_wantOnlyLocalDeclarations(CXIndex CIdx) {
378 static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true);
379}
380
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000381const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
382{
383 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000384 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
385 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000386}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000387
Steve Naroffc857ea42009-09-02 13:28:54 +0000388void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
389 CXTranslationUnitIterator callback,
390 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000391{
Steve Naroff50398192009-08-28 15:28:48 +0000392 assert(CTUnit && "Passed null CXTranslationUnit");
393 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
394 ASTContext &Ctx = CXXUnit->getASTContext();
395
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000396 TUVisitor DVisit(CTUnit, callback, CData,
397 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000398 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000399}
400
Steve Naroffc857ea42009-09-02 13:28:54 +0000401void clang_loadDeclaration(CXDecl Dcl,
402 CXDeclIterator callback,
403 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000404{
Steve Naroffc857ea42009-09-02 13:28:54 +0000405 assert(Dcl && "Passed null CXDecl");
406
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000407 CDeclVisitor DVisit(Dcl, callback, CData,
408 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000409 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000410}
411
Steve Naroff7e8f8182009-08-28 12:07:44 +0000412// Some notes on CXEntity:
413//
414// - Since the 'ordinary' namespace includes functions, data, typedefs,
415// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
416// entity for 2 different types). For example:
417//
418// module1.m: @interface Foo @end Foo *x;
419// module2.m: void Foo(int);
420//
421// - Since the unique name spans translation units, static data/functions
422// within a CXTranslationUnit are *not* currently represented by entities.
423// As a result, there will be no entity for the following:
424//
425// module.m: static void Foo() { }
426//
427
428
Steve Naroff600866c2009-08-27 19:51:58 +0000429const char *clang_getDeclarationName(CXEntity)
430{
431 return "";
432}
433const char *clang_getURI(CXEntity)
434{
435 return "";
436}
437
438CXEntity clang_getEntity(const char *URI)
439{
440 return 0;
441}
442
443//
444// CXDecl Operations.
445//
Steve Naroff600866c2009-08-27 19:51:58 +0000446CXEntity clang_getEntityFromDecl(CXDecl)
447{
448 return 0;
449}
Steve Naroff89922f82009-08-31 00:59:03 +0000450const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000451{
Steve Naroff89922f82009-08-31 00:59:03 +0000452 assert(AnonDecl && "Passed null CXDecl");
453 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000454
455 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
456 return OMD->getSelector().getAsString().c_str();
457 }
Steve Naroff89922f82009-08-31 00:59:03 +0000458 if (ND->getIdentifier())
459 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000460 else
Steve Naroff89922f82009-08-31 00:59:03 +0000461 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000462}
Steve Narofff334b4e2009-09-02 18:26:48 +0000463
Steve Naroff699a07d2009-09-25 21:32:34 +0000464unsigned clang_getDeclLine(CXDecl AnonDecl)
465{
466 assert(AnonDecl && "Passed null CXDecl");
467 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
468 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
469 return SourceMgr.getSpellingLineNumber(ND->getLocation());
470}
471
472unsigned clang_getDeclColumn(CXDecl AnonDecl)
473{
474 assert(AnonDecl && "Passed null CXDecl");
475 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
476 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000477 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000478}
479
Steve Naroffee9405e2009-09-25 21:45:39 +0000480const char *clang_getDeclSource(CXDecl AnonDecl)
481{
482 assert(AnonDecl && "Passed null CXDecl");
483 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
484 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
485 return SourceMgr.getBufferName(ND->getLocation());
486}
487
Steve Narofff334b4e2009-09-02 18:26:48 +0000488const char *clang_getCursorSpelling(CXCursor C)
489{
490 assert(C.decl && "CXCursor has null decl");
491 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
492
493 if (clang_isReference(C.kind)) {
494 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000495 case CXCursor_ObjCSuperClassRef:
496 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000497 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
498 assert(OID && "clang_getCursorLine(): Missing interface decl");
499 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000500 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000501 case CXCursor_ObjCClassRef:
502 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000503 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
504 return OID->getIdentifier()->getName();
505 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000506 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
507 assert(OID && "clang_getCursorLine(): Missing category decl");
508 return OID->getClassInterface()->getIdentifier()->getName();
509 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000510 case CXCursor_ObjCProtocolRef:
511 {
512 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
513 assert(OID && "clang_getCursorLine(): Missing protocol decl");
514 return OID->getIdentifier()->getName();
515 }
Steve Narofffb570422009-09-22 19:25:29 +0000516 case CXCursor_ObjCSelectorRef:
517 {
518 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
519 static_cast<Stmt *>(C.stmt));
520 assert(OME && "clang_getCursorLine(): Missing message expr");
521 return OME->getSelector().getAsString().c_str();
522 }
523 case CXCursor_VarRef:
524 case CXCursor_FunctionRef:
525 case CXCursor_EnumConstantRef:
526 {
527 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
528 static_cast<Stmt *>(C.stmt));
529 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
530 return DRE->getDecl()->getIdentifier()->getName();
531 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000532 default:
533 return "<not implemented>";
534 }
535 }
536 return clang_getDeclSpelling(C.decl);
537}
538
539const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000540{
Steve Naroff89922f82009-08-31 00:59:03 +0000541 switch (Kind) {
542 case CXCursor_FunctionDecl: return "FunctionDecl";
543 case CXCursor_TypedefDecl: return "TypedefDecl";
544 case CXCursor_EnumDecl: return "EnumDecl";
545 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000546 case CXCursor_StructDecl: return "StructDecl";
547 case CXCursor_UnionDecl: return "UnionDecl";
548 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000549 case CXCursor_FieldDecl: return "FieldDecl";
550 case CXCursor_VarDecl: return "VarDecl";
551 case CXCursor_ParmDecl: return "ParmDecl";
552 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
553 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
554 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
555 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
556 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000557 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
558 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
559 case CXCursor_FunctionDefn: return "FunctionDefn";
560 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
561 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
562 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
563 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000564 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000565 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000566 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000567 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
568
569 case CXCursor_VarRef: return "VarRef";
570 case CXCursor_FunctionRef: return "FunctionRef";
571 case CXCursor_EnumConstantRef: return "EnumConstantRef";
572 case CXCursor_MemberRef: return "MemberRef";
573
Steve Naroff77128dd2009-09-15 20:25:34 +0000574 case CXCursor_InvalidFile: return "InvalidFile";
575 case CXCursor_NoDeclFound: return "NoDeclFound";
576 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000577 default: return "<not implemented>";
578 }
Steve Naroff600866c2009-08-27 19:51:58 +0000579}
Steve Naroff89922f82009-08-31 00:59:03 +0000580
Steve Naroff9efa7672009-09-04 15:44:05 +0000581static enum CXCursorKind TranslateKind(Decl *D) {
582 switch (D->getKind()) {
583 case Decl::Function: return CXCursor_FunctionDecl;
584 case Decl::Typedef: return CXCursor_TypedefDecl;
585 case Decl::Enum: return CXCursor_EnumDecl;
586 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
587 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
588 case Decl::Field: return CXCursor_FieldDecl;
589 case Decl::Var: return CXCursor_VarDecl;
590 case Decl::ParmVar: return CXCursor_ParmDecl;
591 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000592 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
593 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000594 case Decl::ObjCMethod: {
595 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
596 if (MD->isInstanceMethod())
597 return CXCursor_ObjCInstanceMethodDecl;
598 return CXCursor_ObjCClassMethodDecl;
599 }
600 default: break;
601 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000602 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000603}
Steve Naroff600866c2009-08-27 19:51:58 +0000604//
605// CXCursor Operations.
606//
Steve Naroff9efa7672009-09-04 15:44:05 +0000607CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000608 unsigned line, unsigned column)
609{
Steve Naroff9efa7672009-09-04 15:44:05 +0000610 assert(CTUnit && "Passed null CXTranslationUnit");
611 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
612
613 FileManager &FMgr = CXXUnit->getFileManager();
614 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000615 source_name+strlen(source_name));
616 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000617 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000618 return C;
619 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000620 SourceLocation SLoc =
621 CXXUnit->getSourceManager().getLocation(File, line, column);
622
623 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
624
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000625 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000626 if (ALoc.isNamedRef())
627 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000628 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000629 if (Dcl) {
630 if (Stm) {
631 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
632 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
633 return C;
634 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
635 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
636 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000637 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000638 // Fall through...treat as a decl, not a ref.
639 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000640 if (ALoc.isNamedRef()) {
641 if (isa<ObjCInterfaceDecl>(Dcl)) {
642 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
643 return C;
644 }
645 if (isa<ObjCProtocolDecl>(Dcl)) {
646 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
647 return C;
648 }
649 }
650 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000651 return C;
652 }
Steve Narofffb570422009-09-22 19:25:29 +0000653 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000654 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000655}
656
Steve Naroff77128dd2009-09-15 20:25:34 +0000657CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
658{
659 assert(AnonDecl && "Passed null CXDecl");
660 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
661
Steve Narofffb570422009-09-22 19:25:29 +0000662 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000663 return C;
664}
665
666unsigned clang_isInvalid(enum CXCursorKind K)
667{
668 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
669}
670
Steve Naroff89922f82009-08-31 00:59:03 +0000671unsigned clang_isDeclaration(enum CXCursorKind K)
672{
673 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
674}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000675
Steve Narofff334b4e2009-09-02 18:26:48 +0000676unsigned clang_isReference(enum CXCursorKind K)
677{
678 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
679}
680
681unsigned clang_isDefinition(enum CXCursorKind K)
682{
683 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
684}
685
Steve Naroff9efa7672009-09-04 15:44:05 +0000686CXCursorKind clang_getCursorKind(CXCursor C)
687{
688 return C.kind;
689}
690
Steve Naroff699a07d2009-09-25 21:32:34 +0000691static Decl *getDeclFromExpr(Stmt *E) {
692 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
693 return RefExpr->getDecl();
694 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
695 return ME->getMemberDecl();
696 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
697 return RE->getDecl();
698
699 if (CallExpr *CE = dyn_cast<CallExpr>(E))
700 return getDeclFromExpr(CE->getCallee());
701 if (CastExpr *CE = dyn_cast<CastExpr>(E))
702 return getDeclFromExpr(CE->getSubExpr());
703 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
704 return OME->getMethodDecl();
705
706 return 0;
707}
708
Steve Naroff9efa7672009-09-04 15:44:05 +0000709CXDecl clang_getCursorDecl(CXCursor C)
710{
Steve Naroff699a07d2009-09-25 21:32:34 +0000711 if (clang_isDeclaration(C.kind))
712 return C.decl;
713
714 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000715 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000716 if (C.kind == CXCursor_ObjCClassRef ||
717 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000718 return static_cast<Stmt *>(C.stmt);
719 else
720 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
721 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000722 return C.decl;
723 }
724 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000725}
726
Steve Naroff85e2db72009-10-01 00:31:07 +0000727
Steve Narofff334b4e2009-09-02 18:26:48 +0000728static SourceLocation getLocationFromCursor(CXCursor C,
729 SourceManager &SourceMgr,
730 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000731 if (clang_isReference(C.kind)) {
732 switch (C.kind) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000733 case CXCursor_ObjCClassRef:
734 {
735 if (isa<ObjCInterfaceDecl>(ND)) {
736 // FIXME: This is a hack (storing the parent decl in the stmt slot).
737 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
738 return parentDecl->getLocation();
739 }
740 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
741 assert(OID && "clang_getCursorLine(): Missing category decl");
742 return OID->getClassInterface()->getLocation();
743 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000744 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000745 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000746 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
747 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000748 return OID->getSuperClassLoc();
749 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000750 case CXCursor_ObjCProtocolRef:
751 {
752 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
753 assert(OID && "clang_getCursorLine(): Missing protocol decl");
754 return OID->getLocation();
755 }
Steve Narofffb570422009-09-22 19:25:29 +0000756 case CXCursor_ObjCSelectorRef:
757 {
758 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
759 static_cast<Stmt *>(C.stmt));
760 assert(OME && "clang_getCursorLine(): Missing message expr");
761 return OME->getLeftLoc(); /* FIXME: should be a range */
762 }
763 case CXCursor_VarRef:
764 case CXCursor_FunctionRef:
765 case CXCursor_EnumConstantRef:
766 {
767 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
768 static_cast<Stmt *>(C.stmt));
769 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
770 return DRE->getLocation();
771 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000772 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000773 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000774 }
775 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000776 SourceLocation SLoc;
777 switch (ND->getKind()) {
778 case Decl::ObjCInterface:
779 {
780 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
781 break;
782 }
783 case Decl::ObjCProtocol:
784 {
785 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
786 break;
787 }
788 default:
789 {
790 SLoc = ND->getLocation();
791 break;
792 }
793 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000794 if (SLoc.isInvalid())
795 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000796 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000797 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000798}
799
Steve Naroff2d4d6292009-08-31 14:26:51 +0000800unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000801{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000802 assert(C.decl && "CXCursor has null decl");
803 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000804 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000805
806 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000807 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000808}
Steve Narofff334b4e2009-09-02 18:26:48 +0000809
Steve Naroff2d4d6292009-08-31 14:26:51 +0000810unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000811{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000812 assert(C.decl && "CXCursor has null decl");
813 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000814 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000815
816 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000817 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000818}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000819const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000820{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000821 assert(C.decl && "CXCursor has null decl");
822 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000823 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000824
825 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000826 return SourceMgr.getBufferName(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000827}
828
Steve Naroff4ade6d62009-09-23 17:52:52 +0000829void clang_getDefinitionSpellingAndExtent(CXCursor C,
830 const char **startBuf,
831 const char **endBuf,
832 unsigned *startLine,
833 unsigned *startColumn,
834 unsigned *endLine,
835 unsigned *endColumn)
836{
837 assert(C.decl && "CXCursor has null decl");
838 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
839 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
840 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
841
842 SourceManager &SM = FD->getASTContext().getSourceManager();
843 *startBuf = SM.getCharacterData(Body->getLBracLoc());
844 *endBuf = SM.getCharacterData(Body->getRBracLoc());
845 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
846 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
847 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
848 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
849}
850
851
Steve Naroff600866c2009-08-27 19:51:58 +0000852} // end extern "C"