blob: 6fc7b530307da13dbd92a68b7c0580edcd7a34ee [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 Kremeneka297de22010-01-25 22:34:44 +000017#include "CXSourceLocation.h"
Douglas Gregor5352ac02010-01-28 00:27:43 +000018#include "CIndexDiagnostic.h"
Ted Kremenekab188932010-01-05 19:32:54 +000019
Ted Kremenek04bb7162010-01-22 22:44:15 +000020#include "clang/Basic/Version.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000021
Steve Naroff50398192009-08-28 15:28:48 +000022#include "clang/AST/DeclVisitor.h"
Steve Narofffb570422009-09-22 19:25:29 +000023#include "clang/AST/StmtVisitor.h"
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000024#include "clang/AST/TypeLocVisitor.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000025#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenekd8210652010-01-06 23:43:31 +000026#include "clang/Lex/Lexer.h"
Douglas Gregor33e9abd2010-01-22 19:49:59 +000027#include "clang/Lex/Preprocessor.h"
Douglas Gregor02465752009-10-16 21:24:31 +000028#include "llvm/Support/MemoryBuffer.h"
Benjamin Kramer0829a832009-10-18 11:19:36 +000029#include "llvm/System/Program.h"
Douglas Gregor0a812cf2010-02-18 23:07:20 +000030#include "llvm/System/Signals.h"
Ted Kremenekfc062212009-10-19 21:44:57 +000031
Ted Kremenekdb3d0da2010-01-05 20:55:39 +000032// Needed to define L_TMPNAM on some systems.
33#include <cstdio>
34
Steve Naroff50398192009-08-28 15:28:48 +000035using namespace clang;
Ted Kremenek16c440a2010-01-15 20:35:54 +000036using namespace clang::cxcursor;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000037using namespace clang::cxstring;
Steve Naroff50398192009-08-28 15:28:48 +000038using namespace idx;
39
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000040//===----------------------------------------------------------------------===//
41// Crash Reporting.
42//===----------------------------------------------------------------------===//
43
44#ifdef __APPLE__
Ted Kremenek29b72842010-01-07 22:49:05 +000045#define USE_CRASHTRACER
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000046#include "clang/Analysis/Support/SaveAndRestore.h"
47// Integrate with crash reporter.
48extern "C" const char *__crashreporter_info__;
Ted Kremenek6b569992010-02-17 21:12:23 +000049#define NUM_CRASH_STRINGS 32
Ted Kremenek29b72842010-01-07 22:49:05 +000050static unsigned crashtracer_counter = 0;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000051static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek29b72842010-01-07 22:49:05 +000052static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
53static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
54
55static unsigned SetCrashTracerInfo(const char *str,
56 llvm::SmallString<1024> &AggStr) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +000057
Ted Kremenek254ba7c2010-01-07 23:13:53 +000058 unsigned slot = 0;
Ted Kremenek29b72842010-01-07 22:49:05 +000059 while (crashtracer_strings[slot]) {
60 if (++slot == NUM_CRASH_STRINGS)
61 slot = 0;
62 }
63 crashtracer_strings[slot] = str;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000064 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek29b72842010-01-07 22:49:05 +000065
66 // We need to create an aggregate string because multiple threads
67 // may be in this method at one time. The crash reporter string
68 // will attempt to overapproximate the set of in-flight invocations
69 // of this function. Race conditions can still cause this goal
70 // to not be achieved.
71 {
Ted Kremenekf0e23e82010-02-17 00:41:40 +000072 llvm::raw_svector_ostream Out(AggStr);
Ted Kremenek29b72842010-01-07 22:49:05 +000073 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
74 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
75 }
76 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
77 return slot;
78}
79
80static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek254ba7c2010-01-07 23:13:53 +000081 unsigned max_slot = 0;
82 unsigned max_value = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +000083
Ted Kremenek254ba7c2010-01-07 23:13:53 +000084 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
85
86 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
87 if (agg_crashtracer_strings[i] &&
88 crashtracer_counter_id[i] > max_value) {
89 max_slot = i;
90 max_value = crashtracer_counter_id[i];
Ted Kremenek29b72842010-01-07 22:49:05 +000091 }
Ted Kremenek254ba7c2010-01-07 23:13:53 +000092
93 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek29b72842010-01-07 22:49:05 +000094}
95
96namespace {
97class ArgsCrashTracerInfo {
98 llvm::SmallString<1024> CrashString;
99 llvm::SmallString<1024> AggregateString;
100 unsigned crashtracerSlot;
101public:
102 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
103 : crashtracerSlot(0)
104 {
105 {
106 llvm::raw_svector_ostream Out(CrashString);
107 Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
108 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
109 E=Args.end(); I!=E; ++I)
110 Out << ' ' << *I;
111 }
112 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
113 AggregateString);
114 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000115
Ted Kremenek29b72842010-01-07 22:49:05 +0000116 ~ArgsCrashTracerInfo() {
117 ResetCrashTracerInfo(crashtracerSlot);
118 }
119};
120}
121#endif
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000122
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000123/// \brief The result of comparing two source ranges.
124enum RangeComparisonResult {
125 /// \brief Either the ranges overlap or one of the ranges is invalid.
126 RangeOverlap,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000127
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000128 /// \brief The first range ends before the second range starts.
129 RangeBefore,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000130
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000131 /// \brief The first range starts after the second range ends.
132 RangeAfter
133};
134
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000135/// \brief Compare two source ranges to determine their relative position in
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000136/// the translation unit.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000137static RangeComparisonResult RangeCompare(SourceManager &SM,
138 SourceRange R1,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000139 SourceRange R2) {
140 assert(R1.isValid() && "First range is invalid?");
141 assert(R2.isValid() && "Second range is invalid?");
Daniel Dunbard52864b2010-02-14 10:02:57 +0000142 if (R1.getEnd() == R2.getBegin() ||
143 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000144 return RangeBefore;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000145 if (R2.getEnd() == R1.getBegin() ||
146 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000147 return RangeAfter;
148 return RangeOverlap;
149}
150
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000151/// \brief Translate a Clang source range into a CIndex source range.
152///
153/// Clang internally represents ranges where the end location points to the
154/// start of the token at the end. However, for external clients it is more
155/// useful to have a CXSourceRange be a proper half-open interval. This routine
156/// does the appropriate translation.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000157CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000158 const LangOptions &LangOpts,
159 SourceRange R) {
160 // FIXME: This is largely copy-paste from
161 // TextDiagnosticPrinter::HighlightRange. When it is clear that this is what
162 // we want the two routines should be refactored.
163
164 // We want the last character in this location, so we will adjust the
165 // instantiation location accordingly.
166
167 // If the location is from a macro instantiation, get the end of the
168 // instantiation range.
169 SourceLocation EndLoc = R.getEnd();
170 SourceLocation InstLoc = SM.getInstantiationLoc(EndLoc);
171 if (EndLoc.isMacroID())
172 InstLoc = SM.getInstantiationRange(EndLoc).second;
173
174 // Measure the length token we're pointing at, so we can adjust the physical
175 // location in the file to point at the last character.
176 //
177 // FIXME: This won't cope with trigraphs or escaped newlines well. For that,
178 // we actually need a preprocessor, which isn't currently available
179 // here. Eventually, we'll switch the pointer data of
180 // CXSourceLocation/CXSourceRange to a translation unit (CXXUnit), so that the
181 // preprocessor will be available here. At that point, we can use
182 // Preprocessor::getLocForEndOfToken().
183 if (InstLoc.isValid()) {
184 unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM, LangOpts);
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000185 EndLoc = EndLoc.getFileLocWithOffset(Length);
186 }
187
188 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
189 R.getBegin().getRawEncoding(),
190 EndLoc.getRawEncoding() };
191 return Result;
192}
Douglas Gregor1db19de2010-01-19 21:36:55 +0000193
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000194//===----------------------------------------------------------------------===//
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000195// Cursor visitor.
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000196//===----------------------------------------------------------------------===//
197
Steve Naroff89922f82009-08-31 00:59:03 +0000198namespace {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000199
Douglas Gregorb1373d02010-01-20 20:59:29 +0000200// Cursor visitor.
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000201class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
Douglas Gregora59e3902010-01-21 23:27:09 +0000202 public TypeLocVisitor<CursorVisitor, bool>,
203 public StmtVisitor<CursorVisitor, bool>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000204{
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000205 /// \brief The translation unit we are traversing.
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000206 ASTUnit *TU;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000207
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000208 /// \brief The parent cursor whose children we are traversing.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000209 CXCursor Parent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000210
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000211 /// \brief The declaration that serves at the parent of any statement or
212 /// expression nodes.
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000213 Decl *StmtParent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000214
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000215 /// \brief The visitor function.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000216 CXCursorVisitor Visitor;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000217
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000218 /// \brief The opaque client data, to be passed along to the visitor.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000219 CXClientData ClientData;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000220
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000221 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
222 // to the visitor. Declarations with a PCH level greater than this value will
223 // be suppressed.
224 unsigned MaxPCHLevel;
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000225
226 /// \brief When valid, a source range to which the cursor should restrict
227 /// its search.
228 SourceRange RegionOfInterest;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000229
Douglas Gregorb1373d02010-01-20 20:59:29 +0000230 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000231 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Douglas Gregora59e3902010-01-21 23:27:09 +0000232 using StmtVisitor<CursorVisitor, bool>::Visit;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000233
234 /// \brief Determine whether this particular source range comes before, comes
235 /// after, or overlaps the region of interest.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000236 ///
Daniel Dunbard52864b2010-02-14 10:02:57 +0000237 /// \param R a half-open source range retrieved from the abstract syntax tree.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000238 RangeComparisonResult CompareRegionOfInterest(SourceRange R);
239
Steve Naroff89922f82009-08-31 00:59:03 +0000240public:
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000241 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
242 unsigned MaxPCHLevel,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000243 SourceRange RegionOfInterest = SourceRange())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000244 : TU(TU), Visitor(Visitor), ClientData(ClientData),
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000245 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000246 {
247 Parent.kind = CXCursor_NoDeclFound;
248 Parent.data[0] = 0;
249 Parent.data[1] = 0;
250 Parent.data[2] = 0;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000251 StmtParent = 0;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000252 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000253
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000254 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000255 bool VisitChildren(CXCursor Parent);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000256
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000257 // Declaration visitors
Ted Kremenek09dfa372010-02-18 05:46:33 +0000258 bool VisitAttributes(Decl *D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000259 bool VisitDeclContext(DeclContext *DC);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000260 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
261 bool VisitTypedefDecl(TypedefDecl *D);
Ted Kremenek79758f62010-02-18 22:36:18 +0000262 bool VisitTagDecl(TagDecl *D);
263 bool VisitEnumConstantDecl(EnumConstantDecl *D);
264 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
265 bool VisitFunctionDecl(FunctionDecl *ND);
266 bool VisitFieldDecl(FieldDecl *D);
Ted Kremenek4540c9c2010-02-18 18:47:08 +0000267 bool VisitVarDecl(VarDecl *);
Ted Kremenek79758f62010-02-18 22:36:18 +0000268 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
269 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
270 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
271 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
272 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
273 bool VisitObjCImplDecl(ObjCImplDecl *D);
274 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
275 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
276 // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
277 // etc.
278 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
279 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
280 bool VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000281
282 // Type visitors
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000283 // FIXME: QualifiedTypeLoc doesn't provide any location information
284 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000285 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000286 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
287 bool VisitTagTypeLoc(TagTypeLoc TL);
288 // FIXME: TemplateTypeParmTypeLoc doesn't provide any location information
289 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
290 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
291 bool VisitPointerTypeLoc(PointerTypeLoc TL);
292 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
293 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
294 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
295 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
296 bool VisitFunctionTypeLoc(FunctionTypeLoc TL);
297 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000298 // FIXME: Implement for TemplateSpecializationTypeLoc
299 // FIXME: Implement visitors here when the unimplemented TypeLocs get
300 // implemented
301 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
302 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000303
Douglas Gregora59e3902010-01-21 23:27:09 +0000304 // Statement visitors
305 bool VisitStmt(Stmt *S);
306 bool VisitDeclStmt(DeclStmt *S);
Douglas Gregorf5bab412010-01-22 01:00:11 +0000307 // FIXME: LabelStmt label?
308 bool VisitIfStmt(IfStmt *S);
309 bool VisitSwitchStmt(SwitchStmt *S);
Douglas Gregor263b47b2010-01-25 16:12:32 +0000310 bool VisitWhileStmt(WhileStmt *S);
311 bool VisitForStmt(ForStmt *S);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000312
Douglas Gregor336fd812010-01-23 00:40:08 +0000313 // Expression visitors
314 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
315 bool VisitExplicitCastExpr(ExplicitCastExpr *E);
316 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Steve Naroff89922f82009-08-31 00:59:03 +0000317};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000318
Ted Kremenekab188932010-01-05 19:32:54 +0000319} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000320
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000321RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000322 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
323}
324
Douglas Gregorb1373d02010-01-20 20:59:29 +0000325/// \brief Visit the given cursor and, if requested by the visitor,
326/// its children.
327///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000328/// \param Cursor the cursor to visit.
329///
330/// \param CheckRegionOfInterest if true, then the caller already checked that
331/// this cursor is within the region of interest.
332///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000333/// \returns true if the visitation should be aborted, false if it
334/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000335bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000336 if (clang_isInvalid(Cursor.kind))
337 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000338
Douglas Gregorb1373d02010-01-20 20:59:29 +0000339 if (clang_isDeclaration(Cursor.kind)) {
340 Decl *D = getCursorDecl(Cursor);
341 assert(D && "Invalid declaration cursor");
342 if (D->getPCHLevel() > MaxPCHLevel)
343 return false;
344
345 if (D->isImplicit())
346 return false;
347 }
348
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000349 // If we have a range of interest, and this cursor doesn't intersect with it,
350 // we're done.
351 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Daniel Dunbarf408f322010-02-14 08:32:05 +0000352 SourceRange Range =
353 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
354 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000355 return false;
356 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000357
Douglas Gregorb1373d02010-01-20 20:59:29 +0000358 switch (Visitor(Cursor, Parent, ClientData)) {
359 case CXChildVisit_Break:
360 return true;
361
362 case CXChildVisit_Continue:
363 return false;
364
365 case CXChildVisit_Recurse:
366 return VisitChildren(Cursor);
367 }
368
Douglas Gregorfd643772010-01-25 16:45:46 +0000369 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000370}
371
372/// \brief Visit the children of the given cursor.
373///
374/// \returns true if the visitation should be aborted, false if it
375/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000376bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000377 if (clang_isReference(Cursor.kind)) {
378 // By definition, references have no children.
379 return false;
380 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000381
382 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000383 // done.
384 class SetParentRAII {
385 CXCursor &Parent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000386 Decl *&StmtParent;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000387 CXCursor OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000388
Douglas Gregorb1373d02010-01-20 20:59:29 +0000389 public:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000390 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000391 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000392 {
393 Parent = NewParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000394 if (clang_isDeclaration(Parent.kind))
395 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000396 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000397
Douglas Gregorb1373d02010-01-20 20:59:29 +0000398 ~SetParentRAII() {
399 Parent = OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000400 if (clang_isDeclaration(Parent.kind))
401 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000402 }
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000403 } SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000404
Douglas Gregorb1373d02010-01-20 20:59:29 +0000405 if (clang_isDeclaration(Cursor.kind)) {
406 Decl *D = getCursorDecl(Cursor);
407 assert(D && "Invalid declaration cursor");
Ted Kremenek539311e2010-02-18 18:47:01 +0000408 return VisitAttributes(D) || Visit(D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000409 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000410
Douglas Gregora59e3902010-01-21 23:27:09 +0000411 if (clang_isStatement(Cursor.kind))
412 return Visit(getCursorStmt(Cursor));
413 if (clang_isExpression(Cursor.kind))
414 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000415
Douglas Gregorb1373d02010-01-20 20:59:29 +0000416 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000417 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000418 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
419 RegionOfInterest.isInvalid()) {
Douglas Gregor7b691f332010-01-20 21:13:59 +0000420 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
421 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
422 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000423 if (Visit(MakeCXCursor(*it, CXXUnit), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000424 return true;
425 }
426 } else {
427 return VisitDeclContext(
Douglas Gregorb1373d02010-01-20 20:59:29 +0000428 CXXUnit->getASTContext().getTranslationUnitDecl());
Douglas Gregor7b691f332010-01-20 21:13:59 +0000429 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000430
Douglas Gregor7b691f332010-01-20 21:13:59 +0000431 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000432 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000433
Douglas Gregorb1373d02010-01-20 20:59:29 +0000434 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000435 return false;
436}
437
Douglas Gregorb1373d02010-01-20 20:59:29 +0000438bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000439 for (DeclContext::decl_iterator
Douglas Gregorb1373d02010-01-20 20:59:29 +0000440 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Ted Kremenek09dfa372010-02-18 05:46:33 +0000441
Daniel Dunbard52864b2010-02-14 10:02:57 +0000442 CXCursor Cursor = MakeCXCursor(*I, TU);
443
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000444 if (RegionOfInterest.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +0000445 SourceRange Range =
446 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
447 if (Range.isInvalid())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000448 continue;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000449
450 switch (CompareRegionOfInterest(Range)) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000451 case RangeBefore:
452 // This declaration comes before the region of interest; skip it.
453 continue;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000454
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000455 case RangeAfter:
456 // This declaration comes after the region of interest; we're done.
457 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000458
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000459 case RangeOverlap:
460 // This declaration overlaps the region of interest; visit it.
461 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000462 }
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000463 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000464
Daniel Dunbard52864b2010-02-14 10:02:57 +0000465 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000466 return true;
467 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000468
Douglas Gregorb1373d02010-01-20 20:59:29 +0000469 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000470}
471
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000472bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
473 llvm_unreachable("Translation units are visited directly by Visit()");
474 return false;
475}
476
477bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
478 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
479 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000480
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000481 return false;
482}
483
484bool CursorVisitor::VisitTagDecl(TagDecl *D) {
485 return VisitDeclContext(D);
486}
487
488bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
489 if (Expr *Init = D->getInitExpr())
490 return Visit(MakeCXCursor(Init, StmtParent, TU));
491 return false;
492}
493
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000494bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
495 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
496 if (Visit(TSInfo->getTypeLoc()))
497 return true;
498
499 return false;
500}
501
Douglas Gregorb1373d02010-01-20 20:59:29 +0000502bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000503 if (VisitDeclaratorDecl(ND))
504 return true;
505
Douglas Gregora59e3902010-01-21 23:27:09 +0000506 if (ND->isThisDeclarationADefinition() &&
507 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
508 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000509
Douglas Gregorb1373d02010-01-20 20:59:29 +0000510 return false;
511}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000512
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000513bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
514 if (VisitDeclaratorDecl(D))
515 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000516
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000517 if (Expr *BitWidth = D->getBitWidth())
518 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000519
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000520 return false;
521}
522
523bool CursorVisitor::VisitVarDecl(VarDecl *D) {
524 if (VisitDeclaratorDecl(D))
525 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000526
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000527 if (Expr *Init = D->getInit())
528 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000529
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000530 return false;
531}
532
533bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
534 // FIXME: We really need a TypeLoc covering Objective-C method declarations.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000535 // At the moment, we don't have information about locations in the return
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000536 // type.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000537 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000538 PEnd = ND->param_end();
539 P != PEnd; ++P) {
540 if (Visit(MakeCXCursor(*P, TU)))
541 return true;
542 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000543
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000544 if (ND->isThisDeclarationADefinition() &&
545 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
546 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000547
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000548 return false;
549}
550
Douglas Gregora59e3902010-01-21 23:27:09 +0000551bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
552 return VisitDeclContext(D);
553}
554
Douglas Gregorb1373d02010-01-20 20:59:29 +0000555bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000556 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
557 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000558 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000559
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000560 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
561 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
562 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000563 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000564 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000565
Douglas Gregora59e3902010-01-21 23:27:09 +0000566 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000567}
568
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000569bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
570 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
571 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
572 E = PID->protocol_end(); I != E; ++I, ++PL)
573 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
574 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000575
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000576 return VisitObjCContainerDecl(PID);
577}
578
Douglas Gregorb1373d02010-01-20 20:59:29 +0000579bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000580 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000581 if (D->getSuperClass() &&
582 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000583 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000584 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000585 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000586
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000587 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
588 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
589 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000590 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000591 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000592
Douglas Gregora59e3902010-01-21 23:27:09 +0000593 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000594}
595
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000596bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
597 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000598}
599
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000600bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000601 if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000602 D->getLocation(), TU)))
603 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000604
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000605 return VisitObjCImplDecl(D);
606}
607
608bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
609#if 0
610 // Issue callbacks for super class.
611 // FIXME: No source location information!
612 if (D->getSuperClass() &&
613 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000614 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000615 TU)))
616 return true;
617#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000618
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000619 return VisitObjCImplDecl(D);
620}
621
622bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
623 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
624 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
625 E = D->protocol_end();
626 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000627 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000628 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000629
630 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000631}
632
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000633bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
634 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
635 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
636 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000637
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000638 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000639}
640
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000641bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
642 ASTContext &Context = TU->getASTContext();
643
644 // Some builtin types (such as Objective-C's "id", "sel", and
645 // "Class") have associated declarations. Create cursors for those.
646 QualType VisitType;
647 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000648 case BuiltinType::Void:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000649 case BuiltinType::Bool:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000650 case BuiltinType::Char_U:
651 case BuiltinType::UChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000652 case BuiltinType::Char16:
653 case BuiltinType::Char32:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000654 case BuiltinType::UShort:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000655 case BuiltinType::UInt:
656 case BuiltinType::ULong:
657 case BuiltinType::ULongLong:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000658 case BuiltinType::UInt128:
659 case BuiltinType::Char_S:
660 case BuiltinType::SChar:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000661 case BuiltinType::WChar:
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000662 case BuiltinType::Short:
663 case BuiltinType::Int:
664 case BuiltinType::Long:
665 case BuiltinType::LongLong:
666 case BuiltinType::Int128:
667 case BuiltinType::Float:
668 case BuiltinType::Double:
669 case BuiltinType::LongDouble:
670 case BuiltinType::NullPtr:
671 case BuiltinType::Overload:
672 case BuiltinType::Dependent:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000673 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000674
675 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000676 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000677
Ted Kremenekc4174cc2010-02-18 18:52:18 +0000678 case BuiltinType::ObjCId:
679 VisitType = Context.getObjCIdType();
680 break;
Ted Kremenek6b3b5142010-02-18 22:32:43 +0000681
682 case BuiltinType::ObjCClass:
683 VisitType = Context.getObjCClassType();
684 break;
685
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000686 case BuiltinType::ObjCSel:
687 VisitType = Context.getObjCSelType();
688 break;
689 }
690
691 if (!VisitType.isNull()) {
692 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000693 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000694 TU));
695 }
696
697 return false;
698}
699
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000700bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
701 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
702}
703
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000704bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
705 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
706}
707
708bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
709 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
710}
711
712bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
713 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
714 return true;
715
716 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
717 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
718 TU)))
719 return true;
720 }
721
722 return false;
723}
724
725bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
726 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
727 return true;
728
729 if (TL.hasProtocolsAsWritten()) {
730 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000731 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000732 TL.getProtocolLoc(I),
733 TU)))
734 return true;
735 }
736 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000737
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000738 return false;
739}
740
741bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
742 return Visit(TL.getPointeeLoc());
743}
744
745bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
746 return Visit(TL.getPointeeLoc());
747}
748
749bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
750 return Visit(TL.getPointeeLoc());
751}
752
753bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000754 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000755}
756
757bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000758 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000759}
760
761bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
762 if (Visit(TL.getResultLoc()))
763 return true;
764
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000765 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
766 if (Visit(MakeCXCursor(TL.getArg(I), TU)))
767 return true;
768
769 return false;
770}
771
772bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
773 if (Visit(TL.getElementLoc()))
774 return true;
775
776 if (Expr *Size = TL.getSizeExpr())
777 return Visit(MakeCXCursor(Size, StmtParent, TU));
778
779 return false;
780}
781
Douglas Gregor2332c112010-01-21 20:48:56 +0000782bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
783 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
784}
785
786bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
787 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
788 return Visit(TSInfo->getTypeLoc());
789
790 return false;
791}
792
Douglas Gregora59e3902010-01-21 23:27:09 +0000793bool CursorVisitor::VisitStmt(Stmt *S) {
794 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
795 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000796 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000797 return true;
798 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000799
Douglas Gregora59e3902010-01-21 23:27:09 +0000800 return false;
801}
802
803bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
804 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
805 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000806 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000807 return true;
808 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000809
Douglas Gregora59e3902010-01-21 23:27:09 +0000810 return false;
811}
812
Douglas Gregorf5bab412010-01-22 01:00:11 +0000813bool CursorVisitor::VisitIfStmt(IfStmt *S) {
814 if (VarDecl *Var = S->getConditionVariable()) {
815 if (Visit(MakeCXCursor(Var, TU)))
816 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000817 }
818
Douglas Gregor263b47b2010-01-25 16:12:32 +0000819 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
820 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000821 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
822 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000823 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
824 return true;
825
826 return false;
827}
828
829bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
830 if (VarDecl *Var = S->getConditionVariable()) {
831 if (Visit(MakeCXCursor(Var, TU)))
832 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000833 }
834
Douglas Gregor263b47b2010-01-25 16:12:32 +0000835 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
836 return true;
837 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
838 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000839
Douglas Gregor263b47b2010-01-25 16:12:32 +0000840 return false;
841}
842
843bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
844 if (VarDecl *Var = S->getConditionVariable()) {
845 if (Visit(MakeCXCursor(Var, TU)))
846 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000847 }
848
Douglas Gregor263b47b2010-01-25 16:12:32 +0000849 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
850 return true;
851 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000852 return true;
853
Douglas Gregor263b47b2010-01-25 16:12:32 +0000854 return false;
855}
856
857bool CursorVisitor::VisitForStmt(ForStmt *S) {
858 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
859 return true;
860 if (VarDecl *Var = S->getConditionVariable()) {
861 if (Visit(MakeCXCursor(Var, TU)))
862 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000863 }
864
Douglas Gregor263b47b2010-01-25 16:12:32 +0000865 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
866 return true;
867 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
868 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000869 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
870 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000871
Douglas Gregorf5bab412010-01-22 01:00:11 +0000872 return false;
873}
874
Douglas Gregor336fd812010-01-23 00:40:08 +0000875bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
876 if (E->isArgumentType()) {
877 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
878 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000879
Douglas Gregor336fd812010-01-23 00:40:08 +0000880 return false;
881 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000882
Douglas Gregor336fd812010-01-23 00:40:08 +0000883 return VisitExpr(E);
884}
885
886bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
887 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
888 if (Visit(TSInfo->getTypeLoc()))
889 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000890
Douglas Gregor336fd812010-01-23 00:40:08 +0000891 return VisitCastExpr(E);
892}
893
894bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
895 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
896 if (Visit(TSInfo->getTypeLoc()))
897 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000898
Douglas Gregor336fd812010-01-23 00:40:08 +0000899 return VisitExpr(E);
900}
901
Ted Kremenek09dfa372010-02-18 05:46:33 +0000902bool CursorVisitor::VisitAttributes(Decl *D) {
903 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
904 if (Visit(MakeCXCursor(A, D, TU)))
905 return true;
906
907 return false;
908}
909
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000910extern "C" {
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000911CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
912 int displayDiagnostics) {
Douglas Gregora030b7c2010-01-22 20:35:53 +0000913 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000914 if (excludeDeclarationsFromPCH)
915 CIdxr->setOnlyLocalDecls();
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000916 if (displayDiagnostics)
917 CIdxr->setDisplayDiagnostics();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000918 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000919}
920
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000921void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000922 if (CIdx)
923 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000924}
925
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000926void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000927 if (CIdx) {
928 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
929 CXXIdx->setUseExternalASTGeneration(value);
930 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000931}
932
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000933CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregora88084b2010-02-18 18:08:43 +0000934 const char *ast_filename) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000935 if (!CIdx)
936 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000937
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000938 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000939
Douglas Gregor5352ac02010-01-28 00:27:43 +0000940 // Configure the diagnostics.
941 DiagnosticOptions DiagOpts;
942 llvm::OwningPtr<Diagnostic> Diags;
943 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Douglas Gregor5352ac02010-01-28 00:27:43 +0000944 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Douglas Gregora88084b2010-02-18 18:08:43 +0000945 CXXIdx->getOnlyLocalDecls(),
946 0, 0, true);
Steve Naroff600866c2009-08-27 19:51:58 +0000947}
948
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000949CXTranslationUnit
950clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
951 const char *source_filename,
952 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000953 const char **command_line_args,
954 unsigned num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000955 struct CXUnsavedFile *unsaved_files) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000956 if (!CIdx)
957 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000958
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000959 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
960
Douglas Gregor5352ac02010-01-28 00:27:43 +0000961 // Configure the diagnostics.
962 DiagnosticOptions DiagOpts;
963 llvm::OwningPtr<Diagnostic> Diags;
964 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000965
Douglas Gregor4db64a42010-01-23 00:14:00 +0000966 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
967 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000968 const llvm::MemoryBuffer *Buffer
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000969 = llvm::MemoryBuffer::getMemBufferCopy(unsaved_files[I].Contents,
Douglas Gregor313e26c2010-02-18 23:35:40 +0000970 unsaved_files[I].Contents + unsaved_files[I].Length,
971 unsaved_files[I].Filename);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000972 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
973 Buffer));
974 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000975
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000976 if (!CXXIdx->getUseExternalASTGeneration()) {
977 llvm::SmallVector<const char *, 16> Args;
978
979 // The 'source_filename' argument is optional. If the caller does not
980 // specify it then it is assumed that the source file is specified
981 // in the actual argument list.
982 if (source_filename)
983 Args.push_back(source_filename);
984 Args.insert(Args.end(), command_line_args,
985 command_line_args + num_command_line_args);
986
Douglas Gregor5352ac02010-01-28 00:27:43 +0000987 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000988
Ted Kremenek29b72842010-01-07 22:49:05 +0000989#ifdef USE_CRASHTRACER
990 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000991#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000992
Daniel Dunbar94220972009-12-05 02:17:18 +0000993 llvm::OwningPtr<ASTUnit> Unit(
994 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000995 *Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000996 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000997 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +0000998 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +0000999 RemappedFiles.size(),
1000 /*CaptureDiagnostics=*/true));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001001
Daniel Dunbar94220972009-12-05 02:17:18 +00001002 // FIXME: Until we have broader testing, just drop the entire AST if we
1003 // encountered an error.
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001004 if (NumErrors != Diags->getNumErrors()) {
1005 if (CXXIdx->getDisplayDiagnostics()) {
1006 for (ASTUnit::diag_iterator D = Unit->diag_begin(),
1007 DEnd = Unit->diag_end();
1008 D != DEnd; ++D) {
1009 CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions());
Douglas Gregor274f1902010-02-22 23:17:23 +00001010 CXString Msg = clang_formatDiagnostic(&Diag,
1011 clang_defaultDiagnosticDisplayOptions());
1012 fprintf(stderr, "%s\n", clang_getCString(Msg));
1013 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001014 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001015#ifdef LLVM_ON_WIN32
1016 // On Windows, force a flush, since there may be multiple copies of
1017 // stderr and stdout in the file system, all with different buffers
1018 // but writing to the same device.
1019 fflush(stderr);
1020#endif
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001021 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001022 return 0;
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001023 }
Daniel Dunbar94220972009-12-05 02:17:18 +00001024
1025 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001026 }
1027
Ted Kremenek139ba862009-10-22 00:03:57 +00001028 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001029 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001030
Ted Kremenek139ba862009-10-22 00:03:57 +00001031 // First add the complete path to the 'clang' executable.
1032 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001033 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001034
Ted Kremenek139ba862009-10-22 00:03:57 +00001035 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001036 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001037
Ted Kremenek139ba862009-10-22 00:03:57 +00001038 // The 'source_filename' argument is optional. If the caller does not
1039 // specify it then it is assumed that the source file is specified
1040 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001041 if (source_filename)
1042 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001043
Steve Naroff37b5ac22009-10-15 20:50:09 +00001044 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001045 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001046 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001047 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001048
Douglas Gregor4db64a42010-01-23 00:14:00 +00001049 // Remap any unsaved files to temporary files.
1050 std::vector<llvm::sys::Path> TemporaryFiles;
1051 std::vector<std::string> RemapArgs;
1052 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1053 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001054
Douglas Gregor4db64a42010-01-23 00:14:00 +00001055 // The pointers into the elements of RemapArgs are stable because we
1056 // won't be adding anything to RemapArgs after this point.
1057 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1058 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001059
Ted Kremenek139ba862009-10-22 00:03:57 +00001060 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1061 for (int i = 0; i < num_command_line_args; ++i)
1062 if (const char *arg = command_line_args[i]) {
1063 if (strcmp(arg, "-o") == 0) {
1064 ++i; // Also skip the matching argument.
1065 continue;
1066 }
1067 if (strcmp(arg, "-emit-ast") == 0 ||
1068 strcmp(arg, "-c") == 0 ||
1069 strcmp(arg, "-fsyntax-only") == 0) {
1070 continue;
1071 }
1072
1073 // Keep the argument.
1074 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001075 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001076
Douglas Gregord93256e2010-01-28 06:00:51 +00001077 // Generate a temporary name for the diagnostics file.
1078 char tmpFileResults[L_tmpnam];
1079 char *tmpResultsFileName = tmpnam(tmpFileResults);
1080 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1081 TemporaryFiles.push_back(DiagnosticsFile);
1082 argv.push_back("-fdiagnostics-binary");
1083
Ted Kremenek139ba862009-10-22 00:03:57 +00001084 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001085 argv.push_back(NULL);
1086
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001087 // Invoke 'clang'.
1088 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1089 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001090 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001091 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1092 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001093 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001094 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001095 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001096
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001097 if (!ErrMsg.empty()) {
1098 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001099 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001100 I != E; ++I) {
1101 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001102 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001103 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001104 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001105
Daniel Dunbar32141c82010-02-23 20:23:45 +00001106 Diags->Report(diag::err_fe_invoking) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001107 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001108
Douglas Gregor5352ac02010-01-28 00:27:43 +00001109 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001110 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001111 RemappedFiles.data(),
Douglas Gregora88084b2010-02-18 18:08:43 +00001112 RemappedFiles.size(),
1113 /*CaptureDiagnostics=*/true);
Douglas Gregora88084b2010-02-18 18:08:43 +00001114 if (ATU) {
1115 LoadSerializedDiagnostics(DiagnosticsFile,
1116 num_unsaved_files, unsaved_files,
1117 ATU->getFileManager(),
1118 ATU->getSourceManager(),
1119 ATU->getDiagnostics());
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001120 } else if (CXXIdx->getDisplayDiagnostics()) {
1121 // We failed to load the ASTUnit, but we can still deserialize the
1122 // diagnostics and emit them.
1123 FileManager FileMgr;
1124 SourceManager SourceMgr;
1125 // FIXME: Faked LangOpts!
1126 LangOptions LangOpts;
1127 llvm::SmallVector<StoredDiagnostic, 4> Diags;
1128 LoadSerializedDiagnostics(DiagnosticsFile,
1129 num_unsaved_files, unsaved_files,
1130 FileMgr, SourceMgr, Diags);
1131 for (llvm::SmallVector<StoredDiagnostic, 4>::iterator D = Diags.begin(),
1132 DEnd = Diags.end();
1133 D != DEnd; ++D) {
1134 CXStoredDiagnostic Diag(*D, LangOpts);
Douglas Gregor274f1902010-02-22 23:17:23 +00001135 CXString Msg = clang_formatDiagnostic(&Diag,
1136 clang_defaultDiagnosticDisplayOptions());
1137 fprintf(stderr, "%s\n", clang_getCString(Msg));
1138 clang_disposeString(Msg);
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001139 }
Douglas Gregor274f1902010-02-22 23:17:23 +00001140
1141#ifdef LLVM_ON_WIN32
1142 // On Windows, force a flush, since there may be multiple copies of
1143 // stderr and stdout in the file system, all with different buffers
1144 // but writing to the same device.
1145 fflush(stderr);
1146#endif
Douglas Gregora88084b2010-02-18 18:08:43 +00001147 }
Douglas Gregord93256e2010-01-28 06:00:51 +00001148
Douglas Gregor313e26c2010-02-18 23:35:40 +00001149 if (ATU) {
1150 // Make the translation unit responsible for destroying all temporary files.
1151 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1152 ATU->addTemporaryFile(TemporaryFiles[i]);
1153 ATU->addTemporaryFile(llvm::sys::Path(ATU->getPCHFileName()));
1154 } else {
1155 // Destroy all of the temporary files now; they can't be referenced any
1156 // longer.
1157 llvm::sys::Path(astTmpFile).eraseFromDisk();
1158 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1159 TemporaryFiles[i].eraseFromDisk();
1160 }
1161
Steve Naroffe19944c2009-10-15 22:23:48 +00001162 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001163}
1164
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001165void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001166 if (CTUnit)
1167 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001168}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001169
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001170CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001171 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001172 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001173
Steve Naroff77accc12009-09-03 18:19:54 +00001174 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001175 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001176}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001177
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001178CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001179 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001180 return Result;
1181}
1182
Ted Kremenekfb480492010-01-13 21:46:36 +00001183} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001184
Ted Kremenekfb480492010-01-13 21:46:36 +00001185//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001186// CXSourceLocation and CXSourceRange Operations.
1187//===----------------------------------------------------------------------===//
1188
Douglas Gregorb9790342010-01-22 21:44:22 +00001189extern "C" {
1190CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001191 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001192 return Result;
1193}
1194
1195unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001196 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1197 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1198 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001199}
1200
1201CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1202 CXFile file,
1203 unsigned line,
1204 unsigned column) {
1205 if (!tu)
1206 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001207
Douglas Gregorb9790342010-01-22 21:44:22 +00001208 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1209 SourceLocation SLoc
1210 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001211 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001212 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001213
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001214 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001215}
1216
Douglas Gregor5352ac02010-01-28 00:27:43 +00001217CXSourceRange clang_getNullRange() {
1218 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1219 return Result;
1220}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001221
Douglas Gregor5352ac02010-01-28 00:27:43 +00001222CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1223 if (begin.ptr_data[0] != end.ptr_data[0] ||
1224 begin.ptr_data[1] != end.ptr_data[1])
1225 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001226
1227 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001228 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001229 return Result;
1230}
1231
Douglas Gregor46766dc2010-01-26 19:19:08 +00001232void clang_getInstantiationLocation(CXSourceLocation location,
1233 CXFile *file,
1234 unsigned *line,
1235 unsigned *column,
1236 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001237 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1238
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001239 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001240 if (file)
1241 *file = 0;
1242 if (line)
1243 *line = 0;
1244 if (column)
1245 *column = 0;
1246 if (offset)
1247 *offset = 0;
1248 return;
1249 }
1250
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001251 const SourceManager &SM =
1252 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001253 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001254
1255 if (file)
1256 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1257 if (line)
1258 *line = SM.getInstantiationLineNumber(InstLoc);
1259 if (column)
1260 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001261 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001262 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001263}
1264
Douglas Gregor1db19de2010-01-19 21:36:55 +00001265CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001266 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001267 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001268 return Result;
1269}
1270
1271CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001272 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001273 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001274 return Result;
1275}
1276
Douglas Gregorb9790342010-01-22 21:44:22 +00001277} // end: extern "C"
1278
Douglas Gregor1db19de2010-01-19 21:36:55 +00001279//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001280// CXFile Operations.
1281//===----------------------------------------------------------------------===//
1282
1283extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001284CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001285 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001286 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001287
Steve Naroff88145032009-10-27 14:35:18 +00001288 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001289 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001290}
1291
1292time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001293 if (!SFile)
1294 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001295
Steve Naroff88145032009-10-27 14:35:18 +00001296 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1297 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001298}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001299
Douglas Gregorb9790342010-01-22 21:44:22 +00001300CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1301 if (!tu)
1302 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001303
Douglas Gregorb9790342010-01-22 21:44:22 +00001304 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001305
Douglas Gregorb9790342010-01-22 21:44:22 +00001306 FileManager &FMgr = CXXUnit->getFileManager();
1307 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1308 return const_cast<FileEntry *>(File);
1309}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001310
Ted Kremenekfb480492010-01-13 21:46:36 +00001311} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001312
Ted Kremenekfb480492010-01-13 21:46:36 +00001313//===----------------------------------------------------------------------===//
1314// CXCursor Operations.
1315//===----------------------------------------------------------------------===//
1316
Ted Kremenekfb480492010-01-13 21:46:36 +00001317static Decl *getDeclFromExpr(Stmt *E) {
1318 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1319 return RefExpr->getDecl();
1320 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1321 return ME->getMemberDecl();
1322 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1323 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001324
Ted Kremenekfb480492010-01-13 21:46:36 +00001325 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1326 return getDeclFromExpr(CE->getCallee());
1327 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1328 return getDeclFromExpr(CE->getSubExpr());
1329 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1330 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001331
Ted Kremenekfb480492010-01-13 21:46:36 +00001332 return 0;
1333}
1334
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001335static SourceLocation getLocationFromExpr(Expr *E) {
1336 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1337 return /*FIXME:*/Msg->getLeftLoc();
1338 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1339 return DRE->getLocation();
1340 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1341 return Member->getMemberLoc();
1342 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1343 return Ivar->getLocation();
1344 return E->getLocStart();
1345}
1346
Ted Kremenekfb480492010-01-13 21:46:36 +00001347extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001348
1349unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001350 CXCursorVisitor visitor,
1351 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001352 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001353
1354 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001355
Douglas Gregorb1373d02010-01-20 20:59:29 +00001356 // Set the PCHLevel to filter out unwanted decls if requested.
1357 if (CXXUnit->getOnlyLocalDecls()) {
1358 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001359
Douglas Gregorb1373d02010-01-20 20:59:29 +00001360 // If the main input was an AST, bump the level.
1361 if (CXXUnit->isMainFileAST())
1362 ++PCHLevel;
1363 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001364
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001365 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001366 return CursorVis.VisitChildren(parent);
1367}
1368
Douglas Gregor78205d42010-01-20 21:45:58 +00001369static CXString getDeclSpelling(Decl *D) {
1370 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1371 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001372 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001373
Douglas Gregor78205d42010-01-20 21:45:58 +00001374 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001375 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001376
Douglas Gregor78205d42010-01-20 21:45:58 +00001377 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1378 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1379 // and returns different names. NamedDecl returns the class name and
1380 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001381 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001382
Douglas Gregor78205d42010-01-20 21:45:58 +00001383 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001384 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001385
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001386 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001387}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001388
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001389CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001390 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001391 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001392
Steve Narofff334b4e2009-09-02 18:26:48 +00001393 if (clang_isReference(C.kind)) {
1394 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001395 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001396 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001397 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001398 }
1399 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001400 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001401 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001402 }
1403 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001404 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001405 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001406 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001407 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001408 case CXCursor_TypeRef: {
1409 TypeDecl *Type = getCursorTypeRef(C).first;
1410 assert(Type && "Missing type decl");
1411
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001412 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1413 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001414 }
1415
Daniel Dunbaracca7252009-11-30 20:42:49 +00001416 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001417 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001418 }
1419 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001420
1421 if (clang_isExpression(C.kind)) {
1422 Decl *D = getDeclFromExpr(getCursorExpr(C));
1423 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001424 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001425 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001426 }
1427
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001428 if (clang_isDeclaration(C.kind))
1429 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001430
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001431 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001432}
1433
Ted Kremeneke68fff62010-02-17 00:41:32 +00001434CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001435 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001436 case CXCursor_FunctionDecl:
1437 return createCXString("FunctionDecl");
1438 case CXCursor_TypedefDecl:
1439 return createCXString("TypedefDecl");
1440 case CXCursor_EnumDecl:
1441 return createCXString("EnumDecl");
1442 case CXCursor_EnumConstantDecl:
1443 return createCXString("EnumConstantDecl");
1444 case CXCursor_StructDecl:
1445 return createCXString("StructDecl");
1446 case CXCursor_UnionDecl:
1447 return createCXString("UnionDecl");
1448 case CXCursor_ClassDecl:
1449 return createCXString("ClassDecl");
1450 case CXCursor_FieldDecl:
1451 return createCXString("FieldDecl");
1452 case CXCursor_VarDecl:
1453 return createCXString("VarDecl");
1454 case CXCursor_ParmDecl:
1455 return createCXString("ParmDecl");
1456 case CXCursor_ObjCInterfaceDecl:
1457 return createCXString("ObjCInterfaceDecl");
1458 case CXCursor_ObjCCategoryDecl:
1459 return createCXString("ObjCCategoryDecl");
1460 case CXCursor_ObjCProtocolDecl:
1461 return createCXString("ObjCProtocolDecl");
1462 case CXCursor_ObjCPropertyDecl:
1463 return createCXString("ObjCPropertyDecl");
1464 case CXCursor_ObjCIvarDecl:
1465 return createCXString("ObjCIvarDecl");
1466 case CXCursor_ObjCInstanceMethodDecl:
1467 return createCXString("ObjCInstanceMethodDecl");
1468 case CXCursor_ObjCClassMethodDecl:
1469 return createCXString("ObjCClassMethodDecl");
1470 case CXCursor_ObjCImplementationDecl:
1471 return createCXString("ObjCImplementationDecl");
1472 case CXCursor_ObjCCategoryImplDecl:
1473 return createCXString("ObjCCategoryImplDecl");
1474 case CXCursor_UnexposedDecl:
1475 return createCXString("UnexposedDecl");
1476 case CXCursor_ObjCSuperClassRef:
1477 return createCXString("ObjCSuperClassRef");
1478 case CXCursor_ObjCProtocolRef:
1479 return createCXString("ObjCProtocolRef");
1480 case CXCursor_ObjCClassRef:
1481 return createCXString("ObjCClassRef");
1482 case CXCursor_TypeRef:
1483 return createCXString("TypeRef");
1484 case CXCursor_UnexposedExpr:
1485 return createCXString("UnexposedExpr");
1486 case CXCursor_DeclRefExpr:
1487 return createCXString("DeclRefExpr");
1488 case CXCursor_MemberRefExpr:
1489 return createCXString("MemberRefExpr");
1490 case CXCursor_CallExpr:
1491 return createCXString("CallExpr");
1492 case CXCursor_ObjCMessageExpr:
1493 return createCXString("ObjCMessageExpr");
1494 case CXCursor_UnexposedStmt:
1495 return createCXString("UnexposedStmt");
1496 case CXCursor_InvalidFile:
1497 return createCXString("InvalidFile");
1498 case CXCursor_NoDeclFound:
1499 return createCXString("NoDeclFound");
1500 case CXCursor_NotImplemented:
1501 return createCXString("NotImplemented");
1502 case CXCursor_TranslationUnit:
1503 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001504 case CXCursor_UnexposedAttr:
1505 return createCXString("UnexposedAttr");
1506 case CXCursor_IBActionAttr:
1507 return createCXString("attribute(ibaction)");
1508 case CXCursor_IBOutletAttr:
1509 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001510 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001511
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001512 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001513 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001514}
Steve Naroff89922f82009-08-31 00:59:03 +00001515
Ted Kremeneke68fff62010-02-17 00:41:32 +00001516enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1517 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001518 CXClientData client_data) {
1519 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1520 *BestCursor = cursor;
1521 return CXChildVisit_Recurse;
1522}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001523
Douglas Gregorb9790342010-01-22 21:44:22 +00001524CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1525 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001526 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001527
Douglas Gregorb9790342010-01-22 21:44:22 +00001528 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1529
Ted Kremeneka297de22010-01-25 22:34:44 +00001530 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001531 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1532 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001533 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001534
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001535 // FIXME: Would be great to have a "hint" cursor, then walk from that
1536 // hint cursor upward until we find a cursor whose source range encloses
1537 // the region of interest, rather than starting from the translation unit.
1538 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001539 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001540 Decl::MaxPCHLevel, RegionOfInterest);
1541 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001542 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001543 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001544}
1545
Ted Kremenek73885552009-11-17 19:28:59 +00001546CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001547 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001548}
1549
1550unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001551 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001552}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001553
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001554unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001555 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1556}
1557
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001558unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001559 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1560}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001561
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001562unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001563 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1564}
1565
Douglas Gregor97b98722010-01-19 23:20:36 +00001566unsigned clang_isExpression(enum CXCursorKind K) {
1567 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1568}
1569
1570unsigned clang_isStatement(enum CXCursorKind K) {
1571 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1572}
1573
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001574unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1575 return K == CXCursor_TranslationUnit;
1576}
1577
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001578CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001579 return C.kind;
1580}
1581
Douglas Gregor98258af2010-01-18 22:46:11 +00001582CXSourceLocation clang_getCursorLocation(CXCursor C) {
1583 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001584 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001585 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001586 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1587 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001588 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001589 }
1590
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001591 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001592 std::pair<ObjCProtocolDecl *, SourceLocation> P
1593 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001594 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001595 }
1596
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001597 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001598 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1599 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001600 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001601 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001602
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001603 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001604 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001605 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001606 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001607
Douglas Gregorf46034a2010-01-18 23:41:10 +00001608 default:
1609 // FIXME: Need a way to enumerate all non-reference cases.
1610 llvm_unreachable("Missed a reference kind");
1611 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001612 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001613
1614 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001615 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001616 getLocationFromExpr(getCursorExpr(C)));
1617
Douglas Gregor5352ac02010-01-28 00:27:43 +00001618 if (!getCursorDecl(C))
1619 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001620
Douglas Gregorf46034a2010-01-18 23:41:10 +00001621 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001622 SourceLocation Loc = D->getLocation();
1623 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1624 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001625 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001626}
Douglas Gregora7bde202010-01-19 00:34:46 +00001627
1628CXSourceRange clang_getCursorExtent(CXCursor C) {
1629 if (clang_isReference(C.kind)) {
1630 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001631 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001632 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1633 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001634 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001635 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001636
1637 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001638 std::pair<ObjCProtocolDecl *, SourceLocation> P
1639 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001640 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001641 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001642
1643 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001644 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1645 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001646
Ted Kremeneka297de22010-01-25 22:34:44 +00001647 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001648 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001649
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001650 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001651 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001652 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001653 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001654
Douglas Gregora7bde202010-01-19 00:34:46 +00001655 default:
1656 // FIXME: Need a way to enumerate all non-reference cases.
1657 llvm_unreachable("Missed a reference kind");
1658 }
1659 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001660
1661 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001662 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001663 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001664
1665 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001666 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001667 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001668
Douglas Gregor5352ac02010-01-28 00:27:43 +00001669 if (!getCursorDecl(C))
1670 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001671
Douglas Gregora7bde202010-01-19 00:34:46 +00001672 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001673 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001674}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001675
1676CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001677 if (clang_isInvalid(C.kind))
1678 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001679
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001680 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001681 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001682 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001683
Douglas Gregor97b98722010-01-19 23:20:36 +00001684 if (clang_isExpression(C.kind)) {
1685 Decl *D = getDeclFromExpr(getCursorExpr(C));
1686 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001687 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001688 return clang_getNullCursor();
1689 }
1690
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001691 if (!clang_isReference(C.kind))
1692 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001693
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001694 switch (C.kind) {
1695 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001696 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001697
1698 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001699 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001700
1701 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001702 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001703
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001704 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001705 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001706
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001707 default:
1708 // We would prefer to enumerate all non-reference cursor kinds here.
1709 llvm_unreachable("Unhandled reference cursor kind");
1710 break;
1711 }
1712 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001713
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001714 return clang_getNullCursor();
1715}
1716
Douglas Gregorb6998662010-01-19 19:34:47 +00001717CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001718 if (clang_isInvalid(C.kind))
1719 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001720
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001721 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001722
Douglas Gregorb6998662010-01-19 19:34:47 +00001723 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001724 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001725 C = clang_getCursorReferenced(C);
1726 WasReference = true;
1727 }
1728
1729 if (!clang_isDeclaration(C.kind))
1730 return clang_getNullCursor();
1731
1732 Decl *D = getCursorDecl(C);
1733 if (!D)
1734 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001735
Douglas Gregorb6998662010-01-19 19:34:47 +00001736 switch (D->getKind()) {
1737 // Declaration kinds that don't really separate the notions of
1738 // declaration and definition.
1739 case Decl::Namespace:
1740 case Decl::Typedef:
1741 case Decl::TemplateTypeParm:
1742 case Decl::EnumConstant:
1743 case Decl::Field:
1744 case Decl::ObjCIvar:
1745 case Decl::ObjCAtDefsField:
1746 case Decl::ImplicitParam:
1747 case Decl::ParmVar:
1748 case Decl::NonTypeTemplateParm:
1749 case Decl::TemplateTemplateParm:
1750 case Decl::ObjCCategoryImpl:
1751 case Decl::ObjCImplementation:
1752 case Decl::LinkageSpec:
1753 case Decl::ObjCPropertyImpl:
1754 case Decl::FileScopeAsm:
1755 case Decl::StaticAssert:
1756 case Decl::Block:
1757 return C;
1758
1759 // Declaration kinds that don't make any sense here, but are
1760 // nonetheless harmless.
1761 case Decl::TranslationUnit:
1762 case Decl::Template:
1763 case Decl::ObjCContainer:
1764 break;
1765
1766 // Declaration kinds for which the definition is not resolvable.
1767 case Decl::UnresolvedUsingTypename:
1768 case Decl::UnresolvedUsingValue:
1769 break;
1770
1771 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001772 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1773 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001774
1775 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001776 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001777
1778 case Decl::Enum:
1779 case Decl::Record:
1780 case Decl::CXXRecord:
1781 case Decl::ClassTemplateSpecialization:
1782 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001783 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001784 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001785 return clang_getNullCursor();
1786
1787 case Decl::Function:
1788 case Decl::CXXMethod:
1789 case Decl::CXXConstructor:
1790 case Decl::CXXDestructor:
1791 case Decl::CXXConversion: {
1792 const FunctionDecl *Def = 0;
1793 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001794 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001795 return clang_getNullCursor();
1796 }
1797
1798 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001799 // Ask the variable if it has a definition.
1800 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1801 return MakeCXCursor(Def, CXXUnit);
1802 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001803 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001804
Douglas Gregorb6998662010-01-19 19:34:47 +00001805 case Decl::FunctionTemplate: {
1806 const FunctionDecl *Def = 0;
1807 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001808 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001809 return clang_getNullCursor();
1810 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001811
Douglas Gregorb6998662010-01-19 19:34:47 +00001812 case Decl::ClassTemplate: {
1813 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001814 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001815 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001816 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001817 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001818 return clang_getNullCursor();
1819 }
1820
1821 case Decl::Using: {
1822 UsingDecl *Using = cast<UsingDecl>(D);
1823 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001824 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1825 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001826 S != SEnd; ++S) {
1827 if (Def != clang_getNullCursor()) {
1828 // FIXME: We have no way to return multiple results.
1829 return clang_getNullCursor();
1830 }
1831
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001832 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001833 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001834 }
1835
1836 return Def;
1837 }
1838
1839 case Decl::UsingShadow:
1840 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001841 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001842 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001843
1844 case Decl::ObjCMethod: {
1845 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1846 if (Method->isThisDeclarationADefinition())
1847 return C;
1848
1849 // Dig out the method definition in the associated
1850 // @implementation, if we have it.
1851 // FIXME: The ASTs should make finding the definition easier.
1852 if (ObjCInterfaceDecl *Class
1853 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1854 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1855 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1856 Method->isInstanceMethod()))
1857 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001858 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001859
1860 return clang_getNullCursor();
1861 }
1862
1863 case Decl::ObjCCategory:
1864 if (ObjCCategoryImplDecl *Impl
1865 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001866 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001867 return clang_getNullCursor();
1868
1869 case Decl::ObjCProtocol:
1870 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1871 return C;
1872 return clang_getNullCursor();
1873
1874 case Decl::ObjCInterface:
1875 // There are two notions of a "definition" for an Objective-C
1876 // class: the interface and its implementation. When we resolved a
1877 // reference to an Objective-C class, produce the @interface as
1878 // the definition; when we were provided with the interface,
1879 // produce the @implementation as the definition.
1880 if (WasReference) {
1881 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1882 return C;
1883 } else if (ObjCImplementationDecl *Impl
1884 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001885 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001886 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001887
Douglas Gregorb6998662010-01-19 19:34:47 +00001888 case Decl::ObjCProperty:
1889 // FIXME: We don't really know where to find the
1890 // ObjCPropertyImplDecls that implement this property.
1891 return clang_getNullCursor();
1892
1893 case Decl::ObjCCompatibleAlias:
1894 if (ObjCInterfaceDecl *Class
1895 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1896 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001897 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001898
Douglas Gregorb6998662010-01-19 19:34:47 +00001899 return clang_getNullCursor();
1900
1901 case Decl::ObjCForwardProtocol: {
1902 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1903 if (Forward->protocol_size() == 1)
1904 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001905 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001906 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001907
1908 // FIXME: Cannot return multiple definitions.
1909 return clang_getNullCursor();
1910 }
1911
1912 case Decl::ObjCClass: {
1913 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1914 if (Class->size() == 1) {
1915 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1916 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001917 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001918 return clang_getNullCursor();
1919 }
1920
1921 // FIXME: Cannot return multiple definitions.
1922 return clang_getNullCursor();
1923 }
1924
1925 case Decl::Friend:
1926 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001927 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001928 return clang_getNullCursor();
1929
1930 case Decl::FriendTemplate:
1931 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001932 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001933 return clang_getNullCursor();
1934 }
1935
1936 return clang_getNullCursor();
1937}
1938
1939unsigned clang_isCursorDefinition(CXCursor C) {
1940 if (!clang_isDeclaration(C.kind))
1941 return 0;
1942
1943 return clang_getCursorDefinition(C) == C;
1944}
1945
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001946void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001947 const char **startBuf,
1948 const char **endBuf,
1949 unsigned *startLine,
1950 unsigned *startColumn,
1951 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001952 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001953 assert(getCursorDecl(C) && "CXCursor has null decl");
1954 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001955 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1956 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001957
Steve Naroff4ade6d62009-09-23 17:52:52 +00001958 SourceManager &SM = FD->getASTContext().getSourceManager();
1959 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1960 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1961 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1962 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1963 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1964 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1965}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001966
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001967void clang_enableStackTraces(void) {
1968 llvm::sys::PrintStackTraceOnErrorSignal();
1969}
1970
Ted Kremenekfb480492010-01-13 21:46:36 +00001971} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001972
Ted Kremenekfb480492010-01-13 21:46:36 +00001973//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001974// Token-based Operations.
1975//===----------------------------------------------------------------------===//
1976
1977/* CXToken layout:
1978 * int_data[0]: a CXTokenKind
1979 * int_data[1]: starting token location
1980 * int_data[2]: token length
1981 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001982 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001983 * otherwise unused.
1984 */
1985extern "C" {
1986
1987CXTokenKind clang_getTokenKind(CXToken CXTok) {
1988 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1989}
1990
1991CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1992 switch (clang_getTokenKind(CXTok)) {
1993 case CXToken_Identifier:
1994 case CXToken_Keyword:
1995 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001996 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
1997 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001998
1999 case CXToken_Literal: {
2000 // We have stashed the starting pointer in the ptr_data field. Use it.
2001 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002002 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002003 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002004
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002005 case CXToken_Punctuation:
2006 case CXToken_Comment:
2007 break;
2008 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002009
2010 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002011 // deconstructing the source location.
2012 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2013 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002014 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002015
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002016 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
2017 std::pair<FileID, unsigned> LocInfo
2018 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
2019 std::pair<const char *,const char *> Buffer
2020 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
2021
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002022 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
2023 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002024}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002025
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002026CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
2027 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2028 if (!CXXUnit)
2029 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002030
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002031 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
2032 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2033}
2034
2035CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
2036 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00002037 if (!CXXUnit)
2038 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002039
2040 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002041 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
2042}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002043
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002044void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
2045 CXToken **Tokens, unsigned *NumTokens) {
2046 if (Tokens)
2047 *Tokens = 0;
2048 if (NumTokens)
2049 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002050
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002051 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2052 if (!CXXUnit || !Tokens || !NumTokens)
2053 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002054
Daniel Dunbar85b988f2010-02-14 08:31:57 +00002055 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002056 if (R.isInvalid())
2057 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002058
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002059 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2060 std::pair<FileID, unsigned> BeginLocInfo
2061 = SourceMgr.getDecomposedLoc(R.getBegin());
2062 std::pair<FileID, unsigned> EndLocInfo
2063 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002064
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002065 // Cannot tokenize across files.
2066 if (BeginLocInfo.first != EndLocInfo.first)
2067 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002068
2069 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002070 std::pair<const char *,const char *> Buffer
2071 = SourceMgr.getBufferData(BeginLocInfo.first);
2072 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2073 CXXUnit->getASTContext().getLangOptions(),
2074 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2075 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002076
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002077 // Lex tokens until we hit the end of the range.
2078 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2079 llvm::SmallVector<CXToken, 32> CXTokens;
2080 Token Tok;
2081 do {
2082 // Lex the next token
2083 Lex.LexFromRawLexer(Tok);
2084 if (Tok.is(tok::eof))
2085 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002086
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002087 // Initialize the CXToken.
2088 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002089
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002090 // - Common fields
2091 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2092 CXTok.int_data[2] = Tok.getLength();
2093 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002094
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002095 // - Kind-specific fields
2096 if (Tok.isLiteral()) {
2097 CXTok.int_data[0] = CXToken_Literal;
2098 CXTok.ptr_data = (void *)Tok.getLiteralData();
2099 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002100 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002101 std::pair<FileID, unsigned> LocInfo
2102 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002103 const char *StartPos
2104 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002105 LocInfo.second;
2106 IdentifierInfo *II
2107 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2108 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2109 CXToken_Identifier
2110 : CXToken_Keyword;
2111 CXTok.ptr_data = II;
2112 } else if (Tok.is(tok::comment)) {
2113 CXTok.int_data[0] = CXToken_Comment;
2114 CXTok.ptr_data = 0;
2115 } else {
2116 CXTok.int_data[0] = CXToken_Punctuation;
2117 CXTok.ptr_data = 0;
2118 }
2119 CXTokens.push_back(CXTok);
2120 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002121
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002122 if (CXTokens.empty())
2123 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002124
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002125 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2126 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2127 *NumTokens = CXTokens.size();
2128}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002129
2130typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2131
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002132enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2133 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002134 CXClientData client_data) {
2135 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2136
2137 // We only annotate the locations of declarations, simple
2138 // references, and expressions which directly reference something.
2139 CXCursorKind Kind = clang_getCursorKind(cursor);
2140 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2141 // Okay: We can annotate the location of this declaration with the
2142 // declaration or reference
2143 } else if (clang_isExpression(cursor.kind)) {
2144 if (Kind != CXCursor_DeclRefExpr &&
2145 Kind != CXCursor_MemberRefExpr &&
2146 Kind != CXCursor_ObjCMessageExpr)
2147 return CXChildVisit_Recurse;
2148
2149 CXCursor Referenced = clang_getCursorReferenced(cursor);
2150 if (Referenced == cursor || Referenced == clang_getNullCursor())
2151 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002152
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002153 // Okay: we can annotate the location of this expression
2154 } else {
2155 // Nothing to annotate
2156 return CXChildVisit_Recurse;
2157 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002158
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002159 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2160 (*Data)[Loc.int_data] = cursor;
2161 return CXChildVisit_Recurse;
2162}
2163
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002164void clang_annotateTokens(CXTranslationUnit TU,
2165 CXToken *Tokens, unsigned NumTokens,
2166 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002167 if (NumTokens == 0)
2168 return;
2169
2170 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002171 for (unsigned I = 0; I != NumTokens; ++I)
2172 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002173
2174 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2175 if (!CXXUnit || !Tokens)
2176 return;
2177
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002178 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002179 SourceRange RegionOfInterest;
2180 RegionOfInterest.setBegin(
2181 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2182 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002183 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002184 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002185 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002186 // FIXME: Would be great to have a "hint" cursor, then walk from that
2187 // hint cursor upward until we find a cursor whose source range encloses
2188 // the region of interest, rather than starting from the translation unit.
2189 AnnotateTokensData Annotated;
2190 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002191 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002192 Decl::MaxPCHLevel, RegionOfInterest);
2193 AnnotateVis.VisitChildren(Parent);
2194
2195 for (unsigned I = 0; I != NumTokens; ++I) {
2196 // Determine whether we saw a cursor at this token's location.
2197 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2198 if (Pos == Annotated.end())
2199 continue;
2200
2201 Cursors[I] = Pos->second;
2202 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002203}
2204
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002205void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002206 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002207 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002208}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002209
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002210} // end: extern "C"
2211
2212//===----------------------------------------------------------------------===//
Ted Kremenek16b42592010-03-03 06:36:57 +00002213// Operations for querying linkage of a cursor.
2214//===----------------------------------------------------------------------===//
2215
2216extern "C" {
2217CXLinkageKind clang_getCursorLinkage(CXCursor cursor) {
2218 Decl *D = cxcursor::getCursorDecl(cursor);
2219 if (NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D))
2220 switch (ND->getLinkage()) {
2221 case NoLinkage: return CXLinkage_NoLinkage;
2222 case InternalLinkage: return CXLinkage_Internal;
2223 case UniqueExternalLinkage: return CXLinkage_UniqueExternal;
2224 case ExternalLinkage: return CXLinkage_External;
2225 };
2226
2227 return CXLinkage_Invalid;
2228}
2229} // end: extern "C"
2230
2231//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002232// CXString Operations.
2233//===----------------------------------------------------------------------===//
2234
2235extern "C" {
2236const char *clang_getCString(CXString string) {
2237 return string.Spelling;
2238}
2239
2240void clang_disposeString(CXString string) {
2241 if (string.MustFreeString && string.Spelling)
2242 free((void*)string.Spelling);
2243}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002244
Ted Kremenekfb480492010-01-13 21:46:36 +00002245} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002246
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002247namespace clang { namespace cxstring {
2248CXString createCXString(const char *String, bool DupString){
2249 CXString Str;
2250 if (DupString) {
2251 Str.Spelling = strdup(String);
2252 Str.MustFreeString = 1;
2253 } else {
2254 Str.Spelling = String;
2255 Str.MustFreeString = 0;
2256 }
2257 return Str;
2258}
2259
2260CXString createCXString(llvm::StringRef String, bool DupString) {
2261 CXString Result;
2262 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2263 char *Spelling = (char *)malloc(String.size() + 1);
2264 memmove(Spelling, String.data(), String.size());
2265 Spelling[String.size()] = 0;
2266 Result.Spelling = Spelling;
2267 Result.MustFreeString = 1;
2268 } else {
2269 Result.Spelling = String.data();
2270 Result.MustFreeString = 0;
2271 }
2272 return Result;
2273}
2274}}
2275
Ted Kremenek04bb7162010-01-22 22:44:15 +00002276//===----------------------------------------------------------------------===//
2277// Misc. utility functions.
2278//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002279
Ted Kremenek04bb7162010-01-22 22:44:15 +00002280extern "C" {
2281
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002282CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002283 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002284}
2285
2286} // end: extern "C"