blob: 6015ffdd4ca73b7f49bf976caf61c679777de655 [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Clang-C Source Indexing library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang-c/Index.h"
Steve Naroff50398192009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroff9efa7672009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Steve Naroff50398192009-08-28 15:28:48 +000019#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000020#include "clang/AST/StmtVisitor.h"
Steve Naroffc857ea42009-09-02 13:28:54 +000021#include "clang/AST/Decl.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000022#include "clang/Basic/FileManager.h"
Steve Naroff2d4d6292009-08-31 14:26:51 +000023#include "clang/Basic/SourceManager.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000024#include "clang/Frontend/ASTUnit.h"
Douglas Gregor02465752009-10-16 21:24:31 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/System/Path.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000027#include <cstdio>
Daniel Dunbara47dd192009-10-17 23:53:11 +000028#ifndef _MSC_VER
Steve Naroff5b7d8e22009-10-15 20:04:39 +000029#include <dlfcn.h>
Ted Kremenek8c4195e2009-10-15 22:10:56 +000030#include <sys/wait.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:
Ted Kremenekdff76892009-10-17 06:21:47 +0000271 explicit CIndexer(Program *prog) : Indexer(*prog), OnlyLocalDecls(false) {}
272
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; }
280
281private:
282 bool OnlyLocalDecls;
283};
284
Steve Naroff89922f82009-08-31 00:59:03 +0000285}
286
Steve Naroff600866c2009-08-27 19:51:58 +0000287extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000288
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000289static const char *clangPath;
290
Steve Naroff600866c2009-08-27 19:51:58 +0000291CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000292{
Daniel Dunbara47dd192009-10-17 23:53:11 +0000293 // FIXME: This is a hack to unbreak the MSVC build.
294#ifdef _MSC_VER
295 llvm::sys::Path CIndexPath("");
296#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000297 // Find the location where this library lives (libCIndex.dylib).
298 // We do the lookup here to avoid poking dladdr too many times.
299 // This silly cast below avoids a C++ warning.
300 Dl_info info;
301 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
302 assert(0 && "Call to dladdr() failed");
303
304 llvm::sys::Path CIndexPath(info.dli_fname);
Daniel Dunbara47dd192009-10-17 23:53:11 +0000305#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000306 std::string CIndexDir = CIndexPath.getDirname();
307
308 // We now have the CIndex directory, locate clang relative to it.
309 std::string ClangPath = CIndexDir + "/../bin/clang";
310
311 clangPath = ClangPath.c_str();
312
Ted Kremenekdff76892009-10-17 06:21:47 +0000313 return new CIndexer(new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000314}
315
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000316void clang_disposeIndex(CXIndex CIdx)
317{
318 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000319 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000320}
321
Steve Naroff50398192009-08-28 15:28:48 +0000322// FIXME: need to pass back error info.
323CXTranslationUnit clang_createTranslationUnit(
324 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000325{
Steve Naroff50398192009-08-28 15:28:48 +0000326 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000327 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000328 std::string astName(ast_filename);
329 std::string ErrMsg;
330
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000331 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000332 CXXIdx->getFileManager(), &ErrMsg,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000333 CXXIdx->getOnlyLocalDecls(),
334 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000335}
336
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000337CXTranslationUnit clang_createTranslationUnitFromSourceFile(
338 CXIndex CIdx,
339 const char *source_filename,
340 int num_command_line_args, const char **command_line_args)
341{
Daniel Dunbara47dd192009-10-17 23:53:11 +0000342 // FIXME: This is a hack to unbreak the build.
343#ifdef _MSC_VER
344 return 0;
345#else
Ted Kremenek74cd0692009-10-15 23:21:22 +0000346 // Build up the arguments for involing clang.
347 std::vector<const char *> argv;
348 argv.push_back(clangPath);
349 argv.push_back("-emit-ast");
350 argv.push_back(source_filename);
351 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000352 // Generate a temporary name for the AST file.
353 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000354 argv.push_back(tmpnam(astTmpFile));
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000355 for (int i = num_command_line_args; i < num_command_line_args; i++)
Ted Kremenek74cd0692009-10-15 23:21:22 +0000356 argv.push_back(command_line_args[i]);
357 argv.push_back(NULL);
358
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000359 // Generate the AST file in a separate process.
360 pid_t child_pid = fork();
361 if (child_pid == 0) { // Child process
362
363 // Execute the command, passing the appropriate arguments.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000364 execv(argv[0], (char *const *)&argv[0]);
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000365
366 // If execv returns, it failed.
367 assert(0 && "execv() failed");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000368 }
369 // This is run by the parent.
370 int child_status;
371 pid_t tpid;
372 do { // Wait for the child to terminate.
373 tpid = wait(&child_status);
374 } while (tpid != child_pid);
375
376 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000377 ASTUnit *ATU = static_cast<ASTUnit *>(
378 clang_createTranslationUnit(CIdx, astTmpFile));
379 ATU->unlinkTemporaryFile();
380 return ATU;
Daniel Dunbara47dd192009-10-17 23:53:11 +0000381#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000382}
383
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000384void clang_disposeTranslationUnit(
385 CXTranslationUnit CTUnit)
386{
387 assert(CTUnit && "Passed null CXTranslationUnit");
388 delete static_cast<ASTUnit *>(CTUnit);
389}
390
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000391void clang_wantOnlyLocalDeclarations(CXIndex CIdx) {
392 static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true);
393}
394
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000395const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
396{
397 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000398 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
399 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000400}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000401
Steve Naroffc857ea42009-09-02 13:28:54 +0000402void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
403 CXTranslationUnitIterator callback,
404 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000405{
Steve Naroff50398192009-08-28 15:28:48 +0000406 assert(CTUnit && "Passed null CXTranslationUnit");
407 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
408 ASTContext &Ctx = CXXUnit->getASTContext();
409
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000410 TUVisitor DVisit(CTUnit, callback, CData,
411 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000412 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000413}
414
Steve Naroffc857ea42009-09-02 13:28:54 +0000415void clang_loadDeclaration(CXDecl Dcl,
416 CXDeclIterator callback,
417 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000418{
Steve Naroffc857ea42009-09-02 13:28:54 +0000419 assert(Dcl && "Passed null CXDecl");
420
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000421 CDeclVisitor DVisit(Dcl, callback, CData,
422 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000423 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000424}
425
Steve Naroff7e8f8182009-08-28 12:07:44 +0000426// Some notes on CXEntity:
427//
428// - Since the 'ordinary' namespace includes functions, data, typedefs,
429// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
430// entity for 2 different types). For example:
431//
432// module1.m: @interface Foo @end Foo *x;
433// module2.m: void Foo(int);
434//
435// - Since the unique name spans translation units, static data/functions
436// within a CXTranslationUnit are *not* currently represented by entities.
437// As a result, there will be no entity for the following:
438//
439// module.m: static void Foo() { }
440//
441
442
Steve Naroff600866c2009-08-27 19:51:58 +0000443const char *clang_getDeclarationName(CXEntity)
444{
445 return "";
446}
447const char *clang_getURI(CXEntity)
448{
449 return "";
450}
451
452CXEntity clang_getEntity(const char *URI)
453{
454 return 0;
455}
456
457//
458// CXDecl Operations.
459//
Steve Naroff600866c2009-08-27 19:51:58 +0000460CXEntity clang_getEntityFromDecl(CXDecl)
461{
462 return 0;
463}
Steve Naroff89922f82009-08-31 00:59:03 +0000464const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000465{
Steve Naroff89922f82009-08-31 00:59:03 +0000466 assert(AnonDecl && "Passed null CXDecl");
467 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000468
469 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
470 return OMD->getSelector().getAsString().c_str();
471 }
Steve Naroff89922f82009-08-31 00:59:03 +0000472 if (ND->getIdentifier())
473 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000474 else
Steve Naroff89922f82009-08-31 00:59:03 +0000475 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000476}
Steve Narofff334b4e2009-09-02 18:26:48 +0000477
Steve Naroff699a07d2009-09-25 21:32:34 +0000478unsigned clang_getDeclLine(CXDecl AnonDecl)
479{
480 assert(AnonDecl && "Passed null CXDecl");
481 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
482 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
483 return SourceMgr.getSpellingLineNumber(ND->getLocation());
484}
485
486unsigned clang_getDeclColumn(CXDecl AnonDecl)
487{
488 assert(AnonDecl && "Passed null CXDecl");
489 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
490 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000491 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000492}
493
Steve Naroffee9405e2009-09-25 21:45:39 +0000494const char *clang_getDeclSource(CXDecl AnonDecl)
495{
496 assert(AnonDecl && "Passed null CXDecl");
497 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
498 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
499 return SourceMgr.getBufferName(ND->getLocation());
500}
501
Steve Narofff334b4e2009-09-02 18:26:48 +0000502const char *clang_getCursorSpelling(CXCursor C)
503{
504 assert(C.decl && "CXCursor has null decl");
505 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
506
507 if (clang_isReference(C.kind)) {
508 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000509 case CXCursor_ObjCSuperClassRef:
510 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000511 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
512 assert(OID && "clang_getCursorLine(): Missing interface decl");
513 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000514 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000515 case CXCursor_ObjCClassRef:
516 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000517 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
518 return OID->getIdentifier()->getName();
519 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000520 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
521 assert(OID && "clang_getCursorLine(): Missing category decl");
522 return OID->getClassInterface()->getIdentifier()->getName();
523 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000524 case CXCursor_ObjCProtocolRef:
525 {
526 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
527 assert(OID && "clang_getCursorLine(): Missing protocol decl");
528 return OID->getIdentifier()->getName();
529 }
Steve Narofffb570422009-09-22 19:25:29 +0000530 case CXCursor_ObjCSelectorRef:
531 {
532 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
533 static_cast<Stmt *>(C.stmt));
534 assert(OME && "clang_getCursorLine(): Missing message expr");
535 return OME->getSelector().getAsString().c_str();
536 }
537 case CXCursor_VarRef:
538 case CXCursor_FunctionRef:
539 case CXCursor_EnumConstantRef:
540 {
541 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
542 static_cast<Stmt *>(C.stmt));
543 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
544 return DRE->getDecl()->getIdentifier()->getName();
545 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000546 default:
547 return "<not implemented>";
548 }
549 }
550 return clang_getDeclSpelling(C.decl);
551}
552
553const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000554{
Steve Naroff89922f82009-08-31 00:59:03 +0000555 switch (Kind) {
556 case CXCursor_FunctionDecl: return "FunctionDecl";
557 case CXCursor_TypedefDecl: return "TypedefDecl";
558 case CXCursor_EnumDecl: return "EnumDecl";
559 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000560 case CXCursor_StructDecl: return "StructDecl";
561 case CXCursor_UnionDecl: return "UnionDecl";
562 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000563 case CXCursor_FieldDecl: return "FieldDecl";
564 case CXCursor_VarDecl: return "VarDecl";
565 case CXCursor_ParmDecl: return "ParmDecl";
566 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
567 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
568 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
569 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
570 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000571 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
572 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
573 case CXCursor_FunctionDefn: return "FunctionDefn";
574 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
575 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
576 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
577 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000578 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000579 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000580 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000581 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
582
583 case CXCursor_VarRef: return "VarRef";
584 case CXCursor_FunctionRef: return "FunctionRef";
585 case CXCursor_EnumConstantRef: return "EnumConstantRef";
586 case CXCursor_MemberRef: return "MemberRef";
587
Steve Naroff77128dd2009-09-15 20:25:34 +0000588 case CXCursor_InvalidFile: return "InvalidFile";
589 case CXCursor_NoDeclFound: return "NoDeclFound";
590 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000591 default: return "<not implemented>";
592 }
Steve Naroff600866c2009-08-27 19:51:58 +0000593}
Steve Naroff89922f82009-08-31 00:59:03 +0000594
Steve Naroff9efa7672009-09-04 15:44:05 +0000595static enum CXCursorKind TranslateKind(Decl *D) {
596 switch (D->getKind()) {
597 case Decl::Function: return CXCursor_FunctionDecl;
598 case Decl::Typedef: return CXCursor_TypedefDecl;
599 case Decl::Enum: return CXCursor_EnumDecl;
600 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
601 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
602 case Decl::Field: return CXCursor_FieldDecl;
603 case Decl::Var: return CXCursor_VarDecl;
604 case Decl::ParmVar: return CXCursor_ParmDecl;
605 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000606 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
607 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000608 case Decl::ObjCMethod: {
609 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
610 if (MD->isInstanceMethod())
611 return CXCursor_ObjCInstanceMethodDecl;
612 return CXCursor_ObjCClassMethodDecl;
613 }
614 default: break;
615 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000616 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000617}
Steve Naroff600866c2009-08-27 19:51:58 +0000618//
619// CXCursor Operations.
620//
Steve Naroff9efa7672009-09-04 15:44:05 +0000621CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000622 unsigned line, unsigned column)
623{
Steve Naroff9efa7672009-09-04 15:44:05 +0000624 assert(CTUnit && "Passed null CXTranslationUnit");
625 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
626
627 FileManager &FMgr = CXXUnit->getFileManager();
628 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000629 source_name+strlen(source_name));
630 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000631 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000632 return C;
633 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000634 SourceLocation SLoc =
635 CXXUnit->getSourceManager().getLocation(File, line, column);
636
637 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
638
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000639 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000640 if (ALoc.isNamedRef())
641 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000642 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000643 if (Dcl) {
644 if (Stm) {
645 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
646 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
647 return C;
648 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
649 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
650 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000651 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000652 // Fall through...treat as a decl, not a ref.
653 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000654 if (ALoc.isNamedRef()) {
655 if (isa<ObjCInterfaceDecl>(Dcl)) {
656 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
657 return C;
658 }
659 if (isa<ObjCProtocolDecl>(Dcl)) {
660 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
661 return C;
662 }
663 }
664 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000665 return C;
666 }
Steve Narofffb570422009-09-22 19:25:29 +0000667 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000668 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000669}
670
Steve Naroff77128dd2009-09-15 20:25:34 +0000671CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
672{
673 assert(AnonDecl && "Passed null CXDecl");
674 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
675
Steve Narofffb570422009-09-22 19:25:29 +0000676 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000677 return C;
678}
679
680unsigned clang_isInvalid(enum CXCursorKind K)
681{
682 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
683}
684
Steve Naroff89922f82009-08-31 00:59:03 +0000685unsigned clang_isDeclaration(enum CXCursorKind K)
686{
687 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
688}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000689
Steve Narofff334b4e2009-09-02 18:26:48 +0000690unsigned clang_isReference(enum CXCursorKind K)
691{
692 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
693}
694
695unsigned clang_isDefinition(enum CXCursorKind K)
696{
697 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
698}
699
Steve Naroff9efa7672009-09-04 15:44:05 +0000700CXCursorKind clang_getCursorKind(CXCursor C)
701{
702 return C.kind;
703}
704
Steve Naroff699a07d2009-09-25 21:32:34 +0000705static Decl *getDeclFromExpr(Stmt *E) {
706 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
707 return RefExpr->getDecl();
708 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
709 return ME->getMemberDecl();
710 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
711 return RE->getDecl();
712
713 if (CallExpr *CE = dyn_cast<CallExpr>(E))
714 return getDeclFromExpr(CE->getCallee());
715 if (CastExpr *CE = dyn_cast<CastExpr>(E))
716 return getDeclFromExpr(CE->getSubExpr());
717 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
718 return OME->getMethodDecl();
719
720 return 0;
721}
722
Steve Naroff9efa7672009-09-04 15:44:05 +0000723CXDecl clang_getCursorDecl(CXCursor C)
724{
Steve Naroff699a07d2009-09-25 21:32:34 +0000725 if (clang_isDeclaration(C.kind))
726 return C.decl;
727
728 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000729 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000730 if (C.kind == CXCursor_ObjCClassRef ||
731 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000732 return static_cast<Stmt *>(C.stmt);
733 else
734 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
735 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000736 return C.decl;
737 }
738 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000739}
740
Steve Naroff85e2db72009-10-01 00:31:07 +0000741
Steve Narofff334b4e2009-09-02 18:26:48 +0000742static SourceLocation getLocationFromCursor(CXCursor C,
743 SourceManager &SourceMgr,
744 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000745 if (clang_isReference(C.kind)) {
746 switch (C.kind) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000747 case CXCursor_ObjCClassRef:
748 {
749 if (isa<ObjCInterfaceDecl>(ND)) {
750 // FIXME: This is a hack (storing the parent decl in the stmt slot).
751 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
752 return parentDecl->getLocation();
753 }
754 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
755 assert(OID && "clang_getCursorLine(): Missing category decl");
756 return OID->getClassInterface()->getLocation();
757 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000758 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000759 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000760 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
761 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000762 return OID->getSuperClassLoc();
763 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000764 case CXCursor_ObjCProtocolRef:
765 {
766 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
767 assert(OID && "clang_getCursorLine(): Missing protocol decl");
768 return OID->getLocation();
769 }
Steve Narofffb570422009-09-22 19:25:29 +0000770 case CXCursor_ObjCSelectorRef:
771 {
772 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
773 static_cast<Stmt *>(C.stmt));
774 assert(OME && "clang_getCursorLine(): Missing message expr");
775 return OME->getLeftLoc(); /* FIXME: should be a range */
776 }
777 case CXCursor_VarRef:
778 case CXCursor_FunctionRef:
779 case CXCursor_EnumConstantRef:
780 {
781 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
782 static_cast<Stmt *>(C.stmt));
783 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
784 return DRE->getLocation();
785 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000786 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000787 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000788 }
789 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000790 SourceLocation SLoc;
791 switch (ND->getKind()) {
792 case Decl::ObjCInterface:
793 {
794 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
795 break;
796 }
797 case Decl::ObjCProtocol:
798 {
799 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
800 break;
801 }
802 default:
803 {
804 SLoc = ND->getLocation();
805 break;
806 }
807 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000808 if (SLoc.isInvalid())
809 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000810 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000811 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000812}
813
Steve Naroff2d4d6292009-08-31 14:26:51 +0000814unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000815{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000816 assert(C.decl && "CXCursor has null decl");
817 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000818 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000819
820 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000821 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000822}
Steve Narofff334b4e2009-09-02 18:26:48 +0000823
Steve Naroff2d4d6292009-08-31 14:26:51 +0000824unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000825{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000826 assert(C.decl && "CXCursor has null decl");
827 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000828 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000829
830 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000831 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000832}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000833const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000834{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000835 assert(C.decl && "CXCursor has null decl");
836 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000837 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000838
839 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000840 if (SLoc.isFileID())
841 return SourceMgr.getBufferName(SLoc);
842
843 // Retrieve the file in which the macro was instantiated, then provide that
844 // buffer name.
845 // FIXME: Do we want to give specific macro-instantiation information?
846 const llvm::MemoryBuffer *Buffer
847 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
848 if (!Buffer)
849 return 0;
850
851 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000852}
853
Steve Naroff4ade6d62009-09-23 17:52:52 +0000854void clang_getDefinitionSpellingAndExtent(CXCursor C,
855 const char **startBuf,
856 const char **endBuf,
857 unsigned *startLine,
858 unsigned *startColumn,
859 unsigned *endLine,
860 unsigned *endColumn)
861{
862 assert(C.decl && "CXCursor has null decl");
863 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
864 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
865 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
866
867 SourceManager &SM = FD->getASTContext().getSourceManager();
868 *startBuf = SM.getCharacterData(Body->getLBracLoc());
869 *endBuf = SM.getCharacterData(Body->getRBracLoc());
870 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
871 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
872 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
873 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
874}
875
876
Steve Naroff600866c2009-08-27 19:51:58 +0000877} // end extern "C"