blob: e58a08a0d6c9ef030ccd9bdafaaa4b32508438e3 [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
Steve Naroff89922f82009-08-31 00:59:03 +0000133// Translation Unit Visitor.
Ted Kremenekf1286182010-01-13 00:13:47 +0000134
Steve Naroff89922f82009-08-31 00:59:03 +0000135class TUVisitor : public DeclVisitor<TUVisitor> {
Ted Kremenekf1286182010-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 Naroff2b8ee6c2009-09-01 15:55:40 +0000141 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000142
Douglas Gregor7d1d49d2009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000147
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000148 void Call(const CXCursor &C) {
Ted Kremenek70ee5422010-01-16 01:44:12 +0000149 if (clang_isInvalid(C.kind))
150 return;
151
Ted Kremenekedc8aa62010-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 Dunbar0d7dd222009-11-30 20:42:43 +0000157
Ted Kremenekedc8aa62010-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 Dunbar0d7dd222009-11-30 20:42:43 +0000162
Ted Kremenekf1286182010-01-13 00:13:47 +0000163 Callback(Root, C, CData);
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000164 }
Ted Kremenekf1286182010-01-13 00:13:47 +0000165
Steve Naroff89922f82009-08-31 00:59:03 +0000166public:
Ted Kremenekf1286182010-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 Dunbar0d7dd222009-11-30 20:42:43 +0000169
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000170 void VisitDecl(Decl *D);
Ted Kremenekf1286182010-01-13 00:13:47 +0000171 void VisitDeclContext(DeclContext *DC);
Ted Kremenekf1286182010-01-13 00:13:47 +0000172 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
Ted Kremenekf1286182010-01-13 00:13:47 +0000173};
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000174
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000175void TUVisitor::VisitDecl(Decl *D) {
176 Call(MakeCXCursor(D));
177}
178
Ted Kremenekf1286182010-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 Kremenekedc8aa62010-01-16 00:36:30 +0000184
Ted Kremenekf1286182010-01-13 00:13:47 +0000185void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
186 VisitDeclContext(dyn_cast<DeclContext>(D));
187}
Steve Naroff89922f82009-08-31 00:59:03 +0000188
Steve Naroffc857ea42009-09-02 13:28:54 +0000189// Declaration visitor.
190class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
191 CXDecl CDecl;
192 CXDeclIterator Callback;
193 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000194
Douglas Gregor7d1d49d2009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000199
Steve Naroffc857ea42009-09-02 13:28:54 +0000200 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000204
Douglas Gregor7d1d49d2009-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 Dunbar0d7dd222009-11-30 20:42:43 +0000208
Douglas Gregor283cae32010-01-15 21:56:13 +0000209 CXCursor C = { CK, { ND, 0, 0 } };
Steve Naroffc857ea42009-09-02 13:28:54 +0000210 Callback(CDecl, C, CData);
211 }
Douglas Gregor2e331b92010-01-16 14:00:32 +0000212
Steve Naroff89922f82009-08-31 00:59:03 +0000213public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000214 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
215 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000216 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000217
Ted Kremenekdd6bcc52010-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 Naroff89922f82009-08-31 00:59:03 +0000233};
Ted Kremenekab188932010-01-05 19:32:54 +0000234} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000235
Ted Kremenekdd6bcc52010-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.
264 Call(CXCursor_ObjCClassRef, ND);
265 // FIXME: Issue callbacks for protocol refs.
266 VisitDeclContext(dyn_cast<DeclContext>(ND));
267}
268
269void CDeclVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
270 VisitDeclContext(dyn_cast<DeclContext>(D));
271}
272
273void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
274 VisitDeclContext(dyn_cast<DeclContext>(D));
275}
276
277void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
278 // Issue callbacks for super class.
279 if (D->getSuperClass())
Douglas Gregor2e331b92010-01-16 14:00:32 +0000280 Callback(CDecl,
281 MakeCursorObjCSuperClassRef(D->getSuperClass(),
282 D->getSuperClassLoc()),
283 CData);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000284
285 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
286 E = D->protocol_end(); I != E; ++I)
287 Call(CXCursor_ObjCProtocolRef, *I);
288 VisitDeclContext(dyn_cast<DeclContext>(D));
289}
290
291void CDeclVisitor::VisitObjCIvarDecl(ObjCIvarDecl *ND) {
292 Call(CXCursor_ObjCIvarDecl, ND);
293}
294
295void CDeclVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
296 if (ND->getBody()) {
297 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
298 : CXCursor_ObjCClassMethodDefn, ND);
299 VisitDeclContext(dyn_cast<DeclContext>(ND));
300 } else
301 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
302 : CXCursor_ObjCClassMethodDecl, ND);
303}
304
305void CDeclVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
306 Call(CXCursor_ObjCPropertyDecl, ND);
307}
308
309void CDeclVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
310 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
311 E = PID->protocol_end(); I != E; ++I)
312 Call(CXCursor_ObjCProtocolRef, *I);
313
314 VisitDeclContext(dyn_cast<DeclContext>(PID));
315}
316
317void CDeclVisitor::VisitParmVarDecl(ParmVarDecl *ND) {
318 Call(CXCursor_ParmDecl, ND);
319}
320
321void CDeclVisitor::VisitTagDecl(TagDecl *D) {
322 VisitDeclContext(dyn_cast<DeclContext>(D));
323}
324
325void CDeclVisitor::VisitVarDecl(VarDecl *ND) {
326 Call(CXCursor_VarDecl, ND);
327}
328
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000329static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000330 SourceManager &SourceMgr,
331 NamedDecl *ND) {
332 if (clang_isReference(C.kind)) {
Ted Kremenek8b456e82010-01-15 18:24:18 +0000333
Douglas Gregor283cae32010-01-15 21:56:13 +0000334 if (Decl *D = getCursorReferringDecl(C))
Ted Kremenek8b456e82010-01-15 18:24:18 +0000335 return D->getLocation();
336
Daniel Dunbarc6190332009-11-08 04:13:53 +0000337 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000338 case CXCursor_ObjCClassRef: {
339 if (isa<ObjCInterfaceDecl>(ND)) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000340 NamedDecl *parentDecl = getCursorInterfaceParent(C);
Daniel Dunbaracca7252009-11-30 20:42:49 +0000341 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000342 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000343 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
344 assert(OID && "clang_getCursorLine(): Missing category decl");
345 return OID->getClassInterface()->getLocation();
346 }
Douglas Gregor2e331b92010-01-16 14:00:32 +0000347 case CXCursor_ObjCSuperClassRef:
348 return getCursorObjCSuperClassRef(C).second;
349
Daniel Dunbaracca7252009-11-30 20:42:49 +0000350 case CXCursor_ObjCProtocolRef: {
351 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
352 assert(OID && "clang_getCursorLine(): Missing protocol decl");
353 return OID->getLocation();
354 }
355 case CXCursor_ObjCSelectorRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000356 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
Daniel Dunbaracca7252009-11-30 20:42:49 +0000357 assert(OME && "clang_getCursorLine(): Missing message expr");
358 return OME->getLeftLoc(); /* FIXME: should be a range */
359 }
360 case CXCursor_VarRef:
361 case CXCursor_FunctionRef:
362 case CXCursor_EnumConstantRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000363 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
Daniel Dunbaracca7252009-11-30 20:42:49 +0000364 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
365 return DRE->getLocation();
366 }
367 default:
368 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000369 }
370 } else { // We have a declaration or a definition.
371 SourceLocation SLoc;
372 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000373 case Decl::ObjCInterface: {
374 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
375 break;
376 }
377 case Decl::ObjCProtocol: {
378 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
379 break;
380 }
381 default: {
382 SLoc = ND->getLocation();
383 break;
384 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000385 }
386 if (SLoc.isInvalid())
387 return SourceLocation();
388 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
389 }
390}
391
Daniel Dunbar140fce22010-01-12 02:34:07 +0000392CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000393 CXString Str;
394 if (DupString) {
395 Str.Spelling = strdup(String);
396 Str.MustFreeString = 1;
397 } else {
398 Str.Spelling = String;
399 Str.MustFreeString = 0;
400 }
401 return Str;
402}
403
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000404extern "C" {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000405CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000406 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000407 CIndexer *CIdxr = new CIndexer(new Program());
408 if (excludeDeclarationsFromPCH)
409 CIdxr->setOnlyLocalDecls();
410 if (displayDiagnostics)
411 CIdxr->setDisplayDiagnostics();
412 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000413}
414
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000415void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000416 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000417 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000418}
419
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000420void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
421 assert(CIdx && "Passed null CXIndex");
422 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
423 CXXIdx->setUseExternalASTGeneration(value);
424}
425
Steve Naroff50398192009-08-28 15:28:48 +0000426// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000427CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
428 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000429 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000430 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000431
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000432 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
433 CXXIdx->getOnlyLocalDecls(),
434 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000435}
436
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000437CXTranslationUnit
438clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
439 const char *source_filename,
440 int num_command_line_args,
441 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000442 assert(CIdx && "Passed null CXIndex");
443 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
444
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000445 if (!CXXIdx->getUseExternalASTGeneration()) {
446 llvm::SmallVector<const char *, 16> Args;
447
448 // The 'source_filename' argument is optional. If the caller does not
449 // specify it then it is assumed that the source file is specified
450 // in the actual argument list.
451 if (source_filename)
452 Args.push_back(source_filename);
453 Args.insert(Args.end(), command_line_args,
454 command_line_args + num_command_line_args);
455
Daniel Dunbar94220972009-12-05 02:17:18 +0000456 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000457
Ted Kremenek29b72842010-01-07 22:49:05 +0000458#ifdef USE_CRASHTRACER
459 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000460#endif
461
Daniel Dunbar94220972009-12-05 02:17:18 +0000462 llvm::OwningPtr<ASTUnit> Unit(
463 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000464 CXXIdx->getDiags(),
465 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000466 CXXIdx->getOnlyLocalDecls(),
467 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000468
Daniel Dunbar94220972009-12-05 02:17:18 +0000469 // FIXME: Until we have broader testing, just drop the entire AST if we
470 // encountered an error.
471 if (NumErrors != CXXIdx->getDiags().getNumErrors())
472 return 0;
473
474 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000475 }
476
Ted Kremenek139ba862009-10-22 00:03:57 +0000477 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000478 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000479
Ted Kremenek139ba862009-10-22 00:03:57 +0000480 // First add the complete path to the 'clang' executable.
481 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000482 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000483
Ted Kremenek139ba862009-10-22 00:03:57 +0000484 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000485 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000486
Ted Kremenek139ba862009-10-22 00:03:57 +0000487 // The 'source_filename' argument is optional. If the caller does not
488 // specify it then it is assumed that the source file is specified
489 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000490 if (source_filename)
491 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000492
Steve Naroff37b5ac22009-10-15 20:50:09 +0000493 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000494 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000495 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000496 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000497
498 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
499 for (int i = 0; i < num_command_line_args; ++i)
500 if (const char *arg = command_line_args[i]) {
501 if (strcmp(arg, "-o") == 0) {
502 ++i; // Also skip the matching argument.
503 continue;
504 }
505 if (strcmp(arg, "-emit-ast") == 0 ||
506 strcmp(arg, "-c") == 0 ||
507 strcmp(arg, "-fsyntax-only") == 0) {
508 continue;
509 }
510
511 // Keep the argument.
512 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000513 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000514
Ted Kremenek139ba862009-10-22 00:03:57 +0000515 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000516 argv.push_back(NULL);
517
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000518 // Invoke 'clang'.
519 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
520 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000521 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000522 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000523 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
524 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
525 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000526
Ted Kremenek0854d702009-11-10 19:18:52 +0000527 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000528 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000529 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000530 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000531 I!=E; ++I) {
532 if (*I)
533 llvm::errs() << ' ' << *I << '\n';
534 }
535 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000536 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000537
Steve Naroff37b5ac22009-10-15 20:50:09 +0000538 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000539 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000540 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000541 if (ATU)
542 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000543 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000544}
545
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000546void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000547 assert(CTUnit && "Passed null CXTranslationUnit");
548 delete static_cast<ASTUnit *>(CTUnit);
549}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000550
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000551CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000552 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000553 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek4b333d22010-01-12 00:36:38 +0000554 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
555 true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000556}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000557
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000558void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000559 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000560 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000561 assert(CTUnit && "Passed null CXTranslationUnit");
562 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
563 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000564
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000565 unsigned PCHLevel = Decl::MaxPCHLevel;
566
567 // Set the PCHLevel to filter out unwanted decls if requested.
568 if (CXXUnit->getOnlyLocalDecls()) {
569 PCHLevel = 0;
570
571 // If the main input was an AST, bump the level.
572 if (CXXUnit->isMainFileAST())
573 ++PCHLevel;
574 }
575
576 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000577
578 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
579 // decls.
580 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
581 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
582 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
583 ie = TLDs.end(); it != ie; ++it) {
584 DVisit.Visit(*it);
585 }
586 } else
587 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000588}
589
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000590void clang_loadDeclaration(CXDecl Dcl,
591 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000592 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000593 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000594
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000595 CDeclVisitor DVisit(Dcl, callback, CData,
596 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000597 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000598}
Ted Kremenekfb480492010-01-13 21:46:36 +0000599} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +0000600
Ted Kremenekfb480492010-01-13 21:46:36 +0000601//===----------------------------------------------------------------------===//
Steve Naroff600866c2009-08-27 19:51:58 +0000602// CXDecl Operations.
Ted Kremenekfb480492010-01-13 21:46:36 +0000603//===----------------------------------------------------------------------===//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000604
Ted Kremenekfb480492010-01-13 21:46:36 +0000605static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
606 SourceLocation SLoc) {
607 FileID FID;
608 if (SLoc.isFileID())
609 FID = SMgr.getFileID(SLoc);
610 else
611 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
612 return SMgr.getFileEntryForID(FID);
613}
614
615extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000616CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000617 assert(AnonDecl && "Passed null CXDecl");
618 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000619
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000620 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000621 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
622 true);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000623
624 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000625 // No, this isn't the same as the code below. getIdentifier() is non-virtual
626 // and returns different names. NamedDecl returns the class name and
627 // ObjCCategoryImplDecl returns the category name.
Ted Kremenek4b333d22010-01-12 00:36:38 +0000628 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000629
630 if (ND->getIdentifier())
Ted Kremenek4b333d22010-01-12 00:36:38 +0000631 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000632
Ted Kremenek4b333d22010-01-12 00:36:38 +0000633 return CIndexer::createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000634}
Steve Narofff334b4e2009-09-02 18:26:48 +0000635
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000636unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000637 assert(AnonDecl && "Passed null CXDecl");
638 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
639 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
640 return SourceMgr.getSpellingLineNumber(ND->getLocation());
641}
642
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000643unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000644 assert(AnonDecl && "Passed null CXDecl");
645 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
646 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000647 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000648}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000649
650CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
651 assert(AnonDecl && "Passed null CXDecl");
652 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekd8210652010-01-06 23:43:31 +0000653 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000654 SourceRange R = ND->getSourceRange();
655
Ted Kremenekd8210652010-01-06 23:43:31 +0000656 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
657 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
658
659 if (!Begin.isValid()) {
660 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
661 return extent;
662 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000663
Ted Kremenekd8210652010-01-06 23:43:31 +0000664 // FIXME: This is largely copy-paste from
665 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
666 // what we want the two routines should be refactored.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000667
Ted Kremenekd8210652010-01-06 23:43:31 +0000668 // If the End location and the start location are the same and are a macro
669 // location, then the range was something that came from a macro expansion
670 // or _Pragma. If this is an object-like macro, the best we can do is to
671 // get the range. If this is a function-like macro, we'd also like to
672 // get the arguments.
673 if (Begin == End && R.getEnd().isMacroID())
674 End = SM.getInstantiationRange(R.getEnd()).second;
675
676 assert(SM.getFileID(Begin) == SM.getFileID(End));
677 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
678 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000679
Ted Kremenekd8210652010-01-06 23:43:31 +0000680 // Compute the column number of the start. Keep the column based at 1.
681 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
682
683 // Compute the column number of the end.
684 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
685 if (EndColNo) {
686 // Offset the end column by 1 so that we point to the last character
687 // in the last token.
688 --EndColNo;
689
690 // Add in the length of the token, so that we cover multi-char tokens.
691 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
692 const LangOptions &LOpts = Ctx.getLangOptions();
693
694 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
695 }
696
697 // Package up the line/column data and return to the caller.
698 CXDeclExtent extent = { { StartLineNo, StartColNo },
699 { EndLineNo, EndColNo } };
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000700 return extent;
701}
Steve Naroff699a07d2009-09-25 21:32:34 +0000702
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000703const char *clang_getDeclSource(CXDecl AnonDecl) {
704 assert(AnonDecl && "Passed null CXDecl");
705 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
706 assert(FEnt && "Cannot find FileEntry for Decl");
707 return clang_getFileName(FEnt);
708}
709
Steve Naroff88145032009-10-27 14:35:18 +0000710
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000711CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000712 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000713 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
714 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000715 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
716}
Ted Kremenekfb480492010-01-13 21:46:36 +0000717} // end: extern "C"
Steve Naroff88145032009-10-27 14:35:18 +0000718
Ted Kremenekfb480492010-01-13 21:46:36 +0000719//===----------------------------------------------------------------------===//
720// CXFile Operations.
721//===----------------------------------------------------------------------===//
722
723extern "C" {
Steve Naroff88145032009-10-27 14:35:18 +0000724const char *clang_getFileName(CXFile SFile) {
725 assert(SFile && "Passed null CXFile");
726 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
727 return FEnt->getName();
728}
729
730time_t clang_getFileTime(CXFile SFile) {
731 assert(SFile && "Passed null CXFile");
732 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
733 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000734}
Ted Kremenekfb480492010-01-13 21:46:36 +0000735} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +0000736
Ted Kremenekfb480492010-01-13 21:46:36 +0000737//===----------------------------------------------------------------------===//
738// CXCursor Operations.
739//===----------------------------------------------------------------------===//
740
Ted Kremenekfb480492010-01-13 21:46:36 +0000741static Decl *getDeclFromExpr(Stmt *E) {
742 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
743 return RefExpr->getDecl();
744 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
745 return ME->getMemberDecl();
746 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
747 return RE->getDecl();
748
749 if (CallExpr *CE = dyn_cast<CallExpr>(E))
750 return getDeclFromExpr(CE->getCallee());
751 if (CastExpr *CE = dyn_cast<CastExpr>(E))
752 return getDeclFromExpr(CE->getSubExpr());
753 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
754 return OME->getMethodDecl();
755
756 return 0;
757}
758
759extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000760CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000761 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
762 assert(ND && "CXCursor has null decl");
Steve Narofff334b4e2009-09-02 18:26:48 +0000763 if (clang_isReference(C.kind)) {
764 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000765 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +0000766 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
767 return CIndexer::createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000768 }
769 case CXCursor_ObjCClassRef: {
770 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000771 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000772
773 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
774 assert(OCD && "clang_getCursorLine(): Missing category decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000775 return CIndexer::createCXString(OCD->getClassInterface()->getIdentifier()
Daniel Dunbaracca7252009-11-30 20:42:49 +0000776 ->getNameStart());
777 }
778 case CXCursor_ObjCProtocolRef: {
779 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
780 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000781 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000782 }
783 case CXCursor_ObjCSelectorRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000784 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
Daniel Dunbaracca7252009-11-30 20:42:49 +0000785 assert(OME && "clang_getCursorLine(): Missing message expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000786 return CIndexer::createCXString(OME->getSelector().getAsString().c_str(),
787 true);
Daniel Dunbaracca7252009-11-30 20:42:49 +0000788 }
789 case CXCursor_VarRef:
790 case CXCursor_FunctionRef:
791 case CXCursor_EnumConstantRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000792 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
Daniel Dunbaracca7252009-11-30 20:42:49 +0000793 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000794 return CIndexer::createCXString(DRE->getDecl()->getIdentifier()
795 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000796 }
797 default:
Ted Kremenek4b333d22010-01-12 00:36:38 +0000798 return CIndexer::createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000799 }
800 }
Douglas Gregor283cae32010-01-15 21:56:13 +0000801 return clang_getDeclSpelling(getCursorDecl(C));
Steve Narofff334b4e2009-09-02 18:26:48 +0000802}
803
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000804const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000805 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000806 case CXCursor_FunctionDecl: return "FunctionDecl";
807 case CXCursor_TypedefDecl: return "TypedefDecl";
808 case CXCursor_EnumDecl: return "EnumDecl";
809 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
810 case CXCursor_StructDecl: return "StructDecl";
811 case CXCursor_UnionDecl: return "UnionDecl";
812 case CXCursor_ClassDecl: return "ClassDecl";
813 case CXCursor_FieldDecl: return "FieldDecl";
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000814 case CXCursor_FunctionDefn: return "FunctionDefn";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000815 case CXCursor_VarDecl: return "VarDecl";
816 case CXCursor_ParmDecl: return "ParmDecl";
817 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
818 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
819 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
820 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
821 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000822 case CXCursor_ObjCIvarRef: return "ObjCIvarRef";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000823 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
824 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000825 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000826 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000827 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
828 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000829 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
830 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
831 case CXCursor_ObjCClassRef: return "ObjCClassRef";
832 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000833
Daniel Dunbaracca7252009-11-30 20:42:49 +0000834 case CXCursor_VarRef: return "VarRef";
835 case CXCursor_FunctionRef: return "FunctionRef";
836 case CXCursor_EnumConstantRef: return "EnumConstantRef";
837 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000838
Daniel Dunbaracca7252009-11-30 20:42:49 +0000839 case CXCursor_InvalidFile: return "InvalidFile";
840 case CXCursor_NoDeclFound: return "NoDeclFound";
841 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000842 }
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000843
844 llvm_unreachable("Unhandled CXCursorKind");
845 return NULL;
Steve Naroff600866c2009-08-27 19:51:58 +0000846}
Steve Naroff89922f82009-08-31 00:59:03 +0000847
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000848CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000849 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000850 assert(CTUnit && "Passed null CXTranslationUnit");
851 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000852
Steve Naroff9efa7672009-09-04 15:44:05 +0000853 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000854 const FileEntry *File = FMgr.getFile(source_name,
855 source_name+strlen(source_name));
Ted Kremenekf4629892010-01-14 01:51:23 +0000856 if (!File)
857 return clang_getNullCursor();
858
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000859 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000860 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000861
Steve Narofff96b5242009-10-28 20:44:47 +0000862 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000863 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000864 &LastLoc);
Ted Kremenekf4629892010-01-14 01:51:23 +0000865
866 // FIXME: This doesn't look thread-safe.
Steve Narofff96b5242009-10-28 20:44:47 +0000867 if (ALoc.isValid())
868 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000869
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000870 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000871 if (ALoc.isNamedRef())
872 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000873 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000874 if (Dcl) {
875 if (Stm) {
Ted Kremenekfb480492010-01-13 21:46:36 +0000876 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm))
877 return MakeCXCursor(TranslateDeclRefExpr(DRE), Dcl, Stm);
878 else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm))
879 return MakeCXCursor(CXCursor_ObjCSelectorRef, Dcl, MExp);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000880 // Fall through...treat as a decl, not a ref.
881 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000882 if (ALoc.isNamedRef()) {
883 if (isa<ObjCInterfaceDecl>(Dcl)) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000884 CXCursor C = { CXCursor_ObjCClassRef, { Dcl, ALoc.getParentDecl(), 0 }};
Steve Naroff85e2db72009-10-01 00:31:07 +0000885 return C;
886 }
887 if (isa<ObjCProtocolDecl>(Dcl)) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000888 CXCursor C = {CXCursor_ObjCProtocolRef, {Dcl, ALoc.getParentDecl(), 0}};
Steve Naroff85e2db72009-10-01 00:31:07 +0000889 return C;
890 }
891 }
Ted Kremenek70ee5422010-01-16 01:44:12 +0000892 return MakeCXCursor(Dcl);
Steve Naroff77128dd2009-09-15 20:25:34 +0000893 }
Ted Kremenekfb480492010-01-13 21:46:36 +0000894 return MakeCXCursor(CXCursor_NoDeclFound, 0);
Steve Naroff600866c2009-08-27 19:51:58 +0000895}
896
Ted Kremenek73885552009-11-17 19:28:59 +0000897CXCursor clang_getNullCursor(void) {
Ted Kremenekfb480492010-01-13 21:46:36 +0000898 return MakeCXCursor(CXCursor_InvalidFile, 0);
Ted Kremenek73885552009-11-17 19:28:59 +0000899}
900
901unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000902 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +0000903}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000904
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000905CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000906 assert(AnonDecl && "Passed null CXDecl");
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000907 return MakeCXCursor(static_cast<NamedDecl *>(AnonDecl));
Steve Naroff77128dd2009-09-15 20:25:34 +0000908}
909
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000910unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000911 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
912}
913
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000914unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000915 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
916}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000917
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000918unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000919 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
920}
921
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000922unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000923 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
924}
925
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000926CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000927 return C.kind;
928}
929
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000930CXDecl clang_getCursorDecl(CXCursor C) {
Ted Kremenek70ee5422010-01-16 01:44:12 +0000931 if (clang_isDeclaration(C.kind) || clang_isDefinition(C.kind))
Douglas Gregor283cae32010-01-15 21:56:13 +0000932 return getCursorDecl(C);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000933
Steve Naroff699a07d2009-09-25 21:32:34 +0000934 if (clang_isReference(C.kind)) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000935 if (getCursorStmt(C)) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000936 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +0000937 C.kind == CXCursor_ObjCProtocolRef)
Douglas Gregor283cae32010-01-15 21:56:13 +0000938 return getCursorStmt(C);
Steve Naroff85e2db72009-10-01 00:31:07 +0000939 else
Douglas Gregor283cae32010-01-15 21:56:13 +0000940 return getDeclFromExpr(getCursorStmt(C));
Steve Naroff85e2db72009-10-01 00:31:07 +0000941 } else
Douglas Gregor283cae32010-01-15 21:56:13 +0000942 return getCursorDecl(C);
Steve Naroff699a07d2009-09-25 21:32:34 +0000943 }
944 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000945}
946
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000947unsigned clang_getCursorLine(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000948 assert(getCursorDecl(C) && "CXCursor has null decl");
949 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff2d4d6292009-08-31 14:26:51 +0000950 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000951
Steve Narofff334b4e2009-09-02 18:26:48 +0000952 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000953 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000954}
Ted Kremenekfb480492010-01-13 21:46:36 +0000955
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000956unsigned clang_getCursorColumn(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000957 assert(getCursorDecl(C) && "CXCursor has null decl");
958 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff2d4d6292009-08-31 14:26:51 +0000959 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +0000960
Steve Narofff334b4e2009-09-02 18:26:48 +0000961 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000962 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +0000963}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000964
965const char *clang_getCursorSource(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000966 assert(getCursorDecl(C) && "CXCursor has null decl");
967 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff2d4d6292009-08-31 14:26:51 +0000968 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +0000969
Steve Narofff334b4e2009-09-02 18:26:48 +0000970 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Ted Kremenekfb480492010-01-13 21:46:36 +0000971
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000972 if (SLoc.isFileID()) {
973 const char *bufferName = SourceMgr.getBufferName(SLoc);
974 return bufferName[0] == '<' ? NULL : bufferName;
975 }
Ted Kremenekfb480492010-01-13 21:46:36 +0000976
Douglas Gregor02465752009-10-16 21:24:31 +0000977 // Retrieve the file in which the macro was instantiated, then provide that
978 // buffer name.
979 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000980 const llvm::MemoryBuffer *Buffer
Ted Kremenekfb480492010-01-13 21:46:36 +0000981 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
Douglas Gregor02465752009-10-16 21:24:31 +0000982 if (!Buffer)
983 return 0;
Ted Kremenekfb480492010-01-13 21:46:36 +0000984
Douglas Gregor02465752009-10-16 21:24:31 +0000985 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +0000986}
987
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000988CXFile clang_getCursorSourceFile(CXCursor C) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000989 assert(getCursorDecl(C) && "CXCursor has null decl");
990 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff88145032009-10-27 14:35:18 +0000991 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Ted Kremenekfb480492010-01-13 21:46:36 +0000992
Ted Kremenek4b333d22010-01-12 00:36:38 +0000993 return (void *)
Ted Kremenekfb480492010-01-13 21:46:36 +0000994 getFileEntryFromSourceLocation(SourceMgr, getLocationFromCursor(C,SourceMgr,
995 ND));
Steve Naroff88145032009-10-27 14:35:18 +0000996}
997
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000998void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000999 const char **startBuf,
1000 const char **endBuf,
1001 unsigned *startLine,
1002 unsigned *startColumn,
1003 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001004 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001005 assert(getCursorDecl(C) && "CXCursor has null decl");
1006 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001007 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1008 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekfb480492010-01-13 21:46:36 +00001009
Steve Naroff4ade6d62009-09-23 17:52:52 +00001010 SourceManager &SM = FD->getASTContext().getSourceManager();
1011 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1012 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1013 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1014 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1015 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1016 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1017}
Ted Kremenekfb480492010-01-13 21:46:36 +00001018
1019} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001020
Ted Kremenekfb480492010-01-13 21:46:36 +00001021//===----------------------------------------------------------------------===//
1022// CXString Operations.
1023//===----------------------------------------------------------------------===//
1024
1025extern "C" {
1026const char *clang_getCString(CXString string) {
1027 return string.Spelling;
1028}
1029
1030void clang_disposeString(CXString string) {
1031 if (string.MustFreeString && string.Spelling)
1032 free((void*)string.Spelling);
1033}
1034} // end: extern "C"