blob: 8d09b527b826f9d1b47fe707fb61e8198ba2c0f5 [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Clang-C Source Indexing library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang-c/Index.h"
Steve Naroff50398192009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroff9efa7672009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Steve Naroff50398192009-08-28 15:28:48 +000019#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000020#include "clang/AST/StmtVisitor.h"
Steve Naroffc857ea42009-09-02 13:28:54 +000021#include "clang/AST/Decl.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000022#include "clang/Basic/FileManager.h"
Steve Naroff2d4d6292009-08-31 14:26:51 +000023#include "clang/Basic/SourceManager.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000024#include "clang/Frontend/ASTUnit.h"
Douglas Gregor02465752009-10-16 21:24:31 +000025#include "llvm/Support/MemoryBuffer.h"
26#include "llvm/System/Path.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000027#include <cstdio>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000028#include <dlfcn.h>
Ted Kremenek8c4195e2009-10-15 22:10:56 +000029#include <sys/wait.h>
Ted Kremenek74cd0692009-10-15 23:21:22 +000030#include <vector>
Steve Naroff5b7d8e22009-10-15 20:04:39 +000031
Steve Naroff50398192009-08-28 15:28:48 +000032using namespace clang;
33using namespace idx;
34
Steve Naroff89922f82009-08-31 00:59:03 +000035namespace {
36
Steve Naroff4ade6d62009-09-23 17:52:52 +000037static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
38{
39 NamedDecl *D = DRE->getDecl();
40 if (isa<VarDecl>(D))
41 return CXCursor_VarRef;
42 else if (isa<FunctionDecl>(D))
43 return CXCursor_FunctionRef;
44 else if (isa<EnumConstantDecl>(D))
45 return CXCursor_EnumConstantRef;
46 else
47 return CXCursor_NotImplemented;
48}
49
50#if 0
51// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000052class CRefVisitor : public StmtVisitor<CRefVisitor> {
53 CXDecl CDecl;
54 CXDeclIterator Callback;
55 CXClientData CData;
56
57 void Call(enum CXCursorKind CK, Stmt *SRef) {
58 CXCursor C = { CK, CDecl, SRef };
59 Callback(CDecl, C, CData);
60 }
61
62public:
63 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
64 CDecl(C), Callback(cback), CData(D) {}
65
66 void VisitStmt(Stmt *S) {
67 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
68 C != CEnd; ++C)
69 Visit(*C);
70 }
71 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000072 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000073 }
74 void VisitMemberExpr(MemberExpr *Node) {
75 Call(CXCursor_MemberRef, Node);
76 }
77 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
78 Call(CXCursor_ObjCSelectorRef, Node);
79 }
80 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
81 Call(CXCursor_ObjCIvarRef, Node);
82 }
83};
Steve Naroff4ade6d62009-09-23 17:52:52 +000084#endif
Steve Narofffb570422009-09-22 19:25:29 +000085
Steve Naroff89922f82009-08-31 00:59:03 +000086// Translation Unit Visitor.
87class TUVisitor : public DeclVisitor<TUVisitor> {
88 CXTranslationUnit TUnit;
89 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000090 CXClientData CData;
91
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000092 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
93 // to the visitor. Declarations with a PCH level greater than this value will
94 // be suppressed.
95 unsigned MaxPCHLevel;
96
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000097 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000098 // Filter any declarations that have a PCH level greater than what we allow.
99 if (ND->getPCHLevel() > MaxPCHLevel)
100 return;
101
Steve Narofffb570422009-09-22 19:25:29 +0000102 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000103 Callback(TUnit, C, CData);
104 }
Steve Naroff89922f82009-08-31 00:59:03 +0000105public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000106 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000107 CXTranslationUnitIterator cback, CXClientData D,
108 unsigned MaxPCHLevel) :
109 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000110
111 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
112 VisitDeclContext(dyn_cast<DeclContext>(D));
113 }
114 void VisitDeclContext(DeclContext *DC) {
115 for (DeclContext::decl_iterator
116 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
117 Visit(*I);
118 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000119 void VisitTypedefDecl(TypedefDecl *ND) {
120 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000121 }
122 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000123 switch (ND->getTagKind()) {
124 case TagDecl::TK_struct:
125 Call(CXCursor_StructDecl, ND);
126 break;
127 case TagDecl::TK_class:
128 Call(CXCursor_ClassDecl, ND);
129 break;
130 case TagDecl::TK_union:
131 Call(CXCursor_UnionDecl, ND);
132 break;
133 case TagDecl::TK_enum:
134 Call(CXCursor_EnumDecl, ND);
135 break;
136 }
Steve Naroff89922f82009-08-31 00:59:03 +0000137 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000138 void VisitVarDecl(VarDecl *ND) {
139 Call(CXCursor_VarDecl, ND);
140 }
Steve Naroff89922f82009-08-31 00:59:03 +0000141 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000142 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
143 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000144 }
145 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000146 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000147 }
148 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000149 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000150 }
151 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000152 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000153 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000154 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
155 Call(CXCursor_ObjCClassDefn, ND);
156 }
157 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
158 Call(CXCursor_ObjCCategoryDefn, ND);
159 }
Steve Naroff89922f82009-08-31 00:59:03 +0000160};
161
Steve Naroffc857ea42009-09-02 13:28:54 +0000162// Declaration visitor.
163class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
164 CXDecl CDecl;
165 CXDeclIterator Callback;
166 CXClientData CData;
167
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000168 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
169 // to the visitor. Declarations with a PCH level greater than this value will
170 // be suppressed.
171 unsigned MaxPCHLevel;
172
Steve Naroffc857ea42009-09-02 13:28:54 +0000173 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000174 // Disable the callback when the context is equal to the visiting decl.
175 if (CDecl == ND && !clang_isReference(CK))
176 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000177
178 // Filter any declarations that have a PCH level greater than what we allow.
179 if (ND->getPCHLevel() > MaxPCHLevel)
180 return;
181
Steve Narofffb570422009-09-22 19:25:29 +0000182 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000183 Callback(CDecl, C, CData);
184 }
Steve Naroff89922f82009-08-31 00:59:03 +0000185public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000186 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
187 unsigned MaxPCHLevel) :
188 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000189
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000190 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
191 // Issue callbacks for the containing class.
192 Call(CXCursor_ObjCClassRef, ND);
193 // FIXME: Issue callbacks for protocol refs.
194 VisitDeclContext(dyn_cast<DeclContext>(ND));
195 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000196 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000197 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000198 if (D->getSuperClass())
199 Call(CXCursor_ObjCSuperClassRef, D);
200
Steve Naroff9efa7672009-09-04 15:44:05 +0000201 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
202 E = D->protocol_end(); I != E; ++I)
203 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000204 VisitDeclContext(dyn_cast<DeclContext>(D));
205 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000206 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
207 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
208 E = PID->protocol_end(); I != E; ++I)
209 Call(CXCursor_ObjCProtocolRef, *I);
210
211 VisitDeclContext(dyn_cast<DeclContext>(PID));
212 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000213 void VisitTagDecl(TagDecl *D) {
214 VisitDeclContext(dyn_cast<DeclContext>(D));
215 }
216 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
217 VisitDeclContext(dyn_cast<DeclContext>(D));
218 }
219 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
220 VisitDeclContext(dyn_cast<DeclContext>(D));
221 }
Steve Naroff89922f82009-08-31 00:59:03 +0000222 void VisitDeclContext(DeclContext *DC) {
223 for (DeclContext::decl_iterator
224 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
225 Visit(*I);
226 }
227 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000228 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000229 }
230 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000231 Call(CXCursor_FieldDecl, ND);
232 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000233 void VisitVarDecl(VarDecl *ND) {
234 Call(CXCursor_VarDecl, ND);
235 }
236 void VisitParmVarDecl(ParmVarDecl *ND) {
237 Call(CXCursor_ParmDecl, ND);
238 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000239 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
240 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000241 }
242 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000243 Call(CXCursor_ObjCIvarDecl, ND);
244 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000245 void VisitFunctionDecl(FunctionDecl *ND) {
246 if (ND->isThisDeclarationADefinition()) {
247 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000248#if 0
249 // Not currently needed.
250 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000251 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000252 RVisit.Visit(Body);
253#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000254 }
255 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000256 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
257 if (ND->getBody()) {
258 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
259 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000260 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000261 } else
262 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
263 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000264 }
265};
266
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000267class CIndexer : public Indexer {
268public:
Ted Kremenekdff76892009-10-17 06:21:47 +0000269 explicit CIndexer(Program *prog) : Indexer(*prog), OnlyLocalDecls(false) {}
270
271 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000272
273 /// \brief Whether we only want to see "local" declarations (that did not
274 /// come from a previous precompiled header). If false, we want to see all
275 /// declarations.
276 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
277 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
278
279private:
280 bool OnlyLocalDecls;
281};
282
Steve Naroff89922f82009-08-31 00:59:03 +0000283}
284
Steve Naroff600866c2009-08-27 19:51:58 +0000285extern "C" {
Ted Kremenekd2fa5662009-08-26 22:36:44 +0000286
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000287static const char *clangPath;
288
Steve Naroff600866c2009-08-27 19:51:58 +0000289CXIndex clang_createIndex()
Steve Naroff50398192009-08-28 15:28:48 +0000290{
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000291 // Find the location where this library lives (libCIndex.dylib).
292 // We do the lookup here to avoid poking dladdr too many times.
293 // This silly cast below avoids a C++ warning.
294 Dl_info info;
295 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
296 assert(0 && "Call to dladdr() failed");
297
298 llvm::sys::Path CIndexPath(info.dli_fname);
299 std::string CIndexDir = CIndexPath.getDirname();
300
301 // We now have the CIndex directory, locate clang relative to it.
302 std::string ClangPath = CIndexDir + "/../bin/clang";
303
304 clangPath = ClangPath.c_str();
305
Ted Kremenekdff76892009-10-17 06:21:47 +0000306 return new CIndexer(new Program());
Steve Naroff600866c2009-08-27 19:51:58 +0000307}
308
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000309void clang_disposeIndex(CXIndex CIdx)
310{
311 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000312 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000313}
314
Steve Naroff50398192009-08-28 15:28:48 +0000315// FIXME: need to pass back error info.
316CXTranslationUnit clang_createTranslationUnit(
317 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000318{
Steve Naroff50398192009-08-28 15:28:48 +0000319 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000320 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000321 std::string astName(ast_filename);
322 std::string ErrMsg;
323
Daniel Dunbar31b87d82009-09-21 03:03:39 +0000324 return ASTUnit::LoadFromPCHFile(astName, CXXIdx->getDiagnostics(),
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000325 CXXIdx->getFileManager(), &ErrMsg,
Ted Kremenek5cf48762009-10-17 00:34:24 +0000326 CXXIdx->getOnlyLocalDecls(),
327 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000328}
329
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000330CXTranslationUnit clang_createTranslationUnitFromSourceFile(
331 CXIndex CIdx,
332 const char *source_filename,
333 int num_command_line_args, const char **command_line_args)
334{
Ted Kremenek74cd0692009-10-15 23:21:22 +0000335 // Build up the arguments for involing clang.
336 std::vector<const char *> argv;
337 argv.push_back(clangPath);
338 argv.push_back("-emit-ast");
339 argv.push_back(source_filename);
340 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000341 // Generate a temporary name for the AST file.
342 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000343 argv.push_back(tmpnam(astTmpFile));
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000344 for (int i = num_command_line_args; i < num_command_line_args; i++)
Ted Kremenek74cd0692009-10-15 23:21:22 +0000345 argv.push_back(command_line_args[i]);
346 argv.push_back(NULL);
347
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000348 // Generate the AST file in a separate process.
349 pid_t child_pid = fork();
350 if (child_pid == 0) { // Child process
351
352 // Execute the command, passing the appropriate arguments.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000353 execv(argv[0], (char *const *)&argv[0]);
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000354
355 // If execv returns, it failed.
356 assert(0 && "execv() failed");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000357 }
358 // This is run by the parent.
359 int child_status;
360 pid_t tpid;
361 do { // Wait for the child to terminate.
362 tpid = wait(&child_status);
363 } while (tpid != child_pid);
364
365 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000366 ASTUnit *ATU = static_cast<ASTUnit *>(
367 clang_createTranslationUnit(CIdx, astTmpFile));
368 ATU->unlinkTemporaryFile();
369 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000370}
371
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000372void clang_disposeTranslationUnit(
373 CXTranslationUnit CTUnit)
374{
375 assert(CTUnit && "Passed null CXTranslationUnit");
376 delete static_cast<ASTUnit *>(CTUnit);
377}
378
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000379void clang_wantOnlyLocalDeclarations(CXIndex CIdx) {
380 static_cast<CIndexer *>(CIdx)->setOnlyLocalDecls(true);
381}
382
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000383const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
384{
385 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000386 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
387 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000388}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000389
Steve Naroffc857ea42009-09-02 13:28:54 +0000390void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
391 CXTranslationUnitIterator callback,
392 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000393{
Steve Naroff50398192009-08-28 15:28:48 +0000394 assert(CTUnit && "Passed null CXTranslationUnit");
395 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
396 ASTContext &Ctx = CXXUnit->getASTContext();
397
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000398 TUVisitor DVisit(CTUnit, callback, CData,
399 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000400 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000401}
402
Steve Naroffc857ea42009-09-02 13:28:54 +0000403void clang_loadDeclaration(CXDecl Dcl,
404 CXDeclIterator callback,
405 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000406{
Steve Naroffc857ea42009-09-02 13:28:54 +0000407 assert(Dcl && "Passed null CXDecl");
408
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000409 CDeclVisitor DVisit(Dcl, callback, CData,
410 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000411 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000412}
413
Steve Naroff7e8f8182009-08-28 12:07:44 +0000414// Some notes on CXEntity:
415//
416// - Since the 'ordinary' namespace includes functions, data, typedefs,
417// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
418// entity for 2 different types). For example:
419//
420// module1.m: @interface Foo @end Foo *x;
421// module2.m: void Foo(int);
422//
423// - Since the unique name spans translation units, static data/functions
424// within a CXTranslationUnit are *not* currently represented by entities.
425// As a result, there will be no entity for the following:
426//
427// module.m: static void Foo() { }
428//
429
430
Steve Naroff600866c2009-08-27 19:51:58 +0000431const char *clang_getDeclarationName(CXEntity)
432{
433 return "";
434}
435const char *clang_getURI(CXEntity)
436{
437 return "";
438}
439
440CXEntity clang_getEntity(const char *URI)
441{
442 return 0;
443}
444
445//
446// CXDecl Operations.
447//
Steve Naroff600866c2009-08-27 19:51:58 +0000448CXEntity clang_getEntityFromDecl(CXDecl)
449{
450 return 0;
451}
Steve Naroff89922f82009-08-31 00:59:03 +0000452const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000453{
Steve Naroff89922f82009-08-31 00:59:03 +0000454 assert(AnonDecl && "Passed null CXDecl");
455 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000456
457 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
458 return OMD->getSelector().getAsString().c_str();
459 }
Steve Naroff89922f82009-08-31 00:59:03 +0000460 if (ND->getIdentifier())
461 return ND->getIdentifier()->getName();
Steve Naroffc857ea42009-09-02 13:28:54 +0000462 else
Steve Naroff89922f82009-08-31 00:59:03 +0000463 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000464}
Steve Narofff334b4e2009-09-02 18:26:48 +0000465
Steve Naroff699a07d2009-09-25 21:32:34 +0000466unsigned clang_getDeclLine(CXDecl AnonDecl)
467{
468 assert(AnonDecl && "Passed null CXDecl");
469 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
470 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
471 return SourceMgr.getSpellingLineNumber(ND->getLocation());
472}
473
474unsigned clang_getDeclColumn(CXDecl AnonDecl)
475{
476 assert(AnonDecl && "Passed null CXDecl");
477 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
478 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000479 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000480}
481
Steve Naroffee9405e2009-09-25 21:45:39 +0000482const char *clang_getDeclSource(CXDecl AnonDecl)
483{
484 assert(AnonDecl && "Passed null CXDecl");
485 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
486 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
487 return SourceMgr.getBufferName(ND->getLocation());
488}
489
Steve Narofff334b4e2009-09-02 18:26:48 +0000490const char *clang_getCursorSpelling(CXCursor C)
491{
492 assert(C.decl && "CXCursor has null decl");
493 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
494
495 if (clang_isReference(C.kind)) {
496 switch (C.kind) {
Steve Naroff1164d852009-09-02 18:58:52 +0000497 case CXCursor_ObjCSuperClassRef:
498 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000499 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
500 assert(OID && "clang_getCursorLine(): Missing interface decl");
501 return OID->getSuperClass()->getIdentifier()->getName();
Steve Naroff1164d852009-09-02 18:58:52 +0000502 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000503 case CXCursor_ObjCClassRef:
504 {
Steve Naroff85e2db72009-10-01 00:31:07 +0000505 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
506 return OID->getIdentifier()->getName();
507 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000508 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
509 assert(OID && "clang_getCursorLine(): Missing category decl");
510 return OID->getClassInterface()->getIdentifier()->getName();
511 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000512 case CXCursor_ObjCProtocolRef:
513 {
514 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
515 assert(OID && "clang_getCursorLine(): Missing protocol decl");
516 return OID->getIdentifier()->getName();
517 }
Steve Narofffb570422009-09-22 19:25:29 +0000518 case CXCursor_ObjCSelectorRef:
519 {
520 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
521 static_cast<Stmt *>(C.stmt));
522 assert(OME && "clang_getCursorLine(): Missing message expr");
523 return OME->getSelector().getAsString().c_str();
524 }
525 case CXCursor_VarRef:
526 case CXCursor_FunctionRef:
527 case CXCursor_EnumConstantRef:
528 {
529 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
530 static_cast<Stmt *>(C.stmt));
531 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
532 return DRE->getDecl()->getIdentifier()->getName();
533 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000534 default:
535 return "<not implemented>";
536 }
537 }
538 return clang_getDeclSpelling(C.decl);
539}
540
541const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000542{
Steve Naroff89922f82009-08-31 00:59:03 +0000543 switch (Kind) {
544 case CXCursor_FunctionDecl: return "FunctionDecl";
545 case CXCursor_TypedefDecl: return "TypedefDecl";
546 case CXCursor_EnumDecl: return "EnumDecl";
547 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000548 case CXCursor_StructDecl: return "StructDecl";
549 case CXCursor_UnionDecl: return "UnionDecl";
550 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000551 case CXCursor_FieldDecl: return "FieldDecl";
552 case CXCursor_VarDecl: return "VarDecl";
553 case CXCursor_ParmDecl: return "ParmDecl";
554 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
555 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
556 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
557 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
558 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000559 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
560 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
561 case CXCursor_FunctionDefn: return "FunctionDefn";
562 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
563 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
564 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
565 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000566 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000567 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000568 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000569 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
570
571 case CXCursor_VarRef: return "VarRef";
572 case CXCursor_FunctionRef: return "FunctionRef";
573 case CXCursor_EnumConstantRef: return "EnumConstantRef";
574 case CXCursor_MemberRef: return "MemberRef";
575
Steve Naroff77128dd2009-09-15 20:25:34 +0000576 case CXCursor_InvalidFile: return "InvalidFile";
577 case CXCursor_NoDeclFound: return "NoDeclFound";
578 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000579 default: return "<not implemented>";
580 }
Steve Naroff600866c2009-08-27 19:51:58 +0000581}
Steve Naroff89922f82009-08-31 00:59:03 +0000582
Steve Naroff9efa7672009-09-04 15:44:05 +0000583static enum CXCursorKind TranslateKind(Decl *D) {
584 switch (D->getKind()) {
585 case Decl::Function: return CXCursor_FunctionDecl;
586 case Decl::Typedef: return CXCursor_TypedefDecl;
587 case Decl::Enum: return CXCursor_EnumDecl;
588 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
589 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
590 case Decl::Field: return CXCursor_FieldDecl;
591 case Decl::Var: return CXCursor_VarDecl;
592 case Decl::ParmVar: return CXCursor_ParmDecl;
593 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000594 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
595 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000596 case Decl::ObjCMethod: {
597 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
598 if (MD->isInstanceMethod())
599 return CXCursor_ObjCInstanceMethodDecl;
600 return CXCursor_ObjCClassMethodDecl;
601 }
602 default: break;
603 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000604 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000605}
Steve Naroff600866c2009-08-27 19:51:58 +0000606//
607// CXCursor Operations.
608//
Steve Naroff9efa7672009-09-04 15:44:05 +0000609CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000610 unsigned line, unsigned column)
611{
Steve Naroff9efa7672009-09-04 15:44:05 +0000612 assert(CTUnit && "Passed null CXTranslationUnit");
613 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
614
615 FileManager &FMgr = CXXUnit->getFileManager();
616 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000617 source_name+strlen(source_name));
618 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000619 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000620 return C;
621 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000622 SourceLocation SLoc =
623 CXXUnit->getSourceManager().getLocation(File, line, column);
624
625 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
626
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000627 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000628 if (ALoc.isNamedRef())
629 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000630 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000631 if (Dcl) {
632 if (Stm) {
633 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
634 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
635 return C;
636 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
637 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
638 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000639 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000640 // Fall through...treat as a decl, not a ref.
641 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000642 if (ALoc.isNamedRef()) {
643 if (isa<ObjCInterfaceDecl>(Dcl)) {
644 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
645 return C;
646 }
647 if (isa<ObjCProtocolDecl>(Dcl)) {
648 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
649 return C;
650 }
651 }
652 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000653 return C;
654 }
Steve Narofffb570422009-09-22 19:25:29 +0000655 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000656 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000657}
658
Steve Naroff77128dd2009-09-15 20:25:34 +0000659CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
660{
661 assert(AnonDecl && "Passed null CXDecl");
662 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
663
Steve Narofffb570422009-09-22 19:25:29 +0000664 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000665 return C;
666}
667
668unsigned clang_isInvalid(enum CXCursorKind K)
669{
670 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
671}
672
Steve Naroff89922f82009-08-31 00:59:03 +0000673unsigned clang_isDeclaration(enum CXCursorKind K)
674{
675 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
676}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000677
Steve Narofff334b4e2009-09-02 18:26:48 +0000678unsigned clang_isReference(enum CXCursorKind K)
679{
680 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
681}
682
683unsigned clang_isDefinition(enum CXCursorKind K)
684{
685 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
686}
687
Steve Naroff9efa7672009-09-04 15:44:05 +0000688CXCursorKind clang_getCursorKind(CXCursor C)
689{
690 return C.kind;
691}
692
Steve Naroff699a07d2009-09-25 21:32:34 +0000693static Decl *getDeclFromExpr(Stmt *E) {
694 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
695 return RefExpr->getDecl();
696 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
697 return ME->getMemberDecl();
698 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
699 return RE->getDecl();
700
701 if (CallExpr *CE = dyn_cast<CallExpr>(E))
702 return getDeclFromExpr(CE->getCallee());
703 if (CastExpr *CE = dyn_cast<CastExpr>(E))
704 return getDeclFromExpr(CE->getSubExpr());
705 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
706 return OME->getMethodDecl();
707
708 return 0;
709}
710
Steve Naroff9efa7672009-09-04 15:44:05 +0000711CXDecl clang_getCursorDecl(CXCursor C)
712{
Steve Naroff699a07d2009-09-25 21:32:34 +0000713 if (clang_isDeclaration(C.kind))
714 return C.decl;
715
716 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000717 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000718 if (C.kind == CXCursor_ObjCClassRef ||
719 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000720 return static_cast<Stmt *>(C.stmt);
721 else
722 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
723 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000724 return C.decl;
725 }
726 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000727}
728
Steve Naroff85e2db72009-10-01 00:31:07 +0000729
Steve Narofff334b4e2009-09-02 18:26:48 +0000730static SourceLocation getLocationFromCursor(CXCursor C,
731 SourceManager &SourceMgr,
732 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000733 if (clang_isReference(C.kind)) {
734 switch (C.kind) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000735 case CXCursor_ObjCClassRef:
736 {
737 if (isa<ObjCInterfaceDecl>(ND)) {
738 // FIXME: This is a hack (storing the parent decl in the stmt slot).
739 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
740 return parentDecl->getLocation();
741 }
742 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
743 assert(OID && "clang_getCursorLine(): Missing category decl");
744 return OID->getClassInterface()->getLocation();
745 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000746 case CXCursor_ObjCSuperClassRef:
Steve Naroff1164d852009-09-02 18:58:52 +0000747 {
Steve Narofff334b4e2009-09-02 18:26:48 +0000748 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
749 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000750 return OID->getSuperClassLoc();
751 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000752 case CXCursor_ObjCProtocolRef:
753 {
754 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
755 assert(OID && "clang_getCursorLine(): Missing protocol decl");
756 return OID->getLocation();
757 }
Steve Narofffb570422009-09-22 19:25:29 +0000758 case CXCursor_ObjCSelectorRef:
759 {
760 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
761 static_cast<Stmt *>(C.stmt));
762 assert(OME && "clang_getCursorLine(): Missing message expr");
763 return OME->getLeftLoc(); /* FIXME: should be a range */
764 }
765 case CXCursor_VarRef:
766 case CXCursor_FunctionRef:
767 case CXCursor_EnumConstantRef:
768 {
769 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
770 static_cast<Stmt *>(C.stmt));
771 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
772 return DRE->getLocation();
773 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000774 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000775 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000776 }
777 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000778 SourceLocation SLoc;
779 switch (ND->getKind()) {
780 case Decl::ObjCInterface:
781 {
782 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
783 break;
784 }
785 case Decl::ObjCProtocol:
786 {
787 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
788 break;
789 }
790 default:
791 {
792 SLoc = ND->getLocation();
793 break;
794 }
795 }
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"