blob: 4ade9477c56e91abb991b00747073283b34d8259 [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 Kramer0829a832009-10-18 11:19:36 +000027#include "llvm/System/Program.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000028#include <cstdio>
Daniel Dunbara47dd192009-10-17 23:53:11 +000029#ifndef _MSC_VER
Steve Naroff5b7d8e22009-10-15 20:04:39 +000030#include <dlfcn.h>
Daniel Dunbara47dd192009-10-17 23:53:11 +000031#endif
Ted Kremenek74cd0692009-10-15 23:21:22 +000032#include <vector>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000033
Steve Naroff50398192009-08-28 15:28:48 +000034using namespace clang;
35using namespace idx;
36
Steve Naroff89922f82009-08-31 00:59:03 +000037namespace {
38
Steve Naroff4ade6d62009-09-23 17:52:52 +000039static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
40{
41 NamedDecl *D = DRE->getDecl();
42 if (isa<VarDecl>(D))
43 return CXCursor_VarRef;
44 else if (isa<FunctionDecl>(D))
45 return CXCursor_FunctionRef;
46 else if (isa<EnumConstantDecl>(D))
47 return CXCursor_EnumConstantRef;
48 else
49 return CXCursor_NotImplemented;
50}
51
52#if 0
53// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000054class CRefVisitor : public StmtVisitor<CRefVisitor> {
55 CXDecl CDecl;
56 CXDeclIterator Callback;
57 CXClientData CData;
58
59 void Call(enum CXCursorKind CK, Stmt *SRef) {
60 CXCursor C = { CK, CDecl, SRef };
61 Callback(CDecl, C, CData);
62 }
63
64public:
65 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
66 CDecl(C), Callback(cback), CData(D) {}
67
68 void VisitStmt(Stmt *S) {
69 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
70 C != CEnd; ++C)
71 Visit(*C);
72 }
73 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000074 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000075 }
76 void VisitMemberExpr(MemberExpr *Node) {
77 Call(CXCursor_MemberRef, Node);
78 }
79 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
80 Call(CXCursor_ObjCSelectorRef, Node);
81 }
82 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
83 Call(CXCursor_ObjCIvarRef, Node);
84 }
85};
Steve Naroff4ade6d62009-09-23 17:52:52 +000086#endif
Steve Narofffb570422009-09-22 19:25:29 +000087
Steve Naroff89922f82009-08-31 00:59:03 +000088// Translation Unit Visitor.
89class TUVisitor : public DeclVisitor<TUVisitor> {
90 CXTranslationUnit TUnit;
91 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000092 CXClientData CData;
93
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000094 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
95 // to the visitor. Declarations with a PCH level greater than this value will
96 // be suppressed.
97 unsigned MaxPCHLevel;
98
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000099 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000100 // Filter any declarations that have a PCH level greater than what we allow.
101 if (ND->getPCHLevel() > MaxPCHLevel)
102 return;
103
Steve Narofffb570422009-09-22 19:25:29 +0000104 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000105 Callback(TUnit, C, CData);
106 }
Steve Naroff89922f82009-08-31 00:59:03 +0000107public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000108 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000109 CXTranslationUnitIterator cback, CXClientData D,
110 unsigned MaxPCHLevel) :
111 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000112
113 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
114 VisitDeclContext(dyn_cast<DeclContext>(D));
115 }
116 void VisitDeclContext(DeclContext *DC) {
117 for (DeclContext::decl_iterator
118 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
119 Visit(*I);
120 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000121 void VisitTypedefDecl(TypedefDecl *ND) {
122 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000123 }
124 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000125 switch (ND->getTagKind()) {
126 case TagDecl::TK_struct:
127 Call(CXCursor_StructDecl, ND);
128 break;
129 case TagDecl::TK_class:
130 Call(CXCursor_ClassDecl, ND);
131 break;
132 case TagDecl::TK_union:
133 Call(CXCursor_UnionDecl, ND);
134 break;
135 case TagDecl::TK_enum:
136 Call(CXCursor_EnumDecl, ND);
137 break;
138 }
Steve Naroff89922f82009-08-31 00:59:03 +0000139 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000140 void VisitVarDecl(VarDecl *ND) {
141 Call(CXCursor_VarDecl, ND);
142 }
Steve Naroff89922f82009-08-31 00:59:03 +0000143 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000144 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
145 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000146 }
147 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000148 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000149 }
150 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000151 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000152 }
153 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000154 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000155 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000156 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
157 Call(CXCursor_ObjCClassDefn, ND);
158 }
159 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
160 Call(CXCursor_ObjCCategoryDefn, ND);
161 }
Steve Naroff89922f82009-08-31 00:59:03 +0000162};
163
Steve Naroffc857ea42009-09-02 13:28:54 +0000164// Declaration visitor.
165class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
166 CXDecl CDecl;
167 CXDeclIterator Callback;
168 CXClientData CData;
169
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000170 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
171 // to the visitor. Declarations with a PCH level greater than this value will
172 // be suppressed.
173 unsigned MaxPCHLevel;
174
Steve Naroffc857ea42009-09-02 13:28:54 +0000175 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000176 // Disable the callback when the context is equal to the visiting decl.
177 if (CDecl == ND && !clang_isReference(CK))
178 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000179
180 // Filter any declarations that have a PCH level greater than what we allow.
181 if (ND->getPCHLevel() > MaxPCHLevel)
182 return;
183
Steve Narofffb570422009-09-22 19:25:29 +0000184 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000185 Callback(CDecl, C, CData);
186 }
Steve Naroff89922f82009-08-31 00:59:03 +0000187public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000188 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
189 unsigned MaxPCHLevel) :
190 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000191
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000192 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
193 // Issue callbacks for the containing class.
194 Call(CXCursor_ObjCClassRef, ND);
195 // FIXME: Issue callbacks for protocol refs.
196 VisitDeclContext(dyn_cast<DeclContext>(ND));
197 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000198 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000199 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000200 if (D->getSuperClass())
201 Call(CXCursor_ObjCSuperClassRef, D);
202
Steve Naroff9efa7672009-09-04 15:44:05 +0000203 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
204 E = D->protocol_end(); I != E; ++I)
205 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000206 VisitDeclContext(dyn_cast<DeclContext>(D));
207 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000208 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
209 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
210 E = PID->protocol_end(); I != E; ++I)
211 Call(CXCursor_ObjCProtocolRef, *I);
212
213 VisitDeclContext(dyn_cast<DeclContext>(PID));
214 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000215 void VisitTagDecl(TagDecl *D) {
216 VisitDeclContext(dyn_cast<DeclContext>(D));
217 }
218 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
219 VisitDeclContext(dyn_cast<DeclContext>(D));
220 }
221 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
222 VisitDeclContext(dyn_cast<DeclContext>(D));
223 }
Steve Naroff89922f82009-08-31 00:59:03 +0000224 void VisitDeclContext(DeclContext *DC) {
225 for (DeclContext::decl_iterator
226 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
227 Visit(*I);
228 }
229 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000230 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000231 }
232 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000233 Call(CXCursor_FieldDecl, ND);
234 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000235 void VisitVarDecl(VarDecl *ND) {
236 Call(CXCursor_VarDecl, ND);
237 }
238 void VisitParmVarDecl(ParmVarDecl *ND) {
239 Call(CXCursor_ParmDecl, ND);
240 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000241 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
242 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000243 }
244 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000245 Call(CXCursor_ObjCIvarDecl, ND);
246 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000247 void VisitFunctionDecl(FunctionDecl *ND) {
248 if (ND->isThisDeclarationADefinition()) {
249 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000250#if 0
251 // Not currently needed.
252 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000253 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000254 RVisit.Visit(Body);
255#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000256 }
257 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000258 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
259 if (ND->getBody()) {
260 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
261 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000262 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000263 } else
264 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
265 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000266 }
267};
268
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000269class CIndexer : public Indexer {
270public:
Benjamin Kramer96707622009-10-18 11:10:55 +0000271 explicit CIndexer(Program *prog, std::string &path)
272 : Indexer(*prog), OnlyLocalDecls(false), ClangPath(path) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000273
274 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000275
276 /// \brief Whether we only want to see "local" declarations (that did not
277 /// come from a previous precompiled header). If false, we want to see all
278 /// declarations.
279 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
280 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000281
282 const std::string& getClangPath() { return ClangPath; }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000283private:
284 bool OnlyLocalDecls;
Benjamin Kramer96707622009-10-18 11:10:55 +0000285 std::string ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000286};
287
Steve Naroff89922f82009-08-31 00:59:03 +0000288}
289
Steve Naroff600866c2009-08-27 19:51:58 +0000290extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000291
Steve Naroff600866c2009-08-27 19:51:58 +0000292CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000293{
Daniel Dunbara47dd192009-10-17 23:53:11 +0000294 // FIXME: This is a hack to unbreak the MSVC build.
295#ifdef _MSC_VER
296 llvm::sys::Path CIndexPath("");
297#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000298 // Find the location where this library lives (libCIndex.dylib).
299 // We do the lookup here to avoid poking dladdr too many times.
300 // This silly cast below avoids a C++ warning.
301 Dl_info info;
302 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
303 assert(0 && "Call to dladdr() failed");
304
305 llvm::sys::Path CIndexPath(info.dli_fname);
Daniel Dunbara47dd192009-10-17 23:53:11 +0000306#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000307 std::string CIndexDir = CIndexPath.getDirname();
308
309 // We now have the CIndex directory, locate clang relative to it.
310 std::string ClangPath = CIndexDir + "/../bin/clang";
311
Benjamin Kramer96707622009-10-18 11:10:55 +0000312 return new CIndexer(new Program(), ClangPath);
Steve Naroff600866c2009-08-27 19:51:58 +0000313}
314
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000315void clang_disposeIndex(CXIndex CIdx)
316{
317 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000318 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000319}
320
Steve Naroff50398192009-08-28 15:28:48 +0000321// FIXME: need to pass back error info.
322CXTranslationUnit clang_createTranslationUnit(
323 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000324{
Steve Naroff50398192009-08-28 15:28:48 +0000325 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000326 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000327 std::string astName(ast_filename);
328 std::string ErrMsg;
329
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000330 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000331 CXXIdx->getFileManager(), &ErrMsg,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000332 CXXIdx->getOnlyLocalDecls(),
333 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000334}
335
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000336CXTranslationUnit clang_createTranslationUnitFromSourceFile(
337 CXIndex CIdx,
338 const char *source_filename,
339 int num_command_line_args, const char **command_line_args)
340{
Daniel Dunbara47dd192009-10-17 23:53:11 +0000341 // FIXME: This is a hack to unbreak the build.
342#ifdef _MSC_VER
343 return 0;
344#else
Ted Kremenek74cd0692009-10-15 23:21:22 +0000345 // Build up the arguments for involing clang.
346 std::vector<const char *> argv;
Benjamin Kramer96707622009-10-18 11:10:55 +0000347 argv.push_back(static_cast<CIndexer *>(CIdx)->getClangPath().c_str());
Ted Kremenek74cd0692009-10-15 23:21:22 +0000348 argv.push_back("-emit-ast");
349 argv.push_back(source_filename);
350 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000351 // Generate a temporary name for the AST file.
352 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000353 argv.push_back(tmpnam(astTmpFile));
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000354 for (int i = num_command_line_args; i < num_command_line_args; i++)
Ted Kremenek74cd0692009-10-15 23:21:22 +0000355 argv.push_back(command_line_args[i]);
356 argv.push_back(NULL);
357
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000358 // Generate the AST file in a separate process.
Benjamin Kramer0829a832009-10-18 11:19:36 +0000359 llvm::sys::Program::ExecuteAndWait(llvm::sys::Path(argv[0]), &argv[0]);
360
Steve Naroff37b5ac22009-10-15 20:50:09 +0000361 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000362 ASTUnit *ATU = static_cast<ASTUnit *>(
363 clang_createTranslationUnit(CIdx, astTmpFile));
364 ATU->unlinkTemporaryFile();
365 return ATU;
Daniel Dunbara47dd192009-10-17 23:53:11 +0000366#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000367}
368
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000369void clang_disposeTranslationUnit(
370 CXTranslationUnit CTUnit)
371{
372 assert(CTUnit && "Passed null CXTranslationUnit");
373 delete static_cast<ASTUnit *>(CTUnit);
374}
375
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000376void clang_wantOnlyLocalDeclarations(CXIndex CIdx) {
377 static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true);
378}
379
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000380const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
381{
382 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000383 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
384 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000385}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000386
Steve Naroffc857ea42009-09-02 13:28:54 +0000387void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
388 CXTranslationUnitIterator callback,
389 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000390{
Steve Naroff50398192009-08-28 15:28:48 +0000391 assert(CTUnit && "Passed null CXTranslationUnit");
392 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
393 ASTContext &Ctx = CXXUnit->getASTContext();
394
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000395 TUVisitor DVisit(CTUnit, callback, CData,
396 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000397 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000398}
399
Steve Naroffc857ea42009-09-02 13:28:54 +0000400void clang_loadDeclaration(CXDecl Dcl,
401 CXDeclIterator callback,
402 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000403{
Steve Naroffc857ea42009-09-02 13:28:54 +0000404 assert(Dcl && "Passed null CXDecl");
405
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000406 CDeclVisitor DVisit(Dcl, callback, CData,
407 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000408 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000409}
410
Steve Naroff7e8f8182009-08-28 12:07:44 +0000411// Some notes on CXEntity:
412//
413// - Since the 'ordinary' namespace includes functions, data, typedefs,
414// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
415// entity for 2 different types). For example:
416//
417// module1.m: @interface Foo @end Foo *x;
418// module2.m: void Foo(int);
419//
420// - Since the unique name spans translation units, static data/functions
421// within a CXTranslationUnit are *not* currently represented by entities.
422// As a result, there will be no entity for the following:
423//
424// module.m: static void Foo() { }
425//
426
427
Steve Naroff600866c2009-08-27 19:51:58 +0000428const char *clang_getDeclarationName(CXEntity)
429{
430 return "";
431}
432const char *clang_getURI(CXEntity)
433{
434 return "";
435}
436
437CXEntity clang_getEntity(const char *URI)
438{
439 return 0;
440}
441
442//
443// CXDecl Operations.
444//
Steve Naroff600866c2009-08-27 19:51:58 +0000445CXEntity clang_getEntityFromDecl(CXDecl)
446{
447 return 0;
448}
Steve Naroff89922f82009-08-31 00:59:03 +0000449const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000450{
Steve Naroff89922f82009-08-31 00:59:03 +0000451 assert(AnonDecl && "Passed null CXDecl");
452 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000453
454 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
455 return OMD->getSelector().getAsString().c_str();
456 }
Steve Naroff89922f82009-08-31 00:59:03 +0000457 if (ND->getIdentifier())
458 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000459 else
Steve Naroff89922f82009-08-31 00:59:03 +0000460 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000461}
Steve Narofff334b4e2009-09-02 18:26:48 +0000462
Steve Naroff699a07d2009-09-25 21:32:34 +0000463unsigned clang_getDeclLine(CXDecl AnonDecl)
464{
465 assert(AnonDecl && "Passed null CXDecl");
466 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
467 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
468 return SourceMgr.getSpellingLineNumber(ND->getLocation());
469}
470
471unsigned clang_getDeclColumn(CXDecl AnonDecl)
472{
473 assert(AnonDecl && "Passed null CXDecl");
474 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
475 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000476 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000477}
478
Steve Naroffee9405e2009-09-25 21:45:39 +0000479const char *clang_getDeclSource(CXDecl AnonDecl)
480{
481 assert(AnonDecl && "Passed null CXDecl");
482 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
483 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
484 return SourceMgr.getBufferName(ND->getLocation());
485}
486
Steve Narofff334b4e2009-09-02 18:26:48 +0000487const char *clang_getCursorSpelling(CXCursor C)
488{
489 assert(C.decl && "CXCursor has null decl");
490 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
491
492 if (clang_isReference(C.kind)) {
493 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000494 case CXCursor_ObjCSuperClassRef:
495 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000496 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
497 assert(OID && "clang_getCursorLine(): Missing interface decl");
498 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000499 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000500 case CXCursor_ObjCClassRef:
501 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000502 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
503 return OID->getIdentifier()->getName();
504 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000505 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
506 assert(OID && "clang_getCursorLine(): Missing category decl");
507 return OID->getClassInterface()->getIdentifier()->getName();
508 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000509 case CXCursor_ObjCProtocolRef:
510 {
511 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
512 assert(OID && "clang_getCursorLine(): Missing protocol decl");
513 return OID->getIdentifier()->getName();
514 }
Steve Narofffb570422009-09-22 19:25:29 +0000515 case CXCursor_ObjCSelectorRef:
516 {
517 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
518 static_cast<Stmt *>(C.stmt));
519 assert(OME && "clang_getCursorLine(): Missing message expr");
520 return OME->getSelector().getAsString().c_str();
521 }
522 case CXCursor_VarRef:
523 case CXCursor_FunctionRef:
524 case CXCursor_EnumConstantRef:
525 {
526 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
527 static_cast<Stmt *>(C.stmt));
528 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
529 return DRE->getDecl()->getIdentifier()->getName();
530 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000531 default:
532 return "<not implemented>";
533 }
534 }
535 return clang_getDeclSpelling(C.decl);
536}
537
538const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000539{
Steve Naroff89922f82009-08-31 00:59:03 +0000540 switch (Kind) {
541 case CXCursor_FunctionDecl: return "FunctionDecl";
542 case CXCursor_TypedefDecl: return "TypedefDecl";
543 case CXCursor_EnumDecl: return "EnumDecl";
544 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000545 case CXCursor_StructDecl: return "StructDecl";
546 case CXCursor_UnionDecl: return "UnionDecl";
547 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000548 case CXCursor_FieldDecl: return "FieldDecl";
549 case CXCursor_VarDecl: return "VarDecl";
550 case CXCursor_ParmDecl: return "ParmDecl";
551 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
552 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
553 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
554 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
555 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000556 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
557 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
558 case CXCursor_FunctionDefn: return "FunctionDefn";
559 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
560 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
561 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
562 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000563 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000564 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000565 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000566 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
567
568 case CXCursor_VarRef: return "VarRef";
569 case CXCursor_FunctionRef: return "FunctionRef";
570 case CXCursor_EnumConstantRef: return "EnumConstantRef";
571 case CXCursor_MemberRef: return "MemberRef";
572
Steve Naroff77128dd2009-09-15 20:25:34 +0000573 case CXCursor_InvalidFile: return "InvalidFile";
574 case CXCursor_NoDeclFound: return "NoDeclFound";
575 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000576 default: return "<not implemented>";
577 }
Steve Naroff600866c2009-08-27 19:51:58 +0000578}
Steve Naroff89922f82009-08-31 00:59:03 +0000579
Steve Naroff9efa7672009-09-04 15:44:05 +0000580static enum CXCursorKind TranslateKind(Decl *D) {
581 switch (D->getKind()) {
582 case Decl::Function: return CXCursor_FunctionDecl;
583 case Decl::Typedef: return CXCursor_TypedefDecl;
584 case Decl::Enum: return CXCursor_EnumDecl;
585 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
586 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
587 case Decl::Field: return CXCursor_FieldDecl;
588 case Decl::Var: return CXCursor_VarDecl;
589 case Decl::ParmVar: return CXCursor_ParmDecl;
590 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000591 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
592 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000593 case Decl::ObjCMethod: {
594 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
595 if (MD->isInstanceMethod())
596 return CXCursor_ObjCInstanceMethodDecl;
597 return CXCursor_ObjCClassMethodDecl;
598 }
599 default: break;
600 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000601 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000602}
Steve Naroff600866c2009-08-27 19:51:58 +0000603//
604// CXCursor Operations.
605//
Steve Naroff9efa7672009-09-04 15:44:05 +0000606CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000607 unsigned line, unsigned column)
608{
Steve Naroff9efa7672009-09-04 15:44:05 +0000609 assert(CTUnit && "Passed null CXTranslationUnit");
610 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
611
612 FileManager &FMgr = CXXUnit->getFileManager();
613 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000614 source_name+strlen(source_name));
615 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000616 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000617 return C;
618 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000619 SourceLocation SLoc =
620 CXXUnit->getSourceManager().getLocation(File, line, column);
621
622 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
623
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000624 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000625 if (ALoc.isNamedRef())
626 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000627 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000628 if (Dcl) {
629 if (Stm) {
630 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
631 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
632 return C;
633 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
634 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
635 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000636 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000637 // Fall through...treat as a decl, not a ref.
638 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000639 if (ALoc.isNamedRef()) {
640 if (isa<ObjCInterfaceDecl>(Dcl)) {
641 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
642 return C;
643 }
644 if (isa<ObjCProtocolDecl>(Dcl)) {
645 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
646 return C;
647 }
648 }
649 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000650 return C;
651 }
Steve Narofffb570422009-09-22 19:25:29 +0000652 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000653 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000654}
655
Steve Naroff77128dd2009-09-15 20:25:34 +0000656CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
657{
658 assert(AnonDecl && "Passed null CXDecl");
659 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
660
Steve Narofffb570422009-09-22 19:25:29 +0000661 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000662 return C;
663}
664
665unsigned clang_isInvalid(enum CXCursorKind K)
666{
667 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
668}
669
Steve Naroff89922f82009-08-31 00:59:03 +0000670unsigned clang_isDeclaration(enum CXCursorKind K)
671{
672 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
673}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000674
Steve Narofff334b4e2009-09-02 18:26:48 +0000675unsigned clang_isReference(enum CXCursorKind K)
676{
677 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
678}
679
680unsigned clang_isDefinition(enum CXCursorKind K)
681{
682 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
683}
684
Steve Naroff9efa7672009-09-04 15:44:05 +0000685CXCursorKind clang_getCursorKind(CXCursor C)
686{
687 return C.kind;
688}
689
Steve Naroff699a07d2009-09-25 21:32:34 +0000690static Decl *getDeclFromExpr(Stmt *E) {
691 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
692 return RefExpr->getDecl();
693 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
694 return ME->getMemberDecl();
695 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
696 return RE->getDecl();
697
698 if (CallExpr *CE = dyn_cast<CallExpr>(E))
699 return getDeclFromExpr(CE->getCallee());
700 if (CastExpr *CE = dyn_cast<CastExpr>(E))
701 return getDeclFromExpr(CE->getSubExpr());
702 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
703 return OME->getMethodDecl();
704
705 return 0;
706}
707
Steve Naroff9efa7672009-09-04 15:44:05 +0000708CXDecl clang_getCursorDecl(CXCursor C)
709{
Steve Naroff699a07d2009-09-25 21:32:34 +0000710 if (clang_isDeclaration(C.kind))
711 return C.decl;
712
713 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000714 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000715 if (C.kind == CXCursor_ObjCClassRef ||
716 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000717 return static_cast<Stmt *>(C.stmt);
718 else
719 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
720 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000721 return C.decl;
722 }
723 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000724}
725
Steve Naroff85e2db72009-10-01 00:31:07 +0000726
Steve Narofff334b4e2009-09-02 18:26:48 +0000727static SourceLocation getLocationFromCursor(CXCursor C,
728 SourceManager &SourceMgr,
729 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000730 if (clang_isReference(C.kind)) {
731 switch (C.kind) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000732 case CXCursor_ObjCClassRef:
733 {
734 if (isa<ObjCInterfaceDecl>(ND)) {
735 // FIXME: This is a hack (storing the parent decl in the stmt slot).
736 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
737 return parentDecl->getLocation();
738 }
739 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
740 assert(OID && "clang_getCursorLine(): Missing category decl");
741 return OID->getClassInterface()->getLocation();
742 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000743 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000744 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000745 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
746 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000747 return OID->getSuperClassLoc();
748 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000749 case CXCursor_ObjCProtocolRef:
750 {
751 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
752 assert(OID && "clang_getCursorLine(): Missing protocol decl");
753 return OID->getLocation();
754 }
Steve Narofffb570422009-09-22 19:25:29 +0000755 case CXCursor_ObjCSelectorRef:
756 {
757 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
758 static_cast<Stmt *>(C.stmt));
759 assert(OME && "clang_getCursorLine(): Missing message expr");
760 return OME->getLeftLoc(); /* FIXME: should be a range */
761 }
762 case CXCursor_VarRef:
763 case CXCursor_FunctionRef:
764 case CXCursor_EnumConstantRef:
765 {
766 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
767 static_cast<Stmt *>(C.stmt));
768 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
769 return DRE->getLocation();
770 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000771 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000772 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000773 }
774 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000775 SourceLocation SLoc;
776 switch (ND->getKind()) {
777 case Decl::ObjCInterface:
778 {
779 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
780 break;
781 }
782 case Decl::ObjCProtocol:
783 {
784 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
785 break;
786 }
787 default:
788 {
789 SLoc = ND->getLocation();
790 break;
791 }
792 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000793 if (SLoc.isInvalid())
794 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000795 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000796 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000797}
798
Steve Naroff2d4d6292009-08-31 14:26:51 +0000799unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000800{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000801 assert(C.decl && "CXCursor has null decl");
802 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000803 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000804
805 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000806 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000807}
Steve Narofff334b4e2009-09-02 18:26:48 +0000808
Steve Naroff2d4d6292009-08-31 14:26:51 +0000809unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000810{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000811 assert(C.decl && "CXCursor has null decl");
812 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000813 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000814
815 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000816 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000817}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000818const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000819{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000820 assert(C.decl && "CXCursor has null decl");
821 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000822 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000823
824 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000825 if (SLoc.isFileID())
826 return SourceMgr.getBufferName(SLoc);
827
828 // Retrieve the file in which the macro was instantiated, then provide that
829 // buffer name.
830 // FIXME: Do we want to give specific macro-instantiation information?
831 const llvm::MemoryBuffer *Buffer
832 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
833 if (!Buffer)
834 return 0;
835
836 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000837}
838
Steve Naroff4ade6d62009-09-23 17:52:52 +0000839void clang_getDefinitionSpellingAndExtent(CXCursor C,
840 const char **startBuf,
841 const char **endBuf,
842 unsigned *startLine,
843 unsigned *startColumn,
844 unsigned *endLine,
845 unsigned *endColumn)
846{
847 assert(C.decl && "CXCursor has null decl");
848 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
849 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
850 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
851
852 SourceManager &SM = FD->getASTContext().getSourceManager();
853 *startBuf = SM.getCharacterData(Body->getLBracLoc());
854 *endBuf = SM.getCharacterData(Body->getRBracLoc());
855 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
856 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
857 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
858 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
859}
860
861
Steve Naroff600866c2009-08-27 19:51:58 +0000862} // end extern "C"