blob: 4e7e86a459535770f731259c742839cfa8a6e2fd [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
Douglas Gregor283cae32010-01-15 21:56:13 +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
Douglas Gregor283cae32010-01-15 21:56:13 +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
Douglas Gregor283cae32010-01-15 21:56:13 +0000415 if (Decl *D = getCursorReferringDecl(C))
Ted Kremenek8b456e82010-01-15 18:24:18 +0000416 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)) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000421 NamedDecl *parentDecl = getCursorInterfaceParent(C);
Daniel Dunbaracca7252009-11-30 20:42:49 +0000422 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000423 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000424 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
425 assert(OID && "clang_getCursorLine(): Missing category decl");
426 return OID->getClassInterface()->getLocation();
427 }
428 case CXCursor_ObjCSuperClassRef: {
429 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
430 assert(OID && "clang_getCursorLine(): Missing interface decl");
431 return OID->getSuperClassLoc();
432 }
433 case CXCursor_ObjCProtocolRef: {
434 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
435 assert(OID && "clang_getCursorLine(): Missing protocol decl");
436 return OID->getLocation();
437 }
438 case CXCursor_ObjCSelectorRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000439 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
Daniel Dunbaracca7252009-11-30 20:42:49 +0000440 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: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000446 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
Daniel Dunbaracca7252009-11-30 20:42:49 +0000447 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
448 return DRE->getLocation();
449 }
450 default:
451 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000452 }
453 } else { // We have a declaration or a definition.
454 SourceLocation SLoc;
455 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000456 case Decl::ObjCInterface: {
457 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
458 break;
459 }
460 case Decl::ObjCProtocol: {
461 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
462 break;
463 }
464 default: {
465 SLoc = ND->getLocation();
466 break;
467 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000468 }
469 if (SLoc.isInvalid())
470 return SourceLocation();
471 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
472 }
473}
474
Daniel Dunbar140fce22010-01-12 02:34:07 +0000475CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000476 CXString Str;
477 if (DupString) {
478 Str.Spelling = strdup(String);
479 Str.MustFreeString = 1;
480 } else {
481 Str.Spelling = String;
482 Str.MustFreeString = 0;
483 }
484 return Str;
485}
486
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000487extern "C" {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000488CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000489 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000490 CIndexer *CIdxr = new CIndexer(new Program());
491 if (excludeDeclarationsFromPCH)
492 CIdxr->setOnlyLocalDecls();
493 if (displayDiagnostics)
494 CIdxr->setDisplayDiagnostics();
495 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000496}
497
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000498void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000499 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000500 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000501}
502
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000503void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
504 assert(CIdx && "Passed null CXIndex");
505 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
506 CXXIdx->setUseExternalASTGeneration(value);
507}
508
Steve Naroff50398192009-08-28 15:28:48 +0000509// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000510CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
511 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000512 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000513 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000514
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000515 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
516 CXXIdx->getOnlyLocalDecls(),
517 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000518}
519
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000520CXTranslationUnit
521clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
522 const char *source_filename,
523 int num_command_line_args,
524 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000525 assert(CIdx && "Passed null CXIndex");
526 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
527
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000528 if (!CXXIdx->getUseExternalASTGeneration()) {
529 llvm::SmallVector<const char *, 16> Args;
530
531 // The 'source_filename' argument is optional. If the caller does not
532 // specify it then it is assumed that the source file is specified
533 // in the actual argument list.
534 if (source_filename)
535 Args.push_back(source_filename);
536 Args.insert(Args.end(), command_line_args,
537 command_line_args + num_command_line_args);
538
Daniel Dunbar94220972009-12-05 02:17:18 +0000539 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000540
Ted Kremenek29b72842010-01-07 22:49:05 +0000541#ifdef USE_CRASHTRACER
542 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000543#endif
544
Daniel Dunbar94220972009-12-05 02:17:18 +0000545 llvm::OwningPtr<ASTUnit> Unit(
546 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000547 CXXIdx->getDiags(),
548 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000549 CXXIdx->getOnlyLocalDecls(),
550 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000551
Daniel Dunbar94220972009-12-05 02:17:18 +0000552 // FIXME: Until we have broader testing, just drop the entire AST if we
553 // encountered an error.
554 if (NumErrors != CXXIdx->getDiags().getNumErrors())
555 return 0;
556
557 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000558 }
559
Ted Kremenek139ba862009-10-22 00:03:57 +0000560 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000561 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000562
Ted Kremenek139ba862009-10-22 00:03:57 +0000563 // First add the complete path to the 'clang' executable.
564 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000565 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000566
Ted Kremenek139ba862009-10-22 00:03:57 +0000567 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000568 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000569
Ted Kremenek139ba862009-10-22 00:03:57 +0000570 // The 'source_filename' argument is optional. If the caller does not
571 // specify it then it is assumed that the source file is specified
572 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000573 if (source_filename)
574 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000575
Steve Naroff37b5ac22009-10-15 20:50:09 +0000576 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000577 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000578 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000579 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000580
581 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
582 for (int i = 0; i < num_command_line_args; ++i)
583 if (const char *arg = command_line_args[i]) {
584 if (strcmp(arg, "-o") == 0) {
585 ++i; // Also skip the matching argument.
586 continue;
587 }
588 if (strcmp(arg, "-emit-ast") == 0 ||
589 strcmp(arg, "-c") == 0 ||
590 strcmp(arg, "-fsyntax-only") == 0) {
591 continue;
592 }
593
594 // Keep the argument.
595 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000596 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000597
Ted Kremenek139ba862009-10-22 00:03:57 +0000598 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000599 argv.push_back(NULL);
600
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000601 // Invoke 'clang'.
602 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
603 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000604 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000605 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000606 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
607 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
608 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000609
Ted Kremenek0854d702009-11-10 19:18:52 +0000610 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000611 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000612 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000613 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000614 I!=E; ++I) {
615 if (*I)
616 llvm::errs() << ' ' << *I << '\n';
617 }
618 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000619 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000620
Steve Naroff37b5ac22009-10-15 20:50:09 +0000621 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000622 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000623 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000624 if (ATU)
625 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000626 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000627}
628
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000629void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000630 assert(CTUnit && "Passed null CXTranslationUnit");
631 delete static_cast<ASTUnit *>(CTUnit);
632}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000633
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000634CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000635 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000636 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek4b333d22010-01-12 00:36:38 +0000637 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
638 true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000639}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000640
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000641void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000642 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000643 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000644 assert(CTUnit && "Passed null CXTranslationUnit");
645 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
646 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000647
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000648 unsigned PCHLevel = Decl::MaxPCHLevel;
649
650 // Set the PCHLevel to filter out unwanted decls if requested.
651 if (CXXUnit->getOnlyLocalDecls()) {
652 PCHLevel = 0;
653
654 // If the main input was an AST, bump the level.
655 if (CXXUnit->isMainFileAST())
656 ++PCHLevel;
657 }
658
659 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000660
661 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
662 // decls.
663 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
664 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
665 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
666 ie = TLDs.end(); it != ie; ++it) {
667 DVisit.Visit(*it);
668 }
669 } else
670 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000671}
672
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000673void clang_loadDeclaration(CXDecl Dcl,
674 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000675 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000676 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000677
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000678 CDeclVisitor DVisit(Dcl, callback, CData,
679 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000680 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000681}
Ted Kremenekfb480492010-01-13 21:46:36 +0000682} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +0000683
Ted Kremenekfb480492010-01-13 21:46:36 +0000684//===----------------------------------------------------------------------===//
Steve Naroff600866c2009-08-27 19:51:58 +0000685// CXDecl Operations.
Ted Kremenekfb480492010-01-13 21:46:36 +0000686//===----------------------------------------------------------------------===//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000687
Ted Kremenekfb480492010-01-13 21:46:36 +0000688static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
689 SourceLocation SLoc) {
690 FileID FID;
691 if (SLoc.isFileID())
692 FID = SMgr.getFileID(SLoc);
693 else
694 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
695 return SMgr.getFileEntryForID(FID);
696}
697
698extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000699CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000700 assert(AnonDecl && "Passed null CXDecl");
701 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000702
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000703 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000704 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
705 true);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000706
707 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000708 // No, this isn't the same as the code below. getIdentifier() is non-virtual
709 // and returns different names. NamedDecl returns the class name and
710 // ObjCCategoryImplDecl returns the category name.
Ted Kremenek4b333d22010-01-12 00:36:38 +0000711 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000712
713 if (ND->getIdentifier())
Ted Kremenek4b333d22010-01-12 00:36:38 +0000714 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000715
Ted Kremenek4b333d22010-01-12 00:36:38 +0000716 return CIndexer::createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000717}
Steve Narofff334b4e2009-09-02 18:26:48 +0000718
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000719unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000720 assert(AnonDecl && "Passed null CXDecl");
721 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
722 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
723 return SourceMgr.getSpellingLineNumber(ND->getLocation());
724}
725
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000726unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000727 assert(AnonDecl && "Passed null CXDecl");
728 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
729 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000730 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000731}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000732
733CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
734 assert(AnonDecl && "Passed null CXDecl");
735 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekd8210652010-01-06 23:43:31 +0000736 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000737 SourceRange R = ND->getSourceRange();
738
Ted Kremenekd8210652010-01-06 23:43:31 +0000739 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
740 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
741
742 if (!Begin.isValid()) {
743 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
744 return extent;
745 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000746
Ted Kremenekd8210652010-01-06 23:43:31 +0000747 // FIXME: This is largely copy-paste from
748 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
749 // what we want the two routines should be refactored.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000750
Ted Kremenekd8210652010-01-06 23:43:31 +0000751 // If the End location and the start location are the same and are a macro
752 // location, then the range was something that came from a macro expansion
753 // or _Pragma. If this is an object-like macro, the best we can do is to
754 // get the range. If this is a function-like macro, we'd also like to
755 // get the arguments.
756 if (Begin == End && R.getEnd().isMacroID())
757 End = SM.getInstantiationRange(R.getEnd()).second;
758
759 assert(SM.getFileID(Begin) == SM.getFileID(End));
760 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
761 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000762
Ted Kremenekd8210652010-01-06 23:43:31 +0000763 // Compute the column number of the start. Keep the column based at 1.
764 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
765
766 // Compute the column number of the end.
767 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
768 if (EndColNo) {
769 // Offset the end column by 1 so that we point to the last character
770 // in the last token.
771 --EndColNo;
772
773 // Add in the length of the token, so that we cover multi-char tokens.
774 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
775 const LangOptions &LOpts = Ctx.getLangOptions();
776
777 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
778 }
779
780 // Package up the line/column data and return to the caller.
781 CXDeclExtent extent = { { StartLineNo, StartColNo },
782 { EndLineNo, EndColNo } };
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000783 return extent;
784}
Steve Naroff699a07d2009-09-25 21:32:34 +0000785
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000786const char *clang_getDeclSource(CXDecl AnonDecl) {
787 assert(AnonDecl && "Passed null CXDecl");
788 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
789 assert(FEnt && "Cannot find FileEntry for Decl");
790 return clang_getFileName(FEnt);
791}
792
Steve Naroff88145032009-10-27 14:35:18 +0000793
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000794CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000795 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000796 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
797 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000798 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
799}
Ted Kremenekfb480492010-01-13 21:46:36 +0000800} // end: extern "C"
Steve Naroff88145032009-10-27 14:35:18 +0000801
Ted Kremenekfb480492010-01-13 21:46:36 +0000802//===----------------------------------------------------------------------===//
803// CXFile Operations.
804//===----------------------------------------------------------------------===//
805
806extern "C" {
Steve Naroff88145032009-10-27 14:35:18 +0000807const char *clang_getFileName(CXFile SFile) {
808 assert(SFile && "Passed null CXFile");
809 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
810 return FEnt->getName();
811}
812
813time_t clang_getFileTime(CXFile SFile) {
814 assert(SFile && "Passed null CXFile");
815 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
816 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000817}
Ted Kremenekfb480492010-01-13 21:46:36 +0000818} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +0000819
Ted Kremenekfb480492010-01-13 21:46:36 +0000820//===----------------------------------------------------------------------===//
821// CXCursor Operations.
822//===----------------------------------------------------------------------===//
823
824static enum CXCursorKind TranslateKind(Decl *D) {
825 switch (D->getKind()) {
826 case Decl::Function: return CXCursor_FunctionDecl;
827 case Decl::Typedef: return CXCursor_TypedefDecl;
828 case Decl::Enum: return CXCursor_EnumDecl;
829 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
830 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
831 case Decl::Field: return CXCursor_FieldDecl;
832 case Decl::Var: return CXCursor_VarDecl;
833 case Decl::ParmVar: return CXCursor_ParmDecl;
834 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
835 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
836 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
837 case Decl::ObjCMethod: {
838 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
839 if (MD->isInstanceMethod())
840 return CXCursor_ObjCInstanceMethodDecl;
841 return CXCursor_ObjCClassMethodDecl;
842 }
843 default: break;
844 }
845 return CXCursor_NotImplemented;
846}
847
Ted Kremenekfb480492010-01-13 21:46:36 +0000848static Decl *getDeclFromExpr(Stmt *E) {
849 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
850 return RefExpr->getDecl();
851 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
852 return ME->getMemberDecl();
853 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
854 return RE->getDecl();
855
856 if (CallExpr *CE = dyn_cast<CallExpr>(E))
857 return getDeclFromExpr(CE->getCallee());
858 if (CastExpr *CE = dyn_cast<CastExpr>(E))
859 return getDeclFromExpr(CE->getSubExpr());
860 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
861 return OME->getMethodDecl();
862
863 return 0;
864}
865
866extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000867CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000868 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
869 assert(ND && "CXCursor has null decl");
Steve Narofff334b4e2009-09-02 18:26:48 +0000870 if (clang_isReference(C.kind)) {
871 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000872 case CXCursor_ObjCSuperClassRef: {
873 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
874 assert(OID && "clang_getCursorLine(): Missing interface decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000875 return CIndexer::createCXString(OID->getSuperClass()->getIdentifier()
876 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000877 }
878 case CXCursor_ObjCClassRef: {
879 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000880 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000881
882 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
883 assert(OCD && "clang_getCursorLine(): Missing category decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000884 return CIndexer::createCXString(OCD->getClassInterface()->getIdentifier()
Daniel Dunbaracca7252009-11-30 20:42:49 +0000885 ->getNameStart());
886 }
887 case CXCursor_ObjCProtocolRef: {
888 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
889 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000890 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000891 }
892 case CXCursor_ObjCSelectorRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000893 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
Daniel Dunbaracca7252009-11-30 20:42:49 +0000894 assert(OME && "clang_getCursorLine(): Missing message expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000895 return CIndexer::createCXString(OME->getSelector().getAsString().c_str(),
896 true);
Daniel Dunbaracca7252009-11-30 20:42:49 +0000897 }
898 case CXCursor_VarRef:
899 case CXCursor_FunctionRef:
900 case CXCursor_EnumConstantRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000901 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
Daniel Dunbaracca7252009-11-30 20:42:49 +0000902 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000903 return CIndexer::createCXString(DRE->getDecl()->getIdentifier()
904 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000905 }
906 default:
Ted Kremenek4b333d22010-01-12 00:36:38 +0000907 return CIndexer::createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000908 }
909 }
Douglas Gregor283cae32010-01-15 21:56:13 +0000910 return clang_getDeclSpelling(getCursorDecl(C));
Steve Narofff334b4e2009-09-02 18:26:48 +0000911}
912
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000913const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000914 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000915 case CXCursor_FunctionDecl: return "FunctionDecl";
916 case CXCursor_TypedefDecl: return "TypedefDecl";
917 case CXCursor_EnumDecl: return "EnumDecl";
918 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
919 case CXCursor_StructDecl: return "StructDecl";
920 case CXCursor_UnionDecl: return "UnionDecl";
921 case CXCursor_ClassDecl: return "ClassDecl";
922 case CXCursor_FieldDecl: return "FieldDecl";
923 case CXCursor_VarDecl: return "VarDecl";
924 case CXCursor_ParmDecl: return "ParmDecl";
925 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
926 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
927 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
928 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
929 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
930 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
931 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
932 case CXCursor_FunctionDefn: return "FunctionDefn";
933 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
934 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
935 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
936 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
937 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
938 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
939 case CXCursor_ObjCClassRef: return "ObjCClassRef";
940 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000941
Daniel Dunbaracca7252009-11-30 20:42:49 +0000942 case CXCursor_VarRef: return "VarRef";
943 case CXCursor_FunctionRef: return "FunctionRef";
944 case CXCursor_EnumConstantRef: return "EnumConstantRef";
945 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000946
Daniel Dunbaracca7252009-11-30 20:42:49 +0000947 case CXCursor_InvalidFile: return "InvalidFile";
948 case CXCursor_NoDeclFound: return "NoDeclFound";
949 case CXCursor_NotImplemented: return "NotImplemented";
950 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000951 }
Steve Naroff600866c2009-08-27 19:51:58 +0000952}
Steve Naroff89922f82009-08-31 00:59:03 +0000953
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000954CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000955 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000956 assert(CTUnit && "Passed null CXTranslationUnit");
957 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000958
Steve Naroff9efa7672009-09-04 15:44:05 +0000959 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000960 const FileEntry *File = FMgr.getFile(source_name,
961 source_name+strlen(source_name));
Ted Kremenekf4629892010-01-14 01:51:23 +0000962 if (!File)
963 return clang_getNullCursor();
964
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000965 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000966 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000967
Steve Narofff96b5242009-10-28 20:44:47 +0000968 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000969 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000970 &LastLoc);
Ted Kremenekf4629892010-01-14 01:51:23 +0000971
972 // FIXME: This doesn't look thread-safe.
Steve Narofff96b5242009-10-28 20:44:47 +0000973 if (ALoc.isValid())
974 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000975
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000976 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000977 if (ALoc.isNamedRef())
978 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000979 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000980 if (Dcl) {
981 if (Stm) {
Ted Kremenekfb480492010-01-13 21:46:36 +0000982 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm))
983 return MakeCXCursor(TranslateDeclRefExpr(DRE), Dcl, Stm);
984 else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm))
985 return MakeCXCursor(CXCursor_ObjCSelectorRef, Dcl, MExp);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000986 // Fall through...treat as a decl, not a ref.
987 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000988 if (ALoc.isNamedRef()) {
989 if (isa<ObjCInterfaceDecl>(Dcl)) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000990 CXCursor C = { CXCursor_ObjCClassRef, { Dcl, ALoc.getParentDecl(), 0 }};
Steve Naroff85e2db72009-10-01 00:31:07 +0000991 return C;
992 }
993 if (isa<ObjCProtocolDecl>(Dcl)) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000994 CXCursor C = {CXCursor_ObjCProtocolRef, {Dcl, ALoc.getParentDecl(), 0}};
Steve Naroff85e2db72009-10-01 00:31:07 +0000995 return C;
996 }
997 }
Ted Kremenekfb480492010-01-13 21:46:36 +0000998 return MakeCXCursor(TranslateKind(Dcl), Dcl);
Steve Naroff77128dd2009-09-15 20:25:34 +0000999 }
Ted Kremenekfb480492010-01-13 21:46:36 +00001000 return MakeCXCursor(CXCursor_NoDeclFound, 0);
Steve Naroff600866c2009-08-27 19:51:58 +00001001}
1002
Ted Kremenek73885552009-11-17 19:28:59 +00001003CXCursor clang_getNullCursor(void) {
Ted Kremenekfb480492010-01-13 21:46:36 +00001004 return MakeCXCursor(CXCursor_InvalidFile, 0);
Ted Kremenek73885552009-11-17 19:28:59 +00001005}
1006
1007unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001008 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001009}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001010
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001011CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001012 assert(AnonDecl && "Passed null CXDecl");
1013 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekfb480492010-01-13 21:46:36 +00001014 return MakeCXCursor(TranslateKind(ND), ND);
Steve Naroff77128dd2009-09-15 20:25:34 +00001015}
1016
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001017unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001018 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1019}
1020
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001021unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001022 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1023}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001024
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001025unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001026 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1027}
1028
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001029unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001030 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
1031}
1032
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001033CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001034 return C.kind;
1035}
1036
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001037CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +00001038 if (clang_isDeclaration(C.kind))
Douglas Gregor283cae32010-01-15 21:56:13 +00001039 return getCursorDecl(C);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001040
Steve Naroff699a07d2009-09-25 21:32:34 +00001041 if (clang_isReference(C.kind)) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001042 if (getCursorStmt(C)) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001043 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +00001044 C.kind == CXCursor_ObjCProtocolRef)
Douglas Gregor283cae32010-01-15 21:56:13 +00001045 return getCursorStmt(C);
Steve Naroff85e2db72009-10-01 00:31:07 +00001046 else
Douglas Gregor283cae32010-01-15 21:56:13 +00001047 return getDeclFromExpr(getCursorStmt(C));
Steve Naroff85e2db72009-10-01 00:31:07 +00001048 } else
Douglas Gregor283cae32010-01-15 21:56:13 +00001049 return getCursorDecl(C);
Steve Naroff699a07d2009-09-25 21:32:34 +00001050 }
1051 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +00001052}
1053
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001054unsigned clang_getCursorLine(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001055 assert(getCursorDecl(C) && "CXCursor has null decl");
1056 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff2d4d6292009-08-31 14:26:51 +00001057 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001058
Steve Narofff334b4e2009-09-02 18:26:48 +00001059 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001060 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001061}
Ted Kremenekfb480492010-01-13 21:46:36 +00001062
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001063unsigned clang_getCursorColumn(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001064 assert(getCursorDecl(C) && "CXCursor has null decl");
1065 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff2d4d6292009-08-31 14:26:51 +00001066 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001067
Steve Narofff334b4e2009-09-02 18:26:48 +00001068 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001069 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001070}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001071
1072const char *clang_getCursorSource(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001073 assert(getCursorDecl(C) && "CXCursor has null decl");
1074 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff2d4d6292009-08-31 14:26:51 +00001075 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001076
Steve Narofff334b4e2009-09-02 18:26:48 +00001077 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Ted Kremenekfb480492010-01-13 21:46:36 +00001078
Ted Kremenek9298cfc2009-11-17 05:31:58 +00001079 if (SLoc.isFileID()) {
1080 const char *bufferName = SourceMgr.getBufferName(SLoc);
1081 return bufferName[0] == '<' ? NULL : bufferName;
1082 }
Ted Kremenekfb480492010-01-13 21:46:36 +00001083
Douglas Gregor02465752009-10-16 21:24:31 +00001084 // Retrieve the file in which the macro was instantiated, then provide that
1085 // buffer name.
1086 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001087 const llvm::MemoryBuffer *Buffer
Ted Kremenekfb480492010-01-13 21:46:36 +00001088 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
Douglas Gregor02465752009-10-16 21:24:31 +00001089 if (!Buffer)
1090 return 0;
Ted Kremenekfb480492010-01-13 21:46:36 +00001091
Douglas Gregor02465752009-10-16 21:24:31 +00001092 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +00001093}
1094
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001095CXFile clang_getCursorSourceFile(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001096 assert(getCursorDecl(C) && "CXCursor has null decl");
1097 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff88145032009-10-27 14:35:18 +00001098 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +00001099
Ted Kremenek4b333d22010-01-12 00:36:38 +00001100 return (void *)
Ted Kremenekfb480492010-01-13 21:46:36 +00001101 getFileEntryFromSourceLocation(SourceMgr, getLocationFromCursor(C,SourceMgr,
1102 ND));
Steve Naroff88145032009-10-27 14:35:18 +00001103}
1104
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001105void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001106 const char **startBuf,
1107 const char **endBuf,
1108 unsigned *startLine,
1109 unsigned *startColumn,
1110 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001111 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001112 assert(getCursorDecl(C) && "CXCursor has null decl");
1113 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001114 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1115 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekfb480492010-01-13 21:46:36 +00001116
Steve Naroff4ade6d62009-09-23 17:52:52 +00001117 SourceManager &SM = FD->getASTContext().getSourceManager();
1118 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1119 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1120 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1121 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1122 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1123 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1124}
Ted Kremenekfb480492010-01-13 21:46:36 +00001125
1126} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001127
Ted Kremenekfb480492010-01-13 21:46:36 +00001128//===----------------------------------------------------------------------===//
1129// CXString Operations.
1130//===----------------------------------------------------------------------===//
1131
1132extern "C" {
1133const char *clang_getCString(CXString string) {
1134 return string.Spelling;
1135}
1136
1137void clang_disposeString(CXString string) {
1138 if (string.MustFreeString && string.Spelling)
1139 free((void*)string.Spelling);
1140}
1141} // end: extern "C"