blob: c1f05a8bc0320de5e3882541861f13942e693cb3 [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"
Ted Kremenek16c440a2010-01-15 20:35:54 +000016#include "CXCursor.h"
Ted Kremenekab188932010-01-05 19:32:54 +000017
Steve Naroff50398192009-08-28 15:28:48 +000018#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000019#include "clang/AST/StmtVisitor.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000020#include "clang/Lex/Lexer.h"
Douglas Gregor02465752009-10-16 21:24:31 +000021#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000022#include "llvm/System/Program.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000023
Ted Kremenekdb3d0da2010-01-05 20:55:39 +000024// Needed to define L_TMPNAM on some systems.
25#include <cstdio>
26
Steve Naroff50398192009-08-28 15:28:48 +000027using namespace clang;
Ted Kremenek16c440a2010-01-15 20:35:54 +000028using namespace clang::cxcursor;
Steve Naroff50398192009-08-28 15:28:48 +000029using namespace idx;
30
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000031//===----------------------------------------------------------------------===//
32// Crash Reporting.
33//===----------------------------------------------------------------------===//
34
35#ifdef __APPLE__
Ted Kremenek29b72842010-01-07 22:49:05 +000036#ifndef NDEBUG
37#define USE_CRASHTRACER
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000038#include "clang/Analysis/Support/SaveAndRestore.h"
39// Integrate with crash reporter.
40extern "C" const char *__crashreporter_info__;
Ted Kremenek29b72842010-01-07 22:49:05 +000041#define NUM_CRASH_STRINGS 16
42static unsigned crashtracer_counter = 0;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000043static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek29b72842010-01-07 22:49:05 +000044static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
45static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
46
47static unsigned SetCrashTracerInfo(const char *str,
48 llvm::SmallString<1024> &AggStr) {
49
Ted Kremenek254ba7c2010-01-07 23:13:53 +000050 unsigned slot = 0;
Ted Kremenek29b72842010-01-07 22:49:05 +000051 while (crashtracer_strings[slot]) {
52 if (++slot == NUM_CRASH_STRINGS)
53 slot = 0;
54 }
55 crashtracer_strings[slot] = str;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000056 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek29b72842010-01-07 22:49:05 +000057
58 // We need to create an aggregate string because multiple threads
59 // may be in this method at one time. The crash reporter string
60 // will attempt to overapproximate the set of in-flight invocations
61 // of this function. Race conditions can still cause this goal
62 // to not be achieved.
63 {
64 llvm::raw_svector_ostream Out(AggStr);
65 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
66 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
67 }
68 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
69 return slot;
70}
71
72static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek254ba7c2010-01-07 23:13:53 +000073 unsigned max_slot = 0;
74 unsigned max_value = 0;
75
76 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
77
78 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
79 if (agg_crashtracer_strings[i] &&
80 crashtracer_counter_id[i] > max_value) {
81 max_slot = i;
82 max_value = crashtracer_counter_id[i];
Ted Kremenek29b72842010-01-07 22:49:05 +000083 }
Ted Kremenek254ba7c2010-01-07 23:13:53 +000084
85 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek29b72842010-01-07 22:49:05 +000086}
87
88namespace {
89class ArgsCrashTracerInfo {
90 llvm::SmallString<1024> CrashString;
91 llvm::SmallString<1024> AggregateString;
92 unsigned crashtracerSlot;
93public:
94 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
95 : crashtracerSlot(0)
96 {
97 {
98 llvm::raw_svector_ostream Out(CrashString);
99 Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
100 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
101 E=Args.end(); I!=E; ++I)
102 Out << ' ' << *I;
103 }
104 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
105 AggregateString);
106 }
107
108 ~ArgsCrashTracerInfo() {
109 ResetCrashTracerInfo(crashtracerSlot);
110 }
111};
112}
113#endif
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000114#endif
115
116//===----------------------------------------------------------------------===//
117// Visitors.
118//===----------------------------------------------------------------------===//
119
Steve Naroff89922f82009-08-31 00:59:03 +0000120namespace {
Ted Kremenekab188932010-01-05 19:32:54 +0000121static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000122 NamedDecl *D = DRE->getDecl();
123 if (isa<VarDecl>(D))
124 return CXCursor_VarRef;
125 else if (isa<FunctionDecl>(D))
126 return CXCursor_FunctionRef;
127 else if (isa<EnumConstantDecl>(D))
128 return CXCursor_EnumConstantRef;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000129 else
Steve Naroff4ade6d62009-09-23 17:52:52 +0000130 return CXCursor_NotImplemented;
131}
132
133#if 0
134// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +0000135class CRefVisitor : public StmtVisitor<CRefVisitor> {
136 CXDecl CDecl;
137 CXDeclIterator Callback;
138 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000139
Steve Narofffb570422009-09-22 19:25:29 +0000140 void Call(enum CXCursorKind CK, Stmt *SRef) {
141 CXCursor C = { CK, CDecl, SRef };
142 Callback(CDecl, C, CData);
143 }
144
145public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000146 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Narofffb570422009-09-22 19:25:29 +0000147 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000148
Steve Narofffb570422009-09-22 19:25:29 +0000149 void VisitStmt(Stmt *S) {
150 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
151 C != CEnd; ++C)
152 Visit(*C);
153 }
154 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000155 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +0000156 }
157 void VisitMemberExpr(MemberExpr *Node) {
158 Call(CXCursor_MemberRef, Node);
159 }
160 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
161 Call(CXCursor_ObjCSelectorRef, Node);
162 }
163 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
164 Call(CXCursor_ObjCIvarRef, Node);
165 }
166};
Steve Naroff4ade6d62009-09-23 17:52:52 +0000167#endif
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000168
Steve Naroff89922f82009-08-31 00:59:03 +0000169// Translation Unit Visitor.
Ted Kremenekf1286182010-01-13 00:13:47 +0000170
Steve Naroff89922f82009-08-31 00:59:03 +0000171class TUVisitor : public DeclVisitor<TUVisitor> {
Ted Kremenekf1286182010-01-13 00:13:47 +0000172public:
173 typedef void (*Iterator)(void *, CXCursor, CXClientData);
174private:
175 void *Root; // CXDecl or CXTranslationUnit
176 Iterator Callback; // CXTranslationUnitIterator or CXDeclIterator.
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000177 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000178
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000179 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
180 // to the visitor. Declarations with a PCH level greater than this value will
181 // be suppressed.
182 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000183
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000184 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000185 // Filter any declarations that have a PCH level greater than what we allow.
186 if (ND->getPCHLevel() > MaxPCHLevel)
187 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000188
Steve Narofff96b5242009-10-28 20:44:47 +0000189 // Filter any implicit declarations (since the source info will be bogus).
190 if (ND->isImplicit())
191 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000192
Ted Kremenekb89e8f62010-01-13 19:59:20 +0000193 CXCursor C = { CK, ND, 0, 0 };
Ted Kremenekf1286182010-01-13 00:13:47 +0000194 Callback(Root, C, CData);
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000195 }
Ted Kremenekf1286182010-01-13 00:13:47 +0000196
Steve Naroff89922f82009-08-31 00:59:03 +0000197public:
Ted Kremenekf1286182010-01-13 00:13:47 +0000198 TUVisitor(void *root, Iterator cback, CXClientData D, unsigned MaxPCHLevel) :
199 Root(root), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000200
Ted Kremenekf1286182010-01-13 00:13:47 +0000201 void VisitDeclContext(DeclContext *DC);
202 void VisitFunctionDecl(FunctionDecl *ND);
203 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
204 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND);
205 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND);
206 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND);
207 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND);
208 void VisitTagDecl(TagDecl *ND);
209 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
210 void VisitTypedefDecl(TypedefDecl *ND);
211 void VisitVarDecl(VarDecl *ND);
212};
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000213
Ted Kremenekf1286182010-01-13 00:13:47 +0000214void TUVisitor::VisitDeclContext(DeclContext *DC) {
215 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
216 I != E; ++I)
217 Visit(*I);
218}
219
220void TUVisitor::VisitFunctionDecl(FunctionDecl *ND) {
221 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
222 : CXCursor_FunctionDecl, ND);
223}
224
225void TUVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
226 Call(CXCursor_ObjCCategoryDecl, ND);
227}
228
229void TUVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
230 Call(CXCursor_ObjCCategoryDefn, ND);
231}
232
233void TUVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
234 Call(CXCursor_ObjCClassDefn, ND);
235}
236
237void TUVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
238 Call(CXCursor_ObjCInterfaceDecl, ND);
239}
240
241void TUVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
242 Call(CXCursor_ObjCProtocolDecl, ND);
243}
244
245void TUVisitor::VisitTagDecl(TagDecl *ND) {
246 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000247 case TagDecl::TK_struct:
248 Call(CXCursor_StructDecl, ND);
249 break;
250 case TagDecl::TK_class:
251 Call(CXCursor_ClassDecl, ND);
252 break;
253 case TagDecl::TK_union:
254 Call(CXCursor_UnionDecl, ND);
255 break;
256 case TagDecl::TK_enum:
257 Call(CXCursor_EnumDecl, ND);
258 break;
Steve Naroff89922f82009-08-31 00:59:03 +0000259 }
Ted Kremenekf1286182010-01-13 00:13:47 +0000260}
261
262void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
263 VisitDeclContext(dyn_cast<DeclContext>(D));
264}
265
266void TUVisitor::VisitTypedefDecl(TypedefDecl *ND) {
267 Call(CXCursor_TypedefDecl, ND);
268}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000269
Ted Kremenekf1286182010-01-13 00:13:47 +0000270void TUVisitor::VisitVarDecl(VarDecl *ND) {
271 Call(CXCursor_VarDecl, ND);
272}
Steve Naroff89922f82009-08-31 00:59:03 +0000273
Steve Naroffc857ea42009-09-02 13:28:54 +0000274// Declaration visitor.
275class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
276 CXDecl CDecl;
277 CXDeclIterator Callback;
278 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000279
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000280 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
281 // to the visitor. Declarations with a PCH level greater than this value will
282 // be suppressed.
283 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000284
Steve Naroffc857ea42009-09-02 13:28:54 +0000285 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000286 // Disable the callback when the context is equal to the visiting decl.
287 if (CDecl == ND && !clang_isReference(CK))
288 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000289
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000290 // Filter any declarations that have a PCH level greater than what we allow.
291 if (ND->getPCHLevel() > MaxPCHLevel)
292 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000293
Ted Kremenekb89e8f62010-01-13 19:59:20 +0000294 CXCursor C = { CK, ND, 0, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000295 Callback(CDecl, C, CData);
296 }
Steve Naroff89922f82009-08-31 00:59:03 +0000297public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000298 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
299 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000300 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000301
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000302 void VisitDeclContext(DeclContext *DC);
303 void VisitEnumConstantDecl(EnumConstantDecl *ND);
304 void VisitFieldDecl(FieldDecl *ND);
305 void VisitFunctionDecl(FunctionDecl *ND);
306 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
307 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
308 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
309 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
310 void VisitObjCIvarDecl(ObjCIvarDecl *ND);
311 void VisitObjCMethodDecl(ObjCMethodDecl *ND);
312 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND);
313 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
314 void VisitParmVarDecl(ParmVarDecl *ND);
315 void VisitTagDecl(TagDecl *D);
316 void VisitVarDecl(VarDecl *ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000317};
Ted Kremenekab188932010-01-05 19:32:54 +0000318} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000319
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000320void CDeclVisitor::VisitDeclContext(DeclContext *DC) {
321 for (DeclContext::decl_iterator
322 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
323 Visit(*I);
324}
325
326void CDeclVisitor::VisitEnumConstantDecl(EnumConstantDecl *ND) {
327 Call(CXCursor_EnumConstantDecl, ND);
328}
329
330void CDeclVisitor::VisitFieldDecl(FieldDecl *ND) {
331 Call(CXCursor_FieldDecl, ND);
332}
333
334void CDeclVisitor::VisitFunctionDecl(FunctionDecl *ND) {
335 if (ND->isThisDeclarationADefinition()) {
336 VisitDeclContext(dyn_cast<DeclContext>(ND));
337#if 0
338 // Not currently needed.
339 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
340 CRefVisitor RVisit(CDecl, Callback, CData);
341 RVisit.Visit(Body);
342#endif
343 }
344}
345
346void CDeclVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
347 // Issue callbacks for the containing class.
348 Call(CXCursor_ObjCClassRef, ND);
349 // FIXME: Issue callbacks for protocol refs.
350 VisitDeclContext(dyn_cast<DeclContext>(ND));
351}
352
353void CDeclVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
354 VisitDeclContext(dyn_cast<DeclContext>(D));
355}
356
357void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
358 VisitDeclContext(dyn_cast<DeclContext>(D));
359}
360
361void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
362 // Issue callbacks for super class.
363 if (D->getSuperClass())
364 Call(CXCursor_ObjCSuperClassRef, D);
365
366 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
367 E = D->protocol_end(); I != E; ++I)
368 Call(CXCursor_ObjCProtocolRef, *I);
369 VisitDeclContext(dyn_cast<DeclContext>(D));
370}
371
372void CDeclVisitor::VisitObjCIvarDecl(ObjCIvarDecl *ND) {
373 Call(CXCursor_ObjCIvarDecl, ND);
374}
375
376void CDeclVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
377 if (ND->getBody()) {
378 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
379 : CXCursor_ObjCClassMethodDefn, ND);
380 VisitDeclContext(dyn_cast<DeclContext>(ND));
381 } else
382 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
383 : CXCursor_ObjCClassMethodDecl, ND);
384}
385
386void CDeclVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
387 Call(CXCursor_ObjCPropertyDecl, ND);
388}
389
390void CDeclVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
391 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
392 E = PID->protocol_end(); I != E; ++I)
393 Call(CXCursor_ObjCProtocolRef, *I);
394
395 VisitDeclContext(dyn_cast<DeclContext>(PID));
396}
397
398void CDeclVisitor::VisitParmVarDecl(ParmVarDecl *ND) {
399 Call(CXCursor_ParmDecl, ND);
400}
401
402void CDeclVisitor::VisitTagDecl(TagDecl *D) {
403 VisitDeclContext(dyn_cast<DeclContext>(D));
404}
405
406void CDeclVisitor::VisitVarDecl(VarDecl *ND) {
407 Call(CXCursor_VarDecl, ND);
408}
409
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000410static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000411 SourceManager &SourceMgr,
412 NamedDecl *ND) {
413 if (clang_isReference(C.kind)) {
Ted Kremenek8b456e82010-01-15 18:24:18 +0000414
415 if (Decl *D = static_cast<Decl*>(C.referringDecl))
416 return D->getLocation();
417
Daniel Dunbarc6190332009-11-08 04:13:53 +0000418 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000419 case CXCursor_ObjCClassRef: {
420 if (isa<ObjCInterfaceDecl>(ND)) {
421 // FIXME: This is a hack (storing the parent decl in the stmt slot).
422 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
423 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000424 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000425 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
426 assert(OID && "clang_getCursorLine(): Missing category decl");
427 return OID->getClassInterface()->getLocation();
428 }
429 case CXCursor_ObjCSuperClassRef: {
430 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
431 assert(OID && "clang_getCursorLine(): Missing interface decl");
432 return OID->getSuperClassLoc();
433 }
434 case CXCursor_ObjCProtocolRef: {
435 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
436 assert(OID && "clang_getCursorLine(): Missing protocol decl");
437 return OID->getLocation();
438 }
439 case CXCursor_ObjCSelectorRef: {
440 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
441 static_cast<Stmt *>(C.stmt));
442 assert(OME && "clang_getCursorLine(): Missing message expr");
443 return OME->getLeftLoc(); /* FIXME: should be a range */
444 }
445 case CXCursor_VarRef:
446 case CXCursor_FunctionRef:
447 case CXCursor_EnumConstantRef: {
448 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
449 static_cast<Stmt *>(C.stmt));
450 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
451 return DRE->getLocation();
452 }
453 default:
454 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000455 }
456 } else { // We have a declaration or a definition.
457 SourceLocation SLoc;
458 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000459 case Decl::ObjCInterface: {
460 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
461 break;
462 }
463 case Decl::ObjCProtocol: {
464 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
465 break;
466 }
467 default: {
468 SLoc = ND->getLocation();
469 break;
470 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000471 }
472 if (SLoc.isInvalid())
473 return SourceLocation();
474 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
475 }
476}
477
Daniel Dunbar140fce22010-01-12 02:34:07 +0000478CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000479 CXString Str;
480 if (DupString) {
481 Str.Spelling = strdup(String);
482 Str.MustFreeString = 1;
483 } else {
484 Str.Spelling = String;
485 Str.MustFreeString = 0;
486 }
487 return Str;
488}
489
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000490extern "C" {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000491CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000492 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000493 CIndexer *CIdxr = new CIndexer(new Program());
494 if (excludeDeclarationsFromPCH)
495 CIdxr->setOnlyLocalDecls();
496 if (displayDiagnostics)
497 CIdxr->setDisplayDiagnostics();
498 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000499}
500
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000501void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000502 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000503 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000504}
505
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000506void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
507 assert(CIdx && "Passed null CXIndex");
508 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
509 CXXIdx->setUseExternalASTGeneration(value);
510}
511
Steve Naroff50398192009-08-28 15:28:48 +0000512// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000513CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
514 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000515 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000516 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000517
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000518 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
519 CXXIdx->getOnlyLocalDecls(),
520 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000521}
522
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000523CXTranslationUnit
524clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
525 const char *source_filename,
526 int num_command_line_args,
527 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000528 assert(CIdx && "Passed null CXIndex");
529 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
530
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000531 if (!CXXIdx->getUseExternalASTGeneration()) {
532 llvm::SmallVector<const char *, 16> Args;
533
534 // The 'source_filename' argument is optional. If the caller does not
535 // specify it then it is assumed that the source file is specified
536 // in the actual argument list.
537 if (source_filename)
538 Args.push_back(source_filename);
539 Args.insert(Args.end(), command_line_args,
540 command_line_args + num_command_line_args);
541
Daniel Dunbar94220972009-12-05 02:17:18 +0000542 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000543
Ted Kremenek29b72842010-01-07 22:49:05 +0000544#ifdef USE_CRASHTRACER
545 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000546#endif
547
Daniel Dunbar94220972009-12-05 02:17:18 +0000548 llvm::OwningPtr<ASTUnit> Unit(
549 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000550 CXXIdx->getDiags(),
551 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000552 CXXIdx->getOnlyLocalDecls(),
553 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000554
Daniel Dunbar94220972009-12-05 02:17:18 +0000555 // FIXME: Until we have broader testing, just drop the entire AST if we
556 // encountered an error.
557 if (NumErrors != CXXIdx->getDiags().getNumErrors())
558 return 0;
559
560 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000561 }
562
Ted Kremenek139ba862009-10-22 00:03:57 +0000563 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000564 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000565
Ted Kremenek139ba862009-10-22 00:03:57 +0000566 // First add the complete path to the 'clang' executable.
567 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000568 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000569
Ted Kremenek139ba862009-10-22 00:03:57 +0000570 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000571 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000572
Ted Kremenek139ba862009-10-22 00:03:57 +0000573 // The 'source_filename' argument is optional. If the caller does not
574 // specify it then it is assumed that the source file is specified
575 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000576 if (source_filename)
577 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000578
Steve Naroff37b5ac22009-10-15 20:50:09 +0000579 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000580 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000581 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000582 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000583
584 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
585 for (int i = 0; i < num_command_line_args; ++i)
586 if (const char *arg = command_line_args[i]) {
587 if (strcmp(arg, "-o") == 0) {
588 ++i; // Also skip the matching argument.
589 continue;
590 }
591 if (strcmp(arg, "-emit-ast") == 0 ||
592 strcmp(arg, "-c") == 0 ||
593 strcmp(arg, "-fsyntax-only") == 0) {
594 continue;
595 }
596
597 // Keep the argument.
598 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000599 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000600
Ted Kremenek139ba862009-10-22 00:03:57 +0000601 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000602 argv.push_back(NULL);
603
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000604 // Invoke 'clang'.
605 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
606 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000607 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000608 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000609 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
610 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
611 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000612
Ted Kremenek0854d702009-11-10 19:18:52 +0000613 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000614 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000615 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000616 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000617 I!=E; ++I) {
618 if (*I)
619 llvm::errs() << ' ' << *I << '\n';
620 }
621 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000622 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000623
Steve Naroff37b5ac22009-10-15 20:50:09 +0000624 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000625 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000626 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000627 if (ATU)
628 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000629 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000630}
631
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000632void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000633 assert(CTUnit && "Passed null CXTranslationUnit");
634 delete static_cast<ASTUnit *>(CTUnit);
635}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000636
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000637CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000638 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000639 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek4b333d22010-01-12 00:36:38 +0000640 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
641 true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000642}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000643
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000644void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000645 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000646 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000647 assert(CTUnit && "Passed null CXTranslationUnit");
648 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
649 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000650
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000651 unsigned PCHLevel = Decl::MaxPCHLevel;
652
653 // Set the PCHLevel to filter out unwanted decls if requested.
654 if (CXXUnit->getOnlyLocalDecls()) {
655 PCHLevel = 0;
656
657 // If the main input was an AST, bump the level.
658 if (CXXUnit->isMainFileAST())
659 ++PCHLevel;
660 }
661
662 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000663
664 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
665 // decls.
666 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
667 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
668 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
669 ie = TLDs.end(); it != ie; ++it) {
670 DVisit.Visit(*it);
671 }
672 } else
673 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000674}
675
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000676void clang_loadDeclaration(CXDecl Dcl,
677 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000678 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000679 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000680
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000681 CDeclVisitor DVisit(Dcl, callback, CData,
682 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000683 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000684}
Ted Kremenekfb480492010-01-13 21:46:36 +0000685} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +0000686
Ted Kremenekfb480492010-01-13 21:46:36 +0000687//===----------------------------------------------------------------------===//
Steve Naroff600866c2009-08-27 19:51:58 +0000688// CXDecl Operations.
Ted Kremenekfb480492010-01-13 21:46:36 +0000689//===----------------------------------------------------------------------===//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000690
Ted Kremenekfb480492010-01-13 21:46:36 +0000691static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
692 SourceLocation SLoc) {
693 FileID FID;
694 if (SLoc.isFileID())
695 FID = SMgr.getFileID(SLoc);
696 else
697 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
698 return SMgr.getFileEntryForID(FID);
699}
700
701extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000702CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000703 assert(AnonDecl && "Passed null CXDecl");
704 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000705
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000706 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000707 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
708 true);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000709
710 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000711 // No, this isn't the same as the code below. getIdentifier() is non-virtual
712 // and returns different names. NamedDecl returns the class name and
713 // ObjCCategoryImplDecl returns the category name.
Ted Kremenek4b333d22010-01-12 00:36:38 +0000714 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000715
716 if (ND->getIdentifier())
Ted Kremenek4b333d22010-01-12 00:36:38 +0000717 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000718
Ted Kremenek4b333d22010-01-12 00:36:38 +0000719 return CIndexer::createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000720}
Steve Narofff334b4e2009-09-02 18:26:48 +0000721
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000722unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000723 assert(AnonDecl && "Passed null CXDecl");
724 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
725 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
726 return SourceMgr.getSpellingLineNumber(ND->getLocation());
727}
728
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000729unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000730 assert(AnonDecl && "Passed null CXDecl");
731 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
732 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000733 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000734}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000735
736CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
737 assert(AnonDecl && "Passed null CXDecl");
738 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekd8210652010-01-06 23:43:31 +0000739 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000740 SourceRange R = ND->getSourceRange();
741
Ted Kremenekd8210652010-01-06 23:43:31 +0000742 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
743 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
744
745 if (!Begin.isValid()) {
746 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
747 return extent;
748 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000749
Ted Kremenekd8210652010-01-06 23:43:31 +0000750 // FIXME: This is largely copy-paste from
751 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
752 // what we want the two routines should be refactored.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000753
Ted Kremenekd8210652010-01-06 23:43:31 +0000754 // If the End location and the start location are the same and are a macro
755 // location, then the range was something that came from a macro expansion
756 // or _Pragma. If this is an object-like macro, the best we can do is to
757 // get the range. If this is a function-like macro, we'd also like to
758 // get the arguments.
759 if (Begin == End && R.getEnd().isMacroID())
760 End = SM.getInstantiationRange(R.getEnd()).second;
761
762 assert(SM.getFileID(Begin) == SM.getFileID(End));
763 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
764 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000765
Ted Kremenekd8210652010-01-06 23:43:31 +0000766 // Compute the column number of the start. Keep the column based at 1.
767 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
768
769 // Compute the column number of the end.
770 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
771 if (EndColNo) {
772 // Offset the end column by 1 so that we point to the last character
773 // in the last token.
774 --EndColNo;
775
776 // Add in the length of the token, so that we cover multi-char tokens.
777 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
778 const LangOptions &LOpts = Ctx.getLangOptions();
779
780 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
781 }
782
783 // Package up the line/column data and return to the caller.
784 CXDeclExtent extent = { { StartLineNo, StartColNo },
785 { EndLineNo, EndColNo } };
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000786 return extent;
787}
Steve Naroff699a07d2009-09-25 21:32:34 +0000788
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000789const char *clang_getDeclSource(CXDecl AnonDecl) {
790 assert(AnonDecl && "Passed null CXDecl");
791 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
792 assert(FEnt && "Cannot find FileEntry for Decl");
793 return clang_getFileName(FEnt);
794}
795
Steve Naroff88145032009-10-27 14:35:18 +0000796
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000797CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000798 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000799 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
800 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000801 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
802}
Ted Kremenekfb480492010-01-13 21:46:36 +0000803} // end: extern "C"
Steve Naroff88145032009-10-27 14:35:18 +0000804
Ted Kremenekfb480492010-01-13 21:46:36 +0000805//===----------------------------------------------------------------------===//
806// CXFile Operations.
807//===----------------------------------------------------------------------===//
808
809extern "C" {
Steve Naroff88145032009-10-27 14:35:18 +0000810const char *clang_getFileName(CXFile SFile) {
811 assert(SFile && "Passed null CXFile");
812 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
813 return FEnt->getName();
814}
815
816time_t clang_getFileTime(CXFile SFile) {
817 assert(SFile && "Passed null CXFile");
818 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
819 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000820}
Ted Kremenekfb480492010-01-13 21:46:36 +0000821} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +0000822
Ted Kremenekfb480492010-01-13 21:46:36 +0000823//===----------------------------------------------------------------------===//
824// CXCursor Operations.
825//===----------------------------------------------------------------------===//
826
827static enum CXCursorKind TranslateKind(Decl *D) {
828 switch (D->getKind()) {
829 case Decl::Function: return CXCursor_FunctionDecl;
830 case Decl::Typedef: return CXCursor_TypedefDecl;
831 case Decl::Enum: return CXCursor_EnumDecl;
832 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
833 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
834 case Decl::Field: return CXCursor_FieldDecl;
835 case Decl::Var: return CXCursor_VarDecl;
836 case Decl::ParmVar: return CXCursor_ParmDecl;
837 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
838 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
839 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
840 case Decl::ObjCMethod: {
841 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
842 if (MD->isInstanceMethod())
843 return CXCursor_ObjCInstanceMethodDecl;
844 return CXCursor_ObjCClassMethodDecl;
845 }
846 default: break;
847 }
848 return CXCursor_NotImplemented;
849}
850
Ted Kremenekfb480492010-01-13 21:46:36 +0000851static Decl *getDeclFromExpr(Stmt *E) {
852 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
853 return RefExpr->getDecl();
854 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
855 return ME->getMemberDecl();
856 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
857 return RE->getDecl();
858
859 if (CallExpr *CE = dyn_cast<CallExpr>(E))
860 return getDeclFromExpr(CE->getCallee());
861 if (CastExpr *CE = dyn_cast<CastExpr>(E))
862 return getDeclFromExpr(CE->getSubExpr());
863 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
864 return OME->getMethodDecl();
865
866 return 0;
867}
868
869extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000870CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000871 assert(C.decl && "CXCursor has null decl");
872 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000873
Steve Narofff334b4e2009-09-02 18:26:48 +0000874 if (clang_isReference(C.kind)) {
875 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000876 case CXCursor_ObjCSuperClassRef: {
877 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
878 assert(OID && "clang_getCursorLine(): Missing interface decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000879 return CIndexer::createCXString(OID->getSuperClass()->getIdentifier()
880 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000881 }
882 case CXCursor_ObjCClassRef: {
883 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000884 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000885
886 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
887 assert(OCD && "clang_getCursorLine(): Missing category decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000888 return CIndexer::createCXString(OCD->getClassInterface()->getIdentifier()
Daniel Dunbaracca7252009-11-30 20:42:49 +0000889 ->getNameStart());
890 }
891 case CXCursor_ObjCProtocolRef: {
892 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
893 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000894 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000895 }
896 case CXCursor_ObjCSelectorRef: {
897 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
898 static_cast<Stmt *>(C.stmt));
899 assert(OME && "clang_getCursorLine(): Missing message expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000900 return CIndexer::createCXString(OME->getSelector().getAsString().c_str(),
901 true);
Daniel Dunbaracca7252009-11-30 20:42:49 +0000902 }
903 case CXCursor_VarRef:
904 case CXCursor_FunctionRef:
905 case CXCursor_EnumConstantRef: {
906 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
907 static_cast<Stmt *>(C.stmt));
908 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000909 return CIndexer::createCXString(DRE->getDecl()->getIdentifier()
910 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000911 }
912 default:
Ted Kremenek4b333d22010-01-12 00:36:38 +0000913 return CIndexer::createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000914 }
915 }
916 return clang_getDeclSpelling(C.decl);
917}
918
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000919const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000920 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000921 case CXCursor_FunctionDecl: return "FunctionDecl";
922 case CXCursor_TypedefDecl: return "TypedefDecl";
923 case CXCursor_EnumDecl: return "EnumDecl";
924 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
925 case CXCursor_StructDecl: return "StructDecl";
926 case CXCursor_UnionDecl: return "UnionDecl";
927 case CXCursor_ClassDecl: return "ClassDecl";
928 case CXCursor_FieldDecl: return "FieldDecl";
929 case CXCursor_VarDecl: return "VarDecl";
930 case CXCursor_ParmDecl: return "ParmDecl";
931 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
932 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
933 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
934 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
935 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
936 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
937 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
938 case CXCursor_FunctionDefn: return "FunctionDefn";
939 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
940 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
941 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
942 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
943 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
944 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
945 case CXCursor_ObjCClassRef: return "ObjCClassRef";
946 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000947
Daniel Dunbaracca7252009-11-30 20:42:49 +0000948 case CXCursor_VarRef: return "VarRef";
949 case CXCursor_FunctionRef: return "FunctionRef";
950 case CXCursor_EnumConstantRef: return "EnumConstantRef";
951 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000952
Daniel Dunbaracca7252009-11-30 20:42:49 +0000953 case CXCursor_InvalidFile: return "InvalidFile";
954 case CXCursor_NoDeclFound: return "NoDeclFound";
955 case CXCursor_NotImplemented: return "NotImplemented";
956 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000957 }
Steve Naroff600866c2009-08-27 19:51:58 +0000958}
Steve Naroff89922f82009-08-31 00:59:03 +0000959
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000960CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000961 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000962 assert(CTUnit && "Passed null CXTranslationUnit");
963 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000964
Steve Naroff9efa7672009-09-04 15:44:05 +0000965 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000966 const FileEntry *File = FMgr.getFile(source_name,
967 source_name+strlen(source_name));
Ted Kremenekf4629892010-01-14 01:51:23 +0000968 if (!File)
969 return clang_getNullCursor();
970
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000971 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000972 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000973
Steve Narofff96b5242009-10-28 20:44:47 +0000974 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000975 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000976 &LastLoc);
Ted Kremenekf4629892010-01-14 01:51:23 +0000977
978 // FIXME: This doesn't look thread-safe.
Steve Narofff96b5242009-10-28 20:44:47 +0000979 if (ALoc.isValid())
980 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000981
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000982 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000983 if (ALoc.isNamedRef())
984 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000985 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000986 if (Dcl) {
987 if (Stm) {
Ted Kremenekfb480492010-01-13 21:46:36 +0000988 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm))
989 return MakeCXCursor(TranslateDeclRefExpr(DRE), Dcl, Stm);
990 else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm))
991 return MakeCXCursor(CXCursor_ObjCSelectorRef, Dcl, MExp);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000992 // Fall through...treat as a decl, not a ref.
993 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000994 if (ALoc.isNamedRef()) {
995 if (isa<ObjCInterfaceDecl>(Dcl)) {
Ted Kremenekb89e8f62010-01-13 19:59:20 +0000996 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl(), 0 };
Steve Naroff85e2db72009-10-01 00:31:07 +0000997 return C;
998 }
999 if (isa<ObjCProtocolDecl>(Dcl)) {
Ted Kremenekb89e8f62010-01-13 19:59:20 +00001000 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl(), 0 };
Steve Naroff85e2db72009-10-01 00:31:07 +00001001 return C;
1002 }
1003 }
Ted Kremenekfb480492010-01-13 21:46:36 +00001004 return MakeCXCursor(TranslateKind(Dcl), Dcl);
Steve Naroff77128dd2009-09-15 20:25:34 +00001005 }
Ted Kremenekfb480492010-01-13 21:46:36 +00001006 return MakeCXCursor(CXCursor_NoDeclFound, 0);
Steve Naroff600866c2009-08-27 19:51:58 +00001007}
1008
Ted Kremenek73885552009-11-17 19:28:59 +00001009CXCursor clang_getNullCursor(void) {
Ted Kremenekfb480492010-01-13 21:46:36 +00001010 return MakeCXCursor(CXCursor_InvalidFile, 0);
Ted Kremenek73885552009-11-17 19:28:59 +00001011}
1012
1013unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Ted Kremenekfb480492010-01-13 21:46:36 +00001014 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt &&
1015 X.referringDecl == Y.referringDecl;
Ted Kremenek73885552009-11-17 19:28:59 +00001016}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001017
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001018CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001019 assert(AnonDecl && "Passed null CXDecl");
1020 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekfb480492010-01-13 21:46:36 +00001021 return MakeCXCursor(TranslateKind(ND), ND);
Steve Naroff77128dd2009-09-15 20:25:34 +00001022}
1023
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001024unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001025 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1026}
1027
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001028unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001029 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1030}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001031
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001032unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001033 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1034}
1035
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001036unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001037 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
1038}
1039
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001040CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001041 return C.kind;
1042}
1043
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001044CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +00001045 if (clang_isDeclaration(C.kind))
1046 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001047
Steve Naroff699a07d2009-09-25 21:32:34 +00001048 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +00001049 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001050 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +00001051 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +00001052 return static_cast<Stmt *>(C.stmt);
1053 else
1054 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
1055 } else
Steve Naroff699a07d2009-09-25 21:32:34 +00001056 return C.decl;
1057 }
1058 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +00001059}
1060
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001061unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001062 assert(C.decl && "CXCursor has null decl");
1063 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001064 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001065
Steve Narofff334b4e2009-09-02 18:26:48 +00001066 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001067 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001068}
Ted Kremenekfb480492010-01-13 21:46:36 +00001069
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001070unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001071 assert(C.decl && "CXCursor has null decl");
1072 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001073 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001074
Steve Narofff334b4e2009-09-02 18:26:48 +00001075 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001076 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001077}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001078
1079const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001080 assert(C.decl && "CXCursor has null decl");
1081 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001082 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001083
Steve Narofff334b4e2009-09-02 18:26:48 +00001084 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Ted Kremenekfb480492010-01-13 21:46:36 +00001085
Ted Kremenek9298cfc2009-11-17 05:31:58 +00001086 if (SLoc.isFileID()) {
1087 const char *bufferName = SourceMgr.getBufferName(SLoc);
1088 return bufferName[0] == '<' ? NULL : bufferName;
1089 }
Ted Kremenekfb480492010-01-13 21:46:36 +00001090
Douglas Gregor02465752009-10-16 21:24:31 +00001091 // Retrieve the file in which the macro was instantiated, then provide that
1092 // buffer name.
1093 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001094 const llvm::MemoryBuffer *Buffer
Ted Kremenekfb480492010-01-13 21:46:36 +00001095 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
Douglas Gregor02465752009-10-16 21:24:31 +00001096 if (!Buffer)
1097 return 0;
Ted Kremenekfb480492010-01-13 21:46:36 +00001098
Douglas Gregor02465752009-10-16 21:24:31 +00001099 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +00001100}
1101
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001102CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +00001103 assert(C.decl && "CXCursor has null decl");
1104 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1105 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001106
Ted Kremenek4b333d22010-01-12 00:36:38 +00001107 return (void *)
Ted Kremenekfb480492010-01-13 21:46:36 +00001108 getFileEntryFromSourceLocation(SourceMgr, getLocationFromCursor(C,SourceMgr,
1109 ND));
Steve Naroff88145032009-10-27 14:35:18 +00001110}
1111
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001112void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001113 const char **startBuf,
1114 const char **endBuf,
1115 unsigned *startLine,
1116 unsigned *startColumn,
1117 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001118 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +00001119 assert(C.decl && "CXCursor has null decl");
1120 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1121 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1122 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekfb480492010-01-13 21:46:36 +00001123
Steve Naroff4ade6d62009-09-23 17:52:52 +00001124 SourceManager &SM = FD->getASTContext().getSourceManager();
1125 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1126 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1127 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1128 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1129 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1130 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1131}
Ted Kremenekfb480492010-01-13 21:46:36 +00001132
1133} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001134
Ted Kremenekfb480492010-01-13 21:46:36 +00001135//===----------------------------------------------------------------------===//
1136// CXString Operations.
1137//===----------------------------------------------------------------------===//
1138
1139extern "C" {
1140const char *clang_getCString(CXString string) {
1141 return string.Spelling;
1142}
1143
1144void clang_disposeString(CXString string) {
1145 if (string.MustFreeString && string.Spelling)
1146 free((void*)string.Spelling);
1147}
1148} // end: extern "C"