blob: 53b4ed6f30902ef5b82e7aea4e76191992d1d990 [file] [log] [blame]
Ted Kremenekb60d87c2009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00007//
Ted Kremenekb60d87c2009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekb60d87c2009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000015#include "CIndexer.h"
16
Steve Naroffa1c72842009-08-28 15:28:48 +000017#include "clang/AST/DeclVisitor.h"
Steve Naroff66af1ae2009-09-22 19:25:29 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenek2a43fd52010-01-06 23:43:31 +000019#include "clang/Lex/Lexer.h"
Douglas Gregord3d923a2009-10-16 21:24:31 +000020#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer2836c4c2009-10-18 11:19:36 +000021#include "llvm/System/Program.h"
Ted Kremenek428c6372009-10-19 21:44:57 +000022
Ted Kremenek902292d2010-01-05 20:55:39 +000023// Needed to define L_TMPNAM on some systems.
24#include <cstdio>
25
Steve Naroffa1c72842009-08-28 15:28:48 +000026using namespace clang;
27using namespace idx;
28
Ted Kremenek991eb3f2010-01-06 03:42:32 +000029//===----------------------------------------------------------------------===//
30// Crash Reporting.
31//===----------------------------------------------------------------------===//
32
33#ifdef __APPLE__
Ted Kremenek7a5ede22010-01-07 22:49:05 +000034#ifndef NDEBUG
35#define USE_CRASHTRACER
Ted Kremenek991eb3f2010-01-06 03:42:32 +000036#include "clang/Analysis/Support/SaveAndRestore.h"
37// Integrate with crash reporter.
38extern "C" const char *__crashreporter_info__;
Ted Kremenek7a5ede22010-01-07 22:49:05 +000039#define NUM_CRASH_STRINGS 16
40static unsigned crashtracer_counter = 0;
Ted Kremenek32b79312010-01-07 23:13:53 +000041static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek7a5ede22010-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 Kremenek32b79312010-01-07 23:13:53 +000048 unsigned slot = 0;
Ted Kremenek7a5ede22010-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 Kremenek32b79312010-01-07 23:13:53 +000054 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek7a5ede22010-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 Kremenek32b79312010-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 Kremenek7a5ede22010-01-07 22:49:05 +000081 }
Ted Kremenek32b79312010-01-07 23:13:53 +000082
83 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek7a5ede22010-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 Kremenek991eb3f2010-01-06 03:42:32 +0000112#endif
113
114//===----------------------------------------------------------------------===//
115// Visitors.
116//===----------------------------------------------------------------------===//
117
Steve Naroff1054e602009-08-31 00:59:03 +0000118namespace {
Ted Kremenek0ec2cca2010-01-05 19:32:54 +0000119static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff76b8f132009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000127 else
Steve Naroff76b8f132009-09-23 17:52:52 +0000128 return CXCursor_NotImplemented;
129}
130
131#if 0
132// Will be useful one day.
Steve Naroff66af1ae2009-09-22 19:25:29 +0000133class CRefVisitor : public StmtVisitor<CRefVisitor> {
134 CXDecl CDecl;
135 CXDeclIterator Callback;
136 CXClientData CData;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000137
Steve Naroff66af1ae2009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000144 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Naroff66af1ae2009-09-22 19:25:29 +0000145 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000146
Steve Naroff66af1ae2009-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 Naroff76b8f132009-09-23 17:52:52 +0000153 Call(TranslateDeclRefExpr(Node), Node);
Steve Naroff66af1ae2009-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 Naroff76b8f132009-09-23 17:52:52 +0000165#endif
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000166
Steve Naroff1054e602009-08-31 00:59:03 +0000167// Translation Unit Visitor.
168class TUVisitor : public DeclVisitor<TUVisitor> {
169 CXTranslationUnit TUnit;
170 CXTranslationUnitIterator Callback;
Steve Naroff69b10fd2009-09-01 15:55:40 +0000171 CXClientData CData;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000172
Douglas Gregor16bef852009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000177
Steve Naroff69b10fd2009-09-01 15:55:40 +0000178 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor16bef852009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000182
Steve Naroff58bd62d2009-10-28 20:44:47 +0000183 // Filter any implicit declarations (since the source info will be bogus).
184 if (ND->isImplicit())
185 return;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000186
Steve Naroff66af1ae2009-09-22 19:25:29 +0000187 CXCursor C = { CK, ND, 0 };
Steve Naroff69b10fd2009-09-01 15:55:40 +0000188 Callback(TUnit, C, CData);
189 }
Steve Naroff1054e602009-08-31 00:59:03 +0000190public:
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000191 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor16bef852009-10-16 20:01:17 +0000192 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000193 unsigned MaxPCHLevel) :
Douglas Gregor16bef852009-10-16 20:01:17 +0000194 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000195
Steve Naroff1054e602009-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 Kremenek98524b12009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000220 }
Ted Kremenek98524b12009-11-17 07:02:15 +0000221 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
222 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000223 }
224 void VisitTagDecl(TagDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000225 switch (ND->getTagKind()) {
Daniel Dunbar5b2f5ca2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000238 }
Steve Naroff1054e602009-08-31 00:59:03 +0000239 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000240 void VisitTypedefDecl(TypedefDecl *ND) {
241 Call(CXCursor_TypedefDecl, ND);
242 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000243 void VisitVarDecl(VarDecl *ND) {
244 Call(CXCursor_VarDecl, ND);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000245 }
Steve Naroff1054e602009-08-31 00:59:03 +0000246};
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000247
Steve Naroff1054e602009-08-31 00:59:03 +0000248
Steve Naroff3645f5a2009-09-02 13:28:54 +0000249// Declaration visitor.
250class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
251 CXDecl CDecl;
252 CXDeclIterator Callback;
253 CXClientData CData;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000254
Douglas Gregor16bef852009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000259
Steve Naroff3645f5a2009-09-02 13:28:54 +0000260 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroff38c1a7b2009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000264
Douglas Gregor16bef852009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000268
Steve Naroff66af1ae2009-09-22 19:25:29 +0000269 CXCursor C = { CK, ND, 0 };
Steve Naroff3645f5a2009-09-02 13:28:54 +0000270 Callback(CDecl, C, CData);
271 }
Steve Naroff1054e602009-08-31 00:59:03 +0000272public:
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000273 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
274 unsigned MaxPCHLevel) :
Douglas Gregor16bef852009-10-16 20:01:17 +0000275 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000276
Steve Naroff38c1a7b2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000283 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000284 // Issue callbacks for super class.
Steve Naroff80a766b2009-09-02 18:26:48 +0000285 if (D->getSuperClass())
286 Call(CXCursor_ObjCSuperClassRef, D);
287
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000288 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000289 E = D->protocol_end(); I != E; ++I)
Steve Naroffef9618b2009-09-04 15:44:05 +0000290 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroff3645f5a2009-09-02 13:28:54 +0000291 VisitDeclContext(dyn_cast<DeclContext>(D));
292 }
Steve Naroffef9618b2009-09-04 15:44:05 +0000293 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000294 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000295 E = PID->protocol_end(); I != E; ++I)
Steve Naroffef9618b2009-09-04 15:44:05 +0000296 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000297
Steve Naroffef9618b2009-09-04 15:44:05 +0000298 VisitDeclContext(dyn_cast<DeclContext>(PID));
299 }
Steve Naroff3645f5a2009-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 Naroff1054e602009-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 Naroff3645f5a2009-09-02 13:28:54 +0000315 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000316 }
317 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000318 Call(CXCursor_FieldDecl, ND);
319 }
Steve Naroff38c1a7b2009-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 Naroff3645f5a2009-09-02 13:28:54 +0000326 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
327 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000328 }
329 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000330 Call(CXCursor_ObjCIvarDecl, ND);
331 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000332 void VisitFunctionDecl(FunctionDecl *ND) {
333 if (ND->isThisDeclarationADefinition()) {
334 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff76b8f132009-09-23 17:52:52 +0000335#if 0
336 // Not currently needed.
337 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Naroff66af1ae2009-09-22 19:25:29 +0000338 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff76b8f132009-09-23 17:52:52 +0000339 RVisit.Visit(Body);
340#endif
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000341 }
342 }
Steve Naroff3645f5a2009-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 Naroff38c1a7b2009-09-03 15:49:00 +0000347 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff3645f5a2009-09-02 13:28:54 +0000348 } else
349 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
350 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000351 }
352};
Ted Kremenek0ec2cca2010-01-05 19:32:54 +0000353} // end anonymous namespace
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000354
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000355static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbar2679a882009-11-08 04:13:53 +0000356 SourceManager &SourceMgr,
357 NamedDecl *ND) {
358 if (clang_isReference(C.kind)) {
359 switch (C.kind) {
Daniel Dunbar5b2f5ca2009-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 Dunbar2679a882009-11-08 04:13:53 +0000365 }
Daniel Dunbar5b2f5ca2009-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 Dunbar2679a882009-11-08 04:13:53 +0000396 }
397 } else { // We have a declaration or a definition.
398 SourceLocation SLoc;
399 switch (ND->getKind()) {
Daniel Dunbar5b2f5ca2009-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 Dunbar2679a882009-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 Kramer04c99a62009-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 Kramer61f5d0c2009-10-18 16:11:04 +0000431extern "C" {
432
Steve Naroff531e2842009-10-20 14:46:24 +0000433CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000434 int displayDiagnostics) {
Steve Naroff531e2842009-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 Naroffd5e8e862009-08-27 19:51:58 +0000441}
442
Daniel Dunbar079203f2009-12-01 03:14:51 +0000443void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff3aa2d732009-09-17 18:33:27 +0000444 assert(CIdx && "Passed null CXIndex");
Douglas Gregor16bef852009-10-16 20:01:17 +0000445 delete static_cast<CIndexer *>(CIdx);
Steve Naroff3aa2d732009-09-17 18:33:27 +0000446}
447
Daniel Dunbar11089662009-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 Naroffa1c72842009-08-28 15:28:48 +0000454// FIXME: need to pass back error info.
Daniel Dunbar079203f2009-12-01 03:14:51 +0000455CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
456 const char *ast_filename) {
Steve Naroffa1c72842009-08-28 15:28:48 +0000457 assert(CIdx && "Passed null CXIndex");
Douglas Gregor16bef852009-10-16 20:01:17 +0000458 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000459
Daniel Dunbar59203002009-12-03 01:45:44 +0000460 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
461 CXXIdx->getOnlyLocalDecls(),
462 /* UseBumpAllocator = */ true);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000463}
464
Daniel Dunbar079203f2009-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 Naroff531e2842009-10-20 14:46:24 +0000470 assert(CIdx && "Passed null CXIndex");
471 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
472
Daniel Dunbar11089662009-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 Dunbar72fe5b12009-12-05 02:17:18 +0000484 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek991eb3f2010-01-06 03:42:32 +0000485
Ted Kremenek7a5ede22010-01-07 22:49:05 +0000486#ifdef USE_CRASHTRACER
487 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek991eb3f2010-01-06 03:42:32 +0000488#endif
489
Daniel Dunbar72fe5b12009-12-05 02:17:18 +0000490 llvm::OwningPtr<ASTUnit> Unit(
491 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar8d4a2022009-12-13 03:46:13 +0000492 CXXIdx->getDiags(),
493 CXXIdx->getClangResourcesPath(),
Daniel Dunbar72fe5b12009-12-05 02:17:18 +0000494 CXXIdx->getOnlyLocalDecls(),
495 /* UseBumpAllocator = */ true));
Ted Kremenek7a5ede22010-01-07 22:49:05 +0000496
Daniel Dunbar72fe5b12009-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 Dunbar11089662009-12-03 01:54:28 +0000503 }
504
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000505 // Build up the arguments for invoking 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000506 std::vector<const char *> argv;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000507
Ted Kremenek649bf5c2009-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 Kramer61f5d0c2009-10-18 16:11:04 +0000510 argv.push_back(ClangPath.c_str());
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000511
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000512 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000513 argv.push_back("-emit-ast");
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000514
Ted Kremenek649bf5c2009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000518 if (source_filename)
519 argv.push_back(source_filename);
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000520
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000521 // Generate a temporary name for the AST file.
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000522 argv.push_back("-o");
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000523 char astTmpFile[L_tmpnam];
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000524 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek649bf5c2009-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 Naroff531e2842009-10-20 14:46:24 +0000541 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000542
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000543 // Add the null terminator.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000544 argv.push_back(NULL);
545
Ted Kremenek12e678d2009-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 Kremenek44886fd2009-10-22 03:24:01 +0000549 std::string ErrMsg;
Ted Kremeneke2896882009-10-19 22:27:32 +0000550 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek44886fd2009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000554
Ted Kremenekba645742009-11-10 19:18:52 +0000555 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000556 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000557 << '\n' << "Arguments: \n";
Ted Kremenek44886fd2009-10-22 03:24:01 +0000558 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenekbf0690c2009-10-26 22:08:39 +0000559 I!=E; ++I) {
560 if (*I)
561 llvm::errs() << ' ' << *I << '\n';
562 }
563 llvm::errs() << '\n';
Ted Kremenek44886fd2009-10-22 03:24:01 +0000564 }
Benjamin Kramer2836c4c2009-10-18 11:19:36 +0000565
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000566 // Finally, we create the translation unit from the ast file.
Steve Naroff44cd60e2009-10-15 22:23:48 +0000567 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000568 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroff531e2842009-10-20 14:46:24 +0000569 if (ATU)
570 ATU->unlinkTemporaryFile();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000571 return ATU;
Steve Naroff7781daa2009-10-15 20:04:39 +0000572}
573
Daniel Dunbar079203f2009-12-01 03:14:51 +0000574void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff3aa2d732009-09-17 18:33:27 +0000575 assert(CTUnit && "Passed null CXTranslationUnit");
576 delete static_cast<ASTUnit *>(CTUnit);
577}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000578
Daniel Dunbar079203f2009-12-01 03:14:51 +0000579CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000580 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroffc0683b92009-09-03 18:19:54 +0000581 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000582 return createCXString(CXXUnit->getOriginalSourceFileName().c_str(), true);
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000583}
Daniel Dunbare58bd8b2009-08-28 16:30:07 +0000584
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000585void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroff3645f5a2009-09-02 13:28:54 +0000586 CXTranslationUnitIterator callback,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000587 CXClientData CData) {
Steve Naroffa1c72842009-08-28 15:28:48 +0000588 assert(CTUnit && "Passed null CXTranslationUnit");
589 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
590 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000591
Daniel Dunbar11089662009-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 Dunbar644dca02009-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 Naroffd5e8e862009-08-27 19:51:58 +0000615}
616
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000617void clang_loadDeclaration(CXDecl Dcl,
618 CXDeclIterator callback,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000619 CXClientData CData) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000620 assert(Dcl && "Passed null CXDecl");
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000621
Douglas Gregor16bef852009-10-16 20:01:17 +0000622 CDeclVisitor DVisit(Dcl, callback, CData,
623 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroff3645f5a2009-09-02 13:28:54 +0000624 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroffd5e8e862009-08-27 19:51:58 +0000625}
626
Steve Naroffd5e8e862009-08-27 19:51:58 +0000627//
628// CXDecl Operations.
629//
Daniel Dunbar079203f2009-12-01 03:14:51 +0000630
Daniel Dunbar079203f2009-12-01 03:14:51 +0000631CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff1054e602009-08-31 00:59:03 +0000632 assert(AnonDecl && "Passed null CXDecl");
633 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000634
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000635 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
636 return createCXString(OMD->getSelector().getAsString().c_str(), true);
637
638 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Narofff406f4d2009-10-29 21:11:04 +0000639 // No, this isn't the same as the code below. getIdentifier() is non-virtual
640 // and returns different names. NamedDecl returns the class name and
641 // ObjCCategoryImplDecl returns the category name.
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000642 return createCXString(CIMP->getIdentifier()->getNameStart());
643
644 if (ND->getIdentifier())
645 return createCXString(ND->getIdentifier()->getNameStart());
646
647 return createCXString("");
Steve Naroffd5e8e862009-08-27 19:51:58 +0000648}
Steve Naroff80a766b2009-09-02 18:26:48 +0000649
Daniel Dunbar079203f2009-12-01 03:14:51 +0000650unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff63f475a2009-09-25 21:32:34 +0000651 assert(AnonDecl && "Passed null CXDecl");
652 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
653 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
654 return SourceMgr.getSpellingLineNumber(ND->getLocation());
655}
656
Daniel Dunbar079203f2009-12-01 03:14:51 +0000657unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff63f475a2009-09-25 21:32:34 +0000658 assert(AnonDecl && "Passed null CXDecl");
659 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
660 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff43b118f2009-09-25 22:15:54 +0000661 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff63f475a2009-09-25 21:32:34 +0000662}
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000663
664CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
665 assert(AnonDecl && "Passed null CXDecl");
666 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000667 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000668 SourceRange R = ND->getSourceRange();
669
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000670 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
671 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
672
673 if (!Begin.isValid()) {
674 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
675 return extent;
676 }
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000677
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000678 // FIXME: This is largely copy-paste from
679 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
680 // what we want the two routines should be refactored.
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000681
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000682 // If the End location and the start location are the same and are a macro
683 // location, then the range was something that came from a macro expansion
684 // or _Pragma. If this is an object-like macro, the best we can do is to
685 // get the range. If this is a function-like macro, we'd also like to
686 // get the arguments.
687 if (Begin == End && R.getEnd().isMacroID())
688 End = SM.getInstantiationRange(R.getEnd()).second;
689
690 assert(SM.getFileID(Begin) == SM.getFileID(End));
691 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
692 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000693
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000694 // Compute the column number of the start. Keep the column based at 1.
695 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
696
697 // Compute the column number of the end.
698 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
699 if (EndColNo) {
700 // Offset the end column by 1 so that we point to the last character
701 // in the last token.
702 --EndColNo;
703
704 // Add in the length of the token, so that we cover multi-char tokens.
705 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
706 const LangOptions &LOpts = Ctx.getLangOptions();
707
708 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
709 }
710
711 // Package up the line/column data and return to the caller.
712 CXDeclExtent extent = { { StartLineNo, StartColNo },
713 { EndLineNo, EndColNo } };
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000714 return extent;
715}
Steve Naroff63f475a2009-09-25 21:32:34 +0000716
Ted Kremenekea903062010-01-08 17:11:32 +0000717const char *clang_getDeclSource(CXDecl AnonDecl) {
718 assert(AnonDecl && "Passed null CXDecl");
719 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
720 assert(FEnt && "Cannot find FileEntry for Decl");
721 return clang_getFileName(FEnt);
722}
723
Steve Naroff6231f182009-10-27 14:35:18 +0000724static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000725 SourceLocation SLoc) {
Steve Naroff6231f182009-10-27 14:35:18 +0000726 FileID FID;
727 if (SLoc.isFileID())
728 FID = SMgr.getFileID(SLoc);
729 else
730 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
731 return SMgr.getFileEntryForID(FID);
732}
733
Daniel Dunbar079203f2009-12-01 03:14:51 +0000734CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff6231f182009-10-27 14:35:18 +0000735 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff26760892009-09-25 21:45:39 +0000736 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
737 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff6231f182009-10-27 14:35:18 +0000738 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
739}
740
741const char *clang_getFileName(CXFile SFile) {
742 assert(SFile && "Passed null CXFile");
743 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
744 return FEnt->getName();
745}
746
747time_t clang_getFileTime(CXFile SFile) {
748 assert(SFile && "Passed null CXFile");
749 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
750 return FEnt->getModificationTime();
Steve Naroff26760892009-09-25 21:45:39 +0000751}
752
Daniel Dunbar079203f2009-12-01 03:14:51 +0000753CXString clang_getCursorSpelling(CXCursor C) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000754 assert(C.decl && "CXCursor has null decl");
755 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000756
Steve Naroff80a766b2009-09-02 18:26:48 +0000757 if (clang_isReference(C.kind)) {
758 switch (C.kind) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000759 case CXCursor_ObjCSuperClassRef: {
760 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
761 assert(OID && "clang_getCursorLine(): Missing interface decl");
762 return createCXString(OID->getSuperClass()->getIdentifier()
763 ->getNameStart());
764 }
765 case CXCursor_ObjCClassRef: {
766 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000767 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000768
769 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
770 assert(OCD && "clang_getCursorLine(): Missing category decl");
771 return createCXString(OCD->getClassInterface()->getIdentifier()
772 ->getNameStart());
773 }
774 case CXCursor_ObjCProtocolRef: {
775 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
776 assert(OID && "clang_getCursorLine(): Missing protocol decl");
777 return createCXString(OID->getIdentifier()->getNameStart());
778 }
779 case CXCursor_ObjCSelectorRef: {
780 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
781 static_cast<Stmt *>(C.stmt));
782 assert(OME && "clang_getCursorLine(): Missing message expr");
783 return createCXString(OME->getSelector().getAsString().c_str(), true);
784 }
785 case CXCursor_VarRef:
786 case CXCursor_FunctionRef:
787 case CXCursor_EnumConstantRef: {
788 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
789 static_cast<Stmt *>(C.stmt));
790 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
791 return createCXString(DRE->getDecl()->getIdentifier()->getNameStart());
792 }
793 default:
794 return createCXString("<not implemented>");
Steve Naroff80a766b2009-09-02 18:26:48 +0000795 }
796 }
797 return clang_getDeclSpelling(C.decl);
798}
799
Daniel Dunbar079203f2009-12-01 03:14:51 +0000800const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff1054e602009-08-31 00:59:03 +0000801 switch (Kind) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000802 case CXCursor_FunctionDecl: return "FunctionDecl";
803 case CXCursor_TypedefDecl: return "TypedefDecl";
804 case CXCursor_EnumDecl: return "EnumDecl";
805 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
806 case CXCursor_StructDecl: return "StructDecl";
807 case CXCursor_UnionDecl: return "UnionDecl";
808 case CXCursor_ClassDecl: return "ClassDecl";
809 case CXCursor_FieldDecl: return "FieldDecl";
810 case CXCursor_VarDecl: return "VarDecl";
811 case CXCursor_ParmDecl: return "ParmDecl";
812 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
813 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
814 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
815 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
816 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
817 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
818 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
819 case CXCursor_FunctionDefn: return "FunctionDefn";
820 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
821 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
822 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
823 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
824 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
825 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
826 case CXCursor_ObjCClassRef: return "ObjCClassRef";
827 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000828
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000829 case CXCursor_VarRef: return "VarRef";
830 case CXCursor_FunctionRef: return "FunctionRef";
831 case CXCursor_EnumConstantRef: return "EnumConstantRef";
832 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000833
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000834 case CXCursor_InvalidFile: return "InvalidFile";
835 case CXCursor_NoDeclFound: return "NoDeclFound";
836 case CXCursor_NotImplemented: return "NotImplemented";
837 default: return "<not implemented>";
Steve Naroff1054e602009-08-31 00:59:03 +0000838 }
Steve Naroffd5e8e862009-08-27 19:51:58 +0000839}
Steve Naroff1054e602009-08-31 00:59:03 +0000840
Steve Naroffef9618b2009-09-04 15:44:05 +0000841static enum CXCursorKind TranslateKind(Decl *D) {
842 switch (D->getKind()) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000843 case Decl::Function: return CXCursor_FunctionDecl;
844 case Decl::Typedef: return CXCursor_TypedefDecl;
845 case Decl::Enum: return CXCursor_EnumDecl;
846 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
847 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
848 case Decl::Field: return CXCursor_FieldDecl;
849 case Decl::Var: return CXCursor_VarDecl;
850 case Decl::ParmVar: return CXCursor_ParmDecl;
851 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
852 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
853 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
854 case Decl::ObjCMethod: {
855 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
856 if (MD->isInstanceMethod())
857 return CXCursor_ObjCInstanceMethodDecl;
858 return CXCursor_ObjCClassMethodDecl;
859 }
860 default: break;
Steve Naroffef9618b2009-09-04 15:44:05 +0000861 }
Steve Naroff54f22fb2009-09-15 20:25:34 +0000862 return CXCursor_NotImplemented;
Steve Naroffef9618b2009-09-04 15:44:05 +0000863}
Steve Naroffd5e8e862009-08-27 19:51:58 +0000864//
865// CXCursor Operations.
866//
Daniel Dunbar079203f2009-12-01 03:14:51 +0000867
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000868CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000869 unsigned line, unsigned column) {
Steve Naroffef9618b2009-09-04 15:44:05 +0000870 assert(CTUnit && "Passed null CXTranslationUnit");
871 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000872
Steve Naroffef9618b2009-09-04 15:44:05 +0000873 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000874 const FileEntry *File = FMgr.getFile(source_name,
875 source_name+strlen(source_name));
Steve Naroff54f22fb2009-09-15 20:25:34 +0000876 if (!File) {
Steve Naroff66af1ae2009-09-22 19:25:29 +0000877 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000878 return C;
879 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000880 SourceLocation SLoc =
Steve Naroffef9618b2009-09-04 15:44:05 +0000881 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000882
Steve Naroff58bd62d2009-10-28 20:44:47 +0000883 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
884
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000885 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Naroff58bd62d2009-10-28 20:44:47 +0000886 &LastLoc);
887 if (ALoc.isValid())
888 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000889
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000890 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis419e38b2009-09-29 19:45:58 +0000891 if (ALoc.isNamedRef())
892 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000893 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff76b8f132009-09-23 17:52:52 +0000894 if (Dcl) {
895 if (Stm) {
896 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
897 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
898 return C;
899 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
900 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
901 return C;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000902 }
Steve Naroff76b8f132009-09-23 17:52:52 +0000903 // Fall through...treat as a decl, not a ref.
904 }
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000905 if (ALoc.isNamedRef()) {
906 if (isa<ObjCInterfaceDecl>(Dcl)) {
907 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
908 return C;
909 }
910 if (isa<ObjCProtocolDecl>(Dcl)) {
911 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
912 return C;
913 }
914 }
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000915 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000916 return C;
917 }
Steve Naroff66af1ae2009-09-22 19:25:29 +0000918 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroffef9618b2009-09-04 15:44:05 +0000919 return C;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000920}
921
Ted Kremeneke05d7802009-11-17 19:28:59 +0000922CXCursor clang_getNullCursor(void) {
923 CXCursor C;
924 C.kind = CXCursor_InvalidFile;
925 C.decl = NULL;
926 C.stmt = NULL;
927 return C;
928}
929
930unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
931 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
932}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000933
Daniel Dunbar079203f2009-12-01 03:14:51 +0000934CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff54f22fb2009-09-15 20:25:34 +0000935 assert(AnonDecl && "Passed null CXDecl");
936 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000937
Steve Naroff66af1ae2009-09-22 19:25:29 +0000938 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff54f22fb2009-09-15 20:25:34 +0000939 return C;
940}
941
Daniel Dunbar079203f2009-12-01 03:14:51 +0000942unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff54f22fb2009-09-15 20:25:34 +0000943 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
944}
945
Daniel Dunbar079203f2009-12-01 03:14:51 +0000946unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff1054e602009-08-31 00:59:03 +0000947 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
948}
Steve Naroff772c1a42009-08-31 14:26:51 +0000949
Daniel Dunbar079203f2009-12-01 03:14:51 +0000950unsigned clang_isReference(enum CXCursorKind K) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000951 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
952}
953
Daniel Dunbar079203f2009-12-01 03:14:51 +0000954unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000955 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
956}
957
Daniel Dunbar079203f2009-12-01 03:14:51 +0000958CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroffef9618b2009-09-04 15:44:05 +0000959 return C.kind;
960}
961
Steve Naroff63f475a2009-09-25 21:32:34 +0000962static Decl *getDeclFromExpr(Stmt *E) {
963 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
964 return RefExpr->getDecl();
965 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
966 return ME->getMemberDecl();
967 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
968 return RE->getDecl();
969
970 if (CallExpr *CE = dyn_cast<CallExpr>(E))
971 return getDeclFromExpr(CE->getCallee());
972 if (CastExpr *CE = dyn_cast<CastExpr>(E))
973 return getDeclFromExpr(CE->getSubExpr());
974 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
975 return OME->getMethodDecl();
976
977 return 0;
978}
979
Daniel Dunbar079203f2009-12-01 03:14:51 +0000980CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff63f475a2009-09-25 21:32:34 +0000981 if (clang_isDeclaration(C.kind))
982 return C.decl;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000983
Steve Naroff63f475a2009-09-25 21:32:34 +0000984 if (clang_isReference(C.kind)) {
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000985 if (C.stmt) {
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000986 if (C.kind == CXCursor_ObjCClassRef ||
Steve Naroffd7eb7172009-10-05 17:58:19 +0000987 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000988 return static_cast<Stmt *>(C.stmt);
989 else
990 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
991 } else
Steve Naroff63f475a2009-09-25 21:32:34 +0000992 return C.decl;
993 }
994 return 0;
Steve Naroffef9618b2009-09-04 15:44:05 +0000995}
996
Daniel Dunbar079203f2009-12-01 03:14:51 +0000997unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff772c1a42009-08-31 14:26:51 +0000998 assert(C.decl && "CXCursor has null decl");
999 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +00001000 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001001
Steve Naroff80a766b2009-09-02 18:26:48 +00001002 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +00001003 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +00001004}
Steve Naroff80a766b2009-09-02 18:26:48 +00001005
Steve Naroff8675d5c2009-11-09 17:45:52 +00001006const char *clang_getCString(CXString string) {
1007 return string.Spelling;
1008}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001009
Steve Naroff8675d5c2009-11-09 17:45:52 +00001010void clang_disposeString(CXString string) {
Benjamin Kramerd6f85a82009-11-09 18:24:53 +00001011 if (string.MustFreeString)
1012 free((void*)string.Spelling);
Steve Naroff8675d5c2009-11-09 17:45:52 +00001013}
1014
Daniel Dunbar079203f2009-12-01 03:14:51 +00001015unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff772c1a42009-08-31 14:26:51 +00001016 assert(C.decl && "CXCursor has null decl");
1017 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +00001018 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001019
Steve Naroff80a766b2009-09-02 18:26:48 +00001020 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +00001021 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +00001022}
Daniel Dunbar079203f2009-12-01 03:14:51 +00001023
1024const char *clang_getCursorSource(CXCursor C) {
Steve Naroff772c1a42009-08-31 14:26:51 +00001025 assert(C.decl && "CXCursor has null decl");
1026 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff772c1a42009-08-31 14:26:51 +00001027 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001028
Steve Naroff80a766b2009-09-02 18:26:48 +00001029 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001030
Ted Kremenek4c4d6432009-11-17 05:31:58 +00001031 if (SLoc.isFileID()) {
1032 const char *bufferName = SourceMgr.getBufferName(SLoc);
1033 return bufferName[0] == '<' ? NULL : bufferName;
1034 }
Douglas Gregord3d923a2009-10-16 21:24:31 +00001035
1036 // Retrieve the file in which the macro was instantiated, then provide that
1037 // buffer name.
1038 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001039 const llvm::MemoryBuffer *Buffer
Douglas Gregord3d923a2009-10-16 21:24:31 +00001040 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
1041 if (!Buffer)
1042 return 0;
1043
1044 return Buffer->getBufferIdentifier();
Steve Naroffd5e8e862009-08-27 19:51:58 +00001045}
1046
Daniel Dunbar079203f2009-12-01 03:14:51 +00001047CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff6231f182009-10-27 14:35:18 +00001048 assert(C.decl && "CXCursor has null decl");
1049 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1050 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001051
Steve Naroff6231f182009-10-27 14:35:18 +00001052 return (void *)getFileEntryFromSourceLocation(SourceMgr,
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +00001053 getLocationFromCursor(C,SourceMgr, ND));
Steve Naroff6231f182009-10-27 14:35:18 +00001054}
1055
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001056void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff76b8f132009-09-23 17:52:52 +00001057 const char **startBuf,
1058 const char **endBuf,
1059 unsigned *startLine,
1060 unsigned *startColumn,
1061 unsigned *endLine,
Daniel Dunbar079203f2009-12-01 03:14:51 +00001062 unsigned *endColumn) {
Steve Naroff76b8f132009-09-23 17:52:52 +00001063 assert(C.decl && "CXCursor has null decl");
1064 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1065 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1066 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00001067
Steve Naroff76b8f132009-09-23 17:52:52 +00001068 SourceManager &SM = FD->getASTContext().getSourceManager();
1069 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1070 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1071 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1072 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1073 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1074 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1075}
1076
Steve Naroffd5e8e862009-08-27 19:51:58 +00001077} // end extern "C"