blob: 907312ab4f3491b438e6d640fedc6a6ab69ba896 [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"
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000020#include "clang/AST/TypeLocVisitor.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000021#include "clang/Lex/Lexer.h"
Douglas Gregor02465752009-10-16 21:24:31 +000022#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000023#include "llvm/System/Program.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000024
Ted Kremenekdb3d0da2010-01-05 20:55:39 +000025// Needed to define L_TMPNAM on some systems.
26#include <cstdio>
27
Steve Naroff50398192009-08-28 15:28:48 +000028using namespace clang;
Ted Kremenek16c440a2010-01-15 20:35:54 +000029using namespace clang::cxcursor;
Steve Naroff50398192009-08-28 15:28:48 +000030using namespace idx;
31
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000032//===----------------------------------------------------------------------===//
33// Crash Reporting.
34//===----------------------------------------------------------------------===//
35
36#ifdef __APPLE__
Ted Kremenek29b72842010-01-07 22:49:05 +000037#ifndef NDEBUG
38#define USE_CRASHTRACER
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000039#include "clang/Analysis/Support/SaveAndRestore.h"
40// Integrate with crash reporter.
41extern "C" const char *__crashreporter_info__;
Ted Kremenek29b72842010-01-07 22:49:05 +000042#define NUM_CRASH_STRINGS 16
43static unsigned crashtracer_counter = 0;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000044static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek29b72842010-01-07 22:49:05 +000045static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
46static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
47
48static unsigned SetCrashTracerInfo(const char *str,
49 llvm::SmallString<1024> &AggStr) {
50
Ted Kremenek254ba7c2010-01-07 23:13:53 +000051 unsigned slot = 0;
Ted Kremenek29b72842010-01-07 22:49:05 +000052 while (crashtracer_strings[slot]) {
53 if (++slot == NUM_CRASH_STRINGS)
54 slot = 0;
55 }
56 crashtracer_strings[slot] = str;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000057 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek29b72842010-01-07 22:49:05 +000058
59 // We need to create an aggregate string because multiple threads
60 // may be in this method at one time. The crash reporter string
61 // will attempt to overapproximate the set of in-flight invocations
62 // of this function. Race conditions can still cause this goal
63 // to not be achieved.
64 {
65 llvm::raw_svector_ostream Out(AggStr);
66 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
67 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
68 }
69 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
70 return slot;
71}
72
73static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek254ba7c2010-01-07 23:13:53 +000074 unsigned max_slot = 0;
75 unsigned max_value = 0;
76
77 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
78
79 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
80 if (agg_crashtracer_strings[i] &&
81 crashtracer_counter_id[i] > max_value) {
82 max_slot = i;
83 max_value = crashtracer_counter_id[i];
Ted Kremenek29b72842010-01-07 22:49:05 +000084 }
Ted Kremenek254ba7c2010-01-07 23:13:53 +000085
86 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek29b72842010-01-07 22:49:05 +000087}
88
89namespace {
90class ArgsCrashTracerInfo {
91 llvm::SmallString<1024> CrashString;
92 llvm::SmallString<1024> AggregateString;
93 unsigned crashtracerSlot;
94public:
95 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
96 : crashtracerSlot(0)
97 {
98 {
99 llvm::raw_svector_ostream Out(CrashString);
100 Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
101 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
102 E=Args.end(); I!=E; ++I)
103 Out << ' ' << *I;
104 }
105 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
106 AggregateString);
107 }
108
109 ~ArgsCrashTracerInfo() {
110 ResetCrashTracerInfo(crashtracerSlot);
111 }
112};
113}
114#endif
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000115#endif
116
Douglas Gregor1db19de2010-01-19 21:36:55 +0000117typedef llvm::PointerIntPair<ASTContext *, 1, bool> CXSourceLocationPtr;
118
Douglas Gregor98258af2010-01-18 22:46:11 +0000119/// \brief Translate a Clang source location into a CIndex source location.
Douglas Gregor1db19de2010-01-19 21:36:55 +0000120static CXSourceLocation translateSourceLocation(ASTContext &Context,
121 SourceLocation Loc,
122 bool AtEnd = false) {
123 CXSourceLocationPtr Ptr(&Context, AtEnd);
124 CXSourceLocation Result = { Ptr.getOpaqueValue(), Loc.getRawEncoding() };
125 return Result;
Douglas Gregor98258af2010-01-18 22:46:11 +0000126}
127
Douglas Gregora7bde202010-01-19 00:34:46 +0000128/// \brief Translate a Clang source range into a CIndex source range.
Douglas Gregor1db19de2010-01-19 21:36:55 +0000129static CXSourceRange translateSourceRange(ASTContext &Context, SourceRange R) {
130 CXSourceRange Result = { &Context,
131 R.getBegin().getRawEncoding(),
132 R.getEnd().getRawEncoding() };
133 return Result;
Douglas Gregora7bde202010-01-19 00:34:46 +0000134}
135
Douglas Gregor1db19de2010-01-19 21:36:55 +0000136
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000137//===----------------------------------------------------------------------===//
138// Visitors.
139//===----------------------------------------------------------------------===//
140
Steve Naroff89922f82009-08-31 00:59:03 +0000141namespace {
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000142
Douglas Gregorb1373d02010-01-20 20:59:29 +0000143// Cursor visitor.
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000144class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
145 public TypeLocVisitor<CursorVisitor, bool>
146{
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000147 ASTUnit *TU;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000148 CXCursor Parent;
149 CXCursorVisitor Visitor;
150 CXClientData ClientData;
151
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000152 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
153 // to the visitor. Declarations with a PCH level greater than this value will
154 // be suppressed.
155 unsigned MaxPCHLevel;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000156
157 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000158 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000159
Steve Naroff89922f82009-08-31 00:59:03 +0000160public:
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000161 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
Douglas Gregorb1373d02010-01-20 20:59:29 +0000162 unsigned MaxPCHLevel)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000163 : TU(TU), Visitor(Visitor), ClientData(ClientData), MaxPCHLevel(MaxPCHLevel)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000164 {
165 Parent.kind = CXCursor_NoDeclFound;
166 Parent.data[0] = 0;
167 Parent.data[1] = 0;
168 Parent.data[2] = 0;
169 }
170
171 bool Visit(CXCursor Cursor);
172 bool VisitChildren(CXCursor Parent);
173
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000174 // Declaration visitors
Douglas Gregorb1373d02010-01-20 20:59:29 +0000175 bool VisitDeclContext(DeclContext *DC);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000176 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000177 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000178 bool VisitFunctionDecl(FunctionDecl *ND);
179 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
180 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
181 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
182 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
183 bool VisitTagDecl(TagDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000184
185 // Type visitors
186 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Steve Naroff89922f82009-08-31 00:59:03 +0000187};
Douglas Gregorb1373d02010-01-20 20:59:29 +0000188
Ted Kremenekab188932010-01-05 19:32:54 +0000189} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000190
Douglas Gregorb1373d02010-01-20 20:59:29 +0000191/// \brief Visit the given cursor and, if requested by the visitor,
192/// its children.
193///
194/// \returns true if the visitation should be aborted, false if it
195/// should continue.
196bool CursorVisitor::Visit(CXCursor Cursor) {
197 if (clang_isInvalid(Cursor.kind))
198 return false;
199
200 if (clang_isDeclaration(Cursor.kind)) {
201 Decl *D = getCursorDecl(Cursor);
202 assert(D && "Invalid declaration cursor");
203 if (D->getPCHLevel() > MaxPCHLevel)
204 return false;
205
206 if (D->isImplicit())
207 return false;
208 }
209
210 switch (Visitor(Cursor, Parent, ClientData)) {
211 case CXChildVisit_Break:
212 return true;
213
214 case CXChildVisit_Continue:
215 return false;
216
217 case CXChildVisit_Recurse:
218 return VisitChildren(Cursor);
219 }
220
221 llvm_unreachable("Silly GCC, we can't get here");
222}
223
224/// \brief Visit the children of the given cursor.
225///
226/// \returns true if the visitation should be aborted, false if it
227/// should continue.
228bool CursorVisitor::VisitChildren(CXCursor Cursor) {
229 // Set the Parent field to Cursor, then back to its old value once we're
230 // done.
231 class SetParentRAII {
232 CXCursor &Parent;
233 CXCursor OldParent;
234
235 public:
236 SetParentRAII(CXCursor &Parent, CXCursor NewParent)
237 : Parent(Parent), OldParent(Parent)
238 {
239 Parent = NewParent;
240 }
241
242 ~SetParentRAII() {
243 Parent = OldParent;
244 }
245 } SetParent(Parent, Cursor);
246
247 if (clang_isDeclaration(Cursor.kind)) {
248 Decl *D = getCursorDecl(Cursor);
249 assert(D && "Invalid declaration cursor");
250 return Visit(D);
251 }
252
253 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000254 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor7b691f332010-01-20 21:13:59 +0000255 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
256 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
257 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
258 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000259 if (Visit(MakeCXCursor(*it, CXXUnit)))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000260 return true;
261 }
262 } else {
263 return VisitDeclContext(
Douglas Gregorb1373d02010-01-20 20:59:29 +0000264 CXXUnit->getASTContext().getTranslationUnitDecl());
Douglas Gregor7b691f332010-01-20 21:13:59 +0000265 }
266
267 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000268 }
269
270 // Nothing to visit at the moment.
271 // FIXME: Traverse statements, declarations, etc. here.
272 return false;
273}
274
275bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Douglas Gregor7b691f332010-01-20 21:13:59 +0000276 llvm_unreachable("Translation units are visited directly by Visit()");
277 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000278}
279
280bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000281 for (DeclContext::decl_iterator
Douglas Gregorb1373d02010-01-20 20:59:29 +0000282 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000283 if (Visit(MakeCXCursor(*I, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000284 return true;
285 }
286
287 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000288}
289
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000290bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
291 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
292 if (Visit(TSInfo->getTypeLoc()))
293 return true;
294
295 return false;
296}
297
Douglas Gregorb1373d02010-01-20 20:59:29 +0000298bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
299 // FIXME: This is wrong. We always want to visit the parameters and
300 // the body, if available.
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000301 if (ND->isThisDeclarationADefinition()) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000302 return VisitDeclContext(ND);
303
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000304#if 0
305 // Not currently needed.
306 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
307 CRefVisitor RVisit(CDecl, Callback, CData);
308 RVisit.Visit(Body);
309#endif
310 }
Douglas Gregorb1373d02010-01-20 20:59:29 +0000311
312 return false;
313}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000314
Douglas Gregorb1373d02010-01-20 20:59:29 +0000315bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000316 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
317 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000318 return true;
319
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000320 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
321 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
322 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000323 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000324 return true;
325
326 return VisitDeclContext(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000327}
328
Douglas Gregorb1373d02010-01-20 20:59:29 +0000329bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000330 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000331 if (D->getSuperClass() &&
332 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000333 D->getSuperClassLoc(),
334 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000335 return true;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000336
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000337 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
338 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
339 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000340 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000341 return true;
342
343 return VisitDeclContext(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000344}
345
Douglas Gregorb1373d02010-01-20 20:59:29 +0000346bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
347 // FIXME: Wrong in the same way that VisitFunctionDecl is wrong.
Douglas Gregorb6998662010-01-19 19:34:47 +0000348 if (ND->getBody())
Douglas Gregorb1373d02010-01-20 20:59:29 +0000349 return VisitDeclContext(ND);
350
351 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000352}
353
Douglas Gregorb1373d02010-01-20 20:59:29 +0000354bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000355 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000356 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000357 E = PID->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000358 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000359 return true;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000360
Douglas Gregorb1373d02010-01-20 20:59:29 +0000361 return VisitDeclContext(PID);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000362}
363
Douglas Gregorb1373d02010-01-20 20:59:29 +0000364bool CursorVisitor::VisitTagDecl(TagDecl *D) {
365 return VisitDeclContext(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000366}
367
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000368bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
369 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
370}
371
Daniel Dunbar140fce22010-01-12 02:34:07 +0000372CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000373 CXString Str;
374 if (DupString) {
375 Str.Spelling = strdup(String);
376 Str.MustFreeString = 1;
377 } else {
378 Str.Spelling = String;
379 Str.MustFreeString = 0;
380 }
381 return Str;
382}
383
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000384extern "C" {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000385CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000386 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000387 CIndexer *CIdxr = new CIndexer(new Program());
388 if (excludeDeclarationsFromPCH)
389 CIdxr->setOnlyLocalDecls();
390 if (displayDiagnostics)
391 CIdxr->setDisplayDiagnostics();
392 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000393}
394
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000395void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000396 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000397 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000398}
399
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000400void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
401 assert(CIdx && "Passed null CXIndex");
402 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
403 CXXIdx->setUseExternalASTGeneration(value);
404}
405
Steve Naroff50398192009-08-28 15:28:48 +0000406// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000407CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
408 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000409 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000410 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000411
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000412 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
413 CXXIdx->getOnlyLocalDecls(),
414 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000415}
416
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000417CXTranslationUnit
418clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
419 const char *source_filename,
420 int num_command_line_args,
421 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000422 assert(CIdx && "Passed null CXIndex");
423 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
424
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000425 if (!CXXIdx->getUseExternalASTGeneration()) {
426 llvm::SmallVector<const char *, 16> Args;
427
428 // The 'source_filename' argument is optional. If the caller does not
429 // specify it then it is assumed that the source file is specified
430 // in the actual argument list.
431 if (source_filename)
432 Args.push_back(source_filename);
433 Args.insert(Args.end(), command_line_args,
434 command_line_args + num_command_line_args);
435
Daniel Dunbar94220972009-12-05 02:17:18 +0000436 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000437
Ted Kremenek29b72842010-01-07 22:49:05 +0000438#ifdef USE_CRASHTRACER
439 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000440#endif
441
Daniel Dunbar94220972009-12-05 02:17:18 +0000442 llvm::OwningPtr<ASTUnit> Unit(
443 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000444 CXXIdx->getDiags(),
445 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000446 CXXIdx->getOnlyLocalDecls(),
447 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000448
Daniel Dunbar94220972009-12-05 02:17:18 +0000449 // FIXME: Until we have broader testing, just drop the entire AST if we
450 // encountered an error.
451 if (NumErrors != CXXIdx->getDiags().getNumErrors())
452 return 0;
453
454 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000455 }
456
Ted Kremenek139ba862009-10-22 00:03:57 +0000457 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000458 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000459
Ted Kremenek139ba862009-10-22 00:03:57 +0000460 // First add the complete path to the 'clang' executable.
461 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000462 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000463
Ted Kremenek139ba862009-10-22 00:03:57 +0000464 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000465 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000466
Ted Kremenek139ba862009-10-22 00:03:57 +0000467 // The 'source_filename' argument is optional. If the caller does not
468 // specify it then it is assumed that the source file is specified
469 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000470 if (source_filename)
471 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000472
Steve Naroff37b5ac22009-10-15 20:50:09 +0000473 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000474 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000475 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000476 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000477
478 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
479 for (int i = 0; i < num_command_line_args; ++i)
480 if (const char *arg = command_line_args[i]) {
481 if (strcmp(arg, "-o") == 0) {
482 ++i; // Also skip the matching argument.
483 continue;
484 }
485 if (strcmp(arg, "-emit-ast") == 0 ||
486 strcmp(arg, "-c") == 0 ||
487 strcmp(arg, "-fsyntax-only") == 0) {
488 continue;
489 }
490
491 // Keep the argument.
492 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000493 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000494
Ted Kremenek139ba862009-10-22 00:03:57 +0000495 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000496 argv.push_back(NULL);
497
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000498 // Invoke 'clang'.
499 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
500 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000501 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000502 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000503 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
504 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
505 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000506
Ted Kremenek0854d702009-11-10 19:18:52 +0000507 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000508 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000509 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000510 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000511 I!=E; ++I) {
512 if (*I)
513 llvm::errs() << ' ' << *I << '\n';
514 }
515 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000516 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000517
Steve Naroff37b5ac22009-10-15 20:50:09 +0000518 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000519 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000520 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000521 if (ATU)
522 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000523 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000524}
525
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000526void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000527 assert(CTUnit && "Passed null CXTranslationUnit");
528 delete static_cast<ASTUnit *>(CTUnit);
529}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000530
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000531CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000532 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000533 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek4b333d22010-01-12 00:36:38 +0000534 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
535 true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000536}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000537
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000538CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000539 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000540 return Result;
541}
542
Ted Kremenekfb480492010-01-13 21:46:36 +0000543} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +0000544
Ted Kremenekfb480492010-01-13 21:46:36 +0000545//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +0000546// CXSourceLocation and CXSourceRange Operations.
547//===----------------------------------------------------------------------===//
548
549void clang_getInstantiationLocation(CXSourceLocation location,
550 CXFile *file,
551 unsigned *line,
552 unsigned *column) {
553 CXSourceLocationPtr Ptr
554 = CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data);
555 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
556
557 if (!Ptr.getPointer() || Loc.isInvalid()) {
558 if (file)
559 *file = 0;
560 if (line)
561 *line = 0;
562 if (column)
563 *column = 0;
564 return;
565 }
566
567 // FIXME: This is largely copy-paste from
568 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
569 // what we want the two routines should be refactored.
570 ASTContext &Context = *Ptr.getPointer();
571 SourceManager &SM = Context.getSourceManager();
572 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
573
574 if (Ptr.getInt()) {
575 // We want the last character in this location, so we will adjust
576 // the instantiation location accordingly.
577
578 // If the location is from a macro instantiation, get the end of
579 // the instantiation range.
580 if (Loc.isMacroID())
581 InstLoc = SM.getInstantiationRange(Loc).second;
582
583 // Measure the length token we're pointing at, so we can adjust
584 // the physical location in the file to point at the last
585 // character.
586 // FIXME: This won't cope with trigraphs or escaped newlines
587 // well. For that, we actually need a preprocessor, which isn't
588 // currently available here. Eventually, we'll switch the pointer
589 // data of CXSourceLocation/CXSourceRange to a translation unit
590 // (CXXUnit), so that the preprocessor will be available here. At
591 // that point, we can use Preprocessor::getLocForEndOfToken().
592 unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM,
593 Context.getLangOptions());
594 if (Length > 0)
595 InstLoc = InstLoc.getFileLocWithOffset(Length - 1);
596 }
597
598 if (file)
599 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
600 if (line)
601 *line = SM.getInstantiationLineNumber(InstLoc);
602 if (column)
603 *column = SM.getInstantiationColumnNumber(InstLoc);
604}
605
606CXSourceLocation clang_getRangeStart(CXSourceRange range) {
607 CXSourceLocation Result = { range.ptr_data, range.begin_int_data };
608 return Result;
609}
610
611CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
612 llvm::PointerIntPair<ASTContext *, 1, bool> Ptr;
613 Ptr.setPointer(static_cast<ASTContext *>(range.ptr_data));
614 Ptr.setInt(true);
615 CXSourceLocation Result = { Ptr.getOpaqueValue(), range.end_int_data };
616 return Result;
617}
618
619//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +0000620// CXFile Operations.
621//===----------------------------------------------------------------------===//
622
623extern "C" {
Steve Naroff88145032009-10-27 14:35:18 +0000624const char *clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000625 if (!SFile)
626 return 0;
627
Steve Naroff88145032009-10-27 14:35:18 +0000628 assert(SFile && "Passed null CXFile");
629 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
630 return FEnt->getName();
631}
632
633time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000634 if (!SFile)
635 return 0;
636
Steve Naroff88145032009-10-27 14:35:18 +0000637 assert(SFile && "Passed null CXFile");
638 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
639 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000640}
Ted Kremenekfb480492010-01-13 21:46:36 +0000641} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +0000642
Ted Kremenekfb480492010-01-13 21:46:36 +0000643//===----------------------------------------------------------------------===//
644// CXCursor Operations.
645//===----------------------------------------------------------------------===//
646
Ted Kremenekfb480492010-01-13 21:46:36 +0000647static Decl *getDeclFromExpr(Stmt *E) {
648 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
649 return RefExpr->getDecl();
650 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
651 return ME->getMemberDecl();
652 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
653 return RE->getDecl();
654
655 if (CallExpr *CE = dyn_cast<CallExpr>(E))
656 return getDeclFromExpr(CE->getCallee());
657 if (CastExpr *CE = dyn_cast<CastExpr>(E))
658 return getDeclFromExpr(CE->getSubExpr());
659 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
660 return OME->getMethodDecl();
661
662 return 0;
663}
664
665extern "C" {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000666
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000667unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +0000668 CXCursorVisitor visitor,
669 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000670 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000671
672 unsigned PCHLevel = Decl::MaxPCHLevel;
673
674 // Set the PCHLevel to filter out unwanted decls if requested.
675 if (CXXUnit->getOnlyLocalDecls()) {
676 PCHLevel = 0;
677
678 // If the main input was an AST, bump the level.
679 if (CXXUnit->isMainFileAST())
680 ++PCHLevel;
681 }
682
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000683 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000684 return CursorVis.VisitChildren(parent);
685}
686
Douglas Gregor78205d42010-01-20 21:45:58 +0000687static CXString getDeclSpelling(Decl *D) {
688 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
689 if (!ND)
690 return CIndexer::createCXString("");
691
692 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
693 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
694 true);
695
696 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
697 // No, this isn't the same as the code below. getIdentifier() is non-virtual
698 // and returns different names. NamedDecl returns the class name and
699 // ObjCCategoryImplDecl returns the category name.
700 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
701
702 if (ND->getIdentifier())
703 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
704
705 return CIndexer::createCXString("");
706}
707
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000708CXString clang_getCursorSpelling(CXCursor C) {
Daniel Dunbarc81d7182010-01-18 17:52:42 +0000709 assert(getCursorDecl(C) && "CXCursor has null decl");
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000710 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000711 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000712
Steve Narofff334b4e2009-09-02 18:26:48 +0000713 if (clang_isReference(C.kind)) {
714 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000715 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +0000716 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
717 return CIndexer::createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000718 }
719 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +0000720 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
721 return CIndexer::createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000722 }
723 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000724 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +0000725 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000726 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000727 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000728 case CXCursor_TypeRef: {
729 TypeDecl *Type = getCursorTypeRef(C).first;
730 assert(Type && "Missing type decl");
731
732 return CIndexer::createCXString(
733 getCursorContext(C).getTypeDeclType(Type).getAsString().c_str(),
734 true);
735 }
736
Daniel Dunbaracca7252009-11-30 20:42:49 +0000737 default:
Ted Kremenek4b333d22010-01-12 00:36:38 +0000738 return CIndexer::createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000739 }
740 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000741
742 if (clang_isExpression(C.kind)) {
743 Decl *D = getDeclFromExpr(getCursorExpr(C));
744 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +0000745 return getDeclSpelling(D);
Douglas Gregor97b98722010-01-19 23:20:36 +0000746 return CIndexer::createCXString("");
747 }
748
Douglas Gregor78205d42010-01-20 21:45:58 +0000749 return getDeclSpelling(getCursorDecl(C));
Steve Narofff334b4e2009-09-02 18:26:48 +0000750}
751
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000752const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000753 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000754 case CXCursor_FunctionDecl: return "FunctionDecl";
755 case CXCursor_TypedefDecl: return "TypedefDecl";
756 case CXCursor_EnumDecl: return "EnumDecl";
757 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
758 case CXCursor_StructDecl: return "StructDecl";
759 case CXCursor_UnionDecl: return "UnionDecl";
760 case CXCursor_ClassDecl: return "ClassDecl";
761 case CXCursor_FieldDecl: return "FieldDecl";
762 case CXCursor_VarDecl: return "VarDecl";
763 case CXCursor_ParmDecl: return "ParmDecl";
764 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
765 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
766 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
767 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
768 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
769 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
770 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
Douglas Gregorb6998662010-01-19 19:34:47 +0000771 case CXCursor_ObjCImplementationDecl: return "ObjCImplementationDecl";
772 case CXCursor_ObjCCategoryImplDecl: return "ObjCCategoryImplDecl";
Douglas Gregor30122132010-01-19 22:07:56 +0000773 case CXCursor_UnexposedDecl: return "UnexposedDecl";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000774 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
775 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
776 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000777 case CXCursor_TypeRef: return "TypeRef";
Douglas Gregor97b98722010-01-19 23:20:36 +0000778 case CXCursor_UnexposedExpr: return "UnexposedExpr";
779 case CXCursor_DeclRefExpr: return "DeclRefExpr";
780 case CXCursor_MemberRefExpr: return "MemberRefExpr";
781 case CXCursor_CallExpr: return "CallExpr";
782 case CXCursor_ObjCMessageExpr: return "ObjCMessageExpr";
783 case CXCursor_UnexposedStmt: return "UnexposedStmt";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000784 case CXCursor_InvalidFile: return "InvalidFile";
785 case CXCursor_NoDeclFound: return "NoDeclFound";
786 case CXCursor_NotImplemented: return "NotImplemented";
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000787 case CXCursor_TranslationUnit: return "TranslationUnit";
Steve Naroff89922f82009-08-31 00:59:03 +0000788 }
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000789
790 llvm_unreachable("Unhandled CXCursorKind");
791 return NULL;
Steve Naroff600866c2009-08-27 19:51:58 +0000792}
Steve Naroff89922f82009-08-31 00:59:03 +0000793
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000794CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000795 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000796 assert(CTUnit && "Passed null CXTranslationUnit");
797 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000798
Steve Naroff9efa7672009-09-04 15:44:05 +0000799 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000800 const FileEntry *File = FMgr.getFile(source_name,
801 source_name+strlen(source_name));
Ted Kremenekf4629892010-01-14 01:51:23 +0000802 if (!File)
803 return clang_getNullCursor();
804
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000805 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000806 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000807
Steve Narofff96b5242009-10-28 20:44:47 +0000808 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000809 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000810 &LastLoc);
Ted Kremenekf4629892010-01-14 01:51:23 +0000811
812 // FIXME: This doesn't look thread-safe.
Steve Narofff96b5242009-10-28 20:44:47 +0000813 if (ALoc.isValid())
814 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000815
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000816 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000817 if (ALoc.isNamedRef())
818 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000819 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000820 if (Dcl) {
Douglas Gregor97b98722010-01-19 23:20:36 +0000821 if (Stm)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000822 return MakeCXCursor(Stm, Dcl, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +0000823
Steve Naroff85e2db72009-10-01 00:31:07 +0000824 if (ALoc.isNamedRef()) {
Douglas Gregor1adb0822010-01-16 17:14:40 +0000825 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(Dcl))
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000826 return MakeCursorObjCClassRef(Class, ALoc.AsNamedRef().Loc, CXXUnit);
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000827 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(Dcl))
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000828 return MakeCursorObjCProtocolRef(Proto, ALoc.AsNamedRef().Loc, CXXUnit);
Steve Naroff85e2db72009-10-01 00:31:07 +0000829 }
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000830 return MakeCXCursor(Dcl, CXXUnit);
Steve Naroff77128dd2009-09-15 20:25:34 +0000831 }
Douglas Gregor5bfb8c12010-01-20 23:34:41 +0000832 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
Steve Naroff600866c2009-08-27 19:51:58 +0000833}
834
Ted Kremenek73885552009-11-17 19:28:59 +0000835CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +0000836 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +0000837}
838
839unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000840 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +0000841}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000842
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000843unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000844 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
845}
846
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000847unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000848 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
849}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000850
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000851unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000852 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
853}
854
Douglas Gregor97b98722010-01-19 23:20:36 +0000855unsigned clang_isExpression(enum CXCursorKind K) {
856 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
857}
858
859unsigned clang_isStatement(enum CXCursorKind K) {
860 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
861}
862
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000863unsigned clang_isTranslationUnit(enum CXCursorKind K) {
864 return K == CXCursor_TranslationUnit;
865}
866
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000867CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000868 return C.kind;
869}
870
Douglas Gregor97b98722010-01-19 23:20:36 +0000871static SourceLocation getLocationFromExpr(Expr *E) {
872 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
873 return /*FIXME:*/Msg->getLeftLoc();
874 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
875 return DRE->getLocation();
876 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
877 return Member->getMemberLoc();
878 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
879 return Ivar->getLocation();
880 return E->getLocStart();
881}
882
Douglas Gregor98258af2010-01-18 22:46:11 +0000883CXSourceLocation clang_getCursorLocation(CXCursor C) {
884 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +0000885 switch (C.kind) {
886 case CXCursor_ObjCSuperClassRef: {
887 std::pair<ObjCInterfaceDecl *, SourceLocation> P
888 = getCursorObjCSuperClassRef(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000889 return translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +0000890 }
891
892 case CXCursor_ObjCProtocolRef: {
893 std::pair<ObjCProtocolDecl *, SourceLocation> P
894 = getCursorObjCProtocolRef(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000895 return translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +0000896 }
897
898 case CXCursor_ObjCClassRef: {
899 std::pair<ObjCInterfaceDecl *, SourceLocation> P
900 = getCursorObjCClassRef(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000901 return translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +0000902 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000903
904 case CXCursor_TypeRef: {
905 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
906 return translateSourceLocation(P.first->getASTContext(), P.second);
907 }
Douglas Gregorf46034a2010-01-18 23:41:10 +0000908
Douglas Gregorf46034a2010-01-18 23:41:10 +0000909 default:
910 // FIXME: Need a way to enumerate all non-reference cases.
911 llvm_unreachable("Missed a reference kind");
912 }
Douglas Gregor98258af2010-01-18 22:46:11 +0000913 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000914
915 if (clang_isExpression(C.kind))
916 return translateSourceLocation(getCursorContext(C),
917 getLocationFromExpr(getCursorExpr(C)));
918
Douglas Gregor98258af2010-01-18 22:46:11 +0000919 if (!getCursorDecl(C)) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000920 CXSourceLocation empty = { 0, 0 };
Douglas Gregor98258af2010-01-18 22:46:11 +0000921 return empty;
922 }
923
Douglas Gregorf46034a2010-01-18 23:41:10 +0000924 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +0000925 SourceLocation Loc = D->getLocation();
926 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
927 Loc = Class->getClassLoc();
Douglas Gregor1db19de2010-01-19 21:36:55 +0000928 return translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000929}
Douglas Gregora7bde202010-01-19 00:34:46 +0000930
931CXSourceRange clang_getCursorExtent(CXCursor C) {
932 if (clang_isReference(C.kind)) {
933 switch (C.kind) {
934 case CXCursor_ObjCSuperClassRef: {
935 std::pair<ObjCInterfaceDecl *, SourceLocation> P
936 = getCursorObjCSuperClassRef(C);
937 return translateSourceRange(P.first->getASTContext(), P.second);
938 }
939
940 case CXCursor_ObjCProtocolRef: {
941 std::pair<ObjCProtocolDecl *, SourceLocation> P
942 = getCursorObjCProtocolRef(C);
943 return translateSourceRange(P.first->getASTContext(), P.second);
944 }
945
946 case CXCursor_ObjCClassRef: {
947 std::pair<ObjCInterfaceDecl *, SourceLocation> P
948 = getCursorObjCClassRef(C);
949
950 return translateSourceRange(P.first->getASTContext(), P.second);
951 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000952
953 case CXCursor_TypeRef: {
954 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
955 return translateSourceRange(P.first->getASTContext(), P.second);
956 }
Douglas Gregora7bde202010-01-19 00:34:46 +0000957
Douglas Gregora7bde202010-01-19 00:34:46 +0000958 default:
959 // FIXME: Need a way to enumerate all non-reference cases.
960 llvm_unreachable("Missed a reference kind");
961 }
962 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000963
964 if (clang_isExpression(C.kind))
965 return translateSourceRange(getCursorContext(C),
966 getCursorExpr(C)->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +0000967
968 if (!getCursorDecl(C)) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000969 CXSourceRange empty = { 0, 0, 0 };
Douglas Gregora7bde202010-01-19 00:34:46 +0000970 return empty;
971 }
972
973 Decl *D = getCursorDecl(C);
974 return translateSourceRange(D->getASTContext(), D->getSourceRange());
975}
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000976
977CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000978 if (clang_isInvalid(C.kind))
979 return clang_getNullCursor();
980
981 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +0000982 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000983 return C;
Douglas Gregor98258af2010-01-18 22:46:11 +0000984
Douglas Gregor97b98722010-01-19 23:20:36 +0000985 if (clang_isExpression(C.kind)) {
986 Decl *D = getDeclFromExpr(getCursorExpr(C));
987 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000988 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +0000989 return clang_getNullCursor();
990 }
991
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000992 if (!clang_isReference(C.kind))
993 return clang_getNullCursor();
994
995 switch (C.kind) {
996 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000997 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000998
999 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001000 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001001
1002 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001003 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001004
1005 case CXCursor_TypeRef:
1006 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001007
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001008 default:
1009 // We would prefer to enumerate all non-reference cursor kinds here.
1010 llvm_unreachable("Unhandled reference cursor kind");
1011 break;
1012 }
1013 }
1014
1015 return clang_getNullCursor();
1016}
1017
Douglas Gregorb6998662010-01-19 19:34:47 +00001018CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001019 if (clang_isInvalid(C.kind))
1020 return clang_getNullCursor();
1021
1022 ASTUnit *CXXUnit = getCursorASTUnit(C);
1023
Douglas Gregorb6998662010-01-19 19:34:47 +00001024 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001025 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001026 C = clang_getCursorReferenced(C);
1027 WasReference = true;
1028 }
1029
1030 if (!clang_isDeclaration(C.kind))
1031 return clang_getNullCursor();
1032
1033 Decl *D = getCursorDecl(C);
1034 if (!D)
1035 return clang_getNullCursor();
1036
1037 switch (D->getKind()) {
1038 // Declaration kinds that don't really separate the notions of
1039 // declaration and definition.
1040 case Decl::Namespace:
1041 case Decl::Typedef:
1042 case Decl::TemplateTypeParm:
1043 case Decl::EnumConstant:
1044 case Decl::Field:
1045 case Decl::ObjCIvar:
1046 case Decl::ObjCAtDefsField:
1047 case Decl::ImplicitParam:
1048 case Decl::ParmVar:
1049 case Decl::NonTypeTemplateParm:
1050 case Decl::TemplateTemplateParm:
1051 case Decl::ObjCCategoryImpl:
1052 case Decl::ObjCImplementation:
1053 case Decl::LinkageSpec:
1054 case Decl::ObjCPropertyImpl:
1055 case Decl::FileScopeAsm:
1056 case Decl::StaticAssert:
1057 case Decl::Block:
1058 return C;
1059
1060 // Declaration kinds that don't make any sense here, but are
1061 // nonetheless harmless.
1062 case Decl::TranslationUnit:
1063 case Decl::Template:
1064 case Decl::ObjCContainer:
1065 break;
1066
1067 // Declaration kinds for which the definition is not resolvable.
1068 case Decl::UnresolvedUsingTypename:
1069 case Decl::UnresolvedUsingValue:
1070 break;
1071
1072 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001073 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1074 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001075
1076 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001077 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001078
1079 case Decl::Enum:
1080 case Decl::Record:
1081 case Decl::CXXRecord:
1082 case Decl::ClassTemplateSpecialization:
1083 case Decl::ClassTemplatePartialSpecialization:
1084 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition(D->getASTContext()))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001085 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001086 return clang_getNullCursor();
1087
1088 case Decl::Function:
1089 case Decl::CXXMethod:
1090 case Decl::CXXConstructor:
1091 case Decl::CXXDestructor:
1092 case Decl::CXXConversion: {
1093 const FunctionDecl *Def = 0;
1094 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001095 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001096 return clang_getNullCursor();
1097 }
1098
1099 case Decl::Var: {
1100 VarDecl *Var = cast<VarDecl>(D);
1101
1102 // Variables with initializers have definitions.
1103 const VarDecl *Def = 0;
1104 if (Var->getDefinition(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001105 return MakeCXCursor(const_cast<VarDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001106
1107 // extern and private_extern variables are not definitions.
1108 if (Var->hasExternalStorage())
1109 return clang_getNullCursor();
1110
1111 // In-line static data members do not have definitions.
1112 if (Var->isStaticDataMember() && !Var->isOutOfLine())
1113 return clang_getNullCursor();
1114
1115 // All other variables are themselves definitions.
1116 return C;
1117 }
1118
1119 case Decl::FunctionTemplate: {
1120 const FunctionDecl *Def = 0;
1121 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001122 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001123 return clang_getNullCursor();
1124 }
1125
1126 case Decl::ClassTemplate: {
1127 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
1128 ->getDefinition(D->getASTContext()))
1129 return MakeCXCursor(
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001130 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
1131 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001132 return clang_getNullCursor();
1133 }
1134
1135 case Decl::Using: {
1136 UsingDecl *Using = cast<UsingDecl>(D);
1137 CXCursor Def = clang_getNullCursor();
1138 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1139 SEnd = Using->shadow_end();
1140 S != SEnd; ++S) {
1141 if (Def != clang_getNullCursor()) {
1142 // FIXME: We have no way to return multiple results.
1143 return clang_getNullCursor();
1144 }
1145
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001146 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
1147 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001148 }
1149
1150 return Def;
1151 }
1152
1153 case Decl::UsingShadow:
1154 return clang_getCursorDefinition(
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001155 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
1156 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001157
1158 case Decl::ObjCMethod: {
1159 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1160 if (Method->isThisDeclarationADefinition())
1161 return C;
1162
1163 // Dig out the method definition in the associated
1164 // @implementation, if we have it.
1165 // FIXME: The ASTs should make finding the definition easier.
1166 if (ObjCInterfaceDecl *Class
1167 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1168 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1169 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1170 Method->isInstanceMethod()))
1171 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001172 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001173
1174 return clang_getNullCursor();
1175 }
1176
1177 case Decl::ObjCCategory:
1178 if (ObjCCategoryImplDecl *Impl
1179 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001180 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001181 return clang_getNullCursor();
1182
1183 case Decl::ObjCProtocol:
1184 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1185 return C;
1186 return clang_getNullCursor();
1187
1188 case Decl::ObjCInterface:
1189 // There are two notions of a "definition" for an Objective-C
1190 // class: the interface and its implementation. When we resolved a
1191 // reference to an Objective-C class, produce the @interface as
1192 // the definition; when we were provided with the interface,
1193 // produce the @implementation as the definition.
1194 if (WasReference) {
1195 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1196 return C;
1197 } else if (ObjCImplementationDecl *Impl
1198 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001199 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001200 return clang_getNullCursor();
1201
1202 case Decl::ObjCProperty:
1203 // FIXME: We don't really know where to find the
1204 // ObjCPropertyImplDecls that implement this property.
1205 return clang_getNullCursor();
1206
1207 case Decl::ObjCCompatibleAlias:
1208 if (ObjCInterfaceDecl *Class
1209 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1210 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001211 return MakeCXCursor(Class, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001212
1213 return clang_getNullCursor();
1214
1215 case Decl::ObjCForwardProtocol: {
1216 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1217 if (Forward->protocol_size() == 1)
1218 return clang_getCursorDefinition(
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001219 MakeCXCursor(*Forward->protocol_begin(),
1220 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001221
1222 // FIXME: Cannot return multiple definitions.
1223 return clang_getNullCursor();
1224 }
1225
1226 case Decl::ObjCClass: {
1227 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1228 if (Class->size() == 1) {
1229 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1230 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001231 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001232 return clang_getNullCursor();
1233 }
1234
1235 // FIXME: Cannot return multiple definitions.
1236 return clang_getNullCursor();
1237 }
1238
1239 case Decl::Friend:
1240 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001241 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001242 return clang_getNullCursor();
1243
1244 case Decl::FriendTemplate:
1245 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001246 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001247 return clang_getNullCursor();
1248 }
1249
1250 return clang_getNullCursor();
1251}
1252
1253unsigned clang_isCursorDefinition(CXCursor C) {
1254 if (!clang_isDeclaration(C.kind))
1255 return 0;
1256
1257 return clang_getCursorDefinition(C) == C;
1258}
1259
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001260void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001261 const char **startBuf,
1262 const char **endBuf,
1263 unsigned *startLine,
1264 unsigned *startColumn,
1265 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001266 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001267 assert(getCursorDecl(C) && "CXCursor has null decl");
1268 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001269 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1270 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekfb480492010-01-13 21:46:36 +00001271
Steve Naroff4ade6d62009-09-23 17:52:52 +00001272 SourceManager &SM = FD->getASTContext().getSourceManager();
1273 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1274 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1275 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1276 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1277 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1278 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1279}
Ted Kremenekfb480492010-01-13 21:46:36 +00001280
1281} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001282
Ted Kremenekfb480492010-01-13 21:46:36 +00001283//===----------------------------------------------------------------------===//
1284// CXString Operations.
1285//===----------------------------------------------------------------------===//
1286
1287extern "C" {
1288const char *clang_getCString(CXString string) {
1289 return string.Spelling;
1290}
1291
1292void clang_disposeString(CXString string) {
1293 if (string.MustFreeString && string.Spelling)
1294 free((void*)string.Spelling);
1295}
1296} // end: extern "C"