blob: 0b08874709070b1bebda51505b39b2121639bc2b [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
Ted Kremenek09dfa372010-02-18 05:46:33 +0000257 bool VisitAttributes(Decl *D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000258 bool VisitDeclContext(DeclContext *DC);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000259 bool VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000260 bool VisitTypedefDecl(TypedefDecl *D);
261 bool VisitTagDecl(TagDecl *D);
262 bool VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000263 bool VisitDeclaratorDecl(DeclaratorDecl *DD);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000264 bool VisitFunctionDecl(FunctionDecl *ND);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000265 bool VisitFieldDecl(FieldDecl *D);
266 bool VisitVarDecl(VarDecl *);
267 bool VisitObjCMethodDecl(ObjCMethodDecl *ND);
Douglas Gregora59e3902010-01-21 23:27:09 +0000268 bool VisitObjCContainerDecl(ObjCContainerDecl *D);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000269 bool VisitObjCCategoryDecl(ObjCCategoryDecl *ND);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000270 bool VisitObjCProtocolDecl(ObjCProtocolDecl *PID);
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000271 bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
272 bool VisitObjCImplDecl(ObjCImplDecl *D);
273 bool VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
274 bool VisitObjCImplementationDecl(ObjCImplementationDecl *D);
275 // FIXME: ObjCPropertyDecl requires TypeSourceInfo, getter/setter locations,
276 // etc.
277 // FIXME: ObjCCompatibleAliasDecl requires aliased-class locations.
278 bool VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
279 bool VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000280
281 // Type visitors
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000282 // FIXME: QualifiedTypeLoc doesn't provide any location information
283 bool VisitBuiltinTypeLoc(BuiltinTypeLoc TL);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000284 bool VisitTypedefTypeLoc(TypedefTypeLoc TL);
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000285 bool VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL);
286 bool VisitTagTypeLoc(TagTypeLoc TL);
287 // FIXME: TemplateTypeParmTypeLoc doesn't provide any location information
288 bool VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL);
289 bool VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL);
290 bool VisitPointerTypeLoc(PointerTypeLoc TL);
291 bool VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL);
292 bool VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL);
293 bool VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL);
294 bool VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL);
295 bool VisitFunctionTypeLoc(FunctionTypeLoc TL);
296 bool VisitArrayTypeLoc(ArrayTypeLoc TL);
Douglas Gregor2332c112010-01-21 20:48:56 +0000297 // FIXME: Implement for TemplateSpecializationTypeLoc
298 // FIXME: Implement visitors here when the unimplemented TypeLocs get
299 // implemented
300 bool VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL);
301 bool VisitTypeOfTypeLoc(TypeOfTypeLoc TL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000302
Douglas Gregora59e3902010-01-21 23:27:09 +0000303 // Statement visitors
304 bool VisitStmt(Stmt *S);
305 bool VisitDeclStmt(DeclStmt *S);
Douglas Gregorf5bab412010-01-22 01:00:11 +0000306 // FIXME: LabelStmt label?
307 bool VisitIfStmt(IfStmt *S);
308 bool VisitSwitchStmt(SwitchStmt *S);
Douglas Gregor263b47b2010-01-25 16:12:32 +0000309 bool VisitWhileStmt(WhileStmt *S);
310 bool VisitForStmt(ForStmt *S);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000311
Douglas Gregor336fd812010-01-23 00:40:08 +0000312 // Expression visitors
313 bool VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
314 bool VisitExplicitCastExpr(ExplicitCastExpr *E);
315 bool VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Steve Naroff89922f82009-08-31 00:59:03 +0000316};
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000317
Ted Kremenekab188932010-01-05 19:32:54 +0000318} // end anonymous namespace
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000319
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000320RangeComparisonResult CursorVisitor::CompareRegionOfInterest(SourceRange R) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000321 return RangeCompare(TU->getSourceManager(), R, RegionOfInterest);
322}
323
Douglas Gregorb1373d02010-01-20 20:59:29 +0000324/// \brief Visit the given cursor and, if requested by the visitor,
325/// its children.
326///
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000327/// \param Cursor the cursor to visit.
328///
329/// \param CheckRegionOfInterest if true, then the caller already checked that
330/// this cursor is within the region of interest.
331///
Douglas Gregorb1373d02010-01-20 20:59:29 +0000332/// \returns true if the visitation should be aborted, false if it
333/// should continue.
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000334bool CursorVisitor::Visit(CXCursor Cursor, bool CheckedRegionOfInterest) {
Douglas Gregorb1373d02010-01-20 20:59:29 +0000335 if (clang_isInvalid(Cursor.kind))
336 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000337
Douglas Gregorb1373d02010-01-20 20:59:29 +0000338 if (clang_isDeclaration(Cursor.kind)) {
339 Decl *D = getCursorDecl(Cursor);
340 assert(D && "Invalid declaration cursor");
341 if (D->getPCHLevel() > MaxPCHLevel)
342 return false;
343
344 if (D->isImplicit())
345 return false;
346 }
347
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000348 // If we have a range of interest, and this cursor doesn't intersect with it,
349 // we're done.
350 if (RegionOfInterest.isValid() && !CheckedRegionOfInterest) {
Daniel Dunbarf408f322010-02-14 08:32:05 +0000351 SourceRange Range =
352 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
353 if (Range.isInvalid() || CompareRegionOfInterest(Range))
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000354 return false;
355 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000356
Douglas Gregorb1373d02010-01-20 20:59:29 +0000357 switch (Visitor(Cursor, Parent, ClientData)) {
358 case CXChildVisit_Break:
359 return true;
360
361 case CXChildVisit_Continue:
362 return false;
363
364 case CXChildVisit_Recurse:
365 return VisitChildren(Cursor);
366 }
367
Douglas Gregorfd643772010-01-25 16:45:46 +0000368 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000369}
370
371/// \brief Visit the children of the given cursor.
372///
373/// \returns true if the visitation should be aborted, false if it
374/// should continue.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000375bool CursorVisitor::VisitChildren(CXCursor Cursor) {
Douglas Gregora59e3902010-01-21 23:27:09 +0000376 if (clang_isReference(Cursor.kind)) {
377 // By definition, references have no children.
378 return false;
379 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000380
381 // Set the Parent field to Cursor, then back to its old value once we're
Douglas Gregorb1373d02010-01-20 20:59:29 +0000382 // done.
383 class SetParentRAII {
384 CXCursor &Parent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000385 Decl *&StmtParent;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000386 CXCursor OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000387
Douglas Gregorb1373d02010-01-20 20:59:29 +0000388 public:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000389 SetParentRAII(CXCursor &Parent, Decl *&StmtParent, CXCursor NewParent)
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000390 : Parent(Parent), StmtParent(StmtParent), OldParent(Parent)
Douglas Gregorb1373d02010-01-20 20:59:29 +0000391 {
392 Parent = NewParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000393 if (clang_isDeclaration(Parent.kind))
394 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000395 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000396
Douglas Gregorb1373d02010-01-20 20:59:29 +0000397 ~SetParentRAII() {
398 Parent = OldParent;
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000399 if (clang_isDeclaration(Parent.kind))
400 StmtParent = getCursorDecl(Parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +0000401 }
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000402 } SetParent(Parent, StmtParent, Cursor);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000403
Douglas Gregorb1373d02010-01-20 20:59:29 +0000404 if (clang_isDeclaration(Cursor.kind)) {
405 Decl *D = getCursorDecl(Cursor);
406 assert(D && "Invalid declaration cursor");
407 return Visit(D);
408 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000409
Douglas Gregora59e3902010-01-21 23:27:09 +0000410 if (clang_isStatement(Cursor.kind))
411 return Visit(getCursorStmt(Cursor));
412 if (clang_isExpression(Cursor.kind))
413 return Visit(getCursorExpr(Cursor));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000414
Douglas Gregorb1373d02010-01-20 20:59:29 +0000415 if (clang_isTranslationUnit(Cursor.kind)) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000416 ASTUnit *CXXUnit = getCursorASTUnit(Cursor);
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000417 if (!CXXUnit->isMainFileAST() && CXXUnit->getOnlyLocalDecls() &&
418 RegionOfInterest.isInvalid()) {
Douglas Gregor7b691f332010-01-20 21:13:59 +0000419 const std::vector<Decl*> &TLDs = CXXUnit->getTopLevelDecls();
420 for (std::vector<Decl*>::const_iterator it = TLDs.begin(),
421 ie = TLDs.end(); it != ie; ++it) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000422 if (Visit(MakeCXCursor(*it, CXXUnit), true))
Douglas Gregor7b691f332010-01-20 21:13:59 +0000423 return true;
424 }
425 } else {
426 return VisitDeclContext(
Douglas Gregorb1373d02010-01-20 20:59:29 +0000427 CXXUnit->getASTContext().getTranslationUnitDecl());
Douglas Gregor7b691f332010-01-20 21:13:59 +0000428 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000429
Douglas Gregor7b691f332010-01-20 21:13:59 +0000430 return false;
Douglas Gregorb1373d02010-01-20 20:59:29 +0000431 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000432
Douglas Gregorb1373d02010-01-20 20:59:29 +0000433 // Nothing to visit at the moment.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000434 return false;
435}
436
Douglas Gregorb1373d02010-01-20 20:59:29 +0000437bool CursorVisitor::VisitDeclContext(DeclContext *DC) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000438 for (DeclContext::decl_iterator
Douglas Gregorb1373d02010-01-20 20:59:29 +0000439 I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I) {
Ted Kremenek09dfa372010-02-18 05:46:33 +0000440
441 // Attributes currently don't have source ranges. We only report them
442 // when the client hasn't specified a region of interest.
443 if (!RegionOfInterest.isValid())
444 if (VisitAttributes(*I))
445 return true;
446
Daniel Dunbard52864b2010-02-14 10:02:57 +0000447 CXCursor Cursor = MakeCXCursor(*I, TU);
448
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000449 if (RegionOfInterest.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +0000450 SourceRange Range =
451 cxloc::translateCXSourceRange(clang_getCursorExtent(Cursor));
452 if (Range.isInvalid())
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000453 continue;
Daniel Dunbard52864b2010-02-14 10:02:57 +0000454
455 switch (CompareRegionOfInterest(Range)) {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000456 case RangeBefore:
457 // This declaration comes before the region of interest; skip it.
458 continue;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000459
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000460 case RangeAfter:
461 // This declaration comes after the region of interest; we're done.
462 return false;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000463
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000464 case RangeOverlap:
465 // This declaration overlaps the region of interest; visit it.
466 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000467 }
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000468 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000469
Daniel Dunbard52864b2010-02-14 10:02:57 +0000470 if (Visit(Cursor, true))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000471 return true;
472 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000473
Douglas Gregorb1373d02010-01-20 20:59:29 +0000474 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000475}
476
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000477bool CursorVisitor::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
478 llvm_unreachable("Translation units are visited directly by Visit()");
479 return false;
480}
481
482bool CursorVisitor::VisitTypedefDecl(TypedefDecl *D) {
483 if (TypeSourceInfo *TSInfo = D->getTypeSourceInfo())
484 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000485
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000486 return false;
487}
488
489bool CursorVisitor::VisitTagDecl(TagDecl *D) {
490 return VisitDeclContext(D);
491}
492
493bool CursorVisitor::VisitEnumConstantDecl(EnumConstantDecl *D) {
494 if (Expr *Init = D->getInitExpr())
495 return Visit(MakeCXCursor(Init, StmtParent, TU));
496 return false;
497}
498
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000499bool CursorVisitor::VisitDeclaratorDecl(DeclaratorDecl *DD) {
500 if (TypeSourceInfo *TSInfo = DD->getTypeSourceInfo())
501 if (Visit(TSInfo->getTypeLoc()))
502 return true;
503
504 return false;
505}
506
Douglas Gregorb1373d02010-01-20 20:59:29 +0000507bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000508 if (VisitDeclaratorDecl(ND))
509 return true;
510
Douglas Gregora59e3902010-01-21 23:27:09 +0000511 if (ND->isThisDeclarationADefinition() &&
512 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
513 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000514
Douglas Gregorb1373d02010-01-20 20:59:29 +0000515 return false;
516}
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000517
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000518bool CursorVisitor::VisitFieldDecl(FieldDecl *D) {
519 if (VisitDeclaratorDecl(D))
520 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000521
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000522 if (Expr *BitWidth = D->getBitWidth())
523 return Visit(MakeCXCursor(BitWidth, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000524
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000525 return false;
526}
527
528bool CursorVisitor::VisitVarDecl(VarDecl *D) {
529 if (VisitDeclaratorDecl(D))
530 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000531
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000532 if (Expr *Init = D->getInit())
533 return Visit(MakeCXCursor(Init, StmtParent, TU));
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000534
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000535 return false;
536}
537
538bool CursorVisitor::VisitObjCMethodDecl(ObjCMethodDecl *ND) {
539 // FIXME: We really need a TypeLoc covering Objective-C method declarations.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000540 // At the moment, we don't have information about locations in the return
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000541 // type.
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000542 for (ObjCMethodDecl::param_iterator P = ND->param_begin(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000543 PEnd = ND->param_end();
544 P != PEnd; ++P) {
545 if (Visit(MakeCXCursor(*P, TU)))
546 return true;
547 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000548
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000549 if (ND->isThisDeclarationADefinition() &&
550 Visit(MakeCXCursor(ND->getBody(), StmtParent, TU)))
551 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000552
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000553 return false;
554}
555
Douglas Gregora59e3902010-01-21 23:27:09 +0000556bool CursorVisitor::VisitObjCContainerDecl(ObjCContainerDecl *D) {
557 return VisitDeclContext(D);
558}
559
Douglas Gregorb1373d02010-01-20 20:59:29 +0000560bool CursorVisitor::VisitObjCCategoryDecl(ObjCCategoryDecl *ND) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000561 if (Visit(MakeCursorObjCClassRef(ND->getClassInterface(), ND->getLocation(),
562 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000563 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000564
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000565 ObjCCategoryDecl::protocol_loc_iterator PL = ND->protocol_loc_begin();
566 for (ObjCCategoryDecl::protocol_iterator I = ND->protocol_begin(),
567 E = ND->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000568 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000569 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000570
Douglas Gregora59e3902010-01-21 23:27:09 +0000571 return VisitObjCContainerDecl(ND);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000572}
573
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000574bool CursorVisitor::VisitObjCProtocolDecl(ObjCProtocolDecl *PID) {
575 ObjCProtocolDecl::protocol_loc_iterator PL = PID->protocol_loc_begin();
576 for (ObjCProtocolDecl::protocol_iterator I = PID->protocol_begin(),
577 E = PID->protocol_end(); I != E; ++I, ++PL)
578 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
579 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000580
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000581 return VisitObjCContainerDecl(PID);
582}
583
Douglas Gregorb1373d02010-01-20 20:59:29 +0000584bool CursorVisitor::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000585 // Issue callbacks for super class.
Douglas Gregorb1373d02010-01-20 20:59:29 +0000586 if (D->getSuperClass() &&
587 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000588 D->getSuperClassLoc(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000589 TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000590 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000591
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000592 ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
593 for (ObjCInterfaceDecl::protocol_iterator I = D->protocol_begin(),
594 E = D->protocol_end(); I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000595 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000596 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000597
Douglas Gregora59e3902010-01-21 23:27:09 +0000598 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000599}
600
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000601bool CursorVisitor::VisitObjCImplDecl(ObjCImplDecl *D) {
602 return VisitObjCContainerDecl(D);
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000603}
604
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000605bool CursorVisitor::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000606 if (Visit(MakeCursorObjCClassRef(D->getCategoryDecl()->getClassInterface(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000607 D->getLocation(), TU)))
608 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000609
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000610 return VisitObjCImplDecl(D);
611}
612
613bool CursorVisitor::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
614#if 0
615 // Issue callbacks for super class.
616 // FIXME: No source location information!
617 if (D->getSuperClass() &&
618 Visit(MakeCursorObjCSuperClassRef(D->getSuperClass(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000619 D->getSuperClassLoc(),
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000620 TU)))
621 return true;
622#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000623
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000624 return VisitObjCImplDecl(D);
625}
626
627bool CursorVisitor::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
628 ObjCForwardProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin();
629 for (ObjCForwardProtocolDecl::protocol_iterator I = D->protocol_begin(),
630 E = D->protocol_end();
631 I != E; ++I, ++PL)
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000632 if (Visit(MakeCursorObjCProtocolRef(*I, *PL, TU)))
Douglas Gregorb1373d02010-01-20 20:59:29 +0000633 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000634
635 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000636}
637
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000638bool CursorVisitor::VisitObjCClassDecl(ObjCClassDecl *D) {
639 for (ObjCClassDecl::iterator C = D->begin(), CEnd = D->end(); C != CEnd; ++C)
640 if (Visit(MakeCursorObjCClassRef(C->getInterface(), C->getLocation(), TU)))
641 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000642
Douglas Gregor1ef2fc12010-01-22 00:50:27 +0000643 return false;
Ted Kremenekdd6bcc52010-01-13 00:22:49 +0000644}
645
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000646bool CursorVisitor::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
647 ASTContext &Context = TU->getASTContext();
648
649 // Some builtin types (such as Objective-C's "id", "sel", and
650 // "Class") have associated declarations. Create cursors for those.
651 QualType VisitType;
652 switch (TL.getType()->getAs<BuiltinType>()->getKind()) {
653 case BuiltinType::Void:
654 case BuiltinType::Bool:
655 case BuiltinType::Char_U:
656 case BuiltinType::UChar:
657 case BuiltinType::Char16:
658 case BuiltinType::Char32:
659 case BuiltinType::UShort:
660 case BuiltinType::UInt:
661 case BuiltinType::ULong:
662 case BuiltinType::ULongLong:
663 case BuiltinType::UInt128:
664 case BuiltinType::Char_S:
665 case BuiltinType::SChar:
666 case BuiltinType::WChar:
667 case BuiltinType::Short:
668 case BuiltinType::Int:
669 case BuiltinType::Long:
670 case BuiltinType::LongLong:
671 case BuiltinType::Int128:
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000672 case BuiltinType::Float:
673 case BuiltinType::Double:
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000674 case BuiltinType::LongDouble:
675 case BuiltinType::NullPtr:
676 case BuiltinType::Overload:
677 case BuiltinType::Dependent:
678 break;
679
680 case BuiltinType::UndeducedAuto: // FIXME: Deserves a cursor?
681 break;
682
683 case BuiltinType::ObjCId:
684 VisitType = Context.getObjCIdType();
685 break;
686
687 case BuiltinType::ObjCClass:
688 VisitType = Context.getObjCClassType();
689 break;
690
691 case BuiltinType::ObjCSel:
692 VisitType = Context.getObjCSelType();
693 break;
694 }
695
696 if (!VisitType.isNull()) {
697 if (const TypedefType *Typedef = VisitType->getAs<TypedefType>())
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000698 return Visit(MakeCursorTypeRef(Typedef->getDecl(), TL.getBuiltinLoc(),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000699 TU));
700 }
701
702 return false;
703}
704
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000705bool CursorVisitor::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
706 return Visit(MakeCursorTypeRef(TL.getTypedefDecl(), TL.getNameLoc(), TU));
707}
708
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000709bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
710 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
711}
712
713bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) {
714 return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU));
715}
716
717bool CursorVisitor::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
718 if (Visit(MakeCursorObjCClassRef(TL.getIFaceDecl(), TL.getNameLoc(), TU)))
719 return true;
720
721 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
722 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I), TL.getProtocolLoc(I),
723 TU)))
724 return true;
725 }
726
727 return false;
728}
729
730bool CursorVisitor::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
731 if (TL.hasBaseTypeAsWritten() && Visit(TL.getBaseTypeLoc()))
732 return true;
733
734 if (TL.hasProtocolsAsWritten()) {
735 for (unsigned I = 0, N = TL.getNumProtocols(); I != N; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000736 if (Visit(MakeCursorObjCProtocolRef(TL.getProtocol(I),
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000737 TL.getProtocolLoc(I),
738 TU)))
739 return true;
740 }
741 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000742
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000743 return false;
744}
745
746bool CursorVisitor::VisitPointerTypeLoc(PointerTypeLoc TL) {
747 return Visit(TL.getPointeeLoc());
748}
749
750bool CursorVisitor::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
751 return Visit(TL.getPointeeLoc());
752}
753
754bool CursorVisitor::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
755 return Visit(TL.getPointeeLoc());
756}
757
758bool CursorVisitor::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000759 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000760}
761
762bool CursorVisitor::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000763 return Visit(TL.getPointeeLoc());
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000764}
765
766bool CursorVisitor::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
767 if (Visit(TL.getResultLoc()))
768 return true;
769
Douglas Gregorf20dfbc2010-01-21 17:29:07 +0000770 for (unsigned I = 0, N = TL.getNumArgs(); I != N; ++I)
771 if (Visit(MakeCXCursor(TL.getArg(I), TU)))
772 return true;
773
774 return false;
775}
776
777bool CursorVisitor::VisitArrayTypeLoc(ArrayTypeLoc TL) {
778 if (Visit(TL.getElementLoc()))
779 return true;
780
781 if (Expr *Size = TL.getSizeExpr())
782 return Visit(MakeCXCursor(Size, StmtParent, TU));
783
784 return false;
785}
786
Douglas Gregor2332c112010-01-21 20:48:56 +0000787bool CursorVisitor::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
788 return Visit(MakeCXCursor(TL.getUnderlyingExpr(), StmtParent, TU));
789}
790
791bool CursorVisitor::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
792 if (TypeSourceInfo *TSInfo = TL.getUnderlyingTInfo())
793 return Visit(TSInfo->getTypeLoc());
794
795 return false;
796}
797
Douglas Gregora59e3902010-01-21 23:27:09 +0000798bool CursorVisitor::VisitStmt(Stmt *S) {
799 for (Stmt::child_iterator Child = S->child_begin(), ChildEnd = S->child_end();
800 Child != ChildEnd; ++Child) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000801 if (*Child && Visit(MakeCXCursor(*Child, StmtParent, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000802 return true;
803 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000804
Douglas Gregora59e3902010-01-21 23:27:09 +0000805 return false;
806}
807
808bool CursorVisitor::VisitDeclStmt(DeclStmt *S) {
809 for (DeclStmt::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
810 D != DEnd; ++D) {
Douglas Gregor263b47b2010-01-25 16:12:32 +0000811 if (*D && Visit(MakeCXCursor(*D, TU)))
Douglas Gregora59e3902010-01-21 23:27:09 +0000812 return true;
813 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000814
Douglas Gregora59e3902010-01-21 23:27:09 +0000815 return false;
816}
817
Douglas Gregorf5bab412010-01-22 01:00:11 +0000818bool CursorVisitor::VisitIfStmt(IfStmt *S) {
819 if (VarDecl *Var = S->getConditionVariable()) {
820 if (Visit(MakeCXCursor(Var, TU)))
821 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000822 }
823
Douglas Gregor263b47b2010-01-25 16:12:32 +0000824 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
825 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000826 if (S->getThen() && Visit(MakeCXCursor(S->getThen(), StmtParent, TU)))
827 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000828 if (S->getElse() && Visit(MakeCXCursor(S->getElse(), StmtParent, TU)))
829 return true;
830
831 return false;
832}
833
834bool CursorVisitor::VisitSwitchStmt(SwitchStmt *S) {
835 if (VarDecl *Var = S->getConditionVariable()) {
836 if (Visit(MakeCXCursor(Var, TU)))
837 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000838 }
839
Douglas Gregor263b47b2010-01-25 16:12:32 +0000840 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
841 return true;
842 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
843 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000844
Douglas Gregor263b47b2010-01-25 16:12:32 +0000845 return false;
846}
847
848bool CursorVisitor::VisitWhileStmt(WhileStmt *S) {
849 if (VarDecl *Var = S->getConditionVariable()) {
850 if (Visit(MakeCXCursor(Var, TU)))
851 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000852 }
853
Douglas Gregor263b47b2010-01-25 16:12:32 +0000854 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
855 return true;
856 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
Douglas Gregorf5bab412010-01-22 01:00:11 +0000857 return true;
858
Douglas Gregor263b47b2010-01-25 16:12:32 +0000859 return false;
860}
861
862bool CursorVisitor::VisitForStmt(ForStmt *S) {
863 if (S->getInit() && Visit(MakeCXCursor(S->getInit(), StmtParent, TU)))
864 return true;
865 if (VarDecl *Var = S->getConditionVariable()) {
866 if (Visit(MakeCXCursor(Var, TU)))
867 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000868 }
869
Douglas Gregor263b47b2010-01-25 16:12:32 +0000870 if (S->getCond() && Visit(MakeCXCursor(S->getCond(), StmtParent, TU)))
871 return true;
872 if (S->getInc() && Visit(MakeCXCursor(S->getInc(), StmtParent, TU)))
873 return true;
Douglas Gregorf5bab412010-01-22 01:00:11 +0000874 if (S->getBody() && Visit(MakeCXCursor(S->getBody(), StmtParent, TU)))
875 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000876
Douglas Gregorf5bab412010-01-22 01:00:11 +0000877 return false;
878}
879
Douglas Gregor336fd812010-01-23 00:40:08 +0000880bool CursorVisitor::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
881 if (E->isArgumentType()) {
882 if (TypeSourceInfo *TSInfo = E->getArgumentTypeInfo())
883 return Visit(TSInfo->getTypeLoc());
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000884
Douglas Gregor336fd812010-01-23 00:40:08 +0000885 return false;
886 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000887
Douglas Gregor336fd812010-01-23 00:40:08 +0000888 return VisitExpr(E);
889}
890
891bool CursorVisitor::VisitExplicitCastExpr(ExplicitCastExpr *E) {
892 if (TypeSourceInfo *TSInfo = E->getTypeInfoAsWritten())
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 VisitCastExpr(E);
897}
898
899bool CursorVisitor::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
900 if (TypeSourceInfo *TSInfo = E->getTypeSourceInfo())
901 if (Visit(TSInfo->getTypeLoc()))
902 return true;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000903
Douglas Gregor336fd812010-01-23 00:40:08 +0000904 return VisitExpr(E);
905}
906
Ted Kremenek09dfa372010-02-18 05:46:33 +0000907bool CursorVisitor::VisitAttributes(Decl *D) {
908 for (const Attr *A = D->getAttrs(); A; A = A->getNext())
909 if (Visit(MakeCXCursor(A, D, TU)))
910 return true;
911
912 return false;
913}
914
Benjamin Kramer5e4bc592009-10-18 16:11:04 +0000915extern "C" {
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000916CXIndex clang_createIndex(int excludeDeclarationsFromPCH) {
Douglas Gregora030b7c2010-01-22 20:35:53 +0000917 CIndexer *CIdxr = new CIndexer();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000918 if (excludeDeclarationsFromPCH)
919 CIdxr->setOnlyLocalDecls();
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000920 return CIdxr;
Steve Naroff600866c2009-08-27 19:51:58 +0000921}
922
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000923void clang_disposeIndex(CXIndex CIdx) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000924 if (CIdx)
925 delete static_cast<CIndexer *>(CIdx);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +0000926}
927
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000928void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000929 if (CIdx) {
930 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
931 CXXIdx->setUseExternalASTGeneration(value);
932 }
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000933}
934
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000935CXTranslationUnit clang_createTranslationUnit(CXIndex CIdx,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000936 const char *ast_filename,
937 CXDiagnosticCallback diag_callback,
938 CXClientData diag_client_data) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000939 if (!CIdx)
940 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000941
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000942 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +0000943
Douglas Gregor5352ac02010-01-28 00:27:43 +0000944 // Configure the diagnostics.
945 DiagnosticOptions DiagOpts;
946 llvm::OwningPtr<Diagnostic> Diags;
947 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
948 CIndexDiagnosticClient DiagClient(diag_callback, diag_client_data);
949 Diags->setClient(&DiagClient);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000950
Douglas Gregor5352ac02010-01-28 00:27:43 +0000951 return ASTUnit::LoadFromPCHFile(ast_filename, *Diags,
Daniel Dunbarb26d4832010-02-16 01:55:04 +0000952 CXXIdx->getOnlyLocalDecls());
Steve Naroff600866c2009-08-27 19:51:58 +0000953}
954
Daniel Dunbar9ebfa312009-12-01 03:14:51 +0000955CXTranslationUnit
956clang_createTranslationUnitFromSourceFile(CXIndex CIdx,
957 const char *source_filename,
958 int num_command_line_args,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000959 const char **command_line_args,
960 unsigned num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000961 struct CXUnsavedFile *unsaved_files,
962 CXDiagnosticCallback diag_callback,
963 CXClientData diag_client_data) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +0000964 if (!CIdx)
965 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000966
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000967 CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
968
Douglas Gregor5352ac02010-01-28 00:27:43 +0000969 // Configure the diagnostics.
970 DiagnosticOptions DiagOpts;
971 llvm::OwningPtr<Diagnostic> Diags;
972 Diags.reset(CompilerInstance::createDiagnostics(DiagOpts, 0, 0));
973 CIndexDiagnosticClient DiagClient(diag_callback, diag_client_data);
974 Diags->setClient(&DiagClient);
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000975
Douglas Gregor4db64a42010-01-23 00:14:00 +0000976 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
977 for (unsigned I = 0; I != num_unsaved_files; ++I) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000978 const llvm::MemoryBuffer *Buffer
Douglas Gregor4db64a42010-01-23 00:14:00 +0000979 = llvm::MemoryBuffer::getMemBuffer(unsaved_files[I].Contents,
980 unsaved_files[I].Contents + unsaved_files[I].Length,
981 unsaved_files[I].Filename);
982 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
983 Buffer));
984 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000985
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000986 if (!CXXIdx->getUseExternalASTGeneration()) {
987 llvm::SmallVector<const char *, 16> Args;
988
989 // The 'source_filename' argument is optional. If the caller does not
990 // specify it then it is assumed that the source file is specified
991 // in the actual argument list.
992 if (source_filename)
993 Args.push_back(source_filename);
994 Args.insert(Args.end(), command_line_args,
995 command_line_args + num_command_line_args);
996
Douglas Gregor5352ac02010-01-28 00:27:43 +0000997 unsigned NumErrors = Diags->getNumErrors();
Ted Kremenekf0e23e82010-02-17 00:41:40 +0000998
Ted Kremenek29b72842010-01-07 22:49:05 +0000999#ifdef USE_CRASHTRACER
1000 ArgsCrashTracerInfo ACTI(Args);
Ted Kremenek8a8da7d2010-01-06 03:42:32 +00001001#endif
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001002
Daniel Dunbar94220972009-12-05 02:17:18 +00001003 llvm::OwningPtr<ASTUnit> Unit(
1004 ASTUnit::LoadFromCommandLine(Args.data(), Args.data() + Args.size(),
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001005 *Diags,
Daniel Dunbar869824e2009-12-13 03:46:13 +00001006 CXXIdx->getClangResourcesPath(),
Daniel Dunbar94220972009-12-05 02:17:18 +00001007 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001008 RemappedFiles.data(),
1009 RemappedFiles.size()));
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001010
Daniel Dunbar94220972009-12-05 02:17:18 +00001011 // FIXME: Until we have broader testing, just drop the entire AST if we
1012 // encountered an error.
Douglas Gregor5352ac02010-01-28 00:27:43 +00001013 if (NumErrors != Diags->getNumErrors())
Daniel Dunbar94220972009-12-05 02:17:18 +00001014 return 0;
1015
1016 return Unit.take();
Daniel Dunbar8506dde2009-12-03 01:54:28 +00001017 }
1018
Ted Kremenek139ba862009-10-22 00:03:57 +00001019 // Build up the arguments for invoking 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001020 std::vector<const char *> argv;
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001021
Ted Kremenek139ba862009-10-22 00:03:57 +00001022 // First add the complete path to the 'clang' executable.
1023 llvm::sys::Path ClangPath = static_cast<CIndexer *>(CIdx)->getClangPath();
Benjamin Kramer5e4bc592009-10-18 16:11:04 +00001024 argv.push_back(ClangPath.c_str());
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001025
Ted Kremenek139ba862009-10-22 00:03:57 +00001026 // Add the '-emit-ast' option as our execution mode for 'clang'.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001027 argv.push_back("-emit-ast");
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001028
Ted Kremenek139ba862009-10-22 00:03:57 +00001029 // The 'source_filename' argument is optional. If the caller does not
1030 // specify it then it is assumed that the source file is specified
1031 // in the actual argument list.
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001032 if (source_filename)
1033 argv.push_back(source_filename);
Ted Kremenek139ba862009-10-22 00:03:57 +00001034
Steve Naroff37b5ac22009-10-15 20:50:09 +00001035 // Generate a temporary name for the AST file.
Ted Kremenek139ba862009-10-22 00:03:57 +00001036 argv.push_back("-o");
Steve Naroff37b5ac22009-10-15 20:50:09 +00001037 char astTmpFile[L_tmpnam];
Ted Kremenek74cd0692009-10-15 23:21:22 +00001038 argv.push_back(tmpnam(astTmpFile));
Ted Kremenek139ba862009-10-22 00:03:57 +00001039
Douglas Gregor4db64a42010-01-23 00:14:00 +00001040 // Remap any unsaved files to temporary files.
1041 std::vector<llvm::sys::Path> TemporaryFiles;
1042 std::vector<std::string> RemapArgs;
1043 if (RemapFiles(num_unsaved_files, unsaved_files, RemapArgs, TemporaryFiles))
1044 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001045
Douglas Gregor4db64a42010-01-23 00:14:00 +00001046 // The pointers into the elements of RemapArgs are stable because we
1047 // won't be adding anything to RemapArgs after this point.
1048 for (unsigned i = 0, e = RemapArgs.size(); i != e; ++i)
1049 argv.push_back(RemapArgs[i].c_str());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001050
Ted Kremenek139ba862009-10-22 00:03:57 +00001051 // Process the compiler options, stripping off '-o', '-c', '-fsyntax-only'.
1052 for (int i = 0; i < num_command_line_args; ++i)
1053 if (const char *arg = command_line_args[i]) {
1054 if (strcmp(arg, "-o") == 0) {
1055 ++i; // Also skip the matching argument.
1056 continue;
1057 }
1058 if (strcmp(arg, "-emit-ast") == 0 ||
1059 strcmp(arg, "-c") == 0 ||
1060 strcmp(arg, "-fsyntax-only") == 0) {
1061 continue;
1062 }
1063
1064 // Keep the argument.
1065 argv.push_back(arg);
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001066 }
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001067
Douglas Gregord93256e2010-01-28 06:00:51 +00001068 // Generate a temporary name for the diagnostics file.
1069 char tmpFileResults[L_tmpnam];
1070 char *tmpResultsFileName = tmpnam(tmpFileResults);
1071 llvm::sys::Path DiagnosticsFile(tmpResultsFileName);
1072 TemporaryFiles.push_back(DiagnosticsFile);
1073 argv.push_back("-fdiagnostics-binary");
1074
Ted Kremenek139ba862009-10-22 00:03:57 +00001075 // Add the null terminator.
Ted Kremenek74cd0692009-10-15 23:21:22 +00001076 argv.push_back(NULL);
1077
Ted Kremenekfeb15e32009-10-26 22:14:08 +00001078 // Invoke 'clang'.
1079 llvm::sys::Path DevNull; // leave empty, causes redirection to /dev/null
1080 // on Unix or NUL (Windows).
Ted Kremenek379afec2009-10-22 03:24:01 +00001081 std::string ErrMsg;
Douglas Gregord93256e2010-01-28 06:00:51 +00001082 const llvm::sys::Path *Redirects[] = { &DevNull, &DevNull, &DiagnosticsFile,
1083 NULL };
Ted Kremenek379afec2009-10-22 03:24:01 +00001084 llvm::sys::Program::ExecuteAndWait(ClangPath, &argv[0], /* env */ NULL,
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001085 /* redirects */ &Redirects[0],
Ted Kremenek379afec2009-10-22 03:24:01 +00001086 /* secondsToWait */ 0, /* memoryLimits */ 0, &ErrMsg);
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001087
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001088 if (!ErrMsg.empty()) {
1089 std::string AllArgs;
Ted Kremenek379afec2009-10-22 03:24:01 +00001090 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001091 I != E; ++I) {
1092 AllArgs += ' ';
Ted Kremenek779e5f42009-10-26 22:08:39 +00001093 if (*I)
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001094 AllArgs += *I;
Ted Kremenek779e5f42009-10-26 22:08:39 +00001095 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001096
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001097 Diags->Report(diag::err_fe_clang) << AllArgs << ErrMsg;
Ted Kremenek379afec2009-10-22 03:24:01 +00001098 }
Benjamin Kramer0829a832009-10-18 11:19:36 +00001099
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001100 // FIXME: Parse the (redirected) standard error to emit diagnostics.
1101
Douglas Gregor5352ac02010-01-28 00:27:43 +00001102 ASTUnit *ATU = ASTUnit::LoadFromPCHFile(astTmpFile, *Diags,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001103 CXXIdx->getOnlyLocalDecls(),
Douglas Gregor4db64a42010-01-23 00:14:00 +00001104 RemappedFiles.data(),
1105 RemappedFiles.size());
Steve Naroffe56b4ba2009-10-20 14:46:24 +00001106 if (ATU)
1107 ATU->unlinkTemporaryFile();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001108
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001109 // FIXME: Currently we don't report diagnostics on invalid ASTs.
1110 if (ATU)
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001111 ReportSerializedDiagnostics(DiagnosticsFile, *Diags,
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001112 num_unsaved_files, unsaved_files,
1113 ATU->getASTContext().getLangOptions());
Douglas Gregord93256e2010-01-28 06:00:51 +00001114
Douglas Gregor4db64a42010-01-23 00:14:00 +00001115 for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i)
1116 TemporaryFiles[i].eraseFromDisk();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001117
Steve Naroffe19944c2009-10-15 22:23:48 +00001118 return ATU;
Steve Naroff5b7d8e22009-10-15 20:04:39 +00001119}
1120
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001121void clang_disposeTranslationUnit(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001122 if (CTUnit)
1123 delete static_cast<ASTUnit *>(CTUnit);
Steve Naroff2bd6b9f2009-09-17 18:33:27 +00001124}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001125
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001126CXString clang_getTranslationUnitSpelling(CXTranslationUnit CTUnit) {
Douglas Gregor2b37c9e2010-01-29 00:47:48 +00001127 if (!CTUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001128 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001129
Steve Naroff77accc12009-09-03 18:19:54 +00001130 ASTUnit *CXXUnit = static_cast<ASTUnit *>(CTUnit);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001131 return createCXString(CXXUnit->getOriginalSourceFileName(), true);
Steve Naroffaf08ddc2009-09-03 15:49:00 +00001132}
Daniel Dunbar1eb79b52009-08-28 16:30:07 +00001133
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001134CXCursor clang_getTranslationUnitCursor(CXTranslationUnit TU) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001135 CXCursor Result = { CXCursor_TranslationUnit, { 0, 0, TU } };
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001136 return Result;
1137}
1138
Ted Kremenekfb480492010-01-13 21:46:36 +00001139} // end: extern "C"
Steve Naroff600866c2009-08-27 19:51:58 +00001140
Ted Kremenekfb480492010-01-13 21:46:36 +00001141//===----------------------------------------------------------------------===//
Douglas Gregor1db19de2010-01-19 21:36:55 +00001142// CXSourceLocation and CXSourceRange Operations.
1143//===----------------------------------------------------------------------===//
1144
Douglas Gregorb9790342010-01-22 21:44:22 +00001145extern "C" {
1146CXSourceLocation clang_getNullLocation() {
Douglas Gregor5352ac02010-01-28 00:27:43 +00001147 CXSourceLocation Result = { { 0, 0 }, 0 };
Douglas Gregorb9790342010-01-22 21:44:22 +00001148 return Result;
1149}
1150
1151unsigned clang_equalLocations(CXSourceLocation loc1, CXSourceLocation loc2) {
Daniel Dunbar90a6b9e2010-01-30 23:58:27 +00001152 return (loc1.ptr_data[0] == loc2.ptr_data[0] &&
1153 loc1.ptr_data[1] == loc2.ptr_data[1] &&
1154 loc1.int_data == loc2.int_data);
Douglas Gregorb9790342010-01-22 21:44:22 +00001155}
1156
1157CXSourceLocation clang_getLocation(CXTranslationUnit tu,
1158 CXFile file,
1159 unsigned line,
1160 unsigned column) {
1161 if (!tu)
1162 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001163
Douglas Gregorb9790342010-01-22 21:44:22 +00001164 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
1165 SourceLocation SLoc
1166 = CXXUnit->getSourceManager().getLocation(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001167 static_cast<const FileEntry *>(file),
Douglas Gregorb9790342010-01-22 21:44:22 +00001168 line, column);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001169
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001170 return cxloc::translateSourceLocation(CXXUnit->getASTContext(), SLoc);
Douglas Gregorb9790342010-01-22 21:44:22 +00001171}
1172
Douglas Gregor5352ac02010-01-28 00:27:43 +00001173CXSourceRange clang_getNullRange() {
1174 CXSourceRange Result = { { 0, 0 }, 0, 0 };
1175 return Result;
1176}
Daniel Dunbard52864b2010-02-14 10:02:57 +00001177
Douglas Gregor5352ac02010-01-28 00:27:43 +00001178CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {
1179 if (begin.ptr_data[0] != end.ptr_data[0] ||
1180 begin.ptr_data[1] != end.ptr_data[1])
1181 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001182
1183 CXSourceRange Result = { { begin.ptr_data[0], begin.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001184 begin.int_data, end.int_data };
Douglas Gregorb9790342010-01-22 21:44:22 +00001185 return Result;
1186}
1187
Douglas Gregor46766dc2010-01-26 19:19:08 +00001188void clang_getInstantiationLocation(CXSourceLocation location,
1189 CXFile *file,
1190 unsigned *line,
1191 unsigned *column,
1192 unsigned *offset) {
Douglas Gregor1db19de2010-01-19 21:36:55 +00001193 SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);
1194
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001195 if (!location.ptr_data[0] || Loc.isInvalid()) {
Douglas Gregor46766dc2010-01-26 19:19:08 +00001196 if (file)
1197 *file = 0;
1198 if (line)
1199 *line = 0;
1200 if (column)
1201 *column = 0;
1202 if (offset)
1203 *offset = 0;
1204 return;
1205 }
1206
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001207 const SourceManager &SM =
1208 *static_cast<const SourceManager*>(location.ptr_data[0]);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001209 SourceLocation InstLoc = SM.getInstantiationLoc(Loc);
Douglas Gregor1db19de2010-01-19 21:36:55 +00001210
1211 if (file)
1212 *file = (void *)SM.getFileEntryForID(SM.getFileID(InstLoc));
1213 if (line)
1214 *line = SM.getInstantiationLineNumber(InstLoc);
1215 if (column)
1216 *column = SM.getInstantiationColumnNumber(InstLoc);
Douglas Gregore69517c2010-01-26 03:07:15 +00001217 if (offset)
Douglas Gregor46766dc2010-01-26 19:19:08 +00001218 *offset = SM.getDecomposedLoc(InstLoc).second;
Douglas Gregore69517c2010-01-26 03:07:15 +00001219}
1220
Douglas Gregor1db19de2010-01-19 21:36:55 +00001221CXSourceLocation clang_getRangeStart(CXSourceRange range) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001222 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001223 range.begin_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001224 return Result;
1225}
1226
1227CXSourceLocation clang_getRangeEnd(CXSourceRange range) {
Daniel Dunbarbb4a61a2010-02-14 01:47:36 +00001228 CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },
Douglas Gregor5352ac02010-01-28 00:27:43 +00001229 range.end_int_data };
Douglas Gregor1db19de2010-01-19 21:36:55 +00001230 return Result;
1231}
1232
Douglas Gregorb9790342010-01-22 21:44:22 +00001233} // end: extern "C"
1234
Douglas Gregor1db19de2010-01-19 21:36:55 +00001235//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00001236// CXFile Operations.
1237//===----------------------------------------------------------------------===//
1238
1239extern "C" {
Ted Kremenek74844072010-02-17 00:41:20 +00001240CXString clang_getFileName(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001241 if (!SFile)
Ted Kremenek74844072010-02-17 00:41:20 +00001242 return createCXString(NULL);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001243
Steve Naroff88145032009-10-27 14:35:18 +00001244 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
Ted Kremenek74844072010-02-17 00:41:20 +00001245 return createCXString(FEnt->getName());
Steve Naroff88145032009-10-27 14:35:18 +00001246}
1247
1248time_t clang_getFileTime(CXFile SFile) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001249 if (!SFile)
1250 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001251
Steve Naroff88145032009-10-27 14:35:18 +00001252 FileEntry *FEnt = static_cast<FileEntry *>(SFile);
1253 return FEnt->getModificationTime();
Steve Naroffee9405e2009-09-25 21:45:39 +00001254}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001255
Douglas Gregorb9790342010-01-22 21:44:22 +00001256CXFile clang_getFile(CXTranslationUnit tu, const char *file_name) {
1257 if (!tu)
1258 return 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001259
Douglas Gregorb9790342010-01-22 21:44:22 +00001260 ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001261
Douglas Gregorb9790342010-01-22 21:44:22 +00001262 FileManager &FMgr = CXXUnit->getFileManager();
1263 const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
1264 return const_cast<FileEntry *>(File);
1265}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001266
Ted Kremenekfb480492010-01-13 21:46:36 +00001267} // end: extern "C"
Steve Naroffee9405e2009-09-25 21:45:39 +00001268
Ted Kremenekfb480492010-01-13 21:46:36 +00001269//===----------------------------------------------------------------------===//
1270// CXCursor Operations.
1271//===----------------------------------------------------------------------===//
1272
Ted Kremenekfb480492010-01-13 21:46:36 +00001273static Decl *getDeclFromExpr(Stmt *E) {
1274 if (DeclRefExpr *RefExpr = dyn_cast<DeclRefExpr>(E))
1275 return RefExpr->getDecl();
1276 if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
1277 return ME->getMemberDecl();
1278 if (ObjCIvarRefExpr *RE = dyn_cast<ObjCIvarRefExpr>(E))
1279 return RE->getDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001280
Ted Kremenekfb480492010-01-13 21:46:36 +00001281 if (CallExpr *CE = dyn_cast<CallExpr>(E))
1282 return getDeclFromExpr(CE->getCallee());
1283 if (CastExpr *CE = dyn_cast<CastExpr>(E))
1284 return getDeclFromExpr(CE->getSubExpr());
1285 if (ObjCMessageExpr *OME = dyn_cast<ObjCMessageExpr>(E))
1286 return OME->getMethodDecl();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001287
Ted Kremenekfb480492010-01-13 21:46:36 +00001288 return 0;
1289}
1290
Daniel Dunbarc29f4c32010-02-02 05:00:22 +00001291static SourceLocation getLocationFromExpr(Expr *E) {
1292 if (ObjCMessageExpr *Msg = dyn_cast<ObjCMessageExpr>(E))
1293 return /*FIXME:*/Msg->getLeftLoc();
1294 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
1295 return DRE->getLocation();
1296 if (MemberExpr *Member = dyn_cast<MemberExpr>(E))
1297 return Member->getMemberLoc();
1298 if (ObjCIvarRefExpr *Ivar = dyn_cast<ObjCIvarRefExpr>(E))
1299 return Ivar->getLocation();
1300 return E->getLocStart();
1301}
1302
Ted Kremenekfb480492010-01-13 21:46:36 +00001303extern "C" {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001304
1305unsigned clang_visitChildren(CXCursor parent,
Douglas Gregorb1373d02010-01-20 20:59:29 +00001306 CXCursorVisitor visitor,
1307 CXClientData client_data) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001308 ASTUnit *CXXUnit = getCursorASTUnit(parent);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001309
1310 unsigned PCHLevel = Decl::MaxPCHLevel;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001311
Douglas Gregorb1373d02010-01-20 20:59:29 +00001312 // Set the PCHLevel to filter out unwanted decls if requested.
1313 if (CXXUnit->getOnlyLocalDecls()) {
1314 PCHLevel = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001315
Douglas Gregorb1373d02010-01-20 20:59:29 +00001316 // If the main input was an AST, bump the level.
1317 if (CXXUnit->isMainFileAST())
1318 ++PCHLevel;
1319 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001320
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001321 CursorVisitor CursorVis(CXXUnit, visitor, client_data, PCHLevel);
Douglas Gregorb1373d02010-01-20 20:59:29 +00001322 return CursorVis.VisitChildren(parent);
1323}
1324
Douglas Gregor78205d42010-01-20 21:45:58 +00001325static CXString getDeclSpelling(Decl *D) {
1326 NamedDecl *ND = dyn_cast_or_null<NamedDecl>(D);
1327 if (!ND)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001328 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001329
Douglas Gregor78205d42010-01-20 21:45:58 +00001330 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(ND))
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001331 return createCXString(OMD->getSelector().getAsString());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001332
Douglas Gregor78205d42010-01-20 21:45:58 +00001333 if (ObjCCategoryImplDecl *CIMP = dyn_cast<ObjCCategoryImplDecl>(ND))
1334 // No, this isn't the same as the code below. getIdentifier() is non-virtual
1335 // and returns different names. NamedDecl returns the class name and
1336 // ObjCCategoryImplDecl returns the category name.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001337 return createCXString(CIMP->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001338
Douglas Gregor78205d42010-01-20 21:45:58 +00001339 if (ND->getIdentifier())
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001340 return createCXString(ND->getIdentifier()->getNameStart());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001341
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001342 return createCXString("");
Douglas Gregor78205d42010-01-20 21:45:58 +00001343}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001344
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001345CXString clang_getCursorSpelling(CXCursor C) {
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001346 if (clang_isTranslationUnit(C.kind))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001347 return clang_getTranslationUnitSpelling(C.data[2]);
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001348
Steve Narofff334b4e2009-09-02 18:26:48 +00001349 if (clang_isReference(C.kind)) {
1350 switch (C.kind) {
Daniel Dunbaracca7252009-11-30 20:42:49 +00001351 case CXCursor_ObjCSuperClassRef: {
Douglas Gregor2e331b92010-01-16 14:00:32 +00001352 ObjCInterfaceDecl *Super = getCursorObjCSuperClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001353 return createCXString(Super->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001354 }
1355 case CXCursor_ObjCClassRef: {
Douglas Gregor1adb0822010-01-16 17:14:40 +00001356 ObjCInterfaceDecl *Class = getCursorObjCClassRef(C).first;
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001357 return createCXString(Class->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001358 }
1359 case CXCursor_ObjCProtocolRef: {
Douglas Gregor78db0cd2010-01-16 15:44:18 +00001360 ObjCProtocolDecl *OID = getCursorObjCProtocolRef(C).first;
Douglas Gregorf46034a2010-01-18 23:41:10 +00001361 assert(OID && "getCursorSpelling(): Missing protocol decl");
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001362 return createCXString(OID->getIdentifier()->getNameStart());
Daniel Dunbaracca7252009-11-30 20:42:49 +00001363 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001364 case CXCursor_TypeRef: {
1365 TypeDecl *Type = getCursorTypeRef(C).first;
1366 assert(Type && "Missing type decl");
1367
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001368 return createCXString(getCursorContext(C).getTypeDeclType(Type).
1369 getAsString());
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001370 }
1371
Daniel Dunbaracca7252009-11-30 20:42:49 +00001372 default:
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001373 return createCXString("<not implemented>");
Steve Narofff334b4e2009-09-02 18:26:48 +00001374 }
1375 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001376
1377 if (clang_isExpression(C.kind)) {
1378 Decl *D = getDeclFromExpr(getCursorExpr(C));
1379 if (D)
Douglas Gregor78205d42010-01-20 21:45:58 +00001380 return getDeclSpelling(D);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001381 return createCXString("");
Douglas Gregor97b98722010-01-19 23:20:36 +00001382 }
1383
Douglas Gregor60cbfac2010-01-25 16:56:17 +00001384 if (clang_isDeclaration(C.kind))
1385 return getDeclSpelling(getCursorDecl(C));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001386
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001387 return createCXString("");
Steve Narofff334b4e2009-09-02 18:26:48 +00001388}
1389
Ted Kremeneke68fff62010-02-17 00:41:32 +00001390CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) {
Steve Naroff89922f82009-08-31 00:59:03 +00001391 switch (Kind) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001392 case CXCursor_FunctionDecl:
1393 return createCXString("FunctionDecl");
1394 case CXCursor_TypedefDecl:
1395 return createCXString("TypedefDecl");
1396 case CXCursor_EnumDecl:
1397 return createCXString("EnumDecl");
1398 case CXCursor_EnumConstantDecl:
1399 return createCXString("EnumConstantDecl");
1400 case CXCursor_StructDecl:
1401 return createCXString("StructDecl");
1402 case CXCursor_UnionDecl:
1403 return createCXString("UnionDecl");
1404 case CXCursor_ClassDecl:
1405 return createCXString("ClassDecl");
1406 case CXCursor_FieldDecl:
1407 return createCXString("FieldDecl");
1408 case CXCursor_VarDecl:
1409 return createCXString("VarDecl");
1410 case CXCursor_ParmDecl:
1411 return createCXString("ParmDecl");
1412 case CXCursor_ObjCInterfaceDecl:
1413 return createCXString("ObjCInterfaceDecl");
1414 case CXCursor_ObjCCategoryDecl:
1415 return createCXString("ObjCCategoryDecl");
1416 case CXCursor_ObjCProtocolDecl:
1417 return createCXString("ObjCProtocolDecl");
1418 case CXCursor_ObjCPropertyDecl:
1419 return createCXString("ObjCPropertyDecl");
1420 case CXCursor_ObjCIvarDecl:
1421 return createCXString("ObjCIvarDecl");
1422 case CXCursor_ObjCInstanceMethodDecl:
1423 return createCXString("ObjCInstanceMethodDecl");
1424 case CXCursor_ObjCClassMethodDecl:
1425 return createCXString("ObjCClassMethodDecl");
1426 case CXCursor_ObjCImplementationDecl:
1427 return createCXString("ObjCImplementationDecl");
1428 case CXCursor_ObjCCategoryImplDecl:
1429 return createCXString("ObjCCategoryImplDecl");
1430 case CXCursor_UnexposedDecl:
1431 return createCXString("UnexposedDecl");
1432 case CXCursor_ObjCSuperClassRef:
1433 return createCXString("ObjCSuperClassRef");
1434 case CXCursor_ObjCProtocolRef:
1435 return createCXString("ObjCProtocolRef");
1436 case CXCursor_ObjCClassRef:
1437 return createCXString("ObjCClassRef");
1438 case CXCursor_TypeRef:
1439 return createCXString("TypeRef");
1440 case CXCursor_UnexposedExpr:
1441 return createCXString("UnexposedExpr");
1442 case CXCursor_DeclRefExpr:
1443 return createCXString("DeclRefExpr");
1444 case CXCursor_MemberRefExpr:
1445 return createCXString("MemberRefExpr");
1446 case CXCursor_CallExpr:
1447 return createCXString("CallExpr");
1448 case CXCursor_ObjCMessageExpr:
1449 return createCXString("ObjCMessageExpr");
1450 case CXCursor_UnexposedStmt:
1451 return createCXString("UnexposedStmt");
1452 case CXCursor_InvalidFile:
1453 return createCXString("InvalidFile");
1454 case CXCursor_NoDeclFound:
1455 return createCXString("NoDeclFound");
1456 case CXCursor_NotImplemented:
1457 return createCXString("NotImplemented");
1458 case CXCursor_TranslationUnit:
1459 return createCXString("TranslationUnit");
Ted Kremeneke77f4432010-02-18 03:09:07 +00001460 case CXCursor_UnexposedAttr:
1461 return createCXString("UnexposedAttr");
1462 case CXCursor_IBActionAttr:
1463 return createCXString("attribute(ibaction)");
1464 case CXCursor_IBOutletAttr:
1465 return createCXString("attribute(iboutlet)");
Steve Naroff89922f82009-08-31 00:59:03 +00001466 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001467
Ted Kremenekdeb06bd2010-01-16 02:02:09 +00001468 llvm_unreachable("Unhandled CXCursorKind");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001469 return createCXString(NULL);
Steve Naroff600866c2009-08-27 19:51:58 +00001470}
Steve Naroff89922f82009-08-31 00:59:03 +00001471
Ted Kremeneke68fff62010-02-17 00:41:32 +00001472enum CXChildVisitResult GetCursorVisitor(CXCursor cursor,
1473 CXCursor parent,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001474 CXClientData client_data) {
1475 CXCursor *BestCursor = static_cast<CXCursor *>(client_data);
1476 *BestCursor = cursor;
1477 return CXChildVisit_Recurse;
1478}
Ted Kremeneke68fff62010-02-17 00:41:32 +00001479
Douglas Gregorb9790342010-01-22 21:44:22 +00001480CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) {
1481 if (!TU)
Ted Kremenekf4629892010-01-14 01:51:23 +00001482 return clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001483
Douglas Gregorb9790342010-01-22 21:44:22 +00001484 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1485
Ted Kremeneka297de22010-01-25 22:34:44 +00001486 SourceLocation SLoc = cxloc::translateSourceLocation(Loc);
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001487 CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound);
1488 if (SLoc.isValid()) {
Daniel Dunbard52864b2010-02-14 10:02:57 +00001489 SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1));
Ted Kremeneke68fff62010-02-17 00:41:32 +00001490
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001491 // FIXME: Would be great to have a "hint" cursor, then walk from that
1492 // hint cursor upward until we find a cursor whose source range encloses
1493 // the region of interest, rather than starting from the translation unit.
1494 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001495 CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result,
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001496 Decl::MaxPCHLevel, RegionOfInterest);
1497 CursorVis.VisitChildren(Parent);
Steve Naroff77128dd2009-09-15 20:25:34 +00001498 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001499 return Result;
Steve Naroff600866c2009-08-27 19:51:58 +00001500}
1501
Ted Kremenek73885552009-11-17 19:28:59 +00001502CXCursor clang_getNullCursor(void) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +00001503 return MakeCXCursorInvalid(CXCursor_InvalidFile);
Ted Kremenek73885552009-11-17 19:28:59 +00001504}
1505
1506unsigned clang_equalCursors(CXCursor X, CXCursor Y) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001507 return X == Y;
Ted Kremenek73885552009-11-17 19:28:59 +00001508}
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001509
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001510unsigned clang_isInvalid(enum CXCursorKind K) {
Steve Naroff77128dd2009-09-15 20:25:34 +00001511 return K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid;
1512}
1513
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001514unsigned clang_isDeclaration(enum CXCursorKind K) {
Steve Naroff89922f82009-08-31 00:59:03 +00001515 return K >= CXCursor_FirstDecl && K <= CXCursor_LastDecl;
1516}
Steve Naroff2d4d6292009-08-31 14:26:51 +00001517
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001518unsigned clang_isReference(enum CXCursorKind K) {
Steve Narofff334b4e2009-09-02 18:26:48 +00001519 return K >= CXCursor_FirstRef && K <= CXCursor_LastRef;
1520}
1521
Douglas Gregor97b98722010-01-19 23:20:36 +00001522unsigned clang_isExpression(enum CXCursorKind K) {
1523 return K >= CXCursor_FirstExpr && K <= CXCursor_LastExpr;
1524}
1525
1526unsigned clang_isStatement(enum CXCursorKind K) {
1527 return K >= CXCursor_FirstStmt && K <= CXCursor_LastStmt;
1528}
1529
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +00001530unsigned clang_isTranslationUnit(enum CXCursorKind K) {
1531 return K == CXCursor_TranslationUnit;
1532}
1533
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001534CXCursorKind clang_getCursorKind(CXCursor C) {
Steve Naroff9efa7672009-09-04 15:44:05 +00001535 return C.kind;
1536}
1537
Douglas Gregor98258af2010-01-18 22:46:11 +00001538CXSourceLocation clang_getCursorLocation(CXCursor C) {
1539 if (clang_isReference(C.kind)) {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001540 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001541 case CXCursor_ObjCSuperClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001542 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1543 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001544 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001545 }
1546
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001547 case CXCursor_ObjCProtocolRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001548 std::pair<ObjCProtocolDecl *, SourceLocation> P
1549 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001550 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001551 }
1552
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001553 case CXCursor_ObjCClassRef: {
Douglas Gregorf46034a2010-01-18 23:41:10 +00001554 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1555 = getCursorObjCClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001556 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001557 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001558
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001559 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001560 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001561 return cxloc::translateSourceLocation(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001562 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001563
Douglas Gregorf46034a2010-01-18 23:41:10 +00001564 default:
1565 // FIXME: Need a way to enumerate all non-reference cases.
1566 llvm_unreachable("Missed a reference kind");
1567 }
Douglas Gregor98258af2010-01-18 22:46:11 +00001568 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001569
1570 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001571 return cxloc::translateSourceLocation(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001572 getLocationFromExpr(getCursorExpr(C)));
1573
Douglas Gregor5352ac02010-01-28 00:27:43 +00001574 if (!getCursorDecl(C))
1575 return clang_getNullLocation();
Douglas Gregor98258af2010-01-18 22:46:11 +00001576
Douglas Gregorf46034a2010-01-18 23:41:10 +00001577 Decl *D = getCursorDecl(C);
Douglas Gregorf46034a2010-01-18 23:41:10 +00001578 SourceLocation Loc = D->getLocation();
1579 if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(D))
1580 Loc = Class->getClassLoc();
Ted Kremeneka297de22010-01-25 22:34:44 +00001581 return cxloc::translateSourceLocation(D->getASTContext(), Loc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001582}
Douglas Gregora7bde202010-01-19 00:34:46 +00001583
1584CXSourceRange clang_getCursorExtent(CXCursor C) {
1585 if (clang_isReference(C.kind)) {
1586 switch (C.kind) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001587 case CXCursor_ObjCSuperClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001588 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1589 = getCursorObjCSuperClassRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001590 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001591 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001592
1593 case CXCursor_ObjCProtocolRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001594 std::pair<ObjCProtocolDecl *, SourceLocation> P
1595 = getCursorObjCProtocolRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001596 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001597 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001598
1599 case CXCursor_ObjCClassRef: {
Douglas Gregora7bde202010-01-19 00:34:46 +00001600 std::pair<ObjCInterfaceDecl *, SourceLocation> P
1601 = getCursorObjCClassRef(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001602
Ted Kremeneka297de22010-01-25 22:34:44 +00001603 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregora7bde202010-01-19 00:34:46 +00001604 }
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001605
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001606 case CXCursor_TypeRef: {
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001607 std::pair<TypeDecl *, SourceLocation> P = getCursorTypeRef(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001608 return cxloc::translateSourceRange(P.first->getASTContext(), P.second);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001609 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001610
Douglas Gregora7bde202010-01-19 00:34:46 +00001611 default:
1612 // FIXME: Need a way to enumerate all non-reference cases.
1613 llvm_unreachable("Missed a reference kind");
1614 }
1615 }
Douglas Gregor97b98722010-01-19 23:20:36 +00001616
1617 if (clang_isExpression(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001618 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor97b98722010-01-19 23:20:36 +00001619 getCursorExpr(C)->getSourceRange());
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001620
1621 if (clang_isStatement(C.kind))
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001622 return cxloc::translateSourceRange(getCursorContext(C),
Douglas Gregor33e9abd2010-01-22 19:49:59 +00001623 getCursorStmt(C)->getSourceRange());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001624
Douglas Gregor5352ac02010-01-28 00:27:43 +00001625 if (!getCursorDecl(C))
1626 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001627
Douglas Gregora7bde202010-01-19 00:34:46 +00001628 Decl *D = getCursorDecl(C);
Ted Kremeneka297de22010-01-25 22:34:44 +00001629 return cxloc::translateSourceRange(D->getASTContext(), D->getSourceRange());
Douglas Gregora7bde202010-01-19 00:34:46 +00001630}
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001631
1632CXCursor clang_getCursorReferenced(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001633 if (clang_isInvalid(C.kind))
1634 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001635
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001636 ASTUnit *CXXUnit = getCursorASTUnit(C);
Douglas Gregorb6998662010-01-19 19:34:47 +00001637 if (clang_isDeclaration(C.kind))
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001638 return C;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001639
Douglas Gregor97b98722010-01-19 23:20:36 +00001640 if (clang_isExpression(C.kind)) {
1641 Decl *D = getDeclFromExpr(getCursorExpr(C));
1642 if (D)
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001643 return MakeCXCursor(D, CXXUnit);
Douglas Gregor97b98722010-01-19 23:20:36 +00001644 return clang_getNullCursor();
1645 }
1646
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001647 if (!clang_isReference(C.kind))
1648 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001649
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001650 switch (C.kind) {
1651 case CXCursor_ObjCSuperClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001652 return MakeCXCursor(getCursorObjCSuperClassRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001653
1654 case CXCursor_ObjCProtocolRef: {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001655 return MakeCXCursor(getCursorObjCProtocolRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001656
1657 case CXCursor_ObjCClassRef:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001658 return MakeCXCursor(getCursorObjCClassRef(C).first, CXXUnit);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001659
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001660 case CXCursor_TypeRef:
Douglas Gregor7d0d40e2010-01-21 16:28:34 +00001661 return MakeCXCursor(getCursorTypeRef(C).first, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001662
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001663 default:
1664 // We would prefer to enumerate all non-reference cursor kinds here.
1665 llvm_unreachable("Unhandled reference cursor kind");
1666 break;
1667 }
1668 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001669
Douglas Gregorc5d1e932010-01-19 01:20:04 +00001670 return clang_getNullCursor();
1671}
1672
Douglas Gregorb6998662010-01-19 19:34:47 +00001673CXCursor clang_getCursorDefinition(CXCursor C) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001674 if (clang_isInvalid(C.kind))
1675 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001676
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001677 ASTUnit *CXXUnit = getCursorASTUnit(C);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001678
Douglas Gregorb6998662010-01-19 19:34:47 +00001679 bool WasReference = false;
Douglas Gregor97b98722010-01-19 23:20:36 +00001680 if (clang_isReference(C.kind) || clang_isExpression(C.kind)) {
Douglas Gregorb6998662010-01-19 19:34:47 +00001681 C = clang_getCursorReferenced(C);
1682 WasReference = true;
1683 }
1684
1685 if (!clang_isDeclaration(C.kind))
1686 return clang_getNullCursor();
1687
1688 Decl *D = getCursorDecl(C);
1689 if (!D)
1690 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001691
Douglas Gregorb6998662010-01-19 19:34:47 +00001692 switch (D->getKind()) {
1693 // Declaration kinds that don't really separate the notions of
1694 // declaration and definition.
1695 case Decl::Namespace:
1696 case Decl::Typedef:
1697 case Decl::TemplateTypeParm:
1698 case Decl::EnumConstant:
1699 case Decl::Field:
1700 case Decl::ObjCIvar:
1701 case Decl::ObjCAtDefsField:
1702 case Decl::ImplicitParam:
1703 case Decl::ParmVar:
1704 case Decl::NonTypeTemplateParm:
1705 case Decl::TemplateTemplateParm:
1706 case Decl::ObjCCategoryImpl:
1707 case Decl::ObjCImplementation:
1708 case Decl::LinkageSpec:
1709 case Decl::ObjCPropertyImpl:
1710 case Decl::FileScopeAsm:
1711 case Decl::StaticAssert:
1712 case Decl::Block:
1713 return C;
1714
1715 // Declaration kinds that don't make any sense here, but are
1716 // nonetheless harmless.
1717 case Decl::TranslationUnit:
1718 case Decl::Template:
1719 case Decl::ObjCContainer:
1720 break;
1721
1722 // Declaration kinds for which the definition is not resolvable.
1723 case Decl::UnresolvedUsingTypename:
1724 case Decl::UnresolvedUsingValue:
1725 break;
1726
1727 case Decl::UsingDirective:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001728 return MakeCXCursor(cast<UsingDirectiveDecl>(D)->getNominatedNamespace(),
1729 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001730
1731 case Decl::NamespaceAlias:
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001732 return MakeCXCursor(cast<NamespaceAliasDecl>(D)->getNamespace(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001733
1734 case Decl::Enum:
1735 case Decl::Record:
1736 case Decl::CXXRecord:
1737 case Decl::ClassTemplateSpecialization:
1738 case Decl::ClassTemplatePartialSpecialization:
Douglas Gregor952b0172010-02-11 01:04:33 +00001739 if (TagDecl *Def = cast<TagDecl>(D)->getDefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001740 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001741 return clang_getNullCursor();
1742
1743 case Decl::Function:
1744 case Decl::CXXMethod:
1745 case Decl::CXXConstructor:
1746 case Decl::CXXDestructor:
1747 case Decl::CXXConversion: {
1748 const FunctionDecl *Def = 0;
1749 if (cast<FunctionDecl>(D)->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001750 return MakeCXCursor(const_cast<FunctionDecl *>(Def), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001751 return clang_getNullCursor();
1752 }
1753
1754 case Decl::Var: {
Sebastian Redl31310a22010-02-01 20:16:42 +00001755 // Ask the variable if it has a definition.
1756 if (VarDecl *Def = cast<VarDecl>(D)->getDefinition())
1757 return MakeCXCursor(Def, CXXUnit);
1758 return clang_getNullCursor();
Douglas Gregorb6998662010-01-19 19:34:47 +00001759 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001760
Douglas Gregorb6998662010-01-19 19:34:47 +00001761 case Decl::FunctionTemplate: {
1762 const FunctionDecl *Def = 0;
1763 if (cast<FunctionTemplateDecl>(D)->getTemplatedDecl()->getBody(Def))
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001764 return MakeCXCursor(Def->getDescribedFunctionTemplate(), CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001765 return clang_getNullCursor();
1766 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001767
Douglas Gregorb6998662010-01-19 19:34:47 +00001768 case Decl::ClassTemplate: {
1769 if (RecordDecl *Def = cast<ClassTemplateDecl>(D)->getTemplatedDecl()
Douglas Gregor952b0172010-02-11 01:04:33 +00001770 ->getDefinition())
Douglas Gregorb6998662010-01-19 19:34:47 +00001771 return MakeCXCursor(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001772 cast<CXXRecordDecl>(Def)->getDescribedClassTemplate(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001773 CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001774 return clang_getNullCursor();
1775 }
1776
1777 case Decl::Using: {
1778 UsingDecl *Using = cast<UsingDecl>(D);
1779 CXCursor Def = clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001780 for (UsingDecl::shadow_iterator S = Using->shadow_begin(),
1781 SEnd = Using->shadow_end();
Douglas Gregorb6998662010-01-19 19:34:47 +00001782 S != SEnd; ++S) {
1783 if (Def != clang_getNullCursor()) {
1784 // FIXME: We have no way to return multiple results.
1785 return clang_getNullCursor();
1786 }
1787
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001788 Def = clang_getCursorDefinition(MakeCXCursor((*S)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001789 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001790 }
1791
1792 return Def;
1793 }
1794
1795 case Decl::UsingShadow:
1796 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001797 MakeCXCursor(cast<UsingShadowDecl>(D)->getTargetDecl(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001798 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001799
1800 case Decl::ObjCMethod: {
1801 ObjCMethodDecl *Method = cast<ObjCMethodDecl>(D);
1802 if (Method->isThisDeclarationADefinition())
1803 return C;
1804
1805 // Dig out the method definition in the associated
1806 // @implementation, if we have it.
1807 // FIXME: The ASTs should make finding the definition easier.
1808 if (ObjCInterfaceDecl *Class
1809 = dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()))
1810 if (ObjCImplementationDecl *ClassImpl = Class->getImplementation())
1811 if (ObjCMethodDecl *Def = ClassImpl->getMethod(Method->getSelector(),
1812 Method->isInstanceMethod()))
1813 if (Def->isThisDeclarationADefinition())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001814 return MakeCXCursor(Def, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001815
1816 return clang_getNullCursor();
1817 }
1818
1819 case Decl::ObjCCategory:
1820 if (ObjCCategoryImplDecl *Impl
1821 = cast<ObjCCategoryDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001822 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001823 return clang_getNullCursor();
1824
1825 case Decl::ObjCProtocol:
1826 if (!cast<ObjCProtocolDecl>(D)->isForwardDecl())
1827 return C;
1828 return clang_getNullCursor();
1829
1830 case Decl::ObjCInterface:
1831 // There are two notions of a "definition" for an Objective-C
1832 // class: the interface and its implementation. When we resolved a
1833 // reference to an Objective-C class, produce the @interface as
1834 // the definition; when we were provided with the interface,
1835 // produce the @implementation as the definition.
1836 if (WasReference) {
1837 if (!cast<ObjCInterfaceDecl>(D)->isForwardDecl())
1838 return C;
1839 } else if (ObjCImplementationDecl *Impl
1840 = cast<ObjCInterfaceDecl>(D)->getImplementation())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001841 return MakeCXCursor(Impl, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001842 return clang_getNullCursor();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001843
Douglas Gregorb6998662010-01-19 19:34:47 +00001844 case Decl::ObjCProperty:
1845 // FIXME: We don't really know where to find the
1846 // ObjCPropertyImplDecls that implement this property.
1847 return clang_getNullCursor();
1848
1849 case Decl::ObjCCompatibleAlias:
1850 if (ObjCInterfaceDecl *Class
1851 = cast<ObjCCompatibleAliasDecl>(D)->getClassInterface())
1852 if (!Class->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001853 return MakeCXCursor(Class, CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001854
Douglas Gregorb6998662010-01-19 19:34:47 +00001855 return clang_getNullCursor();
1856
1857 case Decl::ObjCForwardProtocol: {
1858 ObjCForwardProtocolDecl *Forward = cast<ObjCForwardProtocolDecl>(D);
1859 if (Forward->protocol_size() == 1)
1860 return clang_getCursorDefinition(
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001861 MakeCXCursor(*Forward->protocol_begin(),
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001862 CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001863
1864 // FIXME: Cannot return multiple definitions.
1865 return clang_getNullCursor();
1866 }
1867
1868 case Decl::ObjCClass: {
1869 ObjCClassDecl *Class = cast<ObjCClassDecl>(D);
1870 if (Class->size() == 1) {
1871 ObjCInterfaceDecl *IFace = Class->begin()->getInterface();
1872 if (!IFace->isForwardDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001873 return MakeCXCursor(IFace, CXXUnit);
Douglas Gregorb6998662010-01-19 19:34:47 +00001874 return clang_getNullCursor();
1875 }
1876
1877 // FIXME: Cannot return multiple definitions.
1878 return clang_getNullCursor();
1879 }
1880
1881 case Decl::Friend:
1882 if (NamedDecl *Friend = cast<FriendDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001883 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001884 return clang_getNullCursor();
1885
1886 case Decl::FriendTemplate:
1887 if (NamedDecl *Friend = cast<FriendTemplateDecl>(D)->getFriendDecl())
Douglas Gregorb2cd4872010-01-20 23:57:43 +00001888 return clang_getCursorDefinition(MakeCXCursor(Friend, CXXUnit));
Douglas Gregorb6998662010-01-19 19:34:47 +00001889 return clang_getNullCursor();
1890 }
1891
1892 return clang_getNullCursor();
1893}
1894
1895unsigned clang_isCursorDefinition(CXCursor C) {
1896 if (!clang_isDeclaration(C.kind))
1897 return 0;
1898
1899 return clang_getCursorDefinition(C) == C;
1900}
1901
Daniel Dunbar0d7dd222009-11-30 20:42:43 +00001902void clang_getDefinitionSpellingAndExtent(CXCursor C,
Steve Naroff4ade6d62009-09-23 17:52:52 +00001903 const char **startBuf,
1904 const char **endBuf,
1905 unsigned *startLine,
1906 unsigned *startColumn,
1907 unsigned *endLine,
Daniel Dunbar9ebfa312009-12-01 03:14:51 +00001908 unsigned *endColumn) {
Douglas Gregor283cae32010-01-15 21:56:13 +00001909 assert(getCursorDecl(C) && "CXCursor has null decl");
1910 NamedDecl *ND = static_cast<NamedDecl *>(getCursorDecl(C));
Steve Naroff4ade6d62009-09-23 17:52:52 +00001911 FunctionDecl *FD = dyn_cast<FunctionDecl>(ND);
1912 CompoundStmt *Body = dyn_cast<CompoundStmt>(FD->getBody());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001913
Steve Naroff4ade6d62009-09-23 17:52:52 +00001914 SourceManager &SM = FD->getASTContext().getSourceManager();
1915 *startBuf = SM.getCharacterData(Body->getLBracLoc());
1916 *endBuf = SM.getCharacterData(Body->getRBracLoc());
1917 *startLine = SM.getSpellingLineNumber(Body->getLBracLoc());
1918 *startColumn = SM.getSpellingColumnNumber(Body->getLBracLoc());
1919 *endLine = SM.getSpellingLineNumber(Body->getRBracLoc());
1920 *endColumn = SM.getSpellingColumnNumber(Body->getRBracLoc());
1921}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001922
Ted Kremenekfb480492010-01-13 21:46:36 +00001923} // end: extern "C"
Steve Naroff4ade6d62009-09-23 17:52:52 +00001924
Ted Kremenekfb480492010-01-13 21:46:36 +00001925//===----------------------------------------------------------------------===//
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001926// Token-based Operations.
1927//===----------------------------------------------------------------------===//
1928
1929/* CXToken layout:
1930 * int_data[0]: a CXTokenKind
1931 * int_data[1]: starting token location
1932 * int_data[2]: token length
1933 * int_data[3]: reserved
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001934 * ptr_data: for identifiers and keywords, an IdentifierInfo*.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001935 * otherwise unused.
1936 */
1937extern "C" {
1938
1939CXTokenKind clang_getTokenKind(CXToken CXTok) {
1940 return static_cast<CXTokenKind>(CXTok.int_data[0]);
1941}
1942
1943CXString clang_getTokenSpelling(CXTranslationUnit TU, CXToken CXTok) {
1944 switch (clang_getTokenKind(CXTok)) {
1945 case CXToken_Identifier:
1946 case CXToken_Keyword:
1947 // We know we have an IdentifierInfo*, so use that.
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001948 return createCXString(static_cast<IdentifierInfo *>(CXTok.ptr_data)
1949 ->getNameStart());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001950
1951 case CXToken_Literal: {
1952 // We have stashed the starting pointer in the ptr_data field. Use it.
1953 const char *Text = static_cast<const char *>(CXTok.ptr_data);
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001954 return createCXString(llvm::StringRef(Text, CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001955 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001956
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001957 case CXToken_Punctuation:
1958 case CXToken_Comment:
1959 break;
1960 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001961
1962 // We have to find the starting buffer pointer the hard way, by
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001963 // deconstructing the source location.
1964 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1965 if (!CXXUnit)
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001966 return createCXString("");
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001967
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001968 SourceLocation Loc = SourceLocation::getFromRawEncoding(CXTok.int_data[1]);
1969 std::pair<FileID, unsigned> LocInfo
1970 = CXXUnit->getSourceManager().getDecomposedLoc(Loc);
1971 std::pair<const char *,const char *> Buffer
1972 = CXXUnit->getSourceManager().getBufferData(LocInfo.first);
1973
Ted Kremenekee4db4f2010-02-17 00:41:08 +00001974 return createCXString(llvm::StringRef(Buffer.first+LocInfo.second,
1975 CXTok.int_data[2]));
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001976}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001977
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001978CXSourceLocation clang_getTokenLocation(CXTranslationUnit TU, CXToken CXTok) {
1979 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
1980 if (!CXXUnit)
1981 return clang_getNullLocation();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001982
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001983 return cxloc::translateSourceLocation(CXXUnit->getASTContext(),
1984 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1985}
1986
1987CXSourceRange clang_getTokenExtent(CXTranslationUnit TU, CXToken CXTok) {
1988 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
Douglas Gregor5352ac02010-01-28 00:27:43 +00001989 if (!CXXUnit)
1990 return clang_getNullRange();
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001991
1992 return cxloc::translateSourceRange(CXXUnit->getASTContext(),
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001993 SourceLocation::getFromRawEncoding(CXTok.int_data[1]));
1994}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00001995
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001996void clang_tokenize(CXTranslationUnit TU, CXSourceRange Range,
1997 CXToken **Tokens, unsigned *NumTokens) {
1998 if (Tokens)
1999 *Tokens = 0;
2000 if (NumTokens)
2001 *NumTokens = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002002
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002003 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2004 if (!CXXUnit || !Tokens || !NumTokens)
2005 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002006
Daniel Dunbar85b988f2010-02-14 08:31:57 +00002007 SourceRange R = cxloc::translateCXSourceRange(Range);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002008 if (R.isInvalid())
2009 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002010
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002011 SourceManager &SourceMgr = CXXUnit->getSourceManager();
2012 std::pair<FileID, unsigned> BeginLocInfo
2013 = SourceMgr.getDecomposedLoc(R.getBegin());
2014 std::pair<FileID, unsigned> EndLocInfo
2015 = SourceMgr.getDecomposedLoc(R.getEnd());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002016
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002017 // Cannot tokenize across files.
2018 if (BeginLocInfo.first != EndLocInfo.first)
2019 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002020
2021 // Create a lexer
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002022 std::pair<const char *,const char *> Buffer
2023 = SourceMgr.getBufferData(BeginLocInfo.first);
2024 Lexer Lex(SourceMgr.getLocForStartOfFile(BeginLocInfo.first),
2025 CXXUnit->getASTContext().getLangOptions(),
2026 Buffer.first, Buffer.first + BeginLocInfo.second, Buffer.second);
2027 Lex.SetCommentRetentionState(true);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002028
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002029 // Lex tokens until we hit the end of the range.
2030 const char *EffectiveBufferEnd = Buffer.first + EndLocInfo.second;
2031 llvm::SmallVector<CXToken, 32> CXTokens;
2032 Token Tok;
2033 do {
2034 // Lex the next token
2035 Lex.LexFromRawLexer(Tok);
2036 if (Tok.is(tok::eof))
2037 break;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002038
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002039 // Initialize the CXToken.
2040 CXToken CXTok;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002041
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002042 // - Common fields
2043 CXTok.int_data[1] = Tok.getLocation().getRawEncoding();
2044 CXTok.int_data[2] = Tok.getLength();
2045 CXTok.int_data[3] = 0;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002046
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002047 // - Kind-specific fields
2048 if (Tok.isLiteral()) {
2049 CXTok.int_data[0] = CXToken_Literal;
2050 CXTok.ptr_data = (void *)Tok.getLiteralData();
2051 } else if (Tok.is(tok::identifier)) {
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002052 // Lookup the identifier to determine whether we have a
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002053 std::pair<FileID, unsigned> LocInfo
2054 = SourceMgr.getDecomposedLoc(Tok.getLocation());
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002055 const char *StartPos
2056 = CXXUnit->getSourceManager().getBufferData(LocInfo.first).first +
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002057 LocInfo.second;
2058 IdentifierInfo *II
2059 = CXXUnit->getPreprocessor().LookUpIdentifierInfo(Tok, StartPos);
2060 CXTok.int_data[0] = II->getTokenID() == tok::identifier?
2061 CXToken_Identifier
2062 : CXToken_Keyword;
2063 CXTok.ptr_data = II;
2064 } else if (Tok.is(tok::comment)) {
2065 CXTok.int_data[0] = CXToken_Comment;
2066 CXTok.ptr_data = 0;
2067 } else {
2068 CXTok.int_data[0] = CXToken_Punctuation;
2069 CXTok.ptr_data = 0;
2070 }
2071 CXTokens.push_back(CXTok);
2072 } while (Lex.getBufferLocation() <= EffectiveBufferEnd);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002073
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002074 if (CXTokens.empty())
2075 return;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002076
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002077 *Tokens = (CXToken *)malloc(sizeof(CXToken) * CXTokens.size());
2078 memmove(*Tokens, CXTokens.data(), sizeof(CXToken) * CXTokens.size());
2079 *NumTokens = CXTokens.size();
2080}
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002081
2082typedef llvm::DenseMap<unsigned, CXCursor> AnnotateTokensData;
2083
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002084enum CXChildVisitResult AnnotateTokensVisitor(CXCursor cursor,
2085 CXCursor parent,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002086 CXClientData client_data) {
2087 AnnotateTokensData *Data = static_cast<AnnotateTokensData *>(client_data);
2088
2089 // We only annotate the locations of declarations, simple
2090 // references, and expressions which directly reference something.
2091 CXCursorKind Kind = clang_getCursorKind(cursor);
2092 if (clang_isDeclaration(Kind) || clang_isReference(Kind)) {
2093 // Okay: We can annotate the location of this declaration with the
2094 // declaration or reference
2095 } else if (clang_isExpression(cursor.kind)) {
2096 if (Kind != CXCursor_DeclRefExpr &&
2097 Kind != CXCursor_MemberRefExpr &&
2098 Kind != CXCursor_ObjCMessageExpr)
2099 return CXChildVisit_Recurse;
2100
2101 CXCursor Referenced = clang_getCursorReferenced(cursor);
2102 if (Referenced == cursor || Referenced == clang_getNullCursor())
2103 return CXChildVisit_Recurse;
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002104
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002105 // Okay: we can annotate the location of this expression
2106 } else {
2107 // Nothing to annotate
2108 return CXChildVisit_Recurse;
2109 }
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002110
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002111 CXSourceLocation Loc = clang_getCursorLocation(cursor);
2112 (*Data)[Loc.int_data] = cursor;
2113 return CXChildVisit_Recurse;
2114}
2115
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002116void clang_annotateTokens(CXTranslationUnit TU,
2117 CXToken *Tokens, unsigned NumTokens,
2118 CXCursor *Cursors) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002119 if (NumTokens == 0)
2120 return;
2121
2122 // Any token we don't specifically annotate will have a NULL cursor.
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002123 for (unsigned I = 0; I != NumTokens; ++I)
2124 Cursors[I] = clang_getNullCursor();
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002125
2126 ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU);
2127 if (!CXXUnit || !Tokens)
2128 return;
2129
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002130 // Annotate all of the source locations in the region of interest that map
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002131 SourceRange RegionOfInterest;
2132 RegionOfInterest.setBegin(
2133 cxloc::translateSourceLocation(clang_getTokenLocation(TU, Tokens[0])));
2134 SourceLocation End
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002135 = cxloc::translateSourceLocation(clang_getTokenLocation(TU,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002136 Tokens[NumTokens - 1]));
Daniel Dunbard52864b2010-02-14 10:02:57 +00002137 RegionOfInterest.setEnd(CXXUnit->getPreprocessor().getLocForEndOfToken(End));
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002138 // FIXME: Would be great to have a "hint" cursor, then walk from that
2139 // hint cursor upward until we find a cursor whose source range encloses
2140 // the region of interest, rather than starting from the translation unit.
2141 AnnotateTokensData Annotated;
2142 CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit);
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002143 CursorVisitor AnnotateVis(CXXUnit, AnnotateTokensVisitor, &Annotated,
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002144 Decl::MaxPCHLevel, RegionOfInterest);
2145 AnnotateVis.VisitChildren(Parent);
2146
2147 for (unsigned I = 0; I != NumTokens; ++I) {
2148 // Determine whether we saw a cursor at this token's location.
2149 AnnotateTokensData::iterator Pos = Annotated.find(Tokens[I].int_data[1]);
2150 if (Pos == Annotated.end())
2151 continue;
2152
2153 Cursors[I] = Pos->second;
2154 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002155}
2156
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002157void clang_disposeTokens(CXTranslationUnit TU,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002158 CXToken *Tokens, unsigned NumTokens) {
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002159 free(Tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002160}
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002161
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002162} // end: extern "C"
2163
2164//===----------------------------------------------------------------------===//
Ted Kremenekfb480492010-01-13 21:46:36 +00002165// CXString Operations.
2166//===----------------------------------------------------------------------===//
2167
2168extern "C" {
2169const char *clang_getCString(CXString string) {
2170 return string.Spelling;
2171}
2172
2173void clang_disposeString(CXString string) {
2174 if (string.MustFreeString && string.Spelling)
2175 free((void*)string.Spelling);
2176}
Ted Kremenek04bb7162010-01-22 22:44:15 +00002177
Ted Kremenekfb480492010-01-13 21:46:36 +00002178} // end: extern "C"
Ted Kremenek04bb7162010-01-22 22:44:15 +00002179
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002180namespace clang { namespace cxstring {
2181CXString createCXString(const char *String, bool DupString){
2182 CXString Str;
2183 if (DupString) {
2184 Str.Spelling = strdup(String);
2185 Str.MustFreeString = 1;
2186 } else {
2187 Str.Spelling = String;
2188 Str.MustFreeString = 0;
2189 }
2190 return Str;
2191}
2192
2193CXString createCXString(llvm::StringRef String, bool DupString) {
2194 CXString Result;
2195 if (DupString || (!String.empty() && String.data()[String.size()] != 0)) {
2196 char *Spelling = (char *)malloc(String.size() + 1);
2197 memmove(Spelling, String.data(), String.size());
2198 Spelling[String.size()] = 0;
2199 Result.Spelling = Spelling;
2200 Result.MustFreeString = 1;
2201 } else {
2202 Result.Spelling = String.data();
2203 Result.MustFreeString = 0;
2204 }
2205 return Result;
2206}
2207}}
2208
Ted Kremenek04bb7162010-01-22 22:44:15 +00002209//===----------------------------------------------------------------------===//
2210// Misc. utility functions.
2211//===----------------------------------------------------------------------===//
Ted Kremenekf0e23e82010-02-17 00:41:40 +00002212
Ted Kremenek04bb7162010-01-22 22:44:15 +00002213extern "C" {
2214
Ted Kremeneka2a9d6e2010-02-12 22:54:40 +00002215CXString clang_getClangVersion() {
Ted Kremenekee4db4f2010-02-17 00:41:08 +00002216 return createCXString(getClangFullVersion());
Ted Kremenek04bb7162010-01-22 22:44:15 +00002217}
2218
2219} // end: extern "C"