blob: fb359c97a15b123fe71454c168e556d1a342f019 [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
Ted Kremenekdb3d0da2010-01-05 20:55:39 +000022// Needed to define L_TMPNAM on some systems.
23#include <cstdio>
24
Steve Naroff50398192009-08-28 15:28:48 +000025using namespace clang;
26using namespace idx;
27
Steve Naroff89922f82009-08-31 00:59:03 +000028namespace {
Ted Kremenekab188932010-01-05 19:32:54 +000029static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff4ade6d62009-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 Dunbar0d7dd222009-11-30 20:42:43 +000037 else
Steve Naroff4ade6d62009-09-23 17:52:52 +000038 return CXCursor_NotImplemented;
39}
40
41#if 0
42// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000043class CRefVisitor : public StmtVisitor<CRefVisitor> {
44 CXDecl CDecl;
45 CXDeclIterator Callback;
46 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000047
Steve Narofffb570422009-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 Dunbar0d7dd222009-11-30 20:42:43 +000054 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Narofffb570422009-09-22 19:25:29 +000055 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000056
Steve Narofffb570422009-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 Naroff4ade6d62009-09-23 17:52:52 +000063 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-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 Naroff4ade6d62009-09-23 17:52:52 +000075#endif
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000076
Steve Naroff89922f82009-08-31 00:59:03 +000077// Translation Unit Visitor.
78class TUVisitor : public DeclVisitor<TUVisitor> {
79 CXTranslationUnit TUnit;
80 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000081 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000082
Douglas Gregor7d1d49d2009-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 Dunbar0d7dd222009-11-30 20:42:43 +000087
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000088 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-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 Dunbar0d7dd222009-11-30 20:42:43 +000092
Steve Narofff96b5242009-10-28 20:44:47 +000093 // Filter any implicit declarations (since the source info will be bogus).
94 if (ND->isImplicit())
95 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000096
Steve Narofffb570422009-09-22 19:25:29 +000097 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000098 Callback(TUnit, C, CData);
99 }
Steve Naroff89922f82009-08-31 00:59:03 +0000100public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000101 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000102 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000103 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000104 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000105
Steve Naroff89922f82009-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 Kremenekef7fdc62009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000130 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000131 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
132 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000133 }
134 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000135 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-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 Naroffc857ea42009-09-02 13:28:54 +0000148 }
Steve Naroff89922f82009-08-31 00:59:03 +0000149 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000150 void VisitTypedefDecl(TypedefDecl *ND) {
151 Call(CXCursor_TypedefDecl, ND);
152 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000153 void VisitVarDecl(VarDecl *ND) {
154 Call(CXCursor_VarDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000155 }
Steve Naroff89922f82009-08-31 00:59:03 +0000156};
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000157
Steve Naroff89922f82009-08-31 00:59:03 +0000158
Steve Naroffc857ea42009-09-02 13:28:54 +0000159// Declaration visitor.
160class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
161 CXDecl CDecl;
162 CXDeclIterator Callback;
163 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000164
Douglas Gregor7d1d49d2009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000169
Steve Naroffc857ea42009-09-02 13:28:54 +0000170 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000174
Douglas Gregor7d1d49d2009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000178
Steve Narofffb570422009-09-22 19:25:29 +0000179 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000180 Callback(CDecl, C, CData);
181 }
Steve Naroff89922f82009-08-31 00:59:03 +0000182public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000183 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
184 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000185 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000186
Steve Naroffaf08ddc2009-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 Naroffc857ea42009-09-02 13:28:54 +0000193 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000194 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000195 if (D->getSuperClass())
196 Call(CXCursor_ObjCSuperClassRef, D);
197
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000198 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000199 E = D->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000200 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000201 VisitDeclContext(dyn_cast<DeclContext>(D));
202 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000203 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000204 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000205 E = PID->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000206 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000207
Steve Naroff9efa7672009-09-04 15:44:05 +0000208 VisitDeclContext(dyn_cast<DeclContext>(PID));
209 }
Steve Naroffc857ea42009-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 Naroff89922f82009-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 Naroffc857ea42009-09-02 13:28:54 +0000225 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000226 }
227 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000228 Call(CXCursor_FieldDecl, ND);
229 }
Steve Naroffaf08ddc2009-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 Naroffc857ea42009-09-02 13:28:54 +0000236 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
237 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000238 }
239 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000240 Call(CXCursor_ObjCIvarDecl, ND);
241 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000242 void VisitFunctionDecl(FunctionDecl *ND) {
243 if (ND->isThisDeclarationADefinition()) {
244 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000245#if 0
246 // Not currently needed.
247 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000248 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000249 RVisit.Visit(Body);
250#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000251 }
252 }
Steve Naroffc857ea42009-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 Naroffaf08ddc2009-09-03 15:49:00 +0000257 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000258 } else
259 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
260 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000261 }
262};
Ted Kremenekab188932010-01-05 19:32:54 +0000263} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000264
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000265static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000266 SourceManager &SourceMgr,
267 NamedDecl *ND) {
268 if (clang_isReference(C.kind)) {
269 switch (C.kind) {
Daniel Dunbaracca7252009-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 Dunbarc6190332009-11-08 04:13:53 +0000275 }
Daniel Dunbaracca7252009-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 Dunbarc6190332009-11-08 04:13:53 +0000306 }
307 } else { // We have a declaration or a definition.
308 SourceLocation SLoc;
309 switch (ND->getKind()) {
Daniel Dunbaracca7252009-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 Dunbarc6190332009-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 Kramer62cf3222009-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 Kramer5e4bc592009-10-18 16:11:04 +0000341extern "C" {
342
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000343CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000344 int displayDiagnostics) {
Steve Naroffe56b4ba2009-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 Naroff600866c2009-08-27 19:51:58 +0000351}
352
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000353void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000354 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000355 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000356}
357
Daniel Dunbar8506dde2009-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 Naroff50398192009-08-28 15:28:48 +0000364// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000365CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
366 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000367 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000368 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000369
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000370 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
371 CXXIdx->getOnlyLocalDecls(),
372 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000373}
374
Daniel Dunbar9ebfa312009-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 Naroffe56b4ba2009-10-20 14:46:24 +0000380 assert(CIdx && "Passed null CXIndex");
381 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
382
Daniel Dunbar8506dde2009-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 Dunbar94220972009-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 Dunbar869824e2009-12-13 03:46:13 +0000397 CXXIdx->getDiags(),
398 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-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 Dunbar8506dde2009-12-03 01:54:28 +0000408 }
409
Ted Kremenek139ba862009-10-22 00:03:57 +0000410 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000411 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000412
Ted Kremenek139ba862009-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 Kramer5e4bc592009-10-18 16:11:04 +0000415 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000416
Ted Kremenek139ba862009-10-22 00:03:57 +0000417 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000418 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000419
Ted Kremenek139ba862009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000423 if (source_filename)
424 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000425
Steve Naroff37b5ac22009-10-15 20:50:09 +0000426 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000427 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000428 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000429 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-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 Naroffe56b4ba2009-10-20 14:46:24 +0000446 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000447
Ted Kremenek139ba862009-10-22 00:03:57 +0000448 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000449 argv.push_back(NULL);
450
Ted Kremenekfeb15e32009-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 Kremenek379afec2009-10-22 03:24:01 +0000454 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000455 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000459
Ted Kremenek0854d702009-11-10 19:18:52 +0000460 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000461 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000462 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000463 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000464 I!=E; ++I) {
465 if (*I)
466 llvm::errs() << ' ' << *I << '\n';
467 }
468 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000469 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000470
Steve Naroff37b5ac22009-10-15 20:50:09 +0000471 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000472 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000473 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000474 if (ATU)
475 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000476 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000477}
478
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000479void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000480 assert(CTUnit && "Passed null CXTranslationUnit");
481 delete static_cast<ASTUnit *>(CTUnit);
482}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000483
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000484CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000485 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000486 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000487 return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000488}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000489
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000490void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000491 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000492 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000493 assert(CTUnit && "Passed null CXTranslationUnit");
494 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
495 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000496
Daniel Dunbar8506dde2009-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 Dunbarf772d1e2009-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 Naroff600866c2009-08-27 19:51:58 +0000520}
521
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000522void clang_loadDeclaration(CXDecl Dcl,
523 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000524 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000525 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000526
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000527 CDeclVisitor DVisit(Dcl, callback, CData,
528 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000529 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000530}
531
Steve Naroff600866c2009-08-27 19:51:58 +0000532//
533// CXDecl Operations.
534//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000535
536CXEntity clang_getEntityFromDecl(CXDecl) {
Steve Naroff600866c2009-08-27 19:51:58 +0000537 return 0;
538}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000539
540CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000541 assert(AnonDecl && "Passed null CXDecl");
542 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000543
Benjamin Kramer62cf3222009-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 Naroff0d69b8c2009-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 Kramer62cf3222009-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 Naroff600866c2009-08-27 19:51:58 +0000557}
Steve Narofff334b4e2009-09-02 18:26:48 +0000558
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000559unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-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 Dunbar9ebfa312009-12-01 03:14:51 +0000566unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-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 Naroff74165242009-09-25 22:15:54 +0000570 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000571}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000572
573CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
574 assert(AnonDecl && "Passed null CXDecl");
575 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
576 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
577 SourceRange R = ND->getSourceRange();
578
579 CXDeclExtent extent;
580
581 SourceLocation L = SourceMgr.getSpellingLoc(R.getBegin());
582 extent.begin.line = SourceMgr.getSpellingLineNumber(L);
583 extent.begin.column = SourceMgr.getSpellingColumnNumber(L);
584
585 L = SourceMgr.getSpellingLoc(R.getEnd());
586 extent.end.line = SourceMgr.getSpellingLineNumber(L);
587 extent.end.column = SourceMgr.getSpellingColumnNumber(L);
588
589 return extent;
590}
Steve Naroff699a07d2009-09-25 21:32:34 +0000591
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000592const char *clang_getDeclSource(CXDecl AnonDecl) {
Steve Naroffee9405e2009-09-25 21:45:39 +0000593 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff88145032009-10-27 14:35:18 +0000594 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
595 assert (FEnt && "Cannot find FileEntry for Decl");
596 return clang_getFileName(FEnt);
597}
598
599static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000600 SourceLocation SLoc) {
Steve Naroff88145032009-10-27 14:35:18 +0000601 FileID FID;
602 if (SLoc.isFileID())
603 FID = SMgr.getFileID(SLoc);
604 else
605 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
606 return SMgr.getFileEntryForID(FID);
607}
608
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000609CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000610 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000611 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
612 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000613 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
614}
615
616const char *clang_getFileName(CXFile SFile) {
617 assert(SFile && "Passed null CXFile");
618 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
619 return FEnt->getName();
620}
621
622time_t clang_getFileTime(CXFile SFile) {
623 assert(SFile && "Passed null CXFile");
624 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
625 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000626}
627
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000628CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000629 assert(C.decl && "CXCursor has null decl");
630 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000631
Steve Narofff334b4e2009-09-02 18:26:48 +0000632 if (clang_isReference(C.kind)) {
633 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000634 case CXCursor_ObjCSuperClassRef: {
635 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
636 assert(OID && "clang_getCursorLine(): Missing interface decl");
637 return createCXString(OID->getSuperClass()->getIdentifier()
638 ->getNameStart());
639 }
640 case CXCursor_ObjCClassRef: {
641 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000642 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000643
644 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
645 assert(OCD && "clang_getCursorLine(): Missing category decl");
646 return createCXString(OCD->getClassInterface()->getIdentifier()
647 ->getNameStart());
648 }
649 case CXCursor_ObjCProtocolRef: {
650 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
651 assert(OID && "clang_getCursorLine(): Missing protocol decl");
652 return createCXString(OID->getIdentifier()->getNameStart());
653 }
654 case CXCursor_ObjCSelectorRef: {
655 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
656 static_cast<Stmt *>(C.stmt));
657 assert(OME && "clang_getCursorLine(): Missing message expr");
658 return createCXString(OME->getSelector().getAsString().c_str(), true);
659 }
660 case CXCursor_VarRef:
661 case CXCursor_FunctionRef:
662 case CXCursor_EnumConstantRef: {
663 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
664 static_cast<Stmt *>(C.stmt));
665 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
666 return createCXString(DRE->getDecl()->getIdentifier()->getNameStart());
667 }
668 default:
669 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000670 }
671 }
672 return clang_getDeclSpelling(C.decl);
673}
674
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000675const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000676 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000677 case CXCursor_FunctionDecl: return "FunctionDecl";
678 case CXCursor_TypedefDecl: return "TypedefDecl";
679 case CXCursor_EnumDecl: return "EnumDecl";
680 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
681 case CXCursor_StructDecl: return "StructDecl";
682 case CXCursor_UnionDecl: return "UnionDecl";
683 case CXCursor_ClassDecl: return "ClassDecl";
684 case CXCursor_FieldDecl: return "FieldDecl";
685 case CXCursor_VarDecl: return "VarDecl";
686 case CXCursor_ParmDecl: return "ParmDecl";
687 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
688 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
689 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
690 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
691 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
692 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
693 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
694 case CXCursor_FunctionDefn: return "FunctionDefn";
695 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
696 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
697 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
698 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
699 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
700 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
701 case CXCursor_ObjCClassRef: return "ObjCClassRef";
702 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000703
Daniel Dunbaracca7252009-11-30 20:42:49 +0000704 case CXCursor_VarRef: return "VarRef";
705 case CXCursor_FunctionRef: return "FunctionRef";
706 case CXCursor_EnumConstantRef: return "EnumConstantRef";
707 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000708
Daniel Dunbaracca7252009-11-30 20:42:49 +0000709 case CXCursor_InvalidFile: return "InvalidFile";
710 case CXCursor_NoDeclFound: return "NoDeclFound";
711 case CXCursor_NotImplemented: return "NotImplemented";
712 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000713 }
Steve Naroff600866c2009-08-27 19:51:58 +0000714}
Steve Naroff89922f82009-08-31 00:59:03 +0000715
Steve Naroff9efa7672009-09-04 15:44:05 +0000716static enum CXCursorKind TranslateKind(Decl *D) {
717 switch (D->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000718 case Decl::Function: return CXCursor_FunctionDecl;
719 case Decl::Typedef: return CXCursor_TypedefDecl;
720 case Decl::Enum: return CXCursor_EnumDecl;
721 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
722 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
723 case Decl::Field: return CXCursor_FieldDecl;
724 case Decl::Var: return CXCursor_VarDecl;
725 case Decl::ParmVar: return CXCursor_ParmDecl;
726 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
727 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
728 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
729 case Decl::ObjCMethod: {
730 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
731 if (MD->isInstanceMethod())
732 return CXCursor_ObjCInstanceMethodDecl;
733 return CXCursor_ObjCClassMethodDecl;
734 }
735 default: break;
Steve Naroff9efa7672009-09-04 15:44:05 +0000736 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000737 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000738}
Steve Naroff600866c2009-08-27 19:51:58 +0000739//
740// CXCursor Operations.
741//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000742
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000743CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000744 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000745 assert(CTUnit && "Passed null CXTranslationUnit");
746 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000747
Steve Naroff9efa7672009-09-04 15:44:05 +0000748 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000749 const FileEntry *File = FMgr.getFile(source_name,
750 source_name+strlen(source_name));
Steve Naroff77128dd2009-09-15 20:25:34 +0000751 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000752 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000753 return C;
754 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000755 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000756 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000757
Steve Narofff96b5242009-10-28 20:44:47 +0000758 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
759
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000760 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000761 &LastLoc);
762 if (ALoc.isValid())
763 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000764
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000765 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000766 if (ALoc.isNamedRef())
767 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000768 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000769 if (Dcl) {
770 if (Stm) {
771 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
772 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
773 return C;
774 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
775 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
776 return C;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000777 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000778 // Fall through...treat as a decl, not a ref.
779 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000780 if (ALoc.isNamedRef()) {
781 if (isa<ObjCInterfaceDecl>(Dcl)) {
782 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
783 return C;
784 }
785 if (isa<ObjCProtocolDecl>(Dcl)) {
786 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
787 return C;
788 }
789 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000790 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000791 return C;
792 }
Steve Narofffb570422009-09-22 19:25:29 +0000793 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000794 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000795}
796
Ted Kremenek73885552009-11-17 19:28:59 +0000797CXCursor clang_getNullCursor(void) {
798 CXCursor C;
799 C.kind = CXCursor_InvalidFile;
800 C.decl = NULL;
801 C.stmt = NULL;
802 return C;
803}
804
805unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
806 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
807}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000808
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000809CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000810 assert(AnonDecl && "Passed null CXDecl");
811 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000812
Steve Narofffb570422009-09-22 19:25:29 +0000813 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000814 return C;
815}
816
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000817unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000818 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
819}
820
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000821unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000822 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
823}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000824
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000825unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000826 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
827}
828
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000829unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000830 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
831}
832
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000833CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000834 return C.kind;
835}
836
Steve Naroff699a07d2009-09-25 21:32:34 +0000837static Decl *getDeclFromExpr(Stmt *E) {
838 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
839 return RefExpr->getDecl();
840 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
841 return ME->getMemberDecl();
842 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
843 return RE->getDecl();
844
845 if (CallExpr *CE = dyn_cast<CallExpr>(E))
846 return getDeclFromExpr(CE->getCallee());
847 if (CastExpr *CE = dyn_cast<CastExpr>(E))
848 return getDeclFromExpr(CE->getSubExpr());
849 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
850 return OME->getMethodDecl();
851
852 return 0;
853}
854
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000855CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000856 if (clang_isDeclaration(C.kind))
857 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000858
Steve Naroff699a07d2009-09-25 21:32:34 +0000859 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000860 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000861 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +0000862 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000863 return static_cast<Stmt *>(C.stmt);
864 else
865 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
866 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000867 return C.decl;
868 }
869 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000870}
871
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000872unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000873 assert(C.decl && "CXCursor has null decl");
874 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000875 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000876
Steve Narofff334b4e2009-09-02 18:26:48 +0000877 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000878 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000879}
Steve Narofff334b4e2009-09-02 18:26:48 +0000880
Steve Naroffef0cef62009-11-09 17:45:52 +0000881const char *clang_getCString(CXString string) {
882 return string.Spelling;
883}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000884
Steve Naroffef0cef62009-11-09 17:45:52 +0000885void clang_disposeString(CXString string) {
Benjamin Kramer858e5de2009-11-09 18:24:53 +0000886 if (string.MustFreeString)
887 free((void*)string.Spelling);
Steve Naroffef0cef62009-11-09 17:45:52 +0000888}
889
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000890unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000891 assert(C.decl && "CXCursor has null decl");
892 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000893 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000894
Steve Narofff334b4e2009-09-02 18:26:48 +0000895 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000896 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000897}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000898
899const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000900 assert(C.decl && "CXCursor has null decl");
901 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000902 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000903
Steve Narofff334b4e2009-09-02 18:26:48 +0000904 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000905
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000906 if (SLoc.isFileID()) {
907 const char *bufferName = SourceMgr.getBufferName(SLoc);
908 return bufferName[0] == '<' ? NULL : bufferName;
909 }
Douglas Gregor02465752009-10-16 21:24:31 +0000910
911 // Retrieve the file in which the macro was instantiated, then provide that
912 // buffer name.
913 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000914 const llvm::MemoryBuffer *Buffer
Douglas Gregor02465752009-10-16 21:24:31 +0000915 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
916 if (!Buffer)
917 return 0;
918
919 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000920}
921
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000922CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +0000923 assert(C.decl && "CXCursor has null decl");
924 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
925 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000926
Steve Naroff88145032009-10-27 14:35:18 +0000927 return (void *)getFileEntryFromSourceLocation(SourceMgr,
Daniel Dunbaracca7252009-11-30 20:42:49 +0000928 getLocationFromCursor(C,SourceMgr, ND));
Steve Naroff88145032009-10-27 14:35:18 +0000929}
930
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000931void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000932 const char **startBuf,
933 const char **endBuf,
934 unsigned *startLine,
935 unsigned *startColumn,
936 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000937 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000938 assert(C.decl && "CXCursor has null decl");
939 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
940 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
941 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000942
Steve Naroff4ade6d62009-09-23 17:52:52 +0000943 SourceManager &SM = FD->getASTContext().getSourceManager();
944 *startBuf = SM.getCharacterData(Body->getLBracLoc());
945 *endBuf = SM.getCharacterData(Body->getRBracLoc());
946 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
947 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
948 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
949 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
950}
951
Steve Naroff600866c2009-08-27 19:51:58 +0000952} // end extern "C"