blob: 2b33a63b98305ea39fcabedce844de7a984cfaf9 [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.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00007//
Ted Kremenekd2fa5662009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenekab188932010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekd2fa5662009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekab188932010-01-05 19:32:54 +000015#include "CIndexer.h"
16
Steve Naroff50398192009-08-28 15:28:48 +000017#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregor02465752009-10-16 21:24:31 +000019#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000020#include "llvm/System/Program.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000021
Steve Naroff50398192009-08-28 15:28:48 +000022using namespace clang;
23using namespace idx;
24
Steve Naroff89922f82009-08-31 00:59:03 +000025namespace {
Ted Kremenekab188932010-01-05 19:32:54 +000026static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000027 NamedDecl *D = DRE->getDecl();
28 if (isa<VarDecl>(D))
29 return CXCursor_VarRef;
30 else if (isa<FunctionDecl>(D))
31 return CXCursor_FunctionRef;
32 else if (isa<EnumConstantDecl>(D))
33 return CXCursor_EnumConstantRef;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000034 else
Steve Naroff4ade6d62009-09-23 17:52:52 +000035 return CXCursor_NotImplemented;
36}
37
38#if 0
39// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000040class CRefVisitor : public StmtVisitor<CRefVisitor> {
41 CXDecl CDecl;
42 CXDeclIterator Callback;
43 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000044
Steve Narofffb570422009-09-22 19:25:29 +000045 void Call(enum CXCursorKind CK, Stmt *SRef) {
46 CXCursor C = { CK, CDecl, SRef };
47 Callback(CDecl, C, CData);
48 }
49
50public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000051 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Narofffb570422009-09-22 19:25:29 +000052 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000053
Steve Narofffb570422009-09-22 19:25:29 +000054 void VisitStmt(Stmt *S) {
55 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
56 C != CEnd; ++C)
57 Visit(*C);
58 }
59 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000060 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000061 }
62 void VisitMemberExpr(MemberExpr *Node) {
63 Call(CXCursor_MemberRef, Node);
64 }
65 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
66 Call(CXCursor_ObjCSelectorRef, Node);
67 }
68 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
69 Call(CXCursor_ObjCIvarRef, Node);
70 }
71};
Steve Naroff4ade6d62009-09-23 17:52:52 +000072#endif
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000073
Steve Naroff89922f82009-08-31 00:59:03 +000074// Translation Unit Visitor.
75class TUVisitor : public DeclVisitor<TUVisitor> {
76 CXTranslationUnit TUnit;
77 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000078 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000079
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000080 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
81 // to the visitor. Declarations with a PCH level greater than this value will
82 // be suppressed.
83 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000084
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000085 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000086 // Filter any declarations that have a PCH level greater than what we allow.
87 if (ND->getPCHLevel() > MaxPCHLevel)
88 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000089
Steve Narofff96b5242009-10-28 20:44:47 +000090 // Filter any implicit declarations (since the source info will be bogus).
91 if (ND->isImplicit())
92 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000093
Steve Narofffb570422009-09-22 19:25:29 +000094 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000095 Callback(TUnit, C, CData);
96 }
Steve Naroff89922f82009-08-31 00:59:03 +000097public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000098 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000099 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000100 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000101 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000102
Steve Naroff89922f82009-08-31 00:59:03 +0000103 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
104 VisitDeclContext(dyn_cast<DeclContext>(D));
105 }
106 void VisitDeclContext(DeclContext *DC) {
107 for (DeclContext::decl_iterator
108 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
109 Visit(*I);
110 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000111
112 void VisitFunctionDecl(FunctionDecl *ND) {
113 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
114 : CXCursor_FunctionDecl, ND);
115 }
116 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
117 Call(CXCursor_ObjCCategoryDecl, ND);
118 }
119 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
120 Call(CXCursor_ObjCCategoryDefn, ND);
121 }
122 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
123 Call(CXCursor_ObjCClassDefn, ND);
124 }
125 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
126 Call(CXCursor_ObjCInterfaceDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000127 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000128 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
129 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000130 }
131 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000132 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000133 case TagDecl::TK_struct:
134 Call(CXCursor_StructDecl, ND);
135 break;
136 case TagDecl::TK_class:
137 Call(CXCursor_ClassDecl, ND);
138 break;
139 case TagDecl::TK_union:
140 Call(CXCursor_UnionDecl, ND);
141 break;
142 case TagDecl::TK_enum:
143 Call(CXCursor_EnumDecl, ND);
144 break;
Steve Naroffc857ea42009-09-02 13:28:54 +0000145 }
Steve Naroff89922f82009-08-31 00:59:03 +0000146 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000147 void VisitTypedefDecl(TypedefDecl *ND) {
148 Call(CXCursor_TypedefDecl, ND);
149 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000150 void VisitVarDecl(VarDecl *ND) {
151 Call(CXCursor_VarDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000152 }
Steve Naroff89922f82009-08-31 00:59:03 +0000153};
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000154
Steve Naroff89922f82009-08-31 00:59:03 +0000155
Steve Naroffc857ea42009-09-02 13:28:54 +0000156// Declaration visitor.
157class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
158 CXDecl CDecl;
159 CXDeclIterator Callback;
160 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000161
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000162 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
163 // to the visitor. Declarations with a PCH level greater than this value will
164 // be suppressed.
165 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000166
Steve Naroffc857ea42009-09-02 13:28:54 +0000167 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000168 // Disable the callback when the context is equal to the visiting decl.
169 if (CDecl == ND && !clang_isReference(CK))
170 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000171
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000172 // Filter any declarations that have a PCH level greater than what we allow.
173 if (ND->getPCHLevel() > MaxPCHLevel)
174 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000175
Steve Narofffb570422009-09-22 19:25:29 +0000176 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000177 Callback(CDecl, C, CData);
178 }
Steve Naroff89922f82009-08-31 00:59:03 +0000179public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000180 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
181 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000182 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000183
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000184 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
185 // Issue callbacks for the containing class.
186 Call(CXCursor_ObjCClassRef, ND);
187 // FIXME: Issue callbacks for protocol refs.
188 VisitDeclContext(dyn_cast<DeclContext>(ND));
189 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000190 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000191 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000192 if (D->getSuperClass())
193 Call(CXCursor_ObjCSuperClassRef, D);
194
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000195 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000196 E = D->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000197 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000198 VisitDeclContext(dyn_cast<DeclContext>(D));
199 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000200 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000201 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000202 E = PID->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000203 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000204
Steve Naroff9efa7672009-09-04 15:44:05 +0000205 VisitDeclContext(dyn_cast<DeclContext>(PID));
206 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000207 void VisitTagDecl(TagDecl *D) {
208 VisitDeclContext(dyn_cast<DeclContext>(D));
209 }
210 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
211 VisitDeclContext(dyn_cast<DeclContext>(D));
212 }
213 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
214 VisitDeclContext(dyn_cast<DeclContext>(D));
215 }
Steve Naroff89922f82009-08-31 00:59:03 +0000216 void VisitDeclContext(DeclContext *DC) {
217 for (DeclContext::decl_iterator
218 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
219 Visit(*I);
220 }
221 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000222 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000223 }
224 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000225 Call(CXCursor_FieldDecl, ND);
226 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000227 void VisitVarDecl(VarDecl *ND) {
228 Call(CXCursor_VarDecl, ND);
229 }
230 void VisitParmVarDecl(ParmVarDecl *ND) {
231 Call(CXCursor_ParmDecl, ND);
232 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000233 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
234 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000235 }
236 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000237 Call(CXCursor_ObjCIvarDecl, ND);
238 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000239 void VisitFunctionDecl(FunctionDecl *ND) {
240 if (ND->isThisDeclarationADefinition()) {
241 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000242#if 0
243 // Not currently needed.
244 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000245 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000246 RVisit.Visit(Body);
247#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000248 }
249 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000250 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
251 if (ND->getBody()) {
252 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
253 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000254 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000255 } else
256 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
257 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000258 }
259};
Ted Kremenekab188932010-01-05 19:32:54 +0000260} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000261
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000262static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000263 SourceManager &SourceMgr,
264 NamedDecl *ND) {
265 if (clang_isReference(C.kind)) {
266 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000267 case CXCursor_ObjCClassRef: {
268 if (isa<ObjCInterfaceDecl>(ND)) {
269 // FIXME: This is a hack (storing the parent decl in the stmt slot).
270 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
271 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000272 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000273 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
274 assert(OID && "clang_getCursorLine(): Missing category decl");
275 return OID->getClassInterface()->getLocation();
276 }
277 case CXCursor_ObjCSuperClassRef: {
278 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
279 assert(OID && "clang_getCursorLine(): Missing interface decl");
280 return OID->getSuperClassLoc();
281 }
282 case CXCursor_ObjCProtocolRef: {
283 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
284 assert(OID && "clang_getCursorLine(): Missing protocol decl");
285 return OID->getLocation();
286 }
287 case CXCursor_ObjCSelectorRef: {
288 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
289 static_cast<Stmt *>(C.stmt));
290 assert(OME && "clang_getCursorLine(): Missing message expr");
291 return OME->getLeftLoc(); /* FIXME: should be a range */
292 }
293 case CXCursor_VarRef:
294 case CXCursor_FunctionRef:
295 case CXCursor_EnumConstantRef: {
296 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
297 static_cast<Stmt *>(C.stmt));
298 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
299 return DRE->getLocation();
300 }
301 default:
302 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000303 }
304 } else { // We have a declaration or a definition.
305 SourceLocation SLoc;
306 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000307 case Decl::ObjCInterface: {
308 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
309 break;
310 }
311 case Decl::ObjCProtocol: {
312 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
313 break;
314 }
315 default: {
316 SLoc = ND->getLocation();
317 break;
318 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000319 }
320 if (SLoc.isInvalid())
321 return SourceLocation();
322 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
323 }
324}
325
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000326static CXString createCXString(const char *String, bool DupString = false) {
327 CXString Str;
328 if (DupString) {
329 Str.Spelling = strdup(String);
330 Str.MustFreeString = 1;
331 } else {
332 Str.Spelling = String;
333 Str.MustFreeString = 0;
334 }
335 return Str;
336}
337
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000338extern "C" {
339
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000340CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000341 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000342 CIndexer *CIdxr = new CIndexer(new Program());
343 if (excludeDeclarationsFromPCH)
344 CIdxr->setOnlyLocalDecls();
345 if (displayDiagnostics)
346 CIdxr->setDisplayDiagnostics();
347 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000348}
349
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000350void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000351 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000352 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000353}
354
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000355void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
356 assert(CIdx && "Passed null CXIndex");
357 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
358 CXXIdx->setUseExternalASTGeneration(value);
359}
360
Steve Naroff50398192009-08-28 15:28:48 +0000361// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000362CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
363 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000364 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000365 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000366
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000367 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
368 CXXIdx->getOnlyLocalDecls(),
369 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000370}
371
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000372CXTranslationUnit
373clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
374 const char *source_filename,
375 int num_command_line_args,
376 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000377 assert(CIdx && "Passed null CXIndex");
378 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
379
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000380 if (!CXXIdx->getUseExternalASTGeneration()) {
381 llvm::SmallVector<const char *, 16> Args;
382
383 // The 'source_filename' argument is optional. If the caller does not
384 // specify it then it is assumed that the source file is specified
385 // in the actual argument list.
386 if (source_filename)
387 Args.push_back(source_filename);
388 Args.insert(Args.end(), command_line_args,
389 command_line_args + num_command_line_args);
390
Daniel Dunbar94220972009-12-05 02:17:18 +0000391 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
392 llvm::OwningPtr<ASTUnit> Unit(
393 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000394 CXXIdx->getDiags(),
395 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000396 CXXIdx->getOnlyLocalDecls(),
397 /* UseBumpAllocator = */ true));
398
399 // FIXME: Until we have broader testing, just drop the entire AST if we
400 // encountered an error.
401 if (NumErrors != CXXIdx->getDiags().getNumErrors())
402 return 0;
403
404 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000405 }
406
Ted Kremenek139ba862009-10-22 00:03:57 +0000407 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000408 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000409
Ted Kremenek139ba862009-10-22 00:03:57 +0000410 // First add the complete path to the 'clang' executable.
411 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000412 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000413
Ted Kremenek139ba862009-10-22 00:03:57 +0000414 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000415 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000416
Ted Kremenek139ba862009-10-22 00:03:57 +0000417 // The 'source_filename' argument is optional. If the caller does not
418 // specify it then it is assumed that the source file is specified
419 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000420 if (source_filename)
421 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000422
Steve Naroff37b5ac22009-10-15 20:50:09 +0000423 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000424 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000425 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000426 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000427
428 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
429 for (int i = 0; i < num_command_line_args; ++i)
430 if (const char *arg = command_line_args[i]) {
431 if (strcmp(arg, "-o") == 0) {
432 ++i; // Also skip the matching argument.
433 continue;
434 }
435 if (strcmp(arg, "-emit-ast") == 0 ||
436 strcmp(arg, "-c") == 0 ||
437 strcmp(arg, "-fsyntax-only") == 0) {
438 continue;
439 }
440
441 // Keep the argument.
442 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000443 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000444
Ted Kremenek139ba862009-10-22 00:03:57 +0000445 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000446 argv.push_back(NULL);
447
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000448 // Invoke 'clang'.
449 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
450 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000451 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000452 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000453 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
454 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
455 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000456
Ted Kremenek0854d702009-11-10 19:18:52 +0000457 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000458 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000459 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000460 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000461 I!=E; ++I) {
462 if (*I)
463 llvm::errs() << ' ' << *I << '\n';
464 }
465 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000466 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000467
Steve Naroff37b5ac22009-10-15 20:50:09 +0000468 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000469 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000470 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000471 if (ATU)
472 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000473 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000474}
475
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000476void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000477 assert(CTUnit && "Passed null CXTranslationUnit");
478 delete static_cast<ASTUnit *>(CTUnit);
479}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000480
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000481CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000482 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000483 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000484 return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000485}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000486
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000487void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000488 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000489 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000490 assert(CTUnit && "Passed null CXTranslationUnit");
491 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
492 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000493
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000494 unsigned PCHLevel = Decl::MaxPCHLevel;
495
496 // Set the PCHLevel to filter out unwanted decls if requested.
497 if (CXXUnit->getOnlyLocalDecls()) {
498 PCHLevel = 0;
499
500 // If the main input was an AST, bump the level.
501 if (CXXUnit->isMainFileAST())
502 ++PCHLevel;
503 }
504
505 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000506
507 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
508 // decls.
509 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
510 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
511 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
512 ie = TLDs.end(); it != ie; ++it) {
513 DVisit.Visit(*it);
514 }
515 } else
516 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000517}
518
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000519void clang_loadDeclaration(CXDecl Dcl,
520 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000521 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000522 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000523
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000524 CDeclVisitor DVisit(Dcl, callback, CData,
525 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000526 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000527}
528
Steve Naroff7e8f8182009-08-28 12:07:44 +0000529// Some notes on CXEntity:
530//
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000531// - Since the 'ordinary' namespace includes functions, data, typedefs,
532// ObjC interfaces, thecurrent algorithm is a bit naive (resulting in one
Steve Naroff7e8f8182009-08-28 12:07:44 +0000533// entity for 2 different types). For example:
534//
535// module1.m: @interface Foo @end Foo *x;
536// module2.m: void Foo(int);
537//
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000538// - Since the unique name spans translation units, static data/functions
Steve Naroff7e8f8182009-08-28 12:07:44 +0000539// within a CXTranslationUnit are *not* currently represented by entities.
540// As a result, there will be no entity for the following:
541//
542// module.m: static void Foo() { }
543//
544
545
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000546const char *clang_getDeclarationName(CXEntity) {
Steve Naroff600866c2009-08-27 19:51:58 +0000547 return "";
548}
549
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000550const char *clang_getURI(CXEntity) {
551 return "";
552}
553
554CXEntity clang_getEntity(const char *URI) {
Steve Naroff600866c2009-08-27 19:51:58 +0000555 return 0;
556}
557
558//
559// CXDecl Operations.
560//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000561
562CXEntity clang_getEntityFromDecl(CXDecl) {
Steve Naroff600866c2009-08-27 19:51:58 +0000563 return 0;
564}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000565
566CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000567 assert(AnonDecl && "Passed null CXDecl");
568 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000569
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000570 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
571 return createCXString(OMD->getSelector().getAsString().c_str(), true);
572
573 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000574 // No, this isn't the same as the code below. getIdentifier() is non-virtual
575 // and returns different names. NamedDecl returns the class name and
576 // ObjCCategoryImplDecl returns the category name.
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000577 return createCXString(CIMP->getIdentifier()->getNameStart());
578
579 if (ND->getIdentifier())
580 return createCXString(ND->getIdentifier()->getNameStart());
581
582 return createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000583}
Steve Narofff334b4e2009-09-02 18:26:48 +0000584
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000585unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000586 assert(AnonDecl && "Passed null CXDecl");
587 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
588 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
589 return SourceMgr.getSpellingLineNumber(ND->getLocation());
590}
591
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000592unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000593 assert(AnonDecl && "Passed null CXDecl");
594 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
595 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000596 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000597}
598
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000599const char *clang_getDeclSource(CXDecl AnonDecl) {
Steve Naroffee9405e2009-09-25 21:45:39 +0000600 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff88145032009-10-27 14:35:18 +0000601 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
602 assert (FEnt && "Cannot find FileEntry for Decl");
603 return clang_getFileName(FEnt);
604}
605
606static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000607 SourceLocation SLoc) {
Steve Naroff88145032009-10-27 14:35:18 +0000608 FileID FID;
609 if (SLoc.isFileID())
610 FID = SMgr.getFileID(SLoc);
611 else
612 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
613 return SMgr.getFileEntryForID(FID);
614}
615
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000616CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000617 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000618 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
619 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000620 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
621}
622
623const char *clang_getFileName(CXFile SFile) {
624 assert(SFile && "Passed null CXFile");
625 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
626 return FEnt->getName();
627}
628
629time_t clang_getFileTime(CXFile SFile) {
630 assert(SFile && "Passed null CXFile");
631 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
632 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000633}
634
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000635CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000636 assert(C.decl && "CXCursor has null decl");
637 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000638
Steve Narofff334b4e2009-09-02 18:26:48 +0000639 if (clang_isReference(C.kind)) {
640 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000641 case CXCursor_ObjCSuperClassRef: {
642 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
643 assert(OID && "clang_getCursorLine(): Missing interface decl");
644 return createCXString(OID->getSuperClass()->getIdentifier()
645 ->getNameStart());
646 }
647 case CXCursor_ObjCClassRef: {
648 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000649 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000650
651 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
652 assert(OCD && "clang_getCursorLine(): Missing category decl");
653 return createCXString(OCD->getClassInterface()->getIdentifier()
654 ->getNameStart());
655 }
656 case CXCursor_ObjCProtocolRef: {
657 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
658 assert(OID && "clang_getCursorLine(): Missing protocol decl");
659 return createCXString(OID->getIdentifier()->getNameStart());
660 }
661 case CXCursor_ObjCSelectorRef: {
662 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
663 static_cast<Stmt *>(C.stmt));
664 assert(OME && "clang_getCursorLine(): Missing message expr");
665 return createCXString(OME->getSelector().getAsString().c_str(), true);
666 }
667 case CXCursor_VarRef:
668 case CXCursor_FunctionRef:
669 case CXCursor_EnumConstantRef: {
670 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
671 static_cast<Stmt *>(C.stmt));
672 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
673 return createCXString(DRE->getDecl()->getIdentifier()->getNameStart());
674 }
675 default:
676 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000677 }
678 }
679 return clang_getDeclSpelling(C.decl);
680}
681
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000682const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000683 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000684 case CXCursor_FunctionDecl: return "FunctionDecl";
685 case CXCursor_TypedefDecl: return "TypedefDecl";
686 case CXCursor_EnumDecl: return "EnumDecl";
687 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
688 case CXCursor_StructDecl: return "StructDecl";
689 case CXCursor_UnionDecl: return "UnionDecl";
690 case CXCursor_ClassDecl: return "ClassDecl";
691 case CXCursor_FieldDecl: return "FieldDecl";
692 case CXCursor_VarDecl: return "VarDecl";
693 case CXCursor_ParmDecl: return "ParmDecl";
694 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
695 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
696 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
697 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
698 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
699 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
700 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
701 case CXCursor_FunctionDefn: return "FunctionDefn";
702 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
703 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
704 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
705 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
706 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
707 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
708 case CXCursor_ObjCClassRef: return "ObjCClassRef";
709 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000710
Daniel Dunbaracca7252009-11-30 20:42:49 +0000711 case CXCursor_VarRef: return "VarRef";
712 case CXCursor_FunctionRef: return "FunctionRef";
713 case CXCursor_EnumConstantRef: return "EnumConstantRef";
714 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000715
Daniel Dunbaracca7252009-11-30 20:42:49 +0000716 case CXCursor_InvalidFile: return "InvalidFile";
717 case CXCursor_NoDeclFound: return "NoDeclFound";
718 case CXCursor_NotImplemented: return "NotImplemented";
719 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000720 }
Steve Naroff600866c2009-08-27 19:51:58 +0000721}
Steve Naroff89922f82009-08-31 00:59:03 +0000722
Steve Naroff9efa7672009-09-04 15:44:05 +0000723static enum CXCursorKind TranslateKind(Decl *D) {
724 switch (D->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000725 case Decl::Function: return CXCursor_FunctionDecl;
726 case Decl::Typedef: return CXCursor_TypedefDecl;
727 case Decl::Enum: return CXCursor_EnumDecl;
728 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
729 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
730 case Decl::Field: return CXCursor_FieldDecl;
731 case Decl::Var: return CXCursor_VarDecl;
732 case Decl::ParmVar: return CXCursor_ParmDecl;
733 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
734 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
735 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
736 case Decl::ObjCMethod: {
737 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
738 if (MD->isInstanceMethod())
739 return CXCursor_ObjCInstanceMethodDecl;
740 return CXCursor_ObjCClassMethodDecl;
741 }
742 default: break;
Steve Naroff9efa7672009-09-04 15:44:05 +0000743 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000744 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000745}
Steve Naroff600866c2009-08-27 19:51:58 +0000746//
747// CXCursor Operations.
748//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000749
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000750CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000751 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000752 assert(CTUnit && "Passed null CXTranslationUnit");
753 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000754
Steve Naroff9efa7672009-09-04 15:44:05 +0000755 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000756 const FileEntry *File = FMgr.getFile(source_name,
757 source_name+strlen(source_name));
Steve Naroff77128dd2009-09-15 20:25:34 +0000758 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000759 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000760 return C;
761 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000762 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000763 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000764
Steve Narofff96b5242009-10-28 20:44:47 +0000765 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
766
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000767 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000768 &LastLoc);
769 if (ALoc.isValid())
770 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000771
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000772 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000773 if (ALoc.isNamedRef())
774 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000775 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000776 if (Dcl) {
777 if (Stm) {
778 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
779 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
780 return C;
781 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
782 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
783 return C;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000784 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000785 // Fall through...treat as a decl, not a ref.
786 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000787 if (ALoc.isNamedRef()) {
788 if (isa<ObjCInterfaceDecl>(Dcl)) {
789 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
790 return C;
791 }
792 if (isa<ObjCProtocolDecl>(Dcl)) {
793 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
794 return C;
795 }
796 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000797 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000798 return C;
799 }
Steve Narofffb570422009-09-22 19:25:29 +0000800 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000801 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000802}
803
Ted Kremenek73885552009-11-17 19:28:59 +0000804CXCursor clang_getNullCursor(void) {
805 CXCursor C;
806 C.kind = CXCursor_InvalidFile;
807 C.decl = NULL;
808 C.stmt = NULL;
809 return C;
810}
811
812unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
813 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
814}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000815
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000816CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000817 assert(AnonDecl && "Passed null CXDecl");
818 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000819
Steve Narofffb570422009-09-22 19:25:29 +0000820 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000821 return C;
822}
823
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000824unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000825 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
826}
827
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000828unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000829 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
830}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000831
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000832unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000833 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
834}
835
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000836unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000837 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
838}
839
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000840CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000841 return C.kind;
842}
843
Steve Naroff699a07d2009-09-25 21:32:34 +0000844static Decl *getDeclFromExpr(Stmt *E) {
845 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
846 return RefExpr->getDecl();
847 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
848 return ME->getMemberDecl();
849 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
850 return RE->getDecl();
851
852 if (CallExpr *CE = dyn_cast<CallExpr>(E))
853 return getDeclFromExpr(CE->getCallee());
854 if (CastExpr *CE = dyn_cast<CastExpr>(E))
855 return getDeclFromExpr(CE->getSubExpr());
856 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
857 return OME->getMethodDecl();
858
859 return 0;
860}
861
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000862CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000863 if (clang_isDeclaration(C.kind))
864 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000865
Steve Naroff699a07d2009-09-25 21:32:34 +0000866 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000867 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000868 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +0000869 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000870 return static_cast<Stmt *>(C.stmt);
871 else
872 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
873 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000874 return C.decl;
875 }
876 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000877}
878
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000879unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000880 assert(C.decl && "CXCursor has null decl");
881 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000882 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000883
Steve Narofff334b4e2009-09-02 18:26:48 +0000884 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000885 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000886}
Steve Narofff334b4e2009-09-02 18:26:48 +0000887
Steve Naroffef0cef62009-11-09 17:45:52 +0000888const char *clang_getCString(CXString string) {
889 return string.Spelling;
890}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000891
Steve Naroffef0cef62009-11-09 17:45:52 +0000892void clang_disposeString(CXString string) {
Benjamin Kramer858e5de2009-11-09 18:24:53 +0000893 if (string.MustFreeString)
894 free((void*)string.Spelling);
Steve Naroffef0cef62009-11-09 17:45:52 +0000895}
896
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000897unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000898 assert(C.decl && "CXCursor has null decl");
899 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000900 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000901
Steve Narofff334b4e2009-09-02 18:26:48 +0000902 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000903 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000904}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000905
906const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000907 assert(C.decl && "CXCursor has null decl");
908 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000909 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000910
Steve Narofff334b4e2009-09-02 18:26:48 +0000911 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000912
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000913 if (SLoc.isFileID()) {
914 const char *bufferName = SourceMgr.getBufferName(SLoc);
915 return bufferName[0] == '<' ? NULL : bufferName;
916 }
Douglas Gregor02465752009-10-16 21:24:31 +0000917
918 // Retrieve the file in which the macro was instantiated, then provide that
919 // buffer name.
920 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000921 const llvm::MemoryBuffer *Buffer
Douglas Gregor02465752009-10-16 21:24:31 +0000922 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
923 if (!Buffer)
924 return 0;
925
926 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000927}
928
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000929CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +0000930 assert(C.decl && "CXCursor has null decl");
931 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
932 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000933
Steve Naroff88145032009-10-27 14:35:18 +0000934 return (void *)getFileEntryFromSourceLocation(SourceMgr,
Daniel Dunbaracca7252009-11-30 20:42:49 +0000935 getLocationFromCursor(C,SourceMgr, ND));
Steve Naroff88145032009-10-27 14:35:18 +0000936}
937
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000938void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000939 const char **startBuf,
940 const char **endBuf,
941 unsigned *startLine,
942 unsigned *startColumn,
943 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000944 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000945 assert(C.decl && "CXCursor has null decl");
946 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
947 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
948 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000949
Steve Naroff4ade6d62009-09-23 17:52:52 +0000950 SourceManager &SM = FD->getASTContext().getSourceManager();
951 *startBuf = SM.getCharacterData(Body->getLBracLoc());
952 *endBuf = SM.getCharacterData(Body->getRBracLoc());
953 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
954 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
955 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
956 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
957}
958
Steve Naroff600866c2009-08-27 19:51:58 +0000959} // end extern "C"