blob: 99f61540562356b52f96413e6199bdc19f2d82a8 [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"
Benjamin Kramer20d75812009-10-18 16:13:48 +000025#include "llvm/Config/config.h"
Douglas Gregor02465752009-10-16 21:24:31 +000026#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/System/Path.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000028#include "llvm/System/Program.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000029#include <cstdio>
Benjamin Kramer20d75812009-10-18 16:13:48 +000030#ifdef LLVM_ON_WIN32
31#define WIN32_LEAN_AND_MEAN
32#include <windows.h>
33#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +000034#include <dlfcn.h>
Daniel Dunbara47dd192009-10-17 23:53:11 +000035#endif
Ted Kremenek74cd0692009-10-15 23:21:22 +000036#include <vector>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000037
Steve Naroff50398192009-08-28 15:28:48 +000038using namespace clang;
39using namespace idx;
40
Steve Naroff89922f82009-08-31 00:59:03 +000041namespace {
42
Steve Naroff4ade6d62009-09-23 17:52:52 +000043static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
44{
45 NamedDecl *D = DRE->getDecl();
46 if (isa<VarDecl>(D))
47 return CXCursor_VarRef;
48 else if (isa<FunctionDecl>(D))
49 return CXCursor_FunctionRef;
50 else if (isa<EnumConstantDecl>(D))
51 return CXCursor_EnumConstantRef;
52 else
53 return CXCursor_NotImplemented;
54}
55
56#if 0
57// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000058class CRefVisitor : public StmtVisitor<CRefVisitor> {
59 CXDecl CDecl;
60 CXDeclIterator Callback;
61 CXClientData CData;
62
63 void Call(enum CXCursorKind CK, Stmt *SRef) {
64 CXCursor C = { CK, CDecl, SRef };
65 Callback(CDecl, C, CData);
66 }
67
68public:
69 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
70 CDecl(C), Callback(cback), CData(D) {}
71
72 void VisitStmt(Stmt *S) {
73 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
74 C != CEnd; ++C)
75 Visit(*C);
76 }
77 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000078 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000079 }
80 void VisitMemberExpr(MemberExpr *Node) {
81 Call(CXCursor_MemberRef, Node);
82 }
83 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
84 Call(CXCursor_ObjCSelectorRef, Node);
85 }
86 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
87 Call(CXCursor_ObjCIvarRef, Node);
88 }
89};
Steve Naroff4ade6d62009-09-23 17:52:52 +000090#endif
Steve Narofffb570422009-09-22 19:25:29 +000091
Steve Naroff89922f82009-08-31 00:59:03 +000092// Translation Unit Visitor.
93class TUVisitor : public DeclVisitor<TUVisitor> {
94 CXTranslationUnit TUnit;
95 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000096 CXClientData CData;
97
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000098 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
99 // to the visitor. Declarations with a PCH level greater than this value will
100 // be suppressed.
101 unsigned MaxPCHLevel;
102
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000103 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000104 // Filter any declarations that have a PCH level greater than what we allow.
105 if (ND->getPCHLevel() > MaxPCHLevel)
106 return;
107
Steve Narofffb570422009-09-22 19:25:29 +0000108 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000109 Callback(TUnit, C, CData);
110 }
Steve Naroff89922f82009-08-31 00:59:03 +0000111public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000112 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000113 CXTranslationUnitIterator cback, CXClientData D,
114 unsigned MaxPCHLevel) :
115 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000116
117 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
118 VisitDeclContext(dyn_cast<DeclContext>(D));
119 }
120 void VisitDeclContext(DeclContext *DC) {
121 for (DeclContext::decl_iterator
122 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
123 Visit(*I);
124 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000125 void VisitTypedefDecl(TypedefDecl *ND) {
126 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000127 }
128 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000129 switch (ND->getTagKind()) {
130 case TagDecl::TK_struct:
131 Call(CXCursor_StructDecl, ND);
132 break;
133 case TagDecl::TK_class:
134 Call(CXCursor_ClassDecl, ND);
135 break;
136 case TagDecl::TK_union:
137 Call(CXCursor_UnionDecl, ND);
138 break;
139 case TagDecl::TK_enum:
140 Call(CXCursor_EnumDecl, ND);
141 break;
142 }
Steve Naroff89922f82009-08-31 00:59:03 +0000143 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000144 void VisitVarDecl(VarDecl *ND) {
145 Call(CXCursor_VarDecl, ND);
146 }
Steve Naroff89922f82009-08-31 00:59:03 +0000147 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000148 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
149 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000150 }
151 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000152 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000153 }
154 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000155 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000156 }
157 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000158 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000159 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000160 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
161 Call(CXCursor_ObjCClassDefn, ND);
162 }
163 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
164 Call(CXCursor_ObjCCategoryDefn, ND);
165 }
Steve Naroff89922f82009-08-31 00:59:03 +0000166};
167
Steve Naroffc857ea42009-09-02 13:28:54 +0000168// Declaration visitor.
169class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
170 CXDecl CDecl;
171 CXDeclIterator Callback;
172 CXClientData CData;
173
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000174 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
175 // to the visitor. Declarations with a PCH level greater than this value will
176 // be suppressed.
177 unsigned MaxPCHLevel;
178
Steve Naroffc857ea42009-09-02 13:28:54 +0000179 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000180 // Disable the callback when the context is equal to the visiting decl.
181 if (CDecl == ND && !clang_isReference(CK))
182 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000183
184 // Filter any declarations that have a PCH level greater than what we allow.
185 if (ND->getPCHLevel() > MaxPCHLevel)
186 return;
187
Steve Narofffb570422009-09-22 19:25:29 +0000188 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000189 Callback(CDecl, C, CData);
190 }
Steve Naroff89922f82009-08-31 00:59:03 +0000191public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000192 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
193 unsigned MaxPCHLevel) :
194 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000195
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000196 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
197 // Issue callbacks for the containing class.
198 Call(CXCursor_ObjCClassRef, ND);
199 // FIXME: Issue callbacks for protocol refs.
200 VisitDeclContext(dyn_cast<DeclContext>(ND));
201 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000202 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000203 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000204 if (D->getSuperClass())
205 Call(CXCursor_ObjCSuperClassRef, D);
206
Steve Naroff9efa7672009-09-04 15:44:05 +0000207 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
208 E = D->protocol_end(); I != E; ++I)
209 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000210 VisitDeclContext(dyn_cast<DeclContext>(D));
211 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000212 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
213 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
214 E = PID->protocol_end(); I != E; ++I)
215 Call(CXCursor_ObjCProtocolRef, *I);
216
217 VisitDeclContext(dyn_cast<DeclContext>(PID));
218 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000219 void VisitTagDecl(TagDecl *D) {
220 VisitDeclContext(dyn_cast<DeclContext>(D));
221 }
222 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
223 VisitDeclContext(dyn_cast<DeclContext>(D));
224 }
225 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
226 VisitDeclContext(dyn_cast<DeclContext>(D));
227 }
Steve Naroff89922f82009-08-31 00:59:03 +0000228 void VisitDeclContext(DeclContext *DC) {
229 for (DeclContext::decl_iterator
230 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
231 Visit(*I);
232 }
233 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000234 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000235 }
236 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000237 Call(CXCursor_FieldDecl, ND);
238 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000239 void VisitVarDecl(VarDecl *ND) {
240 Call(CXCursor_VarDecl, ND);
241 }
242 void VisitParmVarDecl(ParmVarDecl *ND) {
243 Call(CXCursor_ParmDecl, ND);
244 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000245 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
246 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000247 }
248 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000249 Call(CXCursor_ObjCIvarDecl, ND);
250 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000251 void VisitFunctionDecl(FunctionDecl *ND) {
252 if (ND->isThisDeclarationADefinition()) {
253 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000254#if 0
255 // Not currently needed.
256 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000257 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000258 RVisit.Visit(Body);
259#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000260 }
261 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000262 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
263 if (ND->getBody()) {
264 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
265 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000266 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000267 } else
268 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
269 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000270 }
271};
272
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000273class CIndexer : public Indexer {
274public:
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000275 explicit CIndexer(Program *prog) : Indexer(*prog), OnlyLocalDecls(false) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000276
277 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000278
279 /// \brief Whether we only want to see "local" declarations (that did not
280 /// come from a previous precompiled header). If false, we want to see all
281 /// declarations.
282 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
283 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000284
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000285 /// \brief Get the path of the clang binary.
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000286 const llvm::sys::Path& getClangPath();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000287private:
288 bool OnlyLocalDecls;
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000289 llvm::sys::Path ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000290};
Steve Naroff89922f82009-08-31 00:59:03 +0000291
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000292const llvm::sys::Path& CIndexer::getClangPath() {
293 // Did we already compute the path?
294 if (!ClangPath.empty())
295 return ClangPath;
296
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000297 // Find the location where this library lives (libCIndex.dylib).
Benjamin Kramer20d75812009-10-18 16:13:48 +0000298#ifdef LLVM_ON_WIN32
299 MEMORY_BASIC_INFORMATION mbi;
300 char path[MAX_PATH];
Benjamin Krameredcd8282009-10-18 16:20:58 +0000301 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
Benjamin Kramer20d75812009-10-18 16:13:48 +0000302 sizeof(mbi));
303 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
304
305 llvm::sys::Path CIndexPath(path);
306#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000307 // This silly cast below avoids a C++ warning.
308 Dl_info info;
309 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
310 assert(0 && "Call to dladdr() failed");
311
312 llvm::sys::Path CIndexPath(info.dli_fname);
Daniel Dunbara47dd192009-10-17 23:53:11 +0000313#endif
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000314
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000315 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000316 CIndexPath.eraseComponent();
317 CIndexPath.eraseComponent();
318 CIndexPath.appendComponent("bin");
319 CIndexPath.appendComponent("clang");
320
321 // Cache our result.
322 ClangPath = CIndexPath;
323 return ClangPath;
324}
325
326}
327
328extern "C" {
329
330CXIndex clang_createIndex()
331{
332 return new CIndexer(new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000333}
334
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000335void clang_disposeIndex(CXIndex CIdx)
336{
337 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000338 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000339}
340
Steve Naroff50398192009-08-28 15:28:48 +0000341// FIXME: need to pass back error info.
342CXTranslationUnit clang_createTranslationUnit(
343 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000344{
Steve Naroff50398192009-08-28 15:28:48 +0000345 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000346 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000347 std::string astName(ast_filename);
348 std::string ErrMsg;
349
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000350 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000351 CXXIdx->getFileManager(), &ErrMsg,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000352 CXXIdx->getOnlyLocalDecls(),
353 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000354}
355
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000356CXTranslationUnit clang_createTranslationUnitFromSourceFile(
357 CXIndex CIdx,
358 const char *source_filename,
359 int num_command_line_args, const char **command_line_args)
360{
Ted Kremenek74cd0692009-10-15 23:21:22 +0000361 // Build up the arguments for involing clang.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000362 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Ted Kremenek74cd0692009-10-15 23:21:22 +0000363 std::vector<const char *> argv;
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000364 argv.push_back(ClangPath.c_str());
Ted Kremenek74cd0692009-10-15 23:21:22 +0000365 argv.push_back("-emit-ast");
366 argv.push_back(source_filename);
367 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000368 // Generate a temporary name for the AST file.
369 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000370 argv.push_back(tmpnam(astTmpFile));
Benjamin Kramerb14346b2009-10-18 16:52:07 +0000371 for (int i = 0; i < num_command_line_args; i++)
Ted Kremenek74cd0692009-10-15 23:21:22 +0000372 argv.push_back(command_line_args[i]);
373 argv.push_back(NULL);
374
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000375 // Generate the AST file in a separate process.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000376 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0]);
Benjamin Kramer0829a832009-10-18 11:19:36 +0000377
Steve Naroff37b5ac22009-10-15 20:50:09 +0000378 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000379 ASTUnit *ATU = static_cast<ASTUnit *>(
380 clang_createTranslationUnit(CIdx, astTmpFile));
381 ATU->unlinkTemporaryFile();
382 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000383}
384
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000385void clang_disposeTranslationUnit(
386 CXTranslationUnit CTUnit)
387{
388 assert(CTUnit && "Passed null CXTranslationUnit");
389 delete static_cast<ASTUnit *>(CTUnit);
390}
391
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000392void clang_wantOnlyLocalDeclarations(CXIndex CIdx) {
393 static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true);
394}
395
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000396const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
397{
398 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000399 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
400 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000401}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000402
Steve Naroffc857ea42009-09-02 13:28:54 +0000403void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
404 CXTranslationUnitIterator callback,
405 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000406{
Steve Naroff50398192009-08-28 15:28:48 +0000407 assert(CTUnit && "Passed null CXTranslationUnit");
408 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
409 ASTContext &Ctx = CXXUnit->getASTContext();
410
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000411 TUVisitor DVisit(CTUnit, callback, CData,
412 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000413 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000414}
415
Steve Naroffc857ea42009-09-02 13:28:54 +0000416void clang_loadDeclaration(CXDecl Dcl,
417 CXDeclIterator callback,
418 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000419{
Steve Naroffc857ea42009-09-02 13:28:54 +0000420 assert(Dcl && "Passed null CXDecl");
421
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000422 CDeclVisitor DVisit(Dcl, callback, CData,
423 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000424 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000425}
426
Steve Naroff7e8f8182009-08-28 12:07:44 +0000427// Some notes on CXEntity:
428//
429// - Since the 'ordinary' namespace includes functions, data, typedefs,
430// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
431// entity for 2 different types). For example:
432//
433// module1.m: @interface Foo @end Foo *x;
434// module2.m: void Foo(int);
435//
436// - Since the unique name spans translation units, static data/functions
437// within a CXTranslationUnit are *not* currently represented by entities.
438// As a result, there will be no entity for the following:
439//
440// module.m: static void Foo() { }
441//
442
443
Steve Naroff600866c2009-08-27 19:51:58 +0000444const char *clang_getDeclarationName(CXEntity)
445{
446 return "";
447}
448const char *clang_getURI(CXEntity)
449{
450 return "";
451}
452
453CXEntity clang_getEntity(const char *URI)
454{
455 return 0;
456}
457
458//
459// CXDecl Operations.
460//
Steve Naroff600866c2009-08-27 19:51:58 +0000461CXEntity clang_getEntityFromDecl(CXDecl)
462{
463 return 0;
464}
Steve Naroff89922f82009-08-31 00:59:03 +0000465const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000466{
Steve Naroff89922f82009-08-31 00:59:03 +0000467 assert(AnonDecl && "Passed null CXDecl");
468 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000469
470 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
471 return OMD->getSelector().getAsString().c_str();
472 }
Steve Naroff89922f82009-08-31 00:59:03 +0000473 if (ND->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000474 return ND->getIdentifier()->getNameStart();
Steve Naroffc857ea42009-09-02 13:28:54 +0000475 else
Steve Naroff89922f82009-08-31 00:59:03 +0000476 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000477}
Steve Narofff334b4e2009-09-02 18:26:48 +0000478
Steve Naroff699a07d2009-09-25 21:32:34 +0000479unsigned clang_getDeclLine(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.getSpellingLineNumber(ND->getLocation());
485}
486
487unsigned clang_getDeclColumn(CXDecl AnonDecl)
488{
489 assert(AnonDecl && "Passed null CXDecl");
490 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
491 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000492 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000493}
494
Steve Naroffee9405e2009-09-25 21:45:39 +0000495const char *clang_getDeclSource(CXDecl AnonDecl)
496{
497 assert(AnonDecl && "Passed null CXDecl");
498 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
499 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
500 return SourceMgr.getBufferName(ND->getLocation());
501}
502
Steve Narofff334b4e2009-09-02 18:26:48 +0000503const char *clang_getCursorSpelling(CXCursor C)
504{
505 assert(C.decl && "CXCursor has null decl");
506 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
507
508 if (clang_isReference(C.kind)) {
509 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000510 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000511 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
512 assert(OID && "clang_getCursorLine(): Missing interface decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000513 return OID->getSuperClass()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000514 }
515 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000516 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000517 return OID->getIdentifier()->getNameStart();
Steve Naroff85e2db72009-10-01 00:31:07 +0000518 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000519 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
520 assert(OID && "clang_getCursorLine(): Missing category decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000521 return OID->getClassInterface()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000522 }
523 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000524 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
525 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000526 return OID->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000527 }
528 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000529 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
530 static_cast<Stmt *>(C.stmt));
531 assert(OME && "clang_getCursorLine(): Missing message expr");
532 return OME->getSelector().getAsString().c_str();
Steve Naroffbade7de2009-10-19 13:41:39 +0000533 }
Steve Narofffb570422009-09-22 19:25:29 +0000534 case CXCursor_VarRef:
535 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000536 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000537 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
538 static_cast<Stmt *>(C.stmt));
539 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Daniel Dunbare013d682009-10-18 20:26:12 +0000540 return DRE->getDecl()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000541 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000542 default:
543 return "<not implemented>";
544 }
545 }
546 return clang_getDeclSpelling(C.decl);
547}
548
549const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000550{
Steve Naroff89922f82009-08-31 00:59:03 +0000551 switch (Kind) {
552 case CXCursor_FunctionDecl: return "FunctionDecl";
553 case CXCursor_TypedefDecl: return "TypedefDecl";
554 case CXCursor_EnumDecl: return "EnumDecl";
555 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000556 case CXCursor_StructDecl: return "StructDecl";
557 case CXCursor_UnionDecl: return "UnionDecl";
558 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000559 case CXCursor_FieldDecl: return "FieldDecl";
560 case CXCursor_VarDecl: return "VarDecl";
561 case CXCursor_ParmDecl: return "ParmDecl";
562 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
563 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
564 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
565 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
566 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000567 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
568 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
569 case CXCursor_FunctionDefn: return "FunctionDefn";
570 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
571 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
572 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
573 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000574 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000575 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000576 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000577 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
578
579 case CXCursor_VarRef: return "VarRef";
580 case CXCursor_FunctionRef: return "FunctionRef";
581 case CXCursor_EnumConstantRef: return "EnumConstantRef";
582 case CXCursor_MemberRef: return "MemberRef";
583
Steve Naroff77128dd2009-09-15 20:25:34 +0000584 case CXCursor_InvalidFile: return "InvalidFile";
585 case CXCursor_NoDeclFound: return "NoDeclFound";
586 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000587 default: return "<not implemented>";
588 }
Steve Naroff600866c2009-08-27 19:51:58 +0000589}
Steve Naroff89922f82009-08-31 00:59:03 +0000590
Steve Naroff9efa7672009-09-04 15:44:05 +0000591static enum CXCursorKind TranslateKind(Decl *D) {
592 switch (D->getKind()) {
593 case Decl::Function: return CXCursor_FunctionDecl;
594 case Decl::Typedef: return CXCursor_TypedefDecl;
595 case Decl::Enum: return CXCursor_EnumDecl;
596 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
597 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
598 case Decl::Field: return CXCursor_FieldDecl;
599 case Decl::Var: return CXCursor_VarDecl;
600 case Decl::ParmVar: return CXCursor_ParmDecl;
601 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000602 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
603 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000604 case Decl::ObjCMethod: {
605 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
606 if (MD->isInstanceMethod())
607 return CXCursor_ObjCInstanceMethodDecl;
608 return CXCursor_ObjCClassMethodDecl;
609 }
610 default: break;
611 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000612 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000613}
Steve Naroff600866c2009-08-27 19:51:58 +0000614//
615// CXCursor Operations.
616//
Steve Naroff9efa7672009-09-04 15:44:05 +0000617CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000618 unsigned line, unsigned column)
619{
Steve Naroff9efa7672009-09-04 15:44:05 +0000620 assert(CTUnit && "Passed null CXTranslationUnit");
621 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
622
623 FileManager &FMgr = CXXUnit->getFileManager();
624 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000625 source_name+strlen(source_name));
626 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000627 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000628 return C;
629 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000630 SourceLocation SLoc =
631 CXXUnit->getSourceManager().getLocation(File, line, column);
632
633 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
634
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000635 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000636 if (ALoc.isNamedRef())
637 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000638 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000639 if (Dcl) {
640 if (Stm) {
641 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
642 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
643 return C;
644 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
645 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
646 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000647 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000648 // Fall through...treat as a decl, not a ref.
649 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000650 if (ALoc.isNamedRef()) {
651 if (isa<ObjCInterfaceDecl>(Dcl)) {
652 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
653 return C;
654 }
655 if (isa<ObjCProtocolDecl>(Dcl)) {
656 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
657 return C;
658 }
659 }
660 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000661 return C;
662 }
Steve Narofffb570422009-09-22 19:25:29 +0000663 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000664 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000665}
666
Steve Naroff77128dd2009-09-15 20:25:34 +0000667CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
668{
669 assert(AnonDecl && "Passed null CXDecl");
670 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
671
Steve Narofffb570422009-09-22 19:25:29 +0000672 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000673 return C;
674}
675
676unsigned clang_isInvalid(enum CXCursorKind K)
677{
678 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
679}
680
Steve Naroff89922f82009-08-31 00:59:03 +0000681unsigned clang_isDeclaration(enum CXCursorKind K)
682{
683 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
684}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000685
Steve Narofff334b4e2009-09-02 18:26:48 +0000686unsigned clang_isReference(enum CXCursorKind K)
687{
688 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
689}
690
691unsigned clang_isDefinition(enum CXCursorKind K)
692{
693 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
694}
695
Steve Naroff9efa7672009-09-04 15:44:05 +0000696CXCursorKind clang_getCursorKind(CXCursor C)
697{
698 return C.kind;
699}
700
Steve Naroff699a07d2009-09-25 21:32:34 +0000701static Decl *getDeclFromExpr(Stmt *E) {
702 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
703 return RefExpr->getDecl();
704 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
705 return ME->getMemberDecl();
706 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
707 return RE->getDecl();
708
709 if (CallExpr *CE = dyn_cast<CallExpr>(E))
710 return getDeclFromExpr(CE->getCallee());
711 if (CastExpr *CE = dyn_cast<CastExpr>(E))
712 return getDeclFromExpr(CE->getSubExpr());
713 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
714 return OME->getMethodDecl();
715
716 return 0;
717}
718
Steve Naroff9efa7672009-09-04 15:44:05 +0000719CXDecl clang_getCursorDecl(CXCursor C)
720{
Steve Naroff699a07d2009-09-25 21:32:34 +0000721 if (clang_isDeclaration(C.kind))
722 return C.decl;
723
724 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000725 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000726 if (C.kind == CXCursor_ObjCClassRef ||
727 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000728 return static_cast<Stmt *>(C.stmt);
729 else
730 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
731 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000732 return C.decl;
733 }
734 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000735}
736
Steve Naroff85e2db72009-10-01 00:31:07 +0000737
Steve Narofff334b4e2009-09-02 18:26:48 +0000738static SourceLocation getLocationFromCursor(CXCursor C,
739 SourceManager &SourceMgr,
740 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000741 if (clang_isReference(C.kind)) {
742 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000743 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000744 if (isa<ObjCInterfaceDecl>(ND)) {
745 // FIXME: This is a hack (storing the parent decl in the stmt slot).
746 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
747 return parentDecl->getLocation();
748 }
749 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
750 assert(OID && "clang_getCursorLine(): Missing category decl");
751 return OID->getClassInterface()->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000752 }
753 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000754 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
755 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000756 return OID->getSuperClassLoc();
Steve Naroffbade7de2009-10-19 13:41:39 +0000757 }
758 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000759 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
760 assert(OID && "clang_getCursorLine(): Missing protocol decl");
761 return OID->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000762 }
763 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000764 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
765 static_cast<Stmt *>(C.stmt));
766 assert(OME && "clang_getCursorLine(): Missing message expr");
767 return OME->getLeftLoc(); /* FIXME: should be a range */
Steve Naroffbade7de2009-10-19 13:41:39 +0000768 }
Steve Narofffb570422009-09-22 19:25:29 +0000769 case CXCursor_VarRef:
770 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000771 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000772 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
773 static_cast<Stmt *>(C.stmt));
774 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
775 return DRE->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000776 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000777 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000778 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000779 }
780 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000781 SourceLocation SLoc;
782 switch (ND->getKind()) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000783 case Decl::ObjCInterface: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000784 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
785 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000786 }
787 case Decl::ObjCProtocol: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000788 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
789 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000790 }
791 default: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000792 SLoc = ND->getLocation();
793 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000794 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000795 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000796 if (SLoc.isInvalid())
797 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000798 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000799 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000800}
801
Steve Naroff2d4d6292009-08-31 14:26:51 +0000802unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000803{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000804 assert(C.decl && "CXCursor has null decl");
805 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000806 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000807
808 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000809 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000810}
Steve Narofff334b4e2009-09-02 18:26:48 +0000811
Steve Naroff2d4d6292009-08-31 14:26:51 +0000812unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000813{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000814 assert(C.decl && "CXCursor has null decl");
815 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000816 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000817
818 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000819 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000820}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000821const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000822{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000823 assert(C.decl && "CXCursor has null decl");
824 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000825 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000826
827 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000828 if (SLoc.isFileID())
829 return SourceMgr.getBufferName(SLoc);
830
831 // Retrieve the file in which the macro was instantiated, then provide that
832 // buffer name.
833 // FIXME: Do we want to give specific macro-instantiation information?
834 const llvm::MemoryBuffer *Buffer
835 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
836 if (!Buffer)
837 return 0;
838
839 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000840}
841
Steve Naroff4ade6d62009-09-23 17:52:52 +0000842void clang_getDefinitionSpellingAndExtent(CXCursor C,
843 const char **startBuf,
844 const char **endBuf,
845 unsigned *startLine,
846 unsigned *startColumn,
847 unsigned *endLine,
848 unsigned *endColumn)
849{
850 assert(C.decl && "CXCursor has null decl");
851 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
852 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
853 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
854
855 SourceManager &SM = FD->getASTContext().getSourceManager();
856 *startBuf = SM.getCharacterData(Body->getLBracLoc());
857 *endBuf = SM.getCharacterData(Body->getRBracLoc());
858 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
859 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
860 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
861 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
862}
863
864
Steve Naroff600866c2009-08-27 19:51:58 +0000865} // end extern "C"