blob: d0638d07728b0bee1c9ee6597b74e74ca0f3e2d7 [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"
Douglas Gregor02465752009-10-16 21:24:31 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/System/Path.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000027#include <cstdio>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000028#include <dlfcn.h>
Ted Kremenek8c4195e2009-10-15 22:10:56 +000029#include <sys/wait.h>
Ted Kremenek74cd0692009-10-15 23:21:22 +000030#include <vector>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000031
Steve Naroff50398192009-08-28 15:28:48 +000032using namespace clang;
33using namespace idx;
34
Steve Naroff89922f82009-08-31 00:59:03 +000035namespace {
36
Steve Naroff4ade6d62009-09-23 17:52:52 +000037static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
38{
39 NamedDecl *D = DRE->getDecl();
40 if (isa<VarDecl>(D))
41 return CXCursor_VarRef;
42 else if (isa<FunctionDecl>(D))
43 return CXCursor_FunctionRef;
44 else if (isa<EnumConstantDecl>(D))
45 return CXCursor_EnumConstantRef;
46 else
47 return CXCursor_NotImplemented;
48}
49
50#if 0
51// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000052class CRefVisitor : public StmtVisitor<CRefVisitor> {
53 CXDecl CDecl;
54 CXDeclIterator Callback;
55 CXClientData CData;
56
57 void Call(enum CXCursorKind CK, Stmt *SRef) {
58 CXCursor C = { CK, CDecl, SRef };
59 Callback(CDecl, C, CData);
60 }
61
62public:
63 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
64 CDecl(C), Callback(cback), CData(D) {}
65
66 void VisitStmt(Stmt *S) {
67 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
68 C != CEnd; ++C)
69 Visit(*C);
70 }
71 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000072 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000073 }
74 void VisitMemberExpr(MemberExpr *Node) {
75 Call(CXCursor_MemberRef, Node);
76 }
77 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
78 Call(CXCursor_ObjCSelectorRef, Node);
79 }
80 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
81 Call(CXCursor_ObjCIvarRef, Node);
82 }
83};
Steve Naroff4ade6d62009-09-23 17:52:52 +000084#endif
Steve Narofffb570422009-09-22 19:25:29 +000085
Steve Naroff89922f82009-08-31 00:59:03 +000086// Translation Unit Visitor.
87class TUVisitor : public DeclVisitor<TUVisitor> {
88 CXTranslationUnit TUnit;
89 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000090 CXClientData CData;
91
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000092 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
93 // to the visitor. Declarations with a PCH level greater than this value will
94 // be suppressed.
95 unsigned MaxPCHLevel;
96
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000097 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000098 // Filter any declarations that have a PCH level greater than what we allow.
99 if (ND->getPCHLevel() > MaxPCHLevel)
100 return;
101
Steve Narofffb570422009-09-22 19:25:29 +0000102 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000103 Callback(TUnit, C, CData);
104 }
Steve Naroff89922f82009-08-31 00:59:03 +0000105public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000106 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000107 CXTranslationUnitIterator cback, CXClientData D,
108 unsigned MaxPCHLevel) :
109 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000110
111 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
112 VisitDeclContext(dyn_cast<DeclContext>(D));
113 }
114 void VisitDeclContext(DeclContext *DC) {
115 for (DeclContext::decl_iterator
116 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
117 Visit(*I);
118 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000119 void VisitTypedefDecl(TypedefDecl *ND) {
120 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000121 }
122 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000123 switch (ND->getTagKind()) {
124 case TagDecl::TK_struct:
125 Call(CXCursor_StructDecl, ND);
126 break;
127 case TagDecl::TK_class:
128 Call(CXCursor_ClassDecl, ND);
129 break;
130 case TagDecl::TK_union:
131 Call(CXCursor_UnionDecl, ND);
132 break;
133 case TagDecl::TK_enum:
134 Call(CXCursor_EnumDecl, ND);
135 break;
136 }
Steve Naroff89922f82009-08-31 00:59:03 +0000137 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000138 void VisitVarDecl(VarDecl *ND) {
139 Call(CXCursor_VarDecl, ND);
140 }
Steve Naroff89922f82009-08-31 00:59:03 +0000141 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000142 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
143 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000144 }
145 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000146 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000147 }
148 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000149 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000150 }
151 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000152 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000153 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000154 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
155 Call(CXCursor_ObjCClassDefn, ND);
156 }
157 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
158 Call(CXCursor_ObjCCategoryDefn, ND);
159 }
Steve Naroff89922f82009-08-31 00:59:03 +0000160};
161
Steve Naroffc857ea42009-09-02 13:28:54 +0000162// Declaration visitor.
163class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
164 CXDecl CDecl;
165 CXDeclIterator Callback;
166 CXClientData CData;
167
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000168 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
169 // to the visitor. Declarations with a PCH level greater than this value will
170 // be suppressed.
171 unsigned MaxPCHLevel;
172
Steve Naroffc857ea42009-09-02 13:28:54 +0000173 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000174 // Disable the callback when the context is equal to the visiting decl.
175 if (CDecl == ND && !clang_isReference(CK))
176 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000177
178 // Filter any declarations that have a PCH level greater than what we allow.
179 if (ND->getPCHLevel() > MaxPCHLevel)
180 return;
181
Steve Narofffb570422009-09-22 19:25:29 +0000182 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000183 Callback(CDecl, C, CData);
184 }
Steve Naroff89922f82009-08-31 00:59:03 +0000185public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000186 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
187 unsigned MaxPCHLevel) :
188 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000189
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000190 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
191 // Issue callbacks for the containing class.
192 Call(CXCursor_ObjCClassRef, ND);
193 // FIXME: Issue callbacks for protocol refs.
194 VisitDeclContext(dyn_cast<DeclContext>(ND));
195 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000196 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000197 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000198 if (D->getSuperClass())
199 Call(CXCursor_ObjCSuperClassRef, D);
200
Steve Naroff9efa7672009-09-04 15:44:05 +0000201 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
202 E = D->protocol_end(); I != E; ++I)
203 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000204 VisitDeclContext(dyn_cast<DeclContext>(D));
205 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000206 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
207 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
208 E = PID->protocol_end(); I != E; ++I)
209 Call(CXCursor_ObjCProtocolRef, *I);
210
211 VisitDeclContext(dyn_cast<DeclContext>(PID));
212 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000213 void VisitTagDecl(TagDecl *D) {
214 VisitDeclContext(dyn_cast<DeclContext>(D));
215 }
216 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
217 VisitDeclContext(dyn_cast<DeclContext>(D));
218 }
219 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
220 VisitDeclContext(dyn_cast<DeclContext>(D));
221 }
Steve Naroff89922f82009-08-31 00:59:03 +0000222 void VisitDeclContext(DeclContext *DC) {
223 for (DeclContext::decl_iterator
224 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
225 Visit(*I);
226 }
227 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000228 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000229 }
230 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000231 Call(CXCursor_FieldDecl, ND);
232 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000233 void VisitVarDecl(VarDecl *ND) {
234 Call(CXCursor_VarDecl, ND);
235 }
236 void VisitParmVarDecl(ParmVarDecl *ND) {
237 Call(CXCursor_ParmDecl, ND);
238 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000239 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
240 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000241 }
242 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000243 Call(CXCursor_ObjCIvarDecl, ND);
244 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000245 void VisitFunctionDecl(FunctionDecl *ND) {
246 if (ND->isThisDeclarationADefinition()) {
247 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000248#if 0
249 // Not currently needed.
250 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000251 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000252 RVisit.Visit(Body);
253#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000254 }
255 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000256 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
257 if (ND->getBody()) {
258 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
259 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000260 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000261 } else
262 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
263 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000264 }
265};
266
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000267class CIndexer : public Indexer {
268public:
269 explicit CIndexer(Program &prog) : Indexer(prog), OnlyLocalDecls(false) { }
270
271 /// \brief Whether we only want to see "local" declarations (that did not
272 /// come from a previous precompiled header). If false, we want to see all
273 /// declarations.
274 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
275 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
276
277private:
278 bool OnlyLocalDecls;
279};
280
Steve Naroff89922f82009-08-31 00:59:03 +0000281}
282
Steve Naroff600866c2009-08-27 19:51:58 +0000283extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000284
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000285static const char *clangPath;
286
Steve Naroff600866c2009-08-27 19:51:58 +0000287CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000288{
Daniel Dunbara3907592009-09-21 03:03:22 +0000289 // FIXME: Program is leaked.
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000290
291 // Find the location where this library lives (libCIndex.dylib).
292 // We do the lookup here to avoid poking dladdr too many times.
293 // This silly cast below avoids a C++ warning.
294 Dl_info info;
295 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
296 assert(0 && "Call to dladdr() failed");
297
298 llvm::sys::Path CIndexPath(info.dli_fname);
299 std::string CIndexDir = CIndexPath.getDirname();
300
301 // We now have the CIndex directory, locate clang relative to it.
302 std::string ClangPath = CIndexDir + "/../bin/clang";
303
304 clangPath = ClangPath.c_str();
305
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000306 return new CIndexer(*new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000307}
308
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000309void clang_disposeIndex(CXIndex CIdx)
310{
311 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000312 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000313}
314
Steve Naroff50398192009-08-28 15:28:48 +0000315// FIXME: need to pass back error info.
316CXTranslationUnit clang_createTranslationUnit(
317 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000318{
Steve Naroff50398192009-08-28 15:28:48 +0000319 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000320 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000321 std::string astName(ast_filename);
322 std::string ErrMsg;
323
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000324 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000325 CXXIdx->getFileManager(), &ErrMsg,
326 CXXIdx->getOnlyLocalDecls());
Steve Naroff600866c2009-08-27 19:51:58 +0000327}
328
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000329CXTranslationUnit clang_createTranslationUnitFromSourceFile(
330 CXIndex CIdx,
331 const char *source_filename,
332 int num_command_line_args, const char **command_line_args)
333{
Ted Kremenek74cd0692009-10-15 23:21:22 +0000334 // Build up the arguments for involing clang.
335 std::vector<const char *> argv;
336 argv.push_back(clangPath);
337 argv.push_back("-emit-ast");
338 argv.push_back(source_filename);
339 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000340 // Generate a temporary name for the AST file.
341 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000342 argv.push_back(tmpnam(astTmpFile));
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000343 for (int i = num_command_line_args; i < num_command_line_args; i++)
Ted Kremenek74cd0692009-10-15 23:21:22 +0000344 argv.push_back(command_line_args[i]);
345 argv.push_back(NULL);
346
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000347 // Generate the AST file in a separate process.
348 pid_t child_pid = fork();
349 if (child_pid == 0) { // Child process
350
351 // Execute the command, passing the appropriate arguments.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000352 execv(argv[0], (char *const *)&argv[0]);
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000353
354 // If execv returns, it failed.
355 assert(0 && "execv() failed");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000356 }
357 // This is run by the parent.
358 int child_status;
359 pid_t tpid;
360 do { // Wait for the child to terminate.
361 tpid = wait(&child_status);
362 } while (tpid != child_pid);
363
364 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000365 ASTUnit *ATU = static_cast<ASTUnit *>(
366 clang_createTranslationUnit(CIdx, astTmpFile));
367 ATU->unlinkTemporaryFile();
368 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000369}
370
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000371void clang_disposeTranslationUnit(
372 CXTranslationUnit CTUnit)
373{
374 assert(CTUnit && "Passed null CXTranslationUnit");
375 delete static_cast<ASTUnit *>(CTUnit);
376}
377
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000378void clang_wantOnlyLocalDeclarations(CXIndex CIdx) {
379 static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true);
380}
381
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000382const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
383{
384 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000385 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
386 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000387}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000388
Steve Naroffc857ea42009-09-02 13:28:54 +0000389void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
390 CXTranslationUnitIterator callback,
391 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000392{
Steve Naroff50398192009-08-28 15:28:48 +0000393 assert(CTUnit && "Passed null CXTranslationUnit");
394 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
395 ASTContext &Ctx = CXXUnit->getASTContext();
396
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000397 TUVisitor DVisit(CTUnit, callback, CData,
398 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000399 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000400}
401
Steve Naroffc857ea42009-09-02 13:28:54 +0000402void clang_loadDeclaration(CXDecl Dcl,
403 CXDeclIterator callback,
404 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000405{
Steve Naroffc857ea42009-09-02 13:28:54 +0000406 assert(Dcl && "Passed null CXDecl");
407
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000408 CDeclVisitor DVisit(Dcl, callback, CData,
409 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000410 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000411}
412
Steve Naroff7e8f8182009-08-28 12:07:44 +0000413// Some notes on CXEntity:
414//
415// - Since the 'ordinary' namespace includes functions, data, typedefs,
416// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
417// entity for 2 different types). For example:
418//
419// module1.m: @interface Foo @end Foo *x;
420// module2.m: void Foo(int);
421//
422// - Since the unique name spans translation units, static data/functions
423// within a CXTranslationUnit are *not* currently represented by entities.
424// As a result, there will be no entity for the following:
425//
426// module.m: static void Foo() { }
427//
428
429
Steve Naroff600866c2009-08-27 19:51:58 +0000430const char *clang_getDeclarationName(CXEntity)
431{
432 return "";
433}
434const char *clang_getURI(CXEntity)
435{
436 return "";
437}
438
439CXEntity clang_getEntity(const char *URI)
440{
441 return 0;
442}
443
444//
445// CXDecl Operations.
446//
Steve Naroff600866c2009-08-27 19:51:58 +0000447CXEntity clang_getEntityFromDecl(CXDecl)
448{
449 return 0;
450}
Steve Naroff89922f82009-08-31 00:59:03 +0000451const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000452{
Steve Naroff89922f82009-08-31 00:59:03 +0000453 assert(AnonDecl && "Passed null CXDecl");
454 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000455
456 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
457 return OMD->getSelector().getAsString().c_str();
458 }
Steve Naroff89922f82009-08-31 00:59:03 +0000459 if (ND->getIdentifier())
460 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000461 else
Steve Naroff89922f82009-08-31 00:59:03 +0000462 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000463}
Steve Narofff334b4e2009-09-02 18:26:48 +0000464
Steve Naroff699a07d2009-09-25 21:32:34 +0000465unsigned clang_getDeclLine(CXDecl AnonDecl)
466{
467 assert(AnonDecl && "Passed null CXDecl");
468 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
469 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
470 return SourceMgr.getSpellingLineNumber(ND->getLocation());
471}
472
473unsigned clang_getDeclColumn(CXDecl AnonDecl)
474{
475 assert(AnonDecl && "Passed null CXDecl");
476 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
477 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000478 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000479}
480
Steve Naroffee9405e2009-09-25 21:45:39 +0000481const char *clang_getDeclSource(CXDecl AnonDecl)
482{
483 assert(AnonDecl && "Passed null CXDecl");
484 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
485 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
486 return SourceMgr.getBufferName(ND->getLocation());
487}
488
Steve Narofff334b4e2009-09-02 18:26:48 +0000489const char *clang_getCursorSpelling(CXCursor C)
490{
491 assert(C.decl && "CXCursor has null decl");
492 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
493
494 if (clang_isReference(C.kind)) {
495 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000496 case CXCursor_ObjCSuperClassRef:
497 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000498 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
499 assert(OID && "clang_getCursorLine(): Missing interface decl");
500 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000501 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000502 case CXCursor_ObjCClassRef:
503 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000504 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
505 return OID->getIdentifier()->getName();
506 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000507 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
508 assert(OID && "clang_getCursorLine(): Missing category decl");
509 return OID->getClassInterface()->getIdentifier()->getName();
510 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000511 case CXCursor_ObjCProtocolRef:
512 {
513 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
514 assert(OID && "clang_getCursorLine(): Missing protocol decl");
515 return OID->getIdentifier()->getName();
516 }
Steve Narofffb570422009-09-22 19:25:29 +0000517 case CXCursor_ObjCSelectorRef:
518 {
519 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
520 static_cast<Stmt *>(C.stmt));
521 assert(OME && "clang_getCursorLine(): Missing message expr");
522 return OME->getSelector().getAsString().c_str();
523 }
524 case CXCursor_VarRef:
525 case CXCursor_FunctionRef:
526 case CXCursor_EnumConstantRef:
527 {
528 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
529 static_cast<Stmt *>(C.stmt));
530 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
531 return DRE->getDecl()->getIdentifier()->getName();
532 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000533 default:
534 return "<not implemented>";
535 }
536 }
537 return clang_getDeclSpelling(C.decl);
538}
539
540const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000541{
Steve Naroff89922f82009-08-31 00:59:03 +0000542 switch (Kind) {
543 case CXCursor_FunctionDecl: return "FunctionDecl";
544 case CXCursor_TypedefDecl: return "TypedefDecl";
545 case CXCursor_EnumDecl: return "EnumDecl";
546 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000547 case CXCursor_StructDecl: return "StructDecl";
548 case CXCursor_UnionDecl: return "UnionDecl";
549 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000550 case CXCursor_FieldDecl: return "FieldDecl";
551 case CXCursor_VarDecl: return "VarDecl";
552 case CXCursor_ParmDecl: return "ParmDecl";
553 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
554 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
555 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
556 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
557 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000558 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
559 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
560 case CXCursor_FunctionDefn: return "FunctionDefn";
561 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
562 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
563 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
564 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000565 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000566 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000567 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000568 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
569
570 case CXCursor_VarRef: return "VarRef";
571 case CXCursor_FunctionRef: return "FunctionRef";
572 case CXCursor_EnumConstantRef: return "EnumConstantRef";
573 case CXCursor_MemberRef: return "MemberRef";
574
Steve Naroff77128dd2009-09-15 20:25:34 +0000575 case CXCursor_InvalidFile: return "InvalidFile";
576 case CXCursor_NoDeclFound: return "NoDeclFound";
577 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000578 default: return "<not implemented>";
579 }
Steve Naroff600866c2009-08-27 19:51:58 +0000580}
Steve Naroff89922f82009-08-31 00:59:03 +0000581
Steve Naroff9efa7672009-09-04 15:44:05 +0000582static enum CXCursorKind TranslateKind(Decl *D) {
583 switch (D->getKind()) {
584 case Decl::Function: return CXCursor_FunctionDecl;
585 case Decl::Typedef: return CXCursor_TypedefDecl;
586 case Decl::Enum: return CXCursor_EnumDecl;
587 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
588 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
589 case Decl::Field: return CXCursor_FieldDecl;
590 case Decl::Var: return CXCursor_VarDecl;
591 case Decl::ParmVar: return CXCursor_ParmDecl;
592 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000593 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
594 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000595 case Decl::ObjCMethod: {
596 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
597 if (MD->isInstanceMethod())
598 return CXCursor_ObjCInstanceMethodDecl;
599 return CXCursor_ObjCClassMethodDecl;
600 }
601 default: break;
602 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000603 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000604}
Steve Naroff600866c2009-08-27 19:51:58 +0000605//
606// CXCursor Operations.
607//
Steve Naroff9efa7672009-09-04 15:44:05 +0000608CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000609 unsigned line, unsigned column)
610{
Steve Naroff9efa7672009-09-04 15:44:05 +0000611 assert(CTUnit && "Passed null CXTranslationUnit");
612 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
613
614 FileManager &FMgr = CXXUnit->getFileManager();
615 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000616 source_name+strlen(source_name));
617 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000618 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000619 return C;
620 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000621 SourceLocation SLoc =
622 CXXUnit->getSourceManager().getLocation(File, line, column);
623
624 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
625
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000626 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000627 if (ALoc.isNamedRef())
628 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000629 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000630 if (Dcl) {
631 if (Stm) {
632 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
633 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
634 return C;
635 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
636 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
637 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000638 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000639 // Fall through...treat as a decl, not a ref.
640 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000641 if (ALoc.isNamedRef()) {
642 if (isa<ObjCInterfaceDecl>(Dcl)) {
643 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
644 return C;
645 }
646 if (isa<ObjCProtocolDecl>(Dcl)) {
647 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
648 return C;
649 }
650 }
651 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000652 return C;
653 }
Steve Narofffb570422009-09-22 19:25:29 +0000654 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000655 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000656}
657
Steve Naroff77128dd2009-09-15 20:25:34 +0000658CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
659{
660 assert(AnonDecl && "Passed null CXDecl");
661 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
662
Steve Narofffb570422009-09-22 19:25:29 +0000663 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000664 return C;
665}
666
667unsigned clang_isInvalid(enum CXCursorKind K)
668{
669 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
670}
671
Steve Naroff89922f82009-08-31 00:59:03 +0000672unsigned clang_isDeclaration(enum CXCursorKind K)
673{
674 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
675}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000676
Steve Narofff334b4e2009-09-02 18:26:48 +0000677unsigned clang_isReference(enum CXCursorKind K)
678{
679 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
680}
681
682unsigned clang_isDefinition(enum CXCursorKind K)
683{
684 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
685}
686
Steve Naroff9efa7672009-09-04 15:44:05 +0000687CXCursorKind clang_getCursorKind(CXCursor C)
688{
689 return C.kind;
690}
691
Steve Naroff699a07d2009-09-25 21:32:34 +0000692static Decl *getDeclFromExpr(Stmt *E) {
693 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
694 return RefExpr->getDecl();
695 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
696 return ME->getMemberDecl();
697 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
698 return RE->getDecl();
699
700 if (CallExpr *CE = dyn_cast<CallExpr>(E))
701 return getDeclFromExpr(CE->getCallee());
702 if (CastExpr *CE = dyn_cast<CastExpr>(E))
703 return getDeclFromExpr(CE->getSubExpr());
704 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
705 return OME->getMethodDecl();
706
707 return 0;
708}
709
Steve Naroff9efa7672009-09-04 15:44:05 +0000710CXDecl clang_getCursorDecl(CXCursor C)
711{
Steve Naroff699a07d2009-09-25 21:32:34 +0000712 if (clang_isDeclaration(C.kind))
713 return C.decl;
714
715 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000716 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000717 if (C.kind == CXCursor_ObjCClassRef ||
718 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000719 return static_cast<Stmt *>(C.stmt);
720 else
721 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
722 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000723 return C.decl;
724 }
725 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000726}
727
Steve Naroff85e2db72009-10-01 00:31:07 +0000728
Steve Narofff334b4e2009-09-02 18:26:48 +0000729static SourceLocation getLocationFromCursor(CXCursor C,
730 SourceManager &SourceMgr,
731 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000732 if (clang_isReference(C.kind)) {
733 switch (C.kind) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000734 case CXCursor_ObjCClassRef:
735 {
736 if (isa<ObjCInterfaceDecl>(ND)) {
737 // FIXME: This is a hack (storing the parent decl in the stmt slot).
738 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
739 return parentDecl->getLocation();
740 }
741 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
742 assert(OID && "clang_getCursorLine(): Missing category decl");
743 return OID->getClassInterface()->getLocation();
744 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000745 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000746 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000747 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
748 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000749 return OID->getSuperClassLoc();
750 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000751 case CXCursor_ObjCProtocolRef:
752 {
753 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
754 assert(OID && "clang_getCursorLine(): Missing protocol decl");
755 return OID->getLocation();
756 }
Steve Narofffb570422009-09-22 19:25:29 +0000757 case CXCursor_ObjCSelectorRef:
758 {
759 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
760 static_cast<Stmt *>(C.stmt));
761 assert(OME && "clang_getCursorLine(): Missing message expr");
762 return OME->getLeftLoc(); /* FIXME: should be a range */
763 }
764 case CXCursor_VarRef:
765 case CXCursor_FunctionRef:
766 case CXCursor_EnumConstantRef:
767 {
768 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
769 static_cast<Stmt *>(C.stmt));
770 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
771 return DRE->getLocation();
772 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000773 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000774 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000775 }
776 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000777 SourceLocation SLoc;
778 switch (ND->getKind()) {
779 case Decl::ObjCInterface:
780 {
781 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
782 break;
783 }
784 case Decl::ObjCProtocol:
785 {
786 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
787 break;
788 }
789 default:
790 {
791 SLoc = ND->getLocation();
792 break;
793 }
794 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000795 if (SLoc.isInvalid())
796 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000797 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000798 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000799}
800
Steve Naroff2d4d6292009-08-31 14:26:51 +0000801unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000802{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000803 assert(C.decl && "CXCursor has null decl");
804 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000805 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000806
807 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000808 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000809}
Steve Narofff334b4e2009-09-02 18:26:48 +0000810
Steve Naroff2d4d6292009-08-31 14:26:51 +0000811unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000812{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000813 assert(C.decl && "CXCursor has null decl");
814 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000815 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000816
817 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000818 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000819}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000820const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000821{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000822 assert(C.decl && "CXCursor has null decl");
823 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000824 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000825
826 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000827 if (SLoc.isFileID())
828 return SourceMgr.getBufferName(SLoc);
829
830 // Retrieve the file in which the macro was instantiated, then provide that
831 // buffer name.
832 // FIXME: Do we want to give specific macro-instantiation information?
833 const llvm::MemoryBuffer *Buffer
834 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
835 if (!Buffer)
836 return 0;
837
838 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000839}
840
Steve Naroff4ade6d62009-09-23 17:52:52 +0000841void clang_getDefinitionSpellingAndExtent(CXCursor C,
842 const char **startBuf,
843 const char **endBuf,
844 unsigned *startLine,
845 unsigned *startColumn,
846 unsigned *endLine,
847 unsigned *endColumn)
848{
849 assert(C.decl && "CXCursor has null decl");
850 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
851 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
852 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
853
854 SourceManager &SM = FD->getASTContext().getSourceManager();
855 *startBuf = SM.getCharacterData(Body->getLBracLoc());
856 *endBuf = SM.getCharacterData(Body->getRBracLoc());
857 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
858 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
859 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
860 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
861}
862
863
Steve Naroff600866c2009-08-27 19:51:58 +0000864} // end extern "C"