blob: a1c12307012d1c6db5442c1488e186fe513f7741 [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 Gregor1db19de2010-01-19 21:36:55 +0000116typedef llvm::PointerIntPair<ASTContext *, 1, bool> CXSourceLocationPtr;
117
Douglas Gregor98258af2010-01-18 22:46:11 +0000118/// \brief Translate a Clang source location into a CIndex source location.
Douglas Gregor1db19de2010-01-19 21:36:55 +0000119static CXSourceLocation translateSourceLocation(ASTContext &Context,
120 SourceLocation Loc,
121 bool AtEnd = false) {
122 CXSourceLocationPtr Ptr(&Context, AtEnd);
123 CXSourceLocation Result = { Ptr.getOpaqueValue(), Loc.getRawEncoding() };
124 return Result;
Douglas Gregor98258af2010-01-18 22:46:11 +0000125}
126
Douglas Gregora7bde202010-01-19 00:34:46 +0000127/// \brief Translate a Clang source range into a CIndex source range.
Douglas Gregor1db19de2010-01-19 21:36:55 +0000128static CXSourceRange translateSourceRange(ASTContext &Context, SourceRange R) {
129 CXSourceRange Result = { &Context,
130 R.getBegin().getRawEncoding(),
131 R.getEnd().getRawEncoding() };
132 return Result;
Douglas Gregora7bde202010-01-19 00:34:46 +0000133}
134
Douglas Gregor1db19de2010-01-19 21:36:55 +0000135
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000136//===----------------------------------------------------------------------===//
137// Visitors.
138//===----------------------------------------------------------------------===//
139
Steve Naroff89922f82009-08-31 00:59:03 +0000140namespace {
Ted Kremenekedc8aa62010-01-16 00:36:30 +0000141
Douglas Gregorb1373d02010-01-20 20:59:29 +0000142// Cursor visitor.
143class CursorVisitor : public DeclVisitor<CursorVisitor, bool> {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000144 ASTUnit *TU;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000145 CXCursor Parent;
146 CXCursorVisitor Visitor;
147 CXClientData ClientData;
148
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000149 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
150 // to the visitor. Declarations with a PCH level greater than this value will
151 // be suppressed.
152 unsigned MaxPCHLevel;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000153
154 using DeclVisitor<CursorVisitor, bool>::Visit;
155
Steve Naroff89922f82009-08-31 00:59:03 +0000156public:
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000157 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
Douglas Gregorb1373d02010-01-20 20:59:29 +0000158 unsigned MaxPCHLevel)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000159 : TU(TU), Visitor(Visitor), ClientData(ClientData), MaxPCHLevel(MaxPCHLevel)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000160 {
161 Parent.kind = CXCursor_NoDeclFound;
162 Parent.data[0] = 0;
163 Parent.data[1] = 0;
164 Parent.data[2] = 0;
165 }
166
167 bool Visit(CXCursor Cursor);
168 bool VisitChildren(CXCursor Parent);
169
170 bool VisitDeclContext(DeclContext *DC);
171
172 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
173 bool VisitFunctionDecl(FunctionDecl *ND);
174 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
175 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
176 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
177 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
178 bool VisitTagDecl(TagDecl *D);
Steve Naroff89922f82009-08-31 00:59:03 +0000179};
Douglas Gregorb1373d02010-01-20 20:59:29 +0000180
Ted Kremenekab188932010-01-05 19:32:54 +0000181} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000182
Douglas Gregorb1373d02010-01-20 20:59:29 +0000183/// \brief Visit the given cursor and, if requested by the visitor,
184/// its children.
185///
186/// \returns true if the visitation should be aborted, false if it
187/// should continue.
188bool CursorVisitor::Visit(CXCursor Cursor) {
189 if (clang_isInvalid(Cursor.kind))
190 return false;
191
192 if (clang_isDeclaration(Cursor.kind)) {
193 Decl *D = getCursorDecl(Cursor);
194 assert(D && "Invalid declaration cursor");
195 if (D->getPCHLevel() > MaxPCHLevel)
196 return false;
197
198 if (D->isImplicit())
199 return false;
200 }
201
202 switch (Visitor(Cursor, Parent, ClientData)) {
203 case CXChildVisit_Break:
204 return true;
205
206 case CXChildVisit_Continue:
207 return false;
208
209 case CXChildVisit_Recurse:
210 return VisitChildren(Cursor);
211 }
212
213 llvm_unreachable("Silly GCC, we can't get here");
214}
215
216/// \brief Visit the children of the given cursor.
217///
218/// \returns true if the visitation should be aborted, false if it
219/// should continue.
220bool CursorVisitor::VisitChildren(CXCursor Cursor) {
221 // Set the Parent field to Cursor, then back to its old value once we're
222 // done.
223 class SetParentRAII {
224 CXCursor &Parent;
225 CXCursor OldParent;
226
227 public:
228 SetParentRAII(CXCursor &Parent, CXCursor NewParent)
229 : Parent(Parent), OldParent(Parent)
230 {
231 Parent = NewParent;
232 }
233
234 ~SetParentRAII() {
235 Parent = OldParent;
236 }
237 } SetParent(Parent, Cursor);
238
239 if (clang_isDeclaration(Cursor.kind)) {
240 Decl *D = getCursorDecl(Cursor);
241 assert(D && "Invalid declaration cursor");
242 return Visit(D);
243 }
244
245 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000246 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor7b691f332010-01-20 21:13:59 +0000247 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls()) {
248 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
249 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
250 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000251 if (Visit(MakeCXCursor(*it, CXXUnit)))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000252 return true;
253 }
254 } else {
255 return VisitDeclContext(
Douglas Gregorb1373d02010-01-20 20:59:29 +0000256 CXXUnit->getASTContext().getTranslationUnitDecl());
Douglas Gregor7b691f332010-01-20 21:13:59 +0000257 }
258
259 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000260 }
261
262 // Nothing to visit at the moment.
263 // FIXME: Traverse statements, declarations, etc. here.
264 return false;
265}
266
267bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
Douglas Gregor7b691f332010-01-20 21:13:59 +0000268 llvm_unreachable("Translation units are visited directly by Visit()");
269 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000270}
271
272bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000273 for (DeclContext::decl_iterator
Douglas Gregorb1373d02010-01-20 20:59:29 +0000274 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000275 if (Visit(MakeCXCursor(*I, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000276 return true;
277 }
278
279 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000280}
281
Douglas Gregorb1373d02010-01-20 20:59:29 +0000282bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
283 // FIXME: This is wrong. We always want to visit the parameters and
284 // the body, if available.
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000285 if (ND->isThisDeclarationADefinition()) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000286 return VisitDeclContext(ND);
287
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000288#if 0
289 // Not currently needed.
290 CompoundStmt *Body = dyn_cast<CompoundStmt>(ND->getBody());
291 CRefVisitor RVisit(CDecl, Callback, CData);
292 RVisit.Visit(Body);
293#endif
294 }
Douglas Gregorb1373d02010-01-20 20:59:29 +0000295
296 return false;
297}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000298
Douglas Gregorb1373d02010-01-20 20:59:29 +0000299bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000300 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
301 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000302 return true;
303
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000304 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
305 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
306 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000307 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000308 return true;
309
310 return VisitDeclContext(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000311}
312
Douglas Gregorb1373d02010-01-20 20:59:29 +0000313bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000314 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000315 if (D->getSuperClass() &&
316 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000317 D->getSuperClassLoc(),
318 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000319 return true;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000320
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000321 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
322 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
323 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000324 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000325 return true;
326
327 return VisitDeclContext(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000328}
329
Douglas Gregorb1373d02010-01-20 20:59:29 +0000330bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
331 // FIXME: Wrong in the same way that VisitFunctionDecl is wrong.
Douglas Gregorb6998662010-01-19 19:34:47 +0000332 if (ND->getBody())
Douglas Gregorb1373d02010-01-20 20:59:29 +0000333 return VisitDeclContext(ND);
334
335 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000336}
337
Douglas Gregorb1373d02010-01-20 20:59:29 +0000338bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000339 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000340 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000341 E = PID->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000342 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000343 return true;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000344
Douglas Gregorb1373d02010-01-20 20:59:29 +0000345 return VisitDeclContext(PID);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000346}
347
Douglas Gregorb1373d02010-01-20 20:59:29 +0000348bool CursorVisitor::VisitTagDecl(TagDecl *D) {
349 return VisitDeclContext(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000350}
351
Daniel Dunbar140fce22010-01-12 02:34:07 +0000352CXString CIndexer::createCXString(const char *String, bool DupString){
Benjamin Kramer62cf3222009-11-09 19:13:48 +0000353 CXString Str;
354 if (DupString) {
355 Str.Spelling = strdup(String);
356 Str.MustFreeString = 1;
357 } else {
358 Str.Spelling = String;
359 Str.MustFreeString = 0;
360 }
361 return Str;
362}
363
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000364extern "C" {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000365CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000366 int displayDiagnostics) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000367 CIndexer *CIdxr = new CIndexer(new Program());
368 if (excludeDeclarationsFromPCH)
369 CIdxr->setOnlyLocalDecls();
370 if (displayDiagnostics)
371 CIdxr->setDisplayDiagnostics();
372 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000373}
374
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000375void clang_disposeIndex(CXIndex CIdx) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000376 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000377 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000378}
379
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000380void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
381 assert(CIdx && "Passed null CXIndex");
382 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
383 CXXIdx->setUseExternalASTGeneration(value);
384}
385
Steve Naroff50398192009-08-28 15:28:48 +0000386// FIXME: need to pass back error info.
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000387CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
388 const char *ast_filename) {
Steve Naroff50398192009-08-28 15:28:48 +0000389 assert(CIdx && "Passed null CXIndex");
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000390 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000391
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000392 return ASTUnit::LoadFromPCHFile(ast_filename, CXXIdx->getDiags(),
393 CXXIdx->getOnlyLocalDecls(),
394 /* UseBumpAllocator = */ true);
Steve Naroff600866c2009-08-27 19:51:58 +0000395}
396
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000397CXTranslationUnit
398clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
399 const char *source_filename,
400 int num_command_line_args,
401 const char **command_line_args) {
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000402 assert(CIdx && "Passed null CXIndex");
403 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
404
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000405 if (!CXXIdx->getUseExternalASTGeneration()) {
406 llvm::SmallVector<const char *, 16> Args;
407
408 // The 'source_filename' argument is optional. If the caller does not
409 // specify it then it is assumed that the source file is specified
410 // in the actual argument list.
411 if (source_filename)
412 Args.push_back(source_filename);
413 Args.insert(Args.end(), command_line_args,
414 command_line_args + num_command_line_args);
415
Daniel Dunbar94220972009-12-05 02:17:18 +0000416 unsigned NumErrors = CXXIdx->getDiags().getNumErrors();
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000417
Ted Kremenek29b72842010-01-07 22:49:05 +0000418#ifdef USE_CRASHTRACER
419 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000420#endif
421
Daniel Dunbar94220972009-12-05 02:17:18 +0000422 llvm::OwningPtr<ASTUnit> Unit(
423 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Daniel Dunbar869824e2009-12-13 03:46:13 +0000424 CXXIdx->getDiags(),
425 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000426 CXXIdx->getOnlyLocalDecls(),
427 /* UseBumpAllocator = */ true));
Ted Kremenek29b72842010-01-07 22:49:05 +0000428
Daniel Dunbar94220972009-12-05 02:17:18 +0000429 // FIXME: Until we have broader testing, just drop the entire AST if we
430 // encountered an error.
431 if (NumErrors != CXXIdx->getDiags().getNumErrors())
432 return 0;
433
434 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000435 }
436
Ted Kremenek139ba862009-10-22 00:03:57 +0000437 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000438 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000439
Ted Kremenek139ba862009-10-22 00:03:57 +0000440 // First add the complete path to the 'clang' executable.
441 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000442 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000443
Ted Kremenek139ba862009-10-22 00:03:57 +0000444 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000445 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000446
Ted Kremenek139ba862009-10-22 00:03:57 +0000447 // The 'source_filename' argument is optional. If the caller does not
448 // specify it then it is assumed that the source file is specified
449 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000450 if (source_filename)
451 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +0000452
Steve Naroff37b5ac22009-10-15 20:50:09 +0000453 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +0000454 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +0000455 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +0000456 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +0000457
458 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
459 for (int i = 0; i < num_command_line_args; ++i)
460 if (const char *arg = command_line_args[i]) {
461 if (strcmp(arg, "-o") == 0) {
462 ++i; // Also skip the matching argument.
463 continue;
464 }
465 if (strcmp(arg, "-emit-ast") == 0 ||
466 strcmp(arg, "-c") == 0 ||
467 strcmp(arg, "-fsyntax-only") == 0) {
468 continue;
469 }
470
471 // Keep the argument.
472 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000473 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000474
Ted Kremenek139ba862009-10-22 00:03:57 +0000475 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +0000476 argv.push_back(NULL);
477
Ted Kremenekfeb15e32009-10-26 22:14:08 +0000478 // Invoke 'clang'.
479 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
480 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +0000481 std::string ErrMsg;
Ted Kremenekc46e4632009-10-19 22:27:32 +0000482 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DevNull, NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +0000483 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
484 /* redirects */ !CXXIdx->getDisplayDiagnostics() ? &Redirects[0] : NULL,
485 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000486
Ted Kremenek0854d702009-11-10 19:18:52 +0000487 if (CXXIdx->getDisplayDiagnostics() && !ErrMsg.empty()) {
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000488 llvm::errs() << "clang_createTranslationUnitFromSourceFile: " << ErrMsg
Daniel Dunbaracca7252009-11-30 20:42:49 +0000489 << '\n' << "Arguments: \n";
Ted Kremenek379afec2009-10-22 03:24:01 +0000490 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Ted Kremenek779e5f42009-10-26 22:08:39 +0000491 I!=E; ++I) {
492 if (*I)
493 llvm::errs() << ' ' << *I << '\n';
494 }
495 llvm::errs() << '\n';
Ted Kremenek379afec2009-10-22 03:24:01 +0000496 }
Benjamin Kramer0829a832009-10-18 11:19:36 +0000497
Steve Naroff37b5ac22009-10-15 20:50:09 +0000498 // Finally, we create the translation unit from the ast file.
Steve Naroffe19944c2009-10-15 22:23:48 +0000499 ASTUnit *ATU = static_cast<ASTUnit *>(
Daniel Dunbaracca7252009-11-30 20:42:49 +0000500 clang_createTranslationUnit(CIdx, astTmpFile));
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000501 if (ATU)
502 ATU->unlinkTemporaryFile();
Steve Naroffe19944c2009-10-15 22:23:48 +0000503 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +0000504}
505
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000506void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000507 assert(CTUnit && "Passed null CXTranslationUnit");
508 delete static_cast<ASTUnit *>(CTUnit);
509}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000510
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000511CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000512 assert(CTUnit && "Passed null CXTranslationUnit");
Steve Naroff77accc12009-09-03 18:19:54 +0000513 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenek4b333d22010-01-12 00:36:38 +0000514 return CIndexer::createCXString(CXXUnit->getOriginalSourceFileName().c_str(),
515 true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000516}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +0000517
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000518CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000519 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000520 return Result;
521}
522
Ted Kremenekfb480492010-01-13 21:46:36 +0000523} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +0000524
Ted Kremenekfb480492010-01-13 21:46:36 +0000525//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +0000526// CXSourceLocation and CXSourceRange Operations.
527//===----------------------------------------------------------------------===//
528
529void clang_getInstantiationLocation(CXSourceLocation location,
530 CXFile *file,
531 unsigned *line,
532 unsigned *column) {
533 CXSourceLocationPtr Ptr
534 = CXSourceLocationPtr::getFromOpaqueValue(location.ptr_data);
535 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
536
537 if (!Ptr.getPointer() || Loc.isInvalid()) {
538 if (file)
539 *file = 0;
540 if (line)
541 *line = 0;
542 if (column)
543 *column = 0;
544 return;
545 }
546
547 // FIXME: This is largely copy-paste from
548 ///TextDiagnosticPrinter::HighlightRange. When it is clear that this is
549 // what we want the two routines should be refactored.
550 ASTContext &Context = *Ptr.getPointer();
551 SourceManager &SM = Context.getSourceManager();
552 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
553
554 if (Ptr.getInt()) {
555 // We want the last character in this location, so we will adjust
556 // the instantiation location accordingly.
557
558 // If the location is from a macro instantiation, get the end of
559 // the instantiation range.
560 if (Loc.isMacroID())
561 InstLoc = SM.getInstantiationRange(Loc).second;
562
563 // Measure the length token we're pointing at, so we can adjust
564 // the physical location in the file to point at the last
565 // character.
566 // FIXME: This won't cope with trigraphs or escaped newlines
567 // well. For that, we actually need a preprocessor, which isn't
568 // currently available here. Eventually, we'll switch the pointer
569 // data of CXSourceLocation/CXSourceRange to a translation unit
570 // (CXXUnit), so that the preprocessor will be available here. At
571 // that point, we can use Preprocessor::getLocForEndOfToken().
572 unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM,
573 Context.getLangOptions());
574 if (Length > 0)
575 InstLoc = InstLoc.getFileLocWithOffset(Length - 1);
576 }
577
578 if (file)
579 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
580 if (line)
581 *line = SM.getInstantiationLineNumber(InstLoc);
582 if (column)
583 *column = SM.getInstantiationColumnNumber(InstLoc);
584}
585
586CXSourceLocation clang_getRangeStart(CXSourceRange range) {
587 CXSourceLocation Result = { range.ptr_data, range.begin_int_data };
588 return Result;
589}
590
591CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
592 llvm::PointerIntPair<ASTContext *, 1, bool> Ptr;
593 Ptr.setPointer(static_cast<ASTContext *>(range.ptr_data));
594 Ptr.setInt(true);
595 CXSourceLocation Result = { Ptr.getOpaqueValue(), range.end_int_data };
596 return Result;
597}
598
599//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +0000600// CXFile Operations.
601//===----------------------------------------------------------------------===//
602
603extern "C" {
Steve Naroff88145032009-10-27 14:35:18 +0000604const char *clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000605 if (!SFile)
606 return 0;
607
Steve Naroff88145032009-10-27 14:35:18 +0000608 assert(SFile && "Passed null CXFile");
609 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
610 return FEnt->getName();
611}
612
613time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000614 if (!SFile)
615 return 0;
616
Steve Naroff88145032009-10-27 14:35:18 +0000617 assert(SFile && "Passed null CXFile");
618 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
619 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +0000620}
Ted Kremenekfb480492010-01-13 21:46:36 +0000621} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +0000622
Ted Kremenekfb480492010-01-13 21:46:36 +0000623//===----------------------------------------------------------------------===//
624// CXCursor Operations.
625//===----------------------------------------------------------------------===//
626
Ted Kremenekfb480492010-01-13 21:46:36 +0000627static Decl *getDeclFromExpr(Stmt *E) {
628 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
629 return RefExpr->getDecl();
630 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
631 return ME->getMemberDecl();
632 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
633 return RE->getDecl();
634
635 if (CallExpr *CE = dyn_cast<CallExpr>(E))
636 return getDeclFromExpr(CE->getCallee());
637 if (CastExpr *CE = dyn_cast<CastExpr>(E))
638 return getDeclFromExpr(CE->getSubExpr());
639 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
640 return OME->getMethodDecl();
641
642 return 0;
643}
644
645extern "C" {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000646
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000647unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +0000648 CXCursorVisitor visitor,
649 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000650 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000651
652 unsigned PCHLevel = Decl::MaxPCHLevel;
653
654 // Set the PCHLevel to filter out unwanted decls if requested.
655 if (CXXUnit->getOnlyLocalDecls()) {
656 PCHLevel = 0;
657
658 // If the main input was an AST, bump the level.
659 if (CXXUnit->isMainFileAST())
660 ++PCHLevel;
661 }
662
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000663 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000664 return CursorVis.VisitChildren(parent);
665}
666
Douglas Gregor78205d42010-01-20 21:45:58 +0000667static CXString getDeclSpelling(Decl *D) {
668 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
669 if (!ND)
670 return CIndexer::createCXString("");
671
672 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
673 return CIndexer::createCXString(OMD->getSelector().getAsString().c_str(),
674 true);
675
676 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
677 // No, this isn't the same as the code below. getIdentifier() is non-virtual
678 // and returns different names. NamedDecl returns the class name and
679 // ObjCCategoryImplDecl returns the category name.
680 return CIndexer::createCXString(CIMP->getIdentifier()->getNameStart());
681
682 if (ND->getIdentifier())
683 return CIndexer::createCXString(ND->getIdentifier()->getNameStart());
684
685 return CIndexer::createCXString("");
686}
687
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000688CXString clang_getCursorSpelling(CXCursor C) {
Daniel Dunbarc81d7182010-01-18 17:52:42 +0000689 assert(getCursorDecl(C) && "CXCursor has null decl");
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000690 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000691 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000692
Steve Narofff334b4e2009-09-02 18:26:48 +0000693 if (clang_isReference(C.kind)) {
694 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000695 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +0000696 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
697 return CIndexer::createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000698 }
699 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +0000700 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
701 return CIndexer::createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000702 }
703 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000704 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +0000705 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenek4b333d22010-01-12 00:36:38 +0000706 return CIndexer::createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +0000707 }
Daniel Dunbaracca7252009-11-30 20:42:49 +0000708 default:
Ted Kremenek4b333d22010-01-12 00:36:38 +0000709 return CIndexer::createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +0000710 }
711 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000712
713 if (clang_isExpression(C.kind)) {
714 Decl *D = getDeclFromExpr(getCursorExpr(C));
715 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +0000716 return getDeclSpelling(D);
Douglas Gregor97b98722010-01-19 23:20:36 +0000717 return CIndexer::createCXString("");
718 }
719
Douglas Gregor78205d42010-01-20 21:45:58 +0000720 return getDeclSpelling(getCursorDecl(C));
Steve Narofff334b4e2009-09-02 18:26:48 +0000721}
722
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000723const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +0000724 switch (Kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +0000725 case CXCursor_FunctionDecl: return "FunctionDecl";
726 case CXCursor_TypedefDecl: return "TypedefDecl";
727 case CXCursor_EnumDecl: return "EnumDecl";
728 case CXCursor_EnumConstantDecl: return "EnumConstantDecl";
729 case CXCursor_StructDecl: return "StructDecl";
730 case CXCursor_UnionDecl: return "UnionDecl";
731 case CXCursor_ClassDecl: return "ClassDecl";
732 case CXCursor_FieldDecl: return "FieldDecl";
733 case CXCursor_VarDecl: return "VarDecl";
734 case CXCursor_ParmDecl: return "ParmDecl";
735 case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl";
736 case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl";
737 case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl";
738 case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl";
739 case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl";
740 case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl";
741 case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl";
Douglas Gregorb6998662010-01-19 19:34:47 +0000742 case CXCursor_ObjCImplementationDecl: return "ObjCImplementationDecl";
743 case CXCursor_ObjCCategoryImplDecl: return "ObjCCategoryImplDecl";
Douglas Gregor30122132010-01-19 22:07:56 +0000744 case CXCursor_UnexposedDecl: return "UnexposedDecl";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000745 case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef";
746 case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef";
747 case CXCursor_ObjCClassRef: return "ObjCClassRef";
Douglas Gregor97b98722010-01-19 23:20:36 +0000748 case CXCursor_UnexposedExpr: return "UnexposedExpr";
749 case CXCursor_DeclRefExpr: return "DeclRefExpr";
750 case CXCursor_MemberRefExpr: return "MemberRefExpr";
751 case CXCursor_CallExpr: return "CallExpr";
752 case CXCursor_ObjCMessageExpr: return "ObjCMessageExpr";
753 case CXCursor_UnexposedStmt: return "UnexposedStmt";
Daniel Dunbaracca7252009-11-30 20:42:49 +0000754 case CXCursor_InvalidFile: return "InvalidFile";
755 case CXCursor_NoDeclFound: return "NoDeclFound";
756 case CXCursor_NotImplemented: return "NotImplemented";
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000757 case CXCursor_TranslationUnit: return "TranslationUnit";
Steve Naroff89922f82009-08-31 00:59:03 +0000758 }
Ted Kremenekdeb06bd2010-01-16 02:02:09 +0000759
760 llvm_unreachable("Unhandled CXCursorKind");
761 return NULL;
Steve Naroff600866c2009-08-27 19:51:58 +0000762}
Steve Naroff89922f82009-08-31 00:59:03 +0000763
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000764CXCursor clang_getCursor(CXTranslationUnit CTUnit, const char *source_name,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000765 unsigned line, unsigned column) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000766 assert(CTUnit && "Passed null CXTranslationUnit");
767 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000768
Steve Naroff9efa7672009-09-04 15:44:05 +0000769 FileManager &FMgr = CXXUnit->getFileManager();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000770 const FileEntry *File = FMgr.getFile(source_name,
771 source_name+strlen(source_name));
Ted Kremenekf4629892010-01-14 01:51:23 +0000772 if (!File)
773 return clang_getNullCursor();
774
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000775 SourceLocation SLoc =
Steve Naroff9efa7672009-09-04 15:44:05 +0000776 CXXUnit->getSourceManager().getLocation(File, line, column);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000777
Steve Narofff96b5242009-10-28 20:44:47 +0000778 ASTLocation LastLoc = CXXUnit->getLastASTLocation();
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000779 ASTLocation ALoc = ResolveLocationInAST(CXXUnit->getASTContext(), SLoc,
Steve Narofff96b5242009-10-28 20:44:47 +0000780 &LastLoc);
Ted Kremenekf4629892010-01-14 01:51:23 +0000781
782 // FIXME: This doesn't look thread-safe.
Steve Narofff96b5242009-10-28 20:44:47 +0000783 if (ALoc.isValid())
784 CXXUnit->setLastASTLocation(ALoc);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000785
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000786 Decl *Dcl = ALoc.getParentDecl();
Argyrios Kyrtzidis05a76512009-09-29 19:45:58 +0000787 if (ALoc.isNamedRef())
788 Dcl = ALoc.AsNamedRef().ND;
Argyrios Kyrtzidisf4526e32009-09-29 19:44:27 +0000789 Stmt *Stm = ALoc.dyn_AsStmt();
Steve Naroff4ade6d62009-09-23 17:52:52 +0000790 if (Dcl) {
Douglas Gregor97b98722010-01-19 23:20:36 +0000791 if (Stm)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000792 return MakeCXCursor(Stm, Dcl, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +0000793
Steve Naroff85e2db72009-10-01 00:31:07 +0000794 if (ALoc.isNamedRef()) {
Douglas Gregor1adb0822010-01-16 17:14:40 +0000795 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(Dcl))
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000796 return MakeCursorObjCClassRef(Class, ALoc.AsNamedRef().Loc, CXXUnit);
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000797 if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(Dcl))
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000798 return MakeCursorObjCProtocolRef(Proto, ALoc.AsNamedRef().Loc, CXXUnit);
Steve Naroff85e2db72009-10-01 00:31:07 +0000799 }
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000800 return MakeCXCursor(Dcl, CXXUnit);
Steve Naroff77128dd2009-09-15 20:25:34 +0000801 }
Douglas Gregor5bfb8c12010-01-20 23:34:41 +0000802 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
Steve Naroff600866c2009-08-27 19:51:58 +0000803}
804
Ted Kremenek73885552009-11-17 19:28:59 +0000805CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +0000806 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +0000807}
808
809unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000810 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +0000811}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000812
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000813unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000814 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
815}
816
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000817unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +0000818 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
819}
Steve Naroff2d4d6292009-08-31 14:26:51 +0000820
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000821unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +0000822 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
823}
824
Douglas Gregor97b98722010-01-19 23:20:36 +0000825unsigned clang_isExpression(enum CXCursorKind K) {
826 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
827}
828
829unsigned clang_isStatement(enum CXCursorKind K) {
830 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
831}
832
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +0000833unsigned clang_isTranslationUnit(enum CXCursorKind K) {
834 return K == CXCursor_TranslationUnit;
835}
836
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000837CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +0000838 return C.kind;
839}
840
Douglas Gregor97b98722010-01-19 23:20:36 +0000841static SourceLocation getLocationFromExpr(Expr *E) {
842 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
843 return /*FIXME:*/Msg->getLeftLoc();
844 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
845 return DRE->getLocation();
846 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
847 return Member->getMemberLoc();
848 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
849 return Ivar->getLocation();
850 return E->getLocStart();
851}
852
Douglas Gregor98258af2010-01-18 22:46:11 +0000853CXSourceLocation clang_getCursorLocation(CXCursor C) {
854 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +0000855 switch (C.kind) {
856 case CXCursor_ObjCSuperClassRef: {
857 std::pair<ObjCInterfaceDecl *, SourceLocation> P
858 = getCursorObjCSuperClassRef(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000859 return translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +0000860 }
861
862 case CXCursor_ObjCProtocolRef: {
863 std::pair<ObjCProtocolDecl *, SourceLocation> P
864 = getCursorObjCProtocolRef(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000865 return translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +0000866 }
867
868 case CXCursor_ObjCClassRef: {
869 std::pair<ObjCInterfaceDecl *, SourceLocation> P
870 = getCursorObjCClassRef(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000871 return translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +0000872 }
873
Douglas Gregorf46034a2010-01-18 23:41:10 +0000874 default:
875 // FIXME: Need a way to enumerate all non-reference cases.
876 llvm_unreachable("Missed a reference kind");
877 }
Douglas Gregor98258af2010-01-18 22:46:11 +0000878 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000879
880 if (clang_isExpression(C.kind))
881 return translateSourceLocation(getCursorContext(C),
882 getLocationFromExpr(getCursorExpr(C)));
883
Douglas Gregor98258af2010-01-18 22:46:11 +0000884 if (!getCursorDecl(C)) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000885 CXSourceLocation empty = { 0, 0 };
Douglas Gregor98258af2010-01-18 22:46:11 +0000886 return empty;
887 }
888
Douglas Gregorf46034a2010-01-18 23:41:10 +0000889 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +0000890 SourceLocation Loc = D->getLocation();
891 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
892 Loc = Class->getClassLoc();
Douglas Gregor1db19de2010-01-19 21:36:55 +0000893 return translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000894}
Douglas Gregora7bde202010-01-19 00:34:46 +0000895
896CXSourceRange clang_getCursorExtent(CXCursor C) {
897 if (clang_isReference(C.kind)) {
898 switch (C.kind) {
899 case CXCursor_ObjCSuperClassRef: {
900 std::pair<ObjCInterfaceDecl *, SourceLocation> P
901 = getCursorObjCSuperClassRef(C);
902 return translateSourceRange(P.first->getASTContext(), P.second);
903 }
904
905 case CXCursor_ObjCProtocolRef: {
906 std::pair<ObjCProtocolDecl *, SourceLocation> P
907 = getCursorObjCProtocolRef(C);
908 return translateSourceRange(P.first->getASTContext(), P.second);
909 }
910
911 case CXCursor_ObjCClassRef: {
912 std::pair<ObjCInterfaceDecl *, SourceLocation> P
913 = getCursorObjCClassRef(C);
914
915 return translateSourceRange(P.first->getASTContext(), P.second);
916 }
917
Douglas Gregora7bde202010-01-19 00:34:46 +0000918 default:
919 // FIXME: Need a way to enumerate all non-reference cases.
920 llvm_unreachable("Missed a reference kind");
921 }
922 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000923
924 if (clang_isExpression(C.kind))
925 return translateSourceRange(getCursorContext(C),
926 getCursorExpr(C)->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +0000927
928 if (!getCursorDecl(C)) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000929 CXSourceRange empty = { 0, 0, 0 };
Douglas Gregora7bde202010-01-19 00:34:46 +0000930 return empty;
931 }
932
933 Decl *D = getCursorDecl(C);
934 return translateSourceRange(D->getASTContext(), D->getSourceRange());
935}
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000936
937CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000938 if (clang_isInvalid(C.kind))
939 return clang_getNullCursor();
940
941 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +0000942 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000943 return C;
Douglas Gregor98258af2010-01-18 22:46:11 +0000944
Douglas Gregor97b98722010-01-19 23:20:36 +0000945 if (clang_isExpression(C.kind)) {
946 Decl *D = getDeclFromExpr(getCursorExpr(C));
947 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000948 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +0000949 return clang_getNullCursor();
950 }
951
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000952 if (!clang_isReference(C.kind))
953 return clang_getNullCursor();
954
955 switch (C.kind) {
956 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000957 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000958
959 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000960 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000961
962 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000963 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000964
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000965 default:
966 // We would prefer to enumerate all non-reference cursor kinds here.
967 llvm_unreachable("Unhandled reference cursor kind");
968 break;
969 }
970 }
971
972 return clang_getNullCursor();
973}
974
Douglas Gregorb6998662010-01-19 19:34:47 +0000975CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000976 if (clang_isInvalid(C.kind))
977 return clang_getNullCursor();
978
979 ASTUnit *CXXUnit = getCursorASTUnit(C);
980
Douglas Gregorb6998662010-01-19 19:34:47 +0000981 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +0000982 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +0000983 C = clang_getCursorReferenced(C);
984 WasReference = true;
985 }
986
987 if (!clang_isDeclaration(C.kind))
988 return clang_getNullCursor();
989
990 Decl *D = getCursorDecl(C);
991 if (!D)
992 return clang_getNullCursor();
993
994 switch (D->getKind()) {
995 // Declaration kinds that don't really separate the notions of
996 // declaration and definition.
997 case Decl::Namespace:
998 case Decl::Typedef:
999 case Decl::TemplateTypeParm:
1000 case Decl::EnumConstant:
1001 case Decl::Field:
1002 case Decl::ObjCIvar:
1003 case Decl::ObjCAtDefsField:
1004 case Decl::ImplicitParam:
1005 case Decl::ParmVar:
1006 case Decl::NonTypeTemplateParm:
1007 case Decl::TemplateTemplateParm:
1008 case Decl::ObjCCategoryImpl:
1009 case Decl::ObjCImplementation:
1010 case Decl::LinkageSpec:
1011 case Decl::ObjCPropertyImpl:
1012 case Decl::FileScopeAsm:
1013 case Decl::StaticAssert:
1014 case Decl::Block:
1015 return C;
1016
1017 // Declaration kinds that don't make any sense here, but are
1018 // nonetheless harmless.
1019 case Decl::TranslationUnit:
1020 case Decl::Template:
1021 case Decl::ObjCContainer:
1022 break;
1023
1024 // Declaration kinds for which the definition is not resolvable.
1025 case Decl::UnresolvedUsingTypename:
1026 case Decl::UnresolvedUsingValue:
1027 break;
1028
1029 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001030 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1031 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001032
1033 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001034 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001035
1036 case Decl::Enum:
1037 case Decl::Record:
1038 case Decl::CXXRecord:
1039 case Decl::ClassTemplateSpecialization:
1040 case Decl::ClassTemplatePartialSpecialization:
1041 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition(D->getASTContext()))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001042 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001043 return clang_getNullCursor();
1044
1045 case Decl::Function:
1046 case Decl::CXXMethod:
1047 case Decl::CXXConstructor:
1048 case Decl::CXXDestructor:
1049 case Decl::CXXConversion: {
1050 const FunctionDecl *Def = 0;
1051 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001052 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001053 return clang_getNullCursor();
1054 }
1055
1056 case Decl::Var: {
1057 VarDecl *Var = cast<VarDecl>(D);
1058
1059 // Variables with initializers have definitions.
1060 const VarDecl *Def = 0;
1061 if (Var->getDefinition(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001062 return MakeCXCursor(const_cast<VarDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001063
1064 // extern and private_extern variables are not definitions.
1065 if (Var->hasExternalStorage())
1066 return clang_getNullCursor();
1067
1068 // In-line static data members do not have definitions.
1069 if (Var->isStaticDataMember() && !Var->isOutOfLine())
1070 return clang_getNullCursor();
1071
1072 // All other variables are themselves definitions.
1073 return C;
1074 }
1075
1076 case Decl::FunctionTemplate: {
1077 const FunctionDecl *Def = 0;
1078 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001079 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001080 return clang_getNullCursor();
1081 }
1082
1083 case Decl::ClassTemplate: {
1084 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
1085 ->getDefinition(D->getASTContext()))
1086 return MakeCXCursor(
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001087 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
1088 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001089 return clang_getNullCursor();
1090 }
1091
1092 case Decl::Using: {
1093 UsingDecl *Using = cast<UsingDecl>(D);
1094 CXCursor Def = clang_getNullCursor();
1095 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1096 SEnd = Using->shadow_end();
1097 S != SEnd; ++S) {
1098 if (Def != clang_getNullCursor()) {
1099 // FIXME: We have no way to return multiple results.
1100 return clang_getNullCursor();
1101 }
1102
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001103 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
1104 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001105 }
1106
1107 return Def;
1108 }
1109
1110 case Decl::UsingShadow:
1111 return clang_getCursorDefinition(
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001112 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
1113 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001114
1115 case Decl::ObjCMethod: {
1116 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1117 if (Method->isThisDeclarationADefinition())
1118 return C;
1119
1120 // Dig out the method definition in the associated
1121 // @implementation, if we have it.
1122 // FIXME: The ASTs should make finding the definition easier.
1123 if (ObjCInterfaceDecl *Class
1124 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1125 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1126 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1127 Method->isInstanceMethod()))
1128 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001129 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001130
1131 return clang_getNullCursor();
1132 }
1133
1134 case Decl::ObjCCategory:
1135 if (ObjCCategoryImplDecl *Impl
1136 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001137 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001138 return clang_getNullCursor();
1139
1140 case Decl::ObjCProtocol:
1141 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1142 return C;
1143 return clang_getNullCursor();
1144
1145 case Decl::ObjCInterface:
1146 // There are two notions of a "definition" for an Objective-C
1147 // class: the interface and its implementation. When we resolved a
1148 // reference to an Objective-C class, produce the @interface as
1149 // the definition; when we were provided with the interface,
1150 // produce the @implementation as the definition.
1151 if (WasReference) {
1152 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1153 return C;
1154 } else if (ObjCImplementationDecl *Impl
1155 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001156 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001157 return clang_getNullCursor();
1158
1159 case Decl::ObjCProperty:
1160 // FIXME: We don't really know where to find the
1161 // ObjCPropertyImplDecls that implement this property.
1162 return clang_getNullCursor();
1163
1164 case Decl::ObjCCompatibleAlias:
1165 if (ObjCInterfaceDecl *Class
1166 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1167 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001168 return MakeCXCursor(Class, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001169
1170 return clang_getNullCursor();
1171
1172 case Decl::ObjCForwardProtocol: {
1173 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1174 if (Forward->protocol_size() == 1)
1175 return clang_getCursorDefinition(
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001176 MakeCXCursor(*Forward->protocol_begin(),
1177 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001178
1179 // FIXME: Cannot return multiple definitions.
1180 return clang_getNullCursor();
1181 }
1182
1183 case Decl::ObjCClass: {
1184 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1185 if (Class->size() == 1) {
1186 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1187 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001188 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001189 return clang_getNullCursor();
1190 }
1191
1192 // FIXME: Cannot return multiple definitions.
1193 return clang_getNullCursor();
1194 }
1195
1196 case Decl::Friend:
1197 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001198 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001199 return clang_getNullCursor();
1200
1201 case Decl::FriendTemplate:
1202 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001203 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001204 return clang_getNullCursor();
1205 }
1206
1207 return clang_getNullCursor();
1208}
1209
1210unsigned clang_isCursorDefinition(CXCursor C) {
1211 if (!clang_isDeclaration(C.kind))
1212 return 0;
1213
1214 return clang_getCursorDefinition(C) == C;
1215}
1216
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001217void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001218 const char **startBuf,
1219 const char **endBuf,
1220 unsigned *startLine,
1221 unsigned *startColumn,
1222 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001223 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001224 assert(getCursorDecl(C) && "CXCursor has null decl");
1225 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001226 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1227 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekfb480492010-01-13 21:46:36 +00001228
Steve Naroff4ade6d62009-09-23 17:52:52 +00001229 SourceManager &SM = FD->getASTContext().getSourceManager();
1230 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1231 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1232 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1233 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1234 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1235 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1236}
Ted Kremenekfb480492010-01-13 21:46:36 +00001237
1238} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001239
Ted Kremenekfb480492010-01-13 21:46:36 +00001240//===----------------------------------------------------------------------===//
1241// CXString Operations.
1242//===----------------------------------------------------------------------===//
1243
1244extern "C" {
1245const char *clang_getCString(CXString string) {
1246 return string.Spelling;
1247}
1248
1249void clang_disposeString(CXString string) {
1250 if (string.MustFreeString && string.Spelling)
1251 free((void*)string.Spelling);
1252}
1253} // end: extern "C"