blob: 4798e28328223e5c0853e5de5092a1e0d2b63737 [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Clang-C Source Indexing library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang-c/Index.h"
Steve Naroff50398192009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroff9efa7672009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Steve Naroff50398192009-08-28 15:28:48 +000019#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000020#include "clang/AST/StmtVisitor.h"
Steve Naroffc857ea42009-09-02 13:28:54 +000021#include "clang/AST/Decl.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000022#include "clang/Basic/FileManager.h"
Steve Naroff2d4d6292009-08-31 14:26:51 +000023#include "clang/Basic/SourceManager.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000024#include "clang/Frontend/ASTUnit.h"
Benjamin Kramer20d75812009-10-18 16:13:48 +000025#include "llvm/Config/config.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000026#include "llvm/Support/Compiler.h"
Douglas Gregor02465752009-10-16 21:24:31 +000027#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/System/Path.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000029#include "llvm/System/Program.h"
Ted Kremenek379afec2009-10-22 03:24:01 +000030#include "llvm/Support/raw_ostream.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000031
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000032#include <cstdio>
Ted Kremenek49358d82009-10-19 21:17:25 +000033#include <vector>
34
Benjamin Kramer20d75812009-10-18 16:13:48 +000035#ifdef LLVM_ON_WIN32
36#define WIN32_LEAN_AND_MEAN
37#include <windows.h>
38#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +000039#include <dlfcn.h>
Daniel Dunbara47dd192009-10-17 23:53:11 +000040#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +000041
Steve Naroff50398192009-08-28 15:28:48 +000042using namespace clang;
43using namespace idx;
44
Steve Naroff89922f82009-08-31 00:59:03 +000045namespace {
Steve Naroff4ade6d62009-09-23 17:52:52 +000046static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
47{
48 NamedDecl *D = DRE->getDecl();
49 if (isa<VarDecl>(D))
50 return CXCursor_VarRef;
51 else if (isa<FunctionDecl>(D))
52 return CXCursor_FunctionRef;
53 else if (isa<EnumConstantDecl>(D))
54 return CXCursor_EnumConstantRef;
55 else
56 return CXCursor_NotImplemented;
57}
58
59#if 0
60// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000061class CRefVisitor : public StmtVisitor<CRefVisitor> {
62 CXDecl CDecl;
63 CXDeclIterator Callback;
64 CXClientData CData;
65
66 void Call(enum CXCursorKind CK, Stmt *SRef) {
67 CXCursor C = { CK, CDecl, SRef };
68 Callback(CDecl, C, CData);
69 }
70
71public:
72 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
73 CDecl(C), Callback(cback), CData(D) {}
74
75 void VisitStmt(Stmt *S) {
76 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
77 C != CEnd; ++C)
78 Visit(*C);
79 }
80 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000081 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000082 }
83 void VisitMemberExpr(MemberExpr *Node) {
84 Call(CXCursor_MemberRef, Node);
85 }
86 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
87 Call(CXCursor_ObjCSelectorRef, Node);
88 }
89 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
90 Call(CXCursor_ObjCIvarRef, Node);
91 }
92};
Steve Naroff4ade6d62009-09-23 17:52:52 +000093#endif
Ted Kremenekfc062212009-10-19 21:44:57 +000094
95/// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted
96/// warnings and errors.
97class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient {
98public:
99 virtual ~IgnoreDiagnosticsClient() {}
100 virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {}
101};
Steve Narofffb570422009-09-22 19:25:29 +0000102
Steve Naroff89922f82009-08-31 00:59:03 +0000103// Translation Unit Visitor.
104class TUVisitor : public DeclVisitor<TUVisitor> {
105 CXTranslationUnit TUnit;
106 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000107 CXClientData CData;
108
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000109 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
110 // to the visitor. Declarations with a PCH level greater than this value will
111 // be suppressed.
112 unsigned MaxPCHLevel;
113
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000114 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000115 // Filter any declarations that have a PCH level greater than what we allow.
116 if (ND->getPCHLevel() > MaxPCHLevel)
117 return;
118
Steve Narofff96b5242009-10-28 20:44:47 +0000119 // Filter any implicit declarations (since the source info will be bogus).
120 if (ND->isImplicit())
121 return;
122
Steve Narofffb570422009-09-22 19:25:29 +0000123 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000124 Callback(TUnit, C, CData);
125 }
Steve Naroff89922f82009-08-31 00:59:03 +0000126public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000127 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000128 CXTranslationUnitIterator cback, CXClientData D,
129 unsigned MaxPCHLevel) :
130 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000131
132 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
133 VisitDeclContext(dyn_cast<DeclContext>(D));
134 }
135 void VisitDeclContext(DeclContext *DC) {
136 for (DeclContext::decl_iterator
137 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
138 Visit(*I);
139 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000140 void VisitTypedefDecl(TypedefDecl *ND) {
141 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000142 }
143 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000144 switch (ND->getTagKind()) {
145 case TagDecl::TK_struct:
146 Call(CXCursor_StructDecl, ND);
147 break;
148 case TagDecl::TK_class:
149 Call(CXCursor_ClassDecl, ND);
150 break;
151 case TagDecl::TK_union:
152 Call(CXCursor_UnionDecl, ND);
153 break;
154 case TagDecl::TK_enum:
155 Call(CXCursor_EnumDecl, ND);
156 break;
157 }
Steve Naroff89922f82009-08-31 00:59:03 +0000158 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000159 void VisitVarDecl(VarDecl *ND) {
160 Call(CXCursor_VarDecl, ND);
161 }
Steve Naroff89922f82009-08-31 00:59:03 +0000162 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000163 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
164 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000165 }
166 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000167 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000168 }
169 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000170 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000171 }
172 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000173 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000174 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000175 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
176 Call(CXCursor_ObjCClassDefn, ND);
177 }
178 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
179 Call(CXCursor_ObjCCategoryDefn, ND);
180 }
Steve Naroff89922f82009-08-31 00:59:03 +0000181};
182
Steve Naroffc857ea42009-09-02 13:28:54 +0000183// Declaration visitor.
184class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
185 CXDecl CDecl;
186 CXDeclIterator Callback;
187 CXClientData CData;
188
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000189 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
190 // to the visitor. Declarations with a PCH level greater than this value will
191 // be suppressed.
192 unsigned MaxPCHLevel;
193
Steve Naroffc857ea42009-09-02 13:28:54 +0000194 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000195 // Disable the callback when the context is equal to the visiting decl.
196 if (CDecl == ND && !clang_isReference(CK))
197 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000198
199 // Filter any declarations that have a PCH level greater than what we allow.
200 if (ND->getPCHLevel() > MaxPCHLevel)
201 return;
202
Steve Narofffb570422009-09-22 19:25:29 +0000203 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000204 Callback(CDecl, C, CData);
205 }
Steve Naroff89922f82009-08-31 00:59:03 +0000206public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000207 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
208 unsigned MaxPCHLevel) :
209 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000210
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000211 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
212 // Issue callbacks for the containing class.
213 Call(CXCursor_ObjCClassRef, ND);
214 // FIXME: Issue callbacks for protocol refs.
215 VisitDeclContext(dyn_cast<DeclContext>(ND));
216 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000217 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000218 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000219 if (D->getSuperClass())
220 Call(CXCursor_ObjCSuperClassRef, D);
221
Steve Naroff9efa7672009-09-04 15:44:05 +0000222 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
223 E = D->protocol_end(); I != E; ++I)
224 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000225 VisitDeclContext(dyn_cast<DeclContext>(D));
226 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000227 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
228 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
229 E = PID->protocol_end(); I != E; ++I)
230 Call(CXCursor_ObjCProtocolRef, *I);
231
232 VisitDeclContext(dyn_cast<DeclContext>(PID));
233 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000234 void VisitTagDecl(TagDecl *D) {
235 VisitDeclContext(dyn_cast<DeclContext>(D));
236 }
237 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
238 VisitDeclContext(dyn_cast<DeclContext>(D));
239 }
240 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
241 VisitDeclContext(dyn_cast<DeclContext>(D));
242 }
Steve Naroff89922f82009-08-31 00:59:03 +0000243 void VisitDeclContext(DeclContext *DC) {
244 for (DeclContext::decl_iterator
245 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
246 Visit(*I);
247 }
248 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000249 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000250 }
251 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000252 Call(CXCursor_FieldDecl, ND);
253 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000254 void VisitVarDecl(VarDecl *ND) {
255 Call(CXCursor_VarDecl, ND);
256 }
257 void VisitParmVarDecl(ParmVarDecl *ND) {
258 Call(CXCursor_ParmDecl, ND);
259 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000260 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
261 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000262 }
263 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000264 Call(CXCursor_ObjCIvarDecl, ND);
265 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000266 void VisitFunctionDecl(FunctionDecl *ND) {
267 if (ND->isThisDeclarationADefinition()) {
268 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000269#if 0
270 // Not currently needed.
271 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000272 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000273 RVisit.Visit(Body);
274#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000275 }
276 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000277 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
278 if (ND->getBody()) {
279 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
280 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000281 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000282 } else
283 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
284 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000285 }
286};
287
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000288class CIndexer : public Indexer {
289public:
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000290 explicit CIndexer(Program *prog) : Indexer(*prog),
291 OnlyLocalDecls(false),
292 DisplayDiagnostics(false) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000293
294 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000295
296 /// \brief Whether we only want to see "local" declarations (that did not
297 /// come from a previous precompiled header). If false, we want to see all
298 /// declarations.
299 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
300 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000301
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000302 void setDisplayDiagnostics(bool Display = true) {
303 DisplayDiagnostics = Display;
304 }
305 bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
306
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000307 /// \brief Get the path of the clang binary.
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000308 const llvm::sys::Path& getClangPath();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000309private:
310 bool OnlyLocalDecls;
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000311 bool DisplayDiagnostics;
312
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000313 llvm::sys::Path ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000314};
Steve Naroff89922f82009-08-31 00:59:03 +0000315
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000316const llvm::sys::Path& CIndexer::getClangPath() {
317 // Did we already compute the path?
318 if (!ClangPath.empty())
319 return ClangPath;
320
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000321 // Find the location where this library lives (libCIndex.dylib).
Benjamin Kramer20d75812009-10-18 16:13:48 +0000322#ifdef LLVM_ON_WIN32
323 MEMORY_BASIC_INFORMATION mbi;
324 char path[MAX_PATH];
Benjamin Krameredcd8282009-10-18 16:20:58 +0000325 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
Benjamin Kramer20d75812009-10-18 16:13:48 +0000326 sizeof(mbi));
327 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
328
329 llvm::sys::Path CIndexPath(path);
330#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000331 // This silly cast below avoids a C++ warning.
332 Dl_info info;
333 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
334 assert(0 && "Call to dladdr() failed");
335
336 llvm::sys::Path CIndexPath(info.dli_fname);
Daniel Dunbara47dd192009-10-17 23:53:11 +0000337#endif
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000338
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000339 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000340 CIndexPath.eraseComponent();
341 CIndexPath.eraseComponent();
342 CIndexPath.appendComponent("bin");
343 CIndexPath.appendComponent("clang");
344
345 // Cache our result.
346 ClangPath = CIndexPath;
347 return ClangPath;
348}
349
350}
351
352extern "C" {
353
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000354CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
355 int displayDiagnostics)
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000356{
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000357 CIndexer *CIdxr = new CIndexer(new Program());
358 if (excludeDeclarationsFromPCH)
359 CIdxr->setOnlyLocalDecls();
360 if (displayDiagnostics)
361 CIdxr->setDisplayDiagnostics();
362 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000363}
364
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000365void clang_disposeIndex(CXIndex CIdx)
366{
367 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000368 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000369}
370
Steve Naroff50398192009-08-28 15:28:48 +0000371// FIXME: need to pass back error info.
372CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000373 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000374{
Steve Naroff50398192009-08-28 15:28:48 +0000375 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000376 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000377 std::string astName(ast_filename);
378 std::string ErrMsg;
379
Ted Kremenek379afec2009-10-22 03:24:01 +0000380 CXTranslationUnit TU =
381 ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
382 CXXIdx->getDisplayDiagnostics() ?
383 NULL : new IgnoreDiagnosticsClient(),
384 CXXIdx->getOnlyLocalDecls(),
385 /* UseBumpAllocator = */ true);
386
Ted Kremenek779e5f42009-10-26 22:08:39 +0000387 if (!ErrMsg.empty())
388 llvm::errs() << "clang_createTranslationUnit: " << ErrMsg << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000389
390 return TU;
Steve Naroff600866c2009-08-27 19:51:58 +0000391}
392
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000393CXTranslationUnit clang_createTranslationUnitFromSourceFile(
394 CXIndex CIdx,
395 const char *source_filename,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000396 int num_command_line_args, const char **command_line_args) {
397 assert(CIdx && "Passed null CXIndex");
398 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
399
Ted Kremenek139ba862009-10-22 00:03:57 +0000400 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000401 std::vector<const char *> argv;
Ted Kremenek139ba862009-10-22 00:03:57 +0000402
403 // First add the complete path to the 'clang' executable.
404 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000405 argv.push_back(ClangPath.c_str());
Ted Kremenek139ba862009-10-22 00:03:57 +0000406
407 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000408 argv.push_back("-emit-ast");
Ted Kremenek139ba862009-10-22 00:03:57 +0000409
410 // The 'source_filename' argument is optional. If the caller does not
411 // specify it then it is assumed that the source file is specified
412 // in the actual argument list.
413 if (source_filename)
414 argv.push_back(source_filename);
415
Steve Naroff37b5ac22009-10-15 20:50:09 +0000416 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000417 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000418 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000419 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000420
421 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
422 for (int i = 0; i < num_command_line_args; ++i)
423 if (const char *arg = command_line_args[i]) {
424 if (strcmp(arg, "-o") == 0) {
425 ++i; // Also skip the matching argument.
426 continue;
427 }
428 if (strcmp(arg, "-emit-ast") == 0 ||
429 strcmp(arg, "-c") == 0 ||
430 strcmp(arg, "-fsyntax-only") == 0) {
431 continue;
432 }
433
434 // Keep the argument.
435 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000436 }
Ted Kremenek139ba862009-10-22 00:03:57 +0000437
438 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000439 argv.push_back(NULL);
440
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000441 // Invoke 'clang'.
442 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
443 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000444 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000445 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000446 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
447 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
448 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
449
Ted Kremenek8ee1f3f2009-10-22 22:19:00 +0000450 if (!ErrMsg.empty()) {
Ted Kremenek379afec2009-10-22 03:24:01 +0000451 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
452 << '\n' << "Arguments: \n";
453 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000454 I!=E; ++I) {
455 if (*I)
456 llvm::errs() << ' ' << *I << '\n';
457 }
458 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000459 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000460
Steve Naroff37b5ac22009-10-15 20:50:09 +0000461 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000462 ASTUnit *ATU = static_cast<ASTUnit *>(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000463 clang_createTranslationUnit(CIdx, astTmpFile));
464 if (ATU)
465 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000466 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000467}
468
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000469void clang_disposeTranslationUnit(
470 CXTranslationUnit CTUnit)
471{
472 assert(CTUnit && "Passed null CXTranslationUnit");
473 delete static_cast<ASTUnit *>(CTUnit);
474}
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000475
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000476const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
477{
478 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000479 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
480 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000481}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000482
Steve Naroffc857ea42009-09-02 13:28:54 +0000483void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
484 CXTranslationUnitIterator callback,
485 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000486{
Steve Naroff50398192009-08-28 15:28:48 +0000487 assert(CTUnit && "Passed null CXTranslationUnit");
488 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
489 ASTContext &Ctx = CXXUnit->getASTContext();
490
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000491 TUVisitor DVisit(CTUnit, callback, CData,
492 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000493 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000494}
495
Steve Naroffc857ea42009-09-02 13:28:54 +0000496void clang_loadDeclaration(CXDecl Dcl,
497 CXDeclIterator callback,
498 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000499{
Steve Naroffc857ea42009-09-02 13:28:54 +0000500 assert(Dcl && "Passed null CXDecl");
501
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000502 CDeclVisitor DVisit(Dcl, callback, CData,
503 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000504 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000505}
506
Steve Naroff7e8f8182009-08-28 12:07:44 +0000507// Some notes on CXEntity:
508//
509// - Since the 'ordinary' namespace includes functions, data, typedefs,
510// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
511// entity for 2 different types). For example:
512//
513// module1.m: @interface Foo @end Foo *x;
514// module2.m: void Foo(int);
515//
516// - Since the unique name spans translation units, static data/functions
517// within a CXTranslationUnit are *not* currently represented by entities.
518// As a result, there will be no entity for the following:
519//
520// module.m: static void Foo() { }
521//
522
523
Steve Naroff600866c2009-08-27 19:51:58 +0000524const char *clang_getDeclarationName(CXEntity)
525{
526 return "";
527}
528const char *clang_getURI(CXEntity)
529{
530 return "";
531}
532
533CXEntity clang_getEntity(const char *URI)
534{
535 return 0;
536}
537
538//
539// CXDecl Operations.
540//
Steve Naroff600866c2009-08-27 19:51:58 +0000541CXEntity clang_getEntityFromDecl(CXDecl)
542{
543 return 0;
544}
Steve Naroff89922f82009-08-31 00:59:03 +0000545const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000546{
Steve Naroff89922f82009-08-31 00:59:03 +0000547 assert(AnonDecl && "Passed null CXDecl");
548 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000549
550 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
551 return OMD->getSelector().getAsString().c_str();
Steve Narofff9f61962009-10-29 18:55:50 +0000552 }
553 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000554 // No, this isn't the same as the code below. getIdentifier() is non-virtual
555 // and returns different names. NamedDecl returns the class name and
556 // ObjCCategoryImplDecl returns the category name.
557 return CIMP->getIdentifier()->getNameStart();
Steve Narofff9f61962009-10-29 18:55:50 +0000558
Steve Naroff89922f82009-08-31 00:59:03 +0000559 if (ND->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000560 return ND->getIdentifier()->getNameStart();
Steve Naroffc857ea42009-09-02 13:28:54 +0000561 else
Steve Naroff89922f82009-08-31 00:59:03 +0000562 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000563}
Steve Narofff334b4e2009-09-02 18:26:48 +0000564
Steve Naroff699a07d2009-09-25 21:32:34 +0000565unsigned clang_getDeclLine(CXDecl AnonDecl)
566{
567 assert(AnonDecl && "Passed null CXDecl");
568 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
569 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
570 return SourceMgr.getSpellingLineNumber(ND->getLocation());
571}
572
573unsigned clang_getDeclColumn(CXDecl AnonDecl)
574{
575 assert(AnonDecl && "Passed null CXDecl");
576 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
577 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000578 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000579}
580
Steve Naroffee9405e2009-09-25 21:45:39 +0000581const char *clang_getDeclSource(CXDecl AnonDecl)
582{
583 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff88145032009-10-27 14:35:18 +0000584 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
585 assert (FEnt && "Cannot find FileEntry for Decl");
586 return clang_getFileName(FEnt);
587}
588
589static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
590 SourceLocation SLoc)
591{
592 FileID FID;
593 if (SLoc.isFileID())
594 FID = SMgr.getFileID(SLoc);
595 else
596 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
597 return SMgr.getFileEntryForID(FID);
598}
599
600CXFile clang_getDeclSourceFile(CXDecl AnonDecl)
601{
602 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000603 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
604 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000605 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
606}
607
608const char *clang_getFileName(CXFile SFile) {
609 assert(SFile && "Passed null CXFile");
610 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
611 return FEnt->getName();
612}
613
614time_t clang_getFileTime(CXFile SFile) {
615 assert(SFile && "Passed null CXFile");
616 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
617 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000618}
619
Steve Narofff334b4e2009-09-02 18:26:48 +0000620const char *clang_getCursorSpelling(CXCursor C)
621{
622 assert(C.decl && "CXCursor has null decl");
623 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
624
625 if (clang_isReference(C.kind)) {
626 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000627 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000628 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
629 assert(OID && "clang_getCursorLine(): Missing interface decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000630 return OID->getSuperClass()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000631 }
632 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000633 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000634 return OID->getIdentifier()->getNameStart();
Steve Naroff85e2db72009-10-01 00:31:07 +0000635 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000636 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
637 assert(OID && "clang_getCursorLine(): Missing category decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000638 return OID->getClassInterface()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000639 }
640 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000641 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
642 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000643 return OID->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000644 }
645 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000646 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
647 static_cast<Stmt *>(C.stmt));
648 assert(OME && "clang_getCursorLine(): Missing message expr");
649 return OME->getSelector().getAsString().c_str();
Steve Naroffbade7de2009-10-19 13:41:39 +0000650 }
Steve Narofffb570422009-09-22 19:25:29 +0000651 case CXCursor_VarRef:
652 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000653 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000654 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
655 static_cast<Stmt *>(C.stmt));
656 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Daniel Dunbare013d682009-10-18 20:26:12 +0000657 return DRE->getDecl()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000658 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000659 default:
660 return "<not implemented>";
661 }
662 }
663 return clang_getDeclSpelling(C.decl);
664}
665
666const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000667{
Steve Naroff89922f82009-08-31 00:59:03 +0000668 switch (Kind) {
669 case CXCursor_FunctionDecl: return "FunctionDecl";
670 case CXCursor_TypedefDecl: return "TypedefDecl";
671 case CXCursor_EnumDecl: return "EnumDecl";
672 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000673 case CXCursor_StructDecl: return "StructDecl";
674 case CXCursor_UnionDecl: return "UnionDecl";
675 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000676 case CXCursor_FieldDecl: return "FieldDecl";
677 case CXCursor_VarDecl: return "VarDecl";
678 case CXCursor_ParmDecl: return "ParmDecl";
679 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
680 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
681 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
682 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
683 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000684 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
685 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
686 case CXCursor_FunctionDefn: return "FunctionDefn";
687 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
688 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
689 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
690 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000691 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000692 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000693 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000694 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
695
696 case CXCursor_VarRef: return "VarRef";
697 case CXCursor_FunctionRef: return "FunctionRef";
698 case CXCursor_EnumConstantRef: return "EnumConstantRef";
699 case CXCursor_MemberRef: return "MemberRef";
700
Steve Naroff77128dd2009-09-15 20:25:34 +0000701 case CXCursor_InvalidFile: return "InvalidFile";
702 case CXCursor_NoDeclFound: return "NoDeclFound";
703 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000704 default: return "<not implemented>";
705 }
Steve Naroff600866c2009-08-27 19:51:58 +0000706}
Steve Naroff89922f82009-08-31 00:59:03 +0000707
Steve Naroff9efa7672009-09-04 15:44:05 +0000708static enum CXCursorKind TranslateKind(Decl *D) {
709 switch (D->getKind()) {
710 case Decl::Function: return CXCursor_FunctionDecl;
711 case Decl::Typedef: return CXCursor_TypedefDecl;
712 case Decl::Enum: return CXCursor_EnumDecl;
713 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
714 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
715 case Decl::Field: return CXCursor_FieldDecl;
716 case Decl::Var: return CXCursor_VarDecl;
717 case Decl::ParmVar: return CXCursor_ParmDecl;
718 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000719 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
720 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000721 case Decl::ObjCMethod: {
722 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
723 if (MD->isInstanceMethod())
724 return CXCursor_ObjCInstanceMethodDecl;
725 return CXCursor_ObjCClassMethodDecl;
726 }
727 default: break;
728 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000729 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000730}
Steve Naroff600866c2009-08-27 19:51:58 +0000731//
732// CXCursor Operations.
733//
Steve Naroff9efa7672009-09-04 15:44:05 +0000734CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Narofff96b5242009-10-28 20:44:47 +0000735 unsigned line, unsigned column)
Steve Naroff600866c2009-08-27 19:51:58 +0000736{
Steve Naroff9efa7672009-09-04 15:44:05 +0000737 assert(CTUnit && "Passed null CXTranslationUnit");
738 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
739
740 FileManager &FMgr = CXXUnit->getFileManager();
741 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000742 source_name+strlen(source_name));
743 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000744 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000745 return C;
746 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000747 SourceLocation SLoc =
748 CXXUnit->getSourceManager().getLocation(File, line, column);
749
Steve Narofff96b5242009-10-28 20:44:47 +0000750 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
751
752 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
753 &LastLoc);
754 if (ALoc.isValid())
755 CXXUnit->setLastASTLocation(ALoc);
756
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"