blob: 71d76536c4b173751b493fd8f41fca75a63dcbd9 [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 Kremenek379afec2009-10-22 03:24:01 +000030#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000031
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000032#include <cstdio>
Ted Kremenek49358d82009-10-19 21:17:25 +000033#include <vector>
34
Benjamin Kramer20d75812009-10-18 16:13:48 +000035#ifdef LLVM_ON_WIN32
36#define WIN32_LEAN_AND_MEAN
37#include <windows.h>
38#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +000039#include <dlfcn.h>
Daniel Dunbara47dd192009-10-17 23:53:11 +000040#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +000041
Steve Naroff50398192009-08-28 15:28:48 +000042using namespace clang;
43using namespace idx;
44
Steve Naroff89922f82009-08-31 00:59:03 +000045namespace {
Steve Naroff4ade6d62009-09-23 17:52:52 +000046static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
47{
48 NamedDecl *D = DRE->getDecl();
49 if (isa<VarDecl>(D))
50 return CXCursor_VarRef;
51 else if (isa<FunctionDecl>(D))
52 return CXCursor_FunctionRef;
53 else if (isa<EnumConstantDecl>(D))
54 return CXCursor_EnumConstantRef;
55 else
56 return CXCursor_NotImplemented;
57}
58
59#if 0
60// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000061class CRefVisitor : public StmtVisitor<CRefVisitor> {
62 CXDecl CDecl;
63 CXDeclIterator Callback;
64 CXClientData CData;
65
66 void Call(enum CXCursorKind CK, Stmt *SRef) {
67 CXCursor C = { CK, CDecl, SRef };
68 Callback(CDecl, C, CData);
69 }
70
71public:
72 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
73 CDecl(C), Callback(cback), CData(D) {}
74
75 void VisitStmt(Stmt *S) {
76 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
77 C != CEnd; ++C)
78 Visit(*C);
79 }
80 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000081 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000082 }
83 void VisitMemberExpr(MemberExpr *Node) {
84 Call(CXCursor_MemberRef, Node);
85 }
86 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
87 Call(CXCursor_ObjCSelectorRef, Node);
88 }
89 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
90 Call(CXCursor_ObjCIvarRef, Node);
91 }
92};
Steve Naroff4ade6d62009-09-23 17:52:52 +000093#endif
Ted Kremenekfc062212009-10-19 21:44:57 +000094
95/// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted
96/// warnings and errors.
97class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient {
98public:
99 virtual ~IgnoreDiagnosticsClient() {}
100 virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {}
101};
Steve Narofffb570422009-09-22 19:25:29 +0000102
Steve Naroff89922f82009-08-31 00:59:03 +0000103// Translation Unit Visitor.
104class TUVisitor : public DeclVisitor<TUVisitor> {
105 CXTranslationUnit TUnit;
106 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000107 CXClientData CData;
108
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000109 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
110 // to the visitor. Declarations with a PCH level greater than this value will
111 // be suppressed.
112 unsigned MaxPCHLevel;
113
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000114 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000115 // Filter any declarations that have a PCH level greater than what we allow.
116 if (ND->getPCHLevel() > MaxPCHLevel)
117 return;
118
Steve Narofff96b5242009-10-28 20:44:47 +0000119 // Filter any implicit declarations (since the source info will be bogus).
120 if (ND->isImplicit())
121 return;
122
Steve Narofffb570422009-09-22 19:25:29 +0000123 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000124 Callback(TUnit, C, CData);
125 }
Steve Naroff89922f82009-08-31 00:59:03 +0000126public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000127 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000128 CXTranslationUnitIterator cback, CXClientData D,
129 unsigned MaxPCHLevel) :
130 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000131
132 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
133 VisitDeclContext(dyn_cast<DeclContext>(D));
134 }
135 void VisitDeclContext(DeclContext *DC) {
136 for (DeclContext::decl_iterator
137 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
138 Visit(*I);
139 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000140 void VisitTypedefDecl(TypedefDecl *ND) {
141 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000142 }
143 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000144 switch (ND->getTagKind()) {
145 case TagDecl::TK_struct:
146 Call(CXCursor_StructDecl, ND);
147 break;
148 case TagDecl::TK_class:
149 Call(CXCursor_ClassDecl, ND);
150 break;
151 case TagDecl::TK_union:
152 Call(CXCursor_UnionDecl, ND);
153 break;
154 case TagDecl::TK_enum:
155 Call(CXCursor_EnumDecl, ND);
156 break;
157 }
Steve Naroff89922f82009-08-31 00:59:03 +0000158 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000159 void VisitVarDecl(VarDecl *ND) {
160 Call(CXCursor_VarDecl, ND);
161 }
Steve Naroff89922f82009-08-31 00:59:03 +0000162 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000163 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
164 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000165 }
166 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000167 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000168 }
169 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000170 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000171 }
172 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000173 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000174 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000175 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
176 Call(CXCursor_ObjCClassDefn, ND);
177 }
178 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
179 Call(CXCursor_ObjCCategoryDefn, ND);
180 }
Steve Naroff89922f82009-08-31 00:59:03 +0000181};
182
Steve Naroffc857ea42009-09-02 13:28:54 +0000183// Declaration visitor.
184class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
185 CXDecl CDecl;
186 CXDeclIterator Callback;
187 CXClientData CData;
188
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000189 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
190 // to the visitor. Declarations with a PCH level greater than this value will
191 // be suppressed.
192 unsigned MaxPCHLevel;
193
Steve Naroffc857ea42009-09-02 13:28:54 +0000194 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000195 // Disable the callback when the context is equal to the visiting decl.
196 if (CDecl == ND && !clang_isReference(CK))
197 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000198
199 // Filter any declarations that have a PCH level greater than what we allow.
200 if (ND->getPCHLevel() > MaxPCHLevel)
201 return;
202
Steve Narofffb570422009-09-22 19:25:29 +0000203 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000204 Callback(CDecl, C, CData);
205 }
Steve Naroff89922f82009-08-31 00:59:03 +0000206public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000207 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
208 unsigned MaxPCHLevel) :
209 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000210
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000211 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
212 // Issue callbacks for the containing class.
213 Call(CXCursor_ObjCClassRef, ND);
214 // FIXME: Issue callbacks for protocol refs.
215 VisitDeclContext(dyn_cast<DeclContext>(ND));
216 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000217 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000218 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000219 if (D->getSuperClass())
220 Call(CXCursor_ObjCSuperClassRef, D);
221
Steve Naroff9efa7672009-09-04 15:44:05 +0000222 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
223 E = D->protocol_end(); I != E; ++I)
224 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000225 VisitDeclContext(dyn_cast<DeclContext>(D));
226 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000227 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
228 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
229 E = PID->protocol_end(); I != E; ++I)
230 Call(CXCursor_ObjCProtocolRef, *I);
231
232 VisitDeclContext(dyn_cast<DeclContext>(PID));
233 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000234 void VisitTagDecl(TagDecl *D) {
235 VisitDeclContext(dyn_cast<DeclContext>(D));
236 }
237 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
238 VisitDeclContext(dyn_cast<DeclContext>(D));
239 }
240 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
241 VisitDeclContext(dyn_cast<DeclContext>(D));
242 }
Steve Naroff89922f82009-08-31 00:59:03 +0000243 void VisitDeclContext(DeclContext *DC) {
244 for (DeclContext::decl_iterator
245 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
246 Visit(*I);
247 }
248 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000249 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000250 }
251 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000252 Call(CXCursor_FieldDecl, ND);
253 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000254 void VisitVarDecl(VarDecl *ND) {
255 Call(CXCursor_VarDecl, ND);
256 }
257 void VisitParmVarDecl(ParmVarDecl *ND) {
258 Call(CXCursor_ParmDecl, ND);
259 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000260 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
261 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000262 }
263 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000264 Call(CXCursor_ObjCIvarDecl, ND);
265 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000266 void VisitFunctionDecl(FunctionDecl *ND) {
267 if (ND->isThisDeclarationADefinition()) {
268 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000269#if 0
270 // Not currently needed.
271 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000272 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000273 RVisit.Visit(Body);
274#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000275 }
276 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000277 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
278 if (ND->getBody()) {
279 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
280 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000281 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000282 } else
283 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
284 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000285 }
286};
287
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000288class CIndexer : public Indexer {
289public:
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000290 explicit CIndexer(Program *prog) : Indexer(*prog),
291 OnlyLocalDecls(false),
292 DisplayDiagnostics(false) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000293
294 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000295
296 /// \brief Whether we only want to see "local" declarations (that did not
297 /// come from a previous precompiled header). If false, we want to see all
298 /// declarations.
299 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
300 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000301
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000302 void setDisplayDiagnostics(bool Display = true) {
303 DisplayDiagnostics = Display;
304 }
305 bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
306
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000307 /// \brief Get the path of the clang binary.
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000308 const llvm::sys::Path& getClangPath();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000309private:
310 bool OnlyLocalDecls;
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000311 bool DisplayDiagnostics;
312
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000313 llvm::sys::Path ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000314};
Steve Naroff89922f82009-08-31 00:59:03 +0000315
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000316const llvm::sys::Path& CIndexer::getClangPath() {
317 // Did we already compute the path?
318 if (!ClangPath.empty())
319 return ClangPath;
320
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000321 // Find the location where this library lives (libCIndex.dylib).
Benjamin Kramer20d75812009-10-18 16:13:48 +0000322#ifdef LLVM_ON_WIN32
323 MEMORY_BASIC_INFORMATION mbi;
324 char path[MAX_PATH];
Benjamin Krameredcd8282009-10-18 16:20:58 +0000325 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
Benjamin Kramer20d75812009-10-18 16:13:48 +0000326 sizeof(mbi));
327 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
328
329 llvm::sys::Path CIndexPath(path);
330#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000331 // This silly cast below avoids a C++ warning.
332 Dl_info info;
333 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
334 assert(0 && "Call to dladdr() failed");
335
336 llvm::sys::Path CIndexPath(info.dli_fname);
Daniel Dunbara47dd192009-10-17 23:53:11 +0000337#endif
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000338
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000339 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000340 CIndexPath.eraseComponent();
341 CIndexPath.eraseComponent();
342 CIndexPath.appendComponent("bin");
343 CIndexPath.appendComponent("clang");
344
345 // Cache our result.
346 ClangPath = CIndexPath;
347 return ClangPath;
348}
349
350}
351
352extern "C" {
353
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000354CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
355 int displayDiagnostics)
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000356{
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000357 CIndexer *CIdxr = new CIndexer(new Program());
358 if (excludeDeclarationsFromPCH)
359 CIdxr->setOnlyLocalDecls();
360 if (displayDiagnostics)
361 CIdxr->setDisplayDiagnostics();
362 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000363}
364
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000365void clang_disposeIndex(CXIndex CIdx)
366{
367 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000368 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000369}
370
Steve Naroff50398192009-08-28 15:28:48 +0000371// FIXME: need to pass back error info.
372CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000373 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000374{
Steve Naroff50398192009-08-28 15:28:48 +0000375 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000376 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000377 std::string astName(ast_filename);
378 std::string ErrMsg;
379
Ted Kremenek379afec2009-10-22 03:24:01 +0000380 CXTranslationUnit TU =
381 ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
382 CXXIdx->getDisplayDiagnostics() ?
383 NULL : new IgnoreDiagnosticsClient(),
384 CXXIdx->getOnlyLocalDecls(),
385 /* UseBumpAllocator = */ true);
386
Ted Kremenek779e5f42009-10-26 22:08:39 +0000387 if (!ErrMsg.empty())
388 llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000389
390 return TU;
Steve Naroff600866c2009-08-27 19:51:58 +0000391}
392
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000393CXTranslationUnit clang_createTranslationUnitFromSourceFile(
394 CXIndex CIdx,
395 const char *source_filename,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000396 int num_command_line_args, const char **command_line_args) {
397 assert(CIdx && "Passed null CXIndex");
398 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
399
Ted Kremenek139ba862009-10-22 00:03:57 +0000400 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000401 std::vector<const char *> argv;
Ted Kremenek139ba862009-10-22 00:03:57 +0000402
403 // First add the complete path to the 'clang' executable.
404 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000405 argv.push_back(ClangPath.c_str());
Ted Kremenek139ba862009-10-22 00:03:57 +0000406
407 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000408 argv.push_back("-emit-ast");
Ted Kremenek139ba862009-10-22 00:03:57 +0000409
410 // The 'source_filename' argument is optional. If the caller does not
411 // specify it then it is assumed that the source file is specified
412 // in the actual argument list.
413 if (source_filename)
414 argv.push_back(source_filename);
415
Steve Naroff37b5ac22009-10-15 20:50:09 +0000416 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000417 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000418 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000419 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000420
421 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
422 for (int i = 0; i < num_command_line_args; ++i)
423 if (const char *arg = command_line_args[i]) {
424 if (strcmp(arg, "-o") == 0) {
425 ++i; // Also skip the matching argument.
426 continue;
427 }
428 if (strcmp(arg, "-emit-ast") == 0 ||
429 strcmp(arg, "-c") == 0 ||
430 strcmp(arg, "-fsyntax-only") == 0) {
431 continue;
432 }
433
434 // Keep the argument.
435 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000436 }
Ted Kremenek139ba862009-10-22 00:03:57 +0000437
438 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000439 argv.push_back(NULL);
440
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000441 // Invoke 'clang'.
442 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
443 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000444 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000445 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000446 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
447 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
448 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
449
Ted Kremenek8ee1f3f2009-10-22 22:19:00 +0000450 if (!ErrMsg.empty()) {
Ted Kremenek379afec2009-10-22 03:24:01 +0000451 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
452 << '\n' << "Arguments: \n";
453 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000454 I!=E; ++I) {
455 if (*I)
456 llvm::errs() << ' ' << *I << '\n';
457 }
458 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000459 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000460
Steve Naroff37b5ac22009-10-15 20:50:09 +0000461 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000462 ASTUnit *ATU = static_cast<ASTUnit *>(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000463 clang_createTranslationUnit(CIdx, astTmpFile));
464 if (ATU)
465 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000466 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000467}
468
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000469void clang_disposeTranslationUnit(
470 CXTranslationUnit CTUnit)
471{
472 assert(CTUnit && "Passed null CXTranslationUnit");
473 delete static_cast<ASTUnit *>(CTUnit);
474}
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000475
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000476const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
477{
478 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000479 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
480 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000481}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000482
Steve Naroffc857ea42009-09-02 13:28:54 +0000483void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
484 CXTranslationUnitIterator callback,
485 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000486{
Steve Naroff50398192009-08-28 15:28:48 +0000487 assert(CTUnit && "Passed null CXTranslationUnit");
488 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
489 ASTContext &Ctx = CXXUnit->getASTContext();
490
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000491 TUVisitor DVisit(CTUnit, callback, CData,
492 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000493 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000494}
495
Steve Naroffc857ea42009-09-02 13:28:54 +0000496void clang_loadDeclaration(CXDecl Dcl,
497 CXDeclIterator callback,
498 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000499{
Steve Naroffc857ea42009-09-02 13:28:54 +0000500 assert(Dcl && "Passed null CXDecl");
501
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000502 CDeclVisitor DVisit(Dcl, callback, CData,
503 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000504 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000505}
506
Steve Naroff7e8f8182009-08-28 12:07:44 +0000507// Some notes on CXEntity:
508//
509// - Since the 'ordinary' namespace includes functions, data, typedefs,
510// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
511// entity for 2 different types). For example:
512//
513// module1.m: @interface Foo @end Foo *x;
514// module2.m: void Foo(int);
515//
516// - Since the unique name spans translation units, static data/functions
517// within a CXTranslationUnit are *not* currently represented by entities.
518// As a result, there will be no entity for the following:
519//
520// module.m: static void Foo() { }
521//
522
523
Steve Naroff600866c2009-08-27 19:51:58 +0000524const char *clang_getDeclarationName(CXEntity)
525{
526 return "";
527}
528const char *clang_getURI(CXEntity)
529{
530 return "";
531}
532
533CXEntity clang_getEntity(const char *URI)
534{
535 return 0;
536}
537
538//
539// CXDecl Operations.
540//
Steve Naroff600866c2009-08-27 19:51:58 +0000541CXEntity clang_getEntityFromDecl(CXDecl)
542{
543 return 0;
544}
Steve Naroff89922f82009-08-31 00:59:03 +0000545const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000546{
Steve Naroff89922f82009-08-31 00:59:03 +0000547 assert(AnonDecl && "Passed null CXDecl");
548 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000549
550 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
551 return OMD->getSelector().getAsString().c_str();
552 }
Steve Naroff89922f82009-08-31 00:59:03 +0000553 if (ND->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000554 return ND->getIdentifier()->getNameStart();
Steve Naroffc857ea42009-09-02 13:28:54 +0000555 else
Steve Naroff89922f82009-08-31 00:59:03 +0000556 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000557}
Steve Narofff334b4e2009-09-02 18:26:48 +0000558
Steve Naroff699a07d2009-09-25 21:32:34 +0000559unsigned clang_getDeclLine(CXDecl AnonDecl)
560{
561 assert(AnonDecl && "Passed null CXDecl");
562 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
563 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
564 return SourceMgr.getSpellingLineNumber(ND->getLocation());
565}
566
567unsigned clang_getDeclColumn(CXDecl AnonDecl)
568{
569 assert(AnonDecl && "Passed null CXDecl");
570 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
571 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000572 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000573}
574
Steve Naroffee9405e2009-09-25 21:45:39 +0000575const char *clang_getDeclSource(CXDecl AnonDecl)
576{
577 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff88145032009-10-27 14:35:18 +0000578 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
579 assert (FEnt && "Cannot find FileEntry for Decl");
580 return clang_getFileName(FEnt);
581}
582
583static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
584 SourceLocation SLoc)
585{
586 FileID FID;
587 if (SLoc.isFileID())
588 FID = SMgr.getFileID(SLoc);
589 else
590 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
591 return SMgr.getFileEntryForID(FID);
592}
593
594CXFile clang_getDeclSourceFile(CXDecl AnonDecl)
595{
596 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000597 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
598 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000599 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
600}
601
602const char *clang_getFileName(CXFile SFile) {
603 assert(SFile && "Passed null CXFile");
604 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
605 return FEnt->getName();
606}
607
608time_t clang_getFileTime(CXFile SFile) {
609 assert(SFile && "Passed null CXFile");
610 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
611 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000612}
613
Steve Narofff334b4e2009-09-02 18:26:48 +0000614const char *clang_getCursorSpelling(CXCursor C)
615{
616 assert(C.decl && "CXCursor has null decl");
617 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
618
619 if (clang_isReference(C.kind)) {
620 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000621 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000622 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
623 assert(OID && "clang_getCursorLine(): Missing interface decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000624 return OID->getSuperClass()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000625 }
626 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000627 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000628 return OID->getIdentifier()->getNameStart();
Steve Naroff85e2db72009-10-01 00:31:07 +0000629 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000630 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
631 assert(OID && "clang_getCursorLine(): Missing category decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000632 return OID->getClassInterface()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000633 }
634 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000635 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
636 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000637 return OID->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000638 }
639 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000640 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
641 static_cast<Stmt *>(C.stmt));
642 assert(OME && "clang_getCursorLine(): Missing message expr");
643 return OME->getSelector().getAsString().c_str();
Steve Naroffbade7de2009-10-19 13:41:39 +0000644 }
Steve Narofffb570422009-09-22 19:25:29 +0000645 case CXCursor_VarRef:
646 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000647 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000648 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
649 static_cast<Stmt *>(C.stmt));
650 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Daniel Dunbare013d682009-10-18 20:26:12 +0000651 return DRE->getDecl()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000652 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000653 default:
654 return "<not implemented>";
655 }
656 }
657 return clang_getDeclSpelling(C.decl);
658}
659
660const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000661{
Steve Naroff89922f82009-08-31 00:59:03 +0000662 switch (Kind) {
663 case CXCursor_FunctionDecl: return "FunctionDecl";
664 case CXCursor_TypedefDecl: return "TypedefDecl";
665 case CXCursor_EnumDecl: return "EnumDecl";
666 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000667 case CXCursor_StructDecl: return "StructDecl";
668 case CXCursor_UnionDecl: return "UnionDecl";
669 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000670 case CXCursor_FieldDecl: return "FieldDecl";
671 case CXCursor_VarDecl: return "VarDecl";
672 case CXCursor_ParmDecl: return "ParmDecl";
673 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
674 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
675 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
676 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
677 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000678 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
679 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
680 case CXCursor_FunctionDefn: return "FunctionDefn";
681 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
682 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
683 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
684 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000685 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000686 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000687 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000688 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
689
690 case CXCursor_VarRef: return "VarRef";
691 case CXCursor_FunctionRef: return "FunctionRef";
692 case CXCursor_EnumConstantRef: return "EnumConstantRef";
693 case CXCursor_MemberRef: return "MemberRef";
694
Steve Naroff77128dd2009-09-15 20:25:34 +0000695 case CXCursor_InvalidFile: return "InvalidFile";
696 case CXCursor_NoDeclFound: return "NoDeclFound";
697 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000698 default: return "<not implemented>";
699 }
Steve Naroff600866c2009-08-27 19:51:58 +0000700}
Steve Naroff89922f82009-08-31 00:59:03 +0000701
Steve Naroff9efa7672009-09-04 15:44:05 +0000702static enum CXCursorKind TranslateKind(Decl *D) {
703 switch (D->getKind()) {
704 case Decl::Function: return CXCursor_FunctionDecl;
705 case Decl::Typedef: return CXCursor_TypedefDecl;
706 case Decl::Enum: return CXCursor_EnumDecl;
707 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
708 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
709 case Decl::Field: return CXCursor_FieldDecl;
710 case Decl::Var: return CXCursor_VarDecl;
711 case Decl::ParmVar: return CXCursor_ParmDecl;
712 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000713 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
714 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000715 case Decl::ObjCMethod: {
716 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
717 if (MD->isInstanceMethod())
718 return CXCursor_ObjCInstanceMethodDecl;
719 return CXCursor_ObjCClassMethodDecl;
720 }
721 default: break;
722 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000723 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000724}
Steve Naroff600866c2009-08-27 19:51:58 +0000725//
726// CXCursor Operations.
727//
Steve Naroff9efa7672009-09-04 15:44:05 +0000728CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Narofff96b5242009-10-28 20:44:47 +0000729 unsigned line, unsigned column)
Steve Naroff600866c2009-08-27 19:51:58 +0000730{
Steve Naroff9efa7672009-09-04 15:44:05 +0000731 assert(CTUnit && "Passed null CXTranslationUnit");
732 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
733
734 FileManager &FMgr = CXXUnit->getFileManager();
735 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000736 source_name+strlen(source_name));
737 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000738 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000739 return C;
740 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000741 SourceLocation SLoc =
742 CXXUnit->getSourceManager().getLocation(File, line, column);
743
Steve Narofff96b5242009-10-28 20:44:47 +0000744 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
745
746 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
747 &LastLoc);
748 if (ALoc.isValid())
749 CXXUnit->setLastASTLocation(ALoc);
750
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000751 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000752 if (ALoc.isNamedRef())
753 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000754 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000755 if (Dcl) {
756 if (Stm) {
757 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
758 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
759 return C;
760 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
761 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
762 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000763 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000764 // Fall through...treat as a decl, not a ref.
765 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000766 if (ALoc.isNamedRef()) {
767 if (isa<ObjCInterfaceDecl>(Dcl)) {
768 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
769 return C;
770 }
771 if (isa<ObjCProtocolDecl>(Dcl)) {
772 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
773 return C;
774 }
775 }
776 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000777 return C;
778 }
Steve Narofffb570422009-09-22 19:25:29 +0000779 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000780 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000781}
782
Steve Naroff77128dd2009-09-15 20:25:34 +0000783CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
784{
785 assert(AnonDecl && "Passed null CXDecl");
786 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
787
Steve Narofffb570422009-09-22 19:25:29 +0000788 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000789 return C;
790}
791
792unsigned clang_isInvalid(enum CXCursorKind K)
793{
794 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
795}
796
Steve Naroff89922f82009-08-31 00:59:03 +0000797unsigned clang_isDeclaration(enum CXCursorKind K)
798{
799 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
800}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000801
Steve Narofff334b4e2009-09-02 18:26:48 +0000802unsigned clang_isReference(enum CXCursorKind K)
803{
804 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
805}
806
807unsigned clang_isDefinition(enum CXCursorKind K)
808{
809 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
810}
811
Steve Naroff9efa7672009-09-04 15:44:05 +0000812CXCursorKind clang_getCursorKind(CXCursor C)
813{
814 return C.kind;
815}
816
Steve Naroff699a07d2009-09-25 21:32:34 +0000817static Decl *getDeclFromExpr(Stmt *E) {
818 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
819 return RefExpr->getDecl();
820 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
821 return ME->getMemberDecl();
822 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
823 return RE->getDecl();
824
825 if (CallExpr *CE = dyn_cast<CallExpr>(E))
826 return getDeclFromExpr(CE->getCallee());
827 if (CastExpr *CE = dyn_cast<CastExpr>(E))
828 return getDeclFromExpr(CE->getSubExpr());
829 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
830 return OME->getMethodDecl();
831
832 return 0;
833}
834
Steve Naroff9efa7672009-09-04 15:44:05 +0000835CXDecl clang_getCursorDecl(CXCursor C)
836{
Steve Naroff699a07d2009-09-25 21:32:34 +0000837 if (clang_isDeclaration(C.kind))
838 return C.decl;
839
840 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000841 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000842 if (C.kind == CXCursor_ObjCClassRef ||
843 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000844 return static_cast<Stmt *>(C.stmt);
845 else
846 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
847 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000848 return C.decl;
849 }
850 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000851}
852
Steve Naroff85e2db72009-10-01 00:31:07 +0000853
Steve Narofff334b4e2009-09-02 18:26:48 +0000854static SourceLocation getLocationFromCursor(CXCursor C,
855 SourceManager &SourceMgr,
856 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000857 if (clang_isReference(C.kind)) {
858 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000859 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000860 if (isa<ObjCInterfaceDecl>(ND)) {
861 // FIXME: This is a hack (storing the parent decl in the stmt slot).
862 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
863 return parentDecl->getLocation();
864 }
865 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
866 assert(OID && "clang_getCursorLine(): Missing category decl");
867 return OID->getClassInterface()->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000868 }
869 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000870 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
871 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000872 return OID->getSuperClassLoc();
Steve Naroffbade7de2009-10-19 13:41:39 +0000873 }
874 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000875 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
876 assert(OID && "clang_getCursorLine(): Missing protocol decl");
877 return OID->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000878 }
879 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000880 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
881 static_cast<Stmt *>(C.stmt));
882 assert(OME && "clang_getCursorLine(): Missing message expr");
883 return OME->getLeftLoc(); /* FIXME: should be a range */
Steve Naroffbade7de2009-10-19 13:41:39 +0000884 }
Steve Narofffb570422009-09-22 19:25:29 +0000885 case CXCursor_VarRef:
886 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000887 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000888 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
889 static_cast<Stmt *>(C.stmt));
890 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
891 return DRE->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000892 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000893 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000894 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000895 }
896 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000897 SourceLocation SLoc;
898 switch (ND->getKind()) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000899 case Decl::ObjCInterface: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000900 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
901 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000902 }
903 case Decl::ObjCProtocol: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000904 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
905 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000906 }
907 default: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000908 SLoc = ND->getLocation();
909 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000910 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000911 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000912 if (SLoc.isInvalid())
913 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000914 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000915 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000916}
917
Steve Naroff2d4d6292009-08-31 14:26:51 +0000918unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000919{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000920 assert(C.decl && "CXCursor has null decl");
921 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000922 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000923
924 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000925 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000926}
Steve Narofff334b4e2009-09-02 18:26:48 +0000927
Steve Naroff2d4d6292009-08-31 14:26:51 +0000928unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000929{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000930 assert(C.decl && "CXCursor has null decl");
931 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000932 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000933
934 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000935 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000936}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000937const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000938{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000939 assert(C.decl && "CXCursor has null decl");
940 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000941 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000942
943 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000944 if (SLoc.isFileID())
945 return SourceMgr.getBufferName(SLoc);
946
947 // Retrieve the file in which the macro was instantiated, then provide that
948 // buffer name.
949 // FIXME: Do we want to give specific macro-instantiation information?
950 const llvm::MemoryBuffer *Buffer
951 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
952 if (!Buffer)
953 return 0;
954
955 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000956}
957
Steve Naroff88145032009-10-27 14:35:18 +0000958CXFile clang_getCursorSourceFile(CXCursor C)
959{
960 assert(C.decl && "CXCursor has null decl");
961 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
962 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
963
964 return (void *)getFileEntryFromSourceLocation(SourceMgr,
965 getLocationFromCursor(C,SourceMgr, ND));
966}
967
Steve Naroff4ade6d62009-09-23 17:52:52 +0000968void clang_getDefinitionSpellingAndExtent(CXCursor C,
969 const char **startBuf,
970 const char **endBuf,
971 unsigned *startLine,
972 unsigned *startColumn,
973 unsigned *endLine,
974 unsigned *endColumn)
975{
976 assert(C.decl && "CXCursor has null decl");
977 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
978 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
979 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
980
981 SourceManager &SM = FD->getASTContext().getSourceManager();
982 *startBuf = SM.getCharacterData(Body->getLBracLoc());
983 *endBuf = SM.getCharacterData(Body->getRBracLoc());
984 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
985 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
986 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
987 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
988}
989
990
Steve Naroff600866c2009-08-27 19:51:58 +0000991} // end extern "C"