blob: d21c6fcc70a48cfcf21db84a47bdba627b6c813d [file] [log] [blame]
Ted Kremenekb60d87c2009-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 Naroffa1c72842009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroffef9618b2009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Steve Naroffa1c72842009-08-28 15:28:48 +000019#include "clang/AST/DeclVisitor.h"
Steve Naroff66af1ae2009-09-22 19:25:29 +000020#include "clang/AST/StmtVisitor.h"
Steve Naroff3645f5a2009-09-02 13:28:54 +000021#include "clang/AST/Decl.h"
Benjamin Kramer8b83f5d2009-08-29 12:56:35 +000022#include "clang/Basic/FileManager.h"
Steve Naroff772c1a42009-08-31 14:26:51 +000023#include "clang/Basic/SourceManager.h"
Benjamin Kramer8b83f5d2009-08-29 12:56:35 +000024#include "clang/Frontend/ASTUnit.h"
Benjamin Kramer6bd6d502009-10-18 16:13:48 +000025#include "llvm/Config/config.h"
Ted Kremenek428c6372009-10-19 21:44:57 +000026#include "llvm/Support/Compiler.h"
Douglas Gregord3d923a2009-10-16 21:24:31 +000027#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/System/Path.h"
Benjamin Kramer2836c4c2009-10-18 11:19:36 +000029#include "llvm/System/Program.h"
Ted Kremenek44886fd2009-10-22 03:24:01 +000030#include "llvm/Support/raw_ostream.h"
Ted Kremenek428c6372009-10-19 21:44:57 +000031
Benjamin Kramer8b83f5d2009-08-29 12:56:35 +000032#include <cstdio>
Ted Kremenek9dd89ba2009-10-19 21:17:25 +000033#include <vector>
34
Benjamin Kramer6bd6d502009-10-18 16:13:48 +000035#ifdef LLVM_ON_WIN32
36#define WIN32_LEAN_AND_MEAN
37#include <windows.h>
38#else
Steve Naroff7781daa2009-10-15 20:04:39 +000039#include <dlfcn.h>
Daniel Dunbarcd237182009-10-17 23:53:11 +000040#endif
Steve Naroff7781daa2009-10-15 20:04:39 +000041
Steve Naroffa1c72842009-08-28 15:28:48 +000042using namespace clang;
43using namespace idx;
44
Steve Naroff1054e602009-08-31 00:59:03 +000045namespace {
Steve Naroff76b8f132009-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 Naroff66af1ae2009-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 Naroff76b8f132009-09-23 17:52:52 +000081 Call(TranslateDeclRefExpr(Node), Node);
Steve Naroff66af1ae2009-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 Naroff76b8f132009-09-23 17:52:52 +000093#endif
Ted Kremenek428c6372009-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 Naroff66af1ae2009-09-22 19:25:29 +0000102
Steve Naroff1054e602009-08-31 00:59:03 +0000103// Translation Unit Visitor.
104class TUVisitor : public DeclVisitor<TUVisitor> {
105 CXTranslationUnit TUnit;
106 CXTranslationUnitIterator Callback;
Steve Naroff69b10fd2009-09-01 15:55:40 +0000107 CXClientData CData;
108
Douglas Gregor16bef852009-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 Naroff69b10fd2009-09-01 15:55:40 +0000114 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor16bef852009-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 Naroff58bd62d2009-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 Naroff66af1ae2009-09-22 19:25:29 +0000123 CXCursor C = { CK, ND, 0 };
Steve Naroff69b10fd2009-09-01 15:55:40 +0000124 Callback(TUnit, C, CData);
125 }
Steve Naroff1054e602009-08-31 00:59:03 +0000126public:
Steve Naroff69b10fd2009-09-01 15:55:40 +0000127 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor16bef852009-10-16 20:01:17 +0000128 CXTranslationUnitIterator cback, CXClientData D,
129 unsigned MaxPCHLevel) :
130 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff1054e602009-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 Naroff69b10fd2009-09-01 15:55:40 +0000140 void VisitTypedefDecl(TypedefDecl *ND) {
141 Call(CXCursor_TypedefDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000142 }
143 void VisitTagDecl(TagDecl *ND) {
Steve Naroff3645f5a2009-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 Naroff1054e602009-08-31 00:59:03 +0000158 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000159 void VisitVarDecl(VarDecl *ND) {
160 Call(CXCursor_VarDecl, ND);
161 }
Steve Naroff1054e602009-08-31 00:59:03 +0000162 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000163 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
164 : CXCursor_FunctionDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000165 }
166 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +0000167 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000168 }
169 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +0000170 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000171 }
172 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff69b10fd2009-09-01 15:55:40 +0000173 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000174 }
Steve Naroff3645f5a2009-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 Naroff1054e602009-08-31 00:59:03 +0000181};
182
Steve Naroff3645f5a2009-09-02 13:28:54 +0000183// Declaration visitor.
184class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
185 CXDecl CDecl;
186 CXDeclIterator Callback;
187 CXClientData CData;
188
Douglas Gregor16bef852009-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 Naroff3645f5a2009-09-02 13:28:54 +0000194 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroff38c1a7b2009-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 Gregor16bef852009-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 Naroff66af1ae2009-09-22 19:25:29 +0000203 CXCursor C = { CK, ND, 0 };
Steve Naroff3645f5a2009-09-02 13:28:54 +0000204 Callback(CDecl, C, CData);
205 }
Steve Naroff1054e602009-08-31 00:59:03 +0000206public:
Douglas Gregor16bef852009-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 Naroff3645f5a2009-09-02 13:28:54 +0000210
Steve Naroff38c1a7b2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000217 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000218 // Issue callbacks for super class.
Steve Naroff80a766b2009-09-02 18:26:48 +0000219 if (D->getSuperClass())
220 Call(CXCursor_ObjCSuperClassRef, D);
221
Steve Naroffef9618b2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000225 VisitDeclContext(dyn_cast<DeclContext>(D));
226 }
Steve Naroffef9618b2009-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 Naroff3645f5a2009-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 Naroff1054e602009-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 Naroff3645f5a2009-09-02 13:28:54 +0000249 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000250 }
251 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000252 Call(CXCursor_FieldDecl, ND);
253 }
Steve Naroff38c1a7b2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000260 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
261 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000262 }
263 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000264 Call(CXCursor_ObjCIvarDecl, ND);
265 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000266 void VisitFunctionDecl(FunctionDecl *ND) {
267 if (ND->isThisDeclarationADefinition()) {
268 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff76b8f132009-09-23 17:52:52 +0000269#if 0
270 // Not currently needed.
271 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Naroff66af1ae2009-09-22 19:25:29 +0000272 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff76b8f132009-09-23 17:52:52 +0000273 RVisit.Visit(Body);
274#endif
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000275 }
276 }
Steve Naroff3645f5a2009-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 Naroff38c1a7b2009-09-03 15:49:00 +0000281 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff3645f5a2009-09-02 13:28:54 +0000282 } else
283 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
284 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000285 }
286};
287
Douglas Gregor16bef852009-10-16 20:01:17 +0000288class CIndexer : public Indexer {
289public:
Steve Naroff531e2842009-10-20 14:46:24 +0000290 explicit CIndexer(Program *prog) : Indexer(*prog),
291 OnlyLocalDecls(false),
292 DisplayDiagnostics(false) {}
Ted Kremenek05729c22009-10-17 06:21:47 +0000293
294 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor16bef852009-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 Kramer817e7e42009-10-18 11:10:55 +0000301
Steve Naroff531e2842009-10-20 14:46:24 +0000302 void setDisplayDiagnostics(bool Display = true) {
303 DisplayDiagnostics = Display;
304 }
305 bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
306
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000307 /// \brief Get the path of the clang binary.
Benjamin Kramerb625a132009-10-19 10:20:24 +0000308 const llvm::sys::Path& getClangPath();
Douglas Gregor16bef852009-10-16 20:01:17 +0000309private:
310 bool OnlyLocalDecls;
Steve Naroff531e2842009-10-20 14:46:24 +0000311 bool DisplayDiagnostics;
312
Benjamin Kramerb625a132009-10-19 10:20:24 +0000313 llvm::sys::Path ClangPath;
Douglas Gregor16bef852009-10-16 20:01:17 +0000314};
Steve Naroff1054e602009-08-31 00:59:03 +0000315
Benjamin Kramer61f5d0c2009-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 Naroff7781daa2009-10-15 20:04:39 +0000321 // Find the location where this library lives (libCIndex.dylib).
Benjamin Kramer6bd6d502009-10-18 16:13:48 +0000322#ifdef LLVM_ON_WIN32
323 MEMORY_BASIC_INFORMATION mbi;
324 char path[MAX_PATH];
Benjamin Kramer49ce64e2009-10-18 16:20:58 +0000325 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
Benjamin Kramer6bd6d502009-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 Naroff7781daa2009-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 Dunbarcd237182009-10-17 23:53:11 +0000337#endif
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000338
Steve Naroff7781daa2009-10-15 20:04:39 +0000339 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer61f5d0c2009-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 Naroff531e2842009-10-20 14:46:24 +0000354CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
355 int displayDiagnostics)
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000356{
Steve Naroff531e2842009-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 Naroffd5e8e862009-08-27 19:51:58 +0000363}
364
Steve Naroff3aa2d732009-09-17 18:33:27 +0000365void clang_disposeIndex(CXIndex CIdx)
366{
367 assert(CIdx && "Passed null CXIndex");
Douglas Gregor16bef852009-10-16 20:01:17 +0000368 delete static_cast<CIndexer *>(CIdx);
Steve Naroff3aa2d732009-09-17 18:33:27 +0000369}
370
Steve Naroffa1c72842009-08-28 15:28:48 +0000371// FIXME: need to pass back error info.
372CXTranslationUnit clang_createTranslationUnit(
Steve Naroff531e2842009-10-20 14:46:24 +0000373 CXIndex CIdx, const char *ast_filename)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000374{
Steve Naroffa1c72842009-08-28 15:28:48 +0000375 assert(CIdx && "Passed null CXIndex");
Douglas Gregor16bef852009-10-16 20:01:17 +0000376 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroffa1c72842009-08-28 15:28:48 +0000377 std::string astName(ast_filename);
378 std::string ErrMsg;
379
Ted Kremenek44886fd2009-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 Kremenekbf0690c2009-10-26 22:08:39 +0000387 if (!ErrMsg.empty())
388 llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n';
Ted Kremenek44886fd2009-10-22 03:24:01 +0000389
390 return TU;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000391}
392
Steve Naroff7781daa2009-10-15 20:04:39 +0000393CXTranslationUnit clang_createTranslationUnitFromSourceFile(
394 CXIndex CIdx,
395 const char *source_filename,
Steve Naroff531e2842009-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 Kremenek649bf5c2009-10-22 00:03:57 +0000400 // Build up the arguments for invoking 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000401 std::vector<const char *> argv;
Ted Kremenek649bf5c2009-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 Kramer61f5d0c2009-10-18 16:11:04 +0000405 argv.push_back(ClangPath.c_str());
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000406
407 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000408 argv.push_back("-emit-ast");
Ted Kremenek649bf5c2009-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 Naroff1cfb96c2009-10-15 20:50:09 +0000416 // Generate a temporary name for the AST file.
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000417 argv.push_back("-o");
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000418 char astTmpFile[L_tmpnam];
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000419 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek649bf5c2009-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 Naroff531e2842009-10-20 14:46:24 +0000436 }
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000437
438 // Add the null terminator.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000439 argv.push_back(NULL);
440
Ted Kremenek12e678d2009-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 Kremenek44886fd2009-10-22 03:24:01 +0000444 std::string ErrMsg;
Ted Kremeneke2896882009-10-19 22:27:32 +0000445 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek44886fd2009-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 Kremenek2a3ffa92009-10-22 22:19:00 +0000450 if (!ErrMsg.empty()) {
Ted Kremenek44886fd2009-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 Kremenekbf0690c2009-10-26 22:08:39 +0000454 I!=E; ++I) {
455 if (*I)
456 llvm::errs() << ' ' << *I << '\n';
457 }
458 llvm::errs() << '\n';
Ted Kremenek44886fd2009-10-22 03:24:01 +0000459 }
Benjamin Kramer2836c4c2009-10-18 11:19:36 +0000460
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000461 // Finally, we create the translation unit from the ast file.
Steve Naroff44cd60e2009-10-15 22:23:48 +0000462 ASTUnit *ATU = static_cast<ASTUnit *>(
Steve Naroff531e2842009-10-20 14:46:24 +0000463 clang_createTranslationUnit(CIdx, astTmpFile));
464 if (ATU)
465 ATU->unlinkTemporaryFile();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000466 return ATU;
Steve Naroff7781daa2009-10-15 20:04:39 +0000467}
468
Steve Naroff3aa2d732009-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 Gregor16bef852009-10-16 20:01:17 +0000475
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000476const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
477{
478 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroffc0683b92009-09-03 18:19:54 +0000479 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
480 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000481}
Daniel Dunbare58bd8b2009-08-28 16:30:07 +0000482
Steve Naroff3645f5a2009-09-02 13:28:54 +0000483void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
484 CXTranslationUnitIterator callback,
485 CXClientData CData)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000486{
Steve Naroffa1c72842009-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 Gregor16bef852009-10-16 20:01:17 +0000491 TUVisitor DVisit(CTUnit, callback, CData,
492 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroffa1c72842009-08-28 15:28:48 +0000493 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroffd5e8e862009-08-27 19:51:58 +0000494}
495
Steve Naroff3645f5a2009-09-02 13:28:54 +0000496void clang_loadDeclaration(CXDecl Dcl,
497 CXDeclIterator callback,
498 CXClientData CData)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000499{
Steve Naroff3645f5a2009-09-02 13:28:54 +0000500 assert(Dcl && "Passed null CXDecl");
501
Douglas Gregor16bef852009-10-16 20:01:17 +0000502 CDeclVisitor DVisit(Dcl, callback, CData,
503 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroff3645f5a2009-09-02 13:28:54 +0000504 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroffd5e8e862009-08-27 19:51:58 +0000505}
506
Steve Naroff87219592009-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 Naroffd5e8e862009-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 Naroffd5e8e862009-08-27 19:51:58 +0000541CXEntity clang_getEntityFromDecl(CXDecl)
542{
543 return 0;
544}
Steve Naroff1054e602009-08-31 00:59:03 +0000545const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000546{
Steve Naroff1054e602009-08-31 00:59:03 +0000547 assert(AnonDecl && "Passed null CXDecl");
548 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroff3645f5a2009-09-02 13:28:54 +0000549
550 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
551 return OMD->getSelector().getAsString().c_str();
Steve Naroffa4aeed62009-10-29 18:55:50 +0000552 }
553 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
554 return CIMP->getCategoryClass()->getName().data();
555
Steve Naroff1054e602009-08-31 00:59:03 +0000556 if (ND->getIdentifier())
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000557 return ND->getIdentifier()->getNameStart();
Steve Naroff3645f5a2009-09-02 13:28:54 +0000558 else
Steve Naroff1054e602009-08-31 00:59:03 +0000559 return "";
Steve Naroffd5e8e862009-08-27 19:51:58 +0000560}
Steve Naroff80a766b2009-09-02 18:26:48 +0000561
Steve Naroff63f475a2009-09-25 21:32:34 +0000562unsigned clang_getDeclLine(CXDecl AnonDecl)
563{
564 assert(AnonDecl && "Passed null CXDecl");
565 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
566 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
567 return SourceMgr.getSpellingLineNumber(ND->getLocation());
568}
569
570unsigned clang_getDeclColumn(CXDecl AnonDecl)
571{
572 assert(AnonDecl && "Passed null CXDecl");
573 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
574 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff43b118f2009-09-25 22:15:54 +0000575 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff63f475a2009-09-25 21:32:34 +0000576}
577
Steve Naroff26760892009-09-25 21:45:39 +0000578const char *clang_getDeclSource(CXDecl AnonDecl)
579{
580 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff6231f182009-10-27 14:35:18 +0000581 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
582 assert (FEnt && "Cannot find FileEntry for Decl");
583 return clang_getFileName(FEnt);
584}
585
586static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
587 SourceLocation SLoc)
588{
589 FileID FID;
590 if (SLoc.isFileID())
591 FID = SMgr.getFileID(SLoc);
592 else
593 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
594 return SMgr.getFileEntryForID(FID);
595}
596
597CXFile clang_getDeclSourceFile(CXDecl AnonDecl)
598{
599 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff26760892009-09-25 21:45:39 +0000600 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
601 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff6231f182009-10-27 14:35:18 +0000602 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
603}
604
605const char *clang_getFileName(CXFile SFile) {
606 assert(SFile && "Passed null CXFile");
607 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
608 return FEnt->getName();
609}
610
611time_t clang_getFileTime(CXFile SFile) {
612 assert(SFile && "Passed null CXFile");
613 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
614 return FEnt->getModificationTime();
Steve Naroff26760892009-09-25 21:45:39 +0000615}
616
Steve Naroff80a766b2009-09-02 18:26:48 +0000617const char *clang_getCursorSpelling(CXCursor C)
618{
619 assert(C.decl && "CXCursor has null decl");
620 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
621
622 if (clang_isReference(C.kind)) {
623 switch (C.kind) {
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000624 case CXCursor_ObjCSuperClassRef: {
Steve Naroff80a766b2009-09-02 18:26:48 +0000625 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
626 assert(OID && "clang_getCursorLine(): Missing interface decl");
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000627 return OID->getSuperClass()->getIdentifier()->getNameStart();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000628 }
629 case CXCursor_ObjCClassRef: {
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000630 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000631 return OID->getIdentifier()->getNameStart();
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000632 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000633 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
634 assert(OID && "clang_getCursorLine(): Missing category decl");
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000635 return OID->getClassInterface()->getIdentifier()->getNameStart();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000636 }
637 case CXCursor_ObjCProtocolRef: {
Steve Naroffef9618b2009-09-04 15:44:05 +0000638 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
639 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000640 return OID->getIdentifier()->getNameStart();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000641 }
642 case CXCursor_ObjCSelectorRef: {
Steve Naroff66af1ae2009-09-22 19:25:29 +0000643 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
644 static_cast<Stmt *>(C.stmt));
645 assert(OME && "clang_getCursorLine(): Missing message expr");
646 return OME->getSelector().getAsString().c_str();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000647 }
Steve Naroff66af1ae2009-09-22 19:25:29 +0000648 case CXCursor_VarRef:
649 case CXCursor_FunctionRef:
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000650 case CXCursor_EnumConstantRef: {
Steve Naroff66af1ae2009-09-22 19:25:29 +0000651 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
652 static_cast<Stmt *>(C.stmt));
653 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000654 return DRE->getDecl()->getIdentifier()->getNameStart();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000655 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000656 default:
657 return "<not implemented>";
658 }
659 }
660 return clang_getDeclSpelling(C.decl);
661}
662
663const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000664{
Steve Naroff1054e602009-08-31 00:59:03 +0000665 switch (Kind) {
666 case CXCursor_FunctionDecl: return "FunctionDecl";
667 case CXCursor_TypedefDecl: return "TypedefDecl";
668 case CXCursor_EnumDecl: return "EnumDecl";
669 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroff3645f5a2009-09-02 13:28:54 +0000670 case CXCursor_StructDecl: return "StructDecl";
671 case CXCursor_UnionDecl: return "UnionDecl";
672 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff1054e602009-08-31 00:59:03 +0000673 case CXCursor_FieldDecl: return "FieldDecl";
674 case CXCursor_VarDecl: return "VarDecl";
675 case CXCursor_ParmDecl: return "ParmDecl";
676 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
677 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
678 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
679 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
680 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroff3645f5a2009-09-02 13:28:54 +0000681 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
682 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
683 case CXCursor_FunctionDefn: return "FunctionDefn";
684 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
685 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
686 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
687 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Naroff80a766b2009-09-02 18:26:48 +0000688 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroffef9618b2009-09-04 15:44:05 +0000689 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000690 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Naroff66af1ae2009-09-22 19:25:29 +0000691 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
692
693 case CXCursor_VarRef: return "VarRef";
694 case CXCursor_FunctionRef: return "FunctionRef";
695 case CXCursor_EnumConstantRef: return "EnumConstantRef";
696 case CXCursor_MemberRef: return "MemberRef";
697
Steve Naroff54f22fb2009-09-15 20:25:34 +0000698 case CXCursor_InvalidFile: return "InvalidFile";
699 case CXCursor_NoDeclFound: return "NoDeclFound";
700 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff1054e602009-08-31 00:59:03 +0000701 default: return "<not implemented>";
702 }
Steve Naroffd5e8e862009-08-27 19:51:58 +0000703}
Steve Naroff1054e602009-08-31 00:59:03 +0000704
Steve Naroffef9618b2009-09-04 15:44:05 +0000705static enum CXCursorKind TranslateKind(Decl *D) {
706 switch (D->getKind()) {
707 case Decl::Function: return CXCursor_FunctionDecl;
708 case Decl::Typedef: return CXCursor_TypedefDecl;
709 case Decl::Enum: return CXCursor_EnumDecl;
710 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
711 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
712 case Decl::Field: return CXCursor_FieldDecl;
713 case Decl::Var: return CXCursor_VarDecl;
714 case Decl::ParmVar: return CXCursor_ParmDecl;
715 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Naroff66af1ae2009-09-22 19:25:29 +0000716 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
717 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroffef9618b2009-09-04 15:44:05 +0000718 case Decl::ObjCMethod: {
719 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
720 if (MD->isInstanceMethod())
721 return CXCursor_ObjCInstanceMethodDecl;
722 return CXCursor_ObjCClassMethodDecl;
723 }
724 default: break;
725 }
Steve Naroff54f22fb2009-09-15 20:25:34 +0000726 return CXCursor_NotImplemented;
Steve Naroffef9618b2009-09-04 15:44:05 +0000727}
Steve Naroffd5e8e862009-08-27 19:51:58 +0000728//
729// CXCursor Operations.
730//
Steve Naroffef9618b2009-09-04 15:44:05 +0000731CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff58bd62d2009-10-28 20:44:47 +0000732 unsigned line, unsigned column)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000733{
Steve Naroffef9618b2009-09-04 15:44:05 +0000734 assert(CTUnit && "Passed null CXTranslationUnit");
735 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
736
737 FileManager &FMgr = CXXUnit->getFileManager();
738 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff54f22fb2009-09-15 20:25:34 +0000739 source_name+strlen(source_name));
740 if (!File) {
Steve Naroff66af1ae2009-09-22 19:25:29 +0000741 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000742 return C;
743 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000744 SourceLocation SLoc =
745 CXXUnit->getSourceManager().getLocation(File, line, column);
746
Steve Naroff58bd62d2009-10-28 20:44:47 +0000747 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
748
749 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
750 &LastLoc);
751 if (ALoc.isValid())
752 CXXUnit->setLastASTLocation(ALoc);
753
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000754 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis419e38b2009-09-29 19:45:58 +0000755 if (ALoc.isNamedRef())
756 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000757 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff76b8f132009-09-23 17:52:52 +0000758 if (Dcl) {
759 if (Stm) {
760 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
761 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
762 return C;
763 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
764 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
765 return C;
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000766 }
Steve Naroff76b8f132009-09-23 17:52:52 +0000767 // Fall through...treat as a decl, not a ref.
768 }
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000769 if (ALoc.isNamedRef()) {
770 if (isa<ObjCInterfaceDecl>(Dcl)) {
771 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
772 return C;
773 }
774 if (isa<ObjCProtocolDecl>(Dcl)) {
775 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
776 return C;
777 }
778 }
779 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000780 return C;
781 }
Steve Naroff66af1ae2009-09-22 19:25:29 +0000782 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroffef9618b2009-09-04 15:44:05 +0000783 return C;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000784}
785
Steve Naroff54f22fb2009-09-15 20:25:34 +0000786CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
787{
788 assert(AnonDecl && "Passed null CXDecl");
789 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
790
Steve Naroff66af1ae2009-09-22 19:25:29 +0000791 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000792 return C;
793}
794
795unsigned clang_isInvalid(enum CXCursorKind K)
796{
797 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
798}
799
Steve Naroff1054e602009-08-31 00:59:03 +0000800unsigned clang_isDeclaration(enum CXCursorKind K)
801{
802 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
803}
Steve Naroff772c1a42009-08-31 14:26:51 +0000804
Steve Naroff80a766b2009-09-02 18:26:48 +0000805unsigned clang_isReference(enum CXCursorKind K)
806{
807 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
808}
809
810unsigned clang_isDefinition(enum CXCursorKind K)
811{
812 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
813}
814
Steve Naroffef9618b2009-09-04 15:44:05 +0000815CXCursorKind clang_getCursorKind(CXCursor C)
816{
817 return C.kind;
818}
819
Steve Naroff63f475a2009-09-25 21:32:34 +0000820static Decl *getDeclFromExpr(Stmt *E) {
821 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
822 return RefExpr->getDecl();
823 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
824 return ME->getMemberDecl();
825 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
826 return RE->getDecl();
827
828 if (CallExpr *CE = dyn_cast<CallExpr>(E))
829 return getDeclFromExpr(CE->getCallee());
830 if (CastExpr *CE = dyn_cast<CastExpr>(E))
831 return getDeclFromExpr(CE->getSubExpr());
832 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
833 return OME->getMethodDecl();
834
835 return 0;
836}
837
Steve Naroffef9618b2009-09-04 15:44:05 +0000838CXDecl clang_getCursorDecl(CXCursor C)
839{
Steve Naroff63f475a2009-09-25 21:32:34 +0000840 if (clang_isDeclaration(C.kind))
841 return C.decl;
842
843 if (clang_isReference(C.kind)) {
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000844 if (C.stmt) {
Steve Naroffd7eb7172009-10-05 17:58:19 +0000845 if (C.kind == CXCursor_ObjCClassRef ||
846 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000847 return static_cast<Stmt *>(C.stmt);
848 else
849 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
850 } else
Steve Naroff63f475a2009-09-25 21:32:34 +0000851 return C.decl;
852 }
853 return 0;
Steve Naroffef9618b2009-09-04 15:44:05 +0000854}
855
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000856
Steve Naroff80a766b2009-09-02 18:26:48 +0000857static SourceLocation getLocationFromCursor(CXCursor C,
858 SourceManager &SourceMgr,
859 NamedDecl *ND) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000860 if (clang_isReference(C.kind)) {
861 switch (C.kind) {
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000862 case CXCursor_ObjCClassRef: {
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000863 if (isa<ObjCInterfaceDecl>(ND)) {
864 // FIXME: This is a hack (storing the parent decl in the stmt slot).
865 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
866 return parentDecl->getLocation();
867 }
868 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
869 assert(OID && "clang_getCursorLine(): Missing category decl");
870 return OID->getClassInterface()->getLocation();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000871 }
872 case CXCursor_ObjCSuperClassRef: {
Steve Naroff80a766b2009-09-02 18:26:48 +0000873 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
874 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroffb92c73a2009-09-02 18:58:52 +0000875 return OID->getSuperClassLoc();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000876 }
877 case CXCursor_ObjCProtocolRef: {
Steve Naroffef9618b2009-09-04 15:44:05 +0000878 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
879 assert(OID && "clang_getCursorLine(): Missing protocol decl");
880 return OID->getLocation();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000881 }
882 case CXCursor_ObjCSelectorRef: {
Steve Naroff66af1ae2009-09-22 19:25:29 +0000883 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
884 static_cast<Stmt *>(C.stmt));
885 assert(OME && "clang_getCursorLine(): Missing message expr");
886 return OME->getLeftLoc(); /* FIXME: should be a range */
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000887 }
Steve Naroff66af1ae2009-09-22 19:25:29 +0000888 case CXCursor_VarRef:
889 case CXCursor_FunctionRef:
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000890 case CXCursor_EnumConstantRef: {
Steve Naroff66af1ae2009-09-22 19:25:29 +0000891 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
892 static_cast<Stmt *>(C.stmt));
893 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
894 return DRE->getLocation();
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000895 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000896 default:
Steve Naroffb92c73a2009-09-02 18:58:52 +0000897 return SourceLocation();
Steve Naroff80a766b2009-09-02 18:26:48 +0000898 }
899 } else { // We have a declaration or a definition.
Steve Naroffef9618b2009-09-04 15:44:05 +0000900 SourceLocation SLoc;
901 switch (ND->getKind()) {
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000902 case Decl::ObjCInterface: {
Steve Naroffef9618b2009-09-04 15:44:05 +0000903 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
904 break;
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000905 }
906 case Decl::ObjCProtocol: {
Steve Naroffef9618b2009-09-04 15:44:05 +0000907 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
908 break;
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000909 }
910 default: {
Steve Naroffef9618b2009-09-04 15:44:05 +0000911 SLoc = ND->getLocation();
912 break;
Steve Naroff2f78a6f2009-10-19 13:41:39 +0000913 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000914 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000915 if (SLoc.isInvalid())
916 return SourceLocation();
Steve Naroffb92c73a2009-09-02 18:58:52 +0000917 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Naroff80a766b2009-09-02 18:26:48 +0000918 }
Steve Naroff80a766b2009-09-02 18:26:48 +0000919}
920
Steve Naroff772c1a42009-08-31 14:26:51 +0000921unsigned clang_getCursorLine(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000922{
Steve Naroff772c1a42009-08-31 14:26:51 +0000923 assert(C.decl && "CXCursor has null decl");
924 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000925 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000926
927 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000928 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000929}
Steve Naroff80a766b2009-09-02 18:26:48 +0000930
Steve Naroff772c1a42009-08-31 14:26:51 +0000931unsigned clang_getCursorColumn(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000932{
Steve Naroff772c1a42009-08-31 14:26:51 +0000933 assert(C.decl && "CXCursor has null decl");
934 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000935 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000936
937 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000938 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000939}
Steve Naroff772c1a42009-08-31 14:26:51 +0000940const char *clang_getCursorSource(CXCursor C)
Steve Naroffd5e8e862009-08-27 19:51:58 +0000941{
Steve Naroff772c1a42009-08-31 14:26:51 +0000942 assert(C.decl && "CXCursor has null decl");
943 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000944 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff80a766b2009-09-02 18:26:48 +0000945
946 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregord3d923a2009-10-16 21:24:31 +0000947 if (SLoc.isFileID())
948 return SourceMgr.getBufferName(SLoc);
949
950 // Retrieve the file in which the macro was instantiated, then provide that
951 // buffer name.
952 // FIXME: Do we want to give specific macro-instantiation information?
953 const llvm::MemoryBuffer *Buffer
954 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
955 if (!Buffer)
956 return 0;
957
958 return Buffer->getBufferIdentifier();
Steve Naroffd5e8e862009-08-27 19:51:58 +0000959}
960
Steve Naroff6231f182009-10-27 14:35:18 +0000961CXFile clang_getCursorSourceFile(CXCursor C)
962{
963 assert(C.decl && "CXCursor has null decl");
964 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
965 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
966
967 return (void *)getFileEntryFromSourceLocation(SourceMgr,
968 getLocationFromCursor(C,SourceMgr, ND));
969}
970
Steve Naroff76b8f132009-09-23 17:52:52 +0000971void clang_getDefinitionSpellingAndExtent(CXCursor C,
972 const char **startBuf,
973 const char **endBuf,
974 unsigned *startLine,
975 unsigned *startColumn,
976 unsigned *endLine,
977 unsigned *endColumn)
978{
979 assert(C.decl && "CXCursor has null decl");
980 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
981 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
982 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
983
984 SourceManager &SM = FD->getASTContext().getSourceManager();
985 *startBuf = SM.getCharacterData(Body->getLBracLoc());
986 *endBuf = SM.getCharacterData(Body->getRBracLoc());
987 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
988 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
989 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
990 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
991}
992
993
Steve Naroffd5e8e862009-08-27 19:51:58 +0000994} // end extern "C"