blob: eff602ff8e84b6620429040d37d99ac6624a6587 [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Clang-C Source Indexing library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang-c/Index.h"
Steve Naroff50398192009-08-28 15:28:48 +000015#include "clang/Index/Program.h"
16#include "clang/Index/Indexer.h"
Steve Naroff9efa7672009-09-04 15:44:05 +000017#include "clang/Index/ASTLocation.h"
18#include "clang/Index/Utils.h"
Steve Naroff50398192009-08-28 15:28:48 +000019#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000020#include "clang/AST/StmtVisitor.h"
Steve Naroffc857ea42009-09-02 13:28:54 +000021#include "clang/AST/Decl.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000022#include "clang/Basic/FileManager.h"
Steve Naroff2d4d6292009-08-31 14:26:51 +000023#include "clang/Basic/SourceManager.h"
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000024#include "clang/Frontend/ASTUnit.h"
Benjamin Kramer20d75812009-10-18 16:13:48 +000025#include "llvm/Config/config.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000026#include "llvm/Support/Compiler.h"
Douglas Gregor02465752009-10-16 21:24:31 +000027#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/System/Path.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000029#include "llvm/System/Program.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000030
Benjamin Kramerd01a0bc2009-08-29 12:56:35 +000031#include <cstdio>
Ted Kremenek49358d82009-10-19 21:17:25 +000032#include <vector>
33
Benjamin Kramer20d75812009-10-18 16:13:48 +000034#ifdef LLVM_ON_WIN32
35#define WIN32_LEAN_AND_MEAN
36#include <windows.h>
37#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +000038#include <dlfcn.h>
Daniel Dunbara47dd192009-10-17 23:53:11 +000039#endif
Steve Naroff5b7d8e22009-10-15 20:04:39 +000040
Steve Naroff50398192009-08-28 15:28:48 +000041using namespace clang;
42using namespace idx;
43
Steve Naroff89922f82009-08-31 00:59:03 +000044namespace {
Steve Naroff4ade6d62009-09-23 17:52:52 +000045static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE)
46{
47 NamedDecl *D = DRE->getDecl();
48 if (isa<VarDecl>(D))
49 return CXCursor_VarRef;
50 else if (isa<FunctionDecl>(D))
51 return CXCursor_FunctionRef;
52 else if (isa<EnumConstantDecl>(D))
53 return CXCursor_EnumConstantRef;
54 else
55 return CXCursor_NotImplemented;
56}
57
58#if 0
59// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000060class CRefVisitor : public StmtVisitor<CRefVisitor> {
61 CXDecl CDecl;
62 CXDeclIterator Callback;
63 CXClientData CData;
64
65 void Call(enum CXCursorKind CK, Stmt *SRef) {
66 CXCursor C = { CK, CDecl, SRef };
67 Callback(CDecl, C, CData);
68 }
69
70public:
71 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
72 CDecl(C), Callback(cback), CData(D) {}
73
74 void VisitStmt(Stmt *S) {
75 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
76 C != CEnd; ++C)
77 Visit(*C);
78 }
79 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000080 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000081 }
82 void VisitMemberExpr(MemberExpr *Node) {
83 Call(CXCursor_MemberRef, Node);
84 }
85 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
86 Call(CXCursor_ObjCSelectorRef, Node);
87 }
88 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
89 Call(CXCursor_ObjCIvarRef, Node);
90 }
91};
Steve Naroff4ade6d62009-09-23 17:52:52 +000092#endif
Ted Kremenekfc062212009-10-19 21:44:57 +000093
94/// IgnoreDiagnosticsClient - A DiagnosticsClient that just ignores emitted
95/// warnings and errors.
96class VISIBILITY_HIDDEN IgnoreDiagnosticsClient : public DiagnosticClient {
97public:
98 virtual ~IgnoreDiagnosticsClient() {}
99 virtual void HandleDiagnostic(Diagnostic::Level, const DiagnosticInfo &) {}
100};
Steve Narofffb570422009-09-22 19:25:29 +0000101
Steve Naroff89922f82009-08-31 00:59:03 +0000102// Translation Unit Visitor.
103class TUVisitor : public DeclVisitor<TUVisitor> {
104 CXTranslationUnit TUnit;
105 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000106 CXClientData CData;
107
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000108 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
109 // to the visitor. Declarations with a PCH level greater than this value will
110 // be suppressed.
111 unsigned MaxPCHLevel;
112
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000113 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000114 // Filter any declarations that have a PCH level greater than what we allow.
115 if (ND->getPCHLevel() > MaxPCHLevel)
116 return;
117
Steve Narofffb570422009-09-22 19:25:29 +0000118 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000119 Callback(TUnit, C, CData);
120 }
Steve Naroff89922f82009-08-31 00:59:03 +0000121public:
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000122 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000123 CXTranslationUnitIterator cback, CXClientData D,
124 unsigned MaxPCHLevel) :
125 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroff89922f82009-08-31 00:59:03 +0000126
127 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
128 VisitDeclContext(dyn_cast<DeclContext>(D));
129 }
130 void VisitDeclContext(DeclContext *DC) {
131 for (DeclContext::decl_iterator
132 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
133 Visit(*I);
134 }
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000135 void VisitTypedefDecl(TypedefDecl *ND) {
136 Call(CXCursor_TypedefDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000137 }
138 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000139 switch (ND->getTagKind()) {
140 case TagDecl::TK_struct:
141 Call(CXCursor_StructDecl, ND);
142 break;
143 case TagDecl::TK_class:
144 Call(CXCursor_ClassDecl, ND);
145 break;
146 case TagDecl::TK_union:
147 Call(CXCursor_UnionDecl, ND);
148 break;
149 case TagDecl::TK_enum:
150 Call(CXCursor_EnumDecl, ND);
151 break;
152 }
Steve Naroff89922f82009-08-31 00:59:03 +0000153 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000154 void VisitVarDecl(VarDecl *ND) {
155 Call(CXCursor_VarDecl, ND);
156 }
Steve Naroff89922f82009-08-31 00:59:03 +0000157 void VisitFunctionDecl(FunctionDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000158 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
159 : CXCursor_FunctionDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000160 }
161 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000162 Call(CXCursor_ObjCInterfaceDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000163 }
164 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000165 Call(CXCursor_ObjCCategoryDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000166 }
167 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000168 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000169 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000170 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
171 Call(CXCursor_ObjCClassDefn, ND);
172 }
173 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
174 Call(CXCursor_ObjCCategoryDefn, ND);
175 }
Steve Naroff89922f82009-08-31 00:59:03 +0000176};
177
Steve Naroffc857ea42009-09-02 13:28:54 +0000178// Declaration visitor.
179class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
180 CXDecl CDecl;
181 CXDeclIterator Callback;
182 CXClientData CData;
183
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000184 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
185 // to the visitor. Declarations with a PCH level greater than this value will
186 // be suppressed.
187 unsigned MaxPCHLevel;
188
Steve Naroffc857ea42009-09-02 13:28:54 +0000189 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000190 // Disable the callback when the context is equal to the visiting decl.
191 if (CDecl == ND && !clang_isReference(CK))
192 return;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000193
194 // Filter any declarations that have a PCH level greater than what we allow.
195 if (ND->getPCHLevel() > MaxPCHLevel)
196 return;
197
Steve Narofffb570422009-09-22 19:25:29 +0000198 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000199 Callback(CDecl, C, CData);
200 }
Steve Naroff89922f82009-08-31 00:59:03 +0000201public:
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000202 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
203 unsigned MaxPCHLevel) :
204 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Steve Naroffc857ea42009-09-02 13:28:54 +0000205
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000206 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
207 // Issue callbacks for the containing class.
208 Call(CXCursor_ObjCClassRef, ND);
209 // FIXME: Issue callbacks for protocol refs.
210 VisitDeclContext(dyn_cast<DeclContext>(ND));
211 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000212 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000213 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000214 if (D->getSuperClass())
215 Call(CXCursor_ObjCSuperClassRef, D);
216
Steve Naroff9efa7672009-09-04 15:44:05 +0000217 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
218 E = D->protocol_end(); I != E; ++I)
219 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000220 VisitDeclContext(dyn_cast<DeclContext>(D));
221 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000222 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
223 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
224 E = PID->protocol_end(); I != E; ++I)
225 Call(CXCursor_ObjCProtocolRef, *I);
226
227 VisitDeclContext(dyn_cast<DeclContext>(PID));
228 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000229 void VisitTagDecl(TagDecl *D) {
230 VisitDeclContext(dyn_cast<DeclContext>(D));
231 }
232 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
233 VisitDeclContext(dyn_cast<DeclContext>(D));
234 }
235 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
236 VisitDeclContext(dyn_cast<DeclContext>(D));
237 }
Steve Naroff89922f82009-08-31 00:59:03 +0000238 void VisitDeclContext(DeclContext *DC) {
239 for (DeclContext::decl_iterator
240 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
241 Visit(*I);
242 }
243 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000244 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000245 }
246 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000247 Call(CXCursor_FieldDecl, ND);
248 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000249 void VisitVarDecl(VarDecl *ND) {
250 Call(CXCursor_VarDecl, ND);
251 }
252 void VisitParmVarDecl(ParmVarDecl *ND) {
253 Call(CXCursor_ParmDecl, ND);
254 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000255 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
256 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000257 }
258 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000259 Call(CXCursor_ObjCIvarDecl, ND);
260 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000261 void VisitFunctionDecl(FunctionDecl *ND) {
262 if (ND->isThisDeclarationADefinition()) {
263 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000264#if 0
265 // Not currently needed.
266 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000267 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000268 RVisit.Visit(Body);
269#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000270 }
271 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000272 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
273 if (ND->getBody()) {
274 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
275 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000276 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000277 } else
278 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
279 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000280 }
281};
282
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000283class CIndexer : public Indexer {
284public:
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000285 explicit CIndexer(Program *prog) : Indexer(*prog),
286 OnlyLocalDecls(false),
287 DisplayDiagnostics(false) {}
Ted Kremenekdff76892009-10-17 06:21:47 +0000288
289 virtual ~CIndexer() { delete &getProgram(); }
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000290
291 /// \brief Whether we only want to see "local" declarations (that did not
292 /// come from a previous precompiled header). If false, we want to see all
293 /// declarations.
294 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
295 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
Benjamin Kramer96707622009-10-18 11:10:55 +0000296
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000297 void setDisplayDiagnostics(bool Display = true) {
298 DisplayDiagnostics = Display;
299 }
300 bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
301
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000302 /// \brief Get the path of the clang binary.
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000303 const llvm::sys::Path& getClangPath();
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000304private:
305 bool OnlyLocalDecls;
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000306 bool DisplayDiagnostics;
307
Benjamin Kramerc5a9e952009-10-19 10:20:24 +0000308 llvm::sys::Path ClangPath;
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000309};
Steve Naroff89922f82009-08-31 00:59:03 +0000310
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000311const llvm::sys::Path& CIndexer::getClangPath() {
312 // Did we already compute the path?
313 if (!ClangPath.empty())
314 return ClangPath;
315
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000316 // Find the location where this library lives (libCIndex.dylib).
Benjamin Kramer20d75812009-10-18 16:13:48 +0000317#ifdef LLVM_ON_WIN32
318 MEMORY_BASIC_INFORMATION mbi;
319 char path[MAX_PATH];
Benjamin Krameredcd8282009-10-18 16:20:58 +0000320 VirtualQuery((void *)(uintptr_t)clang_createTranslationUnit, &mbi,
Benjamin Kramer20d75812009-10-18 16:13:48 +0000321 sizeof(mbi));
322 GetModuleFileNameA((HINSTANCE)mbi.AllocationBase, path, MAX_PATH);
323
324 llvm::sys::Path CIndexPath(path);
325#else
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000326 // This silly cast below avoids a C++ warning.
327 Dl_info info;
328 if (dladdr((void *)(uintptr_t)clang_createTranslationUnit, &info) == 0)
329 assert(0 && "Call to dladdr() failed");
330
331 llvm::sys::Path CIndexPath(info.dli_fname);
Daniel Dunbara47dd192009-10-17 23:53:11 +0000332#endif
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000333
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000334 // We now have the CIndex directory, locate clang relative to it.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000335 CIndexPath.eraseComponent();
336 CIndexPath.eraseComponent();
337 CIndexPath.appendComponent("bin");
338 CIndexPath.appendComponent("clang");
339
340 // Cache our result.
341 ClangPath = CIndexPath;
342 return ClangPath;
343}
344
345}
346
347extern "C" {
348
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000349CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
350 int displayDiagnostics)
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000351{
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000352 CIndexer *CIdxr = new CIndexer(new Program());
353 if (excludeDeclarationsFromPCH)
354 CIdxr->setOnlyLocalDecls();
355 if (displayDiagnostics)
356 CIdxr->setDisplayDiagnostics();
357 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000358}
359
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000360void clang_disposeIndex(CXIndex CIdx)
361{
362 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000363 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000364}
365
Steve Naroff50398192009-08-28 15:28:48 +0000366// FIXME: need to pass back error info.
367CXTranslationUnit clang_createTranslationUnit(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000368 CXIndex CIdx, const char *ast_filename)
Steve Naroff600866c2009-08-27 19:51:58 +0000369{
Steve Naroff50398192009-08-28 15:28:48 +0000370 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000371 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Steve Naroff50398192009-08-28 15:28:48 +0000372 std::string astName(ast_filename);
373 std::string ErrMsg;
374
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000375 return ASTUnit::LoadFromPCHFile(astName, &ErrMsg,
376 CXXIdx->getDisplayDiagnostics() ?
377 NULL : new IgnoreDiagnosticsClient(),
Ted Kremenek5cf48762009-10-17 00:34:24 +0000378 CXXIdx->getOnlyLocalDecls(),
379 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000380}
381
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000382CXTranslationUnit clang_createTranslationUnitFromSourceFile(
383 CXIndex CIdx,
384 const char *source_filename,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000385 int num_command_line_args, const char **command_line_args) {
386 assert(CIdx && "Passed null CXIndex");
387 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
388
Ted Kremenek74cd0692009-10-15 23:21:22 +0000389 // Build up the arguments for involing clang.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000390 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Ted Kremenek74cd0692009-10-15 23:21:22 +0000391 std::vector<const char *> argv;
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000392 argv.push_back(ClangPath.c_str());
Ted Kremenek74cd0692009-10-15 23:21:22 +0000393 argv.push_back("-emit-ast");
394 argv.push_back(source_filename);
395 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000396 // Generate a temporary name for the AST file.
397 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000398 argv.push_back(tmpnam(astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000399 for (int i = 0; i < num_command_line_args; i++) {
400 if (command_line_args[i] && strcmp(command_line_args[i], "-o") != 0)
401 argv.push_back(command_line_args[i]);
402 else {
403 if (++i < num_command_line_args) // Skip "-o"...
404 i++; // ...and the following argument as well.
405 }
406 }
Ted Kremenek74cd0692009-10-15 23:21:22 +0000407 argv.push_back(NULL);
408
Ted Kremenekc46e4632009-10-19 22:27:32 +0000409#ifndef LLVM_ON_WIN32
410 llvm::sys::Path DevNull("/dev/null");
411 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
412 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], NULL,
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000413 !CXXIdx->getDisplayDiagnostics() ?
414 &Redirects[0] : NULL);
Ted Kremenek13745982009-10-19 22:15:09 +0000415#else
416 // FIXME: I don't know what is the equivalent '/dev/null' redirect for
417 // Windows for this API.
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000418 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0]);
Ted Kremenek13745982009-10-19 22:15:09 +0000419#endif
Benjamin Kramer0829a832009-10-18 11:19:36 +0000420
Steve Naroff37b5ac22009-10-15 20:50:09 +0000421 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000422 ASTUnit *ATU = static_cast<ASTUnit *>(
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000423 clang_createTranslationUnit(CIdx, astTmpFile));
424 if (ATU)
425 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000426 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000427}
428
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000429void clang_disposeTranslationUnit(
430 CXTranslationUnit CTUnit)
431{
432 assert(CTUnit && "Passed null CXTranslationUnit");
433 delete static_cast<ASTUnit *>(CTUnit);
434}
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000435
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000436const char *clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit)
437{
438 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000439 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
440 return CXXUnit->getOriginalSourceFileName().c_str();
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000441}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000442
Steve Naroffc857ea42009-09-02 13:28:54 +0000443void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
444 CXTranslationUnitIterator callback,
445 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000446{
Steve Naroff50398192009-08-28 15:28:48 +0000447 assert(CTUnit && "Passed null CXTranslationUnit");
448 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
449 ASTContext &Ctx = CXXUnit->getASTContext();
450
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000451 TUVisitor DVisit(CTUnit, callback, CData,
452 CXXUnit->getOnlyLocalDecls()? 1 : Decl::MaxPCHLevel);
Steve Naroff50398192009-08-28 15:28:48 +0000453 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000454}
455
Steve Naroffc857ea42009-09-02 13:28:54 +0000456void clang_loadDeclaration(CXDecl Dcl,
457 CXDeclIterator callback,
458 CXClientData CData)
Steve Naroff600866c2009-08-27 19:51:58 +0000459{
Steve Naroffc857ea42009-09-02 13:28:54 +0000460 assert(Dcl && "Passed null CXDecl");
461
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000462 CDeclVisitor DVisit(Dcl, callback, CData,
463 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000464 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000465}
466
Steve Naroff7e8f8182009-08-28 12:07:44 +0000467// Some notes on CXEntity:
468//
469// - Since the 'ordinary' namespace includes functions, data, typedefs,
470// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
471// entity for 2 different types). For example:
472//
473// module1.m: @interface Foo @end Foo *x;
474// module2.m: void Foo(int);
475//
476// - Since the unique name spans translation units, static data/functions
477// within a CXTranslationUnit are *not* currently represented by entities.
478// As a result, there will be no entity for the following:
479//
480// module.m: static void Foo() { }
481//
482
483
Steve Naroff600866c2009-08-27 19:51:58 +0000484const char *clang_getDeclarationName(CXEntity)
485{
486 return "";
487}
488const char *clang_getURI(CXEntity)
489{
490 return "";
491}
492
493CXEntity clang_getEntity(const char *URI)
494{
495 return 0;
496}
497
498//
499// CXDecl Operations.
500//
Steve Naroff600866c2009-08-27 19:51:58 +0000501CXEntity clang_getEntityFromDecl(CXDecl)
502{
503 return 0;
504}
Steve Naroff89922f82009-08-31 00:59:03 +0000505const char *clang_getDeclSpelling(CXDecl AnonDecl)
Steve Naroff600866c2009-08-27 19:51:58 +0000506{
Steve Naroff89922f82009-08-31 00:59:03 +0000507 assert(AnonDecl && "Passed null CXDecl");
508 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffc857ea42009-09-02 13:28:54 +0000509
510 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND)) {
511 return OMD->getSelector().getAsString().c_str();
512 }
Steve Naroff89922f82009-08-31 00:59:03 +0000513 if (ND->getIdentifier())
Daniel Dunbare013d682009-10-18 20:26:12 +0000514 return ND->getIdentifier()->getNameStart();
Steve Naroffc857ea42009-09-02 13:28:54 +0000515 else
Steve Naroff89922f82009-08-31 00:59:03 +0000516 return "";
Steve Naroff600866c2009-08-27 19:51:58 +0000517}
Steve Narofff334b4e2009-09-02 18:26:48 +0000518
Steve Naroff699a07d2009-09-25 21:32:34 +0000519unsigned clang_getDeclLine(CXDecl AnonDecl)
520{
521 assert(AnonDecl && "Passed null CXDecl");
522 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
523 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
524 return SourceMgr.getSpellingLineNumber(ND->getLocation());
525}
526
527unsigned clang_getDeclColumn(CXDecl AnonDecl)
528{
529 assert(AnonDecl && "Passed null CXDecl");
530 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
531 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000532 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000533}
534
Steve Naroffee9405e2009-09-25 21:45:39 +0000535const char *clang_getDeclSource(CXDecl AnonDecl)
536{
537 assert(AnonDecl && "Passed null CXDecl");
538 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
539 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
540 return SourceMgr.getBufferName(ND->getLocation());
541}
542
Steve Narofff334b4e2009-09-02 18:26:48 +0000543const char *clang_getCursorSpelling(CXCursor C)
544{
545 assert(C.decl && "CXCursor has null decl");
546 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
547
548 if (clang_isReference(C.kind)) {
549 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000550 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000551 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
552 assert(OID && "clang_getCursorLine(): Missing interface decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000553 return OID->getSuperClass()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000554 }
555 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000556 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND)) {
Daniel Dunbare013d682009-10-18 20:26:12 +0000557 return OID->getIdentifier()->getNameStart();
Steve Naroff85e2db72009-10-01 00:31:07 +0000558 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000559 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
560 assert(OID && "clang_getCursorLine(): Missing category decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000561 return OID->getClassInterface()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000562 }
563 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000564 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
565 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Daniel Dunbare013d682009-10-18 20:26:12 +0000566 return OID->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000567 }
568 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000569 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
570 static_cast<Stmt *>(C.stmt));
571 assert(OME && "clang_getCursorLine(): Missing message expr");
572 return OME->getSelector().getAsString().c_str();
Steve Naroffbade7de2009-10-19 13:41:39 +0000573 }
Steve Narofffb570422009-09-22 19:25:29 +0000574 case CXCursor_VarRef:
575 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000576 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000577 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
578 static_cast<Stmt *>(C.stmt));
579 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Daniel Dunbare013d682009-10-18 20:26:12 +0000580 return DRE->getDecl()->getIdentifier()->getNameStart();
Steve Naroffbade7de2009-10-19 13:41:39 +0000581 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000582 default:
583 return "<not implemented>";
584 }
585 }
586 return clang_getDeclSpelling(C.decl);
587}
588
589const char *clang_getCursorKindSpelling(enum CXCursorKind Kind)
Steve Naroff600866c2009-08-27 19:51:58 +0000590{
Steve Naroff89922f82009-08-31 00:59:03 +0000591 switch (Kind) {
592 case CXCursor_FunctionDecl: return "FunctionDecl";
593 case CXCursor_TypedefDecl: return "TypedefDecl";
594 case CXCursor_EnumDecl: return "EnumDecl";
595 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000596 case CXCursor_StructDecl: return "StructDecl";
597 case CXCursor_UnionDecl: return "UnionDecl";
598 case CXCursor_ClassDecl: return "ClassDecl";
Steve Naroff89922f82009-08-31 00:59:03 +0000599 case CXCursor_FieldDecl: return "FieldDecl";
600 case CXCursor_VarDecl: return "VarDecl";
601 case CXCursor_ParmDecl: return "ParmDecl";
602 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
603 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
604 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
605 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
606 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Steve Naroffc857ea42009-09-02 13:28:54 +0000607 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
608 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
609 case CXCursor_FunctionDefn: return "FunctionDefn";
610 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
611 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
612 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
613 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Steve Narofff334b4e2009-09-02 18:26:48 +0000614 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
Steve Naroff9efa7672009-09-04 15:44:05 +0000615 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000616 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Steve Narofffb570422009-09-22 19:25:29 +0000617 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
618
619 case CXCursor_VarRef: return "VarRef";
620 case CXCursor_FunctionRef: return "FunctionRef";
621 case CXCursor_EnumConstantRef: return "EnumConstantRef";
622 case CXCursor_MemberRef: return "MemberRef";
623
Steve Naroff77128dd2009-09-15 20:25:34 +0000624 case CXCursor_InvalidFile: return "InvalidFile";
625 case CXCursor_NoDeclFound: return "NoDeclFound";
626 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000627 default: return "<not implemented>";
628 }
Steve Naroff600866c2009-08-27 19:51:58 +0000629}
Steve Naroff89922f82009-08-31 00:59:03 +0000630
Steve Naroff9efa7672009-09-04 15:44:05 +0000631static enum CXCursorKind TranslateKind(Decl *D) {
632 switch (D->getKind()) {
633 case Decl::Function: return CXCursor_FunctionDecl;
634 case Decl::Typedef: return CXCursor_TypedefDecl;
635 case Decl::Enum: return CXCursor_EnumDecl;
636 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
637 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
638 case Decl::Field: return CXCursor_FieldDecl;
639 case Decl::Var: return CXCursor_VarDecl;
640 case Decl::ParmVar: return CXCursor_ParmDecl;
641 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
Steve Narofffb570422009-09-22 19:25:29 +0000642 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
643 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
Steve Naroff9efa7672009-09-04 15:44:05 +0000644 case Decl::ObjCMethod: {
645 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
646 if (MD->isInstanceMethod())
647 return CXCursor_ObjCInstanceMethodDecl;
648 return CXCursor_ObjCClassMethodDecl;
649 }
650 default: break;
651 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000652 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000653}
Steve Naroff600866c2009-08-27 19:51:58 +0000654//
655// CXCursor Operations.
656//
Steve Naroff9efa7672009-09-04 15:44:05 +0000657CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Steve Naroff600866c2009-08-27 19:51:58 +0000658 unsigned line, unsigned column)
659{
Steve Naroff9efa7672009-09-04 15:44:05 +0000660 assert(CTUnit && "Passed null CXTranslationUnit");
661 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
662
663 FileManager &FMgr = CXXUnit->getFileManager();
664 const FileEntry *File = FMgr.getFile(source_name,
Steve Naroff77128dd2009-09-15 20:25:34 +0000665 source_name+strlen(source_name));
666 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000667 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000668 return C;
669 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000670 SourceLocation SLoc =
671 CXXUnit->getSourceManager().getLocation(File, line, column);
672
673 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc);
674
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000675 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000676 if (ALoc.isNamedRef())
677 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000678 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000679 if (Dcl) {
680 if (Stm) {
681 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
682 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
683 return C;
684 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
685 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
686 return C;
Steve Naroff85e2db72009-10-01 00:31:07 +0000687 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000688 // Fall through...treat as a decl, not a ref.
689 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000690 if (ALoc.isNamedRef()) {
691 if (isa<ObjCInterfaceDecl>(Dcl)) {
692 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
693 return C;
694 }
695 if (isa<ObjCProtocolDecl>(Dcl)) {
696 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
697 return C;
698 }
699 }
700 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000701 return C;
702 }
Steve Narofffb570422009-09-22 19:25:29 +0000703 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000704 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000705}
706
Steve Naroff77128dd2009-09-15 20:25:34 +0000707CXCursor clang_getCursorFromDecl(CXDecl AnonDecl)
708{
709 assert(AnonDecl && "Passed null CXDecl");
710 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
711
Steve Narofffb570422009-09-22 19:25:29 +0000712 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000713 return C;
714}
715
716unsigned clang_isInvalid(enum CXCursorKind K)
717{
718 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
719}
720
Steve Naroff89922f82009-08-31 00:59:03 +0000721unsigned clang_isDeclaration(enum CXCursorKind K)
722{
723 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
724}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000725
Steve Narofff334b4e2009-09-02 18:26:48 +0000726unsigned clang_isReference(enum CXCursorKind K)
727{
728 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
729}
730
731unsigned clang_isDefinition(enum CXCursorKind K)
732{
733 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
734}
735
Steve Naroff9efa7672009-09-04 15:44:05 +0000736CXCursorKind clang_getCursorKind(CXCursor C)
737{
738 return C.kind;
739}
740
Steve Naroff699a07d2009-09-25 21:32:34 +0000741static Decl *getDeclFromExpr(Stmt *E) {
742 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
743 return RefExpr->getDecl();
744 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
745 return ME->getMemberDecl();
746 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
747 return RE->getDecl();
748
749 if (CallExpr *CE = dyn_cast<CallExpr>(E))
750 return getDeclFromExpr(CE->getCallee());
751 if (CastExpr *CE = dyn_cast<CastExpr>(E))
752 return getDeclFromExpr(CE->getSubExpr());
753 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
754 return OME->getMethodDecl();
755
756 return 0;
757}
758
Steve Naroff9efa7672009-09-04 15:44:05 +0000759CXDecl clang_getCursorDecl(CXCursor C)
760{
Steve Naroff699a07d2009-09-25 21:32:34 +0000761 if (clang_isDeclaration(C.kind))
762 return C.decl;
763
764 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000765 if (C.stmt) {
Steve Narofff9adf8f2009-10-05 17:58:19 +0000766 if (C.kind == CXCursor_ObjCClassRef ||
767 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000768 return static_cast<Stmt *>(C.stmt);
769 else
770 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
771 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000772 return C.decl;
773 }
774 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000775}
776
Steve Naroff85e2db72009-10-01 00:31:07 +0000777
Steve Narofff334b4e2009-09-02 18:26:48 +0000778static SourceLocation getLocationFromCursor(CXCursor C,
779 SourceManager &SourceMgr,
780 NamedDecl *ND) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000781 if (clang_isReference(C.kind)) {
782 switch (C.kind) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000783 case CXCursor_ObjCClassRef: {
Steve Naroff85e2db72009-10-01 00:31:07 +0000784 if (isa<ObjCInterfaceDecl>(ND)) {
785 // FIXME: This is a hack (storing the parent decl in the stmt slot).
786 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
787 return parentDecl->getLocation();
788 }
789 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
790 assert(OID && "clang_getCursorLine(): Missing category decl");
791 return OID->getClassInterface()->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000792 }
793 case CXCursor_ObjCSuperClassRef: {
Steve Narofff334b4e2009-09-02 18:26:48 +0000794 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
795 assert(OID && "clang_getCursorLine(): Missing interface decl");
Steve Naroff1164d852009-09-02 18:58:52 +0000796 return OID->getSuperClassLoc();
Steve Naroffbade7de2009-10-19 13:41:39 +0000797 }
798 case CXCursor_ObjCProtocolRef: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000799 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
800 assert(OID && "clang_getCursorLine(): Missing protocol decl");
801 return OID->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000802 }
803 case CXCursor_ObjCSelectorRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000804 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
805 static_cast<Stmt *>(C.stmt));
806 assert(OME && "clang_getCursorLine(): Missing message expr");
807 return OME->getLeftLoc(); /* FIXME: should be a range */
Steve Naroffbade7de2009-10-19 13:41:39 +0000808 }
Steve Narofffb570422009-09-22 19:25:29 +0000809 case CXCursor_VarRef:
810 case CXCursor_FunctionRef:
Steve Naroffbade7de2009-10-19 13:41:39 +0000811 case CXCursor_EnumConstantRef: {
Steve Narofffb570422009-09-22 19:25:29 +0000812 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
813 static_cast<Stmt *>(C.stmt));
814 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
815 return DRE->getLocation();
Steve Naroffbade7de2009-10-19 13:41:39 +0000816 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000817 default:
Steve Naroff1164d852009-09-02 18:58:52 +0000818 return SourceLocation();
Steve Narofff334b4e2009-09-02 18:26:48 +0000819 }
820 } else { // We have a declaration or a definition.
Steve Naroff9efa7672009-09-04 15:44:05 +0000821 SourceLocation SLoc;
822 switch (ND->getKind()) {
Steve Naroffbade7de2009-10-19 13:41:39 +0000823 case Decl::ObjCInterface: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000824 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
825 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000826 }
827 case Decl::ObjCProtocol: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000828 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
829 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000830 }
831 default: {
Steve Naroff9efa7672009-09-04 15:44:05 +0000832 SLoc = ND->getLocation();
833 break;
Steve Naroffbade7de2009-10-19 13:41:39 +0000834 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000835 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000836 if (SLoc.isInvalid())
837 return SourceLocation();
Steve Naroff1164d852009-09-02 18:58:52 +0000838 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
Steve Narofff334b4e2009-09-02 18:26:48 +0000839 }
Steve Narofff334b4e2009-09-02 18:26:48 +0000840}
841
Steve Naroff2d4d6292009-08-31 14:26:51 +0000842unsigned clang_getCursorLine(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000843{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000844 assert(C.decl && "CXCursor has null decl");
845 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000846 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000847
848 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000849 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000850}
Steve Narofff334b4e2009-09-02 18:26:48 +0000851
Steve Naroff2d4d6292009-08-31 14:26:51 +0000852unsigned clang_getCursorColumn(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000853{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000854 assert(C.decl && "CXCursor has null decl");
855 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000856 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000857
858 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000859 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000860}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000861const char *clang_getCursorSource(CXCursor C)
Steve Naroff600866c2009-08-27 19:51:58 +0000862{
Steve Naroff2d4d6292009-08-31 14:26:51 +0000863 assert(C.decl && "CXCursor has null decl");
864 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000865 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Narofff334b4e2009-09-02 18:26:48 +0000866
867 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Douglas Gregor02465752009-10-16 21:24:31 +0000868 if (SLoc.isFileID())
869 return SourceMgr.getBufferName(SLoc);
870
871 // Retrieve the file in which the macro was instantiated, then provide that
872 // buffer name.
873 // FIXME: Do we want to give specific macro-instantiation information?
874 const llvm::MemoryBuffer *Buffer
875 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
876 if (!Buffer)
877 return 0;
878
879 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000880}
881
Steve Naroff4ade6d62009-09-23 17:52:52 +0000882void clang_getDefinitionSpellingAndExtent(CXCursor C,
883 const char **startBuf,
884 const char **endBuf,
885 unsigned *startLine,
886 unsigned *startColumn,
887 unsigned *endLine,
888 unsigned *endColumn)
889{
890 assert(C.decl && "CXCursor has null decl");
891 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
892 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
893 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
894
895 SourceManager &SM = FD->getASTContext().getSourceManager();
896 *startBuf = SM.getCharacterData(Body->getLBracLoc());
897 *endBuf = SM.getCharacterData(Body->getRBracLoc());
898 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
899 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
900 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
901 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
902}
903
904
Steve Naroff600866c2009-08-27 19:51:58 +0000905} // end extern "C"