blob: f24a74259367e41fedaefcbcb8a770ddd82d785e [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.
Ted Kremenekf1286182010-01-13 00:13:47 +0000168
Steve Naroff89922f82009-08-31 00:59:03 +0000169class TUVisitor : public DeclVisitor<TUVisitor> {
Ted Kremenekf1286182010-01-13 00:13:47 +0000170public:
171 typedef void (*Iterator)(void *, CXCursor, CXClientData);
172private:
173 void *Root; // CXDecl or CXTranslationUnit
174 Iterator Callback; // CXTranslationUnitIterator or CXDeclIterator.
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000175 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000176
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000177 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
178 // to the visitor. Declarations with a PCH level greater than this value will
179 // be suppressed.
180 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000181
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000182 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000183 // Filter any declarations that have a PCH level greater than what we allow.
184 if (ND->getPCHLevel() > MaxPCHLevel)
185 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000186
Steve Narofff96b5242009-10-28 20:44:47 +0000187 // Filter any implicit declarations (since the source info will be bogus).
188 if (ND->isImplicit())
189 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000190
Ted Kremenekb89e8f62010-01-13 19:59:20 +0000191 CXCursor C = { CK, ND, 0, 0 };
Ted Kremenekf1286182010-01-13 00:13:47 +0000192 Callback(Root, C, CData);
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000193 }
Ted Kremenekf1286182010-01-13 00:13:47 +0000194
Steve Naroff89922f82009-08-31 00:59:03 +0000195public:
Ted Kremenekf1286182010-01-13 00:13:47 +0000196 TUVisitor(void *root, Iterator cback, CXClientData D, unsigned MaxPCHLevel) :
197 Root(root), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000198
Ted Kremenekf1286182010-01-13 00:13:47 +0000199 void VisitDeclContext(DeclContext *DC);
200 void VisitFunctionDecl(FunctionDecl *ND);
201 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
202 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND);
203 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND);
204 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND);
205 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND);
206 void VisitTagDecl(TagDecl *ND);
207 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
208 void VisitTypedefDecl(TypedefDecl *ND);
209 void VisitVarDecl(VarDecl *ND);
210};
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000211
Ted Kremenekf1286182010-01-13 00:13:47 +0000212void TUVisitor::VisitDeclContext(DeclContext *DC) {
213 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
214 I != E; ++I)
215 Visit(*I);
216}
217
218void TUVisitor::VisitFunctionDecl(FunctionDecl *ND) {
219 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
220 : CXCursor_FunctionDecl, ND);
221}
222
223void TUVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
224 Call(CXCursor_ObjCCategoryDecl, ND);
225}
226
227void TUVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
228 Call(CXCursor_ObjCCategoryDefn, ND);
229}
230
231void TUVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
232 Call(CXCursor_ObjCClassDefn, ND);
233}
234
235void TUVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
236 Call(CXCursor_ObjCInterfaceDecl, ND);
237}
238
239void TUVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
240 Call(CXCursor_ObjCProtocolDecl, ND);
241}
242
243void TUVisitor::VisitTagDecl(TagDecl *ND) {
244 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000245 case TagDecl::TK_struct:
246 Call(CXCursor_StructDecl, ND);
247 break;
248 case TagDecl::TK_class:
249 Call(CXCursor_ClassDecl, ND);
250 break;
251 case TagDecl::TK_union:
252 Call(CXCursor_UnionDecl, ND);
253 break;
254 case TagDecl::TK_enum:
255 Call(CXCursor_EnumDecl, ND);
256 break;
Steve Naroff89922f82009-08-31 00:59:03 +0000257 }
Ted Kremenekf1286182010-01-13 00:13:47 +0000258}
259
260void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
261 VisitDeclContext(dyn_cast<DeclContext>(D));
262}
263
264void TUVisitor::VisitTypedefDecl(TypedefDecl *ND) {
265 Call(CXCursor_TypedefDecl, ND);
266}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000267
Ted Kremenekf1286182010-01-13 00:13:47 +0000268void TUVisitor::VisitVarDecl(VarDecl *ND) {
269 Call(CXCursor_VarDecl, ND);
270}
Steve Naroff89922f82009-08-31 00:59:03 +0000271
Steve Naroffc857ea42009-09-02 13:28:54 +0000272// Declaration visitor.
273class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
274 CXDecl CDecl;
275 CXDeclIterator Callback;
276 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000277
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000278 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
279 // to the visitor. Declarations with a PCH level greater than this value will
280 // be suppressed.
281 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000282
Steve Naroffc857ea42009-09-02 13:28:54 +0000283 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000284 // Disable the callback when the context is equal to the visiting decl.
285 if (CDecl == ND && !clang_isReference(CK))
286 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000287
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000288 // Filter any declarations that have a PCH level greater than what we allow.
289 if (ND->getPCHLevel() > MaxPCHLevel)
290 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000291
Ted Kremenekb89e8f62010-01-13 19:59:20 +0000292 CXCursor C = { CK, ND, 0, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000293 Callback(CDecl, C, CData);
294 }
Steve Naroff89922f82009-08-31 00:59:03 +0000295public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000296 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
297 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000298 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000299
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000300 void VisitDeclContext(DeclContext *DC);
301 void VisitEnumConstantDecl(EnumConstantDecl *ND);
302 void VisitFieldDecl(FieldDecl *ND);
303 void VisitFunctionDecl(FunctionDecl *ND);
304 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
305 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
306 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
307 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
308 void VisitObjCIvarDecl(ObjCIvarDecl *ND);
309 void VisitObjCMethodDecl(ObjCMethodDecl *ND);
310 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND);
311 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
312 void VisitParmVarDecl(ParmVarDecl *ND);
313 void VisitTagDecl(TagDecl *D);
314 void VisitVarDecl(VarDecl *ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000315};
Ted Kremenekab188932010-01-05 19:32:54 +0000316} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000317
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000318void CDeclVisitor::VisitDeclContext(DeclContext *DC) {
319 for (DeclContext::decl_iterator
320 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
321 Visit(*I);
322}
323
324void CDeclVisitor::VisitEnumConstantDecl(EnumConstantDecl *ND) {
325 Call(CXCursor_EnumConstantDecl, ND);
326}
327
328void CDeclVisitor::VisitFieldDecl(FieldDecl *ND) {
329 Call(CXCursor_FieldDecl, ND);
330}
331
332void CDeclVisitor::VisitFunctionDecl(FunctionDecl *ND) {
333 if (ND->isThisDeclarationADefinition()) {
334 VisitDeclContext(dyn_cast<DeclContext>(ND));
335#if 0
336 // Not currently needed.
337 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
338 CRefVisitor RVisit(CDecl, Callback, CData);
339 RVisit.Visit(Body);
340#endif
341 }
342}
343
344void CDeclVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
345 // Issue callbacks for the containing class.
346 Call(CXCursor_ObjCClassRef, ND);
347 // FIXME: Issue callbacks for protocol refs.
348 VisitDeclContext(dyn_cast<DeclContext>(ND));
349}
350
351void CDeclVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
352 VisitDeclContext(dyn_cast<DeclContext>(D));
353}
354
355void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
356 VisitDeclContext(dyn_cast<DeclContext>(D));
357}
358
359void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
360 // Issue callbacks for super class.
361 if (D->getSuperClass())
362 Call(CXCursor_ObjCSuperClassRef, D);
363
364 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
365 E = D->protocol_end(); I != E; ++I)
366 Call(CXCursor_ObjCProtocolRef, *I);
367 VisitDeclContext(dyn_cast<DeclContext>(D));
368}
369
370void CDeclVisitor::VisitObjCIvarDecl(ObjCIvarDecl *ND) {
371 Call(CXCursor_ObjCIvarDecl, ND);
372}
373
374void CDeclVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
375 if (ND->getBody()) {
376 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
377 : CXCursor_ObjCClassMethodDefn, ND);
378 VisitDeclContext(dyn_cast<DeclContext>(ND));
379 } else
380 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
381 : CXCursor_ObjCClassMethodDecl, ND);
382}
383
384void CDeclVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
385 Call(CXCursor_ObjCPropertyDecl, ND);
386}
387
388void CDeclVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
389 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
390 E = PID->protocol_end(); I != E; ++I)
391 Call(CXCursor_ObjCProtocolRef, *I);
392
393 VisitDeclContext(dyn_cast<DeclContext>(PID));
394}
395
396void CDeclVisitor::VisitParmVarDecl(ParmVarDecl *ND) {
397 Call(CXCursor_ParmDecl, ND);
398}
399
400void CDeclVisitor::VisitTagDecl(TagDecl *D) {
401 VisitDeclContext(dyn_cast<DeclContext>(D));
402}
403
404void CDeclVisitor::VisitVarDecl(VarDecl *ND) {
405 Call(CXCursor_VarDecl, ND);
406}
407
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000408static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000409 SourceManager &SourceMgr,
410 NamedDecl *ND) {
411 if (clang_isReference(C.kind)) {
Ted Kremenek8b456e82010-01-15 18:24:18 +0000412
413 if (Decl *D = static_cast<Decl*>(C.referringDecl))
414 return D->getLocation();
415
Daniel Dunbarc6190332009-11-08 04:13:53 +0000416 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000417 case CXCursor_ObjCClassRef: {
418 if (isa<ObjCInterfaceDecl>(ND)) {
419 // FIXME: This is a hack (storing the parent decl in the stmt slot).
420 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
421 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000422 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000423 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
424 assert(OID && "clang_getCursorLine(): Missing category decl");
425 return OID->getClassInterface()->getLocation();
426 }
427 case CXCursor_ObjCSuperClassRef: {
428 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
429 assert(OID && "clang_getCursorLine(): Missing interface decl");
430 return OID->getSuperClassLoc();
431 }
432 case CXCursor_ObjCProtocolRef: {
433 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
434 assert(OID && "clang_getCursorLine(): Missing protocol decl");
435 return OID->getLocation();
436 }
437 case CXCursor_ObjCSelectorRef: {
438 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
439 static_cast<Stmt *>(C.stmt));
440 assert(OME && "clang_getCursorLine(): Missing message expr");
441 return OME->getLeftLoc(); /* FIXME: should be a range */
442 }
443 case CXCursor_VarRef:
444 case CXCursor_FunctionRef:
445 case CXCursor_EnumConstantRef: {
446 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
447 static_cast<Stmt *>(C.stmt));
448 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
449 return DRE->getLocation();
450 }
451 default:
452 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000453 }
454 } else { // We have a declaration or a definition.
455 SourceLocation SLoc;
456 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000457 case Decl::ObjCInterface: {
458 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
459 break;
460 }
461 case Decl::ObjCProtocol: {
462 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
463 break;
464 }
465 default: {
466 SLoc = ND->getLocation();
467 break;
468 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000469 }
470 if (SLoc.isInvalid())
471 return SourceLocation();
472 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
473 }
474}
475
Daniel Dunbar140fce22010-01-12 02:34:07 +0000476CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000477 CXString Str;
478 if (DupString) {
479 Str.Spelling = strdup(String);
480 Str.MustFreeString = 1;
481 } else {
482 Str.Spelling = String;
483 Str.MustFreeString = 0;
484 }
485 return Str;
486}
487
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000488extern "C" {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000489CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000490 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000491 CIndexer *CIdxr = new CIndexer(new Program());
492 if (excludeDeclarationsFromPCH)
493 CIdxr->setOnlyLocalDecls();
494 if (displayDiagnostics)
495 CIdxr->setDisplayDiagnostics();
496 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000497}
498
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000499void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000500 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000501 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000502}
503
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000504void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
505 assert(CIdx && "Passed null CXIndex");
506 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
507 CXXIdx->setUseExternalASTGeneration(value);
508}
509
Steve Naroff50398192009-08-28 15:28:48 +0000510// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000511CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
512 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000513 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000514 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000515
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000516 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
517 CXXIdx->getOnlyLocalDecls(),
518 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000519}
520
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000521CXTranslationUnit
522clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
523 const char *source_filename,
524 int num_command_line_args,
525 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000526 assert(CIdx && "Passed null CXIndex");
527 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
528
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000529 if (!CXXIdx->getUseExternalASTGeneration()) {
530 llvm::SmallVector<const char *, 16> Args;
531
532 // The 'source_filename' argument is optional. If the caller does not
533 // specify it then it is assumed that the source file is specified
534 // in the actual argument list.
535 if (source_filename)
536 Args.push_back(source_filename);
537 Args.insert(Args.end(), command_line_args,
538 command_line_args + num_command_line_args);
539
Daniel Dunbar94220972009-12-05 02:17:18 +0000540 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000541
Ted Kremenek29b72842010-01-07 22:49:05 +0000542#ifdef USE_CRASHTRACER
543 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000544#endif
545
Daniel Dunbar94220972009-12-05 02:17:18 +0000546 llvm::OwningPtr<ASTUnit> Unit(
547 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000548 CXXIdx->getDiags(),
549 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000550 CXXIdx->getOnlyLocalDecls(),
551 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000552
Daniel Dunbar94220972009-12-05 02:17:18 +0000553 // FIXME: Until we have broader testing, just drop the entire AST if we
554 // encountered an error.
555 if (NumErrors != CXXIdx->getDiags().getNumErrors())
556 return 0;
557
558 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000559 }
560
Ted Kremenek139ba862009-10-22 00:03:57 +0000561 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000562 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000563
Ted Kremenek139ba862009-10-22 00:03:57 +0000564 // First add the complete path to the 'clang' executable.
565 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000566 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000567
Ted Kremenek139ba862009-10-22 00:03:57 +0000568 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000569 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000570
Ted Kremenek139ba862009-10-22 00:03:57 +0000571 // The 'source_filename' argument is optional. If the caller does not
572 // specify it then it is assumed that the source file is specified
573 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000574 if (source_filename)
575 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000576
Steve Naroff37b5ac22009-10-15 20:50:09 +0000577 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000578 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000579 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000580 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000581
582 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
583 for (int i = 0; i < num_command_line_args; ++i)
584 if (const char *arg = command_line_args[i]) {
585 if (strcmp(arg, "-o") == 0) {
586 ++i; // Also skip the matching argument.
587 continue;
588 }
589 if (strcmp(arg, "-emit-ast") == 0 ||
590 strcmp(arg, "-c") == 0 ||
591 strcmp(arg, "-fsyntax-only") == 0) {
592 continue;
593 }
594
595 // Keep the argument.
596 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000597 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000598
Ted Kremenek139ba862009-10-22 00:03:57 +0000599 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000600 argv.push_back(NULL);
601
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000602 // Invoke 'clang'.
603 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
604 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000605 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000606 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000607 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
608 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
609 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000610
Ted Kremenek0854d702009-11-10 19:18:52 +0000611 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000612 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000613 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000614 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000615 I!=E; ++I) {
616 if (*I)
617 llvm::errs() << ' ' << *I << '\n';
618 }
619 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000620 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000621
Steve Naroff37b5ac22009-10-15 20:50:09 +0000622 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000623 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000624 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000625 if (ATU)
626 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000627 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000628}
629
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000630void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000631 assert(CTUnit && "Passed null CXTranslationUnit");
632 delete static_cast<ASTUnit *>(CTUnit);
633}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000634
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000635CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000636 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000637 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek4b333d22010-01-12 00:36:38 +0000638 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
639 true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000640}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000641
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000642void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000643 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000644 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000645 assert(CTUnit && "Passed null CXTranslationUnit");
646 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
647 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000648
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000649 unsigned PCHLevel = Decl::MaxPCHLevel;
650
651 // Set the PCHLevel to filter out unwanted decls if requested.
652 if (CXXUnit->getOnlyLocalDecls()) {
653 PCHLevel = 0;
654
655 // If the main input was an AST, bump the level.
656 if (CXXUnit->isMainFileAST())
657 ++PCHLevel;
658 }
659
660 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000661
662 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
663 // decls.
664 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
665 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
666 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
667 ie = TLDs.end(); it != ie; ++it) {
668 DVisit.Visit(*it);
669 }
670 } else
671 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000672}
673
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000674void clang_loadDeclaration(CXDecl Dcl,
675 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000676 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000677 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000678
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000679 CDeclVisitor DVisit(Dcl, callback, CData,
680 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000681 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000682}
Ted Kremenekfb480492010-01-13 21:46:36 +0000683} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +0000684
Ted Kremenekfb480492010-01-13 21:46:36 +0000685//===----------------------------------------------------------------------===//
Steve Naroff600866c2009-08-27 19:51:58 +0000686// CXDecl Operations.
Ted Kremenekfb480492010-01-13 21:46:36 +0000687//===----------------------------------------------------------------------===//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000688
Ted Kremenekfb480492010-01-13 21:46:36 +0000689static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
690 SourceLocation SLoc) {
691 FileID FID;
692 if (SLoc.isFileID())
693 FID = SMgr.getFileID(SLoc);
694 else
695 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
696 return SMgr.getFileEntryForID(FID);
697}
698
699extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000700CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000701 assert(AnonDecl && "Passed null CXDecl");
702 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000703
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000704 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000705 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
706 true);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000707
708 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000709 // No, this isn't the same as the code below. getIdentifier() is non-virtual
710 // and returns different names. NamedDecl returns the class name and
711 // ObjCCategoryImplDecl returns the category name.
Ted Kremenek4b333d22010-01-12 00:36:38 +0000712 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000713
714 if (ND->getIdentifier())
Ted Kremenek4b333d22010-01-12 00:36:38 +0000715 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000716
Ted Kremenek4b333d22010-01-12 00:36:38 +0000717 return CIndexer::createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000718}
Steve Narofff334b4e2009-09-02 18:26:48 +0000719
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000720unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000721 assert(AnonDecl && "Passed null CXDecl");
722 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
723 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
724 return SourceMgr.getSpellingLineNumber(ND->getLocation());
725}
726
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000727unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000728 assert(AnonDecl && "Passed null CXDecl");
729 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
730 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000731 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000732}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000733
734CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
735 assert(AnonDecl && "Passed null CXDecl");
736 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekd8210652010-01-06 23:43:31 +0000737 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000738 SourceRange R = ND->getSourceRange();
739
Ted Kremenekd8210652010-01-06 23:43:31 +0000740 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
741 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
742
743 if (!Begin.isValid()) {
744 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
745 return extent;
746 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000747
Ted Kremenekd8210652010-01-06 23:43:31 +0000748 // FIXME: This is largely copy-paste from
749 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
750 // what we want the two routines should be refactored.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000751
Ted Kremenekd8210652010-01-06 23:43:31 +0000752 // If the End location and the start location are the same and are a macro
753 // location, then the range was something that came from a macro expansion
754 // or _Pragma. If this is an object-like macro, the best we can do is to
755 // get the range. If this is a function-like macro, we'd also like to
756 // get the arguments.
757 if (Begin == End && R.getEnd().isMacroID())
758 End = SM.getInstantiationRange(R.getEnd()).second;
759
760 assert(SM.getFileID(Begin) == SM.getFileID(End));
761 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
762 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000763
Ted Kremenekd8210652010-01-06 23:43:31 +0000764 // Compute the column number of the start. Keep the column based at 1.
765 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
766
767 // Compute the column number of the end.
768 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
769 if (EndColNo) {
770 // Offset the end column by 1 so that we point to the last character
771 // in the last token.
772 --EndColNo;
773
774 // Add in the length of the token, so that we cover multi-char tokens.
775 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
776 const LangOptions &LOpts = Ctx.getLangOptions();
777
778 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
779 }
780
781 // Package up the line/column data and return to the caller.
782 CXDeclExtent extent = { { StartLineNo, StartColNo },
783 { EndLineNo, EndColNo } };
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000784 return extent;
785}
Steve Naroff699a07d2009-09-25 21:32:34 +0000786
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000787const char *clang_getDeclSource(CXDecl AnonDecl) {
788 assert(AnonDecl && "Passed null CXDecl");
789 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
790 assert(FEnt && "Cannot find FileEntry for Decl");
791 return clang_getFileName(FEnt);
792}
793
Steve Naroff88145032009-10-27 14:35:18 +0000794
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000795CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000796 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000797 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
798 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000799 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
800}
Ted Kremenekfb480492010-01-13 21:46:36 +0000801} // end: extern "C"
Steve Naroff88145032009-10-27 14:35:18 +0000802
Ted Kremenekfb480492010-01-13 21:46:36 +0000803//===----------------------------------------------------------------------===//
804// CXFile Operations.
805//===----------------------------------------------------------------------===//
806
807extern "C" {
Steve Naroff88145032009-10-27 14:35:18 +0000808const char *clang_getFileName(CXFile SFile) {
809 assert(SFile && "Passed null CXFile");
810 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
811 return FEnt->getName();
812}
813
814time_t clang_getFileTime(CXFile SFile) {
815 assert(SFile && "Passed null CXFile");
816 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
817 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000818}
Ted Kremenekfb480492010-01-13 21:46:36 +0000819} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +0000820
Ted Kremenekfb480492010-01-13 21:46:36 +0000821//===----------------------------------------------------------------------===//
822// CXCursor Operations.
823//===----------------------------------------------------------------------===//
824
825static enum CXCursorKind TranslateKind(Decl *D) {
826 switch (D->getKind()) {
827 case Decl::Function: return CXCursor_FunctionDecl;
828 case Decl::Typedef: return CXCursor_TypedefDecl;
829 case Decl::Enum: return CXCursor_EnumDecl;
830 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
831 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
832 case Decl::Field: return CXCursor_FieldDecl;
833 case Decl::Var: return CXCursor_VarDecl;
834 case Decl::ParmVar: return CXCursor_ParmDecl;
835 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
836 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
837 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
838 case Decl::ObjCMethod: {
839 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
840 if (MD->isInstanceMethod())
841 return CXCursor_ObjCInstanceMethodDecl;
842 return CXCursor_ObjCClassMethodDecl;
843 }
844 default: break;
845 }
846 return CXCursor_NotImplemented;
847}
848
849
850static CXCursor MakeCXCursor(CXCursorKind K, Decl *D) {
851 CXCursor C = { K, D, 0, 0 };
852 return C;
853}
854
855static CXCursor MakeCXCursor(CXCursorKind K, Decl *D, Stmt *S) {
856 assert(clang_isReference(K));
857 CXCursor C = { K, D, S, 0 };
858 return C;
859}
860
861static Decl *getDeclFromExpr(Stmt *E) {
862 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
863 return RefExpr->getDecl();
864 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
865 return ME->getMemberDecl();
866 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
867 return RE->getDecl();
868
869 if (CallExpr *CE = dyn_cast<CallExpr>(E))
870 return getDeclFromExpr(CE->getCallee());
871 if (CastExpr *CE = dyn_cast<CastExpr>(E))
872 return getDeclFromExpr(CE->getSubExpr());
873 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
874 return OME->getMethodDecl();
875
876 return 0;
877}
878
879extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000880CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000881 assert(C.decl && "CXCursor has null decl");
882 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000883
Steve Narofff334b4e2009-09-02 18:26:48 +0000884 if (clang_isReference(C.kind)) {
885 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000886 case CXCursor_ObjCSuperClassRef: {
887 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
888 assert(OID && "clang_getCursorLine(): Missing interface decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000889 return CIndexer::createCXString(OID->getSuperClass()->getIdentifier()
890 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000891 }
892 case CXCursor_ObjCClassRef: {
893 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000894 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000895
896 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
897 assert(OCD && "clang_getCursorLine(): Missing category decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000898 return CIndexer::createCXString(OCD->getClassInterface()->getIdentifier()
Daniel Dunbaracca7252009-11-30 20:42:49 +0000899 ->getNameStart());
900 }
901 case CXCursor_ObjCProtocolRef: {
902 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
903 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000904 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000905 }
906 case CXCursor_ObjCSelectorRef: {
907 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
908 static_cast<Stmt *>(C.stmt));
909 assert(OME && "clang_getCursorLine(): Missing message expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000910 return CIndexer::createCXString(OME->getSelector().getAsString().c_str(),
911 true);
Daniel Dunbaracca7252009-11-30 20:42:49 +0000912 }
913 case CXCursor_VarRef:
914 case CXCursor_FunctionRef:
915 case CXCursor_EnumConstantRef: {
916 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
917 static_cast<Stmt *>(C.stmt));
918 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000919 return CIndexer::createCXString(DRE->getDecl()->getIdentifier()
920 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000921 }
922 default:
Ted Kremenek4b333d22010-01-12 00:36:38 +0000923 return CIndexer::createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000924 }
925 }
926 return clang_getDeclSpelling(C.decl);
927}
928
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000929const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000930 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000931 case CXCursor_FunctionDecl: return "FunctionDecl";
932 case CXCursor_TypedefDecl: return "TypedefDecl";
933 case CXCursor_EnumDecl: return "EnumDecl";
934 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
935 case CXCursor_StructDecl: return "StructDecl";
936 case CXCursor_UnionDecl: return "UnionDecl";
937 case CXCursor_ClassDecl: return "ClassDecl";
938 case CXCursor_FieldDecl: return "FieldDecl";
939 case CXCursor_VarDecl: return "VarDecl";
940 case CXCursor_ParmDecl: return "ParmDecl";
941 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
942 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
943 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
944 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
945 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
946 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
947 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
948 case CXCursor_FunctionDefn: return "FunctionDefn";
949 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
950 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
951 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
952 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
953 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
954 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
955 case CXCursor_ObjCClassRef: return "ObjCClassRef";
956 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000957
Daniel Dunbaracca7252009-11-30 20:42:49 +0000958 case CXCursor_VarRef: return "VarRef";
959 case CXCursor_FunctionRef: return "FunctionRef";
960 case CXCursor_EnumConstantRef: return "EnumConstantRef";
961 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000962
Daniel Dunbaracca7252009-11-30 20:42:49 +0000963 case CXCursor_InvalidFile: return "InvalidFile";
964 case CXCursor_NoDeclFound: return "NoDeclFound";
965 case CXCursor_NotImplemented: return "NotImplemented";
966 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000967 }
Steve Naroff600866c2009-08-27 19:51:58 +0000968}
Steve Naroff89922f82009-08-31 00:59:03 +0000969
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000970CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000971 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000972 assert(CTUnit && "Passed null CXTranslationUnit");
973 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000974
Steve Naroff9efa7672009-09-04 15:44:05 +0000975 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000976 const FileEntry *File = FMgr.getFile(source_name,
977 source_name+strlen(source_name));
Ted Kremenekf4629892010-01-14 01:51:23 +0000978 if (!File)
979 return clang_getNullCursor();
980
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000981 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000982 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000983
Steve Narofff96b5242009-10-28 20:44:47 +0000984 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000985 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000986 &LastLoc);
Ted Kremenekf4629892010-01-14 01:51:23 +0000987
988 // FIXME: This doesn't look thread-safe.
Steve Narofff96b5242009-10-28 20:44:47 +0000989 if (ALoc.isValid())
990 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000991
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000992 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000993 if (ALoc.isNamedRef())
994 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000995 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000996 if (Dcl) {
997 if (Stm) {
Ted Kremenekfb480492010-01-13 21:46:36 +0000998 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm))
999 return MakeCXCursor(TranslateDeclRefExpr(DRE), Dcl, Stm);
1000 else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm))
1001 return MakeCXCursor(CXCursor_ObjCSelectorRef, Dcl, MExp);
Steve Naroff4ade6d62009-09-23 17:52:52 +00001002 // Fall through...treat as a decl, not a ref.
1003 }
Steve Naroff85e2db72009-10-01 00:31:07 +00001004 if (ALoc.isNamedRef()) {
1005 if (isa<ObjCInterfaceDecl>(Dcl)) {
Ted Kremenekb89e8f62010-01-13 19:59:20 +00001006 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl(), 0 };
Steve Naroff85e2db72009-10-01 00:31:07 +00001007 return C;
1008 }
1009 if (isa<ObjCProtocolDecl>(Dcl)) {
Ted Kremenekb89e8f62010-01-13 19:59:20 +00001010 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl(), 0 };
Steve Naroff85e2db72009-10-01 00:31:07 +00001011 return C;
1012 }
1013 }
Ted Kremenekfb480492010-01-13 21:46:36 +00001014 return MakeCXCursor(TranslateKind(Dcl), Dcl);
Steve Naroff77128dd2009-09-15 20:25:34 +00001015 }
Ted Kremenekfb480492010-01-13 21:46:36 +00001016 return MakeCXCursor(CXCursor_NoDeclFound, 0);
Steve Naroff600866c2009-08-27 19:51:58 +00001017}
1018
Ted Kremenek73885552009-11-17 19:28:59 +00001019CXCursor clang_getNullCursor(void) {
Ted Kremenekfb480492010-01-13 21:46:36 +00001020 return MakeCXCursor(CXCursor_InvalidFile, 0);
Ted Kremenek73885552009-11-17 19:28:59 +00001021}
1022
1023unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Ted Kremenekfb480492010-01-13 21:46:36 +00001024 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt &&
1025 X.referringDecl == Y.referringDecl;
Ted Kremenek73885552009-11-17 19:28:59 +00001026}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001027
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001028CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001029 assert(AnonDecl && "Passed null CXDecl");
1030 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekfb480492010-01-13 21:46:36 +00001031 return MakeCXCursor(TranslateKind(ND), ND);
Steve Naroff77128dd2009-09-15 20:25:34 +00001032}
1033
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001034unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001035 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1036}
1037
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001038unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001039 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1040}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001041
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001042unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001043 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1044}
1045
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001046unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001047 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
1048}
1049
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001050CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001051 return C.kind;
1052}
1053
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001054CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +00001055 if (clang_isDeclaration(C.kind))
1056 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001057
Steve Naroff699a07d2009-09-25 21:32:34 +00001058 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +00001059 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001060 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +00001061 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +00001062 return static_cast<Stmt *>(C.stmt);
1063 else
1064 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
1065 } else
Steve Naroff699a07d2009-09-25 21:32:34 +00001066 return C.decl;
1067 }
1068 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +00001069}
1070
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001071unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001072 assert(C.decl && "CXCursor has null decl");
1073 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001074 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001075
Steve Narofff334b4e2009-09-02 18:26:48 +00001076 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001077 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001078}
Ted Kremenekfb480492010-01-13 21:46:36 +00001079
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001080unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001081 assert(C.decl && "CXCursor has null decl");
1082 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001083 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001084
Steve Narofff334b4e2009-09-02 18:26:48 +00001085 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001086 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001087}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001088
1089const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001090 assert(C.decl && "CXCursor has null decl");
1091 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001092 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001093
Steve Narofff334b4e2009-09-02 18:26:48 +00001094 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Ted Kremenekfb480492010-01-13 21:46:36 +00001095
Ted Kremenek9298cfc2009-11-17 05:31:58 +00001096 if (SLoc.isFileID()) {
1097 const char *bufferName = SourceMgr.getBufferName(SLoc);
1098 return bufferName[0] == '<' ? NULL : bufferName;
1099 }
Ted Kremenekfb480492010-01-13 21:46:36 +00001100
Douglas Gregor02465752009-10-16 21:24:31 +00001101 // Retrieve the file in which the macro was instantiated, then provide that
1102 // buffer name.
1103 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001104 const llvm::MemoryBuffer *Buffer
Ted Kremenekfb480492010-01-13 21:46:36 +00001105 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
Douglas Gregor02465752009-10-16 21:24:31 +00001106 if (!Buffer)
1107 return 0;
Ted Kremenekfb480492010-01-13 21:46:36 +00001108
Douglas Gregor02465752009-10-16 21:24:31 +00001109 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +00001110}
1111
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001112CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +00001113 assert(C.decl && "CXCursor has null decl");
1114 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1115 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001116
Ted Kremenek4b333d22010-01-12 00:36:38 +00001117 return (void *)
Ted Kremenekfb480492010-01-13 21:46:36 +00001118 getFileEntryFromSourceLocation(SourceMgr, getLocationFromCursor(C,SourceMgr,
1119 ND));
Steve Naroff88145032009-10-27 14:35:18 +00001120}
1121
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001122void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001123 const char **startBuf,
1124 const char **endBuf,
1125 unsigned *startLine,
1126 unsigned *startColumn,
1127 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001128 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +00001129 assert(C.decl && "CXCursor has null decl");
1130 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1131 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1132 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekfb480492010-01-13 21:46:36 +00001133
Steve Naroff4ade6d62009-09-23 17:52:52 +00001134 SourceManager &SM = FD->getASTContext().getSourceManager();
1135 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1136 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1137 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1138 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1139 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1140 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1141}
Ted Kremenekfb480492010-01-13 21:46:36 +00001142
1143} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001144
Ted Kremenekfb480492010-01-13 21:46:36 +00001145//===----------------------------------------------------------------------===//
1146// CXString Operations.
1147//===----------------------------------------------------------------------===//
1148
1149extern "C" {
1150const char *clang_getCString(CXString string) {
1151 return string.Spelling;
1152}
1153
1154void clang_disposeString(CXString string) {
1155 if (string.MustFreeString && string.Spelling)
1156 free((void*)string.Spelling);
1157}
1158} // end: extern "C"