blob: 5ffa6d78e8623d078e925bdca123ca4dd1365847 [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 Narofffb570422009-09-22 19:25:29 +0000119 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000120 Callback(TUnit, C, CData);
121 }
Steve Naroff89922f82009-08-31 00:59:03 +0000122public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000123 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000124 CXTranslationUnitIterator cback, CXClientData D,
125 unsigned MaxPCHLevel) :
126 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000127
128 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
129 VisitDeclContext(dyn_cast<DeclContext>(D));
130 }
131 void VisitDeclContext(DeclContext *DC) {
132 for (DeclContext::decl_iterator
133 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
134 Visit(*I);
135 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000136 void VisitTypedefDecl(TypedefDecl *ND) {
137 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000138 }
139 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000140 switch (ND->getTagKind()) {
141 case TagDecl::TK_struct:
142 Call(CXCursor_StructDecl, ND);
143 break;
144 case TagDecl::TK_class:
145 Call(CXCursor_ClassDecl, ND);
146 break;
147 case TagDecl::TK_union:
148 Call(CXCursor_UnionDecl, ND);
149 break;
150 case TagDecl::TK_enum:
151 Call(CXCursor_EnumDecl, ND);
152 break;
153 }
Steve Naroff89922f82009-08-31 00:59:03 +0000154 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000155 void VisitVarDecl(VarDecl *ND) {
156 Call(CXCursor_VarDecl, ND);
157 }
Steve Naroff89922f82009-08-31 00:59:03 +0000158 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000159 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
160 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000161 }
162 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000163 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000164 }
165 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000166 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000167 }
168 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000169 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000170 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000171 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
172 Call(CXCursor_ObjCClassDefn, ND);
173 }
174 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
175 Call(CXCursor_ObjCCategoryDefn, ND);
176 }
Steve Naroff89922f82009-08-31 00:59:03 +0000177};
178
Steve Naroffc857ea42009-09-02 13:28:54 +0000179// Declaration visitor.
180class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
181 CXDecl CDecl;
182 CXDeclIterator Callback;
183 CXClientData CData;
184
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000185 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
186 // to the visitor. Declarations with a PCH level greater than this value will
187 // be suppressed.
188 unsigned MaxPCHLevel;
189
Steve Naroffc857ea42009-09-02 13:28:54 +0000190 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000191 // Disable the callback when the context is equal to the visiting decl.
192 if (CDecl == ND && !clang_isReference(CK))
193 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000194
195 // Filter any declarations that have a PCH level greater than what we allow.
196 if (ND->getPCHLevel() > MaxPCHLevel)
197 return;
198
Steve Narofffb570422009-09-22 19:25:29 +0000199 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000200 Callback(CDecl, C, CData);
201 }
Steve Naroff89922f82009-08-31 00:59:03 +0000202public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000203 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
204 unsigned MaxPCHLevel) :
205 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000206
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000207 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
208 // Issue callbacks for the containing class.
209 Call(CXCursor_ObjCClassRef, ND);
210 // FIXME: Issue callbacks for protocol refs.
211 VisitDeclContext(dyn_cast<DeclContext>(ND));
212 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000213 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000214 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000215 if (D->getSuperClass())
216 Call(CXCursor_ObjCSuperClassRef, D);
217
Steve Naroff9efa7672009-09-04 15:44:05 +0000218 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
219 E = D->protocol_end(); I != E; ++I)
220 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000221 VisitDeclContext(dyn_cast<DeclContext>(D));
222 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000223 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
224 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
225 E = PID->protocol_end(); I != E; ++I)
226 Call(CXCursor_ObjCProtocolRef, *I);
227
228 VisitDeclContext(dyn_cast<DeclContext>(PID));
229 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000230 void VisitTagDecl(TagDecl *D) {
231 VisitDeclContext(dyn_cast<DeclContext>(D));
232 }
233 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
234 VisitDeclContext(dyn_cast<DeclContext>(D));
235 }
236 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
237 VisitDeclContext(dyn_cast<DeclContext>(D));
238 }
Steve Naroff89922f82009-08-31 00:59:03 +0000239 void VisitDeclContext(DeclContext *DC) {
240 for (DeclContext::decl_iterator
241 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
242 Visit(*I);
243 }
244 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000245 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000246 }
247 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000248 Call(CXCursor_FieldDecl, ND);
249 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000250 void VisitVarDecl(VarDecl *ND) {
251 Call(CXCursor_VarDecl, ND);
252 }
253 void VisitParmVarDecl(ParmVarDecl *ND) {
254 Call(CXCursor_ParmDecl, ND);
255 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000256 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
257 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000258 }
259 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000260 Call(CXCursor_ObjCIvarDecl, ND);
261 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000262 void VisitFunctionDecl(FunctionDecl *ND) {
263 if (ND->isThisDeclarationADefinition()) {
264 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000265#if 0
266 // Not currently needed.
267 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000268 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000269 RVisit.Visit(Body);
270#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000271 }
272 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000273 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
274 if (ND->getBody()) {
275 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
276 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000277 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000278 } else
279 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
280 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000281 }
282};
283
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000284class CIndexer : public Indexer {
285public:
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000286 explicit CIndexer(Program *prog) : Indexer(*prog),
287 OnlyLocalDecls(false),
288 DisplayDiagnostics(false) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000289
290 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000291
292 /// \brief Whether we only want to see "local" declarations (that did not
293 /// come from a previous precompiled header). If false, we want to see all
294 /// declarations.
295 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
296 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000297
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000298 void setDisplayDiagnostics(bool Display = true) {
299 DisplayDiagnostics = Display;
300 }
301 bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
302
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000303 /// \brief Get the path of the clang binary.
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000304 const llvm::sys::Path& getClangPath();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000305private:
306 bool OnlyLocalDecls;
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000307 bool DisplayDiagnostics;
308
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000309 llvm::sys::Path ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000310};
Steve Naroff89922f82009-08-31 00:59:03 +0000311
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000312const llvm::sys::Path& CIndexer::getClangPath() {
313 // Did we already compute the path?
314 if (!ClangPath.empty())
315 return ClangPath;
316
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000317 // Find the location where this library lives (libCIndex.dylib).
Benjamin Kramer20d75812009-10-18 16:13:48 +0000318#ifdef LLVM_ON_WIN32
319 MEMORY_BASIC_INFORMATION mbi;
320 char path[MAX_PATH];
Benjamin Krameredcd8282009-10-18 16:20:58 +0000321 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
Benjamin Kramer20d75812009-10-18 16:13:48 +0000322 sizeof(mbi));
323 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
324
325 llvm::sys::Path CIndexPath(path);
326#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000327 // This silly cast below avoids a C++ warning.
328 Dl_info info;
329 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
330 assert(0 && "Call to dladdr() failed");
331
332 llvm::sys::Path CIndexPath(info.dli_fname);
Daniel Dunbara47dd192009-10-17 23:53:11 +0000333#endif
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000334
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000335 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000336 CIndexPath.eraseComponent();
337 CIndexPath.eraseComponent();
338 CIndexPath.appendComponent("bin");
339 CIndexPath.appendComponent("clang");
340
341 // Cache our result.
342 ClangPath = CIndexPath;
343 return ClangPath;
344}
345
346}
347
348extern "C" {
349
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000350CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
351 int displayDiagnostics)
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000352{
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000353 CIndexer *CIdxr = new CIndexer(new Program());
354 if (excludeDeclarationsFromPCH)
355 CIdxr->setOnlyLocalDecls();
356 if (displayDiagnostics)
357 CIdxr->setDisplayDiagnostics();
358 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000359}
360
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000361void clang_disposeIndex(CXIndex CIdx)
362{
363 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000364 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000365}
366
Steve Naroff50398192009-08-28 15:28:48 +0000367// FIXME: need to pass back error info.
368CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000369 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000370{
Steve Naroff50398192009-08-28 15:28:48 +0000371 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000372 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000373 std::string astName(ast_filename);
374 std::string ErrMsg;
375
Ted Kremenek379afec2009-10-22 03:24:01 +0000376 CXTranslationUnit TU =
377 ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
378 CXXIdx->getDisplayDiagnostics() ?
379 NULL : new IgnoreDiagnosticsClient(),
380 CXXIdx->getOnlyLocalDecls(),
381 /* UseBumpAllocator = */ true);
382
Ted Kremenek779e5f42009-10-26 22:08:39 +0000383 if (!ErrMsg.empty())
384 llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000385
386 return TU;
Steve Naroff600866c2009-08-27 19:51:58 +0000387}
388
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000389CXTranslationUnit clang_createTranslationUnitFromSourceFile(
390 CXIndex CIdx,
391 const char *source_filename,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000392 int num_command_line_args, const char **command_line_args) {
393 assert(CIdx && "Passed null CXIndex");
394 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
395
Ted Kremenek139ba862009-10-22 00:03:57 +0000396 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000397 std::vector<const char *> argv;
Ted Kremenek139ba862009-10-22 00:03:57 +0000398
399 // First add the complete path to the 'clang' executable.
400 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000401 argv.push_back(ClangPath.c_str());
Ted Kremenek139ba862009-10-22 00:03:57 +0000402
403 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000404 argv.push_back("-emit-ast");
Ted Kremenek139ba862009-10-22 00:03:57 +0000405
406 // The 'source_filename' argument is optional. If the caller does not
407 // specify it then it is assumed that the source file is specified
408 // in the actual argument list.
409 if (source_filename)
410 argv.push_back(source_filename);
411
Steve Naroff37b5ac22009-10-15 20:50:09 +0000412 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000413 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000414 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000415 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000416
417 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
418 for (int i = 0; i < num_command_line_args; ++i)
419 if (const char *arg = command_line_args[i]) {
420 if (strcmp(arg, "-o") == 0) {
421 ++i; // Also skip the matching argument.
422 continue;
423 }
424 if (strcmp(arg, "-emit-ast") == 0 ||
425 strcmp(arg, "-c") == 0 ||
426 strcmp(arg, "-fsyntax-only") == 0) {
427 continue;
428 }
429
430 // Keep the argument.
431 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000432 }
Ted Kremenek139ba862009-10-22 00:03:57 +0000433
434 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000435 argv.push_back(NULL);
436
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000437 // Invoke 'clang'.
438 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
439 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000440 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000441 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000442 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
443 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
444 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
445
Ted Kremenek8ee1f3f2009-10-22 22:19:00 +0000446 if (!ErrMsg.empty()) {
Ted Kremenek379afec2009-10-22 03:24:01 +0000447 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
448 << '\n' << "Arguments: \n";
449 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000450 I!=E; ++I) {
451 if (*I)
452 llvm::errs() << ' ' << *I << '\n';
453 }
454 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000455 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000456
Steve Naroff37b5ac22009-10-15 20:50:09 +0000457 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000458 ASTUnit *ATU = static_cast<ASTUnit *>(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000459 clang_createTranslationUnit(CIdx, astTmpFile));
460 if (ATU)
461 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000462 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000463}
464
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000465void clang_disposeTranslationUnit(
466 CXTranslationUnit CTUnit)
467{
468 assert(CTUnit && "Passed null CXTranslationUnit");
469 delete static_cast<ASTUnit *>(CTUnit);
470}
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000471
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000472const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
473{
474 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000475 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
476 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000477}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000478
Steve Naroffc857ea42009-09-02 13:28:54 +0000479void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
480 CXTranslationUnitIterator callback,
481 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000482{
Steve Naroff50398192009-08-28 15:28:48 +0000483 assert(CTUnit && "Passed null CXTranslationUnit");
484 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
485 ASTContext &Ctx = CXXUnit->getASTContext();
486
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000487 TUVisitor DVisit(CTUnit, callback, CData,
488 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000489 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000490}
491
Steve Naroffc857ea42009-09-02 13:28:54 +0000492void clang_loadDeclaration(CXDecl Dcl,
493 CXDeclIterator callback,
494 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000495{
Steve Naroffc857ea42009-09-02 13:28:54 +0000496 assert(Dcl && "Passed null CXDecl");
497
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000498 CDeclVisitor DVisit(Dcl, callback, CData,
499 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000500 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000501}
502
Steve Naroff7e8f8182009-08-28 12:07:44 +0000503// Some notes on CXEntity:
504//
505// - Since the 'ordinary' namespace includes functions, data, typedefs,
506// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
507// entity for 2 different types). For example:
508//
509// module1.m: @interface Foo @end Foo *x;
510// module2.m: void Foo(int);
511//
512// - Since the unique name spans translation units, static data/functions
513// within a CXTranslationUnit are *not* currently represented by entities.
514// As a result, there will be no entity for the following:
515//
516// module.m: static void Foo() { }
517//
518
519
Steve Naroff600866c2009-08-27 19:51:58 +0000520const char *clang_getDeclarationName(CXEntity)
521{
522 return "";
523}
524const char *clang_getURI(CXEntity)
525{
526 return "";
527}
528
529CXEntity clang_getEntity(const char *URI)
530{
531 return 0;
532}
533
534//
535// CXDecl Operations.
536//
Steve Naroff600866c2009-08-27 19:51:58 +0000537CXEntity clang_getEntityFromDecl(CXDecl)
538{
539 return 0;
540}
Steve Naroff89922f82009-08-31 00:59:03 +0000541const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000542{
Steve Naroff89922f82009-08-31 00:59:03 +0000543 assert(AnonDecl && "Passed null CXDecl");
544 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000545
546 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
547 return OMD->getSelector().getAsString().c_str();
548 }
Steve Naroff89922f82009-08-31 00:59:03 +0000549 if (ND->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000550 return ND->getIdentifier()->getNameStart();
Steve Naroffc857ea42009-09-02 13:28:54 +0000551 else
Steve Naroff89922f82009-08-31 00:59:03 +0000552 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000553}
Steve Narofff334b4e2009-09-02 18:26:48 +0000554
Steve Naroff699a07d2009-09-25 21:32:34 +0000555unsigned clang_getDeclLine(CXDecl AnonDecl)
556{
557 assert(AnonDecl && "Passed null CXDecl");
558 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
559 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
560 return SourceMgr.getSpellingLineNumber(ND->getLocation());
561}
562
563unsigned clang_getDeclColumn(CXDecl AnonDecl)
564{
565 assert(AnonDecl && "Passed null CXDecl");
566 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
567 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000568 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000569}
570
Steve Naroffee9405e2009-09-25 21:45:39 +0000571const char *clang_getDeclSource(CXDecl AnonDecl)
572{
573 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff88145032009-10-27 14:35:18 +0000574 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
575 assert (FEnt && "Cannot find FileEntry for Decl");
576 return clang_getFileName(FEnt);
577}
578
579static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
580 SourceLocation SLoc)
581{
582 FileID FID;
583 if (SLoc.isFileID())
584 FID = SMgr.getFileID(SLoc);
585 else
586 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
587 return SMgr.getFileEntryForID(FID);
588}
589
590CXFile clang_getDeclSourceFile(CXDecl AnonDecl)
591{
592 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000593 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
594 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000595 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
596}
597
598const char *clang_getFileName(CXFile SFile) {
599 assert(SFile && "Passed null CXFile");
600 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
601 return FEnt->getName();
602}
603
604time_t clang_getFileTime(CXFile SFile) {
605 assert(SFile && "Passed null CXFile");
606 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
607 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000608}
609
Steve Narofff334b4e2009-09-02 18:26:48 +0000610const char *clang_getCursorSpelling(CXCursor C)
611{
612 assert(C.decl && "CXCursor has null decl");
613 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
614
615 if (clang_isReference(C.kind)) {
616 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000617 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000618 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
619 assert(OID && "clang_getCursorLine(): Missing interface decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000620 return OID->getSuperClass()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000621 }
622 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000623 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000624 return OID->getIdentifier()->getNameStart();
Steve Naroff85e2db72009-10-01 00:31:07 +0000625 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000626 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
627 assert(OID && "clang_getCursorLine(): Missing category decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000628 return OID->getClassInterface()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000629 }
630 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000631 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
632 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000633 return OID->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000634 }
635 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000636 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
637 static_cast<Stmt *>(C.stmt));
638 assert(OME && "clang_getCursorLine(): Missing message expr");
639 return OME->getSelector().getAsString().c_str();
Steve Naroffbade7de2009-10-19 13:41:39 +0000640 }
Steve Narofffb570422009-09-22 19:25:29 +0000641 case CXCursor_VarRef:
642 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000643 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000644 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
645 static_cast<Stmt *>(C.stmt));
646 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Daniel Dunbare013d682009-10-18 20:26:12 +0000647 return DRE->getDecl()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000648 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000649 default:
650 return "<not implemented>";
651 }
652 }
653 return clang_getDeclSpelling(C.decl);
654}
655
656const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000657{
Steve Naroff89922f82009-08-31 00:59:03 +0000658 switch (Kind) {
659 case CXCursor_FunctionDecl: return "FunctionDecl";
660 case CXCursor_TypedefDecl: return "TypedefDecl";
661 case CXCursor_EnumDecl: return "EnumDecl";
662 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000663 case CXCursor_StructDecl: return "StructDecl";
664 case CXCursor_UnionDecl: return "UnionDecl";
665 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000666 case CXCursor_FieldDecl: return "FieldDecl";
667 case CXCursor_VarDecl: return "VarDecl";
668 case CXCursor_ParmDecl: return "ParmDecl";
669 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
670 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
671 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
672 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
673 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000674 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
675 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
676 case CXCursor_FunctionDefn: return "FunctionDefn";
677 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
678 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
679 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
680 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000681 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000682 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000683 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000684 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
685
686 case CXCursor_VarRef: return "VarRef";
687 case CXCursor_FunctionRef: return "FunctionRef";
688 case CXCursor_EnumConstantRef: return "EnumConstantRef";
689 case CXCursor_MemberRef: return "MemberRef";
690
Steve Naroff77128dd2009-09-15 20:25:34 +0000691 case CXCursor_InvalidFile: return "InvalidFile";
692 case CXCursor_NoDeclFound: return "NoDeclFound";
693 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000694 default: return "<not implemented>";
695 }
Steve Naroff600866c2009-08-27 19:51:58 +0000696}
Steve Naroff89922f82009-08-31 00:59:03 +0000697
Steve Naroff9efa7672009-09-04 15:44:05 +0000698static enum CXCursorKind TranslateKind(Decl *D) {
699 switch (D->getKind()) {
700 case Decl::Function: return CXCursor_FunctionDecl;
701 case Decl::Typedef: return CXCursor_TypedefDecl;
702 case Decl::Enum: return CXCursor_EnumDecl;
703 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
704 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
705 case Decl::Field: return CXCursor_FieldDecl;
706 case Decl::Var: return CXCursor_VarDecl;
707 case Decl::ParmVar: return CXCursor_ParmDecl;
708 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000709 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
710 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000711 case Decl::ObjCMethod: {
712 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
713 if (MD->isInstanceMethod())
714 return CXCursor_ObjCInstanceMethodDecl;
715 return CXCursor_ObjCClassMethodDecl;
716 }
717 default: break;
718 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000719 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000720}
Steve Naroff600866c2009-08-27 19:51:58 +0000721//
722// CXCursor Operations.
723//
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000724void clang_initCXLookupHint(CXLookupHint *hint) {
725 memset(hint, 0, sizeof(*hint));
726}
727
Steve Naroff9efa7672009-09-04 15:44:05 +0000728CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000729 unsigned line, unsigned column) {
730 return clang_getCursorWithHint(CTUnit, source_name, line, column, NULL);
731}
732
733CXCursor clang_getCursorWithHint(CXTranslationUnit CTUnit,
734 const char *source_name,
735 unsigned line, unsigned column,
736 CXLookupHint *hint)
Steve Naroff600866c2009-08-27 19:51:58 +0000737{
Steve Naroff9efa7672009-09-04 15:44:05 +0000738 assert(CTUnit && "Passed null CXTranslationUnit");
739 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
740
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000741 // FIXME: Make this better.
742 CXDecl RelativeToDecl = hint ? hint->decl : NULL;
743
Steve Naroff9efa7672009-09-04 15:44:05 +0000744 FileManager &FMgr = CXXUnit->getFileManager();
745 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000746 source_name+strlen(source_name));
747 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000748 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000749 return C;
750 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000751 SourceLocation SLoc =
752 CXXUnit->getSourceManager().getLocation(File, line, column);
753
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000754 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
755 static_cast<NamedDecl *>(RelativeToDecl));
Steve Naroff9efa7672009-09-04 15:44:05 +0000756
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000757 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000758 if (ALoc.isNamedRef())
759 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000760 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000761 if (Dcl) {
762 if (Stm) {
763 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
764 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
765 return C;
766 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
767 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
768 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000769 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000770 // Fall through...treat as a decl, not a ref.
771 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000772 if (ALoc.isNamedRef()) {
773 if (isa<ObjCInterfaceDecl>(Dcl)) {
774 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
775 return C;
776 }
777 if (isa<ObjCProtocolDecl>(Dcl)) {
778 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
779 return C;
780 }
781 }
782 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000783 return C;
784 }
Steve Narofffb570422009-09-22 19:25:29 +0000785 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000786 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000787}
788
Steve Naroff77128dd2009-09-15 20:25:34 +0000789CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
790{
791 assert(AnonDecl && "Passed null CXDecl");
792 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
793
Steve Narofffb570422009-09-22 19:25:29 +0000794 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000795 return C;
796}
797
798unsigned clang_isInvalid(enum CXCursorKind K)
799{
800 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
801}
802
Steve Naroff89922f82009-08-31 00:59:03 +0000803unsigned clang_isDeclaration(enum CXCursorKind K)
804{
805 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
806}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000807
Steve Narofff334b4e2009-09-02 18:26:48 +0000808unsigned clang_isReference(enum CXCursorKind K)
809{
810 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
811}
812
813unsigned clang_isDefinition(enum CXCursorKind K)
814{
815 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
816}
817
Steve Naroff9efa7672009-09-04 15:44:05 +0000818CXCursorKind clang_getCursorKind(CXCursor C)
819{
820 return C.kind;
821}
822
Steve Naroff699a07d2009-09-25 21:32:34 +0000823static Decl *getDeclFromExpr(Stmt *E) {
824 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
825 return RefExpr->getDecl();
826 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
827 return ME->getMemberDecl();
828 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
829 return RE->getDecl();
830
831 if (CallExpr *CE = dyn_cast<CallExpr>(E))
832 return getDeclFromExpr(CE->getCallee());
833 if (CastExpr *CE = dyn_cast<CastExpr>(E))
834 return getDeclFromExpr(CE->getSubExpr());
835 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
836 return OME->getMethodDecl();
837
838 return 0;
839}
840
Steve Naroff9efa7672009-09-04 15:44:05 +0000841CXDecl clang_getCursorDecl(CXCursor C)
842{
Steve Naroff699a07d2009-09-25 21:32:34 +0000843 if (clang_isDeclaration(C.kind))
844 return C.decl;
845
846 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000847 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000848 if (C.kind == CXCursor_ObjCClassRef ||
849 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000850 return static_cast<Stmt *>(C.stmt);
851 else
852 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
853 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000854 return C.decl;
855 }
856 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000857}
858
Steve Naroff85e2db72009-10-01 00:31:07 +0000859
Steve Narofff334b4e2009-09-02 18:26:48 +0000860static SourceLocation getLocationFromCursor(CXCursor C,
861 SourceManager &SourceMgr,
862 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000863 if (clang_isReference(C.kind)) {
864 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000865 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000866 if (isa<ObjCInterfaceDecl>(ND)) {
867 // FIXME: This is a hack (storing the parent decl in the stmt slot).
868 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
869 return parentDecl->getLocation();
870 }
871 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
872 assert(OID && "clang_getCursorLine(): Missing category decl");
873 return OID->getClassInterface()->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000874 }
875 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000876 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
877 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000878 return OID->getSuperClassLoc();
Steve Naroffbade7de2009-10-19 13:41:39 +0000879 }
880 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000881 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
882 assert(OID && "clang_getCursorLine(): Missing protocol decl");
883 return OID->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000884 }
885 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000886 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
887 static_cast<Stmt *>(C.stmt));
888 assert(OME && "clang_getCursorLine(): Missing message expr");
889 return OME->getLeftLoc(); /* FIXME: should be a range */
Steve Naroffbade7de2009-10-19 13:41:39 +0000890 }
Steve Narofffb570422009-09-22 19:25:29 +0000891 case CXCursor_VarRef:
892 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000893 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000894 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
895 static_cast<Stmt *>(C.stmt));
896 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
897 return DRE->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000898 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000899 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000900 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000901 }
902 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000903 SourceLocation SLoc;
904 switch (ND->getKind()) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000905 case Decl::ObjCInterface: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000906 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
907 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000908 }
909 case Decl::ObjCProtocol: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000910 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
911 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000912 }
913 default: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000914 SLoc = ND->getLocation();
915 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000916 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000917 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000918 if (SLoc.isInvalid())
919 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000920 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000921 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000922}
923
Steve Naroff2d4d6292009-08-31 14:26:51 +0000924unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000925{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000926 assert(C.decl && "CXCursor has null decl");
927 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000928 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000929
930 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000931 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000932}
Steve Narofff334b4e2009-09-02 18:26:48 +0000933
Steve Naroff2d4d6292009-08-31 14:26:51 +0000934unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000935{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000936 assert(C.decl && "CXCursor has null decl");
937 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000938 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000939
940 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000941 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000942}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000943const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000944{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000945 assert(C.decl && "CXCursor has null decl");
946 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000947 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000948
949 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000950 if (SLoc.isFileID())
951 return SourceMgr.getBufferName(SLoc);
952
953 // Retrieve the file in which the macro was instantiated, then provide that
954 // buffer name.
955 // FIXME: Do we want to give specific macro-instantiation information?
956 const llvm::MemoryBuffer *Buffer
957 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
958 if (!Buffer)
959 return 0;
960
961 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000962}
963
Steve Naroff88145032009-10-27 14:35:18 +0000964CXFile clang_getCursorSourceFile(CXCursor C)
965{
966 assert(C.decl && "CXCursor has null decl");
967 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
968 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
969
970 return (void *)getFileEntryFromSourceLocation(SourceMgr,
971 getLocationFromCursor(C,SourceMgr, ND));
972}
973
Steve Naroff4ade6d62009-09-23 17:52:52 +0000974void clang_getDefinitionSpellingAndExtent(CXCursor C,
975 const char **startBuf,
976 const char **endBuf,
977 unsigned *startLine,
978 unsigned *startColumn,
979 unsigned *endLine,
980 unsigned *endColumn)
981{
982 assert(C.decl && "CXCursor has null decl");
983 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
984 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
985 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
986
987 SourceManager &SM = FD->getASTContext().getSourceManager();
988 *startBuf = SM.getCharacterData(Body->getLBracLoc());
989 *endBuf = SM.getCharacterData(Body->getRBracLoc());
990 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
991 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
992 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
993 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
994}
995
996
Steve Naroff600866c2009-08-27 19:51:58 +0000997} // end extern "C"