blob: 6361174b222f4fedf79d2e7d8d8bb58209190a3c [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
Douglas Gregor98258af2010-01-18 22:46:11 +0000116/// \brief Translate a Clang source location into a CIndex source location.
117static CXSourceLocation translateSourceLocation(SourceManager &SourceMgr,
118 SourceLocation Loc) {
119 SourceLocation InstLoc = SourceMgr.getInstantiationLoc(Loc);
120 if (InstLoc.isInvalid()) {
121 CXSourceLocation Loc = { 0, 0, 0 };
122 return Loc;
123 }
124
125 CXSourceLocation Result;
126 Result.file
127 = (void*)SourceMgr.getFileEntryForID(SourceMgr.getFileID(InstLoc));
128 Result.line = SourceMgr.getInstantiationLineNumber(InstLoc);
129 Result.column = SourceMgr.getInstantiationColumnNumber(InstLoc);
130 return Result;
131}
132
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000133//===----------------------------------------------------------------------===//
134// Visitors.
135//===----------------------------------------------------------------------===//
136
Steve Naroff89922f82009-08-31 00:59:03 +0000137namespace {
Ted Kremenekab188932010-01-05 19:32:54 +0000138static enum CXCursorKind TranslateDeclRefExpr(DeclRefExpr *DRE) {
Steve Naroff4ade6d62009-09-23 17:52:52 +0000139 NamedDecl *D = DRE->getDecl();
140 if (isa<VarDecl>(D))
141 return CXCursor_VarRef;
142 else if (isa<FunctionDecl>(D))
143 return CXCursor_FunctionRef;
144 else if (isa<EnumConstantDecl>(D))
145 return CXCursor_EnumConstantRef;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000146 else
Steve Naroff4ade6d62009-09-23 17:52:52 +0000147 return CXCursor_NotImplemented;
148}
149
Steve Naroff89922f82009-08-31 00:59:03 +0000150// Translation Unit Visitor.
Ted Kremenekf1286182010-01-13 00:13:47 +0000151
Steve Naroff89922f82009-08-31 00:59:03 +0000152class TUVisitor : public DeclVisitor<TUVisitor> {
Ted Kremenekf1286182010-01-13 00:13:47 +0000153public:
154 typedef void (*Iterator)(void *, CXCursor, CXClientData);
155private:
156 void *Root; // CXDecl or CXTranslationUnit
157 Iterator Callback; // CXTranslationUnitIterator or CXDeclIterator.
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000158 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000159
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000160 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
161 // to the visitor. Declarations with a PCH level greater than this value will
162 // be suppressed.
163 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000164
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000165 void Call(const CXCursor &C) {
Ted Kremenek70ee5422010-01-16 01:44:12 +0000166 if (clang_isInvalid(C.kind))
167 return;
168
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000169 if (const Decl *D = getCursorDecl(C)) {
170 // Filter any declarations that have a PCH level greater than what
171 // we allow.
172 if (D->getPCHLevel() > MaxPCHLevel)
173 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000174
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000175 // Filter any implicit declarations (since the source info will be bogus).
176 if (D->isImplicit())
177 return;
178 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000179
Ted Kremenekf1286182010-01-13 00:13:47 +0000180 Callback(Root, C, CData);
Steve Naroff2b8ee6c2009-09-01 15:55:40 +0000181 }
Ted Kremenekf1286182010-01-13 00:13:47 +0000182
Steve Naroff89922f82009-08-31 00:59:03 +0000183public:
Ted Kremenekf1286182010-01-13 00:13:47 +0000184 TUVisitor(void *root, Iterator cback, CXClientData D, unsigned MaxPCHLevel) :
185 Root(root), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000186
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000187 void VisitDecl(Decl *D);
Ted Kremenekf1286182010-01-13 00:13:47 +0000188 void VisitDeclContext(DeclContext *DC);
Ted Kremenekf1286182010-01-13 00:13:47 +0000189 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
Ted Kremenekf1286182010-01-13 00:13:47 +0000190};
Ted Kremenekef7fdc62009-11-17 07:02:15 +0000191
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000192void TUVisitor::VisitDecl(Decl *D) {
193 Call(MakeCXCursor(D));
194}
195
Ted Kremenekf1286182010-01-13 00:13:47 +0000196void TUVisitor::VisitDeclContext(DeclContext *DC) {
197 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
198 I != E; ++I)
199 Visit(*I);
200}
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000201
Ted Kremenekf1286182010-01-13 00:13:47 +0000202void TUVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
203 VisitDeclContext(dyn_cast<DeclContext>(D));
204}
Steve Naroff89922f82009-08-31 00:59:03 +0000205
Steve Naroffc857ea42009-09-02 13:28:54 +0000206// Declaration visitor.
207class CDeclVisitor : public DeclVisitor<CDeclVisitor> {
208 CXDecl CDecl;
209 CXDeclIterator Callback;
210 CXClientData CData;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000211
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000212 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
213 // to the visitor. Declarations with a PCH level greater than this value will
214 // be suppressed.
215 unsigned MaxPCHLevel;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000216
Steve Naroffc857ea42009-09-02 13:28:54 +0000217 void Call(enum CXCursorKind CK, NamedDecl *ND) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000218 // Disable the callback when the context is equal to the visiting decl.
219 if (CDecl == ND && !clang_isReference(CK))
220 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000221
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000222 // Filter any declarations that have a PCH level greater than what we allow.
223 if (ND->getPCHLevel() > MaxPCHLevel)
224 return;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000225
Douglas Gregor283cae32010-01-15 21:56:13 +0000226 CXCursor C = { CK, { ND, 0, 0 } };
Steve Naroffc857ea42009-09-02 13:28:54 +0000227 Callback(CDecl, C, CData);
228 }
Douglas Gregor2e331b92010-01-16 14:00:32 +0000229
Steve Naroff89922f82009-08-31 00:59:03 +0000230public:
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000231 CDeclVisitor(CXDecl C, CXDeclIterator cback, CXClientData D,
232 unsigned MaxPCHLevel) :
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000233 CDecl(C), Callback(cback), CData(D), MaxPCHLevel(MaxPCHLevel) {}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000234
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000235 void VisitDeclContext(DeclContext *DC);
236 void VisitEnumConstantDecl(EnumConstantDecl *ND);
237 void VisitFieldDecl(FieldDecl *ND);
238 void VisitFunctionDecl(FunctionDecl *ND);
239 void VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
240 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
241 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
242 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
243 void VisitObjCIvarDecl(ObjCIvarDecl *ND);
244 void VisitObjCMethodDecl(ObjCMethodDecl *ND);
245 void VisitObjCPropertyDecl(ObjCPropertyDecl *ND);
246 void VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
247 void VisitParmVarDecl(ParmVarDecl *ND);
248 void VisitTagDecl(TagDecl *D);
249 void VisitVarDecl(VarDecl *ND);
Steve Naroff89922f82009-08-31 00:59:03 +0000250};
Ted Kremenekab188932010-01-05 19:32:54 +0000251} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000252
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000253void CDeclVisitor::VisitDeclContext(DeclContext *DC) {
254 for (DeclContext::decl_iterator
255 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
256 Visit(*I);
257}
258
259void CDeclVisitor::VisitEnumConstantDecl(EnumConstantDecl *ND) {
260 Call(CXCursor_EnumConstantDecl, ND);
261}
262
263void CDeclVisitor::VisitFieldDecl(FieldDecl *ND) {
264 Call(CXCursor_FieldDecl, ND);
265}
266
267void CDeclVisitor::VisitFunctionDecl(FunctionDecl *ND) {
268 if (ND->isThisDeclarationADefinition()) {
269 VisitDeclContext(dyn_cast<DeclContext>(ND));
270#if 0
271 // Not currently needed.
272 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
273 CRefVisitor RVisit(CDecl, Callback, CData);
274 RVisit.Visit(Body);
275#endif
276 }
277}
278
279void CDeclVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
280 // Issue callbacks for the containing class.
Douglas Gregor1adb0822010-01-16 17:14:40 +0000281 Callback(CDecl,
282 MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation()),
283 CData);
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000284 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
285 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
286 E = ND->protocol_end(); I != E; ++I, ++PL)
287 Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000288 VisitDeclContext(dyn_cast<DeclContext>(ND));
289}
290
291void CDeclVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
292 VisitDeclContext(dyn_cast<DeclContext>(D));
293}
294
295void CDeclVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
296 VisitDeclContext(dyn_cast<DeclContext>(D));
297}
298
299void CDeclVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
300 // Issue callbacks for super class.
301 if (D->getSuperClass())
Douglas Gregor2e331b92010-01-16 14:00:32 +0000302 Callback(CDecl,
303 MakeCursorObjCSuperClassRef(D->getSuperClass(),
304 D->getSuperClassLoc()),
305 CData);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000306
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000307 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
308 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
309 E = D->protocol_end(); I != E; ++I, ++PL)
310 Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000311 VisitDeclContext(dyn_cast<DeclContext>(D));
312}
313
314void CDeclVisitor::VisitObjCIvarDecl(ObjCIvarDecl *ND) {
315 Call(CXCursor_ObjCIvarDecl, ND);
316}
317
318void CDeclVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
319 if (ND->getBody()) {
320 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDefn
321 : CXCursor_ObjCClassMethodDefn, ND);
322 VisitDeclContext(dyn_cast<DeclContext>(ND));
323 } else
324 Call(ND->isInstanceMethod() ? CXCursor_ObjCInstanceMethodDecl
325 : CXCursor_ObjCClassMethodDecl, ND);
326}
327
328void CDeclVisitor::VisitObjCPropertyDecl(ObjCPropertyDecl *ND) {
329 Call(CXCursor_ObjCPropertyDecl, ND);
330}
331
332void CDeclVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000333 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000334 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000335 E = PID->protocol_end(); I != E; ++I, ++PL)
336 Callback(CDecl, MakeCursorObjCProtocolRef(*I, *PL), CData);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000337
338 VisitDeclContext(dyn_cast<DeclContext>(PID));
339}
340
341void CDeclVisitor::VisitParmVarDecl(ParmVarDecl *ND) {
342 Call(CXCursor_ParmDecl, ND);
343}
344
345void CDeclVisitor::VisitTagDecl(TagDecl *D) {
346 VisitDeclContext(dyn_cast<DeclContext>(D));
347}
348
349void CDeclVisitor::VisitVarDecl(VarDecl *ND) {
350 Call(CXCursor_VarDecl, ND);
351}
352
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000353static SourceLocation getLocationFromCursor(CXCursor C,
Daniel Dunbarc6190332009-11-08 04:13:53 +0000354 SourceManager &SourceMgr,
355 NamedDecl *ND) {
356 if (clang_isReference(C.kind)) {
Ted Kremenek8b456e82010-01-15 18:24:18 +0000357
Douglas Gregor283cae32010-01-15 21:56:13 +0000358 if (Decl *D = getCursorReferringDecl(C))
Ted Kremenek8b456e82010-01-15 18:24:18 +0000359 return D->getLocation();
360
Daniel Dunbarc6190332009-11-08 04:13:53 +0000361 switch (C.kind) {
Douglas Gregor1adb0822010-01-16 17:14:40 +0000362 case CXCursor_ObjCClassRef:
363 return getCursorObjCClassRef(C).second;
Douglas Gregor2e331b92010-01-16 14:00:32 +0000364 case CXCursor_ObjCSuperClassRef:
365 return getCursorObjCSuperClassRef(C).second;
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000366 case CXCursor_ObjCProtocolRef:
367 return getCursorObjCProtocolRef(C).second;
Daniel Dunbaracca7252009-11-30 20:42:49 +0000368 case CXCursor_ObjCSelectorRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000369 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
Douglas Gregor98258af2010-01-18 22:46:11 +0000370 assert(OME && "getLocationFromCursor(): Missing message expr");
Daniel Dunbaracca7252009-11-30 20:42:49 +0000371 return OME->getLeftLoc(); /* FIXME: should be a range */
372 }
373 case CXCursor_VarRef:
374 case CXCursor_FunctionRef:
375 case CXCursor_EnumConstantRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000376 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
Douglas Gregor98258af2010-01-18 22:46:11 +0000377 assert(DRE && "getLocationFromCursor(): Missing decl ref expr");
Daniel Dunbaracca7252009-11-30 20:42:49 +0000378 return DRE->getLocation();
379 }
380 default:
381 return SourceLocation();
Daniel Dunbarc6190332009-11-08 04:13:53 +0000382 }
383 } else { // We have a declaration or a definition.
384 SourceLocation SLoc;
385 switch (ND->getKind()) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000386 case Decl::ObjCInterface: {
387 SLoc = dyn_cast<ObjCInterfaceDecl>(ND)->getClassLoc();
388 break;
389 }
390 case Decl::ObjCProtocol: {
391 SLoc = ND->getLocation(); /* FIXME: need to get the name location. */
392 break;
393 }
394 default: {
395 SLoc = ND->getLocation();
396 break;
397 }
Daniel Dunbarc6190332009-11-08 04:13:53 +0000398 }
399 if (SLoc.isInvalid())
400 return SourceLocation();
401 return SourceMgr.getSpellingLoc(SLoc); // handles macro instantiations.
402 }
403}
404
Daniel Dunbar140fce22010-01-12 02:34:07 +0000405CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000406 CXString Str;
407 if (DupString) {
408 Str.Spelling = strdup(String);
409 Str.MustFreeString = 1;
410 } else {
411 Str.Spelling = String;
412 Str.MustFreeString = 0;
413 }
414 return Str;
415}
416
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000417extern "C" {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000418CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000419 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000420 CIndexer *CIdxr = new CIndexer(new Program());
421 if (excludeDeclarationsFromPCH)
422 CIdxr->setOnlyLocalDecls();
423 if (displayDiagnostics)
424 CIdxr->setDisplayDiagnostics();
425 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000426}
427
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000428void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000429 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000430 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000431}
432
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000433void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
434 assert(CIdx && "Passed null CXIndex");
435 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
436 CXXIdx->setUseExternalASTGeneration(value);
437}
438
Steve Naroff50398192009-08-28 15:28:48 +0000439// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000440CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
441 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000442 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000443 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000444
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000445 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
446 CXXIdx->getOnlyLocalDecls(),
447 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000448}
449
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000450CXTranslationUnit
451clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
452 const char *source_filename,
453 int num_command_line_args,
454 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000455 assert(CIdx && "Passed null CXIndex");
456 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
457
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000458 if (!CXXIdx->getUseExternalASTGeneration()) {
459 llvm::SmallVector<const char *, 16> Args;
460
461 // The 'source_filename' argument is optional. If the caller does not
462 // specify it then it is assumed that the source file is specified
463 // in the actual argument list.
464 if (source_filename)
465 Args.push_back(source_filename);
466 Args.insert(Args.end(), command_line_args,
467 command_line_args + num_command_line_args);
468
Daniel Dunbar94220972009-12-05 02:17:18 +0000469 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000470
Ted Kremenek29b72842010-01-07 22:49:05 +0000471#ifdef USE_CRASHTRACER
472 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000473#endif
474
Daniel Dunbar94220972009-12-05 02:17:18 +0000475 llvm::OwningPtr<ASTUnit> Unit(
476 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000477 CXXIdx->getDiags(),
478 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000479 CXXIdx->getOnlyLocalDecls(),
480 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000481
Daniel Dunbar94220972009-12-05 02:17:18 +0000482 // FIXME: Until we have broader testing, just drop the entire AST if we
483 // encountered an error.
484 if (NumErrors != CXXIdx->getDiags().getNumErrors())
485 return 0;
486
487 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000488 }
489
Ted Kremenek139ba862009-10-22 00:03:57 +0000490 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000491 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000492
Ted Kremenek139ba862009-10-22 00:03:57 +0000493 // First add the complete path to the 'clang' executable.
494 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000495 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000496
Ted Kremenek139ba862009-10-22 00:03:57 +0000497 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000498 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000499
Ted Kremenek139ba862009-10-22 00:03:57 +0000500 // The 'source_filename' argument is optional. If the caller does not
501 // specify it then it is assumed that the source file is specified
502 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000503 if (source_filename)
504 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000505
Steve Naroff37b5ac22009-10-15 20:50:09 +0000506 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000507 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000508 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000509 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000510
511 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
512 for (int i = 0; i < num_command_line_args; ++i)
513 if (const char *arg = command_line_args[i]) {
514 if (strcmp(arg, "-o") == 0) {
515 ++i; // Also skip the matching argument.
516 continue;
517 }
518 if (strcmp(arg, "-emit-ast") == 0 ||
519 strcmp(arg, "-c") == 0 ||
520 strcmp(arg, "-fsyntax-only") == 0) {
521 continue;
522 }
523
524 // Keep the argument.
525 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000526 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000527
Ted Kremenek139ba862009-10-22 00:03:57 +0000528 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000529 argv.push_back(NULL);
530
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000531 // Invoke 'clang'.
532 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
533 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000534 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000535 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000536 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
537 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
538 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000539
Ted Kremenek0854d702009-11-10 19:18:52 +0000540 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000541 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000542 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000543 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000544 I!=E; ++I) {
545 if (*I)
546 llvm::errs() << ' ' << *I << '\n';
547 }
548 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000549 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000550
Steve Naroff37b5ac22009-10-15 20:50:09 +0000551 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000552 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000553 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000554 if (ATU)
555 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000556 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000557}
558
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000559void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000560 assert(CTUnit && "Passed null CXTranslationUnit");
561 delete static_cast<ASTUnit *>(CTUnit);
562}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000563
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000564CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000565 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000566 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek4b333d22010-01-12 00:36:38 +0000567 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
568 true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000569}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000570
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000571void clang_loadTranslationUnit(CXTranslationUnit CTUnit,
Steve Naroffc857ea42009-09-02 13:28:54 +0000572 CXTranslationUnitIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000573 CXClientData CData) {
Steve Naroff50398192009-08-28 15:28:48 +0000574 assert(CTUnit && "Passed null CXTranslationUnit");
575 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
576 ASTContext &Ctx = CXXUnit->getASTContext();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000577
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000578 unsigned PCHLevel = Decl::MaxPCHLevel;
579
580 // Set the PCHLevel to filter out unwanted decls if requested.
581 if (CXXUnit->getOnlyLocalDecls()) {
582 PCHLevel = 0;
583
584 // If the main input was an AST, bump the level.
585 if (CXXUnit->isMainFileAST())
586 ++PCHLevel;
587 }
588
589 TUVisitor DVisit(CTUnit, callback, CData, PCHLevel);
Daniel Dunbarf772d1e2009-12-04 08:17:33 +0000590
591 // If using a non-AST based ASTUnit, iterate over the stored list of top-level
592 // decls.
593 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
594 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
595 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
596 ie = TLDs.end(); it != ie; ++it) {
597 DVisit.Visit(*it);
598 }
599 } else
600 DVisit.Visit(Ctx.getTranslationUnitDecl());
Steve Naroff600866c2009-08-27 19:51:58 +0000601}
602
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000603void clang_loadDeclaration(CXDecl Dcl,
604 CXDeclIterator callback,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000605 CXClientData CData) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000606 assert(Dcl && "Passed null CXDecl");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000607
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000608 CDeclVisitor DVisit(Dcl, callback, CData,
609 static_cast<Decl *>(Dcl)->getPCHLevel());
Steve Naroffc857ea42009-09-02 13:28:54 +0000610 DVisit.Visit(static_cast<Decl *>(Dcl));
Steve Naroff600866c2009-08-27 19:51:58 +0000611}
Ted Kremenekfb480492010-01-13 21:46:36 +0000612} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +0000613
Ted Kremenekfb480492010-01-13 21:46:36 +0000614//===----------------------------------------------------------------------===//
Steve Naroff600866c2009-08-27 19:51:58 +0000615// CXDecl Operations.
Ted Kremenekfb480492010-01-13 21:46:36 +0000616//===----------------------------------------------------------------------===//
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000617
Ted Kremenekfb480492010-01-13 21:46:36 +0000618static const FileEntry *getFileEntryFromSourceLocation(SourceManager &SMgr,
619 SourceLocation SLoc) {
620 FileID FID;
621 if (SLoc.isFileID())
622 FID = SMgr.getFileID(SLoc);
623 else
624 FID = SMgr.getDecomposedSpellingLoc(SLoc).first;
625 return SMgr.getFileEntryForID(FID);
626}
627
628extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000629CXString clang_getDeclSpelling(CXDecl AnonDecl) {
Steve Naroff89922f82009-08-31 00:59:03 +0000630 assert(AnonDecl && "Passed null CXDecl");
631 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Steve Naroffef0cef62009-11-09 17:45:52 +0000632
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000633 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenek4b333d22010-01-12 00:36:38 +0000634 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
635 true);
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000636
637 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
Steve Naroff0d69b8c2009-10-29 21:11:04 +0000638 // No, this isn't the same as the code below. getIdentifier() is non-virtual
639 // and returns different names. NamedDecl returns the class name and
640 // ObjCCategoryImplDecl returns the category name.
Ted Kremenek4b333d22010-01-12 00:36:38 +0000641 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000642
643 if (ND->getIdentifier())
Ted Kremenek4b333d22010-01-12 00:36:38 +0000644 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000645
Ted Kremenek4b333d22010-01-12 00:36:38 +0000646 return CIndexer::createCXString("");
Steve Naroff600866c2009-08-27 19:51:58 +0000647}
Steve Narofff334b4e2009-09-02 18:26:48 +0000648
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000649unsigned clang_getDeclLine(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000650 assert(AnonDecl && "Passed null CXDecl");
651 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
652 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
653 return SourceMgr.getSpellingLineNumber(ND->getLocation());
654}
655
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000656unsigned clang_getDeclColumn(CXDecl AnonDecl) {
Steve Naroff699a07d2009-09-25 21:32:34 +0000657 assert(AnonDecl && "Passed null CXDecl");
658 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
659 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff74165242009-09-25 22:15:54 +0000660 return SourceMgr.getSpellingColumnNumber(ND->getLocation());
Steve Naroff699a07d2009-09-25 21:32:34 +0000661}
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000662
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000663CXSourceRange clang_getDeclExtent(CXDecl AnonDecl) {
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000664 assert(AnonDecl && "Passed null CXDecl");
665 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
Ted Kremenekd8210652010-01-06 23:43:31 +0000666 SourceManager &SM = ND->getASTContext().getSourceManager();
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000667 SourceRange R = ND->getSourceRange();
668
Ted Kremenekd8210652010-01-06 23:43:31 +0000669 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
670 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
671
672 if (!Begin.isValid()) {
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000673 CXSourceRange extent = { { 0, 0, 0 }, { 0, 0, 0 } };
Ted Kremenekd8210652010-01-06 23:43:31 +0000674 return extent;
675 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000676
Ted Kremenekd8210652010-01-06 23:43:31 +0000677 // FIXME: This is largely copy-paste from
678 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
679 // what we want the two routines should be refactored.
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000680
Ted Kremenekd8210652010-01-06 23:43:31 +0000681 // If the End location and the start location are the same and are a macro
682 // location, then the range was something that came from a macro expansion
683 // or _Pragma. If this is an object-like macro, the best we can do is to
684 // get the range. If this is a function-like macro, we'd also like to
685 // get the arguments.
686 if (Begin == End && R.getEnd().isMacroID())
687 End = SM.getInstantiationRange(R.getEnd()).second;
688
689 assert(SM.getFileID(Begin) == SM.getFileID(End));
690 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
691 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000692
Ted Kremenekd8210652010-01-06 23:43:31 +0000693 // Compute the column number of the start. Keep the column based at 1.
694 unsigned StartColNo = SM.getInstantiationColumnNumber(Begin);
695
696 // Compute the column number of the end.
697 unsigned EndColNo = SM.getInstantiationColumnNumber(End);
698 if (EndColNo) {
699 // Offset the end column by 1 so that we point to the last character
700 // in the last token.
701 --EndColNo;
702
703 // Add in the length of the token, so that we cover multi-char tokens.
704 ASTContext &Ctx = ND->getTranslationUnitDecl()->getASTContext();
705 const LangOptions &LOpts = Ctx.getLangOptions();
706
707 EndColNo += Lexer::MeasureTokenLength(End, SM, LOpts);
708 }
709
710 // Package up the line/column data and return to the caller.
Douglas Gregor3c7313d2010-01-18 22:13:09 +0000711 const FileEntry *FEntry = SM.getFileEntryForID(SM.getFileID(Begin));
712 CXSourceRange extent = { { (void *)FEntry, StartLineNo, StartColNo },
713 { (void *)FEntry, EndLineNo, EndColNo } };
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000714 return extent;
715}
Steve Naroff699a07d2009-09-25 21:32:34 +0000716
Ted Kremenek6ab9db12010-01-08 17:11:32 +0000717const char *clang_getDeclSource(CXDecl AnonDecl) {
718 assert(AnonDecl && "Passed null CXDecl");
719 FileEntry *FEnt = static_cast<FileEntry *>(clang_getDeclSourceFile(AnonDecl));
720 assert(FEnt && "Cannot find FileEntry for Decl");
721 return clang_getFileName(FEnt);
722}
723
Steve Naroff88145032009-10-27 14:35:18 +0000724
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000725CXFile clang_getDeclSourceFile(CXDecl AnonDecl) {
Steve Naroff88145032009-10-27 14:35:18 +0000726 assert(AnonDecl && "Passed null CXDecl");
Steve Naroffee9405e2009-09-25 21:45:39 +0000727 NamedDecl *ND = static_cast<NamedDecl *>(AnonDecl);
728 SourceManager &SourceMgr = ND->getASTContext().getSourceManager();
Steve Naroff88145032009-10-27 14:35:18 +0000729 return (void *)getFileEntryFromSourceLocation(SourceMgr, ND->getLocation());
730}
Ted Kremenekfb480492010-01-13 21:46:36 +0000731} // end: extern "C"
Steve Naroff88145032009-10-27 14:35:18 +0000732
Ted Kremenekfb480492010-01-13 21:46:36 +0000733//===----------------------------------------------------------------------===//
734// CXFile Operations.
735//===----------------------------------------------------------------------===//
736
737extern "C" {
Steve Naroff88145032009-10-27 14:35:18 +0000738const char *clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000739 if (!SFile)
740 return 0;
741
Steve Naroff88145032009-10-27 14:35:18 +0000742 assert(SFile && "Passed null CXFile");
743 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
744 return FEnt->getName();
745}
746
747time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000748 if (!SFile)
749 return 0;
750
Steve Naroff88145032009-10-27 14:35:18 +0000751 assert(SFile && "Passed null CXFile");
752 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
753 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000754}
Ted Kremenekfb480492010-01-13 21:46:36 +0000755} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +0000756
Ted Kremenekfb480492010-01-13 21:46:36 +0000757//===----------------------------------------------------------------------===//
758// CXCursor Operations.
759//===----------------------------------------------------------------------===//
760
Ted Kremenekfb480492010-01-13 21:46:36 +0000761static Decl *getDeclFromExpr(Stmt *E) {
762 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
763 return RefExpr->getDecl();
764 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
765 return ME->getMemberDecl();
766 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
767 return RE->getDecl();
768
769 if (CallExpr *CE = dyn_cast<CallExpr>(E))
770 return getDeclFromExpr(CE->getCallee());
771 if (CastExpr *CE = dyn_cast<CastExpr>(E))
772 return getDeclFromExpr(CE->getSubExpr());
773 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
774 return OME->getMethodDecl();
775
776 return 0;
777}
778
779extern "C" {
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000780CXString clang_getCursorSpelling(CXCursor C) {
Daniel Dunbarc81d7182010-01-18 17:52:42 +0000781 assert(getCursorDecl(C) && "CXCursor has null decl");
Steve Narofff334b4e2009-09-02 18:26:48 +0000782 if (clang_isReference(C.kind)) {
783 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000784 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +0000785 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
786 return CIndexer::createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000787 }
788 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +0000789 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
790 return CIndexer::createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000791 }
792 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000793 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregor98258af2010-01-18 22:46:11 +0000794 assert(OID && "getLocationFromCursor(): Missing protocol decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000795 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000796 }
797 case CXCursor_ObjCSelectorRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000798 ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(getCursorStmt(C));
Douglas Gregor98258af2010-01-18 22:46:11 +0000799 assert(OME && "getLocationFromCursor(): Missing message expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000800 return CIndexer::createCXString(OME->getSelector().getAsString().c_str(),
801 true);
Daniel Dunbaracca7252009-11-30 20:42:49 +0000802 }
803 case CXCursor_VarRef:
804 case CXCursor_FunctionRef:
805 case CXCursor_EnumConstantRef: {
Douglas Gregor283cae32010-01-15 21:56:13 +0000806 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(getCursorStmt(C));
Douglas Gregor98258af2010-01-18 22:46:11 +0000807 assert(DRE && "getLocationFromCursor(): Missing decl ref expr");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000808 return CIndexer::createCXString(DRE->getDecl()->getIdentifier()
809 ->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000810 }
811 default:
Ted Kremenek4b333d22010-01-12 00:36:38 +0000812 return CIndexer::createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000813 }
814 }
Douglas Gregor283cae32010-01-15 21:56:13 +0000815 return clang_getDeclSpelling(getCursorDecl(C));
Steve Narofff334b4e2009-09-02 18:26:48 +0000816}
817
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000818const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000819 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000820 case CXCursor_FunctionDecl: return "FunctionDecl";
821 case CXCursor_TypedefDecl: return "TypedefDecl";
822 case CXCursor_EnumDecl: return "EnumDecl";
823 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
824 case CXCursor_StructDecl: return "StructDecl";
825 case CXCursor_UnionDecl: return "UnionDecl";
826 case CXCursor_ClassDecl: return "ClassDecl";
827 case CXCursor_FieldDecl: return "FieldDecl";
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000828 case CXCursor_FunctionDefn: return "FunctionDefn";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000829 case CXCursor_VarDecl: return "VarDecl";
830 case CXCursor_ParmDecl: return "ParmDecl";
831 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
832 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
833 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
834 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
835 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000836 case CXCursor_ObjCIvarRef: return "ObjCIvarRef";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000837 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
838 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000839 case CXCursor_ObjCInstanceMethodDefn: return "ObjCInstanceMethodDefn";
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000840 case CXCursor_ObjCCategoryDefn: return "ObjCCategoryDefn";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000841 case CXCursor_ObjCClassMethodDefn: return "ObjCClassMethodDefn";
842 case CXCursor_ObjCClassDefn: return "ObjCClassDefn";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000843 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
844 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
845 case CXCursor_ObjCClassRef: return "ObjCClassRef";
846 case CXCursor_ObjCSelectorRef: return "ObjCSelectorRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000847
Daniel Dunbaracca7252009-11-30 20:42:49 +0000848 case CXCursor_VarRef: return "VarRef";
849 case CXCursor_FunctionRef: return "FunctionRef";
850 case CXCursor_EnumConstantRef: return "EnumConstantRef";
851 case CXCursor_MemberRef: return "MemberRef";
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000852
Daniel Dunbaracca7252009-11-30 20:42:49 +0000853 case CXCursor_InvalidFile: return "InvalidFile";
854 case CXCursor_NoDeclFound: return "NoDeclFound";
855 case CXCursor_NotImplemented: return "NotImplemented";
Steve Naroff89922f82009-08-31 00:59:03 +0000856 }
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000857
858 llvm_unreachable("Unhandled CXCursorKind");
859 return NULL;
Steve Naroff600866c2009-08-27 19:51:58 +0000860}
Steve Naroff89922f82009-08-31 00:59:03 +0000861
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000862CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000863 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000864 assert(CTUnit && "Passed null CXTranslationUnit");
865 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000866
Steve Naroff9efa7672009-09-04 15:44:05 +0000867 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000868 const FileEntry *File = FMgr.getFile(source_name,
869 source_name+strlen(source_name));
Ted Kremenekf4629892010-01-14 01:51:23 +0000870 if (!File)
871 return clang_getNullCursor();
872
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000873 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000874 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000875
Steve Narofff96b5242009-10-28 20:44:47 +0000876 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000877 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000878 &LastLoc);
Ted Kremenekf4629892010-01-14 01:51:23 +0000879
880 // FIXME: This doesn't look thread-safe.
Steve Narofff96b5242009-10-28 20:44:47 +0000881 if (ALoc.isValid())
882 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000883
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000884 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000885 if (ALoc.isNamedRef())
886 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000887 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000888 if (Dcl) {
889 if (Stm) {
Ted Kremenekfb480492010-01-13 21:46:36 +0000890 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Stm))
891 return MakeCXCursor(TranslateDeclRefExpr(DRE), Dcl, Stm);
892 else if (ObjCMessageExpr *MExp = dyn_cast<ObjCMessageExpr>(Stm))
893 return MakeCXCursor(CXCursor_ObjCSelectorRef, Dcl, MExp);
Steve Naroff4ade6d62009-09-23 17:52:52 +0000894 // Fall through...treat as a decl, not a ref.
895 }
Steve Naroff85e2db72009-10-01 00:31:07 +0000896 if (ALoc.isNamedRef()) {
Douglas Gregor1adb0822010-01-16 17:14:40 +0000897 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(Dcl))
898 return MakeCursorObjCClassRef(Class, ALoc.AsNamedRef().Loc);
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000899 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(Dcl))
900 return MakeCursorObjCProtocolRef(Proto, ALoc.AsNamedRef().Loc);
Steve Naroff85e2db72009-10-01 00:31:07 +0000901 }
Ted Kremenek70ee5422010-01-16 01:44:12 +0000902 return MakeCXCursor(Dcl);
Steve Naroff77128dd2009-09-15 20:25:34 +0000903 }
Ted Kremenekfb480492010-01-13 21:46:36 +0000904 return MakeCXCursor(CXCursor_NoDeclFound, 0);
Steve Naroff600866c2009-08-27 19:51:58 +0000905}
906
Ted Kremenek73885552009-11-17 19:28:59 +0000907CXCursor clang_getNullCursor(void) {
Ted Kremenekfb480492010-01-13 21:46:36 +0000908 return MakeCXCursor(CXCursor_InvalidFile, 0);
Ted Kremenek73885552009-11-17 19:28:59 +0000909}
910
911unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000912 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +0000913}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000914
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000915CXCursor clang_getCursorFromDecl(CXDecl AnonDecl) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000916 assert(AnonDecl && "Passed null CXDecl");
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000917 return MakeCXCursor(static_cast<NamedDecl *>(AnonDecl));
Steve Naroff77128dd2009-09-15 20:25:34 +0000918}
919
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000920unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000921 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
922}
923
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000924unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000925 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
926}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000927
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000928unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000929 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
930}
931
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000932unsigned clang_isDefinition(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000933 return K >= CXCursor_FirstDefn && K <= CXCursor_LastDefn;
934}
935
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000936CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000937 return C.kind;
938}
939
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000940CXDecl clang_getCursorDecl(CXCursor C) {
Ted Kremenek70ee5422010-01-16 01:44:12 +0000941 if (clang_isDeclaration(C.kind) || clang_isDefinition(C.kind))
Douglas Gregor283cae32010-01-15 21:56:13 +0000942 return getCursorDecl(C);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000943
Steve Naroff699a07d2009-09-25 21:32:34 +0000944 if (clang_isReference(C.kind)) {
Douglas Gregor1adb0822010-01-16 17:14:40 +0000945 if (getCursorStmt(C))
946 return getDeclFromExpr(getCursorStmt(C));
947
948 return getCursorDecl(C);
Steve Naroff699a07d2009-09-25 21:32:34 +0000949 }
950 return 0;
Steve Naroff9efa7672009-09-04 15:44:05 +0000951}
952
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000953unsigned clang_getCursorLine(CXCursor C) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000954 return clang_getCursorLocation(C).line;
Steve Naroff600866c2009-08-27 19:51:58 +0000955}
Ted Kremenekfb480492010-01-13 21:46:36 +0000956
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000957unsigned clang_getCursorColumn(CXCursor C) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000958 return clang_getCursorLocation(C).column;
Steve Naroff600866c2009-08-27 19:51:58 +0000959}
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000960
961const char *clang_getCursorSource(CXCursor C) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000962 return clang_getFileName(clang_getCursorLocation(C).file);
Steve Naroff600866c2009-08-27 19:51:58 +0000963}
964
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000965CXFile clang_getCursorSourceFile(CXCursor C) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000966 return clang_getCursorLocation(C).file;
Steve Naroff88145032009-10-27 14:35:18 +0000967}
968
Douglas Gregor98258af2010-01-18 22:46:11 +0000969CXSourceLocation clang_getCursorLocation(CXCursor C) {
970 if (clang_isReference(C.kind)) {
971 // FIXME: Return the location of the reference, not of the underlying
972 // declaration (which may not even exist!).
973 }
974
975 if (!getCursorDecl(C)) {
976 CXSourceLocation empty = { 0, 0, 0 };
977 return empty;
978 }
979
980 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
981 SourceManager &SM = ND->getASTContext().getSourceManager();
982
983 return translateSourceLocation(SM, getLocationFromCursor(C, SM, ND));
984}
985
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000986void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +0000987 const char **startBuf,
988 const char **endBuf,
989 unsigned *startLine,
990 unsigned *startColumn,
991 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000992 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000993 assert(getCursorDecl(C) && "CXCursor has null decl");
994 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +0000995 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
996 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekfb480492010-01-13 21:46:36 +0000997
Steve Naroff4ade6d62009-09-23 17:52:52 +0000998 SourceManager &SM = FD->getASTContext().getSourceManager();
999 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1000 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1001 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1002 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1003 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1004 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1005}
Ted Kremenekfb480492010-01-13 21:46:36 +00001006
1007} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001008
Ted Kremenekfb480492010-01-13 21:46:36 +00001009//===----------------------------------------------------------------------===//
1010// CXString Operations.
1011//===----------------------------------------------------------------------===//
1012
1013extern "C" {
1014const char *clang_getCString(CXString string) {
1015 return string.Spelling;
1016}
1017
1018void clang_disposeString(CXString string) {
1019 if (string.MustFreeString && string.Spelling)
1020 free((void*)string.Spelling);
1021}
1022} // end: extern "C"