blob: c5c7409ffcfb10751509a1386627fc80539d753b [file] [log] [blame]
Ted Kremenekb60d87c2009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Daniel Dunbarbbc569c2009-11-30 20:42:43 +00007//
Ted Kremenekb60d87c2009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekb60d87c2009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000015#include "CIndexer.h"
Ted Kremenek87553c42010-01-15 20:35:54 +000016#include "CXCursor.h"
Ted Kremenek0ec2cca2010-01-05 19:32:54 +000017
Steve Naroffa1c72842009-08-28 15:28:48 +000018#include "clang/AST/DeclVisitor.h"
Steve Naroff66af1ae2009-09-22 19:25:29 +000019#include "clang/AST/StmtVisitor.h"
Ted Kremenek2a43fd52010-01-06 23:43:31 +000020#include "clang/Lex/Lexer.h"
Douglas Gregord3d923a2009-10-16 21:24:31 +000021#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer2836c4c2009-10-18 11:19:36 +000022#include "llvm/System/Program.h"
Ted Kremenek428c6372009-10-19 21:44:57 +000023
Ted Kremenek902292d2010-01-05 20:55:39 +000024// Needed to define L_TMPNAM on some systems.
25#include <cstdio>
26
Steve Naroffa1c72842009-08-28 15:28:48 +000027using namespace clang;
Ted Kremenek87553c42010-01-15 20:35:54 +000028using namespace clang::cxcursor;
Steve Naroffa1c72842009-08-28 15:28:48 +000029using namespace idx;
30
Ted Kremenek991eb3f2010-01-06 03:42:32 +000031//===----------------------------------------------------------------------===//
32// Crash Reporting.
33//===----------------------------------------------------------------------===//
34
35#ifdef __APPLE__
Ted Kremenek7a5ede22010-01-07 22:49:05 +000036#ifndef NDEBUG
37#define USE_CRASHTRACER
Ted Kremenek991eb3f2010-01-06 03:42:32 +000038#include "clang/Analysis/Support/SaveAndRestore.h"
39// Integrate with crash reporter.
40extern "C" const char *__crashreporter_info__;
Ted Kremenek7a5ede22010-01-07 22:49:05 +000041#define NUM_CRASH_STRINGS 16
42static unsigned crashtracer_counter = 0;
Ted Kremenek32b79312010-01-07 23:13:53 +000043static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek7a5ede22010-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 Kremenek32b79312010-01-07 23:13:53 +000050 unsigned slot = 0;
Ted Kremenek7a5ede22010-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 Kremenek32b79312010-01-07 23:13:53 +000056 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek7a5ede22010-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 Kremenek32b79312010-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 Kremenek7a5ede22010-01-07 22:49:05 +000083 }
Ted Kremenek32b79312010-01-07 23:13:53 +000084
85 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek7a5ede22010-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 Kremenek991eb3f2010-01-06 03:42:32 +0000114#endif
115
116//===----------------------------------------------------------------------===//
117// Visitors.
118//===----------------------------------------------------------------------===//
119
Steve Naroff1054e602009-08-31 00:59:03 +0000120namespace {
Ted Kremenek0ec2cca2010-01-05 19:32:54 +0000121static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff76b8f132009-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 Dunbarbbc569c2009-11-30 20:42:43 +0000129 else
Steve Naroff76b8f132009-09-23 17:52:52 +0000130 return CXCursor_NotImplemented;
131}
132
Steve Naroff1054e602009-08-31 00:59:03 +0000133// Translation Unit Visitor.
Ted Kremenek1351b092010-01-13 00:13:47 +0000134
Steve Naroff1054e602009-08-31 00:59:03 +0000135class TUVisitor : public DeclVisitor<TUVisitor> {
Ted Kremenek1351b092010-01-13 00:13:47 +0000136public:
137 typedef void (*Iterator)(void *, CXCursor, CXClientData);
138private:
139 void *Root; // CXDecl or CXTranslationUnit
140 Iterator Callback; // CXTranslationUnitIterator or CXDeclIterator.
Steve Naroff69b10fd2009-09-01 15:55:40 +0000141 CXClientData CData;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000142
Douglas Gregor16bef852009-10-16 20:01:17 +0000143 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
144 // to the visitor. Declarations with a PCH level greater than this value will
145 // be suppressed.
146 unsigned MaxPCHLevel;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000147
Ted Kremenekc2aa0f12010-01-16 00:36:30 +0000148 void Call(const CXCursor &C) {
Ted Kremenek9cec0002010-01-16 01:44:12 +0000149 if (clang_isInvalid(C.kind))
150 return;
151
Ted Kremenekc2aa0f12010-01-16 00:36:30 +0000152 if (const Decl *D = getCursorDecl(C)) {
153 // Filter any declarations that have a PCH level greater than what
154 // we allow.
155 if (D->getPCHLevel() > MaxPCHLevel)
156 return;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000157
Ted Kremenekc2aa0f12010-01-16 00:36:30 +0000158 // Filter any implicit declarations (since the source info will be bogus).
159 if (D->isImplicit())
160 return;
161 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000162
Ted Kremenek1351b092010-01-13 00:13:47 +0000163 Callback(Root, C, CData);
Steve Naroff69b10fd2009-09-01 15:55:40 +0000164 }
Ted Kremenek1351b092010-01-13 00:13:47 +0000165
Steve Naroff1054e602009-08-31 00:59:03 +0000166public:
Ted Kremenek1351b092010-01-13 00:13:47 +0000167 TUVisitor(void *root, Iterator cback, CXClientData D, unsigned MaxPCHLevel) :
168 Root(root), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000169
Ted Kremenekc2aa0f12010-01-16 00:36:30 +0000170 void VisitDecl(Decl *D);
Ted Kremenek1351b092010-01-13 00:13:47 +0000171 void VisitDeclContext(DeclContext *DC);
Ted Kremenek1351b092010-01-13 00:13:47 +0000172 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
Ted Kremenek1351b092010-01-13 00:13:47 +0000173};
Ted Kremenek98524b12009-11-17 07:02:15 +0000174
Ted Kremenekc2aa0f12010-01-16 00:36:30 +0000175void TUVisitor::VisitDecl(Decl *D) {
176 Call(MakeCXCursor(D));
177}
178
Ted Kremenek1351b092010-01-13 00:13:47 +0000179void TUVisitor::VisitDeclContext(DeclContext *DC) {
180 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
181 I != E; ++I)
182 Visit(*I);
183}
Ted Kremenekc2aa0f12010-01-16 00:36:30 +0000184
Ted Kremenek1351b092010-01-13 00:13:47 +0000185void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
186 VisitDeclContext(dyn_cast<DeclContext>(D));
187}
Steve Naroff1054e602009-08-31 00:59:03 +0000188
Steve Naroff3645f5a2009-09-02 13:28:54 +0000189// Declaration visitor.
190class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
191 CXDecl CDecl;
192 CXDeclIterator Callback;
193 CXClientData CData;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000194
Douglas Gregor16bef852009-10-16 20:01:17 +0000195 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
196 // to the visitor. Declarations with a PCH level greater than this value will
197 // be suppressed.
198 unsigned MaxPCHLevel;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000199
Steve Naroff3645f5a2009-09-02 13:28:54 +0000200 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000201 // Disable the callback when the context is equal to the visiting decl.
202 if (CDecl == ND && !clang_isReference(CK))
203 return;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000204
Douglas Gregor16bef852009-10-16 20:01:17 +0000205 // Filter any declarations that have a PCH level greater than what we allow.
206 if (ND->getPCHLevel() > MaxPCHLevel)
207 return;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000208
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000209 CXCursor C = { CK, { ND, 0, 0 } };
Steve Naroff3645f5a2009-09-02 13:28:54 +0000210 Callback(CDecl, C, CData);
211 }
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000212
Steve Naroff1054e602009-08-31 00:59:03 +0000213public:
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000214 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
215 unsigned MaxPCHLevel) :
Douglas Gregor16bef852009-10-16 20:01:17 +0000216 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000217
Ted Kremenek78668fd2010-01-13 00:22:49 +0000218 void VisitDeclContext(DeclContext *DC);
219 void VisitEnumConstantDecl(EnumConstantDecl *ND);
220 void VisitFieldDecl(FieldDecl *ND);
221 void VisitFunctionDecl(FunctionDecl *ND);
222 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
223 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
224 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
225 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
226 void VisitObjCIvarDecl(ObjCIvarDecl *ND);
227 void VisitObjCMethodDecl(ObjCMethodDecl *ND);
228 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND);
229 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
230 void VisitParmVarDecl(ParmVarDecl *ND);
231 void VisitTagDecl(TagDecl *D);
232 void VisitVarDecl(VarDecl *ND);
Steve Naroff1054e602009-08-31 00:59:03 +0000233};
Ted Kremenek0ec2cca2010-01-05 19:32:54 +0000234} // end anonymous namespace
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000235
Ted Kremenek78668fd2010-01-13 00:22:49 +0000236void CDeclVisitor::VisitDeclContext(DeclContext *DC) {
237 for (DeclContext::decl_iterator
238 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
239 Visit(*I);
240}
241
242void CDeclVisitor::VisitEnumConstantDecl(EnumConstantDecl *ND) {
243 Call(CXCursor_EnumConstantDecl, ND);
244}
245
246void CDeclVisitor::VisitFieldDecl(FieldDecl *ND) {
247 Call(CXCursor_FieldDecl, ND);
248}
249
250void CDeclVisitor::VisitFunctionDecl(FunctionDecl *ND) {
251 if (ND->isThisDeclarationADefinition()) {
252 VisitDeclContext(dyn_cast<DeclContext>(ND));
253#if 0
254 // Not currently needed.
255 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
256 CRefVisitor RVisit(CDecl, Callback, CData);
257 RVisit.Visit(Body);
258#endif
259 }
260}
261
262void CDeclVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
263 // Issue callbacks for the containing class.
Douglas Gregor46d66142010-01-16 17:14:40 +0000264 Callback(CDecl,
265 MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation()),
266 CData);
Douglas Gregoref6eb842010-01-16 15:44:18 +0000267 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
268 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
269 E = ND->protocol_end(); I != E; ++I, ++PL)
270 Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData);
Ted Kremenek78668fd2010-01-13 00:22:49 +0000271 VisitDeclContext(dyn_cast<DeclContext>(ND));
272}
273
274void CDeclVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
275 VisitDeclContext(dyn_cast<DeclContext>(D));
276}
277
278void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
279 VisitDeclContext(dyn_cast<DeclContext>(D));
280}
281
282void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
283 // Issue callbacks for super class.
284 if (D->getSuperClass())
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000285 Callback(CDecl,
286 MakeCursorObjCSuperClassRef(D->getSuperClass(),
287 D->getSuperClassLoc()),
288 CData);
Ted Kremenek78668fd2010-01-13 00:22:49 +0000289
Douglas Gregoref6eb842010-01-16 15:44:18 +0000290 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
291 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
292 E = D->protocol_end(); I != E; ++I, ++PL)
293 Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData);
Ted Kremenek78668fd2010-01-13 00:22:49 +0000294 VisitDeclContext(dyn_cast<DeclContext>(D));
295}
296
297void CDeclVisitor::VisitObjCIvarDecl(ObjCIvarDecl *ND) {
298 Call(CXCursor_ObjCIvarDecl, ND);
299}
300
301void CDeclVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
302 if (ND->getBody()) {
303 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
304 : CXCursor_ObjCClassMethodDefn, ND);
305 VisitDeclContext(dyn_cast<DeclContext>(ND));
306 } else
307 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
308 : CXCursor_ObjCClassMethodDecl, ND);
309}
310
311void CDeclVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
312 Call(CXCursor_ObjCPropertyDecl, ND);
313}
314
315void CDeclVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregoref6eb842010-01-16 15:44:18 +0000316 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
Ted Kremenek78668fd2010-01-13 00:22:49 +0000317 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Douglas Gregoref6eb842010-01-16 15:44:18 +0000318 E = PID->protocol_end(); I != E; ++I, ++PL)
319 Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData);
Ted Kremenek78668fd2010-01-13 00:22:49 +0000320
321 VisitDeclContext(dyn_cast<DeclContext>(PID));
322}
323
324void CDeclVisitor::VisitParmVarDecl(ParmVarDecl *ND) {
325 Call(CXCursor_ParmDecl, ND);
326}
327
328void CDeclVisitor::VisitTagDecl(TagDecl *D) {
329 VisitDeclContext(dyn_cast<DeclContext>(D));
330}
331
332void CDeclVisitor::VisitVarDecl(VarDecl *ND) {
333 Call(CXCursor_VarDecl, ND);
334}
335
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000336static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbar2679a882009-11-08 04:13:53 +0000337 SourceManager &SourceMgr,
338 NamedDecl *ND) {
339 if (clang_isReference(C.kind)) {
Ted Kremenek63b15c32010-01-15 18:24:18 +0000340
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000341 if (Decl *D = getCursorReferringDecl(C))
Ted Kremenek63b15c32010-01-15 18:24:18 +0000342 return D->getLocation();
343
Daniel Dunbar2679a882009-11-08 04:13:53 +0000344 switch (C.kind) {
Douglas Gregor46d66142010-01-16 17:14:40 +0000345 case CXCursor_ObjCClassRef:
346 return getCursorObjCClassRef(C).second;
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000347 case CXCursor_ObjCSuperClassRef:
348 return getCursorObjCSuperClassRef(C).second;
Douglas Gregoref6eb842010-01-16 15:44:18 +0000349 case CXCursor_ObjCProtocolRef:
350 return getCursorObjCProtocolRef(C).second;
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000351 case CXCursor_ObjCSelectorRef: {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000352 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000353 assert(OME && "clang_getCursorLine(): Missing message expr");
354 return OME->getLeftLoc(); /* FIXME: should be a range */
355 }
356 case CXCursor_VarRef:
357 case CXCursor_FunctionRef:
358 case CXCursor_EnumConstantRef: {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000359 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000360 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
361 return DRE->getLocation();
362 }
363 default:
364 return SourceLocation();
Daniel Dunbar2679a882009-11-08 04:13:53 +0000365 }
366 } else { // We have a declaration or a definition.
367 SourceLocation SLoc;
368 switch (ND->getKind()) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000369 case Decl::ObjCInterface: {
370 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
371 break;
372 }
373 case Decl::ObjCProtocol: {
374 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
375 break;
376 }
377 default: {
378 SLoc = ND->getLocation();
379 break;
380 }
Daniel Dunbar2679a882009-11-08 04:13:53 +0000381 }
382 if (SLoc.isInvalid())
383 return SourceLocation();
384 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
385 }
386}
387
Daniel Dunbarabd36862010-01-12 02:34:07 +0000388CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000389 CXString Str;
390 if (DupString) {
391 Str.Spelling = strdup(String);
392 Str.MustFreeString = 1;
393 } else {
394 Str.Spelling = String;
395 Str.MustFreeString = 0;
396 }
397 return Str;
398}
399
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000400extern "C" {
Steve Naroff531e2842009-10-20 14:46:24 +0000401CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000402 int displayDiagnostics) {
Steve Naroff531e2842009-10-20 14:46:24 +0000403 CIndexer *CIdxr = new CIndexer(new Program());
404 if (excludeDeclarationsFromPCH)
405 CIdxr->setOnlyLocalDecls();
406 if (displayDiagnostics)
407 CIdxr->setDisplayDiagnostics();
408 return CIdxr;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000409}
410
Daniel Dunbar079203f2009-12-01 03:14:51 +0000411void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff3aa2d732009-09-17 18:33:27 +0000412 assert(CIdx && "Passed null CXIndex");
Douglas Gregor16bef852009-10-16 20:01:17 +0000413 delete static_cast<CIndexer *>(CIdx);
Steve Naroff3aa2d732009-09-17 18:33:27 +0000414}
415
Daniel Dunbar11089662009-12-03 01:54:28 +0000416void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
417 assert(CIdx && "Passed null CXIndex");
418 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
419 CXXIdx->setUseExternalASTGeneration(value);
420}
421
Steve Naroffa1c72842009-08-28 15:28:48 +0000422// FIXME: need to pass back error info.
Daniel Dunbar079203f2009-12-01 03:14:51 +0000423CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
424 const char *ast_filename) {
Steve Naroffa1c72842009-08-28 15:28:48 +0000425 assert(CIdx && "Passed null CXIndex");
Douglas Gregor16bef852009-10-16 20:01:17 +0000426 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000427
Daniel Dunbar59203002009-12-03 01:45:44 +0000428 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
429 CXXIdx->getOnlyLocalDecls(),
430 /* UseBumpAllocator = */ true);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000431}
432
Daniel Dunbar079203f2009-12-01 03:14:51 +0000433CXTranslationUnit
434clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
435 const char *source_filename,
436 int num_command_line_args,
437 const char **command_line_args) {
Steve Naroff531e2842009-10-20 14:46:24 +0000438 assert(CIdx && "Passed null CXIndex");
439 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
440
Daniel Dunbar11089662009-12-03 01:54:28 +0000441 if (!CXXIdx->getUseExternalASTGeneration()) {
442 llvm::SmallVector<const char *, 16> Args;
443
444 // The 'source_filename' argument is optional. If the caller does not
445 // specify it then it is assumed that the source file is specified
446 // in the actual argument list.
447 if (source_filename)
448 Args.push_back(source_filename);
449 Args.insert(Args.end(), command_line_args,
450 command_line_args + num_command_line_args);
451
Daniel Dunbar72fe5b12009-12-05 02:17:18 +0000452 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek991eb3f2010-01-06 03:42:32 +0000453
Ted Kremenek7a5ede22010-01-07 22:49:05 +0000454#ifdef USE_CRASHTRACER
455 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek991eb3f2010-01-06 03:42:32 +0000456#endif
457
Daniel Dunbar72fe5b12009-12-05 02:17:18 +0000458 llvm::OwningPtr<ASTUnit> Unit(
459 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar8d4a2022009-12-13 03:46:13 +0000460 CXXIdx->getDiags(),
461 CXXIdx->getClangResourcesPath(),
Daniel Dunbar72fe5b12009-12-05 02:17:18 +0000462 CXXIdx->getOnlyLocalDecls(),
463 /* UseBumpAllocator = */ true));
Ted Kremenek7a5ede22010-01-07 22:49:05 +0000464
Daniel Dunbar72fe5b12009-12-05 02:17:18 +0000465 // FIXME: Until we have broader testing, just drop the entire AST if we
466 // encountered an error.
467 if (NumErrors != CXXIdx->getDiags().getNumErrors())
468 return 0;
469
470 return Unit.take();
Daniel Dunbar11089662009-12-03 01:54:28 +0000471 }
472
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000473 // Build up the arguments for invoking 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000474 std::vector<const char *> argv;
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000475
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000476 // First add the complete path to the 'clang' executable.
477 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer61f5d0c2009-10-18 16:11:04 +0000478 argv.push_back(ClangPath.c_str());
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000479
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000480 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000481 argv.push_back("-emit-ast");
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000482
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000483 // The 'source_filename' argument is optional. If the caller does not
484 // specify it then it is assumed that the source file is specified
485 // in the actual argument list.
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000486 if (source_filename)
487 argv.push_back(source_filename);
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000488
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000489 // Generate a temporary name for the AST file.
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000490 argv.push_back("-o");
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000491 char astTmpFile[L_tmpnam];
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000492 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000493
494 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
495 for (int i = 0; i < num_command_line_args; ++i)
496 if (const char *arg = command_line_args[i]) {
497 if (strcmp(arg, "-o") == 0) {
498 ++i; // Also skip the matching argument.
499 continue;
500 }
501 if (strcmp(arg, "-emit-ast") == 0 ||
502 strcmp(arg, "-c") == 0 ||
503 strcmp(arg, "-fsyntax-only") == 0) {
504 continue;
505 }
506
507 // Keep the argument.
508 argv.push_back(arg);
Steve Naroff531e2842009-10-20 14:46:24 +0000509 }
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000510
Ted Kremenek649bf5c2009-10-22 00:03:57 +0000511 // Add the null terminator.
Ted Kremenek51d06bb2009-10-15 23:21:22 +0000512 argv.push_back(NULL);
513
Ted Kremenek12e678d2009-10-26 22:14:08 +0000514 // Invoke 'clang'.
515 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
516 // on Unix or NUL (Windows).
Ted Kremenek44886fd2009-10-22 03:24:01 +0000517 std::string ErrMsg;
Ted Kremeneke2896882009-10-19 22:27:32 +0000518 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek44886fd2009-10-22 03:24:01 +0000519 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
520 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
521 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000522
Ted Kremenekba645742009-11-10 19:18:52 +0000523 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000524 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000525 << '\n' << "Arguments: \n";
Ted Kremenek44886fd2009-10-22 03:24:01 +0000526 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenekbf0690c2009-10-26 22:08:39 +0000527 I!=E; ++I) {
528 if (*I)
529 llvm::errs() << ' ' << *I << '\n';
530 }
531 llvm::errs() << '\n';
Ted Kremenek44886fd2009-10-22 03:24:01 +0000532 }
Benjamin Kramer2836c4c2009-10-18 11:19:36 +0000533
Steve Naroff1cfb96c2009-10-15 20:50:09 +0000534 // Finally, we create the translation unit from the ast file.
Steve Naroff44cd60e2009-10-15 22:23:48 +0000535 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000536 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroff531e2842009-10-20 14:46:24 +0000537 if (ATU)
538 ATU->unlinkTemporaryFile();
Steve Naroff44cd60e2009-10-15 22:23:48 +0000539 return ATU;
Steve Naroff7781daa2009-10-15 20:04:39 +0000540}
541
Daniel Dunbar079203f2009-12-01 03:14:51 +0000542void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff3aa2d732009-09-17 18:33:27 +0000543 assert(CTUnit && "Passed null CXTranslationUnit");
544 delete static_cast<ASTUnit *>(CTUnit);
545}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000546
Daniel Dunbar079203f2009-12-01 03:14:51 +0000547CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000548 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroffc0683b92009-09-03 18:19:54 +0000549 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek46157972010-01-12 00:36:38 +0000550 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
551 true);
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000552}
Daniel Dunbare58bd8b2009-08-28 16:30:07 +0000553
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000554void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroff3645f5a2009-09-02 13:28:54 +0000555 CXTranslationUnitIterator callback,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000556 CXClientData CData) {
Steve Naroffa1c72842009-08-28 15:28:48 +0000557 assert(CTUnit && "Passed null CXTranslationUnit");
558 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
559 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000560
Daniel Dunbar11089662009-12-03 01:54:28 +0000561 unsigned PCHLevel = Decl::MaxPCHLevel;
562
563 // Set the PCHLevel to filter out unwanted decls if requested.
564 if (CXXUnit->getOnlyLocalDecls()) {
565 PCHLevel = 0;
566
567 // If the main input was an AST, bump the level.
568 if (CXXUnit->isMainFileAST())
569 ++PCHLevel;
570 }
571
572 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbar644dca02009-12-04 08:17:33 +0000573
574 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
575 // decls.
576 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
577 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
578 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
579 ie = TLDs.end(); it != ie; ++it) {
580 DVisit.Visit(*it);
581 }
582 } else
583 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroffd5e8e862009-08-27 19:51:58 +0000584}
585
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000586void clang_loadDeclaration(CXDecl Dcl,
587 CXDeclIterator callback,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000588 CXClientData CData) {
Steve Naroff3645f5a2009-09-02 13:28:54 +0000589 assert(Dcl && "Passed null CXDecl");
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000590
Douglas Gregor16bef852009-10-16 20:01:17 +0000591 CDeclVisitor DVisit(Dcl, callback, CData,
592 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroff3645f5a2009-09-02 13:28:54 +0000593 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroffd5e8e862009-08-27 19:51:58 +0000594}
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000595} // end: extern "C"
Steve Naroffd5e8e862009-08-27 19:51:58 +0000596
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000597//===----------------------------------------------------------------------===//
Steve Naroffd5e8e862009-08-27 19:51:58 +0000598// CXDecl Operations.
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000599//===----------------------------------------------------------------------===//
Daniel Dunbar079203f2009-12-01 03:14:51 +0000600
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000601static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
602 SourceLocation SLoc) {
603 FileID FID;
604 if (SLoc.isFileID())
605 FID = SMgr.getFileID(SLoc);
606 else
607 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
608 return SMgr.getFileEntryForID(FID);
609}
610
611extern "C" {
Daniel Dunbar079203f2009-12-01 03:14:51 +0000612CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff1054e602009-08-31 00:59:03 +0000613 assert(AnonDecl && "Passed null CXDecl");
614 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000615
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000616 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenek46157972010-01-12 00:36:38 +0000617 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
618 true);
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000619
620 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Narofff406f4d2009-10-29 21:11:04 +0000621 // No, this isn't the same as the code below. getIdentifier() is non-virtual
622 // and returns different names. NamedDecl returns the class name and
623 // ObjCCategoryImplDecl returns the category name.
Ted Kremenek46157972010-01-12 00:36:38 +0000624 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000625
626 if (ND->getIdentifier())
Ted Kremenek46157972010-01-12 00:36:38 +0000627 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
Benjamin Kramer04c99a62009-11-09 19:13:48 +0000628
Ted Kremenek46157972010-01-12 00:36:38 +0000629 return CIndexer::createCXString("");
Steve Naroffd5e8e862009-08-27 19:51:58 +0000630}
Steve Naroff80a766b2009-09-02 18:26:48 +0000631
Daniel Dunbar079203f2009-12-01 03:14:51 +0000632unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff63f475a2009-09-25 21:32:34 +0000633 assert(AnonDecl && "Passed null CXDecl");
634 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
635 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
636 return SourceMgr.getSpellingLineNumber(ND->getLocation());
637}
638
Daniel Dunbar079203f2009-12-01 03:14:51 +0000639unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff63f475a2009-09-25 21:32:34 +0000640 assert(AnonDecl && "Passed null CXDecl");
641 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
642 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff43b118f2009-09-25 22:15:54 +0000643 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff63f475a2009-09-25 21:32:34 +0000644}
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000645
646CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
647 assert(AnonDecl && "Passed null CXDecl");
648 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000649 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000650 SourceRange R = ND->getSourceRange();
651
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000652 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
653 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
654
655 if (!Begin.isValid()) {
656 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
657 return extent;
658 }
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000659
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000660 // FIXME: This is largely copy-paste from
661 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
662 // what we want the two routines should be refactored.
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000663
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000664 // If the End location and the start location are the same and are a macro
665 // location, then the range was something that came from a macro expansion
666 // or _Pragma. If this is an object-like macro, the best we can do is to
667 // get the range. If this is a function-like macro, we'd also like to
668 // get the arguments.
669 if (Begin == End && R.getEnd().isMacroID())
670 End = SM.getInstantiationRange(R.getEnd()).second;
671
672 assert(SM.getFileID(Begin) == SM.getFileID(End));
673 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
674 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000675
Ted Kremenek2a43fd52010-01-06 23:43:31 +0000676 // Compute the column number of the start. Keep the column based at 1.
677 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
678
679 // Compute the column number of the end.
680 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
681 if (EndColNo) {
682 // Offset the end column by 1 so that we point to the last character
683 // in the last token.
684 --EndColNo;
685
686 // Add in the length of the token, so that we cover multi-char tokens.
687 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
688 const LangOptions &LOpts = Ctx.getLangOptions();
689
690 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
691 }
692
693 // Package up the line/column data and return to the caller.
694 CXDeclExtent extent = { { StartLineNo, StartColNo },
695 { EndLineNo, EndColNo } };
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000696 return extent;
697}
Steve Naroff63f475a2009-09-25 21:32:34 +0000698
Ted Kremenekea903062010-01-08 17:11:32 +0000699const char *clang_getDeclSource(CXDecl AnonDecl) {
700 assert(AnonDecl && "Passed null CXDecl");
701 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
702 assert(FEnt && "Cannot find FileEntry for Decl");
703 return clang_getFileName(FEnt);
704}
705
Steve Naroff6231f182009-10-27 14:35:18 +0000706
Daniel Dunbar079203f2009-12-01 03:14:51 +0000707CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff6231f182009-10-27 14:35:18 +0000708 assert(AnonDecl && "Passed null CXDecl");
Steve Naroff26760892009-09-25 21:45:39 +0000709 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
710 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff6231f182009-10-27 14:35:18 +0000711 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
712}
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000713} // end: extern "C"
Steve Naroff6231f182009-10-27 14:35:18 +0000714
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000715//===----------------------------------------------------------------------===//
716// CXFile Operations.
717//===----------------------------------------------------------------------===//
718
719extern "C" {
Steve Naroff6231f182009-10-27 14:35:18 +0000720const char *clang_getFileName(CXFile SFile) {
721 assert(SFile && "Passed null CXFile");
722 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
723 return FEnt->getName();
724}
725
726time_t clang_getFileTime(CXFile SFile) {
727 assert(SFile && "Passed null CXFile");
728 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
729 return FEnt->getModificationTime();
Steve Naroff26760892009-09-25 21:45:39 +0000730}
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000731} // end: extern "C"
Steve Naroff26760892009-09-25 21:45:39 +0000732
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000733//===----------------------------------------------------------------------===//
734// CXCursor Operations.
735//===----------------------------------------------------------------------===//
736
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000737static Decl *getDeclFromExpr(Stmt *E) {
738 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
739 return RefExpr->getDecl();
740 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
741 return ME->getMemberDecl();
742 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
743 return RE->getDecl();
744
745 if (CallExpr *CE = dyn_cast<CallExpr>(E))
746 return getDeclFromExpr(CE->getCallee());
747 if (CastExpr *CE = dyn_cast<CastExpr>(E))
748 return getDeclFromExpr(CE->getSubExpr());
749 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
750 return OME->getMethodDecl();
751
752 return 0;
753}
754
755extern "C" {
Daniel Dunbar079203f2009-12-01 03:14:51 +0000756CXString clang_getCursorSpelling(CXCursor C) {
Daniel Dunbarfadf43c2010-01-18 17:52:42 +0000757 assert(getCursorDecl(C) && "CXCursor has null decl");
Steve Naroff80a766b2009-09-02 18:26:48 +0000758 if (clang_isReference(C.kind)) {
759 switch (C.kind) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000760 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000761 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
762 return CIndexer::createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000763 }
764 case CXCursor_ObjCClassRef: {
Douglas Gregor46d66142010-01-16 17:14:40 +0000765 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
766 return CIndexer::createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000767 }
768 case CXCursor_ObjCProtocolRef: {
Douglas Gregoref6eb842010-01-16 15:44:18 +0000769 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000770 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Ted Kremenek46157972010-01-12 00:36:38 +0000771 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000772 }
773 case CXCursor_ObjCSelectorRef: {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000774 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000775 assert(OME && "clang_getCursorLine(): Missing message expr");
Ted Kremenek46157972010-01-12 00:36:38 +0000776 return CIndexer::createCXString(OME->getSelector().getAsString().c_str(),
777 true);
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000778 }
779 case CXCursor_VarRef:
780 case CXCursor_FunctionRef:
781 case CXCursor_EnumConstantRef: {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000782 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000783 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Ted Kremenek46157972010-01-12 00:36:38 +0000784 return CIndexer::createCXString(DRE->getDecl()->getIdentifier()
785 ->getNameStart());
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000786 }
787 default:
Ted Kremenek46157972010-01-12 00:36:38 +0000788 return CIndexer::createCXString("<not implemented>");
Steve Naroff80a766b2009-09-02 18:26:48 +0000789 }
790 }
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000791 return clang_getDeclSpelling(getCursorDecl(C));
Steve Naroff80a766b2009-09-02 18:26:48 +0000792}
793
Daniel Dunbar079203f2009-12-01 03:14:51 +0000794const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff1054e602009-08-31 00:59:03 +0000795 switch (Kind) {
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000796 case CXCursor_FunctionDecl: return "FunctionDecl";
797 case CXCursor_TypedefDecl: return "TypedefDecl";
798 case CXCursor_EnumDecl: return "EnumDecl";
799 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
800 case CXCursor_StructDecl: return "StructDecl";
801 case CXCursor_UnionDecl: return "UnionDecl";
802 case CXCursor_ClassDecl: return "ClassDecl";
803 case CXCursor_FieldDecl: return "FieldDecl";
Ted Kremenek4ba52632010-01-16 02:02:09 +0000804 case CXCursor_FunctionDefn: return "FunctionDefn";
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000805 case CXCursor_VarDecl: return "VarDecl";
806 case CXCursor_ParmDecl: return "ParmDecl";
807 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
808 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
809 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
810 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
811 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Ted Kremenek4ba52632010-01-16 02:02:09 +0000812 case CXCursor_ObjCIvarRef: return "ObjCIvarRef";
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000813 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
814 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000815 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
Ted Kremenek4ba52632010-01-16 02:02:09 +0000816 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000817 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
818 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000819 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
820 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
821 case CXCursor_ObjCClassRef: return "ObjCClassRef";
822 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000823
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000824 case CXCursor_VarRef: return "VarRef";
825 case CXCursor_FunctionRef: return "FunctionRef";
826 case CXCursor_EnumConstantRef: return "EnumConstantRef";
827 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000828
Daniel Dunbar5b2f5ca2009-11-30 20:42:49 +0000829 case CXCursor_InvalidFile: return "InvalidFile";
830 case CXCursor_NoDeclFound: return "NoDeclFound";
831 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff1054e602009-08-31 00:59:03 +0000832 }
Ted Kremenek4ba52632010-01-16 02:02:09 +0000833
834 llvm_unreachable("Unhandled CXCursorKind");
835 return NULL;
Steve Naroffd5e8e862009-08-27 19:51:58 +0000836}
Steve Naroff1054e602009-08-31 00:59:03 +0000837
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000838CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000839 unsigned line, unsigned column) {
Steve Naroffef9618b2009-09-04 15:44:05 +0000840 assert(CTUnit && "Passed null CXTranslationUnit");
841 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000842
Steve Naroffef9618b2009-09-04 15:44:05 +0000843 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000844 const FileEntry *File = FMgr.getFile(source_name,
845 source_name+strlen(source_name));
Ted Kremeneke34cbde2010-01-14 01:51:23 +0000846 if (!File)
847 return clang_getNullCursor();
848
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000849 SourceLocation SLoc =
Steve Naroffef9618b2009-09-04 15:44:05 +0000850 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000851
Steve Naroff58bd62d2009-10-28 20:44:47 +0000852 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000853 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Naroff58bd62d2009-10-28 20:44:47 +0000854 &LastLoc);
Ted Kremeneke34cbde2010-01-14 01:51:23 +0000855
856 // FIXME: This doesn't look thread-safe.
Steve Naroff58bd62d2009-10-28 20:44:47 +0000857 if (ALoc.isValid())
858 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000859
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000860 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis419e38b2009-09-29 19:45:58 +0000861 if (ALoc.isNamedRef())
862 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidis4cbe8592009-09-29 19:44:27 +0000863 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff76b8f132009-09-23 17:52:52 +0000864 if (Dcl) {
865 if (Stm) {
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000866 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm))
867 return MakeCXCursor(TranslateDeclRefExpr(DRE), Dcl, Stm);
868 else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm))
869 return MakeCXCursor(CXCursor_ObjCSelectorRef, Dcl, MExp);
Steve Naroff76b8f132009-09-23 17:52:52 +0000870 // Fall through...treat as a decl, not a ref.
871 }
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000872 if (ALoc.isNamedRef()) {
Douglas Gregor46d66142010-01-16 17:14:40 +0000873 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(Dcl))
874 return MakeCursorObjCClassRef(Class, ALoc.AsNamedRef().Loc);
Douglas Gregoref6eb842010-01-16 15:44:18 +0000875 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(Dcl))
876 return MakeCursorObjCProtocolRef(Proto, ALoc.AsNamedRef().Loc);
Steve Naroffa6c56bb2009-10-01 00:31:07 +0000877 }
Ted Kremenek9cec0002010-01-16 01:44:12 +0000878 return MakeCXCursor(Dcl);
Steve Naroff54f22fb2009-09-15 20:25:34 +0000879 }
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000880 return MakeCXCursor(CXCursor_NoDeclFound, 0);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000881}
882
Ted Kremeneke05d7802009-11-17 19:28:59 +0000883CXCursor clang_getNullCursor(void) {
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000884 return MakeCXCursor(CXCursor_InvalidFile, 0);
Ted Kremeneke05d7802009-11-17 19:28:59 +0000885}
886
887unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000888 return X == Y;
Ted Kremeneke05d7802009-11-17 19:28:59 +0000889}
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000890
Daniel Dunbar079203f2009-12-01 03:14:51 +0000891CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff54f22fb2009-09-15 20:25:34 +0000892 assert(AnonDecl && "Passed null CXDecl");
Ted Kremenek4ba52632010-01-16 02:02:09 +0000893 return MakeCXCursor(static_cast<NamedDecl *>(AnonDecl));
Steve Naroff54f22fb2009-09-15 20:25:34 +0000894}
895
Daniel Dunbar079203f2009-12-01 03:14:51 +0000896unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff54f22fb2009-09-15 20:25:34 +0000897 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
898}
899
Daniel Dunbar079203f2009-12-01 03:14:51 +0000900unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff1054e602009-08-31 00:59:03 +0000901 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
902}
Steve Naroff772c1a42009-08-31 14:26:51 +0000903
Daniel Dunbar079203f2009-12-01 03:14:51 +0000904unsigned clang_isReference(enum CXCursorKind K) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000905 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
906}
907
Daniel Dunbar079203f2009-12-01 03:14:51 +0000908unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Naroff80a766b2009-09-02 18:26:48 +0000909 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
910}
911
Daniel Dunbar079203f2009-12-01 03:14:51 +0000912CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroffef9618b2009-09-04 15:44:05 +0000913 return C.kind;
914}
915
Daniel Dunbar079203f2009-12-01 03:14:51 +0000916CXDecl clang_getCursorDecl(CXCursor C) {
Ted Kremenek9cec0002010-01-16 01:44:12 +0000917 if (clang_isDeclaration(C.kind) || clang_isDefinition(C.kind))
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000918 return getCursorDecl(C);
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000919
Steve Naroff63f475a2009-09-25 21:32:34 +0000920 if (clang_isReference(C.kind)) {
Douglas Gregor46d66142010-01-16 17:14:40 +0000921 if (getCursorStmt(C))
922 return getDeclFromExpr(getCursorStmt(C));
923
924 return getCursorDecl(C);
Steve Naroff63f475a2009-09-25 21:32:34 +0000925 }
926 return 0;
Steve Naroffef9618b2009-09-04 15:44:05 +0000927}
928
Daniel Dunbar079203f2009-12-01 03:14:51 +0000929unsigned clang_getCursorLine(CXCursor C) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000930 assert(getCursorDecl(C) && "CXCursor has null decl");
931 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff772c1a42009-08-31 14:26:51 +0000932 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000933
Steve Naroff80a766b2009-09-02 18:26:48 +0000934 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000935 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000936}
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000937
Daniel Dunbar079203f2009-12-01 03:14:51 +0000938unsigned clang_getCursorColumn(CXCursor C) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000939 assert(getCursorDecl(C) && "CXCursor has null decl");
940 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff772c1a42009-08-31 14:26:51 +0000941 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000942
Steve Naroff80a766b2009-09-02 18:26:48 +0000943 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff772c1a42009-08-31 14:26:51 +0000944 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroffd5e8e862009-08-27 19:51:58 +0000945}
Daniel Dunbar079203f2009-12-01 03:14:51 +0000946
947const char *clang_getCursorSource(CXCursor C) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000948 assert(getCursorDecl(C) && "CXCursor has null decl");
949 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff772c1a42009-08-31 14:26:51 +0000950 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000951
Steve Naroff80a766b2009-09-02 18:26:48 +0000952 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000953
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000954 if (SLoc.isFileID()) {
955 const char *bufferName = SourceMgr.getBufferName(SLoc);
956 return bufferName[0] == '<' ? NULL : bufferName;
957 }
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000958
Douglas Gregord3d923a2009-10-16 21:24:31 +0000959 // Retrieve the file in which the macro was instantiated, then provide that
960 // buffer name.
961 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000962 const llvm::MemoryBuffer *Buffer
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000963 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
Douglas Gregord3d923a2009-10-16 21:24:31 +0000964 if (!Buffer)
965 return 0;
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000966
Douglas Gregord3d923a2009-10-16 21:24:31 +0000967 return Buffer->getBufferIdentifier();
Steve Naroffd5e8e862009-08-27 19:51:58 +0000968}
969
Daniel Dunbar079203f2009-12-01 03:14:51 +0000970CXFile clang_getCursorSourceFile(CXCursor C) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000971 assert(getCursorDecl(C) && "CXCursor has null decl");
972 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff6231f182009-10-27 14:35:18 +0000973 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000974
Ted Kremenek46157972010-01-12 00:36:38 +0000975 return (void *)
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000976 getFileEntryFromSourceLocation(SourceMgr, getLocationFromCursor(C,SourceMgr,
977 ND));
Steve Naroff6231f182009-10-27 14:35:18 +0000978}
979
Daniel Dunbarbbc569c2009-11-30 20:42:43 +0000980void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff76b8f132009-09-23 17:52:52 +0000981 const char **startBuf,
982 const char **endBuf,
983 unsigned *startLine,
984 unsigned *startColumn,
985 unsigned *endLine,
Daniel Dunbar079203f2009-12-01 03:14:51 +0000986 unsigned *endColumn) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000987 assert(getCursorDecl(C) && "CXCursor has null decl");
988 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff76b8f132009-09-23 17:52:52 +0000989 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
990 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +0000991
Steve Naroff76b8f132009-09-23 17:52:52 +0000992 SourceManager &SM = FD->getASTContext().getSourceManager();
993 *startBuf = SM.getCharacterData(Body->getLBracLoc());
994 *endBuf = SM.getCharacterData(Body->getRBracLoc());
995 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
996 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
997 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
998 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
999}
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001000
1001} // end: extern "C"
Steve Naroff76b8f132009-09-23 17:52:52 +00001002
Ted Kremenekd5c6eaf2010-01-13 21:46:36 +00001003//===----------------------------------------------------------------------===//
1004// CXString Operations.
1005//===----------------------------------------------------------------------===//
1006
1007extern "C" {
1008const char *clang_getCString(CXString string) {
1009 return string.Spelling;
1010}
1011
1012void clang_disposeString(CXString string) {
1013 if (string.MustFreeString && string.Spelling)
1014 free((void*)string.Spelling);
1015}
1016} // end: extern "C"