blob: 7b624e6dd3f30e606c296274742916f1ae8ac5b1 [file] [log] [blame]
Ted Kremenekb60d87c2009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00007//
Ted Kremenekb60d87c2009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekb60d87c2009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000015#include "CIndexer.h"
16
Steve Naroffa1c72842009-08-28 15:28:48 +000017#include "clang/AST/DeclVisitor.h"
Steve Naroff66af1ae2009-09-22 19:25:29 +000018#include "clang/AST/StmtVisitor.h"
Douglas Gregord3d923a2009-10-16 21:24:31 +000019#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer2836c4c2009-10-18 11:19:36 +000020#include "llvm/System/Program.h"
Ted Kremenek428c6372009-10-19 21:44:57 +000021
Ted Kremenek902292d2010-01-05 20:55:39 +000022// Needed to define L_TMPNAM on some systems.
23#include <cstdio>
24
Steve Naroffa1c72842009-08-28 15:28:48 +000025using namespace clang;
26using namespace idx;
27
Steve Naroff1054e602009-08-31 00:59:03 +000028namespace {
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000029static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff76b8f132009-09-23 17:52:52 +000030 NamedDecl *D = DRE->getDecl();
31 if (isa<VarDecl>(D))
32 return CXCursor_VarRef;
33 else if (isa<FunctionDecl>(D))
34 return CXCursor_FunctionRef;
35 else if (isa<EnumConstantDecl>(D))
36 return CXCursor_EnumConstantRef;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000037 else
Steve Naroff76b8f132009-09-23 17:52:52 +000038 return CXCursor_NotImplemented;
39}
40
41#if 0
42// Will be useful one day.
Steve Naroff66af1ae2009-09-22 19:25:29 +000043class CRefVisitor : public StmtVisitor<CRefVisitor> {
44 CXDecl CDecl;
45 CXDeclIterator Callback;
46 CXClientData CData;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000047
Steve Naroff66af1ae2009-09-22 19:25:29 +000048 void Call(enum CXCursorKind CK, Stmt *SRef) {
49 CXCursor C = { CK, CDecl, SRef };
50 Callback(CDecl, C, CData);
51 }
52
53public:
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000054 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Naroff66af1ae2009-09-22 19:25:29 +000055 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000056
Steve Naroff66af1ae2009-09-22 19:25:29 +000057 void VisitStmt(Stmt *S) {
58 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
59 C != CEnd; ++C)
60 Visit(*C);
61 }
62 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff76b8f132009-09-23 17:52:52 +000063 Call(TranslateDeclRefExpr(Node), Node);
Steve Naroff66af1ae2009-09-22 19:25:29 +000064 }
65 void VisitMemberExpr(MemberExpr *Node) {
66 Call(CXCursor_MemberRef, Node);
67 }
68 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
69 Call(CXCursor_ObjCSelectorRef, Node);
70 }
71 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
72 Call(CXCursor_ObjCIvarRef, Node);
73 }
74};
Steve Naroff76b8f132009-09-23 17:52:52 +000075#endif
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000076
Steve Naroff1054e602009-08-31 00:59:03 +000077// Translation Unit Visitor.
78class TUVisitor : public DeclVisitor<TUVisitor> {
79 CXTranslationUnit TUnit;
80 CXTranslationUnitIterator Callback;
Steve Naroff69b10fd2009-09-01 15:55:40 +000081 CXClientData CData;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000082
Douglas Gregor16bef852009-10-16 20:01:17 +000083 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
84 // to the visitor. Declarations with a PCH level greater than this value will
85 // be suppressed.
86 unsigned MaxPCHLevel;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000087
Steve Naroff69b10fd2009-09-01 15:55:40 +000088 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor16bef852009-10-16 20:01:17 +000089 // Filter any declarations that have a PCH level greater than what we allow.
90 if (ND->getPCHLevel() > MaxPCHLevel)
91 return;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000092
Steve Naroff58bd62d2009-10-28 20:44:47 +000093 // Filter any implicit declarations (since the source info will be bogus).
94 if (ND->isImplicit())
95 return;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +000096
Steve Naroff66af1ae2009-09-22 19:25:29 +000097 CXCursor C = { CK, ND, 0 };
Steve Naroff69b10fd2009-09-01 15:55:40 +000098 Callback(TUnit, C, CData);
99 }
Steve Naroff1054e602009-08-31 00:59:03 +0000100public:
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000101 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor16bef852009-10-16 20:01:17 +0000102 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000103 unsigned MaxPCHLevel) :
Douglas Gregor16bef852009-10-16 20:01:17 +0000104 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000105
Steve Naroff1054e602009-08-31 00:59:03 +0000106 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
107 VisitDeclContext(dyn_cast<DeclContext>(D));
108 }
109 void VisitDeclContext(DeclContext *DC) {
110 for (DeclContext::decl_iterator
111 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
112 Visit(*I);
113 }
Ted Kremenek98524b12009-11-17 07:02:15 +0000114
115 void VisitFunctionDecl(FunctionDecl *ND) {
116 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
117 : CXCursor_FunctionDecl, ND);
118 }
119 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
120 Call(CXCursor_ObjCCategoryDecl, ND);
121 }
122 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
123 Call(CXCursor_ObjCCategoryDefn, ND);
124 }
125 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
126 Call(CXCursor_ObjCClassDefn, ND);
127 }
128 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
129 Call(CXCursor_ObjCInterfaceDecl, ND);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000130 }
Ted Kremenek98524b12009-11-17 07:02:15 +0000131 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
132 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000133 }
134 void VisitTagDecl(TagDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000135 switch (ND->getTagKind()) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000136 case TagDecl::TK_struct:
137 Call(CXCursor_StructDecl, ND);
138 break;
139 case TagDecl::TK_class:
140 Call(CXCursor_ClassDecl, ND);
141 break;
142 case TagDecl::TK_union:
143 Call(CXCursor_UnionDecl, ND);
144 break;
145 case TagDecl::TK_enum:
146 Call(CXCursor_EnumDecl, ND);
147 break;
Steve Naroff3645f5a2009-09-02 13:28:54 +0000148 }
Steve Naroff1054e602009-08-31 00:59:03 +0000149 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000150 void VisitTypedefDecl(TypedefDecl *ND) {
151 Call(CXCursor_TypedefDecl, ND);
152 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000153 void VisitVarDecl(VarDecl *ND) {
154 Call(CXCursor_VarDecl, ND);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000155 }
Steve Naroff1054e602009-08-31 00:59:03 +0000156};
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000157
Steve Naroff1054e602009-08-31 00:59:03 +0000158
Steve Naroff3645f5a2009-09-02 13:28:54 +0000159// Declaration visitor.
160class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
161 CXDecl CDecl;
162 CXDeclIterator Callback;
163 CXClientData CData;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000164
Douglas Gregor16bef852009-10-16 20:01:17 +0000165 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
166 // to the visitor. Declarations with a PCH level greater than this value will
167 // be suppressed.
168 unsigned MaxPCHLevel;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000169
Steve Naroff3645f5a2009-09-02 13:28:54 +0000170 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000171 // Disable the callback when the context is equal to the visiting decl.
172 if (CDecl == ND && !clang_isReference(CK))
173 return;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000174
Douglas Gregor16bef852009-10-16 20:01:17 +0000175 // Filter any declarations that have a PCH level greater than what we allow.
176 if (ND->getPCHLevel() > MaxPCHLevel)
177 return;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000178
Steve Naroff66af1ae2009-09-22 19:25:29 +0000179 CXCursor C = { CK, ND, 0 };
Steve Naroff3645f5a2009-09-02 13:28:54 +0000180 Callback(CDecl, C, CData);
181 }
Steve Naroff1054e602009-08-31 00:59:03 +0000182public:
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000183 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
184 unsigned MaxPCHLevel) :
Douglas Gregor16bef852009-10-16 20:01:17 +0000185 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000186
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000187 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
188 // Issue callbacks for the containing class.
189 Call(CXCursor_ObjCClassRef, ND);
190 // FIXME: Issue callbacks for protocol refs.
191 VisitDeclContext(dyn_cast<DeclContext>(ND));
192 }
Steve Naroff3645f5a2009-09-02 13:28:54 +0000193 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000194 // Issue callbacks for super class.
Steve Naroff80a766b2009-09-02 18:26:48 +0000195 if (D->getSuperClass())
196 Call(CXCursor_ObjCSuperClassRef, D);
197
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000198 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000199 E = D->protocol_end(); I != E; ++I)
Steve Naroffef9618b2009-09-04 15:44:05 +0000200 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroff3645f5a2009-09-02 13:28:54 +0000201 VisitDeclContext(dyn_cast<DeclContext>(D));
202 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000203 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000204 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000205 E = PID->protocol_end(); I != E; ++I)
Steve Naroffef9618b2009-09-04 15:44:05 +0000206 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000207
Steve Naroffef9618b2009-09-04 15:44:05 +0000208 VisitDeclContext(dyn_cast<DeclContext>(PID));
209 }
Steve Naroff3645f5a2009-09-02 13:28:54 +0000210 void VisitTagDecl(TagDecl *D) {
211 VisitDeclContext(dyn_cast<DeclContext>(D));
212 }
213 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
214 VisitDeclContext(dyn_cast<DeclContext>(D));
215 }
216 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
217 VisitDeclContext(dyn_cast<DeclContext>(D));
218 }
Steve Naroff1054e602009-08-31 00:59:03 +0000219 void VisitDeclContext(DeclContext *DC) {
220 for (DeclContext::decl_iterator
221 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
222 Visit(*I);
223 }
224 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000225 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000226 }
227 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000228 Call(CXCursor_FieldDecl, ND);
229 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000230 void VisitVarDecl(VarDecl *ND) {
231 Call(CXCursor_VarDecl, ND);
232 }
233 void VisitParmVarDecl(ParmVarDecl *ND) {
234 Call(CXCursor_ParmDecl, ND);
235 }
Steve Naroff3645f5a2009-09-02 13:28:54 +0000236 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
237 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000238 }
239 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000240 Call(CXCursor_ObjCIvarDecl, ND);
241 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000242 void VisitFunctionDecl(FunctionDecl *ND) {
243 if (ND->isThisDeclarationADefinition()) {
244 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff76b8f132009-09-23 17:52:52 +0000245#if 0
246 // Not currently needed.
247 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Naroff66af1ae2009-09-22 19:25:29 +0000248 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff76b8f132009-09-23 17:52:52 +0000249 RVisit.Visit(Body);
250#endif
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000251 }
252 }
Steve Naroff3645f5a2009-09-02 13:28:54 +0000253 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
254 if (ND->getBody()) {
255 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
256 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000257 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff3645f5a2009-09-02 13:28:54 +0000258 } else
259 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
260 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000261 }
262};
Ted Kremenek0ec2cca2010-01-05 19:32:54 +0000263} // end anonymous namespace
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000264
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000265static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbar2679a882009-11-08 04:13:53 +0000266 SourceManager &SourceMgr,
267 NamedDecl *ND) {
268 if (clang_isReference(C.kind)) {
269 switch (C.kind) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000270 case CXCursor_ObjCClassRef: {
271 if (isa<ObjCInterfaceDecl>(ND)) {
272 // FIXME: This is a hack (storing the parent decl in the stmt slot).
273 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
274 return parentDecl->getLocation();
Daniel Dunbar2679a882009-11-08 04:13:53 +0000275 }
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000276 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
277 assert(OID && "clang_getCursorLine(): Missing category decl");
278 return OID->getClassInterface()->getLocation();
279 }
280 case CXCursor_ObjCSuperClassRef: {
281 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
282 assert(OID && "clang_getCursorLine(): Missing interface decl");
283 return OID->getSuperClassLoc();
284 }
285 case CXCursor_ObjCProtocolRef: {
286 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
287 assert(OID && "clang_getCursorLine(): Missing protocol decl");
288 return OID->getLocation();
289 }
290 case CXCursor_ObjCSelectorRef: {
291 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
292 static_cast<Stmt *>(C.stmt));
293 assert(OME && "clang_getCursorLine(): Missing message expr");
294 return OME->getLeftLoc(); /* FIXME: should be a range */
295 }
296 case CXCursor_VarRef:
297 case CXCursor_FunctionRef:
298 case CXCursor_EnumConstantRef: {
299 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
300 static_cast<Stmt *>(C.stmt));
301 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
302 return DRE->getLocation();
303 }
304 default:
305 return SourceLocation();
Daniel Dunbar2679a882009-11-08 04:13:53 +0000306 }
307 } else { // We have a declaration or a definition.
308 SourceLocation SLoc;
309 switch (ND->getKind()) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000310 case Decl::ObjCInterface: {
311 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
312 break;
313 }
314 case Decl::ObjCProtocol: {
315 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
316 break;
317 }
318 default: {
319 SLoc = ND->getLocation();
320 break;
321 }
Daniel Dunbar2679a882009-11-08 04:13:53 +0000322 }
323 if (SLoc.isInvalid())
324 return SourceLocation();
325 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
326 }
327}
328
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000329static CXString createCXString(const char *String, bool DupString = false) {
330 CXString Str;
331 if (DupString) {
332 Str.Spelling = strdup(String);
333 Str.MustFreeString = 1;
334 } else {
335 Str.Spelling = String;
336 Str.MustFreeString = 0;
337 }
338 return Str;
339}
340
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000341extern "C" {
342
Steve Naroff531e2842009-10-20 14:46:24 +0000343CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000344 int displayDiagnostics) {
Steve Naroff531e2842009-10-20 14:46:24 +0000345 CIndexer *CIdxr = new CIndexer(new Program());
346 if (excludeDeclarationsFromPCH)
347 CIdxr->setOnlyLocalDecls();
348 if (displayDiagnostics)
349 CIdxr->setDisplayDiagnostics();
350 return CIdxr;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000351}
352
Daniel Dunbar079203f2009-12-01 03:14:51 +0000353void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff3aa2d732009-09-17 18:33:27 +0000354 assert(CIdx && "Passed null CXIndex");
Douglas Gregor16bef852009-10-16 20:01:17 +0000355 delete static_cast<CIndexer *>(CIdx);
Steve Naroff3aa2d732009-09-17 18:33:27 +0000356}
357
Daniel Dunbar11089662009-12-03 01:54:28 +0000358void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
359 assert(CIdx && "Passed null CXIndex");
360 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
361 CXXIdx->setUseExternalASTGeneration(value);
362}
363
Steve Naroffa1c72842009-08-28 15:28:48 +0000364// FIXME: need to pass back error info.
Daniel Dunbar079203f2009-12-01 03:14:51 +0000365CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
366 const char *ast_filename) {
Steve Naroffa1c72842009-08-28 15:28:48 +0000367 assert(CIdx && "Passed null CXIndex");
Douglas Gregor16bef852009-10-16 20:01:17 +0000368 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000369
Daniel Dunbar59203002009-12-03 01:45:44 +0000370 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
371 CXXIdx->getOnlyLocalDecls(),
372 /* UseBumpAllocator = */ true);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000373}
374
Daniel Dunbar079203f2009-12-01 03:14:51 +0000375CXTranslationUnit
376clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
377 const char *source_filename,
378 int num_command_line_args,
379 const char **command_line_args) {
Steve Naroff531e2842009-10-20 14:46:24 +0000380 assert(CIdx && "Passed null CXIndex");
381 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
382
Daniel Dunbar11089662009-12-03 01:54:28 +0000383 if (!CXXIdx->getUseExternalASTGeneration()) {
384 llvm::SmallVector<const char *, 16> Args;
385
386 // The 'source_filename' argument is optional. If the caller does not
387 // specify it then it is assumed that the source file is specified
388 // in the actual argument list.
389 if (source_filename)
390 Args.push_back(source_filename);
391 Args.insert(Args.end(), command_line_args,
392 command_line_args + num_command_line_args);
393
Daniel Dunbar72fe5b12009-12-05 02:17:18 +0000394 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
395 llvm::OwningPtr<ASTUnit> Unit(
396 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar8d4a2022009-12-13 03:46:13 +0000397 CXXIdx->getDiags(),
398 CXXIdx->getClangResourcesPath(),
Daniel Dunbar72fe5b12009-12-05 02:17:18 +0000399 CXXIdx->getOnlyLocalDecls(),
400 /* UseBumpAllocator = */ true));
401
402 // FIXME: Until we have broader testing, just drop the entire AST if we
403 // encountered an error.
404 if (NumErrors != CXXIdx->getDiags().getNumErrors())
405 return 0;
406
407 return Unit.take();
Daniel Dunbar11089662009-12-03 01:54:28 +0000408 }
409
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000410 // Build up the arguments for invoking 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000411 std::vector<const char *> argv;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000412
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000413 // First add the complete path to the 'clang' executable.
414 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000415 argv.push_back(ClangPath.c_str());
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000416
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000417 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000418 argv.push_back("-emit-ast");
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000419
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000420 // The 'source_filename' argument is optional. If the caller does not
421 // specify it then it is assumed that the source file is specified
422 // in the actual argument list.
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000423 if (source_filename)
424 argv.push_back(source_filename);
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000425
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000426 // Generate a temporary name for the AST file.
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000427 argv.push_back("-o");
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000428 char astTmpFile[L_tmpnam];
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000429 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000430
431 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
432 for (int i = 0; i < num_command_line_args; ++i)
433 if (const char *arg = command_line_args[i]) {
434 if (strcmp(arg, "-o") == 0) {
435 ++i; // Also skip the matching argument.
436 continue;
437 }
438 if (strcmp(arg, "-emit-ast") == 0 ||
439 strcmp(arg, "-c") == 0 ||
440 strcmp(arg, "-fsyntax-only") == 0) {
441 continue;
442 }
443
444 // Keep the argument.
445 argv.push_back(arg);
Steve Naroff531e2842009-10-20 14:46:24 +0000446 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000447
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000448 // Add the null terminator.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000449 argv.push_back(NULL);
450
Ted Kremenek12e678d2009-10-26 22:14:08 +0000451 // Invoke 'clang'.
452 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
453 // on Unix or NUL (Windows).
Ted Kremenek44886fd2009-10-22 03:24:01 +0000454 std::string ErrMsg;
Ted Kremeneke2896882009-10-19 22:27:32 +0000455 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek44886fd2009-10-22 03:24:01 +0000456 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
457 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
458 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000459
Ted Kremenekba645742009-11-10 19:18:52 +0000460 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000461 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000462 << '\n' << "Arguments: \n";
Ted Kremenek44886fd2009-10-22 03:24:01 +0000463 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenekbf0690c2009-10-26 22:08:39 +0000464 I!=E; ++I) {
465 if (*I)
466 llvm::errs() << ' ' << *I << '\n';
467 }
468 llvm::errs() << '\n';
Ted Kremenek44886fd2009-10-22 03:24:01 +0000469 }
Benjamin Kramer2836c4c2009-10-18 11:19:36 +0000470
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000471 // Finally, we create the translation unit from the ast file.
Steve Naroff44cd60e2009-10-15 22:23:48 +0000472 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000473 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroff531e2842009-10-20 14:46:24 +0000474 if (ATU)
475 ATU->unlinkTemporaryFile();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000476 return ATU;
Steve Naroff7781daa2009-10-15 20:04:39 +0000477}
478
Daniel Dunbar079203f2009-12-01 03:14:51 +0000479void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff3aa2d732009-09-17 18:33:27 +0000480 assert(CTUnit && "Passed null CXTranslationUnit");
481 delete static_cast<ASTUnit *>(CTUnit);
482}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000483
Daniel Dunbar079203f2009-12-01 03:14:51 +0000484CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000485 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroffc0683b92009-09-03 18:19:54 +0000486 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000487 return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true);
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000488}
Daniel Dunbare58bd8b2009-08-28 16:30:07 +0000489
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000490void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroff3645f5a2009-09-02 13:28:54 +0000491 CXTranslationUnitIterator callback,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000492 CXClientData CData) {
Steve Naroffa1c72842009-08-28 15:28:48 +0000493 assert(CTUnit && "Passed null CXTranslationUnit");
494 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
495 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000496
Daniel Dunbar11089662009-12-03 01:54:28 +0000497 unsigned PCHLevel = Decl::MaxPCHLevel;
498
499 // Set the PCHLevel to filter out unwanted decls if requested.
500 if (CXXUnit->getOnlyLocalDecls()) {
501 PCHLevel = 0;
502
503 // If the main input was an AST, bump the level.
504 if (CXXUnit->isMainFileAST())
505 ++PCHLevel;
506 }
507
508 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbar644dca02009-12-04 08:17:33 +0000509
510 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
511 // decls.
512 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
513 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
514 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
515 ie = TLDs.end(); it != ie; ++it) {
516 DVisit.Visit(*it);
517 }
518 } else
519 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroffd5e8e862009-08-27 19:51:58 +0000520}
521
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000522void clang_loadDeclaration(CXDecl Dcl,
523 CXDeclIterator callback,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000524 CXClientData CData) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000525 assert(Dcl && "Passed null CXDecl");
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000526
Douglas Gregor16bef852009-10-16 20:01:17 +0000527 CDeclVisitor DVisit(Dcl, callback, CData,
528 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroff3645f5a2009-09-02 13:28:54 +0000529 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroffd5e8e862009-08-27 19:51:58 +0000530}
531
Steve Naroffd5e8e862009-08-27 19:51:58 +0000532//
533// CXDecl Operations.
534//
Daniel Dunbar079203f2009-12-01 03:14:51 +0000535
536CXEntity clang_getEntityFromDecl(CXDecl) {
Steve Naroffd5e8e862009-08-27 19:51:58 +0000537 return 0;
538}
Daniel Dunbar079203f2009-12-01 03:14:51 +0000539
540CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff1054e602009-08-31 00:59:03 +0000541 assert(AnonDecl && "Passed null CXDecl");
542 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000543
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000544 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
545 return createCXString(OMD->getSelector().getAsString().c_str(), true);
546
547 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Narofff406f4d2009-10-29 21:11:04 +0000548 // No, this isn't the same as the code below. getIdentifier() is non-virtual
549 // and returns different names. NamedDecl returns the class name and
550 // ObjCCategoryImplDecl returns the category name.
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000551 return createCXString(CIMP->getIdentifier()->getNameStart());
552
553 if (ND->getIdentifier())
554 return createCXString(ND->getIdentifier()->getNameStart());
555
556 return createCXString("");
Steve Naroffd5e8e862009-08-27 19:51:58 +0000557}
Steve Naroff80a766b2009-09-02 18:26:48 +0000558
Daniel Dunbar079203f2009-12-01 03:14:51 +0000559unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff63f475a2009-09-25 21:32:34 +0000560 assert(AnonDecl && "Passed null CXDecl");
561 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
562 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
563 return SourceMgr.getSpellingLineNumber(ND->getLocation());
564}
565
Daniel Dunbar079203f2009-12-01 03:14:51 +0000566unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff63f475a2009-09-25 21:32:34 +0000567 assert(AnonDecl && "Passed null CXDecl");
568 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
569 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff43b118f2009-09-25 22:15:54 +0000570 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff63f475a2009-09-25 21:32:34 +0000571}
572
Daniel Dunbar079203f2009-12-01 03:14:51 +0000573const char *clang_getDeclSource(CXDecl AnonDecl) {
Steve Naroff26760892009-09-25 21:45:39 +0000574 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff6231f182009-10-27 14:35:18 +0000575 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
576 assert (FEnt && "Cannot find FileEntry for Decl");
577 return clang_getFileName(FEnt);
578}
579
580static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000581 SourceLocation SLoc) {
Steve Naroff6231f182009-10-27 14:35:18 +0000582 FileID FID;
583 if (SLoc.isFileID())
584 FID = SMgr.getFileID(SLoc);
585 else
586 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
587 return SMgr.getFileEntryForID(FID);
588}
589
Daniel Dunbar079203f2009-12-01 03:14:51 +0000590CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff6231f182009-10-27 14:35:18 +0000591 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff26760892009-09-25 21:45:39 +0000592 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
593 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff6231f182009-10-27 14:35:18 +0000594 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
595}
596
597const char *clang_getFileName(CXFile SFile) {
598 assert(SFile && "Passed null CXFile");
599 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
600 return FEnt->getName();
601}
602
603time_t clang_getFileTime(CXFile SFile) {
604 assert(SFile && "Passed null CXFile");
605 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
606 return FEnt->getModificationTime();
Steve Naroff26760892009-09-25 21:45:39 +0000607}
608
Daniel Dunbar079203f2009-12-01 03:14:51 +0000609CXString clang_getCursorSpelling(CXCursor C) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000610 assert(C.decl && "CXCursor has null decl");
611 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000612
Steve Naroff80a766b2009-09-02 18:26:48 +0000613 if (clang_isReference(C.kind)) {
614 switch (C.kind) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000615 case CXCursor_ObjCSuperClassRef: {
616 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
617 assert(OID && "clang_getCursorLine(): Missing interface decl");
618 return createCXString(OID->getSuperClass()->getIdentifier()
619 ->getNameStart());
620 }
621 case CXCursor_ObjCClassRef: {
622 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000623 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000624
625 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
626 assert(OCD && "clang_getCursorLine(): Missing category decl");
627 return createCXString(OCD->getClassInterface()->getIdentifier()
628 ->getNameStart());
629 }
630 case CXCursor_ObjCProtocolRef: {
631 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
632 assert(OID && "clang_getCursorLine(): Missing protocol decl");
633 return createCXString(OID->getIdentifier()->getNameStart());
634 }
635 case CXCursor_ObjCSelectorRef: {
636 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
637 static_cast<Stmt *>(C.stmt));
638 assert(OME && "clang_getCursorLine(): Missing message expr");
639 return createCXString(OME->getSelector().getAsString().c_str(), true);
640 }
641 case CXCursor_VarRef:
642 case CXCursor_FunctionRef:
643 case CXCursor_EnumConstantRef: {
644 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
645 static_cast<Stmt *>(C.stmt));
646 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
647 return createCXString(DRE->getDecl()->getIdentifier()->getNameStart());
648 }
649 default:
650 return createCXString("<not implemented>");
Steve Naroff80a766b2009-09-02 18:26:48 +0000651 }
652 }
653 return clang_getDeclSpelling(C.decl);
654}
655
Daniel Dunbar079203f2009-12-01 03:14:51 +0000656const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff1054e602009-08-31 00:59:03 +0000657 switch (Kind) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000658 case CXCursor_FunctionDecl: return "FunctionDecl";
659 case CXCursor_TypedefDecl: return "TypedefDecl";
660 case CXCursor_EnumDecl: return "EnumDecl";
661 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
662 case CXCursor_StructDecl: return "StructDecl";
663 case CXCursor_UnionDecl: return "UnionDecl";
664 case CXCursor_ClassDecl: return "ClassDecl";
665 case CXCursor_FieldDecl: return "FieldDecl";
666 case CXCursor_VarDecl: return "VarDecl";
667 case CXCursor_ParmDecl: return "ParmDecl";
668 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
669 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
670 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
671 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
672 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
673 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
674 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
675 case CXCursor_FunctionDefn: return "FunctionDefn";
676 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
677 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
678 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
679 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
680 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
681 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
682 case CXCursor_ObjCClassRef: return "ObjCClassRef";
683 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000684
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000685 case CXCursor_VarRef: return "VarRef";
686 case CXCursor_FunctionRef: return "FunctionRef";
687 case CXCursor_EnumConstantRef: return "EnumConstantRef";
688 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000689
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000690 case CXCursor_InvalidFile: return "InvalidFile";
691 case CXCursor_NoDeclFound: return "NoDeclFound";
692 case CXCursor_NotImplemented: return "NotImplemented";
693 default: return "<not implemented>";
Steve Naroff1054e602009-08-31 00:59:03 +0000694 }
Steve Naroffd5e8e862009-08-27 19:51:58 +0000695}
Steve Naroff1054e602009-08-31 00:59:03 +0000696
Steve Naroffef9618b2009-09-04 15:44:05 +0000697static enum CXCursorKind TranslateKind(Decl *D) {
698 switch (D->getKind()) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000699 case Decl::Function: return CXCursor_FunctionDecl;
700 case Decl::Typedef: return CXCursor_TypedefDecl;
701 case Decl::Enum: return CXCursor_EnumDecl;
702 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
703 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
704 case Decl::Field: return CXCursor_FieldDecl;
705 case Decl::Var: return CXCursor_VarDecl;
706 case Decl::ParmVar: return CXCursor_ParmDecl;
707 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
708 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
709 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
710 case Decl::ObjCMethod: {
711 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
712 if (MD->isInstanceMethod())
713 return CXCursor_ObjCInstanceMethodDecl;
714 return CXCursor_ObjCClassMethodDecl;
715 }
716 default: break;
Steve Naroffef9618b2009-09-04 15:44:05 +0000717 }
Steve Naroff54f22fb2009-09-15 20:25:34 +0000718 return CXCursor_NotImplemented;
Steve Naroffef9618b2009-09-04 15:44:05 +0000719}
Steve Naroffd5e8e862009-08-27 19:51:58 +0000720//
721// CXCursor Operations.
722//
Daniel Dunbar079203f2009-12-01 03:14:51 +0000723
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000724CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000725 unsigned line, unsigned column) {
Steve Naroffef9618b2009-09-04 15:44:05 +0000726 assert(CTUnit && "Passed null CXTranslationUnit");
727 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000728
Steve Naroffef9618b2009-09-04 15:44:05 +0000729 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000730 const FileEntry *File = FMgr.getFile(source_name,
731 source_name+strlen(source_name));
Steve Naroff54f22fb2009-09-15 20:25:34 +0000732 if (!File) {
Steve Naroff66af1ae2009-09-22 19:25:29 +0000733 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000734 return C;
735 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000736 SourceLocation SLoc =
Steve Naroffef9618b2009-09-04 15:44:05 +0000737 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000738
Steve Naroff58bd62d2009-10-28 20:44:47 +0000739 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
740
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000741 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Naroff58bd62d2009-10-28 20:44:47 +0000742 &LastLoc);
743 if (ALoc.isValid())
744 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000745
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000746 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis419e38b2009-09-29 19:45:58 +0000747 if (ALoc.isNamedRef())
748 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000749 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff76b8f132009-09-23 17:52:52 +0000750 if (Dcl) {
751 if (Stm) {
752 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
753 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
754 return C;
755 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
756 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
757 return C;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000758 }
Steve Naroff76b8f132009-09-23 17:52:52 +0000759 // Fall through...treat as a decl, not a ref.
760 }
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000761 if (ALoc.isNamedRef()) {
762 if (isa<ObjCInterfaceDecl>(Dcl)) {
763 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
764 return C;
765 }
766 if (isa<ObjCProtocolDecl>(Dcl)) {
767 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
768 return C;
769 }
770 }
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000771 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000772 return C;
773 }
Steve Naroff66af1ae2009-09-22 19:25:29 +0000774 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroffef9618b2009-09-04 15:44:05 +0000775 return C;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000776}
777
Ted Kremeneke05d7802009-11-17 19:28:59 +0000778CXCursor clang_getNullCursor(void) {
779 CXCursor C;
780 C.kind = CXCursor_InvalidFile;
781 C.decl = NULL;
782 C.stmt = NULL;
783 return C;
784}
785
786unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
787 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
788}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000789
Daniel Dunbar079203f2009-12-01 03:14:51 +0000790CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff54f22fb2009-09-15 20:25:34 +0000791 assert(AnonDecl && "Passed null CXDecl");
792 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000793
Steve Naroff66af1ae2009-09-22 19:25:29 +0000794 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000795 return C;
796}
797
Daniel Dunbar079203f2009-12-01 03:14:51 +0000798unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff54f22fb2009-09-15 20:25:34 +0000799 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
800}
801
Daniel Dunbar079203f2009-12-01 03:14:51 +0000802unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff1054e602009-08-31 00:59:03 +0000803 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
804}
Steve Naroff772c1a42009-08-31 14:26:51 +0000805
Daniel Dunbar079203f2009-12-01 03:14:51 +0000806unsigned clang_isReference(enum CXCursorKind K) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000807 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
808}
809
Daniel Dunbar079203f2009-12-01 03:14:51 +0000810unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000811 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
812}
813
Daniel Dunbar079203f2009-12-01 03:14:51 +0000814CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroffef9618b2009-09-04 15:44:05 +0000815 return C.kind;
816}
817
Steve Naroff63f475a2009-09-25 21:32:34 +0000818static Decl *getDeclFromExpr(Stmt *E) {
819 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
820 return RefExpr->getDecl();
821 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
822 return ME->getMemberDecl();
823 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
824 return RE->getDecl();
825
826 if (CallExpr *CE = dyn_cast<CallExpr>(E))
827 return getDeclFromExpr(CE->getCallee());
828 if (CastExpr *CE = dyn_cast<CastExpr>(E))
829 return getDeclFromExpr(CE->getSubExpr());
830 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
831 return OME->getMethodDecl();
832
833 return 0;
834}
835
Daniel Dunbar079203f2009-12-01 03:14:51 +0000836CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff63f475a2009-09-25 21:32:34 +0000837 if (clang_isDeclaration(C.kind))
838 return C.decl;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000839
Steve Naroff63f475a2009-09-25 21:32:34 +0000840 if (clang_isReference(C.kind)) {
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000841 if (C.stmt) {
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000842 if (C.kind == CXCursor_ObjCClassRef ||
Steve Naroffd7eb7172009-10-05 17:58:19 +0000843 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000844 return static_cast<Stmt *>(C.stmt);
845 else
846 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
847 } else
Steve Naroff63f475a2009-09-25 21:32:34 +0000848 return C.decl;
849 }
850 return 0;
Steve Naroffef9618b2009-09-04 15:44:05 +0000851}
852
Daniel Dunbar079203f2009-12-01 03:14:51 +0000853unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff772c1a42009-08-31 14:26:51 +0000854 assert(C.decl && "CXCursor has null decl");
855 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000856 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000857
Steve Naroff80a766b2009-09-02 18:26:48 +0000858 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000859 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000860}
Steve Naroff80a766b2009-09-02 18:26:48 +0000861
Steve Naroff8675d5c2009-11-09 17:45:52 +0000862const char *clang_getCString(CXString string) {
863 return string.Spelling;
864}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000865
Steve Naroff8675d5c2009-11-09 17:45:52 +0000866void clang_disposeString(CXString string) {
Benjamin Kramerd6f85a82009-11-09 18:24:53 +0000867 if (string.MustFreeString)
868 free((void*)string.Spelling);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000869}
870
Daniel Dunbar079203f2009-12-01 03:14:51 +0000871unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff772c1a42009-08-31 14:26:51 +0000872 assert(C.decl && "CXCursor has null decl");
873 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000874 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000875
Steve Naroff80a766b2009-09-02 18:26:48 +0000876 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000877 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000878}
Daniel Dunbar079203f2009-12-01 03:14:51 +0000879
880const char *clang_getCursorSource(CXCursor C) {
Steve Naroff772c1a42009-08-31 14:26:51 +0000881 assert(C.decl && "CXCursor has null decl");
882 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +0000883 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000884
Steve Naroff80a766b2009-09-02 18:26:48 +0000885 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000886
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000887 if (SLoc.isFileID()) {
888 const char *bufferName = SourceMgr.getBufferName(SLoc);
889 return bufferName[0] == '<' ? NULL : bufferName;
890 }
Douglas Gregord3d923a2009-10-16 21:24:31 +0000891
892 // Retrieve the file in which the macro was instantiated, then provide that
893 // buffer name.
894 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000895 const llvm::MemoryBuffer *Buffer
Douglas Gregord3d923a2009-10-16 21:24:31 +0000896 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
897 if (!Buffer)
898 return 0;
899
900 return Buffer->getBufferIdentifier();
Steve Naroffd5e8e862009-08-27 19:51:58 +0000901}
902
Daniel Dunbar079203f2009-12-01 03:14:51 +0000903CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff6231f182009-10-27 14:35:18 +0000904 assert(C.decl && "CXCursor has null decl");
905 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
906 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000907
Steve Naroff6231f182009-10-27 14:35:18 +0000908 return (void *)getFileEntryFromSourceLocation(SourceMgr,
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000909 getLocationFromCursor(C,SourceMgr, ND));
Steve Naroff6231f182009-10-27 14:35:18 +0000910}
911
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000912void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff76b8f132009-09-23 17:52:52 +0000913 const char **startBuf,
914 const char **endBuf,
915 unsigned *startLine,
916 unsigned *startColumn,
917 unsigned *endLine,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000918 unsigned *endColumn) {
Steve Naroff76b8f132009-09-23 17:52:52 +0000919 assert(C.decl && "CXCursor has null decl");
920 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
921 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
922 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000923
Steve Naroff76b8f132009-09-23 17:52:52 +0000924 SourceManager &SM = FD->getASTContext().getSourceManager();
925 *startBuf = SM.getCharacterData(Body->getLBracLoc());
926 *endBuf = SM.getCharacterData(Body->getRBracLoc());
927 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
928 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
929 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
930 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
931}
932
Steve Naroffd5e8e862009-08-27 19:51:58 +0000933} // end extern "C"