blob: 21f3860abd02779e4aee0d1d07ceff5592efd5d0 [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"
Ted Kremenekfc062212009-10-19 21:44:57 +000030
Ted Kremenekdb3d0da2010-01-05 20:55:39 +000031// Needed to define L_TMPNAM on some systems.
32#include <cstdio>
33
Steve Naroff50398192009-08-28 15:28:48 +000034using namespace clang;
Ted Kremenek16c440a2010-01-15 20:35:54 +000035using namespace clang::cxcursor;
Ted Kremenekee4db4f2010-02-17 00:41:08 +000036using namespace clang::cxstring;
Steve Naroff50398192009-08-28 15:28:48 +000037using namespace idx;
38
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000039//===----------------------------------------------------------------------===//
40// Crash Reporting.
41//===----------------------------------------------------------------------===//
42
43#ifdef __APPLE__
Ted Kremenek29b72842010-01-07 22:49:05 +000044#define USE_CRASHTRACER
Ted Kremenek8a8da7d2010-01-06 03:42:32 +000045#include "clang/Analysis/Support/SaveAndRestore.h"
46// Integrate with crash reporter.
47extern "C" const char *__crashreporter_info__;
Ted Kremenek6b569992010-02-17 21:12:23 +000048#define NUM_CRASH_STRINGS 32
Ted Kremenek29b72842010-01-07 22:49:05 +000049static unsigned crashtracer_counter = 0;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000050static unsigned crashtracer_counter_id[NUM_CRASH_STRINGS] = { 0 };
Ted Kremenek29b72842010-01-07 22:49:05 +000051static const char *crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
52static const char *agg_crashtracer_strings[NUM_CRASH_STRINGS] = { 0 };
53
54static unsigned SetCrashTracerInfo(const char *str,
55 llvm::SmallString<1024> &AggStr) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +000056
Ted Kremenek254ba7c2010-01-07 23:13:53 +000057 unsigned slot = 0;
Ted Kremenek29b72842010-01-07 22:49:05 +000058 while (crashtracer_strings[slot]) {
59 if (++slot == NUM_CRASH_STRINGS)
60 slot = 0;
61 }
62 crashtracer_strings[slot] = str;
Ted Kremenek254ba7c2010-01-07 23:13:53 +000063 crashtracer_counter_id[slot] = ++crashtracer_counter;
Ted Kremenek29b72842010-01-07 22:49:05 +000064
65 // We need to create an aggregate string because multiple threads
66 // may be in this method at one time. The crash reporter string
67 // will attempt to overapproximate the set of in-flight invocations
68 // of this function. Race conditions can still cause this goal
69 // to not be achieved.
70 {
Ted Kremenekf0e23e82010-02-17 00:41:40 +000071 llvm::raw_svector_ostream Out(AggStr);
Ted Kremenek29b72842010-01-07 22:49:05 +000072 for (unsigned i = 0; i < NUM_CRASH_STRINGS; ++i)
73 if (crashtracer_strings[i]) Out << crashtracer_strings[i] << '\n';
74 }
75 __crashreporter_info__ = agg_crashtracer_strings[slot] = AggStr.c_str();
76 return slot;
77}
78
79static void ResetCrashTracerInfo(unsigned slot) {
Ted Kremenek254ba7c2010-01-07 23:13:53 +000080 unsigned max_slot = 0;
81 unsigned max_value = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +000082
Ted Kremenek254ba7c2010-01-07 23:13:53 +000083 crashtracer_strings[slot] = agg_crashtracer_strings[slot] = 0;
84
85 for (unsigned i = 0 ; i < NUM_CRASH_STRINGS; ++i)
86 if (agg_crashtracer_strings[i] &&
87 crashtracer_counter_id[i] > max_value) {
88 max_slot = i;
89 max_value = crashtracer_counter_id[i];
Ted Kremenek29b72842010-01-07 22:49:05 +000090 }
Ted Kremenek254ba7c2010-01-07 23:13:53 +000091
92 __crashreporter_info__ = agg_crashtracer_strings[max_slot];
Ted Kremenek29b72842010-01-07 22:49:05 +000093}
94
95namespace {
96class ArgsCrashTracerInfo {
97 llvm::SmallString<1024> CrashString;
98 llvm::SmallString<1024> AggregateString;
99 unsigned crashtracerSlot;
100public:
101 ArgsCrashTracerInfo(llvm::SmallVectorImpl<const char*> &Args)
102 : crashtracerSlot(0)
103 {
104 {
105 llvm::raw_svector_ostream Out(CrashString);
106 Out << "ClangCIndex [createTranslationUnitFromSourceFile]: clang";
107 for (llvm::SmallVectorImpl<const char*>::iterator I=Args.begin(),
108 E=Args.end(); I!=E; ++I)
109 Out << ' ' << *I;
110 }
111 crashtracerSlot = SetCrashTracerInfo(CrashString.c_str(),
112 AggregateString);
113 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000114
Ted Kremenek29b72842010-01-07 22:49:05 +0000115 ~ArgsCrashTracerInfo() {
116 ResetCrashTracerInfo(crashtracerSlot);
117 }
118};
119}
120#endif
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000121
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000122/// \brief The result of comparing two source ranges.
123enum RangeComparisonResult {
124 /// \brief Either the ranges overlap or one of the ranges is invalid.
125 RangeOverlap,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000126
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000127 /// \brief The first range ends before the second range starts.
128 RangeBefore,
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000129
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000130 /// \brief The first range starts after the second range ends.
131 RangeAfter
132};
133
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000134/// \brief Compare two source ranges to determine their relative position in
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000135/// the translation unit.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000136static RangeComparisonResult RangeCompare(SourceManager &SM,
137 SourceRange R1,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000138 SourceRange R2) {
139 assert(R1.isValid() && "First range is invalid?");
140 assert(R2.isValid() && "Second range is invalid?");
Daniel Dunbard52864b2010-02-14 10:02:57 +0000141 if (R1.getEnd() == R2.getBegin() ||
142 SM.isBeforeInTranslationUnit(R1.getEnd(), R2.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000143 return RangeBefore;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000144 if (R2.getEnd() == R1.getBegin() ||
145 SM.isBeforeInTranslationUnit(R2.getEnd(), R1.getBegin()))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000146 return RangeAfter;
147 return RangeOverlap;
148}
149
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000150/// \brief Translate a Clang source range into a CIndex source range.
151///
152/// Clang internally represents ranges where the end location points to the
153/// start of the token at the end. However, for external clients it is more
154/// useful to have a CXSourceRange be a proper half-open interval. This routine
155/// does the appropriate translation.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000156CXSourceRange cxloc::translateSourceRange(const SourceManager &SM,
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000157 const LangOptions &LangOpts,
158 SourceRange R) {
159 // FIXME: This is largely copy-paste from
160 // TextDiagnosticPrinter::HighlightRange. When it is clear that this is what
161 // we want the two routines should be refactored.
162
163 // We want the last character in this location, so we will adjust the
164 // instantiation location accordingly.
165
166 // If the location is from a macro instantiation, get the end of the
167 // instantiation range.
168 SourceLocation EndLoc = R.getEnd();
169 SourceLocation InstLoc = SM.getInstantiationLoc(EndLoc);
170 if (EndLoc.isMacroID())
171 InstLoc = SM.getInstantiationRange(EndLoc).second;
172
173 // Measure the length token we're pointing at, so we can adjust the physical
174 // location in the file to point at the last character.
175 //
176 // FIXME: This won't cope with trigraphs or escaped newlines well. For that,
177 // we actually need a preprocessor, which isn't currently available
178 // here. Eventually, we'll switch the pointer data of
179 // CXSourceLocation/CXSourceRange to a translation unit (CXXUnit), so that the
180 // preprocessor will be available here. At that point, we can use
181 // Preprocessor::getLocForEndOfToken().
182 if (InstLoc.isValid()) {
183 unsigned Length = Lexer::MeasureTokenLength(InstLoc, SM, LangOpts);
Daniel Dunbar76dd3c22010-02-14 01:47:29 +0000184 EndLoc = EndLoc.getFileLocWithOffset(Length);
185 }
186
187 CXSourceRange Result = { { (void *)&SM, (void *)&LangOpts },
188 R.getBegin().getRawEncoding(),
189 EndLoc.getRawEncoding() };
190 return Result;
191}
Douglas Gregor1db19de2010-01-19 21:36:55 +0000192
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000193//===----------------------------------------------------------------------===//
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000194// Cursor visitor.
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000195//===----------------------------------------------------------------------===//
196
Steve Naroff89922f82009-08-31 00:59:03 +0000197namespace {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000198
Douglas Gregorb1373d02010-01-20 20:59:29 +0000199// Cursor visitor.
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000200class CursorVisitor : public DeclVisitor<CursorVisitor, bool>,
Douglas Gregora59e3902010-01-21 23:27:09 +0000201 public TypeLocVisitor<CursorVisitor, bool>,
202 public StmtVisitor<CursorVisitor, bool>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000203{
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000204 /// \brief The translation unit we are traversing.
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000205 ASTUnit *TU;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000206
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000207 /// \brief The parent cursor whose children we are traversing.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000208 CXCursor Parent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000209
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000210 /// \brief The declaration that serves at the parent of any statement or
211 /// expression nodes.
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000212 Decl *StmtParent;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000213
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000214 /// \brief The visitor function.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000215 CXCursorVisitor Visitor;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000216
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000217 /// \brief The opaque client data, to be passed along to the visitor.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000218 CXClientData ClientData;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000219
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000220 // MaxPCHLevel - the maximum PCH level of declarations that we will pass on
221 // to the visitor. Declarations with a PCH level greater than this value will
222 // be suppressed.
223 unsigned MaxPCHLevel;
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000224
225 /// \brief When valid, a source range to which the cursor should restrict
226 /// its search.
227 SourceRange RegionOfInterest;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000228
Douglas Gregorb1373d02010-01-20 20:59:29 +0000229 using DeclVisitor<CursorVisitor, bool>::Visit;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000230 using TypeLocVisitor<CursorVisitor, bool>::Visit;
Douglas Gregora59e3902010-01-21 23:27:09 +0000231 using StmtVisitor<CursorVisitor, bool>::Visit;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000232
233 /// \brief Determine whether this particular source range comes before, comes
234 /// after, or overlaps the region of interest.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000235 ///
Daniel Dunbard52864b2010-02-14 10:02:57 +0000236 /// \param R a half-open source range retrieved from the abstract syntax tree.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000237 RangeComparisonResult CompareRegionOfInterest(SourceRange R);
238
Steve Naroff89922f82009-08-31 00:59:03 +0000239public:
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000240 CursorVisitor(ASTUnit *TU, CXCursorVisitor Visitor, CXClientData ClientData,
241 unsigned MaxPCHLevel,
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000242 SourceRange RegionOfInterest = SourceRange())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000243 : TU(TU), Visitor(Visitor), ClientData(ClientData),
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000244 MaxPCHLevel(MaxPCHLevel), RegionOfInterest(RegionOfInterest)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000245 {
246 Parent.kind = CXCursor_NoDeclFound;
247 Parent.data[0] = 0;
248 Parent.data[1] = 0;
249 Parent.data[2] = 0;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000250 StmtParent = 0;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000251 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000252
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000253 bool Visit(CXCursor Cursor, bool CheckedRegionOfInterest = false);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000254 bool VisitChildren(CXCursor Parent);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000255
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000256 // Declaration visitors
Douglas Gregorb1373d02010-01-20 20:59:29 +0000257 bool VisitDeclContext(DeclContext *DC);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000258 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000259 bool VisitTypedefDecl(TypedefDecl *D);
260 bool VisitTagDecl(TagDecl *D);
261 bool VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000262 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000263 bool VisitFunctionDecl(FunctionDecl *ND);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000264 bool VisitFieldDecl(FieldDecl *D);
265 bool VisitVarDecl(VarDecl *);
266 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
Douglas Gregora59e3902010-01-21 23:27:09 +0000267 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000268 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000269 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000270 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
271 bool VisitObjCImplDecl(ObjCImplDecl *D);
272 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
273 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
274 // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
275 // etc.
276 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
277 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
278 bool VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000279
280 // Type visitors
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000281 // FIXME: QualifiedTypeLoc doesn't provide any location information
282 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000283 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000284 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
285 bool VisitTagTypeLoc(TagTypeLoc TL);
286 // FIXME: TemplateTypeParmTypeLoc doesn't provide any location information
287 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
288 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
289 bool VisitPointerTypeLoc(PointerTypeLoc TL);
290 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
291 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
292 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
293 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
294 bool VisitFunctionTypeLoc(FunctionTypeLoc TL);
295 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000296 // FIXME: Implement for TemplateSpecializationTypeLoc
297 // FIXME: Implement visitors here when the unimplemented TypeLocs get
298 // implemented
299 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
300 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000301
Douglas Gregora59e3902010-01-21 23:27:09 +0000302 // Statement visitors
303 bool VisitStmt(Stmt *S);
304 bool VisitDeclStmt(DeclStmt *S);
Douglas Gregorf5bab412010-01-22 01:00:11 +0000305 // FIXME: LabelStmt label?
306 bool VisitIfStmt(IfStmt *S);
307 bool VisitSwitchStmt(SwitchStmt *S);
Douglas Gregor263b47b2010-01-25 16:12:32 +0000308 bool VisitWhileStmt(WhileStmt *S);
309 bool VisitForStmt(ForStmt *S);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000310
Douglas Gregor336fd812010-01-23 00:40:08 +0000311 // Expression visitors
312 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
313 bool VisitExplicitCastExpr(ExplicitCastExpr *E);
314 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Steve Naroff89922f82009-08-31 00:59:03 +0000315};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000316
Ted Kremenekab188932010-01-05 19:32:54 +0000317} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000318
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000319RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000320 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
321}
322
Douglas Gregorb1373d02010-01-20 20:59:29 +0000323/// \brief Visit the given cursor and, if requested by the visitor,
324/// its children.
325///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000326/// \param Cursor the cursor to visit.
327///
328/// \param CheckRegionOfInterest if true, then the caller already checked that
329/// this cursor is within the region of interest.
330///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000331/// \returns true if the visitation should be aborted, false if it
332/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000333bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000334 if (clang_isInvalid(Cursor.kind))
335 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000336
Douglas Gregorb1373d02010-01-20 20:59:29 +0000337 if (clang_isDeclaration(Cursor.kind)) {
338 Decl *D = getCursorDecl(Cursor);
339 assert(D && "Invalid declaration cursor");
340 if (D->getPCHLevel() > MaxPCHLevel)
341 return false;
342
343 if (D->isImplicit())
344 return false;
345 }
346
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000347 // If we have a range of interest, and this cursor doesn't intersect with it,
348 // we're done.
349 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Daniel Dunbarf408f322010-02-14 08:32:05 +0000350 SourceRange Range =
351 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
352 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000353 return false;
354 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000355
Douglas Gregorb1373d02010-01-20 20:59:29 +0000356 switch (Visitor(Cursor, Parent, ClientData)) {
357 case CXChildVisit_Break:
358 return true;
359
360 case CXChildVisit_Continue:
361 return false;
362
363 case CXChildVisit_Recurse:
364 return VisitChildren(Cursor);
365 }
366
Douglas Gregorfd643772010-01-25 16:45:46 +0000367 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000368}
369
370/// \brief Visit the children of the given cursor.
371///
372/// \returns true if the visitation should be aborted, false if it
373/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000374bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000375 if (clang_isReference(Cursor.kind)) {
376 // By definition, references have no children.
377 return false;
378 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000379
380 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000381 // done.
382 class SetParentRAII {
383 CXCursor &Parent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000384 Decl *&StmtParent;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000385 CXCursor OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000386
Douglas Gregorb1373d02010-01-20 20:59:29 +0000387 public:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000388 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000389 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000390 {
391 Parent = NewParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000392 if (clang_isDeclaration(Parent.kind))
393 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000394 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000395
Douglas Gregorb1373d02010-01-20 20:59:29 +0000396 ~SetParentRAII() {
397 Parent = OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000398 if (clang_isDeclaration(Parent.kind))
399 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000400 }
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000401 } SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000402
Douglas Gregorb1373d02010-01-20 20:59:29 +0000403 if (clang_isDeclaration(Cursor.kind)) {
404 Decl *D = getCursorDecl(Cursor);
405 assert(D && "Invalid declaration cursor");
406 return Visit(D);
407 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000408
Douglas Gregora59e3902010-01-21 23:27:09 +0000409 if (clang_isStatement(Cursor.kind))
410 return Visit(getCursorStmt(Cursor));
411 if (clang_isExpression(Cursor.kind))
412 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000413
Douglas Gregorb1373d02010-01-20 20:59:29 +0000414 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000415 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000416 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
417 RegionOfInterest.isInvalid()) {
Douglas Gregor7b691f332010-01-20 21:13:59 +0000418 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
419 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
420 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000421 if (Visit(MakeCXCursor(*it, CXXUnit), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000422 return true;
423 }
424 } else {
425 return VisitDeclContext(
Douglas Gregorb1373d02010-01-20 20:59:29 +0000426 CXXUnit->getASTContext().getTranslationUnitDecl());
Douglas Gregor7b691f332010-01-20 21:13:59 +0000427 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000428
Douglas Gregor7b691f332010-01-20 21:13:59 +0000429 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000430 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000431
Douglas Gregorb1373d02010-01-20 20:59:29 +0000432 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000433 return false;
434}
435
Douglas Gregorb1373d02010-01-20 20:59:29 +0000436bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000437 for (DeclContext::decl_iterator
Douglas Gregorb1373d02010-01-20 20:59:29 +0000438 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Daniel Dunbard52864b2010-02-14 10:02:57 +0000439 CXCursor Cursor = MakeCXCursor(*I, TU);
440
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000441 if (RegionOfInterest.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +0000442 SourceRange Range =
443 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
444 if (Range.isInvalid())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000445 continue;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000446
447 switch (CompareRegionOfInterest(Range)) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000448 case RangeBefore:
449 // This declaration comes before the region of interest; skip it.
450 continue;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000451
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000452 case RangeAfter:
453 // This declaration comes after the region of interest; we're done.
454 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000455
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000456 case RangeOverlap:
457 // This declaration overlaps the region of interest; visit it.
458 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000459 }
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000460 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000461
Daniel Dunbard52864b2010-02-14 10:02:57 +0000462 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000463 return true;
464 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000465
Douglas Gregorb1373d02010-01-20 20:59:29 +0000466 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000467}
468
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000469bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
470 llvm_unreachable("Translation units are visited directly by Visit()");
471 return false;
472}
473
474bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
475 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
476 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000477
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000478 return false;
479}
480
481bool CursorVisitor::VisitTagDecl(TagDecl *D) {
482 return VisitDeclContext(D);
483}
484
485bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
486 if (Expr *Init = D->getInitExpr())
487 return Visit(MakeCXCursor(Init, StmtParent, TU));
488 return false;
489}
490
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000491bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
492 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
493 if (Visit(TSInfo->getTypeLoc()))
494 return true;
495
496 return false;
497}
498
Douglas Gregorb1373d02010-01-20 20:59:29 +0000499bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000500 if (VisitDeclaratorDecl(ND))
501 return true;
502
Douglas Gregora59e3902010-01-21 23:27:09 +0000503 if (ND->isThisDeclarationADefinition() &&
504 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
505 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000506
Douglas Gregorb1373d02010-01-20 20:59:29 +0000507 return false;
508}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000509
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000510bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
511 if (VisitDeclaratorDecl(D))
512 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000513
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000514 if (Expr *BitWidth = D->getBitWidth())
515 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000516
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000517 return false;
518}
519
520bool CursorVisitor::VisitVarDecl(VarDecl *D) {
521 if (VisitDeclaratorDecl(D))
522 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000523
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000524 if (Expr *Init = D->getInit())
525 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000526
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000527 return false;
528}
529
530bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
531 // FIXME: We really need a TypeLoc covering Objective-C method declarations.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000532 // At the moment, we don't have information about locations in the return
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000533 // type.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000534 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000535 PEnd = ND->param_end();
536 P != PEnd; ++P) {
537 if (Visit(MakeCXCursor(*P, TU)))
538 return true;
539 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000540
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000541 if (ND->isThisDeclarationADefinition() &&
542 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
543 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000544
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000545 return false;
546}
547
Douglas Gregora59e3902010-01-21 23:27:09 +0000548bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
549 return VisitDeclContext(D);
550}
551
Douglas Gregorb1373d02010-01-20 20:59:29 +0000552bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000553 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
554 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000555 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000556
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000557 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
558 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
559 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000560 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000561 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000562
Douglas Gregora59e3902010-01-21 23:27:09 +0000563 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000564}
565
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000566bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
567 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
568 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
569 E = PID->protocol_end(); I != E; ++I, ++PL)
570 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
571 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000572
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000573 return VisitObjCContainerDecl(PID);
574}
575
Douglas Gregorb1373d02010-01-20 20:59:29 +0000576bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000577 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000578 if (D->getSuperClass() &&
579 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000580 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000581 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000582 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000583
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000584 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
585 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
586 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000587 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000588 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000589
Douglas Gregora59e3902010-01-21 23:27:09 +0000590 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000591}
592
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000593bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
594 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000595}
596
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000597bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000598 if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000599 D->getLocation(), TU)))
600 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000601
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000602 return VisitObjCImplDecl(D);
603}
604
605bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
606#if 0
607 // Issue callbacks for super class.
608 // FIXME: No source location information!
609 if (D->getSuperClass() &&
610 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000611 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000612 TU)))
613 return true;
614#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000615
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000616 return VisitObjCImplDecl(D);
617}
618
619bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
620 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
621 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
622 E = D->protocol_end();
623 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000624 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000625 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000626
627 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000628}
629
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000630bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
631 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
632 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
633 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000634
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000635 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000636}
637
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000638bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
639 ASTContext &Context = TU->getASTContext();
640
641 // Some builtin types (such as Objective-C's "id", "sel", and
642 // "Class") have associated declarations. Create cursors for those.
643 QualType VisitType;
644 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
645 case BuiltinType::Void:
646 case BuiltinType::Bool:
647 case BuiltinType::Char_U:
648 case BuiltinType::UChar:
649 case BuiltinType::Char16:
650 case BuiltinType::Char32:
651 case BuiltinType::UShort:
652 case BuiltinType::UInt:
653 case BuiltinType::ULong:
654 case BuiltinType::ULongLong:
655 case BuiltinType::UInt128:
656 case BuiltinType::Char_S:
657 case BuiltinType::SChar:
658 case BuiltinType::WChar:
659 case BuiltinType::Short:
660 case BuiltinType::Int:
661 case BuiltinType::Long:
662 case BuiltinType::LongLong:
663 case BuiltinType::Int128:
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000664 case BuiltinType::Float:
665 case BuiltinType::Double:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000666 case BuiltinType::LongDouble:
667 case BuiltinType::NullPtr:
668 case BuiltinType::Overload:
669 case BuiltinType::Dependent:
670 break;
671
672 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
673 break;
674
675 case BuiltinType::ObjCId:
676 VisitType = Context.getObjCIdType();
677 break;
678
679 case BuiltinType::ObjCClass:
680 VisitType = Context.getObjCClassType();
681 break;
682
683 case BuiltinType::ObjCSel:
684 VisitType = Context.getObjCSelType();
685 break;
686 }
687
688 if (!VisitType.isNull()) {
689 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000690 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000691 TU));
692 }
693
694 return false;
695}
696
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000697bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
698 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
699}
700
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000701bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
702 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
703}
704
705bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
706 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
707}
708
709bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
710 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
711 return true;
712
713 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
714 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
715 TU)))
716 return true;
717 }
718
719 return false;
720}
721
722bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
723 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
724 return true;
725
726 if (TL.hasProtocolsAsWritten()) {
727 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000728 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000729 TL.getProtocolLoc(I),
730 TU)))
731 return true;
732 }
733 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000734
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000735 return false;
736}
737
738bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
739 return Visit(TL.getPointeeLoc());
740}
741
742bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
743 return Visit(TL.getPointeeLoc());
744}
745
746bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
747 return Visit(TL.getPointeeLoc());
748}
749
750bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000751 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000752}
753
754bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000755 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000756}
757
758bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
759 if (Visit(TL.getResultLoc()))
760 return true;
761
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000762 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
763 if (Visit(MakeCXCursor(TL.getArg(I), TU)))
764 return true;
765
766 return false;
767}
768
769bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
770 if (Visit(TL.getElementLoc()))
771 return true;
772
773 if (Expr *Size = TL.getSizeExpr())
774 return Visit(MakeCXCursor(Size, StmtParent, TU));
775
776 return false;
777}
778
Douglas Gregor2332c112010-01-21 20:48:56 +0000779bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
780 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
781}
782
783bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
784 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
785 return Visit(TSInfo->getTypeLoc());
786
787 return false;
788}
789
Douglas Gregora59e3902010-01-21 23:27:09 +0000790bool CursorVisitor::VisitStmt(Stmt *S) {
791 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
792 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000793 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000794 return true;
795 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000796
Douglas Gregora59e3902010-01-21 23:27:09 +0000797 return false;
798}
799
800bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
801 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
802 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000803 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000804 return true;
805 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000806
Douglas Gregora59e3902010-01-21 23:27:09 +0000807 return false;
808}
809
Douglas Gregorf5bab412010-01-22 01:00:11 +0000810bool CursorVisitor::VisitIfStmt(IfStmt *S) {
811 if (VarDecl *Var = S->getConditionVariable()) {
812 if (Visit(MakeCXCursor(Var, TU)))
813 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000814 }
815
Douglas Gregor263b47b2010-01-25 16:12:32 +0000816 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
817 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000818 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
819 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000820 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
821 return true;
822
823 return false;
824}
825
826bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
827 if (VarDecl *Var = S->getConditionVariable()) {
828 if (Visit(MakeCXCursor(Var, TU)))
829 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000830 }
831
Douglas Gregor263b47b2010-01-25 16:12:32 +0000832 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
833 return true;
834 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
835 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000836
Douglas Gregor263b47b2010-01-25 16:12:32 +0000837 return false;
838}
839
840bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
841 if (VarDecl *Var = S->getConditionVariable()) {
842 if (Visit(MakeCXCursor(Var, TU)))
843 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000844 }
845
Douglas Gregor263b47b2010-01-25 16:12:32 +0000846 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
847 return true;
848 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000849 return true;
850
Douglas Gregor263b47b2010-01-25 16:12:32 +0000851 return false;
852}
853
854bool CursorVisitor::VisitForStmt(ForStmt *S) {
855 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
856 return true;
857 if (VarDecl *Var = S->getConditionVariable()) {
858 if (Visit(MakeCXCursor(Var, TU)))
859 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000860 }
861
Douglas Gregor263b47b2010-01-25 16:12:32 +0000862 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
863 return true;
864 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
865 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000866 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
867 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000868
Douglas Gregorf5bab412010-01-22 01:00:11 +0000869 return false;
870}
871
Douglas Gregor336fd812010-01-23 00:40:08 +0000872bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
873 if (E->isArgumentType()) {
874 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
875 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000876
Douglas Gregor336fd812010-01-23 00:40:08 +0000877 return false;
878 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000879
Douglas Gregor336fd812010-01-23 00:40:08 +0000880 return VisitExpr(E);
881}
882
883bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
884 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
885 if (Visit(TSInfo->getTypeLoc()))
886 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000887
Douglas Gregor336fd812010-01-23 00:40:08 +0000888 return VisitCastExpr(E);
889}
890
891bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
892 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
893 if (Visit(TSInfo->getTypeLoc()))
894 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000895
Douglas Gregor336fd812010-01-23 00:40:08 +0000896 return VisitExpr(E);
897}
898
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000899extern "C" {
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000900CXIndex clang_createIndex(int excludeDeclarationsFromPCH) {
Douglas Gregora030b7c2010-01-22 20:35:53 +0000901 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000902 if (excludeDeclarationsFromPCH)
903 CIdxr->setOnlyLocalDecls();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000904 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000905}
906
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000907void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000908 if (CIdx)
909 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000910}
911
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000912void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000913 if (CIdx) {
914 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
915 CXXIdx->setUseExternalASTGeneration(value);
916 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000917}
918
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000919CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000920 const char *ast_filename,
921 CXDiagnosticCallback diag_callback,
922 CXClientData diag_client_data) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000923 if (!CIdx)
924 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000925
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000926 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000927
Douglas Gregor5352ac02010-01-28 00:27:43 +0000928 // Configure the diagnostics.
929 DiagnosticOptions DiagOpts;
930 llvm::OwningPtr<Diagnostic> Diags;
931 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
932 CIndexDiagnosticClient DiagClient(diag_callback, diag_client_data);
933 Diags->setClient(&DiagClient);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000934
Douglas Gregor5352ac02010-01-28 00:27:43 +0000935 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000936 CXXIdx->getOnlyLocalDecls());
Steve Naroff600866c2009-08-27 19:51:58 +0000937}
938
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000939CXTranslationUnit
940clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
941 const char *source_filename,
942 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000943 const char **command_line_args,
944 unsigned num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000945 struct CXUnsavedFile *unsaved_files,
946 CXDiagnosticCallback diag_callback,
947 CXClientData diag_client_data) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000948 if (!CIdx)
949 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000950
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000951 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
952
Douglas Gregor5352ac02010-01-28 00:27:43 +0000953 // Configure the diagnostics.
954 DiagnosticOptions DiagOpts;
955 llvm::OwningPtr<Diagnostic> Diags;
956 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
957 CIndexDiagnosticClient DiagClient(diag_callback, diag_client_data);
958 Diags->setClient(&DiagClient);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000959
Douglas Gregor4db64a42010-01-23 00:14:00 +0000960 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
961 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000962 const llvm::MemoryBuffer *Buffer
Douglas Gregor4db64a42010-01-23 00:14:00 +0000963 = llvm::MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
964 unsaved_files[I].Contents + unsaved_files[I].Length,
965 unsaved_files[I].Filename);
966 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
967 Buffer));
968 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000969
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000970 if (!CXXIdx->getUseExternalASTGeneration()) {
971 llvm::SmallVector<const char *, 16> Args;
972
973 // The 'source_filename' argument is optional. If the caller does not
974 // specify it then it is assumed that the source file is specified
975 // in the actual argument list.
976 if (source_filename)
977 Args.push_back(source_filename);
978 Args.insert(Args.end(), command_line_args,
979 command_line_args + num_command_line_args);
980
Douglas Gregor5352ac02010-01-28 00:27:43 +0000981 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000982
Ted Kremenek29b72842010-01-07 22:49:05 +0000983#ifdef USE_CRASHTRACER
984 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +0000985#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000986
Daniel Dunbar94220972009-12-05 02:17:18 +0000987 llvm::OwningPtr<ASTUnit> Unit(
988 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000989 *Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +0000990 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +0000991 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +0000992 RemappedFiles.data(),
993 RemappedFiles.size()));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000994
Daniel Dunbar94220972009-12-05 02:17:18 +0000995 // FIXME: Until we have broader testing, just drop the entire AST if we
996 // encountered an error.
Douglas Gregor5352ac02010-01-28 00:27:43 +0000997 if (NumErrors != Diags->getNumErrors())
Daniel Dunbar94220972009-12-05 02:17:18 +0000998 return 0;
999
1000 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001001 }
1002
Ted Kremenek139ba862009-10-22 00:03:57 +00001003 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001004 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001005
Ted Kremenek139ba862009-10-22 00:03:57 +00001006 // First add the complete path to the 'clang' executable.
1007 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001008 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001009
Ted Kremenek139ba862009-10-22 00:03:57 +00001010 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001011 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001012
Ted Kremenek139ba862009-10-22 00:03:57 +00001013 // The 'source_filename' argument is optional. If the caller does not
1014 // specify it then it is assumed that the source file is specified
1015 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001016 if (source_filename)
1017 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001018
Steve Naroff37b5ac22009-10-15 20:50:09 +00001019 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001020 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001021 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001022 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001023
Douglas Gregor4db64a42010-01-23 00:14:00 +00001024 // Remap any unsaved files to temporary files.
1025 std::vector<llvm::sys::Path> TemporaryFiles;
1026 std::vector<std::string> RemapArgs;
1027 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1028 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001029
Douglas Gregor4db64a42010-01-23 00:14:00 +00001030 // The pointers into the elements of RemapArgs are stable because we
1031 // won't be adding anything to RemapArgs after this point.
1032 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1033 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001034
Ted Kremenek139ba862009-10-22 00:03:57 +00001035 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1036 for (int i = 0; i < num_command_line_args; ++i)
1037 if (const char *arg = command_line_args[i]) {
1038 if (strcmp(arg, "-o") == 0) {
1039 ++i; // Also skip the matching argument.
1040 continue;
1041 }
1042 if (strcmp(arg, "-emit-ast") == 0 ||
1043 strcmp(arg, "-c") == 0 ||
1044 strcmp(arg, "-fsyntax-only") == 0) {
1045 continue;
1046 }
1047
1048 // Keep the argument.
1049 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001050 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001051
Douglas Gregord93256e2010-01-28 06:00:51 +00001052 // Generate a temporary name for the diagnostics file.
1053 char tmpFileResults[L_tmpnam];
1054 char *tmpResultsFileName = tmpnam(tmpFileResults);
1055 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1056 TemporaryFiles.push_back(DiagnosticsFile);
1057 argv.push_back("-fdiagnostics-binary");
1058
Ted Kremenek139ba862009-10-22 00:03:57 +00001059 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001060 argv.push_back(NULL);
1061
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001062 // Invoke 'clang'.
1063 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1064 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001065 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001066 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1067 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001068 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001069 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001070 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001071
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001072 if (!ErrMsg.empty()) {
1073 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001074 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001075 I != E; ++I) {
1076 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001077 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001078 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001079 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001080
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001081 Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001082 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001083
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001084 // FIXME: Parse the (redirected) standard error to emit diagnostics.
1085
Douglas Gregor5352ac02010-01-28 00:27:43 +00001086 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001087 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001088 RemappedFiles.data(),
1089 RemappedFiles.size());
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001090 if (ATU)
1091 ATU->unlinkTemporaryFile();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001092
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001093 // FIXME: Currently we don't report diagnostics on invalid ASTs.
1094 if (ATU)
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001095 ReportSerializedDiagnostics(DiagnosticsFile, *Diags,
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001096 num_unsaved_files, unsaved_files,
1097 ATU->getASTContext().getLangOptions());
Douglas Gregord93256e2010-01-28 06:00:51 +00001098
Douglas Gregor4db64a42010-01-23 00:14:00 +00001099 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1100 TemporaryFiles[i].eraseFromDisk();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001101
Steve Naroffe19944c2009-10-15 22:23:48 +00001102 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001103}
1104
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001105void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001106 if (CTUnit)
1107 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001108}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001109
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001110CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001111 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001112 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001113
Steve Naroff77accc12009-09-03 18:19:54 +00001114 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001115 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001116}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001117
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001118CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001119 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001120 return Result;
1121}
1122
Ted Kremenekfb480492010-01-13 21:46:36 +00001123} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001124
Ted Kremenekfb480492010-01-13 21:46:36 +00001125//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001126// CXSourceLocation and CXSourceRange Operations.
1127//===----------------------------------------------------------------------===//
1128
Douglas Gregorb9790342010-01-22 21:44:22 +00001129extern "C" {
1130CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001131 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001132 return Result;
1133}
1134
1135unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001136 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1137 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1138 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001139}
1140
1141CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1142 CXFile file,
1143 unsigned line,
1144 unsigned column) {
1145 if (!tu)
1146 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001147
Douglas Gregorb9790342010-01-22 21:44:22 +00001148 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1149 SourceLocation SLoc
1150 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001151 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001152 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001153
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001154 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001155}
1156
Douglas Gregor5352ac02010-01-28 00:27:43 +00001157CXSourceRange clang_getNullRange() {
1158 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1159 return Result;
1160}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001161
Douglas Gregor5352ac02010-01-28 00:27:43 +00001162CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1163 if (begin.ptr_data[0] != end.ptr_data[0] ||
1164 begin.ptr_data[1] != end.ptr_data[1])
1165 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001166
1167 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001168 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001169 return Result;
1170}
1171
Douglas Gregor46766dc2010-01-26 19:19:08 +00001172void clang_getInstantiationLocation(CXSourceLocation location,
1173 CXFile *file,
1174 unsigned *line,
1175 unsigned *column,
1176 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001177 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1178
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001179 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001180 if (file)
1181 *file = 0;
1182 if (line)
1183 *line = 0;
1184 if (column)
1185 *column = 0;
1186 if (offset)
1187 *offset = 0;
1188 return;
1189 }
1190
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001191 const SourceManager &SM =
1192 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001193 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001194
1195 if (file)
1196 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1197 if (line)
1198 *line = SM.getInstantiationLineNumber(InstLoc);
1199 if (column)
1200 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001201 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001202 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001203}
1204
Douglas Gregor1db19de2010-01-19 21:36:55 +00001205CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001206 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001207 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001208 return Result;
1209}
1210
1211CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001212 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001213 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001214 return Result;
1215}
1216
Douglas Gregorb9790342010-01-22 21:44:22 +00001217} // end: extern "C"
1218
Douglas Gregor1db19de2010-01-19 21:36:55 +00001219//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001220// CXFile Operations.
1221//===----------------------------------------------------------------------===//
1222
1223extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001224CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001225 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001226 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001227
Steve Naroff88145032009-10-27 14:35:18 +00001228 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001229 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001230}
1231
1232time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001233 if (!SFile)
1234 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001235
Steve Naroff88145032009-10-27 14:35:18 +00001236 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1237 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001238}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001239
Douglas Gregorb9790342010-01-22 21:44:22 +00001240CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1241 if (!tu)
1242 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001243
Douglas Gregorb9790342010-01-22 21:44:22 +00001244 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001245
Douglas Gregorb9790342010-01-22 21:44:22 +00001246 FileManager &FMgr = CXXUnit->getFileManager();
1247 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1248 return const_cast<FileEntry *>(File);
1249}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001250
Ted Kremenekfb480492010-01-13 21:46:36 +00001251} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001252
Ted Kremenekfb480492010-01-13 21:46:36 +00001253//===----------------------------------------------------------------------===//
1254// CXCursor Operations.
1255//===----------------------------------------------------------------------===//
1256
Ted Kremenekfb480492010-01-13 21:46:36 +00001257static Decl *getDeclFromExpr(Stmt *E) {
1258 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1259 return RefExpr->getDecl();
1260 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1261 return ME->getMemberDecl();
1262 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1263 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001264
Ted Kremenekfb480492010-01-13 21:46:36 +00001265 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1266 return getDeclFromExpr(CE->getCallee());
1267 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1268 return getDeclFromExpr(CE->getSubExpr());
1269 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1270 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001271
Ted Kremenekfb480492010-01-13 21:46:36 +00001272 return 0;
1273}
1274
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001275static SourceLocation getLocationFromExpr(Expr *E) {
1276 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1277 return /*FIXME:*/Msg->getLeftLoc();
1278 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1279 return DRE->getLocation();
1280 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1281 return Member->getMemberLoc();
1282 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1283 return Ivar->getLocation();
1284 return E->getLocStart();
1285}
1286
Ted Kremenekfb480492010-01-13 21:46:36 +00001287extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001288
1289unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001290 CXCursorVisitor visitor,
1291 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001292 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001293
1294 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001295
Douglas Gregorb1373d02010-01-20 20:59:29 +00001296 // Set the PCHLevel to filter out unwanted decls if requested.
1297 if (CXXUnit->getOnlyLocalDecls()) {
1298 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001299
Douglas Gregorb1373d02010-01-20 20:59:29 +00001300 // If the main input was an AST, bump the level.
1301 if (CXXUnit->isMainFileAST())
1302 ++PCHLevel;
1303 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001304
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001305 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001306 return CursorVis.VisitChildren(parent);
1307}
1308
Douglas Gregor78205d42010-01-20 21:45:58 +00001309static CXString getDeclSpelling(Decl *D) {
1310 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1311 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001312 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001313
Douglas Gregor78205d42010-01-20 21:45:58 +00001314 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001315 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001316
Douglas Gregor78205d42010-01-20 21:45:58 +00001317 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1318 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1319 // and returns different names. NamedDecl returns the class name and
1320 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001321 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001322
Douglas Gregor78205d42010-01-20 21:45:58 +00001323 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001324 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001325
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001326 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001327}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001328
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001329CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001330 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001331 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001332
Steve Narofff334b4e2009-09-02 18:26:48 +00001333 if (clang_isReference(C.kind)) {
1334 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001335 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001336 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001337 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001338 }
1339 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001340 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001341 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001342 }
1343 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001344 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001345 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001346 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001347 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001348 case CXCursor_TypeRef: {
1349 TypeDecl *Type = getCursorTypeRef(C).first;
1350 assert(Type && "Missing type decl");
1351
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001352 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1353 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001354 }
1355
Daniel Dunbaracca7252009-11-30 20:42:49 +00001356 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001357 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001358 }
1359 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001360
1361 if (clang_isExpression(C.kind)) {
1362 Decl *D = getDeclFromExpr(getCursorExpr(C));
1363 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001364 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001365 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001366 }
1367
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001368 if (clang_isDeclaration(C.kind))
1369 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001370
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001371 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001372}
1373
Ted Kremeneke68fff62010-02-17 00:41:32 +00001374CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001375 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001376 case CXCursor_FunctionDecl:
1377 return createCXString("FunctionDecl");
1378 case CXCursor_TypedefDecl:
1379 return createCXString("TypedefDecl");
1380 case CXCursor_EnumDecl:
1381 return createCXString("EnumDecl");
1382 case CXCursor_EnumConstantDecl:
1383 return createCXString("EnumConstantDecl");
1384 case CXCursor_StructDecl:
1385 return createCXString("StructDecl");
1386 case CXCursor_UnionDecl:
1387 return createCXString("UnionDecl");
1388 case CXCursor_ClassDecl:
1389 return createCXString("ClassDecl");
1390 case CXCursor_FieldDecl:
1391 return createCXString("FieldDecl");
1392 case CXCursor_VarDecl:
1393 return createCXString("VarDecl");
1394 case CXCursor_ParmDecl:
1395 return createCXString("ParmDecl");
1396 case CXCursor_ObjCInterfaceDecl:
1397 return createCXString("ObjCInterfaceDecl");
1398 case CXCursor_ObjCCategoryDecl:
1399 return createCXString("ObjCCategoryDecl");
1400 case CXCursor_ObjCProtocolDecl:
1401 return createCXString("ObjCProtocolDecl");
1402 case CXCursor_ObjCPropertyDecl:
1403 return createCXString("ObjCPropertyDecl");
1404 case CXCursor_ObjCIvarDecl:
1405 return createCXString("ObjCIvarDecl");
1406 case CXCursor_ObjCInstanceMethodDecl:
1407 return createCXString("ObjCInstanceMethodDecl");
1408 case CXCursor_ObjCClassMethodDecl:
1409 return createCXString("ObjCClassMethodDecl");
1410 case CXCursor_ObjCImplementationDecl:
1411 return createCXString("ObjCImplementationDecl");
1412 case CXCursor_ObjCCategoryImplDecl:
1413 return createCXString("ObjCCategoryImplDecl");
1414 case CXCursor_UnexposedDecl:
1415 return createCXString("UnexposedDecl");
1416 case CXCursor_ObjCSuperClassRef:
1417 return createCXString("ObjCSuperClassRef");
1418 case CXCursor_ObjCProtocolRef:
1419 return createCXString("ObjCProtocolRef");
1420 case CXCursor_ObjCClassRef:
1421 return createCXString("ObjCClassRef");
1422 case CXCursor_TypeRef:
1423 return createCXString("TypeRef");
1424 case CXCursor_UnexposedExpr:
1425 return createCXString("UnexposedExpr");
1426 case CXCursor_DeclRefExpr:
1427 return createCXString("DeclRefExpr");
1428 case CXCursor_MemberRefExpr:
1429 return createCXString("MemberRefExpr");
1430 case CXCursor_CallExpr:
1431 return createCXString("CallExpr");
1432 case CXCursor_ObjCMessageExpr:
1433 return createCXString("ObjCMessageExpr");
1434 case CXCursor_UnexposedStmt:
1435 return createCXString("UnexposedStmt");
1436 case CXCursor_InvalidFile:
1437 return createCXString("InvalidFile");
1438 case CXCursor_NoDeclFound:
1439 return createCXString("NoDeclFound");
1440 case CXCursor_NotImplemented:
1441 return createCXString("NotImplemented");
1442 case CXCursor_TranslationUnit:
1443 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001444 case CXCursor_UnexposedAttr:
1445 return createCXString("UnexposedAttr");
1446 case CXCursor_IBActionAttr:
1447 return createCXString("attribute(ibaction)");
1448 case CXCursor_IBOutletAttr:
1449 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001450 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001451
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001452 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001453 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001454}
Steve Naroff89922f82009-08-31 00:59:03 +00001455
Ted Kremeneke68fff62010-02-17 00:41:32 +00001456enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1457 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001458 CXClientData client_data) {
1459 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1460 *BestCursor = cursor;
1461 return CXChildVisit_Recurse;
1462}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001463
Douglas Gregorb9790342010-01-22 21:44:22 +00001464CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1465 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001466 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001467
Douglas Gregorb9790342010-01-22 21:44:22 +00001468 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1469
Ted Kremeneka297de22010-01-25 22:34:44 +00001470 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001471 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1472 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001473 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001474
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001475 // FIXME: Would be great to have a "hint" cursor, then walk from that
1476 // hint cursor upward until we find a cursor whose source range encloses
1477 // the region of interest, rather than starting from the translation unit.
1478 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001479 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001480 Decl::MaxPCHLevel, RegionOfInterest);
1481 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001482 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001483 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001484}
1485
Ted Kremenek73885552009-11-17 19:28:59 +00001486CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001487 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001488}
1489
1490unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001491 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001492}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001493
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001494unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001495 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1496}
1497
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001498unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001499 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1500}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001501
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001502unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001503 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1504}
1505
Douglas Gregor97b98722010-01-19 23:20:36 +00001506unsigned clang_isExpression(enum CXCursorKind K) {
1507 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1508}
1509
1510unsigned clang_isStatement(enum CXCursorKind K) {
1511 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1512}
1513
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001514unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1515 return K == CXCursor_TranslationUnit;
1516}
1517
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001518CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001519 return C.kind;
1520}
1521
Douglas Gregor98258af2010-01-18 22:46:11 +00001522CXSourceLocation clang_getCursorLocation(CXCursor C) {
1523 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001524 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001525 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001526 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1527 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001528 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001529 }
1530
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001531 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001532 std::pair<ObjCProtocolDecl *, SourceLocation> P
1533 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001534 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001535 }
1536
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001537 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001538 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1539 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001540 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001541 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001542
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001543 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001544 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001545 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001546 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001547
Douglas Gregorf46034a2010-01-18 23:41:10 +00001548 default:
1549 // FIXME: Need a way to enumerate all non-reference cases.
1550 llvm_unreachable("Missed a reference kind");
1551 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001552 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001553
1554 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001555 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001556 getLocationFromExpr(getCursorExpr(C)));
1557
Douglas Gregor5352ac02010-01-28 00:27:43 +00001558 if (!getCursorDecl(C))
1559 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001560
Douglas Gregorf46034a2010-01-18 23:41:10 +00001561 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001562 SourceLocation Loc = D->getLocation();
1563 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1564 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001565 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001566}
Douglas Gregora7bde202010-01-19 00:34:46 +00001567
1568CXSourceRange clang_getCursorExtent(CXCursor C) {
1569 if (clang_isReference(C.kind)) {
1570 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001571 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001572 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1573 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001574 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001575 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001576
1577 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001578 std::pair<ObjCProtocolDecl *, SourceLocation> P
1579 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001580 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001581 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001582
1583 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001584 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1585 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001586
Ted Kremeneka297de22010-01-25 22:34:44 +00001587 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001588 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001589
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001590 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001591 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001592 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001593 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001594
Douglas Gregora7bde202010-01-19 00:34:46 +00001595 default:
1596 // FIXME: Need a way to enumerate all non-reference cases.
1597 llvm_unreachable("Missed a reference kind");
1598 }
1599 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001600
1601 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001602 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001603 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001604
1605 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001606 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001607 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001608
Douglas Gregor5352ac02010-01-28 00:27:43 +00001609 if (!getCursorDecl(C))
1610 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001611
Douglas Gregora7bde202010-01-19 00:34:46 +00001612 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001613 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001614}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001615
1616CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001617 if (clang_isInvalid(C.kind))
1618 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001619
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001620 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001621 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001622 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001623
Douglas Gregor97b98722010-01-19 23:20:36 +00001624 if (clang_isExpression(C.kind)) {
1625 Decl *D = getDeclFromExpr(getCursorExpr(C));
1626 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001627 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001628 return clang_getNullCursor();
1629 }
1630
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001631 if (!clang_isReference(C.kind))
1632 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001633
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001634 switch (C.kind) {
1635 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001636 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001637
1638 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001639 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001640
1641 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001642 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001643
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001644 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001645 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001646
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001647 default:
1648 // We would prefer to enumerate all non-reference cursor kinds here.
1649 llvm_unreachable("Unhandled reference cursor kind");
1650 break;
1651 }
1652 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001653
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001654 return clang_getNullCursor();
1655}
1656
Douglas Gregorb6998662010-01-19 19:34:47 +00001657CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001658 if (clang_isInvalid(C.kind))
1659 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001660
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001661 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001662
Douglas Gregorb6998662010-01-19 19:34:47 +00001663 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001664 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001665 C = clang_getCursorReferenced(C);
1666 WasReference = true;
1667 }
1668
1669 if (!clang_isDeclaration(C.kind))
1670 return clang_getNullCursor();
1671
1672 Decl *D = getCursorDecl(C);
1673 if (!D)
1674 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001675
Douglas Gregorb6998662010-01-19 19:34:47 +00001676 switch (D->getKind()) {
1677 // Declaration kinds that don't really separate the notions of
1678 // declaration and definition.
1679 case Decl::Namespace:
1680 case Decl::Typedef:
1681 case Decl::TemplateTypeParm:
1682 case Decl::EnumConstant:
1683 case Decl::Field:
1684 case Decl::ObjCIvar:
1685 case Decl::ObjCAtDefsField:
1686 case Decl::ImplicitParam:
1687 case Decl::ParmVar:
1688 case Decl::NonTypeTemplateParm:
1689 case Decl::TemplateTemplateParm:
1690 case Decl::ObjCCategoryImpl:
1691 case Decl::ObjCImplementation:
1692 case Decl::LinkageSpec:
1693 case Decl::ObjCPropertyImpl:
1694 case Decl::FileScopeAsm:
1695 case Decl::StaticAssert:
1696 case Decl::Block:
1697 return C;
1698
1699 // Declaration kinds that don't make any sense here, but are
1700 // nonetheless harmless.
1701 case Decl::TranslationUnit:
1702 case Decl::Template:
1703 case Decl::ObjCContainer:
1704 break;
1705
1706 // Declaration kinds for which the definition is not resolvable.
1707 case Decl::UnresolvedUsingTypename:
1708 case Decl::UnresolvedUsingValue:
1709 break;
1710
1711 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001712 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1713 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001714
1715 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001716 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001717
1718 case Decl::Enum:
1719 case Decl::Record:
1720 case Decl::CXXRecord:
1721 case Decl::ClassTemplateSpecialization:
1722 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001723 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001724 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001725 return clang_getNullCursor();
1726
1727 case Decl::Function:
1728 case Decl::CXXMethod:
1729 case Decl::CXXConstructor:
1730 case Decl::CXXDestructor:
1731 case Decl::CXXConversion: {
1732 const FunctionDecl *Def = 0;
1733 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001734 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001735 return clang_getNullCursor();
1736 }
1737
1738 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001739 // Ask the variable if it has a definition.
1740 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1741 return MakeCXCursor(Def, CXXUnit);
1742 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001743 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001744
Douglas Gregorb6998662010-01-19 19:34:47 +00001745 case Decl::FunctionTemplate: {
1746 const FunctionDecl *Def = 0;
1747 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001748 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001749 return clang_getNullCursor();
1750 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001751
Douglas Gregorb6998662010-01-19 19:34:47 +00001752 case Decl::ClassTemplate: {
1753 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001754 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001755 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001756 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001757 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001758 return clang_getNullCursor();
1759 }
1760
1761 case Decl::Using: {
1762 UsingDecl *Using = cast<UsingDecl>(D);
1763 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001764 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1765 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001766 S != SEnd; ++S) {
1767 if (Def != clang_getNullCursor()) {
1768 // FIXME: We have no way to return multiple results.
1769 return clang_getNullCursor();
1770 }
1771
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001772 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001773 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001774 }
1775
1776 return Def;
1777 }
1778
1779 case Decl::UsingShadow:
1780 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001781 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001782 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001783
1784 case Decl::ObjCMethod: {
1785 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1786 if (Method->isThisDeclarationADefinition())
1787 return C;
1788
1789 // Dig out the method definition in the associated
1790 // @implementation, if we have it.
1791 // FIXME: The ASTs should make finding the definition easier.
1792 if (ObjCInterfaceDecl *Class
1793 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1794 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1795 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1796 Method->isInstanceMethod()))
1797 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001798 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001799
1800 return clang_getNullCursor();
1801 }
1802
1803 case Decl::ObjCCategory:
1804 if (ObjCCategoryImplDecl *Impl
1805 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001806 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001807 return clang_getNullCursor();
1808
1809 case Decl::ObjCProtocol:
1810 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1811 return C;
1812 return clang_getNullCursor();
1813
1814 case Decl::ObjCInterface:
1815 // There are two notions of a "definition" for an Objective-C
1816 // class: the interface and its implementation. When we resolved a
1817 // reference to an Objective-C class, produce the @interface as
1818 // the definition; when we were provided with the interface,
1819 // produce the @implementation as the definition.
1820 if (WasReference) {
1821 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1822 return C;
1823 } else if (ObjCImplementationDecl *Impl
1824 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001825 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001826 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001827
Douglas Gregorb6998662010-01-19 19:34:47 +00001828 case Decl::ObjCProperty:
1829 // FIXME: We don't really know where to find the
1830 // ObjCPropertyImplDecls that implement this property.
1831 return clang_getNullCursor();
1832
1833 case Decl::ObjCCompatibleAlias:
1834 if (ObjCInterfaceDecl *Class
1835 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1836 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001837 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001838
Douglas Gregorb6998662010-01-19 19:34:47 +00001839 return clang_getNullCursor();
1840
1841 case Decl::ObjCForwardProtocol: {
1842 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1843 if (Forward->protocol_size() == 1)
1844 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001845 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001846 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001847
1848 // FIXME: Cannot return multiple definitions.
1849 return clang_getNullCursor();
1850 }
1851
1852 case Decl::ObjCClass: {
1853 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1854 if (Class->size() == 1) {
1855 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1856 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001857 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001858 return clang_getNullCursor();
1859 }
1860
1861 // FIXME: Cannot return multiple definitions.
1862 return clang_getNullCursor();
1863 }
1864
1865 case Decl::Friend:
1866 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001867 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001868 return clang_getNullCursor();
1869
1870 case Decl::FriendTemplate:
1871 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001872 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001873 return clang_getNullCursor();
1874 }
1875
1876 return clang_getNullCursor();
1877}
1878
1879unsigned clang_isCursorDefinition(CXCursor C) {
1880 if (!clang_isDeclaration(C.kind))
1881 return 0;
1882
1883 return clang_getCursorDefinition(C) == C;
1884}
1885
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001886void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001887 const char **startBuf,
1888 const char **endBuf,
1889 unsigned *startLine,
1890 unsigned *startColumn,
1891 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001892 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001893 assert(getCursorDecl(C) && "CXCursor has null decl");
1894 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001895 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1896 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001897
Steve Naroff4ade6d62009-09-23 17:52:52 +00001898 SourceManager &SM = FD->getASTContext().getSourceManager();
1899 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1900 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1901 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1902 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1903 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1904 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1905}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001906
Ted Kremenekfb480492010-01-13 21:46:36 +00001907} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001908
Ted Kremenekfb480492010-01-13 21:46:36 +00001909//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001910// Token-based Operations.
1911//===----------------------------------------------------------------------===//
1912
1913/* CXToken layout:
1914 * int_data[0]: a CXTokenKind
1915 * int_data[1]: starting token location
1916 * int_data[2]: token length
1917 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001918 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001919 * otherwise unused.
1920 */
1921extern "C" {
1922
1923CXTokenKind clang_getTokenKind(CXToken CXTok) {
1924 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1925}
1926
1927CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1928 switch (clang_getTokenKind(CXTok)) {
1929 case CXToken_Identifier:
1930 case CXToken_Keyword:
1931 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001932 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
1933 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001934
1935 case CXToken_Literal: {
1936 // We have stashed the starting pointer in the ptr_data field. Use it.
1937 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001938 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001939 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001940
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001941 case CXToken_Punctuation:
1942 case CXToken_Comment:
1943 break;
1944 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001945
1946 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001947 // deconstructing the source location.
1948 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1949 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001950 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001951
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001952 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
1953 std::pair<FileID, unsigned> LocInfo
1954 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
1955 std::pair<const char *,const char *> Buffer
1956 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
1957
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001958 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
1959 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001960}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001961
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001962CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
1963 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1964 if (!CXXUnit)
1965 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001966
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001967 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
1968 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1969}
1970
1971CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
1972 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00001973 if (!CXXUnit)
1974 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001975
1976 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001977 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1978}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001979
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001980void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
1981 CXToken **Tokens, unsigned *NumTokens) {
1982 if (Tokens)
1983 *Tokens = 0;
1984 if (NumTokens)
1985 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001986
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001987 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1988 if (!CXXUnit || !Tokens || !NumTokens)
1989 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001990
Daniel Dunbar85b988f2010-02-14 08:31:57 +00001991 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001992 if (R.isInvalid())
1993 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001994
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001995 SourceManager &SourceMgr = CXXUnit->getSourceManager();
1996 std::pair<FileID, unsigned> BeginLocInfo
1997 = SourceMgr.getDecomposedLoc(R.getBegin());
1998 std::pair<FileID, unsigned> EndLocInfo
1999 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002000
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002001 // Cannot tokenize across files.
2002 if (BeginLocInfo.first != EndLocInfo.first)
2003 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002004
2005 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002006 std::pair<const char *,const char *> Buffer
2007 = SourceMgr.getBufferData(BeginLocInfo.first);
2008 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2009 CXXUnit->getASTContext().getLangOptions(),
2010 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2011 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002012
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002013 // Lex tokens until we hit the end of the range.
2014 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2015 llvm::SmallVector<CXToken, 32> CXTokens;
2016 Token Tok;
2017 do {
2018 // Lex the next token
2019 Lex.LexFromRawLexer(Tok);
2020 if (Tok.is(tok::eof))
2021 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002022
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002023 // Initialize the CXToken.
2024 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002025
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002026 // - Common fields
2027 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2028 CXTok.int_data[2] = Tok.getLength();
2029 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002030
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002031 // - Kind-specific fields
2032 if (Tok.isLiteral()) {
2033 CXTok.int_data[0] = CXToken_Literal;
2034 CXTok.ptr_data = (void *)Tok.getLiteralData();
2035 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002036 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002037 std::pair<FileID, unsigned> LocInfo
2038 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002039 const char *StartPos
2040 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002041 LocInfo.second;
2042 IdentifierInfo *II
2043 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2044 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2045 CXToken_Identifier
2046 : CXToken_Keyword;
2047 CXTok.ptr_data = II;
2048 } else if (Tok.is(tok::comment)) {
2049 CXTok.int_data[0] = CXToken_Comment;
2050 CXTok.ptr_data = 0;
2051 } else {
2052 CXTok.int_data[0] = CXToken_Punctuation;
2053 CXTok.ptr_data = 0;
2054 }
2055 CXTokens.push_back(CXTok);
2056 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002057
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002058 if (CXTokens.empty())
2059 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002060
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002061 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2062 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2063 *NumTokens = CXTokens.size();
2064}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002065
2066typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2067
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002068enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2069 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002070 CXClientData client_data) {
2071 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2072
2073 // We only annotate the locations of declarations, simple
2074 // references, and expressions which directly reference something.
2075 CXCursorKind Kind = clang_getCursorKind(cursor);
2076 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2077 // Okay: We can annotate the location of this declaration with the
2078 // declaration or reference
2079 } else if (clang_isExpression(cursor.kind)) {
2080 if (Kind != CXCursor_DeclRefExpr &&
2081 Kind != CXCursor_MemberRefExpr &&
2082 Kind != CXCursor_ObjCMessageExpr)
2083 return CXChildVisit_Recurse;
2084
2085 CXCursor Referenced = clang_getCursorReferenced(cursor);
2086 if (Referenced == cursor || Referenced == clang_getNullCursor())
2087 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002088
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002089 // Okay: we can annotate the location of this expression
2090 } else {
2091 // Nothing to annotate
2092 return CXChildVisit_Recurse;
2093 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002094
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002095 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2096 (*Data)[Loc.int_data] = cursor;
2097 return CXChildVisit_Recurse;
2098}
2099
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002100void clang_annotateTokens(CXTranslationUnit TU,
2101 CXToken *Tokens, unsigned NumTokens,
2102 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002103 if (NumTokens == 0)
2104 return;
2105
2106 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002107 for (unsigned I = 0; I != NumTokens; ++I)
2108 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002109
2110 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2111 if (!CXXUnit || !Tokens)
2112 return;
2113
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002114 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002115 SourceRange RegionOfInterest;
2116 RegionOfInterest.setBegin(
2117 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2118 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002119 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002120 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002121 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002122 // FIXME: Would be great to have a "hint" cursor, then walk from that
2123 // hint cursor upward until we find a cursor whose source range encloses
2124 // the region of interest, rather than starting from the translation unit.
2125 AnnotateTokensData Annotated;
2126 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002127 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002128 Decl::MaxPCHLevel, RegionOfInterest);
2129 AnnotateVis.VisitChildren(Parent);
2130
2131 for (unsigned I = 0; I != NumTokens; ++I) {
2132 // Determine whether we saw a cursor at this token's location.
2133 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2134 if (Pos == Annotated.end())
2135 continue;
2136
2137 Cursors[I] = Pos->second;
2138 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002139}
2140
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002141void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002142 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002143 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002144}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002145
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002146} // end: extern "C"
2147
2148//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002149// CXString Operations.
2150//===----------------------------------------------------------------------===//
2151
2152extern "C" {
2153const char *clang_getCString(CXString string) {
2154 return string.Spelling;
2155}
2156
2157void clang_disposeString(CXString string) {
2158 if (string.MustFreeString && string.Spelling)
2159 free((void*)string.Spelling);
2160}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002161
Ted Kremenekfb480492010-01-13 21:46:36 +00002162} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002163
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002164namespace clang { namespace cxstring {
2165CXString createCXString(const char *String, bool DupString){
2166 CXString Str;
2167 if (DupString) {
2168 Str.Spelling = strdup(String);
2169 Str.MustFreeString = 1;
2170 } else {
2171 Str.Spelling = String;
2172 Str.MustFreeString = 0;
2173 }
2174 return Str;
2175}
2176
2177CXString createCXString(llvm::StringRef String, bool DupString) {
2178 CXString Result;
2179 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2180 char *Spelling = (char *)malloc(String.size() + 1);
2181 memmove(Spelling, String.data(), String.size());
2182 Spelling[String.size()] = 0;
2183 Result.Spelling = Spelling;
2184 Result.MustFreeString = 1;
2185 } else {
2186 Result.Spelling = String.data();
2187 Result.MustFreeString = 0;
2188 }
2189 return Result;
2190}
2191}}
2192
Ted Kremenek04bb7162010-01-22 22:44:15 +00002193//===----------------------------------------------------------------------===//
2194// Misc. utility functions.
2195//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002196
Ted Kremenek04bb7162010-01-22 22:44:15 +00002197extern "C" {
2198
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002199CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002200 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002201}
2202
2203} // end: extern "C"