blob: 66db65b009062c044e13dc61aff197980a6ab3d8 [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"
Ted Kremenekfc062212009-10-19 21:44:57 +000026#include "llvm/Support/Compiler.h"
Douglas Gregor02465752009-10-16 21:24:31 +000027#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/System/Path.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000029#include "llvm/System/Program.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000030
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000031#include <cstdio>
Ted Kremenek49358d82009-10-19 21:17:25 +000032#include <vector>
33
Benjamin Kramer20d75812009-10-18 16:13:48 +000034#ifdef LLVM_ON_WIN32
35#define WIN32_LEAN_AND_MEAN
36#include <windows.h>
37#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +000038#include <dlfcn.h>
Daniel Dunbara47dd192009-10-17 23:53:11 +000039#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +000040
Steve Naroff50398192009-08-28 15:28:48 +000041using namespace clang;
42using namespace idx;
43
Steve Naroff89922f82009-08-31 00:59:03 +000044namespace {
Steve Naroff4ade6d62009-09-23 17:52:52 +000045static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
46{
47 NamedDecl *D = DRE->getDecl();
48 if (isa<VarDecl>(D))
49 return CXCursor_VarRef;
50 else if (isa<FunctionDecl>(D))
51 return CXCursor_FunctionRef;
52 else if (isa<EnumConstantDecl>(D))
53 return CXCursor_EnumConstantRef;
54 else
55 return CXCursor_NotImplemented;
56}
57
58#if 0
59// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000060class CRefVisitor : public StmtVisitor<CRefVisitor> {
61 CXDecl CDecl;
62 CXDeclIterator Callback;
63 CXClientData CData;
64
65 void Call(enum CXCursorKind CK, Stmt *SRef) {
66 CXCursor C = { CK, CDecl, SRef };
67 Callback(CDecl, C, CData);
68 }
69
70public:
71 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
72 CDecl(C), Callback(cback), CData(D) {}
73
74 void VisitStmt(Stmt *S) {
75 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
76 C != CEnd; ++C)
77 Visit(*C);
78 }
79 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000080 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000081 }
82 void VisitMemberExpr(MemberExpr *Node) {
83 Call(CXCursor_MemberRef, Node);
84 }
85 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
86 Call(CXCursor_ObjCSelectorRef, Node);
87 }
88 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
89 Call(CXCursor_ObjCIvarRef, Node);
90 }
91};
Steve Naroff4ade6d62009-09-23 17:52:52 +000092#endif
Ted Kremenekfc062212009-10-19 21:44:57 +000093
94/// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted
95/// warnings and errors.
96class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient {
97public:
98 virtual ~IgnoreDiagnosticsClient() {}
99 virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {}
100};
Steve Narofffb570422009-09-22 19:25:29 +0000101
Steve Naroff89922f82009-08-31 00:59:03 +0000102// Translation Unit Visitor.
103class TUVisitor : public DeclVisitor<TUVisitor> {
104 CXTranslationUnit TUnit;
105 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000106 CXClientData CData;
107
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000108 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
109 // to the visitor. Declarations with a PCH level greater than this value will
110 // be suppressed.
111 unsigned MaxPCHLevel;
112
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000113 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000114 // Filter any declarations that have a PCH level greater than what we allow.
115 if (ND->getPCHLevel() > MaxPCHLevel)
116 return;
117
Steve Narofffb570422009-09-22 19:25:29 +0000118 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000119 Callback(TUnit, C, CData);
120 }
Steve Naroff89922f82009-08-31 00:59:03 +0000121public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000122 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000123 CXTranslationUnitIterator cback, CXClientData D,
124 unsigned MaxPCHLevel) :
125 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000126
127 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
128 VisitDeclContext(dyn_cast<DeclContext>(D));
129 }
130 void VisitDeclContext(DeclContext *DC) {
131 for (DeclContext::decl_iterator
132 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
133 Visit(*I);
134 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000135 void VisitTypedefDecl(TypedefDecl *ND) {
136 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000137 }
138 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000139 switch (ND->getTagKind()) {
140 case TagDecl::TK_struct:
141 Call(CXCursor_StructDecl, ND);
142 break;
143 case TagDecl::TK_class:
144 Call(CXCursor_ClassDecl, ND);
145 break;
146 case TagDecl::TK_union:
147 Call(CXCursor_UnionDecl, ND);
148 break;
149 case TagDecl::TK_enum:
150 Call(CXCursor_EnumDecl, ND);
151 break;
152 }
Steve Naroff89922f82009-08-31 00:59:03 +0000153 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000154 void VisitVarDecl(VarDecl *ND) {
155 Call(CXCursor_VarDecl, ND);
156 }
Steve Naroff89922f82009-08-31 00:59:03 +0000157 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000158 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
159 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000160 }
161 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000162 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000163 }
164 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000165 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000166 }
167 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000168 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000169 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000170 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
171 Call(CXCursor_ObjCClassDefn, ND);
172 }
173 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
174 Call(CXCursor_ObjCCategoryDefn, ND);
175 }
Steve Naroff89922f82009-08-31 00:59:03 +0000176};
177
Steve Naroffc857ea42009-09-02 13:28:54 +0000178// Declaration visitor.
179class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
180 CXDecl CDecl;
181 CXDeclIterator Callback;
182 CXClientData CData;
183
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000184 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
185 // to the visitor. Declarations with a PCH level greater than this value will
186 // be suppressed.
187 unsigned MaxPCHLevel;
188
Steve Naroffc857ea42009-09-02 13:28:54 +0000189 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000190 // Disable the callback when the context is equal to the visiting decl.
191 if (CDecl == ND && !clang_isReference(CK))
192 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000193
194 // Filter any declarations that have a PCH level greater than what we allow.
195 if (ND->getPCHLevel() > MaxPCHLevel)
196 return;
197
Steve Narofffb570422009-09-22 19:25:29 +0000198 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000199 Callback(CDecl, C, CData);
200 }
Steve Naroff89922f82009-08-31 00:59:03 +0000201public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000202 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
203 unsigned MaxPCHLevel) :
204 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000205
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000206 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
207 // Issue callbacks for the containing class.
208 Call(CXCursor_ObjCClassRef, ND);
209 // FIXME: Issue callbacks for protocol refs.
210 VisitDeclContext(dyn_cast<DeclContext>(ND));
211 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000212 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000213 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000214 if (D->getSuperClass())
215 Call(CXCursor_ObjCSuperClassRef, D);
216
Steve Naroff9efa7672009-09-04 15:44:05 +0000217 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
218 E = D->protocol_end(); I != E; ++I)
219 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000220 VisitDeclContext(dyn_cast<DeclContext>(D));
221 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000222 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
223 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
224 E = PID->protocol_end(); I != E; ++I)
225 Call(CXCursor_ObjCProtocolRef, *I);
226
227 VisitDeclContext(dyn_cast<DeclContext>(PID));
228 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000229 void VisitTagDecl(TagDecl *D) {
230 VisitDeclContext(dyn_cast<DeclContext>(D));
231 }
232 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
233 VisitDeclContext(dyn_cast<DeclContext>(D));
234 }
235 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
236 VisitDeclContext(dyn_cast<DeclContext>(D));
237 }
Steve Naroff89922f82009-08-31 00:59:03 +0000238 void VisitDeclContext(DeclContext *DC) {
239 for (DeclContext::decl_iterator
240 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
241 Visit(*I);
242 }
243 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000244 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000245 }
246 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000247 Call(CXCursor_FieldDecl, ND);
248 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000249 void VisitVarDecl(VarDecl *ND) {
250 Call(CXCursor_VarDecl, ND);
251 }
252 void VisitParmVarDecl(ParmVarDecl *ND) {
253 Call(CXCursor_ParmDecl, ND);
254 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000255 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
256 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000257 }
258 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000259 Call(CXCursor_ObjCIvarDecl, ND);
260 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000261 void VisitFunctionDecl(FunctionDecl *ND) {
262 if (ND->isThisDeclarationADefinition()) {
263 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000264#if 0
265 // Not currently needed.
266 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000267 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000268 RVisit.Visit(Body);
269#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000270 }
271 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000272 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
273 if (ND->getBody()) {
274 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
275 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000276 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000277 } else
278 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
279 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000280 }
281};
282
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000283class CIndexer : public Indexer {
284public:
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000285 explicit CIndexer(Program *prog) : Indexer(*prog),
286 OnlyLocalDecls(false),
287 DisplayDiagnostics(false) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000288
289 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000290
291 /// \brief Whether we only want to see "local" declarations (that did not
292 /// come from a previous precompiled header). If false, we want to see all
293 /// declarations.
294 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
295 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000296
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000297 void setDisplayDiagnostics(bool Display = true) {
298 DisplayDiagnostics = Display;
299 }
300 bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
301
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000302 /// \brief Get the path of the clang binary.
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000303 const llvm::sys::Path& getClangPath();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000304private:
305 bool OnlyLocalDecls;
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000306 bool DisplayDiagnostics;
307
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000308 llvm::sys::Path ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000309};
Steve Naroff89922f82009-08-31 00:59:03 +0000310
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000311const llvm::sys::Path& CIndexer::getClangPath() {
312 // Did we already compute the path?
313 if (!ClangPath.empty())
314 return ClangPath;
315
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000316 // Find the location where this library lives (libCIndex.dylib).
Benjamin Kramer20d75812009-10-18 16:13:48 +0000317#ifdef LLVM_ON_WIN32
318 MEMORY_BASIC_INFORMATION mbi;
319 char path[MAX_PATH];
Benjamin Krameredcd8282009-10-18 16:20:58 +0000320 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
Benjamin Kramer20d75812009-10-18 16:13:48 +0000321 sizeof(mbi));
322 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
323
324 llvm::sys::Path CIndexPath(path);
325#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000326 // This silly cast below avoids a C++ warning.
327 Dl_info info;
328 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
329 assert(0 && "Call to dladdr() failed");
330
331 llvm::sys::Path CIndexPath(info.dli_fname);
Daniel Dunbara47dd192009-10-17 23:53:11 +0000332#endif
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000333
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000334 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000335 CIndexPath.eraseComponent();
336 CIndexPath.eraseComponent();
337 CIndexPath.appendComponent("bin");
338 CIndexPath.appendComponent("clang");
339
340 // Cache our result.
341 ClangPath = CIndexPath;
342 return ClangPath;
343}
344
345}
346
347extern "C" {
348
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000349CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
350 int displayDiagnostics)
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000351{
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000352 CIndexer *CIdxr = new CIndexer(new Program());
353 if (excludeDeclarationsFromPCH)
354 CIdxr->setOnlyLocalDecls();
355 if (displayDiagnostics)
356 CIdxr->setDisplayDiagnostics();
357 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000358}
359
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000360void clang_disposeIndex(CXIndex CIdx)
361{
362 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000363 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000364}
365
Steve Naroff50398192009-08-28 15:28:48 +0000366// FIXME: need to pass back error info.
367CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000368 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000369{
Steve Naroff50398192009-08-28 15:28:48 +0000370 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000371 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000372 std::string astName(ast_filename);
373 std::string ErrMsg;
374
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000375 return ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
376 CXXIdx->getDisplayDiagnostics() ?
377 NULL : new IgnoreDiagnosticsClient(),
Ted Kremenek5cf48762009-10-17 00:34:24 +0000378 CXXIdx->getOnlyLocalDecls(),
379 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000380}
381
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000382CXTranslationUnit clang_createTranslationUnitFromSourceFile(
383 CXIndex CIdx,
384 const char *source_filename,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000385 int num_command_line_args, const char **command_line_args) {
386 assert(CIdx && "Passed null CXIndex");
387 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
388
Ted Kremenek139ba862009-10-22 00:03:57 +0000389 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000390 std::vector<const char *> argv;
Ted Kremenek139ba862009-10-22 00:03:57 +0000391
392 // First add the complete path to the 'clang' executable.
393 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000394 argv.push_back(ClangPath.c_str());
Ted Kremenek139ba862009-10-22 00:03:57 +0000395
396 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000397 argv.push_back("-emit-ast");
Ted Kremenek139ba862009-10-22 00:03:57 +0000398
399 // The 'source_filename' argument is optional. If the caller does not
400 // specify it then it is assumed that the source file is specified
401 // in the actual argument list.
402 if (source_filename)
403 argv.push_back(source_filename);
404
Steve Naroff37b5ac22009-10-15 20:50:09 +0000405 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000406 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000407 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000408 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000409
410 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
411 for (int i = 0; i < num_command_line_args; ++i)
412 if (const char *arg = command_line_args[i]) {
413 if (strcmp(arg, "-o") == 0) {
414 ++i; // Also skip the matching argument.
415 continue;
416 }
417 if (strcmp(arg, "-emit-ast") == 0 ||
418 strcmp(arg, "-c") == 0 ||
419 strcmp(arg, "-fsyntax-only") == 0) {
420 continue;
421 }
422
423 // Keep the argument.
424 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000425 }
Ted Kremenek139ba862009-10-22 00:03:57 +0000426
427 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000428 argv.push_back(NULL);
429
Ted Kremenekc46e4632009-10-19 22:27:32 +0000430#ifndef LLVM_ON_WIN32
431 llvm::sys::Path DevNull("/dev/null");
432 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
433 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], NULL,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000434 !CXXIdx->getDisplayDiagnostics() ?
435 &Redirects[0] : NULL);
Ted Kremenek13745982009-10-19 22:15:09 +0000436#else
437 // FIXME: I don't know what is the equivalent '/dev/null' redirect for
438 // Windows for this API.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000439 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0]);
Ted Kremenek13745982009-10-19 22:15:09 +0000440#endif
Benjamin Kramer0829a832009-10-18 11:19:36 +0000441
Steve Naroff37b5ac22009-10-15 20:50:09 +0000442 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000443 ASTUnit *ATU = static_cast<ASTUnit *>(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000444 clang_createTranslationUnit(CIdx, astTmpFile));
445 if (ATU)
446 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000447 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000448}
449
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000450void clang_disposeTranslationUnit(
451 CXTranslationUnit CTUnit)
452{
453 assert(CTUnit && "Passed null CXTranslationUnit");
454 delete static_cast<ASTUnit *>(CTUnit);
455}
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000456
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000457const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
458{
459 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000460 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
461 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000462}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000463
Steve Naroffc857ea42009-09-02 13:28:54 +0000464void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
465 CXTranslationUnitIterator callback,
466 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000467{
Steve Naroff50398192009-08-28 15:28:48 +0000468 assert(CTUnit && "Passed null CXTranslationUnit");
469 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
470 ASTContext &Ctx = CXXUnit->getASTContext();
471
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000472 TUVisitor DVisit(CTUnit, callback, CData,
473 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000474 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000475}
476
Steve Naroffc857ea42009-09-02 13:28:54 +0000477void clang_loadDeclaration(CXDecl Dcl,
478 CXDeclIterator callback,
479 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000480{
Steve Naroffc857ea42009-09-02 13:28:54 +0000481 assert(Dcl && "Passed null CXDecl");
482
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000483 CDeclVisitor DVisit(Dcl, callback, CData,
484 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000485 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000486}
487
Steve Naroff7e8f8182009-08-28 12:07:44 +0000488// Some notes on CXEntity:
489//
490// - Since the 'ordinary' namespace includes functions, data, typedefs,
491// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
492// entity for 2 different types). For example:
493//
494// module1.m: @interface Foo @end Foo *x;
495// module2.m: void Foo(int);
496//
497// - Since the unique name spans translation units, static data/functions
498// within a CXTranslationUnit are *not* currently represented by entities.
499// As a result, there will be no entity for the following:
500//
501// module.m: static void Foo() { }
502//
503
504
Steve Naroff600866c2009-08-27 19:51:58 +0000505const char *clang_getDeclarationName(CXEntity)
506{
507 return "";
508}
509const char *clang_getURI(CXEntity)
510{
511 return "";
512}
513
514CXEntity clang_getEntity(const char *URI)
515{
516 return 0;
517}
518
519//
520// CXDecl Operations.
521//
Steve Naroff600866c2009-08-27 19:51:58 +0000522CXEntity clang_getEntityFromDecl(CXDecl)
523{
524 return 0;
525}
Steve Naroff89922f82009-08-31 00:59:03 +0000526const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000527{
Steve Naroff89922f82009-08-31 00:59:03 +0000528 assert(AnonDecl && "Passed null CXDecl");
529 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000530
531 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
532 return OMD->getSelector().getAsString().c_str();
533 }
Steve Naroff89922f82009-08-31 00:59:03 +0000534 if (ND->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000535 return ND->getIdentifier()->getNameStart();
Steve Naroffc857ea42009-09-02 13:28:54 +0000536 else
Steve Naroff89922f82009-08-31 00:59:03 +0000537 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000538}
Steve Narofff334b4e2009-09-02 18:26:48 +0000539
Steve Naroff699a07d2009-09-25 21:32:34 +0000540unsigned clang_getDeclLine(CXDecl AnonDecl)
541{
542 assert(AnonDecl && "Passed null CXDecl");
543 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
544 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
545 return SourceMgr.getSpellingLineNumber(ND->getLocation());
546}
547
548unsigned clang_getDeclColumn(CXDecl AnonDecl)
549{
550 assert(AnonDecl && "Passed null CXDecl");
551 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
552 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000553 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000554}
555
Steve Naroffee9405e2009-09-25 21:45:39 +0000556const char *clang_getDeclSource(CXDecl AnonDecl)
557{
558 assert(AnonDecl && "Passed null CXDecl");
559 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
560 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
561 return SourceMgr.getBufferName(ND->getLocation());
562}
563
Steve Narofff334b4e2009-09-02 18:26:48 +0000564const char *clang_getCursorSpelling(CXCursor C)
565{
566 assert(C.decl && "CXCursor has null decl");
567 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
568
569 if (clang_isReference(C.kind)) {
570 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000571 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000572 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
573 assert(OID && "clang_getCursorLine(): Missing interface decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000574 return OID->getSuperClass()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000575 }
576 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000577 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000578 return OID->getIdentifier()->getNameStart();
Steve Naroff85e2db72009-10-01 00:31:07 +0000579 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000580 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
581 assert(OID && "clang_getCursorLine(): Missing category decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000582 return OID->getClassInterface()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000583 }
584 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000585 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
586 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000587 return OID->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000588 }
589 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000590 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
591 static_cast<Stmt *>(C.stmt));
592 assert(OME && "clang_getCursorLine(): Missing message expr");
593 return OME->getSelector().getAsString().c_str();
Steve Naroffbade7de2009-10-19 13:41:39 +0000594 }
Steve Narofffb570422009-09-22 19:25:29 +0000595 case CXCursor_VarRef:
596 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000597 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000598 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
599 static_cast<Stmt *>(C.stmt));
600 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Daniel Dunbare013d682009-10-18 20:26:12 +0000601 return DRE->getDecl()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000602 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000603 default:
604 return "<not implemented>";
605 }
606 }
607 return clang_getDeclSpelling(C.decl);
608}
609
610const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000611{
Steve Naroff89922f82009-08-31 00:59:03 +0000612 switch (Kind) {
613 case CXCursor_FunctionDecl: return "FunctionDecl";
614 case CXCursor_TypedefDecl: return "TypedefDecl";
615 case CXCursor_EnumDecl: return "EnumDecl";
616 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000617 case CXCursor_StructDecl: return "StructDecl";
618 case CXCursor_UnionDecl: return "UnionDecl";
619 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000620 case CXCursor_FieldDecl: return "FieldDecl";
621 case CXCursor_VarDecl: return "VarDecl";
622 case CXCursor_ParmDecl: return "ParmDecl";
623 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
624 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
625 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
626 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
627 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000628 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
629 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
630 case CXCursor_FunctionDefn: return "FunctionDefn";
631 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
632 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
633 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
634 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000635 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000636 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000637 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000638 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
639
640 case CXCursor_VarRef: return "VarRef";
641 case CXCursor_FunctionRef: return "FunctionRef";
642 case CXCursor_EnumConstantRef: return "EnumConstantRef";
643 case CXCursor_MemberRef: return "MemberRef";
644
Steve Naroff77128dd2009-09-15 20:25:34 +0000645 case CXCursor_InvalidFile: return "InvalidFile";
646 case CXCursor_NoDeclFound: return "NoDeclFound";
647 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000648 default: return "<not implemented>";
649 }
Steve Naroff600866c2009-08-27 19:51:58 +0000650}
Steve Naroff89922f82009-08-31 00:59:03 +0000651
Steve Naroff9efa7672009-09-04 15:44:05 +0000652static enum CXCursorKind TranslateKind(Decl *D) {
653 switch (D->getKind()) {
654 case Decl::Function: return CXCursor_FunctionDecl;
655 case Decl::Typedef: return CXCursor_TypedefDecl;
656 case Decl::Enum: return CXCursor_EnumDecl;
657 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
658 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
659 case Decl::Field: return CXCursor_FieldDecl;
660 case Decl::Var: return CXCursor_VarDecl;
661 case Decl::ParmVar: return CXCursor_ParmDecl;
662 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000663 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
664 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000665 case Decl::ObjCMethod: {
666 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
667 if (MD->isInstanceMethod())
668 return CXCursor_ObjCInstanceMethodDecl;
669 return CXCursor_ObjCClassMethodDecl;
670 }
671 default: break;
672 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000673 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000674}
Steve Naroff600866c2009-08-27 19:51:58 +0000675//
676// CXCursor Operations.
677//
Steve Naroff9efa7672009-09-04 15:44:05 +0000678CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000679 unsigned line, unsigned column,
680 CXDecl RelativeToDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000681{
Steve Naroff9efa7672009-09-04 15:44:05 +0000682 assert(CTUnit && "Passed null CXTranslationUnit");
683 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
684
685 FileManager &FMgr = CXXUnit->getFileManager();
686 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000687 source_name+strlen(source_name));
688 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000689 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000690 return C;
691 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000692 SourceLocation SLoc =
693 CXXUnit->getSourceManager().getLocation(File, line, column);
694
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000695 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
696 static_cast<NamedDecl *>(RelativeToDecl));
Steve Naroff9efa7672009-09-04 15:44:05 +0000697
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000698 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000699 if (ALoc.isNamedRef())
700 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000701 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000702 if (Dcl) {
703 if (Stm) {
704 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
705 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
706 return C;
707 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
708 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
709 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000710 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000711 // Fall through...treat as a decl, not a ref.
712 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000713 if (ALoc.isNamedRef()) {
714 if (isa<ObjCInterfaceDecl>(Dcl)) {
715 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
716 return C;
717 }
718 if (isa<ObjCProtocolDecl>(Dcl)) {
719 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
720 return C;
721 }
722 }
723 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000724 return C;
725 }
Steve Narofffb570422009-09-22 19:25:29 +0000726 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000727 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000728}
729
Steve Naroff77128dd2009-09-15 20:25:34 +0000730CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
731{
732 assert(AnonDecl && "Passed null CXDecl");
733 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
734
Steve Narofffb570422009-09-22 19:25:29 +0000735 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000736 return C;
737}
738
739unsigned clang_isInvalid(enum CXCursorKind K)
740{
741 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
742}
743
Steve Naroff89922f82009-08-31 00:59:03 +0000744unsigned clang_isDeclaration(enum CXCursorKind K)
745{
746 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
747}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000748
Steve Narofff334b4e2009-09-02 18:26:48 +0000749unsigned clang_isReference(enum CXCursorKind K)
750{
751 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
752}
753
754unsigned clang_isDefinition(enum CXCursorKind K)
755{
756 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
757}
758
Steve Naroff9efa7672009-09-04 15:44:05 +0000759CXCursorKind clang_getCursorKind(CXCursor C)
760{
761 return C.kind;
762}
763
Steve Naroff699a07d2009-09-25 21:32:34 +0000764static Decl *getDeclFromExpr(Stmt *E) {
765 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
766 return RefExpr->getDecl();
767 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
768 return ME->getMemberDecl();
769 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
770 return RE->getDecl();
771
772 if (CallExpr *CE = dyn_cast<CallExpr>(E))
773 return getDeclFromExpr(CE->getCallee());
774 if (CastExpr *CE = dyn_cast<CastExpr>(E))
775 return getDeclFromExpr(CE->getSubExpr());
776 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
777 return OME->getMethodDecl();
778
779 return 0;
780}
781
Steve Naroff9efa7672009-09-04 15:44:05 +0000782CXDecl clang_getCursorDecl(CXCursor C)
783{
Steve Naroff699a07d2009-09-25 21:32:34 +0000784 if (clang_isDeclaration(C.kind))
785 return C.decl;
786
787 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000788 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000789 if (C.kind == CXCursor_ObjCClassRef ||
790 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000791 return static_cast<Stmt *>(C.stmt);
792 else
793 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
794 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000795 return C.decl;
796 }
797 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000798}
799
Steve Naroff85e2db72009-10-01 00:31:07 +0000800
Steve Narofff334b4e2009-09-02 18:26:48 +0000801static SourceLocation getLocationFromCursor(CXCursor C,
802 SourceManager &SourceMgr,
803 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000804 if (clang_isReference(C.kind)) {
805 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000806 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000807 if (isa<ObjCInterfaceDecl>(ND)) {
808 // FIXME: This is a hack (storing the parent decl in the stmt slot).
809 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
810 return parentDecl->getLocation();
811 }
812 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
813 assert(OID && "clang_getCursorLine(): Missing category decl");
814 return OID->getClassInterface()->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000815 }
816 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000817 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
818 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000819 return OID->getSuperClassLoc();
Steve Naroffbade7de2009-10-19 13:41:39 +0000820 }
821 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000822 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
823 assert(OID && "clang_getCursorLine(): Missing protocol decl");
824 return OID->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000825 }
826 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000827 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
828 static_cast<Stmt *>(C.stmt));
829 assert(OME && "clang_getCursorLine(): Missing message expr");
830 return OME->getLeftLoc(); /* FIXME: should be a range */
Steve Naroffbade7de2009-10-19 13:41:39 +0000831 }
Steve Narofffb570422009-09-22 19:25:29 +0000832 case CXCursor_VarRef:
833 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000834 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000835 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
836 static_cast<Stmt *>(C.stmt));
837 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
838 return DRE->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000839 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000840 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000841 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000842 }
843 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000844 SourceLocation SLoc;
845 switch (ND->getKind()) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000846 case Decl::ObjCInterface: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000847 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
848 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000849 }
850 case Decl::ObjCProtocol: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000851 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
852 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000853 }
854 default: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000855 SLoc = ND->getLocation();
856 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000857 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000858 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000859 if (SLoc.isInvalid())
860 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000861 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000862 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000863}
864
Steve Naroff2d4d6292009-08-31 14:26:51 +0000865unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000866{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000867 assert(C.decl && "CXCursor has null decl");
868 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000869 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000870
871 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000872 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000873}
Steve Narofff334b4e2009-09-02 18:26:48 +0000874
Steve Naroff2d4d6292009-08-31 14:26:51 +0000875unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000876{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000877 assert(C.decl && "CXCursor has null decl");
878 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000879 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000880
881 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000882 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000883}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000884const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000885{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000886 assert(C.decl && "CXCursor has null decl");
887 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000888 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000889
890 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000891 if (SLoc.isFileID())
892 return SourceMgr.getBufferName(SLoc);
893
894 // Retrieve the file in which the macro was instantiated, then provide that
895 // buffer name.
896 // FIXME: Do we want to give specific macro-instantiation information?
897 const llvm::MemoryBuffer *Buffer
898 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
899 if (!Buffer)
900 return 0;
901
902 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000903}
904
Steve Naroff4ade6d62009-09-23 17:52:52 +0000905void clang_getDefinitionSpellingAndExtent(CXCursor C,
906 const char **startBuf,
907 const char **endBuf,
908 unsigned *startLine,
909 unsigned *startColumn,
910 unsigned *endLine,
911 unsigned *endColumn)
912{
913 assert(C.decl && "CXCursor has null decl");
914 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
915 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
916 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
917
918 SourceManager &SM = FD->getASTContext().getSourceManager();
919 *startBuf = SM.getCharacterData(Body->getLBracLoc());
920 *endBuf = SM.getCharacterData(Body->getRBracLoc());
921 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
922 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
923 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
924 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
925}
926
927
Steve Naroff600866c2009-08-27 19:51:58 +0000928} // end extern "C"