blob: 8e579f139d565d0922b89e3f66845a01e21b2a3a [file] [log] [blame]
Ted Kremenekd2fa5662009-08-26 22:36:44 +00001//===- CIndex.cpp - Clang-C Source Indexing Library -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00007//
Ted Kremenekd2fa5662009-08-26 22:36:44 +00008//===----------------------------------------------------------------------===//
9//
Ted Kremenekab188932010-01-05 19:32:54 +000010// This file implements the main API hooks in the Clang-C Source Indexing
11// library.
Ted Kremenekd2fa5662009-08-26 22:36:44 +000012//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekab188932010-01-05 19:32:54 +000015#include "CIndexer.h"
16
Steve Naroff50398192009-08-28 15:28:48 +000017#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000018#include "clang/AST/StmtVisitor.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000019#include "clang/Lex/Lexer.h"
Douglas Gregor02465752009-10-16 21:24:31 +000020#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000021#include "llvm/System/Program.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000022
Ted Kremenekdb3d0da2010-01-05 20:55:39 +000023// Needed to define L_TMPNAM on some systems.
24#include <cstdio>
25
Steve Naroff50398192009-08-28 15:28:48 +000026using namespace clang;
27using namespace idx;
28
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000029//===----------------------------------------------------------------------===//
30// Crash Reporting.
31//===----------------------------------------------------------------------===//
32
33#ifdef __APPLE__
Ted Kremenek29b72842010-01-07 22:49:05 +000034#ifndef NDEBUG
35#define USE_CRASHTRACER
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000036#include "clang/Analysis/Support/SaveAndRestore.h"
37// Integrate with crash reporter.
38extern "C" const char *__crashreporter_info__;
Ted Kremenek29b72842010-01-07 22:49:05 +000039#define NUM_CRASH_STRINGS 16
40static unsigned crashtracer_counter = 0;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000041static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek29b72842010-01-07 22:49:05 +000042static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
43static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
44
45static unsigned SetCrashTracerInfo(const char *str,
46 llvm::SmallString<1024> &AggStr) {
47
Ted Kremenek254ba7c2010-01-07 23:13:53 +000048 unsigned slot = 0;
Ted Kremenek29b72842010-01-07 22:49:05 +000049 while (crashtracer_strings[slot]) {
50 if (++slot == NUM_CRASH_STRINGS)
51 slot = 0;
52 }
53 crashtracer_strings[slot] = str;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000054 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek29b72842010-01-07 22:49:05 +000055
56 // We need to create an aggregate string because multiple threads
57 // may be in this method at one time. The crash reporter string
58 // will attempt to overapproximate the set of in-flight invocations
59 // of this function. Race conditions can still cause this goal
60 // to not be achieved.
61 {
62 llvm::raw_svector_ostream Out(AggStr);
63 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
64 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
65 }
66 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
67 return slot;
68}
69
70static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek254ba7c2010-01-07 23:13:53 +000071 unsigned max_slot = 0;
72 unsigned max_value = 0;
73
74 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
75
76 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
77 if (agg_crashtracer_strings[i] &&
78 crashtracer_counter_id[i] > max_value) {
79 max_slot = i;
80 max_value = crashtracer_counter_id[i];
Ted Kremenek29b72842010-01-07 22:49:05 +000081 }
Ted Kremenek254ba7c2010-01-07 23:13:53 +000082
83 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek29b72842010-01-07 22:49:05 +000084}
85
86namespace {
87class ArgsCrashTracerInfo {
88 llvm::SmallString<1024> CrashString;
89 llvm::SmallString<1024> AggregateString;
90 unsigned crashtracerSlot;
91public:
92 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
93 : crashtracerSlot(0)
94 {
95 {
96 llvm::raw_svector_ostream Out(CrashString);
97 Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
98 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
99 E=Args.end(); I!=E; ++I)
100 Out << ' ' << *I;
101 }
102 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
103 AggregateString);
104 }
105
106 ~ArgsCrashTracerInfo() {
107 ResetCrashTracerInfo(crashtracerSlot);
108 }
109};
110}
111#endif
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000112#endif
113
114//===----------------------------------------------------------------------===//
115// Visitors.
116//===----------------------------------------------------------------------===//
117
Steve Naroff89922f82009-08-31 00:59:03 +0000118namespace {
Ted Kremenekab188932010-01-05 19:32:54 +0000119static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000120 NamedDecl *D = DRE->getDecl();
121 if (isa<VarDecl>(D))
122 return CXCursor_VarRef;
123 else if (isa<FunctionDecl>(D))
124 return CXCursor_FunctionRef;
125 else if (isa<EnumConstantDecl>(D))
126 return CXCursor_EnumConstantRef;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000127 else
Steve Naroff4ade6d62009-09-23 17:52:52 +0000128 return CXCursor_NotImplemented;
129}
130
131#if 0
132// Will be useful one day.
Steve Narofffb570422009-09-22 19:25:29 +0000133class CRefVisitor : public StmtVisitor<CRefVisitor> {
134 CXDecl CDecl;
135 CXDeclIterator Callback;
136 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000137
Steve Narofffb570422009-09-22 19:25:29 +0000138 void Call(enum CXCursorKind CK, Stmt *SRef) {
139 CXCursor C = { CK, CDecl, SRef };
140 Callback(CDecl, C, CData);
141 }
142
143public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000144 CRefVisitor(CXDecl C, CXDeclIterator cback, CXClientData D) :
Steve Narofffb570422009-09-22 19:25:29 +0000145 CDecl(C), Callback(cback), CData(D) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000146
Steve Narofffb570422009-09-22 19:25:29 +0000147 void VisitStmt(Stmt *S) {
148 for (Stmt::child_iterator C = S->child_begin(), CEnd = S->child_end();
149 C != CEnd; ++C)
150 Visit(*C);
151 }
152 void VisitDeclRefExpr(DeclRefExpr *Node) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000153 Call(TranslateDeclRefExpr(Node), Node);
Steve Narofffb570422009-09-22 19:25:29 +0000154 }
155 void VisitMemberExpr(MemberExpr *Node) {
156 Call(CXCursor_MemberRef, Node);
157 }
158 void VisitObjCMessageExpr(ObjCMessageExpr *Node) {
159 Call(CXCursor_ObjCSelectorRef, Node);
160 }
161 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *Node) {
162 Call(CXCursor_ObjCIvarRef, Node);
163 }
164};
Steve Naroff4ade6d62009-09-23 17:52:52 +0000165#endif
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000166
Steve Naroff89922f82009-08-31 00:59:03 +0000167// Translation Unit Visitor.
168class TUVisitor : public DeclVisitor<TUVisitor> {
169 CXTranslationUnit TUnit;
170 CXTranslationUnitIterator Callback;
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000171 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000172
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000173 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
174 // to the visitor. Declarations with a PCH level greater than this value will
175 // be suppressed.
176 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000177
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000178 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000179 // Filter any declarations that have a PCH level greater than what we allow.
180 if (ND->getPCHLevel() > MaxPCHLevel)
181 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000182
Steve Narofff96b5242009-10-28 20:44:47 +0000183 // Filter any implicit declarations (since the source info will be bogus).
184 if (ND->isImplicit())
185 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000186
Steve Narofffb570422009-09-22 19:25:29 +0000187 CXCursor C = { CK, ND, 0 };
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000188 Callback(TUnit, C, CData);
189 }
Steve Naroff89922f82009-08-31 00:59:03 +0000190public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000191 TUVisitor(CXTranslationUnit CTU,
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000192 CXTranslationUnitIterator cback, CXClientData D,
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000193 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000194 TUnit(CTU), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000195
Steve Naroff89922f82009-08-31 00:59:03 +0000196 void VisitTranslationUnitDecl(TranslationUnitDecl *D) {
197 VisitDeclContext(dyn_cast<DeclContext>(D));
198 }
199 void VisitDeclContext(DeclContext *DC) {
200 for (DeclContext::decl_iterator
201 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
202 Visit(*I);
203 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000204
205 void VisitFunctionDecl(FunctionDecl *ND) {
206 Call(ND->isThisDeclarationADefinition() ? CXCursor_FunctionDefn
207 : CXCursor_FunctionDecl, ND);
208 }
209 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
210 Call(CXCursor_ObjCCategoryDecl, ND);
211 }
212 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *ND) {
213 Call(CXCursor_ObjCCategoryDefn, ND);
214 }
215 void VisitObjCImplementationDecl(ObjCImplementationDecl *ND) {
216 Call(CXCursor_ObjCClassDefn, ND);
217 }
218 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *ND) {
219 Call(CXCursor_ObjCInterfaceDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000220 }
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000221 void VisitObjCProtocolDecl(ObjCProtocolDecl *ND) {
222 Call(CXCursor_ObjCProtocolDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000223 }
224 void VisitTagDecl(TagDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000225 switch (ND->getTagKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000226 case TagDecl::TK_struct:
227 Call(CXCursor_StructDecl, ND);
228 break;
229 case TagDecl::TK_class:
230 Call(CXCursor_ClassDecl, ND);
231 break;
232 case TagDecl::TK_union:
233 Call(CXCursor_UnionDecl, ND);
234 break;
235 case TagDecl::TK_enum:
236 Call(CXCursor_EnumDecl, ND);
237 break;
Steve Naroffc857ea42009-09-02 13:28:54 +0000238 }
Steve Naroff89922f82009-08-31 00:59:03 +0000239 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000240 void VisitTypedefDecl(TypedefDecl *ND) {
241 Call(CXCursor_TypedefDecl, ND);
242 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000243 void VisitVarDecl(VarDecl *ND) {
244 Call(CXCursor_VarDecl, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000245 }
Steve Naroff89922f82009-08-31 00:59:03 +0000246};
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000247
Steve Naroff89922f82009-08-31 00:59:03 +0000248
Steve Naroffc857ea42009-09-02 13:28:54 +0000249// Declaration visitor.
250class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
251 CXDecl CDecl;
252 CXDeclIterator Callback;
253 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000254
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000255 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
256 // to the visitor. Declarations with a PCH level greater than this value will
257 // be suppressed.
258 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000259
Steve Naroffc857ea42009-09-02 13:28:54 +0000260 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000261 // Disable the callback when the context is equal to the visiting decl.
262 if (CDecl == ND && !clang_isReference(CK))
263 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000264
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000265 // Filter any declarations that have a PCH level greater than what we allow.
266 if (ND->getPCHLevel() > MaxPCHLevel)
267 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000268
Steve Narofffb570422009-09-22 19:25:29 +0000269 CXCursor C = { CK, ND, 0 };
Steve Naroffc857ea42009-09-02 13:28:54 +0000270 Callback(CDecl, C, CData);
271 }
Steve Naroff89922f82009-08-31 00:59:03 +0000272public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000273 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
274 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000275 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000276
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000277 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
278 // Issue callbacks for the containing class.
279 Call(CXCursor_ObjCClassRef, ND);
280 // FIXME: Issue callbacks for protocol refs.
281 VisitDeclContext(dyn_cast<DeclContext>(ND));
282 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000283 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000284 // Issue callbacks for super class.
Steve Narofff334b4e2009-09-02 18:26:48 +0000285 if (D->getSuperClass())
286 Call(CXCursor_ObjCSuperClassRef, D);
287
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000288 for (ObjCProtocolDecl::protocol_iterator I = D->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000289 E = D->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000290 Call(CXCursor_ObjCProtocolRef, *I);
Steve Naroffc857ea42009-09-02 13:28:54 +0000291 VisitDeclContext(dyn_cast<DeclContext>(D));
292 }
Steve Naroff9efa7672009-09-04 15:44:05 +0000293 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000294 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Daniel Dunbaracca7252009-11-30 20:42:49 +0000295 E = PID->protocol_end(); I != E; ++I)
Steve Naroff9efa7672009-09-04 15:44:05 +0000296 Call(CXCursor_ObjCProtocolRef, *I);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000297
Steve Naroff9efa7672009-09-04 15:44:05 +0000298 VisitDeclContext(dyn_cast<DeclContext>(PID));
299 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000300 void VisitTagDecl(TagDecl *D) {
301 VisitDeclContext(dyn_cast<DeclContext>(D));
302 }
303 void VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
304 VisitDeclContext(dyn_cast<DeclContext>(D));
305 }
306 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
307 VisitDeclContext(dyn_cast<DeclContext>(D));
308 }
Steve Naroff89922f82009-08-31 00:59:03 +0000309 void VisitDeclContext(DeclContext *DC) {
310 for (DeclContext::decl_iterator
311 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
312 Visit(*I);
313 }
314 void VisitEnumConstantDecl(EnumConstantDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000315 Call(CXCursor_EnumConstantDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000316 }
317 void VisitFieldDecl(FieldDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000318 Call(CXCursor_FieldDecl, ND);
319 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000320 void VisitVarDecl(VarDecl *ND) {
321 Call(CXCursor_VarDecl, ND);
322 }
323 void VisitParmVarDecl(ParmVarDecl *ND) {
324 Call(CXCursor_ParmDecl, ND);
325 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000326 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
327 Call(CXCursor_ObjCPropertyDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000328 }
329 void VisitObjCIvarDecl(ObjCIvarDecl *ND) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000330 Call(CXCursor_ObjCIvarDecl, ND);
331 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000332 void VisitFunctionDecl(FunctionDecl *ND) {
333 if (ND->isThisDeclarationADefinition()) {
334 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000335#if 0
336 // Not currently needed.
337 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
Steve Narofffb570422009-09-22 19:25:29 +0000338 CRefVisitor RVisit(CDecl, Callback, CData);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000339 RVisit.Visit(Body);
340#endif
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000341 }
342 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000343 void VisitObjCMethodDecl(ObjCMethodDecl *ND) {
344 if (ND->getBody()) {
345 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
346 : CXCursor_ObjCClassMethodDefn, ND);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000347 VisitDeclContext(dyn_cast<DeclContext>(ND));
Steve Naroffc857ea42009-09-02 13:28:54 +0000348 } else
349 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
350 : CXCursor_ObjCClassMethodDecl, ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000351 }
352};
Ted Kremenekab188932010-01-05 19:32:54 +0000353} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000354
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000355static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000356 SourceManager &SourceMgr,
357 NamedDecl *ND) {
358 if (clang_isReference(C.kind)) {
359 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000360 case CXCursor_ObjCClassRef: {
361 if (isa<ObjCInterfaceDecl>(ND)) {
362 // FIXME: This is a hack (storing the parent decl in the stmt slot).
363 NamedDecl *parentDecl = static_cast<NamedDecl *>(C.stmt);
364 return parentDecl->getLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000365 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000366 ObjCCategoryDecl *OID = dyn_cast<ObjCCategoryDecl>(ND);
367 assert(OID && "clang_getCursorLine(): Missing category decl");
368 return OID->getClassInterface()->getLocation();
369 }
370 case CXCursor_ObjCSuperClassRef: {
371 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
372 assert(OID && "clang_getCursorLine(): Missing interface decl");
373 return OID->getSuperClassLoc();
374 }
375 case CXCursor_ObjCProtocolRef: {
376 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
377 assert(OID && "clang_getCursorLine(): Missing protocol decl");
378 return OID->getLocation();
379 }
380 case CXCursor_ObjCSelectorRef: {
381 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
382 static_cast<Stmt *>(C.stmt));
383 assert(OME && "clang_getCursorLine(): Missing message expr");
384 return OME->getLeftLoc(); /* FIXME: should be a range */
385 }
386 case CXCursor_VarRef:
387 case CXCursor_FunctionRef:
388 case CXCursor_EnumConstantRef: {
389 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
390 static_cast<Stmt *>(C.stmt));
391 assert(DRE && "clang_getCursorLine(): Missing decl ref expr");
392 return DRE->getLocation();
393 }
394 default:
395 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000396 }
397 } else { // We have a declaration or a definition.
398 SourceLocation SLoc;
399 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000400 case Decl::ObjCInterface: {
401 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
402 break;
403 }
404 case Decl::ObjCProtocol: {
405 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
406 break;
407 }
408 default: {
409 SLoc = ND->getLocation();
410 break;
411 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000412 }
413 if (SLoc.isInvalid())
414 return SourceLocation();
415 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
416 }
417}
418
Daniel Dunbar140fce22010-01-12 02:34:07 +0000419CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000420 CXString Str;
421 if (DupString) {
422 Str.Spelling = strdup(String);
423 Str.MustFreeString = 1;
424 } else {
425 Str.Spelling = String;
426 Str.MustFreeString = 0;
427 }
428 return Str;
429}
430
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000431extern "C" {
432
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000433CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000434 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000435 CIndexer *CIdxr = new CIndexer(new Program());
436 if (excludeDeclarationsFromPCH)
437 CIdxr->setOnlyLocalDecls();
438 if (displayDiagnostics)
439 CIdxr->setDisplayDiagnostics();
440 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000441}
442
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000443void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000444 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000445 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000446}
447
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000448void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
449 assert(CIdx && "Passed null CXIndex");
450 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
451 CXXIdx->setUseExternalASTGeneration(value);
452}
453
Steve Naroff50398192009-08-28 15:28:48 +0000454// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000455CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
456 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000457 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000458 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000459
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000460 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
461 CXXIdx->getOnlyLocalDecls(),
462 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000463}
464
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000465CXTranslationUnit
466clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
467 const char *source_filename,
468 int num_command_line_args,
469 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000470 assert(CIdx && "Passed null CXIndex");
471 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
472
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000473 if (!CXXIdx->getUseExternalASTGeneration()) {
474 llvm::SmallVector<const char *, 16> Args;
475
476 // The 'source_filename' argument is optional. If the caller does not
477 // specify it then it is assumed that the source file is specified
478 // in the actual argument list.
479 if (source_filename)
480 Args.push_back(source_filename);
481 Args.insert(Args.end(), command_line_args,
482 command_line_args + num_command_line_args);
483
Daniel Dunbar94220972009-12-05 02:17:18 +0000484 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000485
Ted Kremenek29b72842010-01-07 22:49:05 +0000486#ifdef USE_CRASHTRACER
487 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000488#endif
489
Daniel Dunbar94220972009-12-05 02:17:18 +0000490 llvm::OwningPtr<ASTUnit> Unit(
491 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000492 CXXIdx->getDiags(),
493 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000494 CXXIdx->getOnlyLocalDecls(),
495 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000496
Daniel Dunbar94220972009-12-05 02:17:18 +0000497 // FIXME: Until we have broader testing, just drop the entire AST if we
498 // encountered an error.
499 if (NumErrors != CXXIdx->getDiags().getNumErrors())
500 return 0;
501
502 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000503 }
504
Ted Kremenek139ba862009-10-22 00:03:57 +0000505 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000506 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000507
Ted Kremenek139ba862009-10-22 00:03:57 +0000508 // First add the complete path to the 'clang' executable.
509 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000510 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000511
Ted Kremenek139ba862009-10-22 00:03:57 +0000512 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000513 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000514
Ted Kremenek139ba862009-10-22 00:03:57 +0000515 // The 'source_filename' argument is optional. If the caller does not
516 // specify it then it is assumed that the source file is specified
517 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000518 if (source_filename)
519 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000520
Steve Naroff37b5ac22009-10-15 20:50:09 +0000521 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000522 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000523 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000524 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000525
526 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
527 for (int i = 0; i < num_command_line_args; ++i)
528 if (const char *arg = command_line_args[i]) {
529 if (strcmp(arg, "-o") == 0) {
530 ++i; // Also skip the matching argument.
531 continue;
532 }
533 if (strcmp(arg, "-emit-ast") == 0 ||
534 strcmp(arg, "-c") == 0 ||
535 strcmp(arg, "-fsyntax-only") == 0) {
536 continue;
537 }
538
539 // Keep the argument.
540 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000541 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000542
Ted Kremenek139ba862009-10-22 00:03:57 +0000543 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000544 argv.push_back(NULL);
545
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000546 // Invoke 'clang'.
547 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
548 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000549 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000550 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000551 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
552 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
553 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000554
Ted Kremenek0854d702009-11-10 19:18:52 +0000555 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000556 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000557 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000558 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000559 I!=E; ++I) {
560 if (*I)
561 llvm::errs() << ' ' << *I << '\n';
562 }
563 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000564 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000565
Steve Naroff37b5ac22009-10-15 20:50:09 +0000566 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000567 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000568 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000569 if (ATU)
570 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000571 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000572}
573
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000574void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000575 assert(CTUnit && "Passed null CXTranslationUnit");
576 delete static_cast<ASTUnit *>(CTUnit);
577}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000578
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000579CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000580 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000581 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek4b333d22010-01-12 00:36:38 +0000582 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
583 true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000584}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000585
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000586void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000587 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000588 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000589 assert(CTUnit && "Passed null CXTranslationUnit");
590 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
591 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000592
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000593 unsigned PCHLevel = Decl::MaxPCHLevel;
594
595 // Set the PCHLevel to filter out unwanted decls if requested.
596 if (CXXUnit->getOnlyLocalDecls()) {
597 PCHLevel = 0;
598
599 // If the main input was an AST, bump the level.
600 if (CXXUnit->isMainFileAST())
601 ++PCHLevel;
602 }
603
604 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000605
606 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
607 // decls.
608 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
609 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
610 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
611 ie = TLDs.end(); it != ie; ++it) {
612 DVisit.Visit(*it);
613 }
614 } else
615 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000616}
617
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000618void clang_loadDeclaration(CXDecl Dcl,
619 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000620 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000621 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000622
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000623 CDeclVisitor DVisit(Dcl, callback, CData,
624 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000625 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000626}
627
Steve Naroff600866c2009-08-27 19:51:58 +0000628//
629// CXDecl Operations.
630//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000631
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000632CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000633 assert(AnonDecl && "Passed null CXDecl");
634 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000635
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000636 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000637 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
638 true);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000639
640 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000641 // No, this isn't the same as the code below. getIdentifier() is non-virtual
642 // and returns different names. NamedDecl returns the class name and
643 // ObjCCategoryImplDecl returns the category name.
Ted Kremenek4b333d22010-01-12 00:36:38 +0000644 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000645
646 if (ND->getIdentifier())
Ted Kremenek4b333d22010-01-12 00:36:38 +0000647 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000648
Ted Kremenek4b333d22010-01-12 00:36:38 +0000649 return CIndexer::createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000650}
Steve Narofff334b4e2009-09-02 18:26:48 +0000651
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000652unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000653 assert(AnonDecl && "Passed null CXDecl");
654 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
655 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
656 return SourceMgr.getSpellingLineNumber(ND->getLocation());
657}
658
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000659unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000660 assert(AnonDecl && "Passed null CXDecl");
661 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
662 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000663 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000664}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000665
666CXDeclExtent clang_getDeclExtent(CXDecl AnonDecl) {
667 assert(AnonDecl && "Passed null CXDecl");
668 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekd8210652010-01-06 23:43:31 +0000669 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000670 SourceRange R = ND->getSourceRange();
671
Ted Kremenekd8210652010-01-06 23:43:31 +0000672 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
673 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
674
675 if (!Begin.isValid()) {
676 CXDeclExtent extent = { { 0, 0 }, { 0, 0 } };
677 return extent;
678 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000679
Ted Kremenekd8210652010-01-06 23:43:31 +0000680 // FIXME: This is largely copy-paste from
681 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
682 // what we want the two routines should be refactored.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000683
Ted Kremenekd8210652010-01-06 23:43:31 +0000684 // If the End location and the start location are the same and are a macro
685 // location, then the range was something that came from a macro expansion
686 // or _Pragma. If this is an object-like macro, the best we can do is to
687 // get the range. If this is a function-like macro, we'd also like to
688 // get the arguments.
689 if (Begin == End && R.getEnd().isMacroID())
690 End = SM.getInstantiationRange(R.getEnd()).second;
691
692 assert(SM.getFileID(Begin) == SM.getFileID(End));
693 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
694 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000695
Ted Kremenekd8210652010-01-06 23:43:31 +0000696 // Compute the column number of the start. Keep the column based at 1.
697 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
698
699 // Compute the column number of the end.
700 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
701 if (EndColNo) {
702 // Offset the end column by 1 so that we point to the last character
703 // in the last token.
704 --EndColNo;
705
706 // Add in the length of the token, so that we cover multi-char tokens.
707 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
708 const LangOptions &LOpts = Ctx.getLangOptions();
709
710 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
711 }
712
713 // Package up the line/column data and return to the caller.
714 CXDeclExtent extent = { { StartLineNo, StartColNo },
715 { EndLineNo, EndColNo } };
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000716 return extent;
717}
Steve Naroff699a07d2009-09-25 21:32:34 +0000718
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000719const char *clang_getDeclSource(CXDecl AnonDecl) {
720 assert(AnonDecl && "Passed null CXDecl");
721 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
722 assert(FEnt && "Cannot find FileEntry for Decl");
723 return clang_getFileName(FEnt);
724}
725
Steve Naroff88145032009-10-27 14:35:18 +0000726static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000727 SourceLocation SLoc) {
Steve Naroff88145032009-10-27 14:35:18 +0000728 FileID FID;
729 if (SLoc.isFileID())
730 FID = SMgr.getFileID(SLoc);
731 else
732 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
733 return SMgr.getFileEntryForID(FID);
734}
735
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000736CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000737 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000738 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
739 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000740 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
741}
742
743const char *clang_getFileName(CXFile SFile) {
744 assert(SFile && "Passed null CXFile");
745 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
746 return FEnt->getName();
747}
748
749time_t clang_getFileTime(CXFile SFile) {
750 assert(SFile && "Passed null CXFile");
751 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
752 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000753}
754
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000755CXString clang_getCursorSpelling(CXCursor C) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000756 assert(C.decl && "CXCursor has null decl");
757 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000758
Steve Narofff334b4e2009-09-02 18:26:48 +0000759 if (clang_isReference(C.kind)) {
760 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000761 case CXCursor_ObjCSuperClassRef: {
762 ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND);
763 assert(OID && "clang_getCursorLine(): Missing interface decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000764 return CIndexer::createCXString(OID->getSuperClass()->getIdentifier()
765 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000766 }
767 case CXCursor_ObjCClassRef: {
768 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000769 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000770
771 ObjCCategoryDecl *OCD = dyn_cast<ObjCCategoryDecl>(ND);
772 assert(OCD && "clang_getCursorLine(): Missing category decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000773 return CIndexer::createCXString(OCD->getClassInterface()->getIdentifier()
Daniel Dunbaracca7252009-11-30 20:42:49 +0000774 ->getNameStart());
775 }
776 case CXCursor_ObjCProtocolRef: {
777 ObjCProtocolDecl *OID = dyn_cast<ObjCProtocolDecl>(ND);
778 assert(OID && "clang_getCursorLine(): Missing protocol decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000779 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000780 }
781 case CXCursor_ObjCSelectorRef: {
782 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(
783 static_cast<Stmt *>(C.stmt));
784 assert(OME && "clang_getCursorLine(): Missing message expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000785 return CIndexer::createCXString(OME->getSelector().getAsString().c_str(),
786 true);
Daniel Dunbaracca7252009-11-30 20:42:49 +0000787 }
788 case CXCursor_VarRef:
789 case CXCursor_FunctionRef:
790 case CXCursor_EnumConstantRef: {
791 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(
792 static_cast<Stmt *>(C.stmt));
793 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 }
801 return clang_getDeclSpelling(C.decl);
802}
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";
814 case CXCursor_VarDecl: return "VarDecl";
815 case CXCursor_ParmDecl: return "ParmDecl";
816 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
817 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
818 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
819 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
820 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
821 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
822 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
823 case CXCursor_FunctionDefn: return "FunctionDefn";
824 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
825 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
826 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
827 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
828 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
829 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
830 case CXCursor_ObjCClassRef: return "ObjCClassRef";
831 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000832
Daniel Dunbaracca7252009-11-30 20:42:49 +0000833 case CXCursor_VarRef: return "VarRef";
834 case CXCursor_FunctionRef: return "FunctionRef";
835 case CXCursor_EnumConstantRef: return "EnumConstantRef";
836 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000837
Daniel Dunbaracca7252009-11-30 20:42:49 +0000838 case CXCursor_InvalidFile: return "InvalidFile";
839 case CXCursor_NoDeclFound: return "NoDeclFound";
840 case CXCursor_NotImplemented: return "NotImplemented";
841 default: return "<not implemented>";
Steve Naroff89922f82009-08-31 00:59:03 +0000842 }
Steve Naroff600866c2009-08-27 19:51:58 +0000843}
Steve Naroff89922f82009-08-31 00:59:03 +0000844
Steve Naroff9efa7672009-09-04 15:44:05 +0000845static enum CXCursorKind TranslateKind(Decl *D) {
846 switch (D->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000847 case Decl::Function: return CXCursor_FunctionDecl;
848 case Decl::Typedef: return CXCursor_TypedefDecl;
849 case Decl::Enum: return CXCursor_EnumDecl;
850 case Decl::EnumConstant: return CXCursor_EnumConstantDecl;
851 case Decl::Record: return CXCursor_StructDecl; // FIXME: union/class
852 case Decl::Field: return CXCursor_FieldDecl;
853 case Decl::Var: return CXCursor_VarDecl;
854 case Decl::ParmVar: return CXCursor_ParmDecl;
855 case Decl::ObjCInterface: return CXCursor_ObjCInterfaceDecl;
856 case Decl::ObjCCategory: return CXCursor_ObjCCategoryDecl;
857 case Decl::ObjCProtocol: return CXCursor_ObjCProtocolDecl;
858 case Decl::ObjCMethod: {
859 ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D);
860 if (MD->isInstanceMethod())
861 return CXCursor_ObjCInstanceMethodDecl;
862 return CXCursor_ObjCClassMethodDecl;
863 }
864 default: break;
Steve Naroff9efa7672009-09-04 15:44:05 +0000865 }
Steve Naroff77128dd2009-09-15 20:25:34 +0000866 return CXCursor_NotImplemented;
Steve Naroff9efa7672009-09-04 15:44:05 +0000867}
Steve Naroff600866c2009-08-27 19:51:58 +0000868//
869// CXCursor Operations.
870//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000871
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000872CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000873 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000874 assert(CTUnit && "Passed null CXTranslationUnit");
875 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000876
Steve Naroff9efa7672009-09-04 15:44:05 +0000877 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000878 const FileEntry *File = FMgr.getFile(source_name,
879 source_name+strlen(source_name));
Steve Naroff77128dd2009-09-15 20:25:34 +0000880 if (!File) {
Steve Narofffb570422009-09-22 19:25:29 +0000881 CXCursor C = { CXCursor_InvalidFile, 0, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000882 return C;
883 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000884 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000885 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000886
Steve Narofff96b5242009-10-28 20:44:47 +0000887 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
888
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000889 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000890 &LastLoc);
891 if (ALoc.isValid())
892 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000893
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000894 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000895 if (ALoc.isNamedRef())
896 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000897 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000898 if (Dcl) {
899 if (Stm) {
900 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm)) {
901 CXCursor C = { TranslateDeclRefExpr(DRE), Dcl, Stm };
902 return C;
903 } else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm)) {
904 CXCursor C = { CXCursor_ObjCSelectorRef, Dcl, MExp };
905 return C;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000906 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000907 // Fall through...treat as a decl, not a ref.
908 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000909 if (ALoc.isNamedRef()) {
910 if (isa<ObjCInterfaceDecl>(Dcl)) {
911 CXCursor C = { CXCursor_ObjCClassRef, Dcl, ALoc.getParentDecl() };
912 return C;
913 }
914 if (isa<ObjCProtocolDecl>(Dcl)) {
915 CXCursor C = { CXCursor_ObjCProtocolRef, Dcl, ALoc.getParentDecl() };
916 return C;
917 }
918 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000919 CXCursor C = { TranslateKind(Dcl), Dcl, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000920 return C;
921 }
Steve Narofffb570422009-09-22 19:25:29 +0000922 CXCursor C = { CXCursor_NoDeclFound, 0, 0 };
Steve Naroff9efa7672009-09-04 15:44:05 +0000923 return C;
Steve Naroff600866c2009-08-27 19:51:58 +0000924}
925
Ted Kremenek73885552009-11-17 19:28:59 +0000926CXCursor clang_getNullCursor(void) {
927 CXCursor C;
928 C.kind = CXCursor_InvalidFile;
929 C.decl = NULL;
930 C.stmt = NULL;
931 return C;
932}
933
934unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
935 return X.kind == Y.kind && X.decl == Y.decl && X.stmt == Y.stmt;
936}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000937
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000938CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000939 assert(AnonDecl && "Passed null CXDecl");
940 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000941
Steve Narofffb570422009-09-22 19:25:29 +0000942 CXCursor C = { TranslateKind(ND), ND, 0 };
Steve Naroff77128dd2009-09-15 20:25:34 +0000943 return C;
944}
945
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000946unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000947 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
948}
949
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000950unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000951 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
952}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000953
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000954unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000955 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
956}
957
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000958unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000959 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
960}
961
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000962CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000963 return C.kind;
964}
965
Steve Naroff699a07d2009-09-25 21:32:34 +0000966static Decl *getDeclFromExpr(Stmt *E) {
967 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
968 return RefExpr->getDecl();
969 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
970 return ME->getMemberDecl();
971 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
972 return RE->getDecl();
973
974 if (CallExpr *CE = dyn_cast<CallExpr>(E))
975 return getDeclFromExpr(CE->getCallee());
976 if (CastExpr *CE = dyn_cast<CastExpr>(E))
977 return getDeclFromExpr(CE->getSubExpr());
978 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
979 return OME->getMethodDecl();
980
981 return 0;
982}
983
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000984CXDecl clang_getCursorDecl(CXCursor C) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000985 if (clang_isDeclaration(C.kind))
986 return C.decl;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000987
Steve Naroff699a07d2009-09-25 21:32:34 +0000988 if (clang_isReference(C.kind)) {
Steve Naroff85e2db72009-10-01 00:31:07 +0000989 if (C.stmt) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000990 if (C.kind == CXCursor_ObjCClassRef ||
Steve Narofff9adf8f2009-10-05 17:58:19 +0000991 C.kind == CXCursor_ObjCProtocolRef)
Steve Naroff85e2db72009-10-01 00:31:07 +0000992 return static_cast<Stmt *>(C.stmt);
993 else
994 return getDeclFromExpr(static_cast<Stmt *>(C.stmt));
995 } else
Steve Naroff699a07d2009-09-25 21:32:34 +0000996 return C.decl;
997 }
998 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000999}
1000
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001001unsigned clang_getCursorLine(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001002 assert(C.decl && "CXCursor has null decl");
1003 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001004 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001005
Steve Narofff334b4e2009-09-02 18:26:48 +00001006 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001007 return SourceMgr.getSpellingLineNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001008}
Steve Narofff334b4e2009-09-02 18:26:48 +00001009
Steve Naroffef0cef62009-11-09 17:45:52 +00001010const char *clang_getCString(CXString string) {
1011 return string.Spelling;
1012}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001013
Steve Naroffef0cef62009-11-09 17:45:52 +00001014void clang_disposeString(CXString string) {
Benjamin Kramer858e5de2009-11-09 18:24:53 +00001015 if (string.MustFreeString)
1016 free((void*)string.Spelling);
Steve Naroffef0cef62009-11-09 17:45:52 +00001017}
1018
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001019unsigned clang_getCursorColumn(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001020 assert(C.decl && "CXCursor has null decl");
1021 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001022 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001023
Steve Narofff334b4e2009-09-02 18:26:48 +00001024 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001025 return SourceMgr.getSpellingColumnNumber(SLoc);
Steve Naroff600866c2009-08-27 19:51:58 +00001026}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001027
1028const char *clang_getCursorSource(CXCursor C) {
Steve Naroff2d4d6292009-08-31 14:26:51 +00001029 assert(C.decl && "CXCursor has null decl");
1030 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
Steve Naroff2d4d6292009-08-31 14:26:51 +00001031 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001032
Steve Narofff334b4e2009-09-02 18:26:48 +00001033 SourceLocation SLoc = getLocationFromCursor(C, SourceMgr, ND);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001034
Ted Kremenek9298cfc2009-11-17 05:31:58 +00001035 if (SLoc.isFileID()) {
1036 const char *bufferName = SourceMgr.getBufferName(SLoc);
1037 return bufferName[0] == '<' ? NULL : bufferName;
1038 }
Douglas Gregor02465752009-10-16 21:24:31 +00001039
1040 // Retrieve the file in which the macro was instantiated, then provide that
1041 // buffer name.
1042 // FIXME: Do we want to give specific macro-instantiation information?
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001043 const llvm::MemoryBuffer *Buffer
Douglas Gregor02465752009-10-16 21:24:31 +00001044 = SourceMgr.getBuffer(SourceMgr.getDecomposedSpellingLoc(SLoc).first);
1045 if (!Buffer)
1046 return 0;
1047
1048 return Buffer->getBufferIdentifier();
Steve Naroff600866c2009-08-27 19:51:58 +00001049}
1050
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001051CXFile clang_getCursorSourceFile(CXCursor C) {
Steve Naroff88145032009-10-27 14:35:18 +00001052 assert(C.decl && "CXCursor has null decl");
1053 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1054 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001055
Ted Kremenek4b333d22010-01-12 00:36:38 +00001056 return (void *)
1057 getFileEntryFromSourceLocation(SourceMgr, getLocationFromCursor(C,SourceMgr,
1058 ND));
Steve Naroff88145032009-10-27 14:35:18 +00001059}
1060
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001061void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001062 const char **startBuf,
1063 const char **endBuf,
1064 unsigned *startLine,
1065 unsigned *startColumn,
1066 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001067 unsigned *endColumn) {
Steve Naroff4ade6d62009-09-23 17:52:52 +00001068 assert(C.decl && "CXCursor has null decl");
1069 NamedDecl *ND = static_cast<NamedDecl *>(C.decl);
1070 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1071 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001072
Steve Naroff4ade6d62009-09-23 17:52:52 +00001073 SourceManager &SM = FD->getASTContext().getSourceManager();
1074 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1075 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1076 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1077 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1078 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1079 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1080}
1081
Steve Naroff600866c2009-08-27 19:51:58 +00001082} // end extern "C"