blob: ad78fbd3112069cee09dabd9f01011edfe7a70dc [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 Kramer5e4bc592009-10-18 16:11:04 +0000271 explicit CIndexer(Program *prog) : Indexer(*prog), OnlyLocalDecls(false) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000272
273 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000274
275 /// \brief Whether we only want to see "local" declarations (that did not
276 /// come from a previous precompiled header). If false, we want to see all
277 /// declarations.
278 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
279 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000280
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000281 /// \brief Get the path of the clang binary.
282 static const llvm::sys::Path& getClangPath();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000283private:
284 bool OnlyLocalDecls;
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000285 static llvm::sys::Path ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000286};
Steve Naroff89922f82009-08-31 00:59:03 +0000287
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000288llvm::sys::Path CIndexer::ClangPath;
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000289
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000290const llvm::sys::Path& CIndexer::getClangPath() {
291 // Did we already compute the path?
292 if (!ClangPath.empty())
293 return ClangPath;
294
Daniel Dunbara47dd192009-10-17 23:53:11 +0000295 // FIXME: This is a hack to unbreak the MSVC build.
296#ifdef _MSC_VER
297 llvm::sys::Path CIndexPath("");
298#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000299 // Find the location where this library lives (libCIndex.dylib).
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000300 // 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
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000307
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000308 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000309 CIndexPath.eraseComponent();
310 CIndexPath.eraseComponent();
311 CIndexPath.appendComponent("bin");
312 CIndexPath.appendComponent("clang");
313
314 // Cache our result.
315 ClangPath = CIndexPath;
316 return ClangPath;
317}
318
319}
320
321extern "C" {
322
323CXIndex clang_createIndex()
324{
325 return new CIndexer(new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000326}
327
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000328void clang_disposeIndex(CXIndex CIdx)
329{
330 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000331 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000332}
333
Steve Naroff50398192009-08-28 15:28:48 +0000334// FIXME: need to pass back error info.
335CXTranslationUnit clang_createTranslationUnit(
336 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000337{
Steve Naroff50398192009-08-28 15:28:48 +0000338 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000339 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000340 std::string astName(ast_filename);
341 std::string ErrMsg;
342
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000343 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000344 CXXIdx->getFileManager(), &ErrMsg,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000345 CXXIdx->getOnlyLocalDecls(),
346 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000347}
348
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000349CXTranslationUnit clang_createTranslationUnitFromSourceFile(
350 CXIndex CIdx,
351 const char *source_filename,
352 int num_command_line_args, const char **command_line_args)
353{
Daniel Dunbara47dd192009-10-17 23:53:11 +0000354 // FIXME: This is a hack to unbreak the build.
355#ifdef _MSC_VER
356 return 0;
357#else
Ted Kremenek74cd0692009-10-15 23:21:22 +0000358 // Build up the arguments for involing clang.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000359 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Ted Kremenek74cd0692009-10-15 23:21:22 +0000360 std::vector<const char *> argv;
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000361 argv.push_back(ClangPath.c_str());
Ted Kremenek74cd0692009-10-15 23:21:22 +0000362 argv.push_back("-emit-ast");
363 argv.push_back(source_filename);
364 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000365 // Generate a temporary name for the AST file.
366 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000367 argv.push_back(tmpnam(astTmpFile));
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000368 for (int i = num_command_line_args; i < num_command_line_args; i++)
Ted Kremenek74cd0692009-10-15 23:21:22 +0000369 argv.push_back(command_line_args[i]);
370 argv.push_back(NULL);
371
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000372 // Generate the AST file in a separate process.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000373 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0]);
Benjamin Kramer0829a832009-10-18 11:19:36 +0000374
Steve Naroff37b5ac22009-10-15 20:50:09 +0000375 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000376 ASTUnit *ATU = static_cast<ASTUnit *>(
377 clang_createTranslationUnit(CIdx, astTmpFile));
378 ATU->unlinkTemporaryFile();
379 return ATU;
Daniel Dunbara47dd192009-10-17 23:53:11 +0000380#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000381}
382
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000383void clang_disposeTranslationUnit(
384 CXTranslationUnit CTUnit)
385{
386 assert(CTUnit && "Passed null CXTranslationUnit");
387 delete static_cast<ASTUnit *>(CTUnit);
388}
389
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000390void clang_wantOnlyLocalDeclarations(CXIndex CIdx) {
391 static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true);
392}
393
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000394const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
395{
396 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000397 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
398 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000399}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000400
Steve Naroffc857ea42009-09-02 13:28:54 +0000401void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
402 CXTranslationUnitIterator callback,
403 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000404{
Steve Naroff50398192009-08-28 15:28:48 +0000405 assert(CTUnit && "Passed null CXTranslationUnit");
406 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
407 ASTContext &Ctx = CXXUnit->getASTContext();
408
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000409 TUVisitor DVisit(CTUnit, callback, CData,
410 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000411 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000412}
413
Steve Naroffc857ea42009-09-02 13:28:54 +0000414void clang_loadDeclaration(CXDecl Dcl,
415 CXDeclIterator callback,
416 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000417{
Steve Naroffc857ea42009-09-02 13:28:54 +0000418 assert(Dcl && "Passed null CXDecl");
419
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000420 CDeclVisitor DVisit(Dcl, callback, CData,
421 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000422 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000423}
424
Steve Naroff7e8f8182009-08-28 12:07:44 +0000425// Some notes on CXEntity:
426//
427// - Since the 'ordinary' namespace includes functions, data, typedefs,
428// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
429// entity for 2 different types). For example:
430//
431// module1.m: @interface Foo @end Foo *x;
432// module2.m: void Foo(int);
433//
434// - Since the unique name spans translation units, static data/functions
435// within a CXTranslationUnit are *not* currently represented by entities.
436// As a result, there will be no entity for the following:
437//
438// module.m: static void Foo() { }
439//
440
441
Steve Naroff600866c2009-08-27 19:51:58 +0000442const char *clang_getDeclarationName(CXEntity)
443{
444 return "";
445}
446const char *clang_getURI(CXEntity)
447{
448 return "";
449}
450
451CXEntity clang_getEntity(const char *URI)
452{
453 return 0;
454}
455
456//
457// CXDecl Operations.
458//
Steve Naroff600866c2009-08-27 19:51:58 +0000459CXEntity clang_getEntityFromDecl(CXDecl)
460{
461 return 0;
462}
Steve Naroff89922f82009-08-31 00:59:03 +0000463const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000464{
Steve Naroff89922f82009-08-31 00:59:03 +0000465 assert(AnonDecl && "Passed null CXDecl");
466 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000467
468 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
469 return OMD->getSelector().getAsString().c_str();
470 }
Steve Naroff89922f82009-08-31 00:59:03 +0000471 if (ND->getIdentifier())
472 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000473 else
Steve Naroff89922f82009-08-31 00:59:03 +0000474 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000475}
Steve Narofff334b4e2009-09-02 18:26:48 +0000476
Steve Naroff699a07d2009-09-25 21:32:34 +0000477unsigned clang_getDeclLine(CXDecl AnonDecl)
478{
479 assert(AnonDecl && "Passed null CXDecl");
480 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
481 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
482 return SourceMgr.getSpellingLineNumber(ND->getLocation());
483}
484
485unsigned clang_getDeclColumn(CXDecl AnonDecl)
486{
487 assert(AnonDecl && "Passed null CXDecl");
488 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
489 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000490 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000491}
492
Steve Naroffee9405e2009-09-25 21:45:39 +0000493const char *clang_getDeclSource(CXDecl AnonDecl)
494{
495 assert(AnonDecl && "Passed null CXDecl");
496 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
497 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
498 return SourceMgr.getBufferName(ND->getLocation());
499}
500
Steve Narofff334b4e2009-09-02 18:26:48 +0000501const char *clang_getCursorSpelling(CXCursor C)
502{
503 assert(C.decl && "CXCursor has null decl");
504 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
505
506 if (clang_isReference(C.kind)) {
507 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000508 case CXCursor_ObjCSuperClassRef:
509 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000510 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
511 assert(OID && "clang_getCursorLine(): Missing interface decl");
512 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000513 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000514 case CXCursor_ObjCClassRef:
515 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000516 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
517 return OID->getIdentifier()->getName();
518 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000519 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
520 assert(OID && "clang_getCursorLine(): Missing category decl");
521 return OID->getClassInterface()->getIdentifier()->getName();
522 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000523 case CXCursor_ObjCProtocolRef:
524 {
525 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
526 assert(OID && "clang_getCursorLine(): Missing protocol decl");
527 return OID->getIdentifier()->getName();
528 }
Steve Narofffb570422009-09-22 19:25:29 +0000529 case CXCursor_ObjCSelectorRef:
530 {
531 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
532 static_cast<Stmt *>(C.stmt));
533 assert(OME && "clang_getCursorLine(): Missing message expr");
534 return OME->getSelector().getAsString().c_str();
535 }
536 case CXCursor_VarRef:
537 case CXCursor_FunctionRef:
538 case CXCursor_EnumConstantRef:
539 {
540 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
541 static_cast<Stmt *>(C.stmt));
542 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
543 return DRE->getDecl()->getIdentifier()->getName();
544 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000545 default:
546 return "<not implemented>";
547 }
548 }
549 return clang_getDeclSpelling(C.decl);
550}
551
552const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000553{
Steve Naroff89922f82009-08-31 00:59:03 +0000554 switch (Kind) {
555 case CXCursor_FunctionDecl: return "FunctionDecl";
556 case CXCursor_TypedefDecl: return "TypedefDecl";
557 case CXCursor_EnumDecl: return "EnumDecl";
558 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000559 case CXCursor_StructDecl: return "StructDecl";
560 case CXCursor_UnionDecl: return "UnionDecl";
561 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000562 case CXCursor_FieldDecl: return "FieldDecl";
563 case CXCursor_VarDecl: return "VarDecl";
564 case CXCursor_ParmDecl: return "ParmDecl";
565 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
566 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
567 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
568 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
569 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000570 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
571 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
572 case CXCursor_FunctionDefn: return "FunctionDefn";
573 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
574 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
575 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
576 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000577 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000578 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000579 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000580 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
581
582 case CXCursor_VarRef: return "VarRef";
583 case CXCursor_FunctionRef: return "FunctionRef";
584 case CXCursor_EnumConstantRef: return "EnumConstantRef";
585 case CXCursor_MemberRef: return "MemberRef";
586
Steve Naroff77128dd2009-09-15 20:25:34 +0000587 case CXCursor_InvalidFile: return "InvalidFile";
588 case CXCursor_NoDeclFound: return "NoDeclFound";
589 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000590 default: return "<not implemented>";
591 }
Steve Naroff600866c2009-08-27 19:51:58 +0000592}
Steve Naroff89922f82009-08-31 00:59:03 +0000593
Steve Naroff9efa7672009-09-04 15:44:05 +0000594static enum CXCursorKind TranslateKind(Decl *D) {
595 switch (D->getKind()) {
596 case Decl::Function: return CXCursor_FunctionDecl;
597 case Decl::Typedef: return CXCursor_TypedefDecl;
598 case Decl::Enum: return CXCursor_EnumDecl;
599 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
600 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
601 case Decl::Field: return CXCursor_FieldDecl;
602 case Decl::Var: return CXCursor_VarDecl;
603 case Decl::ParmVar: return CXCursor_ParmDecl;
604 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000605 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
606 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000607 case Decl::ObjCMethod: {
608 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
609 if (MD->isInstanceMethod())
610 return CXCursor_ObjCInstanceMethodDecl;
611 return CXCursor_ObjCClassMethodDecl;
612 }
613 default: break;
614 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000615 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000616}
Steve Naroff600866c2009-08-27 19:51:58 +0000617//
618// CXCursor Operations.
619//
Steve Naroff9efa7672009-09-04 15:44:05 +0000620CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000621 unsigned line, unsigned column)
622{
Steve Naroff9efa7672009-09-04 15:44:05 +0000623 assert(CTUnit && "Passed null CXTranslationUnit");
624 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
625
626 FileManager &FMgr = CXXUnit->getFileManager();
627 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000628 source_name+strlen(source_name));
629 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000630 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000631 return C;
632 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000633 SourceLocation SLoc =
634 CXXUnit->getSourceManager().getLocation(File, line, column);
635
636 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
637
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000638 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000639 if (ALoc.isNamedRef())
640 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000641 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000642 if (Dcl) {
643 if (Stm) {
644 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
645 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
646 return C;
647 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
648 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
649 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000650 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000651 // Fall through...treat as a decl, not a ref.
652 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000653 if (ALoc.isNamedRef()) {
654 if (isa<ObjCInterfaceDecl>(Dcl)) {
655 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
656 return C;
657 }
658 if (isa<ObjCProtocolDecl>(Dcl)) {
659 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
660 return C;
661 }
662 }
663 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000664 return C;
665 }
Steve Narofffb570422009-09-22 19:25:29 +0000666 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000667 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000668}
669
Steve Naroff77128dd2009-09-15 20:25:34 +0000670CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
671{
672 assert(AnonDecl && "Passed null CXDecl");
673 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
674
Steve Narofffb570422009-09-22 19:25:29 +0000675 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000676 return C;
677}
678
679unsigned clang_isInvalid(enum CXCursorKind K)
680{
681 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
682}
683
Steve Naroff89922f82009-08-31 00:59:03 +0000684unsigned clang_isDeclaration(enum CXCursorKind K)
685{
686 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
687}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000688
Steve Narofff334b4e2009-09-02 18:26:48 +0000689unsigned clang_isReference(enum CXCursorKind K)
690{
691 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
692}
693
694unsigned clang_isDefinition(enum CXCursorKind K)
695{
696 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
697}
698
Steve Naroff9efa7672009-09-04 15:44:05 +0000699CXCursorKind clang_getCursorKind(CXCursor C)
700{
701 return C.kind;
702}
703
Steve Naroff699a07d2009-09-25 21:32:34 +0000704static Decl *getDeclFromExpr(Stmt *E) {
705 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
706 return RefExpr->getDecl();
707 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
708 return ME->getMemberDecl();
709 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
710 return RE->getDecl();
711
712 if (CallExpr *CE = dyn_cast<CallExpr>(E))
713 return getDeclFromExpr(CE->getCallee());
714 if (CastExpr *CE = dyn_cast<CastExpr>(E))
715 return getDeclFromExpr(CE->getSubExpr());
716 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
717 return OME->getMethodDecl();
718
719 return 0;
720}
721
Steve Naroff9efa7672009-09-04 15:44:05 +0000722CXDecl clang_getCursorDecl(CXCursor C)
723{
Steve Naroff699a07d2009-09-25 21:32:34 +0000724 if (clang_isDeclaration(C.kind))
725 return C.decl;
726
727 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000728 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000729 if (C.kind == CXCursor_ObjCClassRef ||
730 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000731 return static_cast<Stmt *>(C.stmt);
732 else
733 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
734 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000735 return C.decl;
736 }
737 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000738}
739
Steve Naroff85e2db72009-10-01 00:31:07 +0000740
Steve Narofff334b4e2009-09-02 18:26:48 +0000741static SourceLocation getLocationFromCursor(CXCursor C,
742 SourceManager &SourceMgr,
743 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000744 if (clang_isReference(C.kind)) {
745 switch (C.kind) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000746 case CXCursor_ObjCClassRef:
747 {
748 if (isa<ObjCInterfaceDecl>(ND)) {
749 // FIXME: This is a hack (storing the parent decl in the stmt slot).
750 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
751 return parentDecl->getLocation();
752 }
753 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
754 assert(OID && "clang_getCursorLine(): Missing category decl");
755 return OID->getClassInterface()->getLocation();
756 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000757 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000758 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000759 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
760 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000761 return OID->getSuperClassLoc();
762 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000763 case CXCursor_ObjCProtocolRef:
764 {
765 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
766 assert(OID && "clang_getCursorLine(): Missing protocol decl");
767 return OID->getLocation();
768 }
Steve Narofffb570422009-09-22 19:25:29 +0000769 case CXCursor_ObjCSelectorRef:
770 {
771 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
772 static_cast<Stmt *>(C.stmt));
773 assert(OME && "clang_getCursorLine(): Missing message expr");
774 return OME->getLeftLoc(); /* FIXME: should be a range */
775 }
776 case CXCursor_VarRef:
777 case CXCursor_FunctionRef:
778 case CXCursor_EnumConstantRef:
779 {
780 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
781 static_cast<Stmt *>(C.stmt));
782 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
783 return DRE->getLocation();
784 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000785 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000786 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000787 }
788 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000789 SourceLocation SLoc;
790 switch (ND->getKind()) {
791 case Decl::ObjCInterface:
792 {
793 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
794 break;
795 }
796 case Decl::ObjCProtocol:
797 {
798 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
799 break;
800 }
801 default:
802 {
803 SLoc = ND->getLocation();
804 break;
805 }
806 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000807 if (SLoc.isInvalid())
808 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000809 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000810 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000811}
812
Steve Naroff2d4d6292009-08-31 14:26:51 +0000813unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000814{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000815 assert(C.decl && "CXCursor has null decl");
816 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000817 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000818
819 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000820 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000821}
Steve Narofff334b4e2009-09-02 18:26:48 +0000822
Steve Naroff2d4d6292009-08-31 14:26:51 +0000823unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000824{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000825 assert(C.decl && "CXCursor has null decl");
826 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000827 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000828
829 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000830 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000831}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000832const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000833{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000834 assert(C.decl && "CXCursor has null decl");
835 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000836 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000837
838 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000839 if (SLoc.isFileID())
840 return SourceMgr.getBufferName(SLoc);
841
842 // Retrieve the file in which the macro was instantiated, then provide that
843 // buffer name.
844 // FIXME: Do we want to give specific macro-instantiation information?
845 const llvm::MemoryBuffer *Buffer
846 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
847 if (!Buffer)
848 return 0;
849
850 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000851}
852
Steve Naroff4ade6d62009-09-23 17:52:52 +0000853void clang_getDefinitionSpellingAndExtent(CXCursor C,
854 const char **startBuf,
855 const char **endBuf,
856 unsigned *startLine,
857 unsigned *startColumn,
858 unsigned *endLine,
859 unsigned *endColumn)
860{
861 assert(C.decl && "CXCursor has null decl");
862 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
863 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
864 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
865
866 SourceManager &SM = FD->getASTContext().getSourceManager();
867 *startBuf = SM.getCharacterData(Body->getLBracLoc());
868 *endBuf = SM.getCharacterData(Body->getRBracLoc());
869 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
870 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
871 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
872 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
873}
874
875
Steve Naroff600866c2009-08-27 19:51:58 +0000876} // end extern "C"