blob: e6b3d60e19a6bf8fcde2d8e00f65987415a133aa [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
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000028//===----------------------------------------------------------------------===//
29// Crash Reporting.
30//===----------------------------------------------------------------------===//
31
32#ifdef __APPLE__
33#include "clang/Analysis/Support/SaveAndRestore.h"
34// Integrate with crash reporter.
35extern "C" const char *__crashreporter_info__;
36#endif
37
38//===----------------------------------------------------------------------===//
39// Visitors.
40//===----------------------------------------------------------------------===//
41
Steve Naroff89922f82009-08-31 00:59:03 +000042namespace {
Ted Kremenekab188932010-01-05 19:32:54 +000043static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000044 NamedDecl *D = DRE->getDecl();
45 if (isa<VarDecl>(D))
46 return CXCursor_VarRef;
47 else if (isa<FunctionDecl>(D))
48 return CXCursor_FunctionRef;
49 else if (isa<EnumConstantDecl>(D))
50 return CXCursor_EnumConstantRef;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000051 else
Steve Naroff4ade6d62009-09-23 17:52:52 +000052 return CXCursor_NotImplemented;
53}
54
55#if 0
56// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000057class CRefVisitor : public StmtVisitor<CRefVisitor> {
58 CXDecl CDecl;
59 CXDeclIterator Callback;
60 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000061
Steve Narofffb570422009-09-22 19:25:29 +000062 void Call(enum CXCursorKind CK, Stmt *SRef) {
63 CXCursor C = { CK, CDecl, SRef };
64 Callback(CDecl, C, CData);
65 }
66
67public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000068 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Narofffb570422009-09-22 19:25:29 +000069 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000070
Steve Narofffb570422009-09-22 19:25:29 +000071 void VisitStmt(Stmt *S) {
72 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
73 C != CEnd; ++C)
74 Visit(*C);
75 }
76 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000077 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000078 }
79 void VisitMemberExpr(MemberExpr *Node) {
80 Call(CXCursor_MemberRef, Node);
81 }
82 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
83 Call(CXCursor_ObjCSelectorRef, Node);
84 }
85 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
86 Call(CXCursor_ObjCIvarRef, Node);
87 }
88};
Steve Naroff4ade6d62009-09-23 17:52:52 +000089#endif
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000090
Steve Naroff89922f82009-08-31 00:59:03 +000091// Translation Unit Visitor.
92class TUVisitor : public DeclVisitor<TUVisitor> {
93 CXTranslationUnit TUnit;
94 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000095 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000096
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000097 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
98 // to the visitor. Declarations with a PCH level greater than this value will
99 // be suppressed.
100 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000101
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000102 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000103 // Filter any declarations that have a PCH level greater than what we allow.
104 if (ND->getPCHLevel() > MaxPCHLevel)
105 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000106
Steve Narofff96b5242009-10-28 20:44:47 +0000107 // Filter any implicit declarations (since the source info will be bogus).
108 if (ND->isImplicit())
109 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000110
Steve Narofffb570422009-09-22 19:25:29 +0000111 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000112 Callback(TUnit, C, CData);
113 }
Steve Naroff89922f82009-08-31 00:59:03 +0000114public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000115 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000116 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000117 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000118 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000119
Steve Naroff89922f82009-08-31 00:59:03 +0000120 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
121 VisitDeclContext(dyn_cast<DeclContext>(D));
122 }
123 void VisitDeclContext(DeclContext *DC) {
124 for (DeclContext::decl_iterator
125 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
126 Visit(*I);
127 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000128
129 void VisitFunctionDecl(FunctionDecl *ND) {
130 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
131 : CXCursor_FunctionDecl, ND);
132 }
133 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
134 Call(CXCursor_ObjCCategoryDecl, ND);
135 }
136 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
137 Call(CXCursor_ObjCCategoryDefn, ND);
138 }
139 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
140 Call(CXCursor_ObjCClassDefn, ND);
141 }
142 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
143 Call(CXCursor_ObjCInterfaceDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000144 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000145 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
146 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000147 }
148 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000149 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000150 case TagDecl::TK_struct:
151 Call(CXCursor_StructDecl, ND);
152 break;
153 case TagDecl::TK_class:
154 Call(CXCursor_ClassDecl, ND);
155 break;
156 case TagDecl::TK_union:
157 Call(CXCursor_UnionDecl, ND);
158 break;
159 case TagDecl::TK_enum:
160 Call(CXCursor_EnumDecl, ND);
161 break;
Steve Naroffc857ea42009-09-02 13:28:54 +0000162 }
Steve Naroff89922f82009-08-31 00:59:03 +0000163 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000164 void VisitTypedefDecl(TypedefDecl *ND) {
165 Call(CXCursor_TypedefDecl, ND);
166 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000167 void VisitVarDecl(VarDecl *ND) {
168 Call(CXCursor_VarDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000169 }
Steve Naroff89922f82009-08-31 00:59:03 +0000170};
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000171
Steve Naroff89922f82009-08-31 00:59:03 +0000172
Steve Naroffc857ea42009-09-02 13:28:54 +0000173// Declaration visitor.
174class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
175 CXDecl CDecl;
176 CXDeclIterator Callback;
177 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000178
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000179 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
180 // to the visitor. Declarations with a PCH level greater than this value will
181 // be suppressed.
182 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000183
Steve Naroffc857ea42009-09-02 13:28:54 +0000184 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000185 // Disable the callback when the context is equal to the visiting decl.
186 if (CDecl == ND && !clang_isReference(CK))
187 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000188
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000189 // Filter any declarations that have a PCH level greater than what we allow.
190 if (ND->getPCHLevel() > MaxPCHLevel)
191 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000192
Steve Narofffb570422009-09-22 19:25:29 +0000193 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000194 Callback(CDecl, C, CData);
195 }
Steve Naroff89922f82009-08-31 00:59:03 +0000196public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000197 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
198 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000199 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000200
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000201 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
202 // Issue callbacks for the containing class.
203 Call(CXCursor_ObjCClassRef, ND);
204 // FIXME: Issue callbacks for protocol refs.
205 VisitDeclContext(dyn_cast<DeclContext>(ND));
206 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000207 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000208 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000209 if (D->getSuperClass())
210 Call(CXCursor_ObjCSuperClassRef, D);
211
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000212 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000213 E = D->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000214 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000215 VisitDeclContext(dyn_cast<DeclContext>(D));
216 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000217 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000218 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000219 E = PID->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000220 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000221
Steve Naroff9efa7672009-09-04 15:44:05 +0000222 VisitDeclContext(dyn_cast<DeclContext>(PID));
223 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000224 void VisitTagDecl(TagDecl *D) {
225 VisitDeclContext(dyn_cast<DeclContext>(D));
226 }
227 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
228 VisitDeclContext(dyn_cast<DeclContext>(D));
229 }
230 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
231 VisitDeclContext(dyn_cast<DeclContext>(D));
232 }
Steve Naroff89922f82009-08-31 00:59:03 +0000233 void VisitDeclContext(DeclContext *DC) {
234 for (DeclContext::decl_iterator
235 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
236 Visit(*I);
237 }
238 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000239 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000240 }
241 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000242 Call(CXCursor_FieldDecl, ND);
243 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000244 void VisitVarDecl(VarDecl *ND) {
245 Call(CXCursor_VarDecl, ND);
246 }
247 void VisitParmVarDecl(ParmVarDecl *ND) {
248 Call(CXCursor_ParmDecl, ND);
249 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000250 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
251 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000252 }
253 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000254 Call(CXCursor_ObjCIvarDecl, ND);
255 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000256 void VisitFunctionDecl(FunctionDecl *ND) {
257 if (ND->isThisDeclarationADefinition()) {
258 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000259#if 0
260 // Not currently needed.
261 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000262 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000263 RVisit.Visit(Body);
264#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000265 }
266 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000267 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
268 if (ND->getBody()) {
269 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
270 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000271 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000272 } else
273 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
274 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000275 }
276};
Ted Kremenekab188932010-01-05 19:32:54 +0000277} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000278
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000279static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000280 SourceManager &SourceMgr,
281 NamedDecl *ND) {
282 if (clang_isReference(C.kind)) {
283 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000284 case CXCursor_ObjCClassRef: {
285 if (isa<ObjCInterfaceDecl>(ND)) {
286 // FIXME: This is a hack (storing the parent decl in the stmt slot).
287 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
288 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000289 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000290 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
291 assert(OID && "clang_getCursorLine(): Missing category decl");
292 return OID->getClassInterface()->getLocation();
293 }
294 case CXCursor_ObjCSuperClassRef: {
295 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
296 assert(OID && "clang_getCursorLine(): Missing interface decl");
297 return OID->getSuperClassLoc();
298 }
299 case CXCursor_ObjCProtocolRef: {
300 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
301 assert(OID && "clang_getCursorLine(): Missing protocol decl");
302 return OID->getLocation();
303 }
304 case CXCursor_ObjCSelectorRef: {
305 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
306 static_cast<Stmt *>(C.stmt));
307 assert(OME && "clang_getCursorLine(): Missing message expr");
308 return OME->getLeftLoc(); /* FIXME: should be a range */
309 }
310 case CXCursor_VarRef:
311 case CXCursor_FunctionRef:
312 case CXCursor_EnumConstantRef: {
313 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
314 static_cast<Stmt *>(C.stmt));
315 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
316 return DRE->getLocation();
317 }
318 default:
319 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000320 }
321 } else { // We have a declaration or a definition.
322 SourceLocation SLoc;
323 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000324 case Decl::ObjCInterface: {
325 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
326 break;
327 }
328 case Decl::ObjCProtocol: {
329 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
330 break;
331 }
332 default: {
333 SLoc = ND->getLocation();
334 break;
335 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000336 }
337 if (SLoc.isInvalid())
338 return SourceLocation();
339 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
340 }
341}
342
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000343static CXString createCXString(const char *String, bool DupString = false) {
344 CXString Str;
345 if (DupString) {
346 Str.Spelling = strdup(String);
347 Str.MustFreeString = 1;
348 } else {
349 Str.Spelling = String;
350 Str.MustFreeString = 0;
351 }
352 return Str;
353}
354
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000355extern "C" {
356
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000357CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000358 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000359 CIndexer *CIdxr = new CIndexer(new Program());
360 if (excludeDeclarationsFromPCH)
361 CIdxr->setOnlyLocalDecls();
362 if (displayDiagnostics)
363 CIdxr->setDisplayDiagnostics();
364 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000365}
366
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000367void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000368 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000369 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000370}
371
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000372void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
373 assert(CIdx && "Passed null CXIndex");
374 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
375 CXXIdx->setUseExternalASTGeneration(value);
376}
377
Steve Naroff50398192009-08-28 15:28:48 +0000378// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000379CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
380 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000381 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000382 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000383
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000384 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
385 CXXIdx->getOnlyLocalDecls(),
386 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000387}
388
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000389CXTranslationUnit
390clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
391 const char *source_filename,
392 int num_command_line_args,
393 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000394 assert(CIdx && "Passed null CXIndex");
395 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
396
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000397 if (!CXXIdx->getUseExternalASTGeneration()) {
398 llvm::SmallVector<const char *, 16> Args;
399
400 // The 'source_filename' argument is optional. If the caller does not
401 // specify it then it is assumed that the source file is specified
402 // in the actual argument list.
403 if (source_filename)
404 Args.push_back(source_filename);
405 Args.insert(Args.end(), command_line_args,
406 command_line_args + num_command_line_args);
407
Daniel Dunbar94220972009-12-05 02:17:18 +0000408 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000409
410#ifdef __APPLE__
411 // Integrate with crash reporter.
412 static unsigned counter = 0;
413 static const char* reportStrings[16] = { 0 };
414
415 llvm::SmallString<1028> CrashString;
416 {
417 llvm::raw_svector_ostream Out(CrashString);
418 Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
419 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
420 E=Args.end(); I!=E; ++I)
421 Out << ' ' << *I;
422 }
423
424 unsigned myCounter = counter;
425 counter = myCounter == 15 ? 0 : myCounter + 1;
426
427 while (reportStrings[myCounter]) {
428 myCounter = counter;
429 counter = myCounter == 15 ? 0 : myCounter + 1;
430 }
431
432 SaveAndRestore<const char*> OldCrashString(reportStrings[myCounter],
433 CrashString.c_str());
434
435 // We need to create an aggregate string because multiple threads
436 // may be in this method at one time. The crash reporter string
437 // will attempt to overapproximate the set of in-flight invocations
438 // of this function. Race conditions can still cause this goal
439 // to not be achieved.
440 llvm::SmallString<1028> AggregateString;
441 {
442 llvm::raw_svector_ostream Out(AggregateString);
443 for (unsigned i = 0; i < 16; ++i)
444 if (reportStrings[i]) Out << reportStrings[i] << '\n';
445 }
446 __crashreporter_info__ = AggregateString.c_str();
447#endif
448
Daniel Dunbar94220972009-12-05 02:17:18 +0000449 llvm::OwningPtr<ASTUnit> Unit(
450 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000451 CXXIdx->getDiags(),
452 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000453 CXXIdx->getOnlyLocalDecls(),
454 /* UseBumpAllocator = */ true));
455
456 // FIXME: Until we have broader testing, just drop the entire AST if we
457 // encountered an error.
458 if (NumErrors != CXXIdx->getDiags().getNumErrors())
459 return 0;
460
461 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000462 }
463
Ted Kremenek139ba862009-10-22 00:03:57 +0000464 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000465 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000466
Ted Kremenek139ba862009-10-22 00:03:57 +0000467 // First add the complete path to the 'clang' executable.
468 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000469 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000470
Ted Kremenek139ba862009-10-22 00:03:57 +0000471 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000472 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000473
Ted Kremenek139ba862009-10-22 00:03:57 +0000474 // The 'source_filename' argument is optional. If the caller does not
475 // specify it then it is assumed that the source file is specified
476 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000477 if (source_filename)
478 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000479
Steve Naroff37b5ac22009-10-15 20:50:09 +0000480 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000481 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000482 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000483 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000484
485 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
486 for (int i = 0; i < num_command_line_args; ++i)
487 if (const char *arg = command_line_args[i]) {
488 if (strcmp(arg, "-o") == 0) {
489 ++i; // Also skip the matching argument.
490 continue;
491 }
492 if (strcmp(arg, "-emit-ast") == 0 ||
493 strcmp(arg, "-c") == 0 ||
494 strcmp(arg, "-fsyntax-only") == 0) {
495 continue;
496 }
497
498 // Keep the argument.
499 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000500 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000501
Ted Kremenek139ba862009-10-22 00:03:57 +0000502 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000503 argv.push_back(NULL);
504
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000505 // Invoke 'clang'.
506 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
507 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000508 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000509 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000510 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
511 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
512 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000513
Ted Kremenek0854d702009-11-10 19:18:52 +0000514 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000515 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000516 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000517 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000518 I!=E; ++I) {
519 if (*I)
520 llvm::errs() << ' ' << *I << '\n';
521 }
522 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000523 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000524
Steve Naroff37b5ac22009-10-15 20:50:09 +0000525 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000526 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000527 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000528 if (ATU)
529 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000530 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000531}
532
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000533void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000534 assert(CTUnit && "Passed null CXTranslationUnit");
535 delete static_cast<ASTUnit *>(CTUnit);
536}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000537
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000538CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000539 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000540 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000541 return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000542}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000543
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000544void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000545 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000546 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000547 assert(CTUnit && "Passed null CXTranslationUnit");
548 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
549 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000550
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000551 unsigned PCHLevel = Decl::MaxPCHLevel;
552
553 // Set the PCHLevel to filter out unwanted decls if requested.
554 if (CXXUnit->getOnlyLocalDecls()) {
555 PCHLevel = 0;
556
557 // If the main input was an AST, bump the level.
558 if (CXXUnit->isMainFileAST())
559 ++PCHLevel;
560 }
561
562 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000563
564 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
565 // decls.
566 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
567 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
568 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
569 ie = TLDs.end(); it != ie; ++it) {
570 DVisit.Visit(*it);
571 }
572 } else
573 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000574}
575
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000576void clang_loadDeclaration(CXDecl Dcl,
577 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000578 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000579 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000580
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000581 CDeclVisitor DVisit(Dcl, callback, CData,
582 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000583 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000584}
585
Steve Naroff600866c2009-08-27 19:51:58 +0000586//
587// CXDecl Operations.
588//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000589
590CXEntity clang_getEntityFromDecl(CXDecl) {
Steve Naroff600866c2009-08-27 19:51:58 +0000591 return 0;
592}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000593
594CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000595 assert(AnonDecl && "Passed null CXDecl");
596 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000597
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000598 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
599 return createCXString(OMD->getSelector().getAsString().c_str(), true);
600
601 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000602 // No, this isn't the same as the code below. getIdentifier() is non-virtual
603 // and returns different names. NamedDecl returns the class name and
604 // ObjCCategoryImplDecl returns the category name.
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000605 return createCXString(CIMP->getIdentifier()->getNameStart());
606
607 if (ND->getIdentifier())
608 return createCXString(ND->getIdentifier()->getNameStart());
609
610 return createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000611}
Steve Narofff334b4e2009-09-02 18:26:48 +0000612
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000613unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000614 assert(AnonDecl && "Passed null CXDecl");
615 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
616 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
617 return SourceMgr.getSpellingLineNumber(ND->getLocation());
618}
619
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000620unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000621 assert(AnonDecl && "Passed null CXDecl");
622 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
623 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000624 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000625}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000626
627CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
628 assert(AnonDecl && "Passed null CXDecl");
629 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
630 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
631 SourceRange R = ND->getSourceRange();
632
633 CXDeclExtent extent;
634
635 SourceLocation L = SourceMgr.getSpellingLoc(R.getBegin());
636 extent.begin.line = SourceMgr.getSpellingLineNumber(L);
637 extent.begin.column = SourceMgr.getSpellingColumnNumber(L);
638
639 L = SourceMgr.getSpellingLoc(R.getEnd());
640 extent.end.line = SourceMgr.getSpellingLineNumber(L);
641 extent.end.column = SourceMgr.getSpellingColumnNumber(L);
642
643 return extent;
644}
Steve Naroff699a07d2009-09-25 21:32:34 +0000645
Steve Naroff88145032009-10-27 14:35:18 +0000646static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000647 SourceLocation SLoc) {
Steve Naroff88145032009-10-27 14:35:18 +0000648 FileID FID;
649 if (SLoc.isFileID())
650 FID = SMgr.getFileID(SLoc);
651 else
652 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
653 return SMgr.getFileEntryForID(FID);
654}
655
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000656CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000657 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000658 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
659 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000660 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
661}
662
663const char *clang_getFileName(CXFile SFile) {
664 assert(SFile && "Passed null CXFile");
665 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
666 return FEnt->getName();
667}
668
669time_t clang_getFileTime(CXFile SFile) {
670 assert(SFile && "Passed null CXFile");
671 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
672 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000673}
674
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000675CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000676 assert(C.decl && "CXCursor has null decl");
677 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000678
Steve Narofff334b4e2009-09-02 18:26:48 +0000679 if (clang_isReference(C.kind)) {
680 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000681 case CXCursor_ObjCSuperClassRef: {
682 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
683 assert(OID && "clang_getCursorLine(): Missing interface decl");
684 return createCXString(OID->getSuperClass()->getIdentifier()
685 ->getNameStart());
686 }
687 case CXCursor_ObjCClassRef: {
688 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000689 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000690
691 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
692 assert(OCD && "clang_getCursorLine(): Missing category decl");
693 return createCXString(OCD->getClassInterface()->getIdentifier()
694 ->getNameStart());
695 }
696 case CXCursor_ObjCProtocolRef: {
697 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
698 assert(OID && "clang_getCursorLine(): Missing protocol decl");
699 return createCXString(OID->getIdentifier()->getNameStart());
700 }
701 case CXCursor_ObjCSelectorRef: {
702 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
703 static_cast<Stmt *>(C.stmt));
704 assert(OME && "clang_getCursorLine(): Missing message expr");
705 return createCXString(OME->getSelector().getAsString().c_str(), true);
706 }
707 case CXCursor_VarRef:
708 case CXCursor_FunctionRef:
709 case CXCursor_EnumConstantRef: {
710 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
711 static_cast<Stmt *>(C.stmt));
712 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
713 return createCXString(DRE->getDecl()->getIdentifier()->getNameStart());
714 }
715 default:
716 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000717 }
718 }
719 return clang_getDeclSpelling(C.decl);
720}
721
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000722const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000723 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000724 case CXCursor_FunctionDecl: return "FunctionDecl";
725 case CXCursor_TypedefDecl: return "TypedefDecl";
726 case CXCursor_EnumDecl: return "EnumDecl";
727 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
728 case CXCursor_StructDecl: return "StructDecl";
729 case CXCursor_UnionDecl: return "UnionDecl";
730 case CXCursor_ClassDecl: return "ClassDecl";
731 case CXCursor_FieldDecl: return "FieldDecl";
732 case CXCursor_VarDecl: return "VarDecl";
733 case CXCursor_ParmDecl: return "ParmDecl";
734 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
735 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
736 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
737 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
738 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
739 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
740 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
741 case CXCursor_FunctionDefn: return "FunctionDefn";
742 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
743 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
744 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
745 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
746 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
747 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
748 case CXCursor_ObjCClassRef: return "ObjCClassRef";
749 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000750
Daniel Dunbaracca7252009-11-30 20:42:49 +0000751 case CXCursor_VarRef: return "VarRef";
752 case CXCursor_FunctionRef: return "FunctionRef";
753 case CXCursor_EnumConstantRef: return "EnumConstantRef";
754 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000755
Daniel Dunbaracca7252009-11-30 20:42:49 +0000756 case CXCursor_InvalidFile: return "InvalidFile";
757 case CXCursor_NoDeclFound: return "NoDeclFound";
758 case CXCursor_NotImplemented: return "NotImplemented";
759 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000760 }
Steve Naroff600866c2009-08-27 19:51:58 +0000761}
Steve Naroff89922f82009-08-31 00:59:03 +0000762
Steve Naroff9efa7672009-09-04 15:44:05 +0000763static enum CXCursorKind TranslateKind(Decl *D) {
764 switch (D->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000765 case Decl::Function: return CXCursor_FunctionDecl;
766 case Decl::Typedef: return CXCursor_TypedefDecl;
767 case Decl::Enum: return CXCursor_EnumDecl;
768 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
769 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
770 case Decl::Field: return CXCursor_FieldDecl;
771 case Decl::Var: return CXCursor_VarDecl;
772 case Decl::ParmVar: return CXCursor_ParmDecl;
773 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
774 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
775 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
776 case Decl::ObjCMethod: {
777 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
778 if (MD->isInstanceMethod())
779 return CXCursor_ObjCInstanceMethodDecl;
780 return CXCursor_ObjCClassMethodDecl;
781 }
782 default: break;
Steve Naroff9efa7672009-09-04 15:44:05 +0000783 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000784 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000785}
Steve Naroff600866c2009-08-27 19:51:58 +0000786//
787// CXCursor Operations.
788//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000789
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000790CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000791 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000792 assert(CTUnit && "Passed null CXTranslationUnit");
793 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000794
Steve Naroff9efa7672009-09-04 15:44:05 +0000795 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000796 const FileEntry *File = FMgr.getFile(source_name,
797 source_name+strlen(source_name));
Steve Naroff77128dd2009-09-15 20:25:34 +0000798 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000799 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000800 return C;
801 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000802 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000803 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000804
Steve Narofff96b5242009-10-28 20:44:47 +0000805 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
806
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000807 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000808 &LastLoc);
809 if (ALoc.isValid())
810 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000811
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000812 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000813 if (ALoc.isNamedRef())
814 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000815 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000816 if (Dcl) {
817 if (Stm) {
818 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
819 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
820 return C;
821 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
822 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
823 return C;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000824 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000825 // Fall through...treat as a decl, not a ref.
826 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000827 if (ALoc.isNamedRef()) {
828 if (isa<ObjCInterfaceDecl>(Dcl)) {
829 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
830 return C;
831 }
832 if (isa<ObjCProtocolDecl>(Dcl)) {
833 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
834 return C;
835 }
836 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000837 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000838 return C;
839 }
Steve Narofffb570422009-09-22 19:25:29 +0000840 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000841 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000842}
843
Ted Kremenek73885552009-11-17 19:28:59 +0000844CXCursor clang_getNullCursor(void) {
845 CXCursor C;
846 C.kind = CXCursor_InvalidFile;
847 C.decl = NULL;
848 C.stmt = NULL;
849 return C;
850}
851
852unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
853 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
854}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000855
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000856CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000857 assert(AnonDecl && "Passed null CXDecl");
858 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000859
Steve Narofffb570422009-09-22 19:25:29 +0000860 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000861 return C;
862}
863
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000864unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000865 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
866}
867
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000868unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000869 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
870}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000871
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000872unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000873 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
874}
875
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000876unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000877 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
878}
879
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000880CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000881 return C.kind;
882}
883
Steve Naroff699a07d2009-09-25 21:32:34 +0000884static Decl *getDeclFromExpr(Stmt *E) {
885 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
886 return RefExpr->getDecl();
887 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
888 return ME->getMemberDecl();
889 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
890 return RE->getDecl();
891
892 if (CallExpr *CE = dyn_cast<CallExpr>(E))
893 return getDeclFromExpr(CE->getCallee());
894 if (CastExpr *CE = dyn_cast<CastExpr>(E))
895 return getDeclFromExpr(CE->getSubExpr());
896 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
897 return OME->getMethodDecl();
898
899 return 0;
900}
901
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000902CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000903 if (clang_isDeclaration(C.kind))
904 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000905
Steve Naroff699a07d2009-09-25 21:32:34 +0000906 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000907 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000908 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +0000909 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000910 return static_cast<Stmt *>(C.stmt);
911 else
912 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
913 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000914 return C.decl;
915 }
916 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000917}
918
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000919unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000920 assert(C.decl && "CXCursor has null decl");
921 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000922 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000923
Steve Narofff334b4e2009-09-02 18:26:48 +0000924 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000925 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000926}
Steve Narofff334b4e2009-09-02 18:26:48 +0000927
Steve Naroffef0cef62009-11-09 17:45:52 +0000928const char *clang_getCString(CXString string) {
929 return string.Spelling;
930}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000931
Steve Naroffef0cef62009-11-09 17:45:52 +0000932void clang_disposeString(CXString string) {
Benjamin Kramer858e5de2009-11-09 18:24:53 +0000933 if (string.MustFreeString)
934 free((void*)string.Spelling);
Steve Naroffef0cef62009-11-09 17:45:52 +0000935}
936
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000937unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000938 assert(C.decl && "CXCursor has null decl");
939 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000940 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000941
Steve Narofff334b4e2009-09-02 18:26:48 +0000942 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000943 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000944}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000945
946const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000947 assert(C.decl && "CXCursor has null decl");
948 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000949 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000950
Steve Narofff334b4e2009-09-02 18:26:48 +0000951 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000952
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000953 if (SLoc.isFileID()) {
954 const char *bufferName = SourceMgr.getBufferName(SLoc);
955 return bufferName[0] == '<' ? NULL : bufferName;
956 }
Douglas Gregor02465752009-10-16 21:24:31 +0000957
958 // Retrieve the file in which the macro was instantiated, then provide that
959 // buffer name.
960 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000961 const llvm::MemoryBuffer *Buffer
Douglas Gregor02465752009-10-16 21:24:31 +0000962 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
963 if (!Buffer)
964 return 0;
965
966 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000967}
968
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000969CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +0000970 assert(C.decl && "CXCursor has null decl");
971 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
972 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000973
Steve Naroff88145032009-10-27 14:35:18 +0000974 return (void *)getFileEntryFromSourceLocation(SourceMgr,
Daniel Dunbaracca7252009-11-30 20:42:49 +0000975 getLocationFromCursor(C,SourceMgr, ND));
Steve Naroff88145032009-10-27 14:35:18 +0000976}
977
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000978void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000979 const char **startBuf,
980 const char **endBuf,
981 unsigned *startLine,
982 unsigned *startColumn,
983 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000984 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000985 assert(C.decl && "CXCursor has null decl");
986 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
987 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
988 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000989
Steve Naroff4ade6d62009-09-23 17:52:52 +0000990 SourceManager &SM = FD->getASTContext().getSourceManager();
991 *startBuf = SM.getCharacterData(Body->getLBracLoc());
992 *endBuf = SM.getCharacterData(Body->getRBracLoc());
993 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
994 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
995 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
996 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
997}
998
Steve Naroff600866c2009-08-27 19:51:58 +0000999} // end extern "C"