blob: 28a86e72bec8a5e5e6d9736f11ef26d708f87998 [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__
Ted Kremenek29b72842010-01-07 22:49:05 +000034#ifndef NDEBUG
35#define USE_CRASHTRACER
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000036#include "clang/Analysis/Support/SaveAndRestore.h"
37// Integrate with crash reporter.
38extern "C" const char *__crashreporter_info__;
Ted Kremenek29b72842010-01-07 22:49:05 +000039#define NUM_CRASH_STRINGS 16
40static unsigned crashtracer_counter = 0;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000041static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek29b72842010-01-07 22:49:05 +000042static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
43static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
44
45static unsigned SetCrashTracerInfo(const char *str,
46 llvm::SmallString<1024> &AggStr) {
47
Ted Kremenek254ba7c2010-01-07 23:13:53 +000048 unsigned slot = 0;
Ted Kremenek29b72842010-01-07 22:49:05 +000049 while (crashtracer_strings[slot]) {
50 if (++slot == NUM_CRASH_STRINGS)
51 slot = 0;
52 }
53 crashtracer_strings[slot] = str;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000054 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek29b72842010-01-07 22:49:05 +000055
56 // We need to create an aggregate string because multiple threads
57 // may be in this method at one time. The crash reporter string
58 // will attempt to overapproximate the set of in-flight invocations
59 // of this function. Race conditions can still cause this goal
60 // to not be achieved.
61 {
62 llvm::raw_svector_ostream Out(AggStr);
63 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
64 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
65 }
66 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
67 return slot;
68}
69
70static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek254ba7c2010-01-07 23:13:53 +000071 unsigned max_slot = 0;
72 unsigned max_value = 0;
73
74 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
75
76 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
77 if (agg_crashtracer_strings[i] &&
78 crashtracer_counter_id[i] > max_value) {
79 max_slot = i;
80 max_value = crashtracer_counter_id[i];
Ted Kremenek29b72842010-01-07 22:49:05 +000081 }
Ted Kremenek254ba7c2010-01-07 23:13:53 +000082
83 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek29b72842010-01-07 22:49:05 +000084}
85
86namespace {
87class ArgsCrashTracerInfo {
88 llvm::SmallString<1024> CrashString;
89 llvm::SmallString<1024> AggregateString;
90 unsigned crashtracerSlot;
91public:
92 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
93 : crashtracerSlot(0)
94 {
95 {
96 llvm::raw_svector_ostream Out(CrashString);
97 Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
98 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
99 E=Args.end(); I!=E; ++I)
100 Out << ' ' << *I;
101 }
102 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
103 AggregateString);
104 }
105
106 ~ArgsCrashTracerInfo() {
107 ResetCrashTracerInfo(crashtracerSlot);
108 }
109};
110}
111#endif
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000112#endif
113
114//===----------------------------------------------------------------------===//
115// Visitors.
116//===----------------------------------------------------------------------===//
117
Steve Naroff89922f82009-08-31 00:59:03 +0000118namespace {
Ted Kremenekab188932010-01-05 19:32:54 +0000119static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000120 NamedDecl *D = DRE->getDecl();
121 if (isa<VarDecl>(D))
122 return CXCursor_VarRef;
123 else if (isa<FunctionDecl>(D))
124 return CXCursor_FunctionRef;
125 else if (isa<EnumConstantDecl>(D))
126 return CXCursor_EnumConstantRef;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000127 else
Steve Naroff4ade6d62009-09-23 17:52:52 +0000128 return CXCursor_NotImplemented;
129}
130
131#if 0
132// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +0000133class CRefVisitor : public StmtVisitor<CRefVisitor> {
134 CXDecl CDecl;
135 CXDeclIterator Callback;
136 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000137
Steve Narofffb570422009-09-22 19:25:29 +0000138 void Call(enum CXCursorKind CK, Stmt *SRef) {
139 CXCursor C = { CK, CDecl, SRef };
140 Callback(CDecl, C, CData);
141 }
142
143public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000144 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Narofffb570422009-09-22 19:25:29 +0000145 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000146
Steve Narofffb570422009-09-22 19:25:29 +0000147 void VisitStmt(Stmt *S) {
148 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
149 C != CEnd; ++C)
150 Visit(*C);
151 }
152 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000153 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +0000154 }
155 void VisitMemberExpr(MemberExpr *Node) {
156 Call(CXCursor_MemberRef, Node);
157 }
158 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
159 Call(CXCursor_ObjCSelectorRef, Node);
160 }
161 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
162 Call(CXCursor_ObjCIvarRef, Node);
163 }
164};
Steve Naroff4ade6d62009-09-23 17:52:52 +0000165#endif
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000166
Steve Naroff89922f82009-08-31 00:59:03 +0000167// Translation Unit Visitor.
168class TUVisitor : public DeclVisitor<TUVisitor> {
169 CXTranslationUnit TUnit;
170 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000171 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000172
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000173 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
174 // to the visitor. Declarations with a PCH level greater than this value will
175 // be suppressed.
176 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000177
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000178 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000179 // Filter any declarations that have a PCH level greater than what we allow.
180 if (ND->getPCHLevel() > MaxPCHLevel)
181 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000182
Steve Narofff96b5242009-10-28 20:44:47 +0000183 // Filter any implicit declarations (since the source info will be bogus).
184 if (ND->isImplicit())
185 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000186
Steve Narofffb570422009-09-22 19:25:29 +0000187 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000188 Callback(TUnit, C, CData);
189 }
Steve Naroff89922f82009-08-31 00:59:03 +0000190public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000191 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000192 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000193 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000194 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000195
Steve Naroff89922f82009-08-31 00:59:03 +0000196 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
197 VisitDeclContext(dyn_cast<DeclContext>(D));
198 }
199 void VisitDeclContext(DeclContext *DC) {
200 for (DeclContext::decl_iterator
201 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
202 Visit(*I);
203 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000204
205 void VisitFunctionDecl(FunctionDecl *ND) {
206 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
207 : CXCursor_FunctionDecl, ND);
208 }
209 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
210 Call(CXCursor_ObjCCategoryDecl, ND);
211 }
212 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
213 Call(CXCursor_ObjCCategoryDefn, ND);
214 }
215 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
216 Call(CXCursor_ObjCClassDefn, ND);
217 }
218 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
219 Call(CXCursor_ObjCInterfaceDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000220 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000221 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
222 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000223 }
224 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000225 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000226 case TagDecl::TK_struct:
227 Call(CXCursor_StructDecl, ND);
228 break;
229 case TagDecl::TK_class:
230 Call(CXCursor_ClassDecl, ND);
231 break;
232 case TagDecl::TK_union:
233 Call(CXCursor_UnionDecl, ND);
234 break;
235 case TagDecl::TK_enum:
236 Call(CXCursor_EnumDecl, ND);
237 break;
Steve Naroffc857ea42009-09-02 13:28:54 +0000238 }
Steve Naroff89922f82009-08-31 00:59:03 +0000239 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000240 void VisitTypedefDecl(TypedefDecl *ND) {
241 Call(CXCursor_TypedefDecl, ND);
242 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000243 void VisitVarDecl(VarDecl *ND) {
244 Call(CXCursor_VarDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000245 }
Steve Naroff89922f82009-08-31 00:59:03 +0000246};
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000247
Steve Naroff89922f82009-08-31 00:59:03 +0000248
Steve Naroffc857ea42009-09-02 13:28:54 +0000249// Declaration visitor.
250class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
251 CXDecl CDecl;
252 CXDeclIterator Callback;
253 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000254
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000255 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
256 // to the visitor. Declarations with a PCH level greater than this value will
257 // be suppressed.
258 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000259
Steve Naroffc857ea42009-09-02 13:28:54 +0000260 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000261 // Disable the callback when the context is equal to the visiting decl.
262 if (CDecl == ND && !clang_isReference(CK))
263 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000264
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000265 // Filter any declarations that have a PCH level greater than what we allow.
266 if (ND->getPCHLevel() > MaxPCHLevel)
267 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000268
Steve Narofffb570422009-09-22 19:25:29 +0000269 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000270 Callback(CDecl, C, CData);
271 }
Steve Naroff89922f82009-08-31 00:59:03 +0000272public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000273 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
274 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000275 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000276
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000277 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
278 // Issue callbacks for the containing class.
279 Call(CXCursor_ObjCClassRef, ND);
280 // FIXME: Issue callbacks for protocol refs.
281 VisitDeclContext(dyn_cast<DeclContext>(ND));
282 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000283 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000284 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000285 if (D->getSuperClass())
286 Call(CXCursor_ObjCSuperClassRef, D);
287
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000288 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000289 E = D->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000290 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000291 VisitDeclContext(dyn_cast<DeclContext>(D));
292 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000293 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000294 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000295 E = PID->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000296 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000297
Steve Naroff9efa7672009-09-04 15:44:05 +0000298 VisitDeclContext(dyn_cast<DeclContext>(PID));
299 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000300 void VisitTagDecl(TagDecl *D) {
301 VisitDeclContext(dyn_cast<DeclContext>(D));
302 }
303 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
304 VisitDeclContext(dyn_cast<DeclContext>(D));
305 }
306 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
307 VisitDeclContext(dyn_cast<DeclContext>(D));
308 }
Steve Naroff89922f82009-08-31 00:59:03 +0000309 void VisitDeclContext(DeclContext *DC) {
310 for (DeclContext::decl_iterator
311 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
312 Visit(*I);
313 }
314 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000315 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000316 }
317 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000318 Call(CXCursor_FieldDecl, ND);
319 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000320 void VisitVarDecl(VarDecl *ND) {
321 Call(CXCursor_VarDecl, ND);
322 }
323 void VisitParmVarDecl(ParmVarDecl *ND) {
324 Call(CXCursor_ParmDecl, ND);
325 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000326 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
327 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000328 }
329 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000330 Call(CXCursor_ObjCIvarDecl, ND);
331 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000332 void VisitFunctionDecl(FunctionDecl *ND) {
333 if (ND->isThisDeclarationADefinition()) {
334 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000335#if 0
336 // Not currently needed.
337 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000338 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000339 RVisit.Visit(Body);
340#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000341 }
342 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000343 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
344 if (ND->getBody()) {
345 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
346 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000347 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000348 } else
349 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
350 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000351 }
352};
Ted Kremenekab188932010-01-05 19:32:54 +0000353} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000354
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000355static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000356 SourceManager &SourceMgr,
357 NamedDecl *ND) {
358 if (clang_isReference(C.kind)) {
359 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000360 case CXCursor_ObjCClassRef: {
361 if (isa<ObjCInterfaceDecl>(ND)) {
362 // FIXME: This is a hack (storing the parent decl in the stmt slot).
363 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
364 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000365 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000366 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
367 assert(OID && "clang_getCursorLine(): Missing category decl");
368 return OID->getClassInterface()->getLocation();
369 }
370 case CXCursor_ObjCSuperClassRef: {
371 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
372 assert(OID && "clang_getCursorLine(): Missing interface decl");
373 return OID->getSuperClassLoc();
374 }
375 case CXCursor_ObjCProtocolRef: {
376 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
377 assert(OID && "clang_getCursorLine(): Missing protocol decl");
378 return OID->getLocation();
379 }
380 case CXCursor_ObjCSelectorRef: {
381 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
382 static_cast<Stmt *>(C.stmt));
383 assert(OME && "clang_getCursorLine(): Missing message expr");
384 return OME->getLeftLoc(); /* FIXME: should be a range */
385 }
386 case CXCursor_VarRef:
387 case CXCursor_FunctionRef:
388 case CXCursor_EnumConstantRef: {
389 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
390 static_cast<Stmt *>(C.stmt));
391 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
392 return DRE->getLocation();
393 }
394 default:
395 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000396 }
397 } else { // We have a declaration or a definition.
398 SourceLocation SLoc;
399 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000400 case Decl::ObjCInterface: {
401 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
402 break;
403 }
404 case Decl::ObjCProtocol: {
405 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
406 break;
407 }
408 default: {
409 SLoc = ND->getLocation();
410 break;
411 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000412 }
413 if (SLoc.isInvalid())
414 return SourceLocation();
415 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
416 }
417}
418
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000419static CXString createCXString(const char *String, bool DupString = false) {
420 CXString Str;
421 if (DupString) {
422 Str.Spelling = strdup(String);
423 Str.MustFreeString = 1;
424 } else {
425 Str.Spelling = String;
426 Str.MustFreeString = 0;
427 }
428 return Str;
429}
430
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000431extern "C" {
432
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000433CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000434 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000435 CIndexer *CIdxr = new CIndexer(new Program());
436 if (excludeDeclarationsFromPCH)
437 CIdxr->setOnlyLocalDecls();
438 if (displayDiagnostics)
439 CIdxr->setDisplayDiagnostics();
440 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000441}
442
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000443void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000444 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000445 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000446}
447
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000448void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
449 assert(CIdx && "Passed null CXIndex");
450 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
451 CXXIdx->setUseExternalASTGeneration(value);
452}
453
Steve Naroff50398192009-08-28 15:28:48 +0000454// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000455CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
456 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000457 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000458 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000459
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000460 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
461 CXXIdx->getOnlyLocalDecls(),
462 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000463}
464
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000465CXTranslationUnit
466clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
467 const char *source_filename,
468 int num_command_line_args,
469 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000470 assert(CIdx && "Passed null CXIndex");
471 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
472
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000473 if (!CXXIdx->getUseExternalASTGeneration()) {
474 llvm::SmallVector<const char *, 16> Args;
475
476 // The 'source_filename' argument is optional. If the caller does not
477 // specify it then it is assumed that the source file is specified
478 // in the actual argument list.
479 if (source_filename)
480 Args.push_back(source_filename);
481 Args.insert(Args.end(), command_line_args,
482 command_line_args + num_command_line_args);
483
Daniel Dunbar94220972009-12-05 02:17:18 +0000484 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000485
Ted Kremenek29b72842010-01-07 22:49:05 +0000486#ifdef USE_CRASHTRACER
487 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000488#endif
489
Daniel Dunbar94220972009-12-05 02:17:18 +0000490 llvm::OwningPtr<ASTUnit> Unit(
491 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000492 CXXIdx->getDiags(),
493 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000494 CXXIdx->getOnlyLocalDecls(),
495 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000496
Daniel Dunbar94220972009-12-05 02:17:18 +0000497 // FIXME: Until we have broader testing, just drop the entire AST if we
498 // encountered an error.
499 if (NumErrors != CXXIdx->getDiags().getNumErrors())
500 return 0;
501
502 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000503 }
504
Ted Kremenek139ba862009-10-22 00:03:57 +0000505 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000506 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000507
Ted Kremenek139ba862009-10-22 00:03:57 +0000508 // First add the complete path to the 'clang' executable.
509 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000510 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000511
Ted Kremenek139ba862009-10-22 00:03:57 +0000512 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000513 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000514
Ted Kremenek139ba862009-10-22 00:03:57 +0000515 // The 'source_filename' argument is optional. If the caller does not
516 // specify it then it is assumed that the source file is specified
517 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000518 if (source_filename)
519 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000520
Steve Naroff37b5ac22009-10-15 20:50:09 +0000521 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000522 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000523 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000524 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000525
526 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
527 for (int i = 0; i < num_command_line_args; ++i)
528 if (const char *arg = command_line_args[i]) {
529 if (strcmp(arg, "-o") == 0) {
530 ++i; // Also skip the matching argument.
531 continue;
532 }
533 if (strcmp(arg, "-emit-ast") == 0 ||
534 strcmp(arg, "-c") == 0 ||
535 strcmp(arg, "-fsyntax-only") == 0) {
536 continue;
537 }
538
539 // Keep the argument.
540 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000541 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000542
Ted Kremenek139ba862009-10-22 00:03:57 +0000543 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000544 argv.push_back(NULL);
545
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000546 // Invoke 'clang'.
547 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
548 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000549 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000550 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000551 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
552 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
553 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000554
Ted Kremenek0854d702009-11-10 19:18:52 +0000555 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000556 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000557 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000558 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000559 I!=E; ++I) {
560 if (*I)
561 llvm::errs() << ' ' << *I << '\n';
562 }
563 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000564 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000565
Steve Naroff37b5ac22009-10-15 20:50:09 +0000566 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000567 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000568 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000569 if (ATU)
570 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000571 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000572}
573
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000574void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000575 assert(CTUnit && "Passed null CXTranslationUnit");
576 delete static_cast<ASTUnit *>(CTUnit);
577}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000578
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000579CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000580 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000581 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000582 return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000583}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000584
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000585void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000586 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000587 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000588 assert(CTUnit && "Passed null CXTranslationUnit");
589 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
590 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000591
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000592 unsigned PCHLevel = Decl::MaxPCHLevel;
593
594 // Set the PCHLevel to filter out unwanted decls if requested.
595 if (CXXUnit->getOnlyLocalDecls()) {
596 PCHLevel = 0;
597
598 // If the main input was an AST, bump the level.
599 if (CXXUnit->isMainFileAST())
600 ++PCHLevel;
601 }
602
603 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000604
605 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
606 // decls.
607 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
608 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
609 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
610 ie = TLDs.end(); it != ie; ++it) {
611 DVisit.Visit(*it);
612 }
613 } else
614 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000615}
616
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000617void clang_loadDeclaration(CXDecl Dcl,
618 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000619 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000620 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000621
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000622 CDeclVisitor DVisit(Dcl, callback, CData,
623 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000624 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000625}
626
Steve Naroff600866c2009-08-27 19:51:58 +0000627//
628// CXDecl Operations.
629//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000630
631CXEntity clang_getEntityFromDecl(CXDecl) {
Steve Naroff600866c2009-08-27 19:51:58 +0000632 return 0;
633}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000634
635CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000636 assert(AnonDecl && "Passed null CXDecl");
637 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000638
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000639 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
640 return createCXString(OMD->getSelector().getAsString().c_str(), true);
641
642 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000643 // No, this isn't the same as the code below. getIdentifier() is non-virtual
644 // and returns different names. NamedDecl returns the class name and
645 // ObjCCategoryImplDecl returns the category name.
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000646 return createCXString(CIMP->getIdentifier()->getNameStart());
647
648 if (ND->getIdentifier())
649 return createCXString(ND->getIdentifier()->getNameStart());
650
651 return createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000652}
Steve Narofff334b4e2009-09-02 18:26:48 +0000653
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000654unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000655 assert(AnonDecl && "Passed null CXDecl");
656 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
657 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
658 return SourceMgr.getSpellingLineNumber(ND->getLocation());
659}
660
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000661unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000662 assert(AnonDecl && "Passed null CXDecl");
663 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
664 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000665 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000666}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000667
668CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
669 assert(AnonDecl && "Passed null CXDecl");
670 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekd8210652010-01-06 23:43:31 +0000671 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000672 SourceRange R = ND->getSourceRange();
673
Ted Kremenekd8210652010-01-06 23:43:31 +0000674 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
675 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
676
677 if (!Begin.isValid()) {
678 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
679 return extent;
680 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000681
Ted Kremenekd8210652010-01-06 23:43:31 +0000682 // FIXME: This is largely copy-paste from
683 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
684 // what we want the two routines should be refactored.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000685
Ted Kremenekd8210652010-01-06 23:43:31 +0000686 // If the End location and the start location are the same and are a macro
687 // location, then the range was something that came from a macro expansion
688 // or _Pragma. If this is an object-like macro, the best we can do is to
689 // get the range. If this is a function-like macro, we'd also like to
690 // get the arguments.
691 if (Begin == End && R.getEnd().isMacroID())
692 End = SM.getInstantiationRange(R.getEnd()).second;
693
694 assert(SM.getFileID(Begin) == SM.getFileID(End));
695 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
696 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000697
Ted Kremenekd8210652010-01-06 23:43:31 +0000698 // Compute the column number of the start. Keep the column based at 1.
699 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
700
701 // Compute the column number of the end.
702 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
703 if (EndColNo) {
704 // Offset the end column by 1 so that we point to the last character
705 // in the last token.
706 --EndColNo;
707
708 // Add in the length of the token, so that we cover multi-char tokens.
709 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
710 const LangOptions &LOpts = Ctx.getLangOptions();
711
712 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
713 }
714
715 // Package up the line/column data and return to the caller.
716 CXDeclExtent extent = { { StartLineNo, StartColNo },
717 { EndLineNo, EndColNo } };
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000718 return extent;
719}
Steve Naroff699a07d2009-09-25 21:32:34 +0000720
Steve Naroff88145032009-10-27 14:35:18 +0000721static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000722 SourceLocation SLoc) {
Steve Naroff88145032009-10-27 14:35:18 +0000723 FileID FID;
724 if (SLoc.isFileID())
725 FID = SMgr.getFileID(SLoc);
726 else
727 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
728 return SMgr.getFileEntryForID(FID);
729}
730
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000731CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000732 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000733 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
734 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000735 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
736}
737
738const char *clang_getFileName(CXFile SFile) {
739 assert(SFile && "Passed null CXFile");
740 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
741 return FEnt->getName();
742}
743
744time_t clang_getFileTime(CXFile SFile) {
745 assert(SFile && "Passed null CXFile");
746 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
747 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000748}
749
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000750CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000751 assert(C.decl && "CXCursor has null decl");
752 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000753
Steve Narofff334b4e2009-09-02 18:26:48 +0000754 if (clang_isReference(C.kind)) {
755 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000756 case CXCursor_ObjCSuperClassRef: {
757 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
758 assert(OID && "clang_getCursorLine(): Missing interface decl");
759 return createCXString(OID->getSuperClass()->getIdentifier()
760 ->getNameStart());
761 }
762 case CXCursor_ObjCClassRef: {
763 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000764 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000765
766 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
767 assert(OCD && "clang_getCursorLine(): Missing category decl");
768 return createCXString(OCD->getClassInterface()->getIdentifier()
769 ->getNameStart());
770 }
771 case CXCursor_ObjCProtocolRef: {
772 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
773 assert(OID && "clang_getCursorLine(): Missing protocol decl");
774 return createCXString(OID->getIdentifier()->getNameStart());
775 }
776 case CXCursor_ObjCSelectorRef: {
777 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
778 static_cast<Stmt *>(C.stmt));
779 assert(OME && "clang_getCursorLine(): Missing message expr");
780 return createCXString(OME->getSelector().getAsString().c_str(), true);
781 }
782 case CXCursor_VarRef:
783 case CXCursor_FunctionRef:
784 case CXCursor_EnumConstantRef: {
785 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
786 static_cast<Stmt *>(C.stmt));
787 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
788 return createCXString(DRE->getDecl()->getIdentifier()->getNameStart());
789 }
790 default:
791 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000792 }
793 }
794 return clang_getDeclSpelling(C.decl);
795}
796
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000797const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000798 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000799 case CXCursor_FunctionDecl: return "FunctionDecl";
800 case CXCursor_TypedefDecl: return "TypedefDecl";
801 case CXCursor_EnumDecl: return "EnumDecl";
802 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
803 case CXCursor_StructDecl: return "StructDecl";
804 case CXCursor_UnionDecl: return "UnionDecl";
805 case CXCursor_ClassDecl: return "ClassDecl";
806 case CXCursor_FieldDecl: return "FieldDecl";
807 case CXCursor_VarDecl: return "VarDecl";
808 case CXCursor_ParmDecl: return "ParmDecl";
809 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
810 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
811 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
812 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
813 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
814 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
815 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
816 case CXCursor_FunctionDefn: return "FunctionDefn";
817 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
818 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
819 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
820 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
821 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
822 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
823 case CXCursor_ObjCClassRef: return "ObjCClassRef";
824 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000825
Daniel Dunbaracca7252009-11-30 20:42:49 +0000826 case CXCursor_VarRef: return "VarRef";
827 case CXCursor_FunctionRef: return "FunctionRef";
828 case CXCursor_EnumConstantRef: return "EnumConstantRef";
829 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000830
Daniel Dunbaracca7252009-11-30 20:42:49 +0000831 case CXCursor_InvalidFile: return "InvalidFile";
832 case CXCursor_NoDeclFound: return "NoDeclFound";
833 case CXCursor_NotImplemented: return "NotImplemented";
834 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000835 }
Steve Naroff600866c2009-08-27 19:51:58 +0000836}
Steve Naroff89922f82009-08-31 00:59:03 +0000837
Steve Naroff9efa7672009-09-04 15:44:05 +0000838static enum CXCursorKind TranslateKind(Decl *D) {
839 switch (D->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000840 case Decl::Function: return CXCursor_FunctionDecl;
841 case Decl::Typedef: return CXCursor_TypedefDecl;
842 case Decl::Enum: return CXCursor_EnumDecl;
843 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
844 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
845 case Decl::Field: return CXCursor_FieldDecl;
846 case Decl::Var: return CXCursor_VarDecl;
847 case Decl::ParmVar: return CXCursor_ParmDecl;
848 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
849 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
850 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
851 case Decl::ObjCMethod: {
852 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
853 if (MD->isInstanceMethod())
854 return CXCursor_ObjCInstanceMethodDecl;
855 return CXCursor_ObjCClassMethodDecl;
856 }
857 default: break;
Steve Naroff9efa7672009-09-04 15:44:05 +0000858 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000859 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000860}
Steve Naroff600866c2009-08-27 19:51:58 +0000861//
862// CXCursor Operations.
863//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000864
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000865CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000866 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000867 assert(CTUnit && "Passed null CXTranslationUnit");
868 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000869
Steve Naroff9efa7672009-09-04 15:44:05 +0000870 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000871 const FileEntry *File = FMgr.getFile(source_name,
872 source_name+strlen(source_name));
Steve Naroff77128dd2009-09-15 20:25:34 +0000873 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000874 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000875 return C;
876 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000877 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000878 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000879
Steve Narofff96b5242009-10-28 20:44:47 +0000880 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
881
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000882 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000883 &LastLoc);
884 if (ALoc.isValid())
885 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000886
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000887 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000888 if (ALoc.isNamedRef())
889 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000890 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000891 if (Dcl) {
892 if (Stm) {
893 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
894 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
895 return C;
896 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
897 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
898 return C;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000899 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000900 // Fall through...treat as a decl, not a ref.
901 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000902 if (ALoc.isNamedRef()) {
903 if (isa<ObjCInterfaceDecl>(Dcl)) {
904 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
905 return C;
906 }
907 if (isa<ObjCProtocolDecl>(Dcl)) {
908 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
909 return C;
910 }
911 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000912 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000913 return C;
914 }
Steve Narofffb570422009-09-22 19:25:29 +0000915 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000916 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000917}
918
Ted Kremenek73885552009-11-17 19:28:59 +0000919CXCursor clang_getNullCursor(void) {
920 CXCursor C;
921 C.kind = CXCursor_InvalidFile;
922 C.decl = NULL;
923 C.stmt = NULL;
924 return C;
925}
926
927unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
928 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
929}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000930
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000931CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000932 assert(AnonDecl && "Passed null CXDecl");
933 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000934
Steve Narofffb570422009-09-22 19:25:29 +0000935 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000936 return C;
937}
938
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000939unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000940 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
941}
942
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000943unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000944 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
945}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000946
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000947unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000948 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
949}
950
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000951unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000952 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
953}
954
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000955CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000956 return C.kind;
957}
958
Steve Naroff699a07d2009-09-25 21:32:34 +0000959static Decl *getDeclFromExpr(Stmt *E) {
960 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
961 return RefExpr->getDecl();
962 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
963 return ME->getMemberDecl();
964 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
965 return RE->getDecl();
966
967 if (CallExpr *CE = dyn_cast<CallExpr>(E))
968 return getDeclFromExpr(CE->getCallee());
969 if (CastExpr *CE = dyn_cast<CastExpr>(E))
970 return getDeclFromExpr(CE->getSubExpr());
971 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
972 return OME->getMethodDecl();
973
974 return 0;
975}
976
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000977CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000978 if (clang_isDeclaration(C.kind))
979 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000980
Steve Naroff699a07d2009-09-25 21:32:34 +0000981 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000982 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000983 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +0000984 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000985 return static_cast<Stmt *>(C.stmt);
986 else
987 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
988 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000989 return C.decl;
990 }
991 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000992}
993
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000994unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +0000995 assert(C.decl && "CXCursor has null decl");
996 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000997 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000998
Steve Narofff334b4e2009-09-02 18:26:48 +0000999 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001000 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001001}
Steve Narofff334b4e2009-09-02 18:26:48 +00001002
Steve Naroffef0cef62009-11-09 17:45:52 +00001003const char *clang_getCString(CXString string) {
1004 return string.Spelling;
1005}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001006
Steve Naroffef0cef62009-11-09 17:45:52 +00001007void clang_disposeString(CXString string) {
Benjamin Kramer858e5de2009-11-09 18:24:53 +00001008 if (string.MustFreeString)
1009 free((void*)string.Spelling);
Steve Naroffef0cef62009-11-09 17:45:52 +00001010}
1011
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001012unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001013 assert(C.decl && "CXCursor has null decl");
1014 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001015 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001016
Steve Narofff334b4e2009-09-02 18:26:48 +00001017 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001018 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001019}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001020
1021const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001022 assert(C.decl && "CXCursor has null decl");
1023 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001024 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001025
Steve Narofff334b4e2009-09-02 18:26:48 +00001026 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001027
Ted Kremenek9298cfc2009-11-17 05:31:58 +00001028 if (SLoc.isFileID()) {
1029 const char *bufferName = SourceMgr.getBufferName(SLoc);
1030 return bufferName[0] == '<' ? NULL : bufferName;
1031 }
Douglas Gregor02465752009-10-16 21:24:31 +00001032
1033 // Retrieve the file in which the macro was instantiated, then provide that
1034 // buffer name.
1035 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001036 const llvm::MemoryBuffer *Buffer
Douglas Gregor02465752009-10-16 21:24:31 +00001037 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
1038 if (!Buffer)
1039 return 0;
1040
1041 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +00001042}
1043
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001044CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +00001045 assert(C.decl && "CXCursor has null decl");
1046 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1047 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001048
Steve Naroff88145032009-10-27 14:35:18 +00001049 return (void *)getFileEntryFromSourceLocation(SourceMgr,
Daniel Dunbaracca7252009-11-30 20:42:49 +00001050 getLocationFromCursor(C,SourceMgr, ND));
Steve Naroff88145032009-10-27 14:35:18 +00001051}
1052
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001053void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001054 const char **startBuf,
1055 const char **endBuf,
1056 unsigned *startLine,
1057 unsigned *startColumn,
1058 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001059 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +00001060 assert(C.decl && "CXCursor has null decl");
1061 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1062 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1063 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001064
Steve Naroff4ade6d62009-09-23 17:52:52 +00001065 SourceManager &SM = FD->getASTContext().getSourceManager();
1066 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1067 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1068 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1069 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1070 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1071 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1072}
1073
Steve Naroff600866c2009-08-27 19:51:58 +00001074} // end extern "C"