blob: 2c3f7373a31fb92961223730f8db4ecf67df9bcf [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"
Ted Kremenekd8210652010-01-06 23:43:31 +000019#include "clang/Lex/Lexer.h"
Douglas Gregor02465752009-10-16 21:24:31 +000020#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000021#include "llvm/System/Program.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000022
Ted Kremenekdb3d0da2010-01-05 20:55:39 +000023// Needed to define L_TMPNAM on some systems.
24#include <cstdio>
25
Steve Naroff50398192009-08-28 15:28:48 +000026using namespace clang;
27using namespace idx;
28
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000029//===----------------------------------------------------------------------===//
30// Crash Reporting.
31//===----------------------------------------------------------------------===//
32
33#ifdef __APPLE__
34#include "clang/Analysis/Support/SaveAndRestore.h"
35// Integrate with crash reporter.
36extern "C" const char *__crashreporter_info__;
37#endif
38
39//===----------------------------------------------------------------------===//
40// Visitors.
41//===----------------------------------------------------------------------===//
42
Steve Naroff89922f82009-08-31 00:59:03 +000043namespace {
Ted Kremenekab188932010-01-05 19:32:54 +000044static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000045 NamedDecl *D = DRE->getDecl();
46 if (isa<VarDecl>(D))
47 return CXCursor_VarRef;
48 else if (isa<FunctionDecl>(D))
49 return CXCursor_FunctionRef;
50 else if (isa<EnumConstantDecl>(D))
51 return CXCursor_EnumConstantRef;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000052 else
Steve Naroff4ade6d62009-09-23 17:52:52 +000053 return CXCursor_NotImplemented;
54}
55
56#if 0
57// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +000058class CRefVisitor : public StmtVisitor<CRefVisitor> {
59 CXDecl CDecl;
60 CXDeclIterator Callback;
61 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000062
Steve Narofffb570422009-09-22 19:25:29 +000063 void Call(enum CXCursorKind CK, Stmt *SRef) {
64 CXCursor C = { CK, CDecl, SRef };
65 Callback(CDecl, C, CData);
66 }
67
68public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000069 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Narofffb570422009-09-22 19:25:29 +000070 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000071
Steve Narofffb570422009-09-22 19:25:29 +000072 void VisitStmt(Stmt *S) {
73 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
74 C != CEnd; ++C)
75 Visit(*C);
76 }
77 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +000078 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +000079 }
80 void VisitMemberExpr(MemberExpr *Node) {
81 Call(CXCursor_MemberRef, Node);
82 }
83 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
84 Call(CXCursor_ObjCSelectorRef, Node);
85 }
86 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
87 Call(CXCursor_ObjCIvarRef, Node);
88 }
89};
Steve Naroff4ade6d62009-09-23 17:52:52 +000090#endif
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000091
Steve Naroff89922f82009-08-31 00:59:03 +000092// Translation Unit Visitor.
93class TUVisitor : public DeclVisitor<TUVisitor> {
94 CXTranslationUnit TUnit;
95 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000096 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +000097
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000098 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
99 // to the visitor. Declarations with a PCH level greater than this value will
100 // be suppressed.
101 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000102
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000103 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000104 // Filter any declarations that have a PCH level greater than what we allow.
105 if (ND->getPCHLevel() > MaxPCHLevel)
106 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000107
Steve Narofff96b5242009-10-28 20:44:47 +0000108 // Filter any implicit declarations (since the source info will be bogus).
109 if (ND->isImplicit())
110 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000111
Steve Narofffb570422009-09-22 19:25:29 +0000112 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000113 Callback(TUnit, C, CData);
114 }
Steve Naroff89922f82009-08-31 00:59:03 +0000115public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000116 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000117 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000118 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000119 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000120
Steve Naroff89922f82009-08-31 00:59:03 +0000121 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
122 VisitDeclContext(dyn_cast<DeclContext>(D));
123 }
124 void VisitDeclContext(DeclContext *DC) {
125 for (DeclContext::decl_iterator
126 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
127 Visit(*I);
128 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000129
130 void VisitFunctionDecl(FunctionDecl *ND) {
131 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
132 : CXCursor_FunctionDecl, ND);
133 }
134 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
135 Call(CXCursor_ObjCCategoryDecl, ND);
136 }
137 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
138 Call(CXCursor_ObjCCategoryDefn, ND);
139 }
140 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
141 Call(CXCursor_ObjCClassDefn, ND);
142 }
143 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
144 Call(CXCursor_ObjCInterfaceDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000145 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000146 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
147 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000148 }
149 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000150 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000151 case TagDecl::TK_struct:
152 Call(CXCursor_StructDecl, ND);
153 break;
154 case TagDecl::TK_class:
155 Call(CXCursor_ClassDecl, ND);
156 break;
157 case TagDecl::TK_union:
158 Call(CXCursor_UnionDecl, ND);
159 break;
160 case TagDecl::TK_enum:
161 Call(CXCursor_EnumDecl, ND);
162 break;
Steve Naroffc857ea42009-09-02 13:28:54 +0000163 }
Steve Naroff89922f82009-08-31 00:59:03 +0000164 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000165 void VisitTypedefDecl(TypedefDecl *ND) {
166 Call(CXCursor_TypedefDecl, ND);
167 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000168 void VisitVarDecl(VarDecl *ND) {
169 Call(CXCursor_VarDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000170 }
Steve Naroff89922f82009-08-31 00:59:03 +0000171};
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000172
Steve Naroff89922f82009-08-31 00:59:03 +0000173
Steve Naroffc857ea42009-09-02 13:28:54 +0000174// Declaration visitor.
175class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
176 CXDecl CDecl;
177 CXDeclIterator Callback;
178 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000179
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000180 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
181 // to the visitor. Declarations with a PCH level greater than this value will
182 // be suppressed.
183 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000184
Steve Naroffc857ea42009-09-02 13:28:54 +0000185 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000186 // Disable the callback when the context is equal to the visiting decl.
187 if (CDecl == ND && !clang_isReference(CK))
188 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000189
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000190 // Filter any declarations that have a PCH level greater than what we allow.
191 if (ND->getPCHLevel() > MaxPCHLevel)
192 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000193
Steve Narofffb570422009-09-22 19:25:29 +0000194 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000195 Callback(CDecl, C, CData);
196 }
Steve Naroff89922f82009-08-31 00:59:03 +0000197public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000198 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
199 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000200 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000201
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000202 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
203 // Issue callbacks for the containing class.
204 Call(CXCursor_ObjCClassRef, ND);
205 // FIXME: Issue callbacks for protocol refs.
206 VisitDeclContext(dyn_cast<DeclContext>(ND));
207 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000208 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000209 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000210 if (D->getSuperClass())
211 Call(CXCursor_ObjCSuperClassRef, D);
212
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000213 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000214 E = D->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000215 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000216 VisitDeclContext(dyn_cast<DeclContext>(D));
217 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000218 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000219 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000220 E = PID->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000221 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000222
Steve Naroff9efa7672009-09-04 15:44:05 +0000223 VisitDeclContext(dyn_cast<DeclContext>(PID));
224 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000225 void VisitTagDecl(TagDecl *D) {
226 VisitDeclContext(dyn_cast<DeclContext>(D));
227 }
228 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
229 VisitDeclContext(dyn_cast<DeclContext>(D));
230 }
231 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
232 VisitDeclContext(dyn_cast<DeclContext>(D));
233 }
Steve Naroff89922f82009-08-31 00:59:03 +0000234 void VisitDeclContext(DeclContext *DC) {
235 for (DeclContext::decl_iterator
236 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
237 Visit(*I);
238 }
239 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000240 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000241 }
242 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000243 Call(CXCursor_FieldDecl, ND);
244 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000245 void VisitVarDecl(VarDecl *ND) {
246 Call(CXCursor_VarDecl, ND);
247 }
248 void VisitParmVarDecl(ParmVarDecl *ND) {
249 Call(CXCursor_ParmDecl, ND);
250 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000251 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
252 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000253 }
254 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000255 Call(CXCursor_ObjCIvarDecl, ND);
256 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000257 void VisitFunctionDecl(FunctionDecl *ND) {
258 if (ND->isThisDeclarationADefinition()) {
259 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000260#if 0
261 // Not currently needed.
262 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000263 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000264 RVisit.Visit(Body);
265#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000266 }
267 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000268 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
269 if (ND->getBody()) {
270 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
271 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000272 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000273 } else
274 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
275 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000276 }
277};
Ted Kremenekab188932010-01-05 19:32:54 +0000278} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000279
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000280static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000281 SourceManager &SourceMgr,
282 NamedDecl *ND) {
283 if (clang_isReference(C.kind)) {
284 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000285 case CXCursor_ObjCClassRef: {
286 if (isa<ObjCInterfaceDecl>(ND)) {
287 // FIXME: This is a hack (storing the parent decl in the stmt slot).
288 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
289 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000290 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000291 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
292 assert(OID && "clang_getCursorLine(): Missing category decl");
293 return OID->getClassInterface()->getLocation();
294 }
295 case CXCursor_ObjCSuperClassRef: {
296 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
297 assert(OID && "clang_getCursorLine(): Missing interface decl");
298 return OID->getSuperClassLoc();
299 }
300 case CXCursor_ObjCProtocolRef: {
301 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
302 assert(OID && "clang_getCursorLine(): Missing protocol decl");
303 return OID->getLocation();
304 }
305 case CXCursor_ObjCSelectorRef: {
306 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
307 static_cast<Stmt *>(C.stmt));
308 assert(OME && "clang_getCursorLine(): Missing message expr");
309 return OME->getLeftLoc(); /* FIXME: should be a range */
310 }
311 case CXCursor_VarRef:
312 case CXCursor_FunctionRef:
313 case CXCursor_EnumConstantRef: {
314 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
315 static_cast<Stmt *>(C.stmt));
316 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
317 return DRE->getLocation();
318 }
319 default:
320 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000321 }
322 } else { // We have a declaration or a definition.
323 SourceLocation SLoc;
324 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000325 case Decl::ObjCInterface: {
326 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
327 break;
328 }
329 case Decl::ObjCProtocol: {
330 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
331 break;
332 }
333 default: {
334 SLoc = ND->getLocation();
335 break;
336 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000337 }
338 if (SLoc.isInvalid())
339 return SourceLocation();
340 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
341 }
342}
343
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000344static CXString createCXString(const char *String, bool DupString = false) {
345 CXString Str;
346 if (DupString) {
347 Str.Spelling = strdup(String);
348 Str.MustFreeString = 1;
349 } else {
350 Str.Spelling = String;
351 Str.MustFreeString = 0;
352 }
353 return Str;
354}
355
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000356extern "C" {
357
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000358CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000359 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000360 CIndexer *CIdxr = new CIndexer(new Program());
361 if (excludeDeclarationsFromPCH)
362 CIdxr->setOnlyLocalDecls();
363 if (displayDiagnostics)
364 CIdxr->setDisplayDiagnostics();
365 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000366}
367
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000368void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000369 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000370 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000371}
372
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000373void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
374 assert(CIdx && "Passed null CXIndex");
375 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
376 CXXIdx->setUseExternalASTGeneration(value);
377}
378
Steve Naroff50398192009-08-28 15:28:48 +0000379// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000380CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
381 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000382 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000383 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000384
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000385 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
386 CXXIdx->getOnlyLocalDecls(),
387 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000388}
389
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000390CXTranslationUnit
391clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
392 const char *source_filename,
393 int num_command_line_args,
394 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000395 assert(CIdx && "Passed null CXIndex");
396 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
397
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000398 if (!CXXIdx->getUseExternalASTGeneration()) {
399 llvm::SmallVector<const char *, 16> Args;
400
401 // The 'source_filename' argument is optional. If the caller does not
402 // specify it then it is assumed that the source file is specified
403 // in the actual argument list.
404 if (source_filename)
405 Args.push_back(source_filename);
406 Args.insert(Args.end(), command_line_args,
407 command_line_args + num_command_line_args);
408
Daniel Dunbar94220972009-12-05 02:17:18 +0000409 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000410
411#ifdef __APPLE__
412 // Integrate with crash reporter.
413 static unsigned counter = 0;
414 static const char* reportStrings[16] = { 0 };
415
416 llvm::SmallString<1028> CrashString;
417 {
418 llvm::raw_svector_ostream Out(CrashString);
419 Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
420 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
421 E=Args.end(); I!=E; ++I)
422 Out << ' ' << *I;
423 }
424
425 unsigned myCounter = counter;
426 counter = myCounter == 15 ? 0 : myCounter + 1;
427
428 while (reportStrings[myCounter]) {
429 myCounter = counter;
430 counter = myCounter == 15 ? 0 : myCounter + 1;
431 }
432
433 SaveAndRestore<const char*> OldCrashString(reportStrings[myCounter],
434 CrashString.c_str());
435
436 // We need to create an aggregate string because multiple threads
437 // may be in this method at one time. The crash reporter string
438 // will attempt to overapproximate the set of in-flight invocations
439 // of this function. Race conditions can still cause this goal
440 // to not be achieved.
441 llvm::SmallString<1028> AggregateString;
442 {
443 llvm::raw_svector_ostream Out(AggregateString);
444 for (unsigned i = 0; i < 16; ++i)
445 if (reportStrings[i]) Out << reportStrings[i] << '\n';
446 }
447 __crashreporter_info__ = AggregateString.c_str();
448#endif
449
Daniel Dunbar94220972009-12-05 02:17:18 +0000450 llvm::OwningPtr<ASTUnit> Unit(
451 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000452 CXXIdx->getDiags(),
453 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000454 CXXIdx->getOnlyLocalDecls(),
455 /* UseBumpAllocator = */ true));
456
457 // FIXME: Until we have broader testing, just drop the entire AST if we
458 // encountered an error.
459 if (NumErrors != CXXIdx->getDiags().getNumErrors())
460 return 0;
461
462 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000463 }
464
Ted Kremenek139ba862009-10-22 00:03:57 +0000465 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000466 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000467
Ted Kremenek139ba862009-10-22 00:03:57 +0000468 // First add the complete path to the 'clang' executable.
469 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000470 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000471
Ted Kremenek139ba862009-10-22 00:03:57 +0000472 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000473 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000474
Ted Kremenek139ba862009-10-22 00:03:57 +0000475 // The 'source_filename' argument is optional. If the caller does not
476 // specify it then it is assumed that the source file is specified
477 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000478 if (source_filename)
479 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000480
Steve Naroff37b5ac22009-10-15 20:50:09 +0000481 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000482 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000483 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000484 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000485
486 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
487 for (int i = 0; i < num_command_line_args; ++i)
488 if (const char *arg = command_line_args[i]) {
489 if (strcmp(arg, "-o") == 0) {
490 ++i; // Also skip the matching argument.
491 continue;
492 }
493 if (strcmp(arg, "-emit-ast") == 0 ||
494 strcmp(arg, "-c") == 0 ||
495 strcmp(arg, "-fsyntax-only") == 0) {
496 continue;
497 }
498
499 // Keep the argument.
500 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000501 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000502
Ted Kremenek139ba862009-10-22 00:03:57 +0000503 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000504 argv.push_back(NULL);
505
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000506 // Invoke 'clang'.
507 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
508 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000509 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000510 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000511 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
512 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
513 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000514
Ted Kremenek0854d702009-11-10 19:18:52 +0000515 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000516 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000517 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000518 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000519 I!=E; ++I) {
520 if (*I)
521 llvm::errs() << ' ' << *I << '\n';
522 }
523 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000524 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000525
Steve Naroff37b5ac22009-10-15 20:50:09 +0000526 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000527 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000528 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000529 if (ATU)
530 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000531 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000532}
533
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000534void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000535 assert(CTUnit && "Passed null CXTranslationUnit");
536 delete static_cast<ASTUnit *>(CTUnit);
537}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000538
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000539CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000540 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000541 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000542 return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000543}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000544
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000545void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000546 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000547 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000548 assert(CTUnit && "Passed null CXTranslationUnit");
549 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
550 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000551
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000552 unsigned PCHLevel = Decl::MaxPCHLevel;
553
554 // Set the PCHLevel to filter out unwanted decls if requested.
555 if (CXXUnit->getOnlyLocalDecls()) {
556 PCHLevel = 0;
557
558 // If the main input was an AST, bump the level.
559 if (CXXUnit->isMainFileAST())
560 ++PCHLevel;
561 }
562
563 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000564
565 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
566 // decls.
567 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
568 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
569 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
570 ie = TLDs.end(); it != ie; ++it) {
571 DVisit.Visit(*it);
572 }
573 } else
574 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000575}
576
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000577void clang_loadDeclaration(CXDecl Dcl,
578 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000579 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000580 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000581
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000582 CDeclVisitor DVisit(Dcl, callback, CData,
583 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000584 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000585}
586
Steve Naroff600866c2009-08-27 19:51:58 +0000587//
588// CXDecl Operations.
589//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000590
591CXEntity clang_getEntityFromDecl(CXDecl) {
Steve Naroff600866c2009-08-27 19:51:58 +0000592 return 0;
593}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000594
595CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000596 assert(AnonDecl && "Passed null CXDecl");
597 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000598
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000599 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
600 return createCXString(OMD->getSelector().getAsString().c_str(), true);
601
602 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000603 // No, this isn't the same as the code below. getIdentifier() is non-virtual
604 // and returns different names. NamedDecl returns the class name and
605 // ObjCCategoryImplDecl returns the category name.
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000606 return createCXString(CIMP->getIdentifier()->getNameStart());
607
608 if (ND->getIdentifier())
609 return createCXString(ND->getIdentifier()->getNameStart());
610
611 return createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000612}
Steve Narofff334b4e2009-09-02 18:26:48 +0000613
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000614unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000615 assert(AnonDecl && "Passed null CXDecl");
616 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
617 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
618 return SourceMgr.getSpellingLineNumber(ND->getLocation());
619}
620
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000621unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000622 assert(AnonDecl && "Passed null CXDecl");
623 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
624 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000625 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000626}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000627
628CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
629 assert(AnonDecl && "Passed null CXDecl");
630 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekd8210652010-01-06 23:43:31 +0000631 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000632 SourceRange R = ND->getSourceRange();
633
Ted Kremenekd8210652010-01-06 23:43:31 +0000634 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
635 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
636
637 if (!Begin.isValid()) {
638 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
639 return extent;
640 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000641
Ted Kremenekd8210652010-01-06 23:43:31 +0000642 // FIXME: This is largely copy-paste from
643 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
644 // what we want the two routines should be refactored.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000645
Ted Kremenekd8210652010-01-06 23:43:31 +0000646 // If the End location and the start location are the same and are a macro
647 // location, then the range was something that came from a macro expansion
648 // or _Pragma. If this is an object-like macro, the best we can do is to
649 // get the range. If this is a function-like macro, we'd also like to
650 // get the arguments.
651 if (Begin == End && R.getEnd().isMacroID())
652 End = SM.getInstantiationRange(R.getEnd()).second;
653
654 assert(SM.getFileID(Begin) == SM.getFileID(End));
655 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
656 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000657
Ted Kremenekd8210652010-01-06 23:43:31 +0000658 // Compute the column number of the start. Keep the column based at 1.
659 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
660
661 // Compute the column number of the end.
662 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
663 if (EndColNo) {
664 // Offset the end column by 1 so that we point to the last character
665 // in the last token.
666 --EndColNo;
667
668 // Add in the length of the token, so that we cover multi-char tokens.
669 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
670 const LangOptions &LOpts = Ctx.getLangOptions();
671
672 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
673 }
674
675 // Package up the line/column data and return to the caller.
676 CXDeclExtent extent = { { StartLineNo, StartColNo },
677 { EndLineNo, EndColNo } };
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000678 return extent;
679}
Steve Naroff699a07d2009-09-25 21:32:34 +0000680
Steve Naroff88145032009-10-27 14:35:18 +0000681static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000682 SourceLocation SLoc) {
Steve Naroff88145032009-10-27 14:35:18 +0000683 FileID FID;
684 if (SLoc.isFileID())
685 FID = SMgr.getFileID(SLoc);
686 else
687 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
688 return SMgr.getFileEntryForID(FID);
689}
690
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000691CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000692 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000693 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
694 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000695 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
696}
697
698const char *clang_getFileName(CXFile SFile) {
699 assert(SFile && "Passed null CXFile");
700 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
701 return FEnt->getName();
702}
703
704time_t clang_getFileTime(CXFile SFile) {
705 assert(SFile && "Passed null CXFile");
706 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
707 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000708}
709
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000710CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000711 assert(C.decl && "CXCursor has null decl");
712 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000713
Steve Narofff334b4e2009-09-02 18:26:48 +0000714 if (clang_isReference(C.kind)) {
715 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000716 case CXCursor_ObjCSuperClassRef: {
717 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
718 assert(OID && "clang_getCursorLine(): Missing interface decl");
719 return createCXString(OID->getSuperClass()->getIdentifier()
720 ->getNameStart());
721 }
722 case CXCursor_ObjCClassRef: {
723 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000724 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000725
726 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
727 assert(OCD && "clang_getCursorLine(): Missing category decl");
728 return createCXString(OCD->getClassInterface()->getIdentifier()
729 ->getNameStart());
730 }
731 case CXCursor_ObjCProtocolRef: {
732 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
733 assert(OID && "clang_getCursorLine(): Missing protocol decl");
734 return createCXString(OID->getIdentifier()->getNameStart());
735 }
736 case CXCursor_ObjCSelectorRef: {
737 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
738 static_cast<Stmt *>(C.stmt));
739 assert(OME && "clang_getCursorLine(): Missing message expr");
740 return createCXString(OME->getSelector().getAsString().c_str(), true);
741 }
742 case CXCursor_VarRef:
743 case CXCursor_FunctionRef:
744 case CXCursor_EnumConstantRef: {
745 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
746 static_cast<Stmt *>(C.stmt));
747 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
748 return createCXString(DRE->getDecl()->getIdentifier()->getNameStart());
749 }
750 default:
751 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000752 }
753 }
754 return clang_getDeclSpelling(C.decl);
755}
756
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000757const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000758 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000759 case CXCursor_FunctionDecl: return "FunctionDecl";
760 case CXCursor_TypedefDecl: return "TypedefDecl";
761 case CXCursor_EnumDecl: return "EnumDecl";
762 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
763 case CXCursor_StructDecl: return "StructDecl";
764 case CXCursor_UnionDecl: return "UnionDecl";
765 case CXCursor_ClassDecl: return "ClassDecl";
766 case CXCursor_FieldDecl: return "FieldDecl";
767 case CXCursor_VarDecl: return "VarDecl";
768 case CXCursor_ParmDecl: return "ParmDecl";
769 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
770 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
771 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
772 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
773 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
774 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
775 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
776 case CXCursor_FunctionDefn: return "FunctionDefn";
777 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
778 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
779 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
780 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
781 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
782 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
783 case CXCursor_ObjCClassRef: return "ObjCClassRef";
784 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000785
Daniel Dunbaracca7252009-11-30 20:42:49 +0000786 case CXCursor_VarRef: return "VarRef";
787 case CXCursor_FunctionRef: return "FunctionRef";
788 case CXCursor_EnumConstantRef: return "EnumConstantRef";
789 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000790
Daniel Dunbaracca7252009-11-30 20:42:49 +0000791 case CXCursor_InvalidFile: return "InvalidFile";
792 case CXCursor_NoDeclFound: return "NoDeclFound";
793 case CXCursor_NotImplemented: return "NotImplemented";
794 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000795 }
Steve Naroff600866c2009-08-27 19:51:58 +0000796}
Steve Naroff89922f82009-08-31 00:59:03 +0000797
Steve Naroff9efa7672009-09-04 15:44:05 +0000798static enum CXCursorKind TranslateKind(Decl *D) {
799 switch (D->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000800 case Decl::Function: return CXCursor_FunctionDecl;
801 case Decl::Typedef: return CXCursor_TypedefDecl;
802 case Decl::Enum: return CXCursor_EnumDecl;
803 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
804 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
805 case Decl::Field: return CXCursor_FieldDecl;
806 case Decl::Var: return CXCursor_VarDecl;
807 case Decl::ParmVar: return CXCursor_ParmDecl;
808 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
809 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
810 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
811 case Decl::ObjCMethod: {
812 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
813 if (MD->isInstanceMethod())
814 return CXCursor_ObjCInstanceMethodDecl;
815 return CXCursor_ObjCClassMethodDecl;
816 }
817 default: break;
Steve Naroff9efa7672009-09-04 15:44:05 +0000818 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000819 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000820}
Steve Naroff600866c2009-08-27 19:51:58 +0000821//
822// CXCursor Operations.
823//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000824
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000825CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000826 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000827 assert(CTUnit && "Passed null CXTranslationUnit");
828 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000829
Steve Naroff9efa7672009-09-04 15:44:05 +0000830 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000831 const FileEntry *File = FMgr.getFile(source_name,
832 source_name+strlen(source_name));
Steve Naroff77128dd2009-09-15 20:25:34 +0000833 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000834 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000835 return C;
836 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000837 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000838 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000839
Steve Narofff96b5242009-10-28 20:44:47 +0000840 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
841
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000842 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000843 &LastLoc);
844 if (ALoc.isValid())
845 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000846
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000847 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000848 if (ALoc.isNamedRef())
849 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000850 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000851 if (Dcl) {
852 if (Stm) {
853 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
854 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
855 return C;
856 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
857 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
858 return C;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000859 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000860 // Fall through...treat as a decl, not a ref.
861 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000862 if (ALoc.isNamedRef()) {
863 if (isa<ObjCInterfaceDecl>(Dcl)) {
864 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
865 return C;
866 }
867 if (isa<ObjCProtocolDecl>(Dcl)) {
868 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
869 return C;
870 }
871 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000872 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000873 return C;
874 }
Steve Narofffb570422009-09-22 19:25:29 +0000875 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000876 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000877}
878
Ted Kremenek73885552009-11-17 19:28:59 +0000879CXCursor clang_getNullCursor(void) {
880 CXCursor C;
881 C.kind = CXCursor_InvalidFile;
882 C.decl = NULL;
883 C.stmt = NULL;
884 return C;
885}
886
887unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
888 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
889}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000890
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000891CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000892 assert(AnonDecl && "Passed null CXDecl");
893 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000894
Steve Narofffb570422009-09-22 19:25:29 +0000895 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000896 return C;
897}
898
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000899unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000900 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
901}
902
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000903unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000904 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
905}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000906
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000907unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000908 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
909}
910
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000911unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000912 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
913}
914
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000915CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000916 return C.kind;
917}
918
Steve Naroff699a07d2009-09-25 21:32:34 +0000919static Decl *getDeclFromExpr(Stmt *E) {
920 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
921 return RefExpr->getDecl();
922 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
923 return ME->getMemberDecl();
924 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
925 return RE->getDecl();
926
927 if (CallExpr *CE = dyn_cast<CallExpr>(E))
928 return getDeclFromExpr(CE->getCallee());
929 if (CastExpr *CE = dyn_cast<CastExpr>(E))
930 return getDeclFromExpr(CE->getSubExpr());
931 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
932 return OME->getMethodDecl();
933
934 return 0;
935}
936
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000937CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000938 if (clang_isDeclaration(C.kind))
939 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000940
Steve Naroff699a07d2009-09-25 21:32:34 +0000941 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000942 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000943 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +0000944 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000945 return static_cast<Stmt *>(C.stmt);
946 else
947 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
948 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000949 return C.decl;
950 }
951 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000952}
953
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000954unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000955 assert(C.decl && "CXCursor has null decl");
956 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000957 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000958
Steve Narofff334b4e2009-09-02 18:26:48 +0000959 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000960 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000961}
Steve Narofff334b4e2009-09-02 18:26:48 +0000962
Steve Naroffef0cef62009-11-09 17:45:52 +0000963const char *clang_getCString(CXString string) {
964 return string.Spelling;
965}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000966
Steve Naroffef0cef62009-11-09 17:45:52 +0000967void clang_disposeString(CXString string) {
Benjamin Kramer858e5de2009-11-09 18:24:53 +0000968 if (string.MustFreeString)
969 free((void*)string.Spelling);
Steve Naroffef0cef62009-11-09 17:45:52 +0000970}
971
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000972unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000973 assert(C.decl && "CXCursor has null decl");
974 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000975 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000976
Steve Narofff334b4e2009-09-02 18:26:48 +0000977 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000978 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000979}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000980
981const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000982 assert(C.decl && "CXCursor has null decl");
983 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000984 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000985
Steve Narofff334b4e2009-09-02 18:26:48 +0000986 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000987
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000988 if (SLoc.isFileID()) {
989 const char *bufferName = SourceMgr.getBufferName(SLoc);
990 return bufferName[0] == '<' ? NULL : bufferName;
991 }
Douglas Gregor02465752009-10-16 21:24:31 +0000992
993 // Retrieve the file in which the macro was instantiated, then provide that
994 // buffer name.
995 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000996 const llvm::MemoryBuffer *Buffer
Douglas Gregor02465752009-10-16 21:24:31 +0000997 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
998 if (!Buffer)
999 return 0;
1000
1001 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +00001002}
1003
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001004CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +00001005 assert(C.decl && "CXCursor has null decl");
1006 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1007 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001008
Steve Naroff88145032009-10-27 14:35:18 +00001009 return (void *)getFileEntryFromSourceLocation(SourceMgr,
Daniel Dunbaracca7252009-11-30 20:42:49 +00001010 getLocationFromCursor(C,SourceMgr, ND));
Steve Naroff88145032009-10-27 14:35:18 +00001011}
1012
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001013void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001014 const char **startBuf,
1015 const char **endBuf,
1016 unsigned *startLine,
1017 unsigned *startColumn,
1018 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001019 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +00001020 assert(C.decl && "CXCursor has null decl");
1021 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1022 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1023 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001024
Steve Naroff4ade6d62009-09-23 17:52:52 +00001025 SourceManager &SM = FD->getASTContext().getSourceManager();
1026 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1027 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1028 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1029 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1030 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1031 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1032}
1033
Steve Naroff600866c2009-08-27 19:51:58 +00001034} // end extern "C"